blob: 5f4e4183d868959e7844d29a63ae3ab9f920dfcc [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000043#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000044#include "llvm/ADT/StringExtras.h"
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000045#include "llvm/ADT/VariadicFunction.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000046#include "llvm/Support/CallSite.h"
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +000047#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000048#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000049#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000050#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000053#include "llvm/Target/TargetOptions.h"
Benjamin Kramer9c683542012-01-30 15:16:21 +000054#include <bitset>
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000056using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000057
Evan Chengb1712452010-01-27 06:25:16 +000058STATISTIC(NumTailCalls, "Number of tail calls");
59
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +000060static cl::opt<bool> UseRegMask("x86-use-regmask",
61 cl::desc("Use register masks for x86 calls"));
62
Evan Cheng10e86422008-04-25 19:11:04 +000063// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000064static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000065 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000066
David Greenea5f26012011-02-07 19:36:54 +000067/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
68/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000069/// simple subregister reference. Idx is an index in the 128 bits we
70/// want. It need not be aligned to a 128-bit bounday. That makes
71/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000072static SDValue Extract128BitVector(SDValue Vec,
73 SDValue Idx,
74 SelectionDAG &DAG,
75 DebugLoc dl) {
76 EVT VT = Vec.getValueType();
77 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000078 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000079 int Factor = VT.getSizeInBits()/128;
80 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
81 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000082
83 // Extract from UNDEF is UNDEF.
84 if (Vec.getOpcode() == ISD::UNDEF)
85 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
86
87 if (isa<ConstantSDNode>(Idx)) {
88 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
89
90 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
91 // we can match to VEXTRACTF128.
92 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
93
94 // This is the index of the first element of the 128-bit chunk
95 // we want.
96 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
97 * ElemsPerChunk);
98
99 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000100 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
101 VecIdx);
102
103 return Result;
104 }
105
106 return SDValue();
107}
108
109/// Generate a DAG to put 128-bits into a vector > 128 bits. This
110/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000111/// simple superregister reference. Idx is an index in the 128 bits
112/// we want. It need not be aligned to a 128-bit bounday. That makes
113/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000114static SDValue Insert128BitVector(SDValue Result,
115 SDValue Vec,
116 SDValue Idx,
117 SelectionDAG &DAG,
118 DebugLoc dl) {
119 if (isa<ConstantSDNode>(Idx)) {
120 EVT VT = Vec.getValueType();
121 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
122
123 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000124 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000125 EVT ResultVT = Result.getValueType();
126
127 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000128 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000129
130 // This is the index of the first element of the 128-bit chunk
131 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000132 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000133 * ElemsPerChunk);
134
135 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000136 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
137 VecIdx);
138 return Result;
139 }
140
141 return SDValue();
142}
143
Chris Lattnerf0144122009-07-28 03:13:23 +0000144static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000145 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
146 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000147
Evan Cheng2bffee22011-02-01 01:14:13 +0000148 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000149 if (is64Bit)
150 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000151 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000152 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000153
Evan Cheng203576a2011-07-20 19:50:42 +0000154 if (Subtarget->isTargetELF())
155 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000156 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000157 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000158 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000159}
160
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000161X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000162 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000163 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +0000164 X86ScalarSSEf64 = Subtarget->hasSSE2();
165 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +0000166 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000167
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000168 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000169 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000170
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000171 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000172 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000173
174 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000175 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000176 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
177 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000178
Eric Christopherde5e1012011-03-11 01:05:58 +0000179 // For 64-bit since we have so many registers use the ILP scheduler, for
180 // 32-bit code use the register pressure specific scheduling.
Andrew Trick922d3142012-02-01 23:20:51 +0000181 // For 32 bit Atom, use Hybrid (register pressure + latency) scheduling.
Eric Christopherde5e1012011-03-11 01:05:58 +0000182 if (Subtarget->is64Bit())
183 setSchedulingPreference(Sched::ILP);
Andrew Trick922d3142012-02-01 23:20:51 +0000184 else if (Subtarget->isAtom())
185 setSchedulingPreference(Sched::Hybrid);
Eric Christopherde5e1012011-03-11 01:05:58 +0000186 else
187 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000188 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000189
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000190 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000191 // Setup Windows compiler runtime calls.
192 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000193 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000194 setLibcallName(RTLIB::SREM_I64, "_allrem");
195 setLibcallName(RTLIB::UREM_I64, "_aullrem");
196 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000197 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000198 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000199 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000200 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000201 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
202 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
203 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000204 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
205 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000206 }
207
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000208 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000209 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000210 setUseUnderscoreSetJmp(false);
211 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000212 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000213 // MS runtime is weird: it exports _setjmp, but longjmp!
214 setUseUnderscoreSetJmp(true);
215 setUseUnderscoreLongJmp(false);
216 } else {
217 setUseUnderscoreSetJmp(true);
218 setUseUnderscoreLongJmp(true);
219 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000220
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000221 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000222 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000223 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000224 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000225 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000227
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000229
Scott Michelfdc40a02009-02-17 22:15:04 +0000230 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000232 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000234 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
236 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000237
238 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
240 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
241 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
242 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
243 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
244 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000245
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000246 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
247 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
249 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
250 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000251
Evan Cheng25ab6902006-09-08 06:48:29 +0000252 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000254 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000255 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000256 // We have an algorithm for SSE2->double, and we turn this into a
257 // 64-bit FILD followed by conditional FADD for other targets.
258 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000259 // We have an algorithm for SSE2, and we turn this into a 64-bit
260 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000261 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000262 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000263
264 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
265 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
267 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000268
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000269 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000270 // SSE has no i16 to fp conversion, only i32
271 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000273 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
277 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000278 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000279 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
281 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000282 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000283
Dale Johannesen73328d12007-09-19 23:55:34 +0000284 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
285 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
287 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000288
Evan Cheng02568ff2006-01-30 22:13:22 +0000289 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
290 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
292 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000293
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000294 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000296 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000297 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000298 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
300 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000301 }
302
303 // Handle FP_TO_UINT by promoting the destination to a larger signed
304 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000305 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
306 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
307 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000308
Evan Cheng25ab6902006-09-08 06:48:29 +0000309 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000312 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000313 // Since AVX is a superset of SSE3, only check for SSE here.
314 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000315 // Expand FP_TO_UINT into a select.
316 // FIXME: We would like to use a Custom expander here eventually to do
317 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000319 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000320 // With SSE3 we can use fisttpll to convert to a signed i64; without
321 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000323 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000324
Chris Lattner399610a2006-12-05 18:22:22 +0000325 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000326 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000327 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
328 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000329 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000330 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000331 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000332 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000333 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000334 }
Chris Lattner21f66852005-12-23 05:15:23 +0000335
Dan Gohmanb00ee212008-02-18 19:34:53 +0000336 // Scalar integer divide and remainder are lowered to use operations that
337 // produce two results, to match the available instructions. This exposes
338 // the two-result form to trivial CSE, which is able to combine x/y and x%y
339 // into a single instruction.
340 //
341 // Scalar integer multiply-high is also lowered to use two-result
342 // operations, to match the available instructions. However, plain multiply
343 // (low) operations are left as Legal, as there are single-result
344 // instructions for this in x86. Using the two-result multiply instructions
345 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000346 for (unsigned i = 0, e = 4; i != e; ++i) {
347 MVT VT = IntVTs[i];
348 setOperationAction(ISD::MULHS, VT, Expand);
349 setOperationAction(ISD::MULHU, VT, Expand);
350 setOperationAction(ISD::SDIV, VT, Expand);
351 setOperationAction(ISD::UDIV, VT, Expand);
352 setOperationAction(ISD::SREM, VT, Expand);
353 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000354
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000355 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000356 setOperationAction(ISD::ADDC, VT, Custom);
357 setOperationAction(ISD::ADDE, VT, Custom);
358 setOperationAction(ISD::SUBC, VT, Custom);
359 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000360 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000361
Owen Anderson825b72b2009-08-11 20:47:22 +0000362 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
363 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
364 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
365 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000366 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
368 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
369 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
370 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
371 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
372 setOperationAction(ISD::FREM , MVT::f32 , Expand);
373 setOperationAction(ISD::FREM , MVT::f64 , Expand);
374 setOperationAction(ISD::FREM , MVT::f80 , Expand);
375 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000376
Chandler Carruth77821022011-12-24 12:12:34 +0000377 // Promote the i8 variants and force them on up to i32 which has a shorter
378 // encoding.
379 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
380 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
381 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
382 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000383 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000384 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
385 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
386 if (Subtarget->is64Bit())
387 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000388 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000389 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
390 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
391 if (Subtarget->is64Bit())
392 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
393 }
Craig Topper37f21672011-10-11 06:44:02 +0000394
395 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000396 // When promoting the i8 variants, force them to i32 for a shorter
397 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000398 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000399 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
400 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
401 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000402 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
403 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
404 if (Subtarget->is64Bit())
405 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000406 } else {
407 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
408 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
409 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000410 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
411 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
412 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
413 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000414 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000415 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
416 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000417 }
418
Benjamin Kramer1292c222010-12-04 20:32:23 +0000419 if (Subtarget->hasPOPCNT()) {
420 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
421 } else {
422 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
423 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
424 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
425 if (Subtarget->is64Bit())
426 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
427 }
428
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
430 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000431
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000432 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000433 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000434 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000435 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000436 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
438 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
439 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
440 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
441 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000442 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
444 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
445 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
446 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000447 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000449 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000450 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000451 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000452
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000453 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
455 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
456 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
457 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000458 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
460 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000461 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000462 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
464 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
465 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
466 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000467 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000468 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000469 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000470 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
471 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
472 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000473 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000474 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
475 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
476 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000477 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000478
Craig Topper1accb7e2012-01-10 06:54:16 +0000479 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000480 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000481
Eric Christopher9a9d2752010-07-22 02:48:34 +0000482 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000483 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000484
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000485 // On X86 and X86-64, atomic operations are lowered to locked instructions.
486 // Locked instructions, in turn, have implicit fence semantics (all memory
487 // operations are flushed before issuing the locked instruction, and they
488 // are not buffered), so we can fold away the common pattern of
489 // fence-atomic-fence.
490 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000491
Mon P Wang63307c32008-05-05 19:05:59 +0000492 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000493 for (unsigned i = 0, e = 4; i != e; ++i) {
494 MVT VT = IntVTs[i];
495 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
496 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000497 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000498 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000499
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000500 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000501 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
503 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
504 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
505 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
506 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
507 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
508 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000509 }
510
Eli Friedman43f51ae2011-08-26 21:21:21 +0000511 if (Subtarget->hasCmpxchg16b()) {
512 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
513 }
514
Evan Cheng3c992d22006-03-07 02:02:57 +0000515 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000516 if (!Subtarget->isTargetDarwin() &&
517 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000518 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000520 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000521
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
523 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
524 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
525 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000526 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000527 setExceptionPointerRegister(X86::RAX);
528 setExceptionSelectorRegister(X86::RDX);
529 } else {
530 setExceptionPointerRegister(X86::EAX);
531 setExceptionSelectorRegister(X86::EDX);
532 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000533 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
534 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000535
Duncan Sands4a544a72011-09-06 13:37:06 +0000536 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
537 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000538
Owen Anderson825b72b2009-08-11 20:47:22 +0000539 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000540
Nate Begemanacc398c2006-01-25 18:21:52 +0000541 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 setOperationAction(ISD::VASTART , MVT::Other, Custom);
543 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000544 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 setOperationAction(ISD::VAARG , MVT::Other, Custom);
546 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000547 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000548 setOperationAction(ISD::VAARG , MVT::Other, Expand);
549 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000550 }
Evan Chengae642192007-03-02 23:16:35 +0000551
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
553 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000554
555 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
556 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
557 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000558 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000559 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
560 MVT::i64 : MVT::i32, Custom);
561 else
562 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
563 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000564
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000565 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000566 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000567 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
569 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000570
Evan Cheng223547a2006-01-31 22:28:30 +0000571 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::FABS , MVT::f64, Custom);
573 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000574
575 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 setOperationAction(ISD::FNEG , MVT::f64, Custom);
577 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000578
Evan Cheng68c47cb2007-01-05 07:55:56 +0000579 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
581 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000582
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000583 // Lower this to FGETSIGNx86 plus an AND.
584 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
585 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
586
Evan Chengd25e9e82006-02-02 00:28:23 +0000587 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 setOperationAction(ISD::FSIN , MVT::f64, Expand);
589 setOperationAction(ISD::FCOS , MVT::f64, Expand);
590 setOperationAction(ISD::FSIN , MVT::f32, Expand);
591 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000592
Chris Lattnera54aa942006-01-29 06:26:08 +0000593 // Expand FP immediates into loads from the stack, except for the special
594 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000595 addLegalFPImmediate(APFloat(+0.0)); // xorpd
596 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000597 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000598 // Use SSE for f32, x87 for f64.
599 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
601 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602
603 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000605
606 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000607 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000608
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000610
611 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
613 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000614
615 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 setOperationAction(ISD::FSIN , MVT::f32, Expand);
617 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000618
Nate Begemane1795842008-02-14 08:57:00 +0000619 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000620 addLegalFPImmediate(APFloat(+0.0f)); // xorps
621 addLegalFPImmediate(APFloat(+0.0)); // FLD0
622 addLegalFPImmediate(APFloat(+1.0)); // FLD1
623 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
624 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
625
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000626 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
628 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000629 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000630 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000631 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000632 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000633 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
634 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000635
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
637 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
638 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
639 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000640
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000641 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000642 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
643 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000644 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000645 addLegalFPImmediate(APFloat(+0.0)); // FLD0
646 addLegalFPImmediate(APFloat(+1.0)); // FLD1
647 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
648 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000649 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
650 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
651 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
652 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000653 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000654
Cameron Zwarich33390842011-07-08 21:39:21 +0000655 // We don't support FMA.
656 setOperationAction(ISD::FMA, MVT::f64, Expand);
657 setOperationAction(ISD::FMA, MVT::f32, Expand);
658
Dale Johannesen59a58732007-08-05 18:49:15 +0000659 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000660 if (!TM.Options.UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
662 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
663 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000664 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000665 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000666 addLegalFPImmediate(TmpFlt); // FLD0
667 TmpFlt.changeSign();
668 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000669
670 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000671 APFloat TmpFlt2(+1.0);
672 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
673 &ignored);
674 addLegalFPImmediate(TmpFlt2); // FLD1
675 TmpFlt2.changeSign();
676 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
677 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000678
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000679 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
681 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000682 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000683
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000684 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
685 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
686 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
687 setOperationAction(ISD::FRINT, MVT::f80, Expand);
688 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000689 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000690 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000691
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000692 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000693 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
694 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
695 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000696
Owen Anderson825b72b2009-08-11 20:47:22 +0000697 setOperationAction(ISD::FLOG, MVT::f80, Expand);
698 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
699 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
700 setOperationAction(ISD::FEXP, MVT::f80, Expand);
701 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000702
Mon P Wangf007a8b2008-11-06 05:31:54 +0000703 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000704 // (for widening) or expand (for scalarization). Then we will selectively
705 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000706 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
707 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
708 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000723 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000724 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
725 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000740 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000741 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000742 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000743 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
745 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
746 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
747 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
748 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000749 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000750 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
751 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
752 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
753 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
754 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
755 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
756 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
757 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
758 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000759 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000760 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
761 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
762 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
763 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000764 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000765 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
766 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
767 setTruncStoreAction((MVT::SimpleValueType)VT,
768 (MVT::SimpleValueType)InnerVT, Expand);
769 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
770 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
771 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000772 }
773
Evan Chengc7ce29b2009-02-13 22:36:38 +0000774 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
775 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000776 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000777 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000778 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000779 }
780
Dale Johannesen0488fb62010-09-30 23:57:10 +0000781 // MMX-sized vectors (other than x86mmx) are expected to be expanded
782 // into smaller operations.
783 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
784 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
785 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
786 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
787 setOperationAction(ISD::AND, MVT::v8i8, Expand);
788 setOperationAction(ISD::AND, MVT::v4i16, Expand);
789 setOperationAction(ISD::AND, MVT::v2i32, Expand);
790 setOperationAction(ISD::AND, MVT::v1i64, Expand);
791 setOperationAction(ISD::OR, MVT::v8i8, Expand);
792 setOperationAction(ISD::OR, MVT::v4i16, Expand);
793 setOperationAction(ISD::OR, MVT::v2i32, Expand);
794 setOperationAction(ISD::OR, MVT::v1i64, Expand);
795 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
796 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
797 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
798 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
799 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
800 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
801 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
802 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
803 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
804 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
805 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
806 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
807 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000808 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
809 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
810 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
811 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000812
Craig Topper1accb7e2012-01-10 06:54:16 +0000813 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000815
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
817 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
818 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
819 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
820 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
821 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
822 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
823 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
824 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
825 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
826 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000827 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000828 }
829
Craig Topper1accb7e2012-01-10 06:54:16 +0000830 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000831 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000832
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000833 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
834 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000835 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
836 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
837 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
838 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000839
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
841 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
842 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
843 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
844 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
845 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
846 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
847 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
848 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
849 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
850 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
851 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
852 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
853 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
854 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
855 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000856
Nadav Rotem354efd82011-09-18 14:57:03 +0000857 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000858 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
859 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
860 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000861
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
863 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
864 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
865 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
866 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000867
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000868 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
869 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
870 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
871 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
872 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
873
Evan Cheng2c3ae372006-04-12 21:21:57 +0000874 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
876 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000877 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000878 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000879 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000880 // Do not attempt to custom lower non-128-bit vectors
881 if (!VT.is128BitVector())
882 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000883 setOperationAction(ISD::BUILD_VECTOR,
884 VT.getSimpleVT().SimpleTy, Custom);
885 setOperationAction(ISD::VECTOR_SHUFFLE,
886 VT.getSimpleVT().SimpleTy, Custom);
887 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
888 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000889 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000890
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
892 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
893 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
894 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
895 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
896 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000897
Nate Begemancdd1eec2008-02-12 22:51:28 +0000898 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
900 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000901 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000902
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000903 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
905 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000906 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000907
908 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000909 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000910 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000911
Owen Andersond6662ad2009-08-10 20:46:15 +0000912 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000914 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000915 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000916 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000917 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000918 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000919 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000920 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000922 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000923
Owen Anderson825b72b2009-08-11 20:47:22 +0000924 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000925
Evan Cheng2c3ae372006-04-12 21:21:57 +0000926 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000927 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
928 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
929 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
930 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000931
Owen Anderson825b72b2009-08-11 20:47:22 +0000932 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
933 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000934 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000935
Craig Topperd0a31172012-01-10 06:37:29 +0000936 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000937 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
938 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
939 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
940 setOperationAction(ISD::FRINT, MVT::f32, Legal);
941 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
942 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
943 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
944 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
945 setOperationAction(ISD::FRINT, MVT::f64, Legal);
946 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
947
Nate Begeman14d12ca2008-02-11 04:19:36 +0000948 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000949 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000950
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000951 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
952 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
953 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
954 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
955 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000956
Nate Begeman14d12ca2008-02-11 04:19:36 +0000957 // i8 and i16 vectors are custom , because the source register and source
958 // source memory operand types are not the same width. f32 vectors are
959 // custom since the immediate controlling the insert encodes additional
960 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000961 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
962 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
963 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
964 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000965
Owen Anderson825b72b2009-08-11 20:47:22 +0000966 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
967 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
968 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
969 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000970
Pete Coopera77214a2011-11-14 19:38:42 +0000971 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +0000972 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +0000973 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000974 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
975 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000976 }
977 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000978
Craig Topper1accb7e2012-01-10 06:54:16 +0000979 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000980 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000981 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000982
Nadav Rotem43012222011-05-11 08:12:09 +0000983 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000984 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000985
Nadav Rotem43012222011-05-11 08:12:09 +0000986 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000987 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000988
989 if (Subtarget->hasAVX2()) {
990 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
991 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
992
993 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
994 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
995
996 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
997 } else {
998 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
999 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1000
1001 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1002 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1003
1004 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1005 }
Nadav Rotem43012222011-05-11 08:12:09 +00001006 }
1007
Craig Topperd0a31172012-01-10 06:37:29 +00001008 if (Subtarget->hasSSE42())
Duncan Sands28b77e92011-09-06 19:07:46 +00001009 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +00001010
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001011 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +00001012 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
1013 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
1014 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
1015 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
1016 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
1017 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +00001018
Owen Anderson825b72b2009-08-11 20:47:22 +00001019 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001020 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1021 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001022
Owen Anderson825b72b2009-08-11 20:47:22 +00001023 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1024 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1025 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1026 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1027 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
1028 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001029
Owen Anderson825b72b2009-08-11 20:47:22 +00001030 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1031 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1032 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1033 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1034 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1035 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001036
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001037 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1038 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001039 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001040
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00001041 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
1042 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
1043 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
1044 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
1045 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1046 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1047
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001048 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1049 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1050
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001051 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1052 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1053
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001054 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001055 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001056
Duncan Sands28b77e92011-09-06 19:07:46 +00001057 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1058 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1059 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1060 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001061
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001062 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1063 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1064 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1065
Craig Topperaaa643c2011-11-09 07:28:55 +00001066 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1067 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1068 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1069 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001070
Craig Topperaaa643c2011-11-09 07:28:55 +00001071 if (Subtarget->hasAVX2()) {
1072 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1073 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1074 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1075 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001076
Craig Topperaaa643c2011-11-09 07:28:55 +00001077 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1078 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1079 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1080 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001081
Craig Topperaaa643c2011-11-09 07:28:55 +00001082 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1083 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1084 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001085 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001086
1087 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001088
1089 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1090 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1091
1092 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1093 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1094
1095 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001096 } else {
1097 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1098 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1099 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1100 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1101
1102 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1103 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1104 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1105 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1106
1107 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1108 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1109 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1110 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001111
1112 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1113 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1114
1115 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1116 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1117
1118 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001119 }
Craig Topper13894fa2011-08-24 06:14:18 +00001120
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001121 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001122 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001123 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1124 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1125 EVT VT = SVT;
1126
1127 // Extract subvector is special because the value type
1128 // (result) is 128-bit but the source is 256-bit wide.
1129 if (VT.is128BitVector())
1130 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1131
1132 // Do not attempt to custom lower other non-256-bit vectors
1133 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001134 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001135
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001136 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1137 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1138 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1139 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001140 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001141 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001142 }
1143
David Greene54d8eba2011-01-27 22:38:56 +00001144 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001145 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1146 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1147 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001148
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001149 // Do not attempt to promote non-256-bit vectors
1150 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001151 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001152
1153 setOperationAction(ISD::AND, SVT, Promote);
1154 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1155 setOperationAction(ISD::OR, SVT, Promote);
1156 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1157 setOperationAction(ISD::XOR, SVT, Promote);
1158 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1159 setOperationAction(ISD::LOAD, SVT, Promote);
1160 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1161 setOperationAction(ISD::SELECT, SVT, Promote);
1162 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001163 }
David Greene9b9838d2009-06-29 16:47:10 +00001164 }
1165
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001166 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1167 // of this type with custom code.
1168 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1169 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001170 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1171 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001172 }
1173
Evan Cheng6be2c582006-04-05 23:38:46 +00001174 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001175 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001176
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001177
Eli Friedman962f5492010-06-02 19:35:46 +00001178 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1179 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001180 //
Eli Friedman962f5492010-06-02 19:35:46 +00001181 // FIXME: We really should do custom legalization for addition and
1182 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1183 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001184 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1185 // Add/Sub/Mul with overflow operations are custom lowered.
1186 MVT VT = IntVTs[i];
1187 setOperationAction(ISD::SADDO, VT, Custom);
1188 setOperationAction(ISD::UADDO, VT, Custom);
1189 setOperationAction(ISD::SSUBO, VT, Custom);
1190 setOperationAction(ISD::USUBO, VT, Custom);
1191 setOperationAction(ISD::SMULO, VT, Custom);
1192 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001193 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001194
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001195 // There are no 8-bit 3-address imul/mul instructions
1196 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1197 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001198
Evan Chengd54f2d52009-03-31 19:38:51 +00001199 if (!Subtarget->is64Bit()) {
1200 // These libcalls are not available in 32-bit.
1201 setLibcallName(RTLIB::SHL_I128, 0);
1202 setLibcallName(RTLIB::SRL_I128, 0);
1203 setLibcallName(RTLIB::SRA_I128, 0);
1204 }
1205
Evan Cheng206ee9d2006-07-07 08:33:52 +00001206 // We have target-specific dag combine patterns for the following nodes:
1207 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001208 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001209 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001210 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001211 setTargetDAGCombine(ISD::SHL);
1212 setTargetDAGCombine(ISD::SRA);
1213 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001214 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001215 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001216 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001217 setTargetDAGCombine(ISD::FADD);
1218 setTargetDAGCombine(ISD::FSUB);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001219 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001220 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001221 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001222 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001223 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001224 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001225 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001226 if (Subtarget->is64Bit())
1227 setTargetDAGCombine(ISD::MUL);
Craig Topperb4c94572011-10-21 06:55:01 +00001228 if (Subtarget->hasBMI())
1229 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001230
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001231 computeRegisterProperties();
1232
Evan Cheng05219282011-01-06 06:52:41 +00001233 // On Darwin, -Os means optimize for size without hurting performance,
1234 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001235 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001236 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001237 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001238 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1239 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1240 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001241 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001242 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001243
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001244 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001245}
1246
Scott Michel5b8f82e2008-03-10 15:42:14 +00001247
Duncan Sands28b77e92011-09-06 19:07:46 +00001248EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1249 if (!VT.isVector()) return MVT::i8;
1250 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001251}
1252
1253
Evan Cheng29286502008-01-23 23:17:41 +00001254/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1255/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001256static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001257 if (MaxAlign == 16)
1258 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001259 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001260 if (VTy->getBitWidth() == 128)
1261 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001262 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001263 unsigned EltAlign = 0;
1264 getMaxByValAlign(ATy->getElementType(), EltAlign);
1265 if (EltAlign > MaxAlign)
1266 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001267 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001268 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1269 unsigned EltAlign = 0;
1270 getMaxByValAlign(STy->getElementType(i), EltAlign);
1271 if (EltAlign > MaxAlign)
1272 MaxAlign = EltAlign;
1273 if (MaxAlign == 16)
1274 break;
1275 }
1276 }
1277 return;
1278}
1279
1280/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1281/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001282/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1283/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001284unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001285 if (Subtarget->is64Bit()) {
1286 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001287 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001288 if (TyAlign > 8)
1289 return TyAlign;
1290 return 8;
1291 }
1292
Evan Cheng29286502008-01-23 23:17:41 +00001293 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001294 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001295 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001296 return Align;
1297}
Chris Lattner2b02a442007-02-25 08:29:00 +00001298
Evan Chengf0df0312008-05-15 08:39:06 +00001299/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001300/// and store operations as a result of memset, memcpy, and memmove
1301/// lowering. If DstAlign is zero that means it's safe to destination
1302/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1303/// means there isn't a need to check it against alignment requirement,
1304/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001305/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001306/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1307/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1308/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001309/// It returns EVT::Other if the type should be determined using generic
1310/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001311EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001312X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1313 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001314 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001315 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001316 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001317 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1318 // linux. This is because the stack realignment code can't handle certain
1319 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001320 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001321 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001322 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001323 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001324 (Subtarget->isUnalignedMemAccessFast() ||
1325 ((DstAlign == 0 || DstAlign >= 16) &&
1326 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001327 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001328 if (Subtarget->getStackAlignment() >= 32) {
1329 if (Subtarget->hasAVX2())
1330 return MVT::v8i32;
1331 if (Subtarget->hasAVX())
1332 return MVT::v8f32;
1333 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001334 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001335 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001336 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001337 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001338 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001339 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001340 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001341 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001342 // Do not use f64 to lower memcpy if source is string constant. It's
1343 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001344 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001345 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001346 }
Evan Chengf0df0312008-05-15 08:39:06 +00001347 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001348 return MVT::i64;
1349 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001350}
1351
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001352/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1353/// current function. The returned value is a member of the
1354/// MachineJumpTableInfo::JTEntryKind enum.
1355unsigned X86TargetLowering::getJumpTableEncoding() const {
1356 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1357 // symbol.
1358 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1359 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001360 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001361
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001362 // Otherwise, use the normal jump table encoding heuristics.
1363 return TargetLowering::getJumpTableEncoding();
1364}
1365
Chris Lattnerc64daab2010-01-26 05:02:42 +00001366const MCExpr *
1367X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1368 const MachineBasicBlock *MBB,
1369 unsigned uid,MCContext &Ctx) const{
1370 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1371 Subtarget->isPICStyleGOT());
1372 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1373 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001374 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1375 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001376}
1377
Evan Chengcc415862007-11-09 01:32:10 +00001378/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1379/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001380SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001381 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001382 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001383 // This doesn't have DebugLoc associated with it, but is not really the
1384 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001385 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001386 return Table;
1387}
1388
Chris Lattner589c6f62010-01-26 06:28:43 +00001389/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1390/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1391/// MCExpr.
1392const MCExpr *X86TargetLowering::
1393getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1394 MCContext &Ctx) const {
1395 // X86-64 uses RIP relative addressing based on the jump table label.
1396 if (Subtarget->isPICStyleRIPRel())
1397 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1398
1399 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001400 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001401}
1402
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001403// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001404std::pair<const TargetRegisterClass*, uint8_t>
1405X86TargetLowering::findRepresentativeClass(EVT VT) const{
1406 const TargetRegisterClass *RRC = 0;
1407 uint8_t Cost = 1;
1408 switch (VT.getSimpleVT().SimpleTy) {
1409 default:
1410 return TargetLowering::findRepresentativeClass(VT);
1411 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1412 RRC = (Subtarget->is64Bit()
1413 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1414 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001415 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001416 RRC = X86::VR64RegisterClass;
1417 break;
1418 case MVT::f32: case MVT::f64:
1419 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1420 case MVT::v4f32: case MVT::v2f64:
1421 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1422 case MVT::v4f64:
1423 RRC = X86::VR128RegisterClass;
1424 break;
1425 }
1426 return std::make_pair(RRC, Cost);
1427}
1428
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001429bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1430 unsigned &Offset) const {
1431 if (!Subtarget->isTargetLinux())
1432 return false;
1433
1434 if (Subtarget->is64Bit()) {
1435 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1436 Offset = 0x28;
1437 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1438 AddressSpace = 256;
1439 else
1440 AddressSpace = 257;
1441 } else {
1442 // %gs:0x14 on i386
1443 Offset = 0x14;
1444 AddressSpace = 256;
1445 }
1446 return true;
1447}
1448
1449
Chris Lattner2b02a442007-02-25 08:29:00 +00001450//===----------------------------------------------------------------------===//
1451// Return Value Calling Convention Implementation
1452//===----------------------------------------------------------------------===//
1453
Chris Lattner59ed56b2007-02-28 04:55:35 +00001454#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001455
Michael J. Spencerec38de22010-10-10 22:04:20 +00001456bool
Eric Christopher471e4222011-06-08 23:55:35 +00001457X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1458 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001459 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001460 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001461 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001462 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001463 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001464 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001465}
1466
Dan Gohman98ca4f22009-08-05 01:29:28 +00001467SDValue
1468X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001469 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001470 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001471 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001472 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001473 MachineFunction &MF = DAG.getMachineFunction();
1474 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001475
Chris Lattner9774c912007-02-27 05:28:59 +00001476 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001477 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001478 RVLocs, *DAG.getContext());
1479 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001480
Evan Chengdcea1632010-02-04 02:40:39 +00001481 // Add the regs to the liveout set for the function.
1482 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1483 for (unsigned i = 0; i != RVLocs.size(); ++i)
1484 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1485 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001486
Dan Gohman475871a2008-07-27 21:46:04 +00001487 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001488
Dan Gohman475871a2008-07-27 21:46:04 +00001489 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001490 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1491 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001492 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1493 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001494
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001495 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001496 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1497 CCValAssign &VA = RVLocs[i];
1498 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001499 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001500 EVT ValVT = ValToCopy.getValueType();
1501
Dale Johannesenc4510512010-09-24 19:05:48 +00001502 // If this is x86-64, and we disabled SSE, we can't return FP values,
1503 // or SSE or MMX vectors.
1504 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1505 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001506 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001507 report_fatal_error("SSE register return with SSE disabled");
1508 }
1509 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1510 // llvm-gcc has never done it right and no one has noticed, so this
1511 // should be OK for now.
1512 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001513 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001514 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001515
Chris Lattner447ff682008-03-11 03:23:40 +00001516 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1517 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001518 if (VA.getLocReg() == X86::ST0 ||
1519 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001520 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1521 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001522 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001523 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001524 RetOps.push_back(ValToCopy);
1525 // Don't emit a copytoreg.
1526 continue;
1527 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001528
Evan Cheng242b38b2009-02-23 09:03:22 +00001529 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1530 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001531 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001532 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001533 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001534 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001535 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1536 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001537 // If we don't have SSE2 available, convert to v4f32 so the generated
1538 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001539 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001540 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001541 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001542 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001543 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001544
Dale Johannesendd64c412009-02-04 00:33:20 +00001545 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001546 Flag = Chain.getValue(1);
1547 }
Dan Gohman61a92132008-04-21 23:59:07 +00001548
1549 // The x86-64 ABI for returning structs by value requires that we copy
1550 // the sret argument into %rax for the return. We saved the argument into
1551 // a virtual register in the entry block, so now we copy the value out
1552 // and into %rax.
1553 if (Subtarget->is64Bit() &&
1554 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1555 MachineFunction &MF = DAG.getMachineFunction();
1556 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1557 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001558 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001559 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001560 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001561
Dale Johannesendd64c412009-02-04 00:33:20 +00001562 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001563 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001564
1565 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001566 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001567 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001568
Chris Lattner447ff682008-03-11 03:23:40 +00001569 RetOps[0] = Chain; // Update chain.
1570
1571 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001572 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001573 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001574
1575 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001576 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001577}
1578
Evan Cheng3d2125c2010-11-30 23:55:39 +00001579bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1580 if (N->getNumValues() != 1)
1581 return false;
1582 if (!N->hasNUsesOfValue(1, 0))
1583 return false;
1584
1585 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001586 if (Copy->getOpcode() != ISD::CopyToReg &&
1587 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001588 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001589
1590 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001591 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001592 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001593 if (UI->getOpcode() != X86ISD::RET_FLAG)
1594 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001595 HasRet = true;
1596 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001597
Evan Cheng1bf891a2010-12-01 22:59:46 +00001598 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001599}
1600
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001601EVT
1602X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001603 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001604 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001605 // TODO: Is this also valid on 32-bit?
1606 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001607 ReturnMVT = MVT::i8;
1608 else
1609 ReturnMVT = MVT::i32;
1610
1611 EVT MinVT = getRegisterType(Context, ReturnMVT);
1612 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001613}
1614
Dan Gohman98ca4f22009-08-05 01:29:28 +00001615/// LowerCallResult - Lower the result values of a call into the
1616/// appropriate copies out of appropriate physical registers.
1617///
1618SDValue
1619X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001620 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001621 const SmallVectorImpl<ISD::InputArg> &Ins,
1622 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001623 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001624
Chris Lattnere32bbf62007-02-28 07:09:55 +00001625 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001626 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001627 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001628 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1629 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001630 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001631
Chris Lattner3085e152007-02-25 08:59:22 +00001632 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001633 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001634 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001635 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001636
Torok Edwin3f142c32009-02-01 18:15:56 +00001637 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001638 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001639 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001640 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001641 }
1642
Evan Cheng79fb3b42009-02-20 20:43:02 +00001643 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001644
1645 // If this is a call to a function that returns an fp value on the floating
1646 // point stack, we must guarantee the the value is popped from the stack, so
1647 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001648 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001649 // instead.
1650 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1651 // If we prefer to use the value in xmm registers, copy it out as f80 and
1652 // use a truncate to move it from fp stack reg to xmm reg.
1653 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001654 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001655 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1656 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001657 Val = Chain.getValue(0);
1658
1659 // Round the f80 to the right size, which also moves it to the appropriate
1660 // xmm register.
1661 if (CopyVT != VA.getValVT())
1662 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1663 // This truncation won't change the value.
1664 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001665 } else {
1666 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1667 CopyVT, InFlag).getValue(1);
1668 Val = Chain.getValue(0);
1669 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001670 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001671 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001672 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001673
Dan Gohman98ca4f22009-08-05 01:29:28 +00001674 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001675}
1676
1677
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001678//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001679// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001680//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001681// StdCall calling convention seems to be standard for many Windows' API
1682// routines and around. It differs from C calling convention just a little:
1683// callee should clean up the stack, not caller. Symbols should be also
1684// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001685// For info on fast calling convention see Fast Calling Convention (tail call)
1686// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001687
Dan Gohman98ca4f22009-08-05 01:29:28 +00001688/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001689/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001690static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1691 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001692 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001693
Dan Gohman98ca4f22009-08-05 01:29:28 +00001694 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001695}
1696
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001697/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001698/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001699static bool
1700ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1701 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001702 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001703
Dan Gohman98ca4f22009-08-05 01:29:28 +00001704 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001705}
1706
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001707/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1708/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001709/// the specific parameter attribute. The copy will be passed as a byval
1710/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001711static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001712CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001713 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1714 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001715 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001716
Dale Johannesendd64c412009-02-04 00:33:20 +00001717 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001718 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001719 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001720}
1721
Chris Lattner29689432010-03-11 00:22:57 +00001722/// IsTailCallConvention - Return true if the calling convention is one that
1723/// supports tail call optimization.
1724static bool IsTailCallConvention(CallingConv::ID CC) {
1725 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1726}
1727
Evan Cheng485fafc2011-03-21 01:19:09 +00001728bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001729 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001730 return false;
1731
1732 CallSite CS(CI);
1733 CallingConv::ID CalleeCC = CS.getCallingConv();
1734 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1735 return false;
1736
1737 return true;
1738}
1739
Evan Cheng0c439eb2010-01-27 00:07:07 +00001740/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1741/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001742static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1743 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001744 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001745}
1746
Dan Gohman98ca4f22009-08-05 01:29:28 +00001747SDValue
1748X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001749 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001750 const SmallVectorImpl<ISD::InputArg> &Ins,
1751 DebugLoc dl, SelectionDAG &DAG,
1752 const CCValAssign &VA,
1753 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001754 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001755 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001756 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001757 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1758 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001759 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001760 EVT ValVT;
1761
1762 // If value is passed by pointer we have address passed instead of the value
1763 // itself.
1764 if (VA.getLocInfo() == CCValAssign::Indirect)
1765 ValVT = VA.getLocVT();
1766 else
1767 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001768
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001769 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001770 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001771 // In case of tail call optimization mark all arguments mutable. Since they
1772 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001773 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001774 unsigned Bytes = Flags.getByValSize();
1775 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1776 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001777 return DAG.getFrameIndex(FI, getPointerTy());
1778 } else {
1779 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001780 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001781 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1782 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001783 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001784 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001785 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001786}
1787
Dan Gohman475871a2008-07-27 21:46:04 +00001788SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001789X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001790 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001791 bool isVarArg,
1792 const SmallVectorImpl<ISD::InputArg> &Ins,
1793 DebugLoc dl,
1794 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001795 SmallVectorImpl<SDValue> &InVals)
1796 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001797 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001798 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001799
Gordon Henriksen86737662008-01-05 16:56:59 +00001800 const Function* Fn = MF.getFunction();
1801 if (Fn->hasExternalLinkage() &&
1802 Subtarget->isTargetCygMing() &&
1803 Fn->getName() == "main")
1804 FuncInfo->setForceFramePointer(true);
1805
Evan Cheng1bc78042006-04-26 01:20:17 +00001806 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001807 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001808 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001809 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001810
Chris Lattner29689432010-03-11 00:22:57 +00001811 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1812 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001813
Chris Lattner638402b2007-02-28 07:00:42 +00001814 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001815 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001816 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001817 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001818
1819 // Allocate shadow area for Win64
1820 if (IsWin64) {
1821 CCInfo.AllocateStack(32, 8);
1822 }
1823
Duncan Sands45907662010-10-31 13:21:44 +00001824 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001825
Chris Lattnerf39f7712007-02-28 05:46:49 +00001826 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001827 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001828 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1829 CCValAssign &VA = ArgLocs[i];
1830 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1831 // places.
1832 assert(VA.getValNo() != LastVal &&
1833 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001834 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001835 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001836
Chris Lattnerf39f7712007-02-28 05:46:49 +00001837 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001838 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001839 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001840 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001841 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001842 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001843 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001844 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001845 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001846 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001847 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001848 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1849 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001850 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001851 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001852 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001853 RC = X86::VR64RegisterClass;
1854 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001855 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001856
Devang Patel68e6bee2011-02-21 23:21:26 +00001857 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001858 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001859
Chris Lattnerf39f7712007-02-28 05:46:49 +00001860 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1861 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1862 // right size.
1863 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001864 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001865 DAG.getValueType(VA.getValVT()));
1866 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001867 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001868 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001869 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001870 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001871
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001872 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001873 // Handle MMX values passed in XMM regs.
1874 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001875 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1876 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001877 } else
1878 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001879 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001880 } else {
1881 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001882 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001883 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001884
1885 // If value is passed via pointer - do a load.
1886 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001887 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001888 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001889
Dan Gohman98ca4f22009-08-05 01:29:28 +00001890 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001891 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001892
Dan Gohman61a92132008-04-21 23:59:07 +00001893 // The x86-64 ABI for returning structs by value requires that we copy
1894 // the sret argument into %rax for the return. Save the argument into
1895 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001896 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001897 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1898 unsigned Reg = FuncInfo->getSRetReturnReg();
1899 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001900 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001901 FuncInfo->setSRetReturnReg(Reg);
1902 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001903 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001904 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001905 }
1906
Chris Lattnerf39f7712007-02-28 05:46:49 +00001907 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001908 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001909 if (FuncIsMadeTailCallSafe(CallConv,
1910 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001911 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001912
Evan Cheng1bc78042006-04-26 01:20:17 +00001913 // If the function takes variable number of arguments, make a frame index for
1914 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001915 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001916 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1917 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001918 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001919 }
1920 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001921 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1922
1923 // FIXME: We should really autogenerate these arrays
1924 static const unsigned GPR64ArgRegsWin64[] = {
1925 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001926 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001927 static const unsigned GPR64ArgRegs64Bit[] = {
1928 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1929 };
1930 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001931 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1932 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1933 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001934 const unsigned *GPR64ArgRegs;
1935 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001936
1937 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001938 // The XMM registers which might contain var arg parameters are shadowed
1939 // in their paired GPR. So we only need to save the GPR to their home
1940 // slots.
1941 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001942 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001943 } else {
1944 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1945 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001946
Chad Rosier30450e82011-12-22 22:35:21 +00001947 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
1948 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001949 }
1950 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1951 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001952
Devang Patel578efa92009-06-05 21:57:13 +00001953 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00001954 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001955 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001956 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
1957 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001958 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001959 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00001960 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001961 // Kernel mode asks for SSE to be disabled, so don't push them
1962 // on the stack.
1963 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001964
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001965 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001966 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001967 // Get to the caller-allocated home save location. Add 8 to account
1968 // for the return address.
1969 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001970 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001971 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001972 // Fixup to set vararg frame on shadow area (4 x i64).
1973 if (NumIntRegs < 4)
1974 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001975 } else {
1976 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00001977 // registers, then we must store them to their spots on the stack so
1978 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001979 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1980 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1981 FuncInfo->setRegSaveFrameIndex(
1982 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001983 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001984 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001985
Gordon Henriksen86737662008-01-05 16:56:59 +00001986 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001987 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001988 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1989 getPointerTy());
1990 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001991 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001992 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1993 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001994 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001995 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001996 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001997 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001998 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001999 MachinePointerInfo::getFixedStack(
2000 FuncInfo->getRegSaveFrameIndex(), Offset),
2001 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002002 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002003 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002004 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002005
Dan Gohmanface41a2009-08-16 21:24:25 +00002006 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2007 // Now store the XMM (fp + vector) parameter registers.
2008 SmallVector<SDValue, 11> SaveXMMOps;
2009 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002010
Devang Patel68e6bee2011-02-21 23:21:26 +00002011 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002012 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2013 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002014
Dan Gohman1e93df62010-04-17 14:41:14 +00002015 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2016 FuncInfo->getRegSaveFrameIndex()));
2017 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2018 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002019
Dan Gohmanface41a2009-08-16 21:24:25 +00002020 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002021 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00002022 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002023 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2024 SaveXMMOps.push_back(Val);
2025 }
2026 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2027 MVT::Other,
2028 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002029 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002030
2031 if (!MemOps.empty())
2032 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2033 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002034 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002035 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002036
Gordon Henriksen86737662008-01-05 16:56:59 +00002037 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002038 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2039 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002040 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002041 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002042 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002043 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002044 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
2045 ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00002046 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002047 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002048
Gordon Henriksen86737662008-01-05 16:56:59 +00002049 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002050 // RegSaveFrameIndex is X86-64 only.
2051 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002052 if (CallConv == CallingConv::X86_FastCall ||
2053 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002054 // fastcc functions can't have varargs.
2055 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002056 }
Evan Cheng25caf632006-05-23 21:06:34 +00002057
Rafael Espindola76927d752011-08-30 19:39:58 +00002058 FuncInfo->setArgumentStackSize(StackSize);
2059
Dan Gohman98ca4f22009-08-05 01:29:28 +00002060 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002061}
2062
Dan Gohman475871a2008-07-27 21:46:04 +00002063SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002064X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2065 SDValue StackPtr, SDValue Arg,
2066 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002067 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002068 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002069 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002070 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002071 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002072 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002073 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002074
2075 return DAG.getStore(Chain, dl, Arg, PtrOff,
2076 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002077 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002078}
2079
Bill Wendling64e87322009-01-16 19:25:27 +00002080/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002081/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002082SDValue
2083X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002084 SDValue &OutRetAddr, SDValue Chain,
2085 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002086 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002087 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002088 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002089 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002090
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002091 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002092 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002093 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002094 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002095}
2096
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002097/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002098/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002099static SDValue
2100EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002101 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002102 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002103 // Store the return address to the appropriate stack slot.
2104 if (!FPDiff) return Chain;
2105 // Calculate the new stack slot for the return address.
2106 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002107 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002108 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002109 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002110 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002111 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002112 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002113 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002114 return Chain;
2115}
2116
Dan Gohman98ca4f22009-08-05 01:29:28 +00002117SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002118X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002119 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002120 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002121 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002122 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002123 const SmallVectorImpl<ISD::InputArg> &Ins,
2124 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002125 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002126 MachineFunction &MF = DAG.getMachineFunction();
2127 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002128 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002129 bool IsWindows = Subtarget->isTargetWindows();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002130 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002131 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002132
Nick Lewycky22de16d2012-01-19 00:34:10 +00002133 if (MF.getTarget().Options.DisableTailCalls)
2134 isTailCall = false;
2135
Evan Cheng5f941932010-02-05 02:21:12 +00002136 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002137 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002138 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2139 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002140 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002141
2142 // Sibcalls are automatically detected tailcalls which do not require
2143 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002144 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002145 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002146
2147 if (isTailCall)
2148 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002149 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002150
Chris Lattner29689432010-03-11 00:22:57 +00002151 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2152 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002153
Chris Lattner638402b2007-02-28 07:00:42 +00002154 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002155 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002156 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002157 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002158
2159 // Allocate shadow area for Win64
2160 if (IsWin64) {
2161 CCInfo.AllocateStack(32, 8);
2162 }
2163
Duncan Sands45907662010-10-31 13:21:44 +00002164 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002165
Chris Lattner423c5f42007-02-28 05:31:48 +00002166 // Get a count of how many bytes are to be pushed on the stack.
2167 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002168 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002169 // This is a sibcall. The memory operands are available in caller's
2170 // own caller's stack.
2171 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002172 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2173 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002174 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002175
Gordon Henriksen86737662008-01-05 16:56:59 +00002176 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002177 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002178 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002179 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002180 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2181 FPDiff = NumBytesCallerPushed - NumBytes;
2182
2183 // Set the delta of movement of the returnaddr stackslot.
2184 // But only set if delta is greater than previous delta.
2185 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2186 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2187 }
2188
Evan Chengf22f9b32010-02-06 03:28:46 +00002189 if (!IsSibcall)
2190 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002191
Dan Gohman475871a2008-07-27 21:46:04 +00002192 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002193 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002194 if (isTailCall && FPDiff)
2195 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2196 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002197
Dan Gohman475871a2008-07-27 21:46:04 +00002198 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2199 SmallVector<SDValue, 8> MemOpChains;
2200 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002201
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002202 // Walk the register/memloc assignments, inserting copies/loads. In the case
2203 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002204 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2205 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002206 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002207 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002208 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002209 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002210
Chris Lattner423c5f42007-02-28 05:31:48 +00002211 // Promote the value if needed.
2212 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002213 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002214 case CCValAssign::Full: break;
2215 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002216 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002217 break;
2218 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002219 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002220 break;
2221 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002222 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2223 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002224 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002225 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2226 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002227 } else
2228 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2229 break;
2230 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002231 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002232 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002233 case CCValAssign::Indirect: {
2234 // Store the argument.
2235 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002236 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002237 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002238 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002239 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002240 Arg = SpillSlot;
2241 break;
2242 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002243 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002244
Chris Lattner423c5f42007-02-28 05:31:48 +00002245 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002246 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2247 if (isVarArg && IsWin64) {
2248 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2249 // shadow reg if callee is a varargs function.
2250 unsigned ShadowReg = 0;
2251 switch (VA.getLocReg()) {
2252 case X86::XMM0: ShadowReg = X86::RCX; break;
2253 case X86::XMM1: ShadowReg = X86::RDX; break;
2254 case X86::XMM2: ShadowReg = X86::R8; break;
2255 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002256 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002257 if (ShadowReg)
2258 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002259 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002260 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002261 assert(VA.isMemLoc());
2262 if (StackPtr.getNode() == 0)
2263 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2264 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2265 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002266 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002267 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002268
Evan Cheng32fe1032006-05-25 00:59:30 +00002269 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002270 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002271 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002272
Evan Cheng347d5f72006-04-28 21:29:37 +00002273 // Build a sequence of copy-to-reg nodes chained together with token chain
2274 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002275 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002276 // Tail call byval lowering might overwrite argument registers so in case of
2277 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002278 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002279 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002280 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002281 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002282 InFlag = Chain.getValue(1);
2283 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002284
Chris Lattner88e1fd52009-07-09 04:24:46 +00002285 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002286 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2287 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002288 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002289 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2290 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002291 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002292 InFlag);
2293 InFlag = Chain.getValue(1);
2294 } else {
2295 // If we are tail calling and generating PIC/GOT style code load the
2296 // address of the callee into ECX. The value in ecx is used as target of
2297 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2298 // for tail calls on PIC/GOT architectures. Normally we would just put the
2299 // address of GOT into ebx and then call target@PLT. But for tail calls
2300 // ebx would be restored (since ebx is callee saved) before jumping to the
2301 // target@PLT.
2302
2303 // Note: The actual moving to ECX is done further down.
2304 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2305 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2306 !G->getGlobal()->hasProtectedVisibility())
2307 Callee = LowerGlobalAddress(Callee, DAG);
2308 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002309 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002310 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002311 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002312
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002313 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002314 // From AMD64 ABI document:
2315 // For calls that may call functions that use varargs or stdargs
2316 // (prototype-less calls or calls to functions containing ellipsis (...) in
2317 // the declaration) %al is used as hidden argument to specify the number
2318 // of SSE registers used. The contents of %al do not need to match exactly
2319 // the number of registers, but must be an ubound on the number of SSE
2320 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002321
Gordon Henriksen86737662008-01-05 16:56:59 +00002322 // Count the number of XMM registers allocated.
2323 static const unsigned XMMArgRegs[] = {
2324 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2325 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2326 };
2327 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002328 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002329 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002330
Dale Johannesendd64c412009-02-04 00:33:20 +00002331 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002332 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002333 InFlag = Chain.getValue(1);
2334 }
2335
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002336
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002337 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002338 if (isTailCall) {
2339 // Force all the incoming stack arguments to be loaded from the stack
2340 // before any new outgoing arguments are stored to the stack, because the
2341 // outgoing stack slots may alias the incoming argument stack slots, and
2342 // the alias isn't otherwise explicit. This is slightly more conservative
2343 // than necessary, because it means that each store effectively depends
2344 // on every argument instead of just those arguments it would clobber.
2345 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2346
Dan Gohman475871a2008-07-27 21:46:04 +00002347 SmallVector<SDValue, 8> MemOpChains2;
2348 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002349 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002350 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002351 InFlag = SDValue();
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002352 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002353 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2354 CCValAssign &VA = ArgLocs[i];
2355 if (VA.isRegLoc())
2356 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002357 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002358 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002359 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002360 // Create frame index.
2361 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002362 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002363 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002364 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002365
Duncan Sands276dcbd2008-03-21 09:14:45 +00002366 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002367 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002368 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002369 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002370 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002371 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002372 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002373
Dan Gohman98ca4f22009-08-05 01:29:28 +00002374 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2375 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002376 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002377 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002378 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002379 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002380 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002381 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002382 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002383 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002384 }
2385 }
2386
2387 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002388 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002389 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002390
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002391 // Copy arguments to their registers.
2392 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002393 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002394 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002395 InFlag = Chain.getValue(1);
2396 }
Dan Gohman475871a2008-07-27 21:46:04 +00002397 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002398
Gordon Henriksen86737662008-01-05 16:56:59 +00002399 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002400 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002401 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002402 }
2403
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002404 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2405 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2406 // In the 64-bit large code model, we have to make all calls
2407 // through a register, since the call instruction's 32-bit
2408 // pc-relative offset may not be large enough to hold the whole
2409 // address.
2410 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002411 // If the callee is a GlobalAddress node (quite common, every direct call
2412 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2413 // it.
2414
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002415 // We should use extra load for direct calls to dllimported functions in
2416 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002417 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002418 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002419 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002420 bool ExtraLoad = false;
2421 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002422
Chris Lattner48a7d022009-07-09 05:02:21 +00002423 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2424 // external symbols most go through the PLT in PIC mode. If the symbol
2425 // has hidden or protected visibility, or if it is static or local, then
2426 // we don't need to use the PLT - we can directly call it.
2427 if (Subtarget->isTargetELF() &&
2428 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002429 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002430 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002431 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002432 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002433 (!Subtarget->getTargetTriple().isMacOSX() ||
2434 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002435 // PC-relative references to external symbols should go through $stub,
2436 // unless we're building with the leopard linker or later, which
2437 // automatically synthesizes these stubs.
2438 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002439 } else if (Subtarget->isPICStyleRIPRel() &&
2440 isa<Function>(GV) &&
2441 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2442 // If the function is marked as non-lazy, generate an indirect call
2443 // which loads from the GOT directly. This avoids runtime overhead
2444 // at the cost of eager binding (and one extra byte of encoding).
2445 OpFlags = X86II::MO_GOTPCREL;
2446 WrapperKind = X86ISD::WrapperRIP;
2447 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002448 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002449
Devang Patel0d881da2010-07-06 22:08:15 +00002450 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002451 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002452
2453 // Add a wrapper if needed.
2454 if (WrapperKind != ISD::DELETED_NODE)
2455 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2456 // Add extra indirection if needed.
2457 if (ExtraLoad)
2458 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2459 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002460 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002461 }
Bill Wendling056292f2008-09-16 21:48:12 +00002462 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002463 unsigned char OpFlags = 0;
2464
Evan Cheng1bf891a2010-12-01 22:59:46 +00002465 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2466 // external symbols should go through the PLT.
2467 if (Subtarget->isTargetELF() &&
2468 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2469 OpFlags = X86II::MO_PLT;
2470 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002471 (!Subtarget->getTargetTriple().isMacOSX() ||
2472 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002473 // PC-relative references to external symbols should go through $stub,
2474 // unless we're building with the leopard linker or later, which
2475 // automatically synthesizes these stubs.
2476 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002477 }
Eric Christopherfd179292009-08-27 18:07:15 +00002478
Chris Lattner48a7d022009-07-09 05:02:21 +00002479 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2480 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002481 }
2482
Chris Lattnerd96d0722007-02-25 06:40:16 +00002483 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002484 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002485 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002486
Evan Chengf22f9b32010-02-06 03:28:46 +00002487 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002488 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2489 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002490 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002491 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002492
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002493 Ops.push_back(Chain);
2494 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002495
Dan Gohman98ca4f22009-08-05 01:29:28 +00002496 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002497 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002498
Gordon Henriksen86737662008-01-05 16:56:59 +00002499 // Add argument registers to the end of the list so that they are known live
2500 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002501 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2502 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2503 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002504
Evan Cheng586ccac2008-03-18 23:36:35 +00002505 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002506 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002507 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2508
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002509 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002510 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002511 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002512
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002513 // Experimental: Add a register mask operand representing the call-preserved
2514 // registers.
2515 if (UseRegMask) {
2516 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
Jakob Stoklund Olesen478a8a02012-02-02 23:52:57 +00002517 if (const uint32_t *Mask = TRI->getCallPreservedMask(CallConv))
2518 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002519 }
2520
Gabor Greifba36cb52008-08-28 21:40:38 +00002521 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002522 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002523
Dan Gohman98ca4f22009-08-05 01:29:28 +00002524 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002525 // We used to do:
2526 //// If this is the first return lowered for this function, add the regs
2527 //// to the liveout set for the function.
2528 // This isn't right, although it's probably harmless on x86; liveouts
2529 // should be computed from returns not tail calls. Consider a void
2530 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002531 return DAG.getNode(X86ISD::TC_RETURN, dl,
2532 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002533 }
2534
Dale Johannesenace16102009-02-03 19:33:06 +00002535 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002536 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002537
Chris Lattner2d297092006-05-23 18:50:38 +00002538 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002539 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002540 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2541 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002542 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002543 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
2544 IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002545 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002546 // pops the hidden struct pointer, so we have to push it back.
2547 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002548 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002549 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002550 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002551 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002552
Gordon Henriksenae636f82008-01-03 16:47:34 +00002553 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002554 if (!IsSibcall) {
2555 Chain = DAG.getCALLSEQ_END(Chain,
2556 DAG.getIntPtrConstant(NumBytes, true),
2557 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2558 true),
2559 InFlag);
2560 InFlag = Chain.getValue(1);
2561 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002562
Chris Lattner3085e152007-02-25 08:59:22 +00002563 // Handle result values, copying them out of physregs into vregs that we
2564 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002565 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2566 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002567}
2568
Evan Cheng25ab6902006-09-08 06:48:29 +00002569
2570//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002571// Fast Calling Convention (tail call) implementation
2572//===----------------------------------------------------------------------===//
2573
2574// Like std call, callee cleans arguments, convention except that ECX is
2575// reserved for storing the tail called function address. Only 2 registers are
2576// free for argument passing (inreg). Tail call optimization is performed
2577// provided:
2578// * tailcallopt is enabled
2579// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002580// On X86_64 architecture with GOT-style position independent code only local
2581// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002582// To keep the stack aligned according to platform abi the function
2583// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2584// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002585// If a tail called function callee has more arguments than the caller the
2586// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002587// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002588// original REtADDR, but before the saved framepointer or the spilled registers
2589// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2590// stack layout:
2591// arg1
2592// arg2
2593// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002594// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002595// move area ]
2596// (possible EBP)
2597// ESI
2598// EDI
2599// local1 ..
2600
2601/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2602/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002603unsigned
2604X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2605 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002606 MachineFunction &MF = DAG.getMachineFunction();
2607 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002608 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002609 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002610 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002611 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002612 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002613 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2614 // Number smaller than 12 so just add the difference.
2615 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2616 } else {
2617 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002618 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002619 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002620 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002621 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002622}
2623
Evan Cheng5f941932010-02-05 02:21:12 +00002624/// MatchingStackOffset - Return true if the given stack call argument is
2625/// already available in the same position (relatively) of the caller's
2626/// incoming argument stack.
2627static
2628bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2629 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2630 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002631 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2632 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002633 if (Arg.getOpcode() == ISD::CopyFromReg) {
2634 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002635 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002636 return false;
2637 MachineInstr *Def = MRI->getVRegDef(VR);
2638 if (!Def)
2639 return false;
2640 if (!Flags.isByVal()) {
2641 if (!TII->isLoadFromStackSlot(Def, FI))
2642 return false;
2643 } else {
2644 unsigned Opcode = Def->getOpcode();
2645 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2646 Def->getOperand(1).isFI()) {
2647 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002648 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002649 } else
2650 return false;
2651 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002652 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2653 if (Flags.isByVal())
2654 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002655 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002656 // define @foo(%struct.X* %A) {
2657 // tail call @bar(%struct.X* byval %A)
2658 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002659 return false;
2660 SDValue Ptr = Ld->getBasePtr();
2661 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2662 if (!FINode)
2663 return false;
2664 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002665 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002666 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002667 FI = FINode->getIndex();
2668 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002669 } else
2670 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002671
Evan Cheng4cae1332010-03-05 08:38:04 +00002672 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002673 if (!MFI->isFixedObjectIndex(FI))
2674 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002675 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002676}
2677
Dan Gohman98ca4f22009-08-05 01:29:28 +00002678/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2679/// for tail call optimization. Targets which want to do tail call
2680/// optimization should implement this function.
2681bool
2682X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002683 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002684 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002685 bool isCalleeStructRet,
2686 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002687 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002688 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002689 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002690 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002691 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002692 CalleeCC != CallingConv::C)
2693 return false;
2694
Evan Cheng7096ae42010-01-29 06:45:59 +00002695 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002696 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002697 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002698 CallingConv::ID CallerCC = CallerF->getCallingConv();
2699 bool CCMatch = CallerCC == CalleeCC;
2700
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002701 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002702 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002703 return true;
2704 return false;
2705 }
2706
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002707 // Look for obvious safe cases to perform tail call optimization that do not
2708 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002709
Evan Cheng2c12cb42010-03-26 16:26:03 +00002710 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2711 // emit a special epilogue.
2712 if (RegInfo->needsStackRealignment(MF))
2713 return false;
2714
Evan Chenga375d472010-03-15 18:54:48 +00002715 // Also avoid sibcall optimization if either caller or callee uses struct
2716 // return semantics.
2717 if (isCalleeStructRet || isCallerStructRet)
2718 return false;
2719
Chad Rosier2416da32011-06-24 21:15:36 +00002720 // An stdcall caller is expected to clean up its arguments; the callee
2721 // isn't going to do that.
2722 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2723 return false;
2724
Chad Rosier871f6642011-05-18 19:59:50 +00002725 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002726 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002727 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002728
2729 // Optimizing for varargs on Win64 is unlikely to be safe without
2730 // additional testing.
2731 if (Subtarget->isTargetWin64())
2732 return false;
2733
Chad Rosier871f6642011-05-18 19:59:50 +00002734 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002735 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2736 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002737
Chad Rosier871f6642011-05-18 19:59:50 +00002738 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2739 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2740 if (!ArgLocs[i].isRegLoc())
2741 return false;
2742 }
2743
Chad Rosier30450e82011-12-22 22:35:21 +00002744 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2745 // stack. Therefore, if it's not used by the call it is not safe to optimize
2746 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002747 bool Unused = false;
2748 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2749 if (!Ins[i].Used) {
2750 Unused = true;
2751 break;
2752 }
2753 }
2754 if (Unused) {
2755 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002756 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2757 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002758 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002759 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002760 CCValAssign &VA = RVLocs[i];
2761 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2762 return false;
2763 }
2764 }
2765
Evan Cheng13617962010-04-30 01:12:32 +00002766 // If the calling conventions do not match, then we'd better make sure the
2767 // results are returned in the same way as what the caller expects.
2768 if (!CCMatch) {
2769 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002770 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2771 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002772 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2773
2774 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002775 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2776 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002777 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2778
2779 if (RVLocs1.size() != RVLocs2.size())
2780 return false;
2781 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2782 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2783 return false;
2784 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2785 return false;
2786 if (RVLocs1[i].isRegLoc()) {
2787 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2788 return false;
2789 } else {
2790 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2791 return false;
2792 }
2793 }
2794 }
2795
Evan Chenga6bff982010-01-30 01:22:00 +00002796 // If the callee takes no arguments then go on to check the results of the
2797 // call.
2798 if (!Outs.empty()) {
2799 // Check if stack adjustment is needed. For now, do not do this if any
2800 // argument is passed on the stack.
2801 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002802 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2803 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002804
2805 // Allocate shadow area for Win64
2806 if (Subtarget->isTargetWin64()) {
2807 CCInfo.AllocateStack(32, 8);
2808 }
2809
Duncan Sands45907662010-10-31 13:21:44 +00002810 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002811 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002812 MachineFunction &MF = DAG.getMachineFunction();
2813 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2814 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002815
2816 // Check if the arguments are already laid out in the right way as
2817 // the caller's fixed stack objects.
2818 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002819 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2820 const X86InstrInfo *TII =
2821 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002822 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2823 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002824 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002825 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002826 if (VA.getLocInfo() == CCValAssign::Indirect)
2827 return false;
2828 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002829 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2830 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002831 return false;
2832 }
2833 }
2834 }
Evan Cheng9c044672010-05-29 01:35:22 +00002835
2836 // If the tailcall address may be in a register, then make sure it's
2837 // possible to register allocate for it. In 32-bit, the call address can
2838 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002839 // callee-saved registers are restored. These happen to be the same
2840 // registers used to pass 'inreg' arguments so watch out for those.
2841 if (!Subtarget->is64Bit() &&
2842 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002843 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002844 unsigned NumInRegs = 0;
2845 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2846 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002847 if (!VA.isRegLoc())
2848 continue;
2849 unsigned Reg = VA.getLocReg();
2850 switch (Reg) {
2851 default: break;
2852 case X86::EAX: case X86::EDX: case X86::ECX:
2853 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002854 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002855 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002856 }
2857 }
2858 }
Evan Chenga6bff982010-01-30 01:22:00 +00002859 }
Evan Chengb1712452010-01-27 06:25:16 +00002860
Evan Cheng86809cc2010-02-03 03:28:02 +00002861 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002862}
2863
Dan Gohman3df24e62008-09-03 23:12:08 +00002864FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002865X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2866 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002867}
2868
2869
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002870//===----------------------------------------------------------------------===//
2871// Other Lowering Hooks
2872//===----------------------------------------------------------------------===//
2873
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002874static bool MayFoldLoad(SDValue Op) {
2875 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2876}
2877
2878static bool MayFoldIntoStore(SDValue Op) {
2879 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2880}
2881
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002882static bool isTargetShuffle(unsigned Opcode) {
2883 switch(Opcode) {
2884 default: return false;
2885 case X86ISD::PSHUFD:
2886 case X86ISD::PSHUFHW:
2887 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002888 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002889 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002890 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002891 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002892 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002893 case X86ISD::MOVLPS:
2894 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002895 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002896 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002897 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002898 case X86ISD::MOVSS:
2899 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002900 case X86ISD::UNPCKL:
2901 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002902 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002903 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002904 return true;
2905 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002906}
2907
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002908static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002909 SDValue V1, SelectionDAG &DAG) {
2910 switch(Opc) {
2911 default: llvm_unreachable("Unknown x86 shuffle node");
2912 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002913 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002914 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002915 return DAG.getNode(Opc, dl, VT, V1);
2916 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002917}
2918
2919static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002920 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002921 switch(Opc) {
2922 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002923 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002924 case X86ISD::PSHUFHW:
2925 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002926 case X86ISD::VPERMILP:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002927 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2928 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002929}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002930
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002931static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2932 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2933 switch(Opc) {
2934 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002935 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00002936 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00002937 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002938 return DAG.getNode(Opc, dl, VT, V1, V2,
2939 DAG.getConstant(TargetMask, MVT::i8));
2940 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002941}
2942
2943static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2944 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2945 switch(Opc) {
2946 default: llvm_unreachable("Unknown x86 shuffle node");
2947 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002948 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002949 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002950 case X86ISD::MOVLPS:
2951 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002952 case X86ISD::MOVSS:
2953 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002954 case X86ISD::UNPCKL:
2955 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002956 return DAG.getNode(Opc, dl, VT, V1, V2);
2957 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002958}
2959
Dan Gohmand858e902010-04-17 15:26:15 +00002960SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002961 MachineFunction &MF = DAG.getMachineFunction();
2962 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2963 int ReturnAddrIndex = FuncInfo->getRAIndex();
2964
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002965 if (ReturnAddrIndex == 0) {
2966 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002967 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002968 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002969 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002970 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002971 }
2972
Evan Cheng25ab6902006-09-08 06:48:29 +00002973 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002974}
2975
2976
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002977bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2978 bool hasSymbolicDisplacement) {
2979 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002980 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002981 return false;
2982
2983 // If we don't have a symbolic displacement - we don't have any extra
2984 // restrictions.
2985 if (!hasSymbolicDisplacement)
2986 return true;
2987
2988 // FIXME: Some tweaks might be needed for medium code model.
2989 if (M != CodeModel::Small && M != CodeModel::Kernel)
2990 return false;
2991
2992 // For small code model we assume that latest object is 16MB before end of 31
2993 // bits boundary. We may also accept pretty large negative constants knowing
2994 // that all objects are in the positive half of address space.
2995 if (M == CodeModel::Small && Offset < 16*1024*1024)
2996 return true;
2997
2998 // For kernel code model we know that all object resist in the negative half
2999 // of 32bits address space. We may not accept negative offsets, since they may
3000 // be just off and we may accept pretty large positive ones.
3001 if (M == CodeModel::Kernel && Offset > 0)
3002 return true;
3003
3004 return false;
3005}
3006
Evan Chengef41ff62011-06-23 17:54:54 +00003007/// isCalleePop - Determines whether the callee is required to pop its
3008/// own arguments. Callee pop is necessary to support tail calls.
3009bool X86::isCalleePop(CallingConv::ID CallingConv,
3010 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3011 if (IsVarArg)
3012 return false;
3013
3014 switch (CallingConv) {
3015 default:
3016 return false;
3017 case CallingConv::X86_StdCall:
3018 return !is64Bit;
3019 case CallingConv::X86_FastCall:
3020 return !is64Bit;
3021 case CallingConv::X86_ThisCall:
3022 return !is64Bit;
3023 case CallingConv::Fast:
3024 return TailCallOpt;
3025 case CallingConv::GHC:
3026 return TailCallOpt;
3027 }
3028}
3029
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003030/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3031/// specific condition code, returning the condition code and the LHS/RHS of the
3032/// comparison to make.
3033static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3034 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003035 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003036 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3037 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3038 // X > -1 -> X == 0, jump !sign.
3039 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003040 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003041 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
3042 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003043 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00003044 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003045 // X < 1 -> X <= 0
3046 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003047 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003048 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003049 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003050
Evan Chengd9558e02006-01-06 00:43:03 +00003051 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003052 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003053 case ISD::SETEQ: return X86::COND_E;
3054 case ISD::SETGT: return X86::COND_G;
3055 case ISD::SETGE: return X86::COND_GE;
3056 case ISD::SETLT: return X86::COND_L;
3057 case ISD::SETLE: return X86::COND_LE;
3058 case ISD::SETNE: return X86::COND_NE;
3059 case ISD::SETULT: return X86::COND_B;
3060 case ISD::SETUGT: return X86::COND_A;
3061 case ISD::SETULE: return X86::COND_BE;
3062 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003063 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003064 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003065
Chris Lattner4c78e022008-12-23 23:42:27 +00003066 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003067
Chris Lattner4c78e022008-12-23 23:42:27 +00003068 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003069 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3070 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003071 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3072 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003073 }
3074
Chris Lattner4c78e022008-12-23 23:42:27 +00003075 switch (SetCCOpcode) {
3076 default: break;
3077 case ISD::SETOLT:
3078 case ISD::SETOLE:
3079 case ISD::SETUGT:
3080 case ISD::SETUGE:
3081 std::swap(LHS, RHS);
3082 break;
3083 }
3084
3085 // On a floating point condition, the flags are set as follows:
3086 // ZF PF CF op
3087 // 0 | 0 | 0 | X > Y
3088 // 0 | 0 | 1 | X < Y
3089 // 1 | 0 | 0 | X == Y
3090 // 1 | 1 | 1 | unordered
3091 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003092 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003093 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003094 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003095 case ISD::SETOLT: // flipped
3096 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003097 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003098 case ISD::SETOLE: // flipped
3099 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003100 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003101 case ISD::SETUGT: // flipped
3102 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003103 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003104 case ISD::SETUGE: // flipped
3105 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003106 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003107 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003108 case ISD::SETNE: return X86::COND_NE;
3109 case ISD::SETUO: return X86::COND_P;
3110 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003111 case ISD::SETOEQ:
3112 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003113 }
Evan Chengd9558e02006-01-06 00:43:03 +00003114}
3115
Evan Cheng4a460802006-01-11 00:33:36 +00003116/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3117/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003118/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003119static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003120 switch (X86CC) {
3121 default:
3122 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003123 case X86::COND_B:
3124 case X86::COND_BE:
3125 case X86::COND_E:
3126 case X86::COND_P:
3127 case X86::COND_A:
3128 case X86::COND_AE:
3129 case X86::COND_NE:
3130 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003131 return true;
3132 }
3133}
3134
Evan Chengeb2f9692009-10-27 19:56:55 +00003135/// isFPImmLegal - Returns true if the target can instruction select the
3136/// specified FP immediate natively. If false, the legalizer will
3137/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003138bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003139 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3140 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3141 return true;
3142 }
3143 return false;
3144}
3145
Nate Begeman9008ca62009-04-27 18:41:29 +00003146/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3147/// the specified range (L, H].
3148static bool isUndefOrInRange(int Val, int Low, int Hi) {
3149 return (Val < 0) || (Val >= Low && Val < Hi);
3150}
3151
3152/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3153/// specified value.
3154static bool isUndefOrEqual(int Val, int CmpVal) {
3155 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003156 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003157 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003158}
3159
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003160/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3161/// from position Pos and ending in Pos+Size, falls within the specified
3162/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003163static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003164 int Pos, int Size, int Low) {
3165 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3166 if (!isUndefOrEqual(Mask[i], Low))
3167 return false;
3168 return true;
3169}
3170
Nate Begeman9008ca62009-04-27 18:41:29 +00003171/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3172/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3173/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003174static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003175 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003176 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003177 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 return (Mask[0] < 2 && Mask[1] < 2);
3179 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003180}
3181
Nate Begeman9008ca62009-04-27 18:41:29 +00003182bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003183 return ::isPSHUFDMask(N->getMask(), N->getValueType(0));
Nate Begeman9008ca62009-04-27 18:41:29 +00003184}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003185
Nate Begeman9008ca62009-04-27 18:41:29 +00003186/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3187/// is suitable for input to PSHUFHW.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003188static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003189 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003190 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003191
Nate Begeman9008ca62009-04-27 18:41:29 +00003192 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003193 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3194 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003195
Evan Cheng506d3df2006-03-29 23:07:14 +00003196 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003197 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003198 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003199 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003200
Evan Cheng506d3df2006-03-29 23:07:14 +00003201 return true;
3202}
3203
Nate Begeman9008ca62009-04-27 18:41:29 +00003204bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003205 return ::isPSHUFHWMask(N->getMask(), N->getValueType(0));
Nate Begeman9008ca62009-04-27 18:41:29 +00003206}
Evan Cheng506d3df2006-03-29 23:07:14 +00003207
Nate Begeman9008ca62009-04-27 18:41:29 +00003208/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3209/// is suitable for input to PSHUFLW.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003210static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003211 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003212 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003213
Rafael Espindola15684b22009-04-24 12:40:33 +00003214 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003215 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3216 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003217
Rafael Espindola15684b22009-04-24 12:40:33 +00003218 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003219 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003220 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003221 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003222
Rafael Espindola15684b22009-04-24 12:40:33 +00003223 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003224}
3225
Nate Begeman9008ca62009-04-27 18:41:29 +00003226bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003227 return ::isPSHUFLWMask(N->getMask(), N->getValueType(0));
Nate Begeman9008ca62009-04-27 18:41:29 +00003228}
3229
Nate Begemana09008b2009-10-19 02:17:23 +00003230/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3231/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003232static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3233 const X86Subtarget *Subtarget) {
3234 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3235 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003236 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003237
Craig Topper0e2037b2012-01-20 05:53:00 +00003238 unsigned NumElts = VT.getVectorNumElements();
3239 unsigned NumLanes = VT.getSizeInBits()/128;
3240 unsigned NumLaneElts = NumElts/NumLanes;
3241
3242 // Do not handle 64-bit element shuffles with palignr.
3243 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003244 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003245
Craig Topper0e2037b2012-01-20 05:53:00 +00003246 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3247 unsigned i;
3248 for (i = 0; i != NumLaneElts; ++i) {
3249 if (Mask[i+l] >= 0)
3250 break;
3251 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003252
Craig Topper0e2037b2012-01-20 05:53:00 +00003253 // Lane is all undef, go to next lane
3254 if (i == NumLaneElts)
3255 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003256
Craig Topper0e2037b2012-01-20 05:53:00 +00003257 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003258
Craig Topper0e2037b2012-01-20 05:53:00 +00003259 // Make sure its in this lane in one of the sources
3260 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3261 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003262 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003263
3264 // If not lane 0, then we must match lane 0
3265 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3266 return false;
3267
3268 // Correct second source to be contiguous with first source
3269 if (Start >= (int)NumElts)
3270 Start -= NumElts - NumLaneElts;
3271
3272 // Make sure we're shifting in the right direction.
3273 if (Start <= (int)(i+l))
3274 return false;
3275
3276 Start -= i;
3277
3278 // Check the rest of the elements to see if they are consecutive.
3279 for (++i; i != NumLaneElts; ++i) {
3280 int Idx = Mask[i+l];
3281
3282 // Make sure its in this lane
3283 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3284 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3285 return false;
3286
3287 // If not lane 0, then we must match lane 0
3288 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3289 return false;
3290
3291 if (Idx >= (int)NumElts)
3292 Idx -= NumElts - NumLaneElts;
3293
3294 if (!isUndefOrEqual(Idx, Start+i))
3295 return false;
3296
3297 }
Nate Begemana09008b2009-10-19 02:17:23 +00003298 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003299
Nate Begemana09008b2009-10-19 02:17:23 +00003300 return true;
3301}
3302
Craig Topper1a7700a2012-01-19 08:19:12 +00003303/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3304/// the two vector operands have swapped position.
3305static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3306 unsigned NumElems) {
3307 for (unsigned i = 0; i != NumElems; ++i) {
3308 int idx = Mask[i];
3309 if (idx < 0)
3310 continue;
3311 else if (idx < (int)NumElems)
3312 Mask[i] = idx + NumElems;
3313 else
3314 Mask[i] = idx - NumElems;
3315 }
3316}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003317
Craig Topper1a7700a2012-01-19 08:19:12 +00003318/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3319/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3320/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3321/// reverse of what x86 shuffles want.
3322static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3323 bool Commuted = false) {
3324 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003325 return false;
3326
Craig Topper1a7700a2012-01-19 08:19:12 +00003327 unsigned NumElems = VT.getVectorNumElements();
3328 unsigned NumLanes = VT.getSizeInBits()/128;
3329 unsigned NumLaneElems = NumElems/NumLanes;
3330
3331 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003332 return false;
3333
3334 // VSHUFPSY divides the resulting vector into 4 chunks.
3335 // The sources are also splitted into 4 chunks, and each destination
3336 // chunk must come from a different source chunk.
3337 //
3338 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3339 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3340 //
3341 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3342 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3343 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003344 // VSHUFPDY divides the resulting vector into 4 chunks.
3345 // The sources are also splitted into 4 chunks, and each destination
3346 // chunk must come from a different source chunk.
3347 //
3348 // SRC1 => X3 X2 X1 X0
3349 // SRC2 => Y3 Y2 Y1 Y0
3350 //
3351 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3352 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003353 unsigned HalfLaneElems = NumLaneElems/2;
3354 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3355 for (unsigned i = 0; i != NumLaneElems; ++i) {
3356 int Idx = Mask[i+l];
3357 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3358 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3359 return false;
3360 // For VSHUFPSY, the mask of the second half must be the same as the
3361 // first but with the appropriate offsets. This works in the same way as
3362 // VPERMILPS works with masks.
3363 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3364 continue;
3365 if (!isUndefOrEqual(Idx, Mask[i]+l))
3366 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003367 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003368 }
3369
3370 return true;
3371}
3372
Craig Topper1a7700a2012-01-19 08:19:12 +00003373bool X86::isSHUFPMask(ShuffleVectorSDNode *N, bool HasAVX) {
3374 return ::isSHUFPMask(N->getMask(), N->getValueType(0), HasAVX);
Evan Cheng39623da2006-04-20 08:58:49 +00003375}
3376
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003377/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3378/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003379bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003380 EVT VT = N->getValueType(0);
3381 unsigned NumElems = VT.getVectorNumElements();
3382
3383 if (VT.getSizeInBits() != 128)
3384 return false;
3385
3386 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003387 return false;
3388
Evan Cheng2064a2b2006-03-28 06:50:32 +00003389 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003390 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3391 isUndefOrEqual(N->getMaskElt(1), 7) &&
3392 isUndefOrEqual(N->getMaskElt(2), 2) &&
3393 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003394}
3395
Nate Begeman0b10b912009-11-07 23:17:15 +00003396/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3397/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3398/// <2, 3, 2, 3>
3399bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003400 EVT VT = N->getValueType(0);
3401 unsigned NumElems = VT.getVectorNumElements();
3402
3403 if (VT.getSizeInBits() != 128)
3404 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003405
Nate Begeman0b10b912009-11-07 23:17:15 +00003406 if (NumElems != 4)
3407 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003408
Nate Begeman0b10b912009-11-07 23:17:15 +00003409 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003410 isUndefOrEqual(N->getMaskElt(1), 3) &&
3411 isUndefOrEqual(N->getMaskElt(2), 2) &&
3412 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003413}
3414
Evan Cheng5ced1d82006-04-06 23:23:56 +00003415/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3416/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003417bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003418 EVT VT = N->getValueType(0);
3419
3420 if (VT.getSizeInBits() != 128)
3421 return false;
3422
Nate Begeman9008ca62009-04-27 18:41:29 +00003423 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003424
Evan Cheng5ced1d82006-04-06 23:23:56 +00003425 if (NumElems != 2 && NumElems != 4)
3426 return false;
3427
Evan Chengc5cdff22006-04-07 21:53:05 +00003428 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003429 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003430 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003431
Evan Chengc5cdff22006-04-07 21:53:05 +00003432 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003433 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003434 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003435
3436 return true;
3437}
3438
Nate Begeman0b10b912009-11-07 23:17:15 +00003439/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3440/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3441bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003442 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003443
David Greenea20244d2011-03-02 17:23:43 +00003444 if ((NumElems != 2 && NumElems != 4)
3445 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003446 return false;
3447
Evan Chengc5cdff22006-04-07 21:53:05 +00003448 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003449 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003450 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003451
Nate Begeman9008ca62009-04-27 18:41:29 +00003452 for (unsigned i = 0; i < NumElems/2; ++i)
3453 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003454 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003455
3456 return true;
3457}
3458
Evan Cheng0038e592006-03-28 00:39:58 +00003459/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3460/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003461static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003462 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003463 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003464
3465 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3466 "Unsupported vector type for unpckh");
3467
Craig Topper6347e862011-11-21 06:57:39 +00003468 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003469 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003470 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003471
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003472 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3473 // independently on 128-bit lanes.
3474 unsigned NumLanes = VT.getSizeInBits()/128;
3475 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003476
Craig Topper94438ba2011-12-16 08:06:31 +00003477 for (unsigned l = 0; l != NumLanes; ++l) {
3478 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3479 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003480 i += 2, ++j) {
3481 int BitI = Mask[i];
3482 int BitI1 = Mask[i+1];
3483 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003484 return false;
David Greenea20244d2011-03-02 17:23:43 +00003485 if (V2IsSplat) {
3486 if (!isUndefOrEqual(BitI1, NumElts))
3487 return false;
3488 } else {
3489 if (!isUndefOrEqual(BitI1, j + NumElts))
3490 return false;
3491 }
Evan Cheng39623da2006-04-20 08:58:49 +00003492 }
Evan Cheng0038e592006-03-28 00:39:58 +00003493 }
David Greenea20244d2011-03-02 17:23:43 +00003494
Evan Cheng0038e592006-03-28 00:39:58 +00003495 return true;
3496}
3497
Craig Topper6347e862011-11-21 06:57:39 +00003498bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003499 return ::isUNPCKLMask(N->getMask(), N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003500}
3501
Evan Cheng4fcb9222006-03-28 02:43:26 +00003502/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3503/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003504static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003505 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003506 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003507
3508 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3509 "Unsupported vector type for unpckh");
3510
Craig Topper6347e862011-11-21 06:57:39 +00003511 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003512 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003513 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003514
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003515 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3516 // independently on 128-bit lanes.
3517 unsigned NumLanes = VT.getSizeInBits()/128;
3518 unsigned NumLaneElts = NumElts/NumLanes;
3519
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003520 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003521 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3522 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003523 int BitI = Mask[i];
3524 int BitI1 = Mask[i+1];
3525 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003526 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003527 if (V2IsSplat) {
3528 if (isUndefOrEqual(BitI1, NumElts))
3529 return false;
3530 } else {
3531 if (!isUndefOrEqual(BitI1, j+NumElts))
3532 return false;
3533 }
Evan Cheng39623da2006-04-20 08:58:49 +00003534 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003535 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003536 return true;
3537}
3538
Craig Topper6347e862011-11-21 06:57:39 +00003539bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003540 return ::isUNPCKHMask(N->getMask(), N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003541}
3542
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003543/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3544/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3545/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003546static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003547 bool HasAVX2) {
3548 unsigned NumElts = VT.getVectorNumElements();
3549
3550 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3551 "Unsupported vector type for unpckh");
3552
3553 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3554 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003555 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003556
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003557 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3558 // FIXME: Need a better way to get rid of this, there's no latency difference
3559 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3560 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003561 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003562 return false;
3563
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003564 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3565 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003566 unsigned NumLanes = VT.getSizeInBits()/128;
3567 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003568
Craig Topper94438ba2011-12-16 08:06:31 +00003569 for (unsigned l = 0; l != NumLanes; ++l) {
3570 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3571 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003572 i += 2, ++j) {
3573 int BitI = Mask[i];
3574 int BitI1 = Mask[i+1];
3575
3576 if (!isUndefOrEqual(BitI, j))
3577 return false;
3578 if (!isUndefOrEqual(BitI1, j))
3579 return false;
3580 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003581 }
David Greenea20244d2011-03-02 17:23:43 +00003582
Rafael Espindola15684b22009-04-24 12:40:33 +00003583 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003584}
3585
Craig Topper94438ba2011-12-16 08:06:31 +00003586bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N, bool HasAVX2) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003587 return ::isUNPCKL_v_undef_Mask(N->getMask(), N->getValueType(0), HasAVX2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003588}
3589
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003590/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3591/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3592/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003593static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003594 unsigned NumElts = VT.getVectorNumElements();
3595
3596 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3597 "Unsupported vector type for unpckh");
3598
3599 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3600 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003601 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003602
Craig Topper94438ba2011-12-16 08:06:31 +00003603 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3604 // independently on 128-bit lanes.
3605 unsigned NumLanes = VT.getSizeInBits()/128;
3606 unsigned NumLaneElts = NumElts/NumLanes;
3607
3608 for (unsigned l = 0; l != NumLanes; ++l) {
3609 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3610 i != (l+1)*NumLaneElts; i += 2, ++j) {
3611 int BitI = Mask[i];
3612 int BitI1 = Mask[i+1];
3613 if (!isUndefOrEqual(BitI, j))
3614 return false;
3615 if (!isUndefOrEqual(BitI1, j))
3616 return false;
3617 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003618 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003619 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003620}
3621
Craig Topper94438ba2011-12-16 08:06:31 +00003622bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N, bool HasAVX2) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003623 return ::isUNPCKH_v_undef_Mask(N->getMask(), N->getValueType(0), HasAVX2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003624}
3625
Evan Cheng017dcc62006-04-21 01:05:10 +00003626/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3627/// specifies a shuffle of elements that is suitable for input to MOVSS,
3628/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003629static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003630 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003631 return false;
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003632 if (VT.getSizeInBits() == 256)
3633 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003634
Craig Topperc612d792012-01-02 09:17:37 +00003635 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003636
Nate Begeman9008ca62009-04-27 18:41:29 +00003637 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003638 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003639
Craig Topperc612d792012-01-02 09:17:37 +00003640 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003641 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003642 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003643
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003644 return true;
3645}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003646
Nate Begeman9008ca62009-04-27 18:41:29 +00003647bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003648 return ::isMOVLMask(N->getMask(), N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003649}
3650
Craig Topper70b883b2011-11-28 10:14:51 +00003651/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003652/// as permutations between 128-bit chunks or halves. As an example: this
3653/// shuffle bellow:
3654/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3655/// The first half comes from the second half of V1 and the second half from the
3656/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003657static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003658 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003659 return false;
3660
3661 // The shuffle result is divided into half A and half B. In total the two
3662 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3663 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003664 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003665 bool MatchA = false, MatchB = false;
3666
3667 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003668 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003669 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3670 MatchA = true;
3671 break;
3672 }
3673 }
3674
3675 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003676 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003677 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3678 MatchB = true;
3679 break;
3680 }
3681 }
3682
3683 return MatchA && MatchB;
3684}
3685
Craig Topper70b883b2011-11-28 10:14:51 +00003686/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3687/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003688static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003689 EVT VT = SVOp->getValueType(0);
3690
Craig Topperc612d792012-01-02 09:17:37 +00003691 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003692
Craig Topperc612d792012-01-02 09:17:37 +00003693 unsigned FstHalf = 0, SndHalf = 0;
3694 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003695 if (SVOp->getMaskElt(i) > 0) {
3696 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3697 break;
3698 }
3699 }
Craig Topperc612d792012-01-02 09:17:37 +00003700 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003701 if (SVOp->getMaskElt(i) > 0) {
3702 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3703 break;
3704 }
3705 }
3706
3707 return (FstHalf | (SndHalf << 4));
3708}
3709
Craig Topper70b883b2011-11-28 10:14:51 +00003710/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003711/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3712/// Note that VPERMIL mask matching is different depending whether theunderlying
3713/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3714/// to the same elements of the low, but to the higher half of the source.
3715/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003716/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003717static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003718 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003719 return false;
3720
Craig Topperc612d792012-01-02 09:17:37 +00003721 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003722 // Only match 256-bit with 32/64-bit types
3723 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003724 return false;
3725
Craig Topperc612d792012-01-02 09:17:37 +00003726 unsigned NumLanes = VT.getSizeInBits()/128;
3727 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003728 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003729 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003730 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003731 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003732 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003733 continue;
3734 // VPERMILPS handling
3735 if (Mask[i] < 0)
3736 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003737 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003738 return false;
3739 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003740 }
3741
3742 return true;
3743}
3744
Evan Cheng017dcc62006-04-21 01:05:10 +00003745/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3746/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003747/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003748static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003749 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topperc612d792012-01-02 09:17:37 +00003750 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003751 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003752 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003753
Nate Begeman9008ca62009-04-27 18:41:29 +00003754 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003755 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003756
Craig Topperc612d792012-01-02 09:17:37 +00003757 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003758 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3759 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3760 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003761 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003762
Evan Cheng39623da2006-04-20 08:58:49 +00003763 return true;
3764}
3765
Nate Begeman9008ca62009-04-27 18:41:29 +00003766static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003767 bool V2IsUndef = false) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003768 return isCommutedMOVLMask(N->getMask(), N->getValueType(0),
3769 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003770}
3771
Evan Chengd9539472006-04-14 21:59:03 +00003772/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3773/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003774/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3775bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3776 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003777 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003778 return false;
3779
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003780 // The second vector must be undef
3781 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3782 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003783
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003784 EVT VT = N->getValueType(0);
3785 unsigned NumElems = VT.getVectorNumElements();
3786
3787 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3788 (VT.getSizeInBits() == 256 && NumElems != 8))
3789 return false;
3790
3791 // "i+1" is the value the indexed mask element must have
3792 for (unsigned i = 0; i < NumElems; i += 2)
3793 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3794 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003795 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003796
3797 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003798}
3799
3800/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3801/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003802/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3803bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3804 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003805 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003806 return false;
3807
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003808 // The second vector must be undef
3809 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3810 return false;
3811
3812 EVT VT = N->getValueType(0);
3813 unsigned NumElems = VT.getVectorNumElements();
3814
3815 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3816 (VT.getSizeInBits() == 256 && NumElems != 8))
3817 return false;
3818
3819 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003820 for (unsigned i = 0; i != NumElems; i += 2)
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003821 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3822 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003823 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003824
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003825 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003826}
3827
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003828/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3829/// specifies a shuffle of elements that is suitable for input to 256-bit
3830/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003831static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topperc612d792012-01-02 09:17:37 +00003832 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003833
Craig Topperbeabc6c2011-12-05 06:56:46 +00003834 if (!HasAVX || VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003835 return false;
3836
Craig Topperc612d792012-01-02 09:17:37 +00003837 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003838 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003839 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003840 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003841 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003842 return false;
3843 return true;
3844}
3845
Evan Cheng0b457f02008-09-25 20:50:48 +00003846/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003847/// specifies a shuffle of elements that is suitable for input to 128-bit
3848/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003849bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003850 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003851
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003852 if (VT.getSizeInBits() != 128)
3853 return false;
3854
Craig Topperc612d792012-01-02 09:17:37 +00003855 unsigned e = VT.getVectorNumElements() / 2;
3856 for (unsigned i = 0; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003857 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003858 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003859 for (unsigned i = 0; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003860 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003861 return false;
3862 return true;
3863}
3864
David Greenec38a03e2011-02-03 15:50:00 +00003865/// isVEXTRACTF128Index - Return true if the specified
3866/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3867/// suitable for input to VEXTRACTF128.
3868bool X86::isVEXTRACTF128Index(SDNode *N) {
3869 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3870 return false;
3871
3872 // The index should be aligned on a 128-bit boundary.
3873 uint64_t Index =
3874 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3875
3876 unsigned VL = N->getValueType(0).getVectorNumElements();
3877 unsigned VBits = N->getValueType(0).getSizeInBits();
3878 unsigned ElSize = VBits / VL;
3879 bool Result = (Index * ElSize) % 128 == 0;
3880
3881 return Result;
3882}
3883
David Greeneccacdc12011-02-04 16:08:29 +00003884/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3885/// operand specifies a subvector insert that is suitable for input to
3886/// VINSERTF128.
3887bool X86::isVINSERTF128Index(SDNode *N) {
3888 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3889 return false;
3890
3891 // The index should be aligned on a 128-bit boundary.
3892 uint64_t Index =
3893 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3894
3895 unsigned VL = N->getValueType(0).getVectorNumElements();
3896 unsigned VBits = N->getValueType(0).getSizeInBits();
3897 unsigned ElSize = VBits / VL;
3898 bool Result = (Index * ElSize) % 128 == 0;
3899
3900 return Result;
3901}
3902
Evan Cheng63d33002006-03-22 08:01:21 +00003903/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003904/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00003905/// Handles 128-bit and 256-bit.
3906unsigned X86::getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
3907 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003908
Craig Topper1a7700a2012-01-19 08:19:12 +00003909 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3910 "Unsupported vector type for PSHUF/SHUFP");
3911
3912 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
3913 // independently on 128-bit lanes.
3914 unsigned NumElts = VT.getVectorNumElements();
3915 unsigned NumLanes = VT.getSizeInBits()/128;
3916 unsigned NumLaneElts = NumElts/NumLanes;
3917
3918 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
3919 "Only supports 2 or 4 elements per lane");
3920
3921 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00003922 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00003923 for (unsigned i = 0; i != NumElts; ++i) {
3924 int Elt = N->getMaskElt(i);
3925 if (Elt < 0) continue;
3926 Elt %= NumLaneElts;
3927 unsigned ShAmt = i << Shift;
3928 if (ShAmt >= 8) ShAmt -= 8;
3929 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00003930 }
Craig Topper1a7700a2012-01-19 08:19:12 +00003931
Evan Cheng63d33002006-03-22 08:01:21 +00003932 return Mask;
3933}
3934
Evan Cheng506d3df2006-03-29 23:07:14 +00003935/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003936/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003937unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003938 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003939 unsigned Mask = 0;
3940 // 8 nodes, but we only care about the last 4.
3941 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003942 int Val = SVOp->getMaskElt(i);
3943 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003944 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003945 if (i != 4)
3946 Mask <<= 2;
3947 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003948 return Mask;
3949}
3950
3951/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003952/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003953unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003954 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003955 unsigned Mask = 0;
3956 // 8 nodes, but we only care about the first 4.
3957 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003958 int Val = SVOp->getMaskElt(i);
3959 if (Val >= 0)
3960 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003961 if (i != 0)
3962 Mask <<= 2;
3963 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003964 return Mask;
3965}
3966
Nate Begemana09008b2009-10-19 02:17:23 +00003967/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3968/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00003969static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
3970 EVT VT = SVOp->getValueType(0);
3971 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00003972
Craig Topper0e2037b2012-01-20 05:53:00 +00003973 unsigned NumElts = VT.getVectorNumElements();
3974 unsigned NumLanes = VT.getSizeInBits()/128;
3975 unsigned NumLaneElts = NumElts/NumLanes;
3976
3977 int Val = 0;
3978 unsigned i;
3979 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00003980 Val = SVOp->getMaskElt(i);
3981 if (Val >= 0)
3982 break;
3983 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003984 if (Val >= (int)NumElts)
3985 Val -= NumElts - NumLaneElts;
3986
Eli Friedman63f8dde2011-07-25 21:36:45 +00003987 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00003988 return (Val - i) * EltSize;
3989}
3990
David Greenec38a03e2011-02-03 15:50:00 +00003991/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3992/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3993/// instructions.
3994unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3995 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3996 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3997
3998 uint64_t Index =
3999 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4000
4001 EVT VecVT = N->getOperand(0).getValueType();
4002 EVT ElVT = VecVT.getVectorElementType();
4003
4004 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004005 return Index / NumElemsPerChunk;
4006}
4007
David Greeneccacdc12011-02-04 16:08:29 +00004008/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4009/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4010/// instructions.
4011unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4012 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4013 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4014
4015 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004016 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004017
4018 EVT VecVT = N->getValueType(0);
4019 EVT ElVT = VecVT.getVectorElementType();
4020
4021 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004022 return Index / NumElemsPerChunk;
4023}
4024
Evan Cheng37b73872009-07-30 08:33:02 +00004025/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4026/// constant +0.0.
4027bool X86::isZeroNode(SDValue Elt) {
4028 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004029 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004030 (isa<ConstantFPSDNode>(Elt) &&
4031 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4032}
4033
Nate Begeman9008ca62009-04-27 18:41:29 +00004034/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4035/// their permute mask.
4036static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4037 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004038 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004039 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004040 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004041
Nate Begeman5a5ca152009-04-29 05:20:52 +00004042 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004043 int idx = SVOp->getMaskElt(i);
4044 if (idx < 0)
4045 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004046 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004047 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004048 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004049 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004050 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004051 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4052 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004053}
4054
Evan Cheng533a0aa2006-04-19 20:35:22 +00004055/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4056/// match movhlps. The lower half elements should come from upper half of
4057/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004058/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004059static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004060 EVT VT = Op->getValueType(0);
4061 if (VT.getSizeInBits() != 128)
4062 return false;
4063 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004064 return false;
4065 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004066 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004067 return false;
4068 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004069 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004070 return false;
4071 return true;
4072}
4073
Evan Cheng5ced1d82006-04-06 23:23:56 +00004074/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004075/// is promoted to a vector. It also returns the LoadSDNode by reference if
4076/// required.
4077static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004078 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4079 return false;
4080 N = N->getOperand(0).getNode();
4081 if (!ISD::isNON_EXTLoad(N))
4082 return false;
4083 if (LD)
4084 *LD = cast<LoadSDNode>(N);
4085 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004086}
4087
Dan Gohman65fd6562011-11-03 21:49:52 +00004088// Test whether the given value is a vector value which will be legalized
4089// into a load.
4090static bool WillBeConstantPoolLoad(SDNode *N) {
4091 if (N->getOpcode() != ISD::BUILD_VECTOR)
4092 return false;
4093
4094 // Check for any non-constant elements.
4095 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4096 switch (N->getOperand(i).getNode()->getOpcode()) {
4097 case ISD::UNDEF:
4098 case ISD::ConstantFP:
4099 case ISD::Constant:
4100 break;
4101 default:
4102 return false;
4103 }
4104
4105 // Vectors of all-zeros and all-ones are materialized with special
4106 // instructions rather than being loaded.
4107 return !ISD::isBuildVectorAllZeros(N) &&
4108 !ISD::isBuildVectorAllOnes(N);
4109}
4110
Evan Cheng533a0aa2006-04-19 20:35:22 +00004111/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4112/// match movlp{s|d}. The lower half elements should come from lower half of
4113/// V1 (and in order), and the upper half elements should come from the upper
4114/// half of V2 (and in order). And since V1 will become the source of the
4115/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004116static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4117 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004118 EVT VT = Op->getValueType(0);
4119 if (VT.getSizeInBits() != 128)
4120 return false;
4121
Evan Cheng466685d2006-10-09 20:57:25 +00004122 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004123 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004124 // Is V2 is a vector load, don't do this transformation. We will try to use
4125 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004126 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004127 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004128
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004129 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004130
Evan Cheng533a0aa2006-04-19 20:35:22 +00004131 if (NumElems != 2 && NumElems != 4)
4132 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004133 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004134 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004135 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004136 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004137 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004138 return false;
4139 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004140}
4141
Evan Cheng39623da2006-04-20 08:58:49 +00004142/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4143/// all the same.
4144static bool isSplatVector(SDNode *N) {
4145 if (N->getOpcode() != ISD::BUILD_VECTOR)
4146 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004147
Dan Gohman475871a2008-07-27 21:46:04 +00004148 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004149 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4150 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004151 return false;
4152 return true;
4153}
4154
Evan Cheng213d2cf2007-05-17 18:45:50 +00004155/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004156/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004157/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004158static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004159 SDValue V1 = N->getOperand(0);
4160 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004161 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4162 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004163 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004164 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004165 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004166 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4167 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004168 if (Opc != ISD::BUILD_VECTOR ||
4169 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004170 return false;
4171 } else if (Idx >= 0) {
4172 unsigned Opc = V1.getOpcode();
4173 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4174 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004175 if (Opc != ISD::BUILD_VECTOR ||
4176 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004177 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004178 }
4179 }
4180 return true;
4181}
4182
4183/// getZeroVector - Returns a vector of specified type with all zero elements.
4184///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004185static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004186 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004187 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004188
Dale Johannesen0488fb62010-09-30 23:57:10 +00004189 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004190 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004191 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004192 if (VT.getSizeInBits() == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004193 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004194 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4195 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4196 } else { // SSE1
4197 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4198 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4199 }
4200 } else if (VT.getSizeInBits() == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004201 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004202 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4203 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4204 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4205 } else {
4206 // 256-bit logic and arithmetic instructions in AVX are all
4207 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4208 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4209 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4210 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4211 }
Evan Chengf0df0312008-05-15 08:39:06 +00004212 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004213 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004214}
4215
Chris Lattner8a594482007-11-25 00:24:49 +00004216/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004217/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4218/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4219/// Then bitcast to their original type, ensuring they get CSE'd.
4220static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4221 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004222 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004223 assert((VT.is128BitVector() || VT.is256BitVector())
4224 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004225
Owen Anderson825b72b2009-08-11 20:47:22 +00004226 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004227 SDValue Vec;
4228 if (VT.getSizeInBits() == 256) {
4229 if (HasAVX2) { // AVX2
4230 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4231 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4232 } else { // AVX
4233 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4234 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4235 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4236 Vec = Insert128BitVector(InsV, Vec,
4237 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4238 }
4239 } else {
4240 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004241 }
4242
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004243 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004244}
4245
Evan Cheng39623da2006-04-20 08:58:49 +00004246/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4247/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004248static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004249 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004250 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004251
Evan Cheng39623da2006-04-20 08:58:49 +00004252 bool Changed = false;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004253 SmallVector<int, 8> MaskVec(SVOp->getMask().begin(), SVOp->getMask().end());
Eric Christopherfd179292009-08-27 18:07:15 +00004254
Nate Begeman5a5ca152009-04-29 05:20:52 +00004255 for (unsigned i = 0; i != NumElems; ++i) {
4256 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004257 MaskVec[i] = NumElems;
4258 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004259 }
Evan Cheng39623da2006-04-20 08:58:49 +00004260 }
Evan Cheng39623da2006-04-20 08:58:49 +00004261 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004262 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4263 SVOp->getOperand(1), &MaskVec[0]);
4264 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004265}
4266
Evan Cheng017dcc62006-04-21 01:05:10 +00004267/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4268/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004269static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004270 SDValue V2) {
4271 unsigned NumElems = VT.getVectorNumElements();
4272 SmallVector<int, 8> Mask;
4273 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004274 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004275 Mask.push_back(i);
4276 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004277}
4278
Nate Begeman9008ca62009-04-27 18:41:29 +00004279/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004280static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004281 SDValue V2) {
4282 unsigned NumElems = VT.getVectorNumElements();
4283 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004284 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004285 Mask.push_back(i);
4286 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004287 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004288 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004289}
4290
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004291/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004292static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004293 SDValue V2) {
4294 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004295 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004296 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004297 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004298 Mask.push_back(i + Half);
4299 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004300 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004301 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004302}
4303
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004304// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004305// a generic shuffle instruction because the target has no such instructions.
4306// Generate shuffles which repeat i16 and i8 several times until they can be
4307// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004308static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004309 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004310 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004311 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004312
Nate Begeman9008ca62009-04-27 18:41:29 +00004313 while (NumElems > 4) {
4314 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004315 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004316 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004317 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004318 EltNo -= NumElems/2;
4319 }
4320 NumElems >>= 1;
4321 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004322 return V;
4323}
Eric Christopherfd179292009-08-27 18:07:15 +00004324
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004325/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4326static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4327 EVT VT = V.getValueType();
4328 DebugLoc dl = V.getDebugLoc();
4329 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4330 && "Vector size not supported");
4331
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004332 if (VT.getSizeInBits() == 128) {
4333 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004334 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004335 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4336 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004337 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004338 // To use VPERMILPS to splat scalars, the second half of indicies must
4339 // refer to the higher part, which is a duplication of the lower one,
4340 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004341 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4342 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004343
4344 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4345 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4346 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004347 }
4348
4349 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4350}
4351
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004352/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004353static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4354 EVT SrcVT = SV->getValueType(0);
4355 SDValue V1 = SV->getOperand(0);
4356 DebugLoc dl = SV->getDebugLoc();
4357
4358 int EltNo = SV->getSplatIndex();
4359 int NumElems = SrcVT.getVectorNumElements();
4360 unsigned Size = SrcVT.getSizeInBits();
4361
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004362 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4363 "Unknown how to promote splat for type");
4364
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004365 // Extract the 128-bit part containing the splat element and update
4366 // the splat element index when it refers to the higher register.
4367 if (Size == 256) {
Nadav Rotemd2070b02012-01-12 15:31:55 +00004368 unsigned Idx = (EltNo >= NumElems/2) ? NumElems/2 : 0;
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004369 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4370 if (Idx > 0)
4371 EltNo -= NumElems/2;
4372 }
4373
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004374 // All i16 and i8 vector types can't be used directly by a generic shuffle
4375 // instruction because the target has no such instruction. Generate shuffles
4376 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004377 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004378 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004379 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004380 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004381
4382 // Recreate the 256-bit vector and place the same 128-bit vector
4383 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004384 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004385 if (Size == 256) {
4386 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4387 DAG.getConstant(0, MVT::i32), DAG, dl);
4388 V1 = Insert128BitVector(InsV, V1,
4389 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4390 }
4391
4392 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004393}
4394
Evan Chengba05f722006-04-21 23:03:30 +00004395/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004396/// vector of zero or undef vector. This produces a shuffle where the low
4397/// element of V2 is swizzled into the zero/undef vector, landing at element
4398/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Dan Gohman475871a2008-07-27 21:46:04 +00004399static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004400 bool IsZero,
4401 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004402 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004403 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004404 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004405 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004406 unsigned NumElems = VT.getVectorNumElements();
4407 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004408 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004409 // If this is the insertion idx, put the low elt of V2 here.
4410 MaskVec.push_back(i == Idx ? NumElems : i);
4411 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004412}
4413
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004414/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4415/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004416static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4417 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004418 if (Depth == 6)
4419 return SDValue(); // Limit search depth.
4420
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004421 SDValue V = SDValue(N, 0);
4422 EVT VT = V.getValueType();
4423 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004424
4425 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4426 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4427 Index = SV->getMaskElt(Index);
4428
4429 if (Index < 0)
4430 return DAG.getUNDEF(VT.getVectorElementType());
4431
Craig Topperd156dc12012-02-06 07:17:51 +00004432 unsigned NumElems = VT.getVectorNumElements();
4433 SDValue NewV = (Index < (int)NumElems) ? SV->getOperand(0)
4434 : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004435 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004436 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004437
4438 // Recurse into target specific vector shuffles to find scalars.
4439 if (isTargetShuffle(Opcode)) {
Craig Topperd156dc12012-02-06 07:17:51 +00004440 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004441 SmallVector<unsigned, 16> ShuffleMask;
4442 SDValue ImmN;
4443
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004444 switch(Opcode) {
Craig Topperb3982da2011-12-31 23:50:21 +00004445 case X86ISD::SHUFP:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004446 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper36e36ac2011-11-29 07:49:05 +00004447 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4448 ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004449 break;
Craig Topper34671b82011-12-06 08:21:25 +00004450 case X86ISD::UNPCKH:
Craig Topper3d8c2ce2011-12-06 05:31:16 +00004451 DecodeUNPCKHMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004452 break;
Craig Topper34671b82011-12-06 08:21:25 +00004453 case X86ISD::UNPCKL:
Craig Topper3d8c2ce2011-12-06 05:31:16 +00004454 DecodeUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004455 break;
4456 case X86ISD::MOVHLPS:
4457 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4458 break;
4459 case X86ISD::MOVLHPS:
4460 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4461 break;
4462 case X86ISD::PSHUFD:
Craig Topperd156dc12012-02-06 07:17:51 +00004463 case X86ISD::VPERMILP:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004464 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topperd156dc12012-02-06 07:17:51 +00004465 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004466 ShuffleMask);
4467 break;
4468 case X86ISD::PSHUFHW:
4469 ImmN = N->getOperand(N->getNumOperands()-1);
4470 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4471 ShuffleMask);
4472 break;
4473 case X86ISD::PSHUFLW:
4474 ImmN = N->getOperand(N->getNumOperands()-1);
4475 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4476 ShuffleMask);
4477 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004478 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004479 case X86ISD::MOVSD: {
4480 // The index 0 always comes from the first element of the second source,
4481 // this is why MOVSS and MOVSD are used in the first place. The other
4482 // elements come from the other positions of the first source vector.
4483 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004484 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4485 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004486 }
Craig Topperec24e612011-11-30 07:47:51 +00004487 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004488 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topperd156dc12012-02-06 07:17:51 +00004489 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004490 ShuffleMask);
4491 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004492 case X86ISD::MOVDDUP:
4493 case X86ISD::MOVLHPD:
4494 case X86ISD::MOVLPD:
4495 case X86ISD::MOVLPS:
4496 case X86ISD::MOVSHDUP:
4497 case X86ISD::MOVSLDUP:
4498 case X86ISD::PALIGN:
4499 return SDValue(); // Not yet implemented.
Craig Topperabb94d02012-02-05 03:43:23 +00004500 default: llvm_unreachable("unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004501 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004502
4503 Index = ShuffleMask[Index];
4504 if (Index < 0)
4505 return DAG.getUNDEF(VT.getVectorElementType());
4506
Craig Topperd156dc12012-02-06 07:17:51 +00004507 SDValue NewV = (Index < (int)NumElems) ? N->getOperand(0)
4508 : N->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004509 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4510 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004511 }
4512
4513 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004514 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004515 V = V.getOperand(0);
4516 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004517 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004518
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004519 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004520 return SDValue();
4521 }
4522
4523 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4524 return (Index == 0) ? V.getOperand(0)
4525 : DAG.getUNDEF(VT.getVectorElementType());
4526
4527 if (V.getOpcode() == ISD::BUILD_VECTOR)
4528 return V.getOperand(Index);
4529
4530 return SDValue();
4531}
4532
4533/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4534/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004535/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004536static
4537unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4538 bool ZerosFromLeft, SelectionDAG &DAG) {
4539 int i = 0;
4540
4541 while (i < NumElems) {
4542 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004543 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004544 if (!(Elt.getNode() &&
4545 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4546 break;
4547 ++i;
4548 }
4549
4550 return i;
4551}
4552
4553/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4554/// MaskE correspond consecutively to elements from one of the vector operands,
4555/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4556static
4557bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4558 int OpIdx, int NumElems, unsigned &OpNum) {
4559 bool SeenV1 = false;
4560 bool SeenV2 = false;
4561
4562 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4563 int Idx = SVOp->getMaskElt(i);
4564 // Ignore undef indicies
4565 if (Idx < 0)
4566 continue;
4567
4568 if (Idx < NumElems)
4569 SeenV1 = true;
4570 else
4571 SeenV2 = true;
4572
4573 // Only accept consecutive elements from the same vector
4574 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4575 return false;
4576 }
4577
4578 OpNum = SeenV1 ? 0 : 1;
4579 return true;
4580}
4581
4582/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4583/// logical left shift of a vector.
4584static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4585 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4586 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4587 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4588 false /* check zeros from right */, DAG);
4589 unsigned OpSrc;
4590
4591 if (!NumZeros)
4592 return false;
4593
4594 // Considering the elements in the mask that are not consecutive zeros,
4595 // check if they consecutively come from only one of the source vectors.
4596 //
4597 // V1 = {X, A, B, C} 0
4598 // \ \ \ /
4599 // vector_shuffle V1, V2 <1, 2, 3, X>
4600 //
4601 if (!isShuffleMaskConsecutive(SVOp,
4602 0, // Mask Start Index
4603 NumElems-NumZeros-1, // Mask End Index
4604 NumZeros, // Where to start looking in the src vector
4605 NumElems, // Number of elements in vector
4606 OpSrc)) // Which source operand ?
4607 return false;
4608
4609 isLeft = false;
4610 ShAmt = NumZeros;
4611 ShVal = SVOp->getOperand(OpSrc);
4612 return true;
4613}
4614
4615/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4616/// logical left shift of a vector.
4617static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4618 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4619 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4620 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4621 true /* check zeros from left */, DAG);
4622 unsigned OpSrc;
4623
4624 if (!NumZeros)
4625 return false;
4626
4627 // Considering the elements in the mask that are not consecutive zeros,
4628 // check if they consecutively come from only one of the source vectors.
4629 //
4630 // 0 { A, B, X, X } = V2
4631 // / \ / /
4632 // vector_shuffle V1, V2 <X, X, 4, 5>
4633 //
4634 if (!isShuffleMaskConsecutive(SVOp,
4635 NumZeros, // Mask Start Index
4636 NumElems-1, // Mask End Index
4637 0, // Where to start looking in the src vector
4638 NumElems, // Number of elements in vector
4639 OpSrc)) // Which source operand ?
4640 return false;
4641
4642 isLeft = true;
4643 ShAmt = NumZeros;
4644 ShVal = SVOp->getOperand(OpSrc);
4645 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004646}
4647
4648/// isVectorShift - Returns true if the shuffle can be implemented as a
4649/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004650static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004651 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004652 // Although the logic below support any bitwidth size, there are no
4653 // shift instructions which handle more than 128-bit vectors.
4654 if (SVOp->getValueType(0).getSizeInBits() > 128)
4655 return false;
4656
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004657 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4658 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4659 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004660
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004661 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004662}
4663
Evan Chengc78d3b42006-04-24 18:01:45 +00004664/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4665///
Dan Gohman475871a2008-07-27 21:46:04 +00004666static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004667 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004668 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004669 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004670 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004671 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004672 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004673
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004674 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004675 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004676 bool First = true;
4677 for (unsigned i = 0; i < 16; ++i) {
4678 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4679 if (ThisIsNonZero && First) {
4680 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004681 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004682 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004683 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004684 First = false;
4685 }
4686
4687 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004688 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004689 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4690 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004691 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004692 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004693 }
4694 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004695 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4696 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4697 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004698 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004699 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004700 } else
4701 ThisElt = LastElt;
4702
Gabor Greifba36cb52008-08-28 21:40:38 +00004703 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004704 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004705 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004706 }
4707 }
4708
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004709 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004710}
4711
Bill Wendlinga348c562007-03-22 18:42:45 +00004712/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004713///
Dan Gohman475871a2008-07-27 21:46:04 +00004714static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004715 unsigned NumNonZero, unsigned NumZero,
4716 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004717 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004718 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004719 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004720 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004721
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004722 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004723 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004724 bool First = true;
4725 for (unsigned i = 0; i < 8; ++i) {
4726 bool isNonZero = (NonZeros & (1 << i)) != 0;
4727 if (isNonZero) {
4728 if (First) {
4729 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004730 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004731 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004732 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004733 First = false;
4734 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004735 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004736 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004737 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004738 }
4739 }
4740
4741 return V;
4742}
4743
Evan Chengf26ffe92008-05-29 08:22:04 +00004744/// getVShift - Return a vector logical shift node.
4745///
Owen Andersone50ed302009-08-10 22:56:29 +00004746static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004747 unsigned NumBits, SelectionDAG &DAG,
4748 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004749 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004750 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004751 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004752 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4753 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004754 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004755 DAG.getConstant(NumBits,
4756 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004757}
4758
Dan Gohman475871a2008-07-27 21:46:04 +00004759SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004760X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004761 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004762
Evan Chengc3630942009-12-09 21:00:30 +00004763 // Check if the scalar load can be widened into a vector load. And if
4764 // the address is "base + cst" see if the cst can be "absorbed" into
4765 // the shuffle mask.
4766 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4767 SDValue Ptr = LD->getBasePtr();
4768 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4769 return SDValue();
4770 EVT PVT = LD->getValueType(0);
4771 if (PVT != MVT::i32 && PVT != MVT::f32)
4772 return SDValue();
4773
4774 int FI = -1;
4775 int64_t Offset = 0;
4776 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4777 FI = FINode->getIndex();
4778 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004779 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004780 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4781 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4782 Offset = Ptr.getConstantOperandVal(1);
4783 Ptr = Ptr.getOperand(0);
4784 } else {
4785 return SDValue();
4786 }
4787
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004788 // FIXME: 256-bit vector instructions don't require a strict alignment,
4789 // improve this code to support it better.
4790 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004791 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004792 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004793 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004794 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004795 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004796 // Can't change the alignment. FIXME: It's possible to compute
4797 // the exact stack offset and reference FI + adjust offset instead.
4798 // If someone *really* cares about this. That's the way to implement it.
4799 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004800 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004801 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004802 }
4803 }
4804
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004805 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004806 // Ptr + (Offset & ~15).
4807 if (Offset < 0)
4808 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004809 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004810 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004811 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004812 if (StartOffset)
4813 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4814 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4815
4816 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004817 int NumElems = VT.getVectorNumElements();
4818
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004819 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4820 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004821 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004822 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004823
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004824 SmallVector<int, 8> Mask;
4825 for (int i = 0; i < NumElems; ++i)
4826 Mask.push_back(EltNo);
4827
Craig Toppercc3000632012-01-30 07:50:31 +00004828 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004829 }
4830
4831 return SDValue();
4832}
4833
Michael J. Spencerec38de22010-10-10 22:04:20 +00004834/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4835/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004836/// load which has the same value as a build_vector whose operands are 'elts'.
4837///
4838/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004839///
Nate Begeman1449f292010-03-24 22:19:06 +00004840/// FIXME: we'd also like to handle the case where the last elements are zero
4841/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4842/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004843static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004844 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004845 EVT EltVT = VT.getVectorElementType();
4846 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004847
Nate Begemanfdea31a2010-03-24 20:49:50 +00004848 LoadSDNode *LDBase = NULL;
4849 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004850
Nate Begeman1449f292010-03-24 22:19:06 +00004851 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004852 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004853 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004854 for (unsigned i = 0; i < NumElems; ++i) {
4855 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004856
Nate Begemanfdea31a2010-03-24 20:49:50 +00004857 if (!Elt.getNode() ||
4858 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4859 return SDValue();
4860 if (!LDBase) {
4861 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4862 return SDValue();
4863 LDBase = cast<LoadSDNode>(Elt.getNode());
4864 LastLoadedElt = i;
4865 continue;
4866 }
4867 if (Elt.getOpcode() == ISD::UNDEF)
4868 continue;
4869
4870 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4871 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4872 return SDValue();
4873 LastLoadedElt = i;
4874 }
Nate Begeman1449f292010-03-24 22:19:06 +00004875
4876 // If we have found an entire vector of loads and undefs, then return a large
4877 // load of the entire vector width starting at the base pointer. If we found
4878 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004879 if (LastLoadedElt == NumElems - 1) {
4880 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004881 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004882 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004883 LDBase->isVolatile(), LDBase->isNonTemporal(),
4884 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004885 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004886 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004887 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004888 LDBase->isInvariant(), LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00004889 } else if (NumElems == 4 && LastLoadedElt == 1 &&
4890 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004891 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4892 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00004893 SDValue ResNode =
4894 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
4895 LDBase->getPointerInfo(),
4896 LDBase->getAlignment(),
4897 false/*isVolatile*/, true/*ReadMem*/,
4898 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004899 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004900 }
4901 return SDValue();
4902}
4903
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004904/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
4905/// a vbroadcast node. We support two patterns:
4906/// 1. A splat BUILD_VECTOR which uses a single scalar load.
4907/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
4908/// a scalar load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004909/// The scalar load node is returned when a pattern is found,
4910/// or SDValue() otherwise.
Craig Toppera9376332012-01-10 08:23:59 +00004911static SDValue isVectorBroadcast(SDValue &Op, const X86Subtarget *Subtarget) {
4912 if (!Subtarget->hasAVX())
4913 return SDValue();
4914
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004915 EVT VT = Op.getValueType();
4916 SDValue V = Op;
4917
4918 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
4919 V = V.getOperand(0);
4920
4921 //A suspected load to be broadcasted.
4922 SDValue Ld;
4923
4924 switch (V.getOpcode()) {
4925 default:
4926 // Unknown pattern found.
4927 return SDValue();
4928
4929 case ISD::BUILD_VECTOR: {
4930 // The BUILD_VECTOR node must be a splat.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004931 if (!isSplatVector(V.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004932 return SDValue();
4933
4934 Ld = V.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004935
4936 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004937 // of its users are from the BUILD_VECTOR node.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004938 if (!Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004939 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004940 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004941 }
4942
4943 case ISD::VECTOR_SHUFFLE: {
4944 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
4945
4946 // Shuffles must have a splat mask where the first element is
4947 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004948 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004949 return SDValue();
4950
4951 SDValue Sc = Op.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004952 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004953 return SDValue();
4954
4955 Ld = Sc.getOperand(0);
4956
4957 // The scalar_to_vector node and the suspected
4958 // load node must have exactly one user.
4959 if (!Sc.hasOneUse() || !Ld.hasOneUse())
4960 return SDValue();
4961 break;
4962 }
4963 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004964
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004965 // The scalar source must be a normal load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004966 if (!ISD::isNormalLoad(Ld.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004967 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004968
Craig Toppera1902a12012-02-01 06:51:58 +00004969 // Reject loads that have uses of the chain result
4970 if (Ld->hasAnyUseOfValue(1))
4971 return SDValue();
4972
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004973 bool Is256 = VT.getSizeInBits() == 256;
4974 bool Is128 = VT.getSizeInBits() == 128;
4975 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
4976
4977 // VBroadcast to YMM
4978 if (Is256 && (ScalarSize == 32 || ScalarSize == 64))
4979 return Ld;
4980
4981 // VBroadcast to XMM
4982 if (Is128 && (ScalarSize == 32))
4983 return Ld;
4984
Craig Toppera9376332012-01-10 08:23:59 +00004985 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
4986 // double since there is vbroadcastsd xmm
4987 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
4988 // VBroadcast to YMM
4989 if (Is256 && (ScalarSize == 8 || ScalarSize == 16))
4990 return Ld;
4991
4992 // VBroadcast to XMM
4993 if (Is128 && (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64))
4994 return Ld;
4995 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004996
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004997 // Unsupported broadcast.
4998 return SDValue();
4999}
5000
Evan Chengc3630942009-12-09 21:00:30 +00005001SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005002X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005003 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005004
David Greenef125a292011-02-08 19:04:41 +00005005 EVT VT = Op.getValueType();
5006 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005007 unsigned NumElems = Op.getNumOperands();
5008
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005009 // Vectors containing all zeros can be matched by pxor and xorps later
5010 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5011 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5012 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005013 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005014 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005015
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005016 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005017 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005018
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005019 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005020 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5021 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005022 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005023 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005024 return Op;
5025
Craig Topper07a27622012-01-22 03:07:48 +00005026 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005027 }
5028
Craig Toppera9376332012-01-10 08:23:59 +00005029 SDValue LD = isVectorBroadcast(Op, Subtarget);
5030 if (LD.getNode())
5031 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005032
Owen Andersone50ed302009-08-10 22:56:29 +00005033 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005034
Evan Cheng0db9fe62006-04-25 20:13:52 +00005035 unsigned NumZero = 0;
5036 unsigned NumNonZero = 0;
5037 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005038 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005039 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005040 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005041 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005042 if (Elt.getOpcode() == ISD::UNDEF)
5043 continue;
5044 Values.insert(Elt);
5045 if (Elt.getOpcode() != ISD::Constant &&
5046 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005047 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005048 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005049 NumZero++;
5050 else {
5051 NonZeros |= (1 << i);
5052 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005053 }
5054 }
5055
Chris Lattner97a2a562010-08-26 05:24:29 +00005056 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5057 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005058 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005059
Chris Lattner67f453a2008-03-09 05:42:06 +00005060 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005061 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005062 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005063 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005064
Chris Lattner62098042008-03-09 01:05:04 +00005065 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5066 // the value are obviously zero, truncate the value to i32 and do the
5067 // insertion that way. Only do this if the value is non-constant or if the
5068 // value is a constant being inserted into element 0. It is cheaper to do
5069 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005070 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005071 (!IsAllConstants || Idx == 0)) {
5072 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005073 // Handle SSE only.
5074 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5075 EVT VecVT = MVT::v4i32;
5076 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005077
Chris Lattner62098042008-03-09 01:05:04 +00005078 // Truncate the value (which may itself be a constant) to i32, and
5079 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005080 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005081 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005082 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005083
Chris Lattner62098042008-03-09 01:05:04 +00005084 // Now we have our 32-bit value zero extended in the low element of
5085 // a vector. If Idx != 0, swizzle it into place.
5086 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005087 SmallVector<int, 4> Mask;
5088 Mask.push_back(Idx);
5089 for (unsigned i = 1; i != VecElts; ++i)
5090 Mask.push_back(i);
5091 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005092 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005093 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005094 }
Craig Topper07a27622012-01-22 03:07:48 +00005095 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005096 }
5097 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005098
Chris Lattner19f79692008-03-08 22:59:52 +00005099 // If we have a constant or non-constant insertion into the low element of
5100 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5101 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005102 // depending on what the source datatype is.
5103 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005104 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005105 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005106
5107 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005108 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005109 if (VT.getSizeInBits() == 256) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005110 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005111 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5112 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005113 }
Craig Topperd62c16e2011-12-29 03:20:51 +00005114 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005115 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5116 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005117 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005118 }
5119
5120 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005121 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005122 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper19ec2a92011-12-29 03:34:54 +00005123 if (VT.getSizeInBits() == 256) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005124 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005125 Item = Insert128BitVector(ZeroVec, Item, DAG.getConstant(0, MVT::i32),
5126 DAG, dl);
5127 } else {
5128 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005129 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005130 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005131 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005132 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005133 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005134
5135 // Is it a vector logical left shift?
5136 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005137 X86::isZeroNode(Op.getOperand(0)) &&
5138 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005139 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005140 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005141 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005142 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005143 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005144 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005145
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005146 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005147 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005148
Chris Lattner19f79692008-03-08 22:59:52 +00005149 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5150 // is a non-constant being inserted into an element other than the low one,
5151 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5152 // movd/movss) to move this into the low element, then shuffle it into
5153 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005154 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005155 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005156
Evan Cheng0db9fe62006-04-25 20:13:52 +00005157 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005158 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005159 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005160 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005161 MaskVec.push_back(i == Idx ? 0 : 1);
5162 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005163 }
5164 }
5165
Chris Lattner67f453a2008-03-09 05:42:06 +00005166 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005167 if (Values.size() == 1) {
5168 if (EVTBits == 32) {
5169 // Instead of a shuffle like this:
5170 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5171 // Check if it's possible to issue this instead.
5172 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5173 unsigned Idx = CountTrailingZeros_32(NonZeros);
5174 SDValue Item = Op.getOperand(Idx);
5175 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5176 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5177 }
Dan Gohman475871a2008-07-27 21:46:04 +00005178 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005179 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005180
Dan Gohmana3941172007-07-24 22:55:08 +00005181 // A vector full of immediates; various special cases are already
5182 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005183 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005184 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005185
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005186 // For AVX-length vectors, build the individual 128-bit pieces and use
5187 // shuffles to put them in place.
Craig Topperfa5b70e2012-02-03 06:32:21 +00005188 if (VT.getSizeInBits() == 256) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005189 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005190 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005191 V.push_back(Op.getOperand(i));
5192
5193 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5194
5195 // Build both the lower and upper subvector.
5196 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5197 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5198 NumElems/2);
5199
5200 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005201 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5202 DAG.getConstant(0, MVT::i32), DAG, dl);
5203 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005204 DAG, dl);
5205 }
5206
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005207 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005208 if (EVTBits == 64) {
5209 if (NumNonZero == 1) {
5210 // One half is zero or undef.
5211 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005212 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005213 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005214 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005215 }
Dan Gohman475871a2008-07-27 21:46:04 +00005216 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005217 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005218
5219 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005220 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005221 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005222 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005223 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005224 }
5225
Bill Wendling826f36f2007-03-28 00:57:11 +00005226 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005227 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005228 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005229 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005230 }
5231
5232 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005233 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005234 if (NumElems == 4 && NumZero > 0) {
5235 for (unsigned i = 0; i < 4; ++i) {
5236 bool isZero = !(NonZeros & (1 << i));
5237 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005238 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005239 else
Dale Johannesenace16102009-02-03 19:33:06 +00005240 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005241 }
5242
5243 for (unsigned i = 0; i < 2; ++i) {
5244 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5245 default: break;
5246 case 0:
5247 V[i] = V[i*2]; // Must be a zero vector.
5248 break;
5249 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005250 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005251 break;
5252 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005253 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005254 break;
5255 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005256 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005257 break;
5258 }
5259 }
5260
Benjamin Kramer9c683542012-01-30 15:16:21 +00005261 bool Reverse1 = (NonZeros & 0x3) == 2;
5262 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5263 int MaskVec[] = {
5264 Reverse1 ? 1 : 0,
5265 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005266 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5267 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005268 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005269 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005270 }
5271
Nate Begemanfdea31a2010-03-24 20:49:50 +00005272 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5273 // Check for a build vector of consecutive loads.
5274 for (unsigned i = 0; i < NumElems; ++i)
5275 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005276
Nate Begemanfdea31a2010-03-24 20:49:50 +00005277 // Check for elements which are consecutive loads.
5278 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5279 if (LD.getNode())
5280 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005281
5282 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005283 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005284 SDValue Result;
5285 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5286 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5287 else
5288 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005289
Chris Lattner24faf612010-08-28 17:59:08 +00005290 for (unsigned i = 1; i < NumElems; ++i) {
5291 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5292 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005293 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005294 }
5295 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005296 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005297
Chris Lattner6e80e442010-08-28 17:15:43 +00005298 // Otherwise, expand into a number of unpckl*, start by extending each of
5299 // our (non-undef) elements to the full vector width with the element in the
5300 // bottom slot of the vector (which generates no code for SSE).
5301 for (unsigned i = 0; i < NumElems; ++i) {
5302 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5303 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5304 else
5305 V[i] = DAG.getUNDEF(VT);
5306 }
5307
5308 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005309 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5310 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5311 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005312 unsigned EltStride = NumElems >> 1;
5313 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005314 for (unsigned i = 0; i < EltStride; ++i) {
5315 // If V[i+EltStride] is undef and this is the first round of mixing,
5316 // then it is safe to just drop this shuffle: V[i] is already in the
5317 // right place, the one element (since it's the first round) being
5318 // inserted as undef can be dropped. This isn't safe for successive
5319 // rounds because they will permute elements within both vectors.
5320 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5321 EltStride == NumElems/2)
5322 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005323
Chris Lattner6e80e442010-08-28 17:15:43 +00005324 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005325 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005326 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005327 }
5328 return V[0];
5329 }
Dan Gohman475871a2008-07-27 21:46:04 +00005330 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005331}
5332
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005333// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5334// them in a MMX register. This is better than doing a stack convert.
5335static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005336 DebugLoc dl = Op.getDebugLoc();
5337 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005338
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005339 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5340 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5341 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005342 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005343 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5344 InVec = Op.getOperand(1);
5345 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5346 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005347 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005348 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5349 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5350 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005351 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005352 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5353 Mask[0] = 0; Mask[1] = 2;
5354 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5355 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005356 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005357}
5358
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005359// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5360// to create 256-bit vectors from two other 128-bit ones.
5361static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5362 DebugLoc dl = Op.getDebugLoc();
5363 EVT ResVT = Op.getValueType();
5364
5365 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5366
5367 SDValue V1 = Op.getOperand(0);
5368 SDValue V2 = Op.getOperand(1);
5369 unsigned NumElems = ResVT.getVectorNumElements();
5370
5371 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5372 DAG.getConstant(0, MVT::i32), DAG, dl);
5373 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5374 DAG, dl);
5375}
5376
5377SDValue
5378X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005379 EVT ResVT = Op.getValueType();
5380
5381 assert(Op.getNumOperands() == 2);
5382 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5383 "Unsupported CONCAT_VECTORS for value type");
5384
5385 // We support concatenate two MMX registers and place them in a MMX register.
5386 // This is better than doing a stack convert.
5387 if (ResVT.is128BitVector())
5388 return LowerMMXCONCAT_VECTORS(Op, DAG);
5389
5390 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5391 // from two other 128-bit ones.
5392 return LowerAVXCONCAT_VECTORS(Op, DAG);
5393}
5394
Nate Begemanb9a47b82009-02-23 08:49:38 +00005395// v8i16 shuffles - Prefer shuffles in the following order:
5396// 1. [all] pshuflw, pshufhw, optional move
5397// 2. [ssse3] 1 x pshufb
5398// 3. [ssse3] 2 x pshufb + 1 x por
5399// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005400SDValue
5401X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5402 SelectionDAG &DAG) const {
5403 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005404 SDValue V1 = SVOp->getOperand(0);
5405 SDValue V2 = SVOp->getOperand(1);
5406 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005407 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005408
Nate Begemanb9a47b82009-02-23 08:49:38 +00005409 // Determine if more than 1 of the words in each of the low and high quadwords
5410 // of the result come from the same quadword of one of the two inputs. Undef
5411 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005412 unsigned LoQuad[] = { 0, 0, 0, 0 };
5413 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005414 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005415 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005416 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005417 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005418 MaskVals.push_back(EltIdx);
5419 if (EltIdx < 0) {
5420 ++Quad[0];
5421 ++Quad[1];
5422 ++Quad[2];
5423 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005424 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005425 }
5426 ++Quad[EltIdx / 4];
5427 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005428 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005429
Nate Begemanb9a47b82009-02-23 08:49:38 +00005430 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005431 unsigned MaxQuad = 1;
5432 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005433 if (LoQuad[i] > MaxQuad) {
5434 BestLoQuad = i;
5435 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005436 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005437 }
5438
Nate Begemanb9a47b82009-02-23 08:49:38 +00005439 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005440 MaxQuad = 1;
5441 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005442 if (HiQuad[i] > MaxQuad) {
5443 BestHiQuad = i;
5444 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005445 }
5446 }
5447
Nate Begemanb9a47b82009-02-23 08:49:38 +00005448 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005449 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005450 // single pshufb instruction is necessary. If There are more than 2 input
5451 // quads, disable the next transformation since it does not help SSSE3.
5452 bool V1Used = InputQuads[0] || InputQuads[1];
5453 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005454 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005455 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005456 BestLoQuad = InputQuads[0] ? 0 : 1;
5457 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005458 }
5459 if (InputQuads.count() > 2) {
5460 BestLoQuad = -1;
5461 BestHiQuad = -1;
5462 }
5463 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005464
Nate Begemanb9a47b82009-02-23 08:49:38 +00005465 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5466 // the shuffle mask. If a quad is scored as -1, that means that it contains
5467 // words from all 4 input quadwords.
5468 SDValue NewV;
5469 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005470 int MaskV[] = {
5471 BestLoQuad < 0 ? 0 : BestLoQuad,
5472 BestHiQuad < 0 ? 1 : BestHiQuad
5473 };
Eric Christopherfd179292009-08-27 18:07:15 +00005474 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005475 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5476 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5477 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005478
Nate Begemanb9a47b82009-02-23 08:49:38 +00005479 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5480 // source words for the shuffle, to aid later transformations.
5481 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005482 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005483 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005484 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005485 if (idx != (int)i)
5486 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005487 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005488 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005489 AllWordsInNewV = false;
5490 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005491 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005492
Nate Begemanb9a47b82009-02-23 08:49:38 +00005493 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5494 if (AllWordsInNewV) {
5495 for (int i = 0; i != 8; ++i) {
5496 int idx = MaskVals[i];
5497 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005498 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005499 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005500 if ((idx != i) && idx < 4)
5501 pshufhw = false;
5502 if ((idx != i) && idx > 3)
5503 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005504 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005505 V1 = NewV;
5506 V2Used = false;
5507 BestLoQuad = 0;
5508 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005509 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005510
Nate Begemanb9a47b82009-02-23 08:49:38 +00005511 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5512 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005513 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005514 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5515 unsigned TargetMask = 0;
5516 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005517 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005518 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5519 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5520 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005521 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005522 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005523 }
Eric Christopherfd179292009-08-27 18:07:15 +00005524
Nate Begemanb9a47b82009-02-23 08:49:38 +00005525 // If we have SSSE3, and all words of the result are from 1 input vector,
5526 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5527 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005528 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005529 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005530
Nate Begemanb9a47b82009-02-23 08:49:38 +00005531 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005532 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005533 // mask, and elements that come from V1 in the V2 mask, so that the two
5534 // results can be OR'd together.
5535 bool TwoInputs = V1Used && V2Used;
5536 for (unsigned i = 0; i != 8; ++i) {
5537 int EltIdx = MaskVals[i] * 2;
5538 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005539 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5540 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005541 continue;
5542 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005543 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5544 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005545 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005546 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005547 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005548 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005549 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005550 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005551 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005552
Nate Begemanb9a47b82009-02-23 08:49:38 +00005553 // Calculate the shuffle mask for the second input, shuffle it, and
5554 // OR it with the first shuffled input.
5555 pshufbMask.clear();
5556 for (unsigned i = 0; i != 8; ++i) {
5557 int EltIdx = MaskVals[i] * 2;
5558 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005559 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5560 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005561 continue;
5562 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005563 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5564 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005565 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005566 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005567 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005568 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005569 MVT::v16i8, &pshufbMask[0], 16));
5570 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005571 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005572 }
5573
5574 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5575 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005576 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005577 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005578 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005579 for (int i = 0; i != 4; ++i) {
5580 int idx = MaskVals[i];
5581 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005582 InOrder.set(i);
5583 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005584 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005585 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005586 }
5587 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005588 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005589 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005590
Craig Topperd0a31172012-01-10 06:37:29 +00005591 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005592 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5593 NewV.getOperand(0),
5594 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5595 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005596 }
Eric Christopherfd179292009-08-27 18:07:15 +00005597
Nate Begemanb9a47b82009-02-23 08:49:38 +00005598 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5599 // and update MaskVals with the new element order.
5600 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005601 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005602 for (unsigned i = 4; i != 8; ++i) {
5603 int idx = MaskVals[i];
5604 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005605 InOrder.set(i);
5606 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005607 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005608 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005609 }
5610 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005611 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005612 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005613
Craig Topperd0a31172012-01-10 06:37:29 +00005614 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005615 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5616 NewV.getOperand(0),
5617 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5618 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005619 }
Eric Christopherfd179292009-08-27 18:07:15 +00005620
Nate Begemanb9a47b82009-02-23 08:49:38 +00005621 // In case BestHi & BestLo were both -1, which means each quadword has a word
5622 // from each of the four input quadwords, calculate the InOrder bitvector now
5623 // before falling through to the insert/extract cleanup.
5624 if (BestLoQuad == -1 && BestHiQuad == -1) {
5625 NewV = V1;
5626 for (int i = 0; i != 8; ++i)
5627 if (MaskVals[i] < 0 || MaskVals[i] == i)
5628 InOrder.set(i);
5629 }
Eric Christopherfd179292009-08-27 18:07:15 +00005630
Nate Begemanb9a47b82009-02-23 08:49:38 +00005631 // The other elements are put in the right place using pextrw and pinsrw.
5632 for (unsigned i = 0; i != 8; ++i) {
5633 if (InOrder[i])
5634 continue;
5635 int EltIdx = MaskVals[i];
5636 if (EltIdx < 0)
5637 continue;
5638 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005639 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005640 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005641 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005642 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005643 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005644 DAG.getIntPtrConstant(i));
5645 }
5646 return NewV;
5647}
5648
5649// v16i8 shuffles - Prefer shuffles in the following order:
5650// 1. [ssse3] 1 x pshufb
5651// 2. [ssse3] 2 x pshufb + 1 x por
5652// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5653static
Nate Begeman9008ca62009-04-27 18:41:29 +00005654SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005655 SelectionDAG &DAG,
5656 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005657 SDValue V1 = SVOp->getOperand(0);
5658 SDValue V2 = SVOp->getOperand(1);
5659 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005660 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005661
Nate Begemanb9a47b82009-02-23 08:49:38 +00005662 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005663 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005664 // present, fall back to case 3.
5665 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5666 bool V1Only = true;
5667 bool V2Only = true;
5668 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005669 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005670 if (EltIdx < 0)
5671 continue;
5672 if (EltIdx < 16)
5673 V2Only = false;
5674 else
5675 V1Only = false;
5676 }
Eric Christopherfd179292009-08-27 18:07:15 +00005677
Nate Begemanb9a47b82009-02-23 08:49:38 +00005678 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005679 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005680 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005681
Nate Begemanb9a47b82009-02-23 08:49:38 +00005682 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005683 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005684 //
5685 // Otherwise, we have elements from both input vectors, and must zero out
5686 // elements that come from V2 in the first mask, and V1 in the second mask
5687 // so that we can OR them together.
5688 bool TwoInputs = !(V1Only || V2Only);
5689 for (unsigned i = 0; i != 16; ++i) {
5690 int EltIdx = MaskVals[i];
5691 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005692 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005693 continue;
5694 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005695 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005696 }
5697 // If all the elements are from V2, assign it to V1 and return after
5698 // building the first pshufb.
5699 if (V2Only)
5700 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005701 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005702 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005703 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005704 if (!TwoInputs)
5705 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005706
Nate Begemanb9a47b82009-02-23 08:49:38 +00005707 // Calculate the shuffle mask for the second input, shuffle it, and
5708 // OR it with the first shuffled input.
5709 pshufbMask.clear();
5710 for (unsigned i = 0; i != 16; ++i) {
5711 int EltIdx = MaskVals[i];
5712 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005713 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005714 continue;
5715 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005716 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005717 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005718 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005719 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005720 MVT::v16i8, &pshufbMask[0], 16));
5721 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 }
Eric Christopherfd179292009-08-27 18:07:15 +00005723
Nate Begemanb9a47b82009-02-23 08:49:38 +00005724 // No SSSE3 - Calculate in place words and then fix all out of place words
5725 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5726 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005727 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5728 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005729 SDValue NewV = V2Only ? V2 : V1;
5730 for (int i = 0; i != 8; ++i) {
5731 int Elt0 = MaskVals[i*2];
5732 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005733
Nate Begemanb9a47b82009-02-23 08:49:38 +00005734 // This word of the result is all undef, skip it.
5735 if (Elt0 < 0 && Elt1 < 0)
5736 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005737
Nate Begemanb9a47b82009-02-23 08:49:38 +00005738 // This word of the result is already in the correct place, skip it.
5739 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5740 continue;
5741 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5742 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005743
Nate Begemanb9a47b82009-02-23 08:49:38 +00005744 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5745 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5746 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005747
5748 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5749 // using a single extract together, load it and store it.
5750 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005751 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005752 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005753 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005754 DAG.getIntPtrConstant(i));
5755 continue;
5756 }
5757
Nate Begemanb9a47b82009-02-23 08:49:38 +00005758 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005759 // source byte is not also odd, shift the extracted word left 8 bits
5760 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005761 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005762 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005763 DAG.getIntPtrConstant(Elt1 / 2));
5764 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005765 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005766 DAG.getConstant(8,
5767 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005768 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005769 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5770 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005771 }
5772 // If Elt0 is defined, extract it from the appropriate source. If the
5773 // source byte is not also even, shift the extracted word right 8 bits. If
5774 // Elt1 was also defined, OR the extracted values together before
5775 // inserting them in the result.
5776 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005777 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005778 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5779 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005780 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005781 DAG.getConstant(8,
5782 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005783 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005784 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5785 DAG.getConstant(0x00FF, MVT::i16));
5786 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005787 : InsElt0;
5788 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005789 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005790 DAG.getIntPtrConstant(i));
5791 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005792 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005793}
5794
Evan Cheng7a831ce2007-12-15 03:00:47 +00005795/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005796/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005797/// done when every pair / quad of shuffle mask elements point to elements in
5798/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005799/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005800static
Nate Begeman9008ca62009-04-27 18:41:29 +00005801SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005802 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005803 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005804 SDValue V1 = SVOp->getOperand(0);
5805 SDValue V2 = SVOp->getOperand(1);
5806 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005807 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005808 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005809 switch (VT.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00005810 default: llvm_unreachable("Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005811 case MVT::v4f32: NewVT = MVT::v2f64; break;
5812 case MVT::v4i32: NewVT = MVT::v2i64; break;
5813 case MVT::v8i16: NewVT = MVT::v4i32; break;
5814 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005815 }
5816
Nate Begeman9008ca62009-04-27 18:41:29 +00005817 int Scale = NumElems / NewWidth;
5818 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005819 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005820 int StartIdx = -1;
5821 for (int j = 0; j < Scale; ++j) {
5822 int EltIdx = SVOp->getMaskElt(i+j);
5823 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005824 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005825 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005826 StartIdx = EltIdx - (EltIdx % Scale);
5827 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005828 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005829 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005830 if (StartIdx == -1)
5831 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005832 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005833 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005834 }
5835
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005836 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5837 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005838 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005839}
5840
Evan Chengd880b972008-05-09 21:53:03 +00005841/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005842///
Owen Andersone50ed302009-08-10 22:56:29 +00005843static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005844 SDValue SrcOp, SelectionDAG &DAG,
5845 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005846 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005847 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005848 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005849 LD = dyn_cast<LoadSDNode>(SrcOp);
5850 if (!LD) {
5851 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5852 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005853 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005854 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005855 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005856 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005857 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005858 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005859 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005860 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005861 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5862 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5863 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005864 SrcOp.getOperand(0)
5865 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005866 }
5867 }
5868 }
5869
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005870 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005871 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005872 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005873 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005874}
5875
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005876/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5877/// which could not be matched by any known target speficic shuffle
5878static SDValue
5879LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Craig Topper8f35c132012-01-20 09:29:03 +00005880 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005881
Craig Topper8f35c132012-01-20 09:29:03 +00005882 unsigned NumElems = VT.getVectorNumElements();
5883 unsigned NumLaneElems = NumElems / 2;
5884
5885 int MinRange[2][2] = { { static_cast<int>(NumElems),
5886 static_cast<int>(NumElems) },
5887 { static_cast<int>(NumElems),
5888 static_cast<int>(NumElems) } };
5889 int MaxRange[2][2] = { { -1, -1 }, { -1, -1 } };
5890
5891 // Collect used ranges for each source in each lane
5892 for (unsigned l = 0; l < 2; ++l) {
5893 unsigned LaneStart = l*NumLaneElems;
5894 for (unsigned i = 0; i != NumLaneElems; ++i) {
5895 int Idx = SVOp->getMaskElt(i+LaneStart);
5896 if (Idx < 0)
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005897 continue;
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005898
Craig Topper8f35c132012-01-20 09:29:03 +00005899 int Input = 0;
5900 if (Idx >= (int)NumElems) {
5901 Idx -= NumElems;
5902 Input = 1;
5903 }
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005904
Craig Topper8f35c132012-01-20 09:29:03 +00005905 if (Idx > MaxRange[l][Input])
5906 MaxRange[l][Input] = Idx;
5907 if (Idx < MinRange[l][Input])
5908 MinRange[l][Input] = Idx;
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005909 }
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005910 }
5911
Craig Topper8f35c132012-01-20 09:29:03 +00005912 // Make sure each range is 128-bits
5913 int ExtractIdx[2][2] = { { -1, -1 }, { -1, -1 } };
5914 for (unsigned l = 0; l < 2; ++l) {
5915 for (unsigned Input = 0; Input < 2; ++Input) {
5916 if (MinRange[l][Input] == (int)NumElems && MaxRange[l][Input] < 0)
5917 continue;
5918
Craig Topperd9ec7252012-01-21 08:49:33 +00005919 if (MinRange[l][Input] >= 0 && MaxRange[l][Input] < (int)NumLaneElems)
Craig Topper8f35c132012-01-20 09:29:03 +00005920 ExtractIdx[l][Input] = 0;
5921 else if (MinRange[l][Input] >= (int)NumLaneElems &&
Craig Topperd9ec7252012-01-21 08:49:33 +00005922 MaxRange[l][Input] < (int)NumElems)
Craig Topper8f35c132012-01-20 09:29:03 +00005923 ExtractIdx[l][Input] = NumLaneElems;
5924 else
5925 return SDValue();
5926 }
5927 }
5928
5929 DebugLoc dl = SVOp->getDebugLoc();
5930 MVT EltVT = VT.getVectorElementType().getSimpleVT();
5931 EVT NVT = MVT::getVectorVT(EltVT, NumElems/2);
5932
5933 SDValue Ops[2][2];
5934 for (unsigned l = 0; l < 2; ++l) {
5935 for (unsigned Input = 0; Input < 2; ++Input) {
5936 if (ExtractIdx[l][Input] >= 0)
5937 Ops[l][Input] = Extract128BitVector(SVOp->getOperand(Input),
5938 DAG.getConstant(ExtractIdx[l][Input], MVT::i32),
5939 DAG, dl);
5940 else
5941 Ops[l][Input] = DAG.getUNDEF(NVT);
5942 }
5943 }
5944
5945 // Generate 128-bit shuffles
5946 SmallVector<int, 16> Mask1, Mask2;
5947 for (unsigned i = 0; i != NumLaneElems; ++i) {
5948 int Elt = SVOp->getMaskElt(i);
5949 if (Elt >= (int)NumElems) {
5950 Elt %= NumLaneElems;
5951 Elt += NumLaneElems;
5952 } else if (Elt >= 0) {
5953 Elt %= NumLaneElems;
5954 }
5955 Mask1.push_back(Elt);
5956 }
5957 for (unsigned i = NumLaneElems; i != NumElems; ++i) {
5958 int Elt = SVOp->getMaskElt(i);
5959 if (Elt >= (int)NumElems) {
5960 Elt %= NumLaneElems;
5961 Elt += NumLaneElems;
5962 } else if (Elt >= 0) {
5963 Elt %= NumLaneElems;
5964 }
5965 Mask2.push_back(Elt);
5966 }
5967
5968 SDValue Shuf1 = DAG.getVectorShuffle(NVT, dl, Ops[0][0], Ops[0][1], &Mask1[0]);
5969 SDValue Shuf2 = DAG.getVectorShuffle(NVT, dl, Ops[1][0], Ops[1][1], &Mask2[0]);
5970
5971 // Concatenate the result back
5972 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Shuf1,
5973 DAG.getConstant(0, MVT::i32), DAG, dl);
5974 return Insert128BitVector(V, Shuf2, DAG.getConstant(NumElems/2, MVT::i32),
5975 DAG, dl);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005976}
5977
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005978/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
5979/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00005980static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005981LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005982 SDValue V1 = SVOp->getOperand(0);
5983 SDValue V2 = SVOp->getOperand(1);
5984 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005985 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005986
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005987 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
5988
Benjamin Kramer9c683542012-01-30 15:16:21 +00005989 std::pair<int, int> Locs[4];
5990 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005991 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00005992
Evan Chengace3c172008-07-22 21:13:36 +00005993 unsigned NumHi = 0;
5994 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005995 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005996 int Idx = PermMask[i];
5997 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005998 Locs[i] = std::make_pair(-1, -1);
5999 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006000 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6001 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006002 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006003 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006004 NumLo++;
6005 } else {
6006 Locs[i] = std::make_pair(1, NumHi);
6007 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006008 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006009 NumHi++;
6010 }
6011 }
6012 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006013
Evan Chengace3c172008-07-22 21:13:36 +00006014 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006015 // If no more than two elements come from either vector. This can be
6016 // implemented with two shuffles. First shuffle gather the elements.
6017 // The second shuffle, which takes the first shuffle as both of its
6018 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006019 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006020
Benjamin Kramer9c683542012-01-30 15:16:21 +00006021 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006022
Benjamin Kramer9c683542012-01-30 15:16:21 +00006023 for (unsigned i = 0; i != 4; ++i)
6024 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006025 unsigned Idx = (i < 2) ? 0 : 4;
6026 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006027 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006028 }
Evan Chengace3c172008-07-22 21:13:36 +00006029
Nate Begeman9008ca62009-04-27 18:41:29 +00006030 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006031 } else if (NumLo == 3 || NumHi == 3) {
6032 // Otherwise, we must have three elements from one vector, call it X, and
6033 // one element from the other, call it Y. First, use a shufps to build an
6034 // intermediate vector with the one element from Y and the element from X
6035 // that will be in the same half in the final destination (the indexes don't
6036 // matter). Then, use a shufps to build the final vector, taking the half
6037 // containing the element from Y from the intermediate, and the other half
6038 // from X.
6039 if (NumHi == 3) {
6040 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006041 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006042 std::swap(V1, V2);
6043 }
6044
6045 // Find the element from V2.
6046 unsigned HiIndex;
6047 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006048 int Val = PermMask[HiIndex];
6049 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006050 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006051 if (Val >= 4)
6052 break;
6053 }
6054
Nate Begeman9008ca62009-04-27 18:41:29 +00006055 Mask1[0] = PermMask[HiIndex];
6056 Mask1[1] = -1;
6057 Mask1[2] = PermMask[HiIndex^1];
6058 Mask1[3] = -1;
6059 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006060
6061 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006062 Mask1[0] = PermMask[0];
6063 Mask1[1] = PermMask[1];
6064 Mask1[2] = HiIndex & 1 ? 6 : 4;
6065 Mask1[3] = HiIndex & 1 ? 4 : 6;
6066 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006067 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006068 Mask1[0] = HiIndex & 1 ? 2 : 0;
6069 Mask1[1] = HiIndex & 1 ? 0 : 2;
6070 Mask1[2] = PermMask[2];
6071 Mask1[3] = PermMask[3];
6072 if (Mask1[2] >= 0)
6073 Mask1[2] += 4;
6074 if (Mask1[3] >= 0)
6075 Mask1[3] += 4;
6076 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006077 }
Evan Chengace3c172008-07-22 21:13:36 +00006078 }
6079
6080 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006081 int LoMask[] = { -1, -1, -1, -1 };
6082 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006083
Benjamin Kramer9c683542012-01-30 15:16:21 +00006084 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006085 unsigned MaskIdx = 0;
6086 unsigned LoIdx = 0;
6087 unsigned HiIdx = 2;
6088 for (unsigned i = 0; i != 4; ++i) {
6089 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006090 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006091 MaskIdx = 1;
6092 LoIdx = 0;
6093 HiIdx = 2;
6094 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006095 int Idx = PermMask[i];
6096 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006097 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006098 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006099 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006100 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006101 LoIdx++;
6102 } else {
6103 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006104 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006105 HiIdx++;
6106 }
6107 }
6108
Nate Begeman9008ca62009-04-27 18:41:29 +00006109 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6110 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006111 int MaskOps[] = { -1, -1, -1, -1 };
6112 for (unsigned i = 0; i != 4; ++i)
6113 if (Locs[i].first != -1)
6114 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006115 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006116}
6117
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006118static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006119 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006120 V = V.getOperand(0);
6121 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6122 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006123 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6124 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6125 // BUILD_VECTOR (load), undef
6126 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006127 if (MayFoldLoad(V))
6128 return true;
6129 return false;
6130}
6131
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006132// FIXME: the version above should always be used. Since there's
6133// a bug where several vector shuffles can't be folded because the
6134// DAG is not updated during lowering and a node claims to have two
6135// uses while it only has one, use this version, and let isel match
6136// another instruction if the load really happens to have more than
6137// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006138// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006139static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006140 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006141 V = V.getOperand(0);
6142 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6143 V = V.getOperand(0);
6144 if (ISD::isNormalLoad(V.getNode()))
6145 return true;
6146 return false;
6147}
6148
6149/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6150/// a vector extract, and if both can be later optimized into a single load.
6151/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6152/// here because otherwise a target specific shuffle node is going to be
6153/// emitted for this shuffle, and the optimization not done.
6154/// FIXME: This is probably not the best approach, but fix the problem
6155/// until the right path is decided.
6156static
6157bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6158 const TargetLowering &TLI) {
6159 EVT VT = V.getValueType();
6160 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6161
6162 // Be sure that the vector shuffle is present in a pattern like this:
6163 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6164 if (!V.hasOneUse())
6165 return false;
6166
6167 SDNode *N = *V.getNode()->use_begin();
6168 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6169 return false;
6170
6171 SDValue EltNo = N->getOperand(1);
6172 if (!isa<ConstantSDNode>(EltNo))
6173 return false;
6174
6175 // If the bit convert changed the number of elements, it is unsafe
6176 // to examine the mask.
6177 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006178 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006179 EVT SrcVT = V.getOperand(0).getValueType();
6180 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6181 return false;
6182 V = V.getOperand(0);
6183 HasShuffleIntoBitcast = true;
6184 }
6185
6186 // Select the input vector, guarding against out of range extract vector.
6187 unsigned NumElems = VT.getVectorNumElements();
6188 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6189 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6190 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6191
Nadav Rotem0b94b5f2012-01-17 09:13:19 +00006192 // If we are accessing the upper part of a YMM register
6193 // then the EXTRACT_VECTOR_ELT is likely to be legalized to a sequence of
6194 // EXTRACT_SUBVECTOR + EXTRACT_VECTOR_ELT, which are not detected at this point
6195 // because the legalization of N did not happen yet.
Nadav Rotema16d4412012-01-17 09:31:09 +00006196 if (Idx >= (int)NumElems/2 && VT.getSizeInBits() == 256)
Nadav Rotem0b94b5f2012-01-17 09:13:19 +00006197 return false;
6198
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006199 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006200 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006201 V = V.getOperand(0);
6202
Craig Toppera51bb3a2012-01-02 08:46:48 +00006203 if (!ISD::isNormalLoad(V.getNode()))
6204 return false;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006205
Craig Toppera51bb3a2012-01-02 08:46:48 +00006206 // Is the original load suitable?
6207 LoadSDNode *LN0 = cast<LoadSDNode>(V);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006208
Craig Toppera51bb3a2012-01-02 08:46:48 +00006209 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
6210 return false;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006211
Craig Toppera51bb3a2012-01-02 08:46:48 +00006212 if (!HasShuffleIntoBitcast)
6213 return true;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006214
Craig Toppera51bb3a2012-01-02 08:46:48 +00006215 // If there's a bitcast before the shuffle, check if the load type and
6216 // alignment is valid.
6217 unsigned Align = LN0->getAlignment();
6218 unsigned NewAlign =
6219 TLI.getTargetData()->getABITypeAlignment(
6220 VT.getTypeForEVT(*DAG.getContext()));
6221
6222 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6223 return false;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006224
6225 return true;
6226}
6227
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006228static
Evan Cheng835580f2010-10-07 20:50:20 +00006229SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6230 EVT VT = Op.getValueType();
6231
6232 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006233 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6234 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006235 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6236 V1, DAG));
6237}
6238
6239static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006240SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006241 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006242 SDValue V1 = Op.getOperand(0);
6243 SDValue V2 = Op.getOperand(1);
6244 EVT VT = Op.getValueType();
6245
6246 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6247
Craig Topper1accb7e2012-01-10 06:54:16 +00006248 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006249 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6250
Evan Cheng0899f5c2011-08-31 02:05:24 +00006251 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6252 return DAG.getNode(ISD::BITCAST, dl, VT,
6253 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6254 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6255 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006256}
6257
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006258static
6259SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6260 SDValue V1 = Op.getOperand(0);
6261 SDValue V2 = Op.getOperand(1);
6262 EVT VT = Op.getValueType();
6263
6264 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6265 "unsupported shuffle type");
6266
6267 if (V2.getOpcode() == ISD::UNDEF)
6268 V2 = V1;
6269
6270 // v4i32 or v4f32
6271 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6272}
6273
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006274static
Craig Topper1accb7e2012-01-10 06:54:16 +00006275SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006276 SDValue V1 = Op.getOperand(0);
6277 SDValue V2 = Op.getOperand(1);
6278 EVT VT = Op.getValueType();
6279 unsigned NumElems = VT.getVectorNumElements();
6280
6281 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6282 // operand of these instructions is only memory, so check if there's a
6283 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6284 // same masks.
6285 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006286
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006287 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006288 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006289 CanFoldLoad = true;
6290
6291 // When V1 is a load, it can be folded later into a store in isel, example:
6292 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6293 // turns into:
6294 // (MOVLPSmr addr:$src1, VR128:$src2)
6295 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006296 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006297 CanFoldLoad = true;
6298
Dan Gohman65fd6562011-11-03 21:49:52 +00006299 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006300 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006301 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006302 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6303
6304 if (NumElems == 4)
Dan Gohman65fd6562011-11-03 21:49:52 +00006305 // If we don't care about the second element, procede to use movss.
6306 if (SVOp->getMaskElt(1) != -1)
6307 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006308 }
6309
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006310 // movl and movlp will both match v2i64, but v2i64 is never matched by
6311 // movl earlier because we make it strict to avoid messing with the movlp load
6312 // folding logic (see the code above getMOVLP call). Match it here then,
6313 // this is horrible, but will stay like this until we move all shuffle
6314 // matching to x86 specific nodes. Note that for the 1st condition all
6315 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006316 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006317 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6318 // as to remove this logic from here, as much as possible
6319 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006320 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006321 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006322 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006323
6324 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6325
6326 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006327 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006328 X86::getShuffleSHUFImmediate(SVOp), DAG);
6329}
6330
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006331static
6332SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006333 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006334 const X86Subtarget *Subtarget) {
6335 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6336 EVT VT = Op.getValueType();
6337 DebugLoc dl = Op.getDebugLoc();
6338 SDValue V1 = Op.getOperand(0);
6339 SDValue V2 = Op.getOperand(1);
6340
6341 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006342 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006343
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006344 // Handle splat operations
6345 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006346 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006347 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006348 // Special case, this is the only place now where it's allowed to return
6349 // a vector_shuffle operation without using a target specific node, because
6350 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6351 // this be moved to DAGCombine instead?
6352 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006353 return Op;
6354
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006355 // Use vbroadcast whenever the splat comes from a foldable load
Craig Toppera9376332012-01-10 08:23:59 +00006356 SDValue LD = isVectorBroadcast(Op, Subtarget);
6357 if (LD.getNode())
Nadav Rotemf8c10e52011-11-15 22:50:37 +00006358 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006359
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006360 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006361 if ((Size == 128 && NumElem <= 4) ||
6362 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006363 return SDValue();
6364
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006365 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006366 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006367 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006368
6369 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6370 // do it!
6371 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6372 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6373 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006374 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006375 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006376 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006377 // FIXME: Figure out a cleaner way to do this.
6378 // Try to make use of movq to zero out the top part.
6379 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6380 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6381 if (NewOp.getNode()) {
6382 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6383 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6384 DAG, Subtarget, dl);
6385 }
6386 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6387 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6388 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6389 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6390 DAG, Subtarget, dl);
6391 }
6392 }
6393 return SDValue();
6394}
6395
Dan Gohman475871a2008-07-27 21:46:04 +00006396SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006397X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006398 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006399 SDValue V1 = Op.getOperand(0);
6400 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006401 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006402 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006403 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006404 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006405 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006406 bool V1IsSplat = false;
6407 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006408 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006409 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006410 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006411 MachineFunction &MF = DAG.getMachineFunction();
6412 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006413
Craig Topper3426a3e2011-11-14 06:46:21 +00006414 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006415
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006416 if (V1IsUndef && V2IsUndef)
6417 return DAG.getUNDEF(VT);
6418
6419 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006420
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006421 // Vector shuffle lowering takes 3 steps:
6422 //
6423 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6424 // narrowing and commutation of operands should be handled.
6425 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6426 // shuffle nodes.
6427 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6428 // so the shuffle can be broken into other shuffles and the legalizer can
6429 // try the lowering again.
6430 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006431 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006432 // be matched during isel, all of them must be converted to a target specific
6433 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006434
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006435 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6436 // narrowing and commutation of operands should be handled. The actual code
6437 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006438 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006439 if (NewOp.getNode())
6440 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006441
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006442 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6443 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper94438ba2011-12-16 08:06:31 +00006444 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006445 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006446 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006447 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006448
Craig Topperd0a31172012-01-10 06:37:29 +00006449 if (X86::isMOVDDUPMask(SVOp) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006450 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006451 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006452
Dale Johannesen0488fb62010-09-30 23:57:10 +00006453 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006454 return getMOVHighToLow(Op, dl, DAG);
6455
6456 // Use to match splats
Craig Topper1accb7e2012-01-10 06:54:16 +00006457 if (HasSSE2 && X86::isUNPCKHMask(SVOp, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006458 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006459 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006460
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006461 if (X86::isPSHUFDMask(SVOp)) {
6462 // The actual implementation will match the mask in the if above and then
6463 // during isel it can match several different instructions, not only pshufd
6464 // as its name says, sad but true, emulate the behavior for now...
6465 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6466 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6467
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006468 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6469
Craig Topperdbd98a42012-02-07 06:28:42 +00006470 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6471 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6472
Craig Topper1accb7e2012-01-10 06:54:16 +00006473 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006474 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6475
Craig Topperb3982da2011-12-31 23:50:21 +00006476 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006477 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006478 }
Eric Christopherfd179292009-08-27 18:07:15 +00006479
Evan Chengf26ffe92008-05-29 08:22:04 +00006480 // Check if this can be converted into a logical shift.
6481 bool isLeft = false;
6482 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006483 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006484 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006485 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006486 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006487 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006488 EVT EltVT = VT.getVectorElementType();
6489 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006490 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006491 }
Eric Christopherfd179292009-08-27 18:07:15 +00006492
Nate Begeman9008ca62009-04-27 18:41:29 +00006493 if (X86::isMOVLMask(SVOp)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006494 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006495 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006496 if (!X86::isMOVLPMask(SVOp)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006497 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006498 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6499
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006500 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006501 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6502 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006503 }
Eric Christopherfd179292009-08-27 18:07:15 +00006504
Nate Begeman9008ca62009-04-27 18:41:29 +00006505 // FIXME: fold these into legal mask.
Craig Topperc0d82852011-11-22 00:44:41 +00006506 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006507 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006508
Dale Johannesen0488fb62010-09-30 23:57:10 +00006509 if (X86::isMOVHLPSMask(SVOp))
6510 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006511
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006512 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006513 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006514
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006515 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006516 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006517
Dale Johannesen0488fb62010-09-30 23:57:10 +00006518 if (X86::isMOVLPMask(SVOp))
Craig Topper1accb7e2012-01-10 06:54:16 +00006519 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006520
Nate Begeman9008ca62009-04-27 18:41:29 +00006521 if (ShouldXformToMOVHLPS(SVOp) ||
6522 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6523 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006524
Evan Chengf26ffe92008-05-29 08:22:04 +00006525 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006526 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006527 EVT EltVT = VT.getVectorElementType();
6528 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006529 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006530 }
Eric Christopherfd179292009-08-27 18:07:15 +00006531
Evan Cheng9eca5e82006-10-25 21:49:50 +00006532 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006533 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6534 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006535 V1IsSplat = isSplatVector(V1.getNode());
6536 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006537
Chris Lattner8a594482007-11-25 00:24:49 +00006538 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper38034c52011-11-26 22:55:48 +00006539 if (V1IsSplat && !V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006540 Op = CommuteVectorShuffle(SVOp, DAG);
6541 SVOp = cast<ShuffleVectorSDNode>(Op);
6542 V1 = SVOp->getOperand(0);
6543 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006544 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006545 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006546 }
6547
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006548 ArrayRef<int> M = SVOp->getMask();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006549
6550 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006551 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006552 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006553 return V1;
6554 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6555 // the instruction selector will not match, so get a canonical MOVL with
6556 // swapped operands to undo the commute.
6557 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006558 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006559
Craig Topperbeabc6c2011-12-05 06:56:46 +00006560 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006561 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006562
Craig Topperbeabc6c2011-12-05 06:56:46 +00006563 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006564 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006565
Evan Cheng9bbbb982006-10-25 20:48:19 +00006566 if (V2IsSplat) {
6567 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006568 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006569 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006570 SDValue NewMask = NormalizeMask(SVOp, DAG);
6571 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6572 if (NSVOp != SVOp) {
Craig Topperc0d82852011-11-22 00:44:41 +00006573 if (X86::isUNPCKLMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006574 return NewMask;
Craig Topperc0d82852011-11-22 00:44:41 +00006575 } else if (X86::isUNPCKHMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006576 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006577 }
6578 }
6579 }
6580
Evan Cheng9eca5e82006-10-25 21:49:50 +00006581 if (Commuted) {
6582 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006583 // FIXME: this seems wrong.
6584 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6585 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006586
Craig Topperc0d82852011-11-22 00:44:41 +00006587 if (X86::isUNPCKLMask(NewSVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006588 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006589
Craig Topperc0d82852011-11-22 00:44:41 +00006590 if (X86::isUNPCKHMask(NewSVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006591 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006592 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006593
Nate Begeman9008ca62009-04-27 18:41:29 +00006594 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006595 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006596 return CommuteVectorShuffle(SVOp, DAG);
6597
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006598 // The checks below are all present in isShuffleMaskLegal, but they are
6599 // inlined here right now to enable us to directly emit target specific
6600 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006601
Craig Topper0e2037b2012-01-20 05:53:00 +00006602 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006603 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006604 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006605 DAG);
6606
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006607 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6608 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006609 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006610 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006611 }
6612
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006613 if (isPSHUFHWMask(M, VT))
6614 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6615 X86::getShufflePSHUFHWImmediate(SVOp),
6616 DAG);
6617
6618 if (isPSHUFLWMask(M, VT))
6619 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6620 X86::getShufflePSHUFLWImmediate(SVOp),
6621 DAG);
6622
Craig Topper1a7700a2012-01-19 08:19:12 +00006623 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006624 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006625 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006626
Craig Topper94438ba2011-12-16 08:06:31 +00006627 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006628 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006629 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006630 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006631
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006632 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006633 // Generate target specific nodes for 128 or 256-bit shuffles only
6634 // supported in the AVX instruction set.
6635 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006636
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006637 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006638 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006639 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6640
Craig Topper70b883b2011-11-28 10:14:51 +00006641 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006642 if (isVPERMILPMask(M, VT, HasAVX)) {
6643 if (HasAVX2 && VT == MVT::v8i32)
6644 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
6645 X86::getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006646 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Toppera0255662012-02-03 06:52:33 +00006647 X86::getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006648 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006649
Craig Topper70b883b2011-11-28 10:14:51 +00006650 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006651 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006652 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006653 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006654
6655 //===--------------------------------------------------------------------===//
6656 // Since no target specific shuffle was selected for this generic one,
6657 // lower it into other known shuffles. FIXME: this isn't true yet, but
6658 // this is the plan.
6659 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006660
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006661 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6662 if (VT == MVT::v8i16) {
6663 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6664 if (NewOp.getNode())
6665 return NewOp;
6666 }
6667
6668 if (VT == MVT::v16i8) {
6669 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6670 if (NewOp.getNode())
6671 return NewOp;
6672 }
6673
6674 // Handle all 128-bit wide vectors with 4 elements, and match them with
6675 // several different shuffle types.
6676 if (NumElems == 4 && VT.getSizeInBits() == 128)
6677 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6678
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006679 // Handle general 256-bit shuffles
6680 if (VT.is256BitVector())
6681 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6682
Dan Gohman475871a2008-07-27 21:46:04 +00006683 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006684}
6685
Dan Gohman475871a2008-07-27 21:46:04 +00006686SDValue
6687X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006688 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006689 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006690 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006691
6692 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6693 return SDValue();
6694
Duncan Sands83ec4b62008-06-06 12:08:01 +00006695 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006696 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006697 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006698 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006699 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006700 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006701 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006702 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6703 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6704 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006705 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6706 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006707 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006708 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006709 Op.getOperand(0)),
6710 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006711 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006712 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006713 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006714 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006715 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006716 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006717 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6718 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006719 // result has a single use which is a store or a bitcast to i32. And in
6720 // the case of a store, it's not worth it if the index is a constant 0,
6721 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006722 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006723 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006724 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006725 if ((User->getOpcode() != ISD::STORE ||
6726 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6727 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006728 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006729 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006730 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006731 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006732 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006733 Op.getOperand(0)),
6734 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006735 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Pete Coopera77214a2011-11-14 19:38:42 +00006736 } else if (VT == MVT::i32 || VT == MVT::i64) {
6737 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006738 if (isa<ConstantSDNode>(Op.getOperand(1)))
6739 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006740 }
Dan Gohman475871a2008-07-27 21:46:04 +00006741 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006742}
6743
6744
Dan Gohman475871a2008-07-27 21:46:04 +00006745SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006746X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6747 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006748 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006749 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006750
David Greene74a579d2011-02-10 16:57:36 +00006751 SDValue Vec = Op.getOperand(0);
6752 EVT VecVT = Vec.getValueType();
6753
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006754 // If this is a 256-bit vector result, first extract the 128-bit vector and
6755 // then extract the element from the 128-bit vector.
6756 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006757 DebugLoc dl = Op.getNode()->getDebugLoc();
6758 unsigned NumElems = VecVT.getVectorNumElements();
6759 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006760 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6761
6762 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006763 bool Upper = IdxVal >= NumElems/2;
6764 Vec = Extract128BitVector(Vec,
6765 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006766
David Greene74a579d2011-02-10 16:57:36 +00006767 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006768 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006769 }
6770
6771 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6772
Craig Topperd0a31172012-01-10 06:37:29 +00006773 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006774 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006775 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006776 return Res;
6777 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006778
Owen Andersone50ed302009-08-10 22:56:29 +00006779 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006780 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006781 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006782 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006783 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006784 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006785 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006786 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6787 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006788 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006789 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006790 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006791 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006792 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006793 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006794 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006795 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006796 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006797 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006798 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006799 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006800 if (Idx == 0)
6801 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006802
Evan Cheng0db9fe62006-04-25 20:13:52 +00006803 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006804 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006805 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006806 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006807 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006808 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006809 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006810 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006811 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6812 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6813 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006814 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006815 if (Idx == 0)
6816 return Op;
6817
6818 // UNPCKHPD the element to the lowest double word, then movsd.
6819 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6820 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006821 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006822 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006823 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006824 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006825 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006826 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006827 }
6828
Dan Gohman475871a2008-07-27 21:46:04 +00006829 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006830}
6831
Dan Gohman475871a2008-07-27 21:46:04 +00006832SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006833X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6834 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006835 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006836 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006837 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006838
Dan Gohman475871a2008-07-27 21:46:04 +00006839 SDValue N0 = Op.getOperand(0);
6840 SDValue N1 = Op.getOperand(1);
6841 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006842
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006843 if (VT.getSizeInBits() == 256)
6844 return SDValue();
6845
Dan Gohman8a55ce42009-09-23 21:02:20 +00006846 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006847 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006848 unsigned Opc;
6849 if (VT == MVT::v8i16)
6850 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006851 else if (VT == MVT::v16i8)
6852 Opc = X86ISD::PINSRB;
6853 else
6854 Opc = X86ISD::PINSRB;
6855
Nate Begeman14d12ca2008-02-11 04:19:36 +00006856 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6857 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006858 if (N1.getValueType() != MVT::i32)
6859 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6860 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006861 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006862 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006863 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006864 // Bits [7:6] of the constant are the source select. This will always be
6865 // zero here. The DAG Combiner may combine an extract_elt index into these
6866 // bits. For example (insert (extract, 3), 2) could be matched by putting
6867 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006868 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006869 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006870 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006871 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006872 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006873 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006874 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006875 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Pete Coopera77214a2011-11-14 19:38:42 +00006876 } else if ((EltVT == MVT::i32 || EltVT == MVT::i64) &&
6877 isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006878 // PINSR* works with constant index.
6879 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006880 }
Dan Gohman475871a2008-07-27 21:46:04 +00006881 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006882}
6883
Dan Gohman475871a2008-07-27 21:46:04 +00006884SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006885X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006886 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006887 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006888
David Greene6b381262011-02-09 15:32:06 +00006889 DebugLoc dl = Op.getDebugLoc();
6890 SDValue N0 = Op.getOperand(0);
6891 SDValue N1 = Op.getOperand(1);
6892 SDValue N2 = Op.getOperand(2);
6893
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006894 // If this is a 256-bit vector result, first extract the 128-bit vector,
6895 // insert the element into the extracted half and then place it back.
6896 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00006897 if (!isa<ConstantSDNode>(N2))
6898 return SDValue();
6899
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006900 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00006901 unsigned NumElems = VT.getVectorNumElements();
6902 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006903 bool Upper = IdxVal >= NumElems/2;
6904 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
6905 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00006906
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006907 // Insert the element into the desired half.
6908 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
6909 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00006910
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006911 // Insert the changed part back to the 256-bit vector
6912 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00006913 }
6914
Craig Topperd0a31172012-01-10 06:37:29 +00006915 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00006916 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6917
Dan Gohman8a55ce42009-09-23 21:02:20 +00006918 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006919 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006920
Dan Gohman8a55ce42009-09-23 21:02:20 +00006921 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006922 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6923 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006924 if (N1.getValueType() != MVT::i32)
6925 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6926 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006927 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006928 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006929 }
Dan Gohman475871a2008-07-27 21:46:04 +00006930 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006931}
6932
Dan Gohman475871a2008-07-27 21:46:04 +00006933SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006934X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00006935 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006936 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006937 EVT OpVT = Op.getValueType();
6938
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00006939 // If this is a 256-bit vector result, first insert into a 128-bit
6940 // vector and then insert into the 256-bit vector.
6941 if (OpVT.getSizeInBits() > 128) {
6942 // Insert into a 128-bit vector.
6943 EVT VT128 = EVT::getVectorVT(*Context,
6944 OpVT.getVectorElementType(),
6945 OpVT.getVectorNumElements() / 2);
6946
6947 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6948
6949 // Insert the 128-bit vector.
6950 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6951 DAG.getConstant(0, MVT::i32),
6952 DAG, dl);
6953 }
6954
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006955 if (Op.getValueType() == MVT::v1i64 &&
6956 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006957 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006958
Owen Anderson825b72b2009-08-11 20:47:22 +00006959 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006960 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6961 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006962 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006963 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006964}
6965
David Greene91585092011-01-26 15:38:49 +00006966// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6967// a simple subregister reference or explicit instructions to grab
6968// upper bits of a vector.
6969SDValue
6970X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6971 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006972 DebugLoc dl = Op.getNode()->getDebugLoc();
6973 SDValue Vec = Op.getNode()->getOperand(0);
6974 SDValue Idx = Op.getNode()->getOperand(1);
6975
6976 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6977 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6978 return Extract128BitVector(Vec, Idx, DAG, dl);
6979 }
David Greene91585092011-01-26 15:38:49 +00006980 }
6981 return SDValue();
6982}
6983
David Greenecfe33c42011-01-26 19:13:22 +00006984// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6985// simple superregister reference or explicit instructions to insert
6986// the upper bits of a vector.
6987SDValue
6988X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6989 if (Subtarget->hasAVX()) {
6990 DebugLoc dl = Op.getNode()->getDebugLoc();
6991 SDValue Vec = Op.getNode()->getOperand(0);
6992 SDValue SubVec = Op.getNode()->getOperand(1);
6993 SDValue Idx = Op.getNode()->getOperand(2);
6994
6995 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6996 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006997 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006998 }
6999 }
7000 return SDValue();
7001}
7002
Bill Wendling056292f2008-09-16 21:48:12 +00007003// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7004// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7005// one of the above mentioned nodes. It has to be wrapped because otherwise
7006// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7007// be used to form addressing mode. These wrapped nodes will be selected
7008// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007009SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007010X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007011 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007012
Chris Lattner41621a22009-06-26 19:22:52 +00007013 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7014 // global base reg.
7015 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007016 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007017 CodeModel::Model M = getTargetMachine().getCodeModel();
7018
Chris Lattner4f066492009-07-11 20:29:19 +00007019 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007020 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007021 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007022 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007023 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007024 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007025 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007026
Evan Cheng1606e8e2009-03-13 07:51:59 +00007027 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007028 CP->getAlignment(),
7029 CP->getOffset(), OpFlag);
7030 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007031 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007032 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007033 if (OpFlag) {
7034 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007035 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007036 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007037 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007038 }
7039
7040 return Result;
7041}
7042
Dan Gohmand858e902010-04-17 15:26:15 +00007043SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007044 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007045
Chris Lattner18c59872009-06-27 04:16:01 +00007046 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7047 // global base reg.
7048 unsigned char OpFlag = 0;
7049 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007050 CodeModel::Model M = getTargetMachine().getCodeModel();
7051
Chris Lattner4f066492009-07-11 20:29:19 +00007052 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007053 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007054 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007055 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007056 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007057 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007058 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007059
Chris Lattner18c59872009-06-27 04:16:01 +00007060 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7061 OpFlag);
7062 DebugLoc DL = JT->getDebugLoc();
7063 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007064
Chris Lattner18c59872009-06-27 04:16:01 +00007065 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007066 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007067 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7068 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007069 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007070 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007071
Chris Lattner18c59872009-06-27 04:16:01 +00007072 return Result;
7073}
7074
7075SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007076X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007077 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007078
Chris Lattner18c59872009-06-27 04:16:01 +00007079 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7080 // global base reg.
7081 unsigned char OpFlag = 0;
7082 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007083 CodeModel::Model M = getTargetMachine().getCodeModel();
7084
Chris Lattner4f066492009-07-11 20:29:19 +00007085 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007086 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7087 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7088 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007089 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007090 } else if (Subtarget->isPICStyleGOT()) {
7091 OpFlag = X86II::MO_GOT;
7092 } else if (Subtarget->isPICStyleStubPIC()) {
7093 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7094 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7095 OpFlag = X86II::MO_DARWIN_NONLAZY;
7096 }
Eric Christopherfd179292009-08-27 18:07:15 +00007097
Chris Lattner18c59872009-06-27 04:16:01 +00007098 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007099
Chris Lattner18c59872009-06-27 04:16:01 +00007100 DebugLoc DL = Op.getDebugLoc();
7101 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007102
7103
Chris Lattner18c59872009-06-27 04:16:01 +00007104 // With PIC, the address is actually $g + Offset.
7105 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007106 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007107 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7108 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007109 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007110 Result);
7111 }
Eric Christopherfd179292009-08-27 18:07:15 +00007112
Eli Friedman586272d2011-08-11 01:48:05 +00007113 // For symbols that require a load from a stub to get the address, emit the
7114 // load.
7115 if (isGlobalStubReference(OpFlag))
7116 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007117 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007118
Chris Lattner18c59872009-06-27 04:16:01 +00007119 return Result;
7120}
7121
Dan Gohman475871a2008-07-27 21:46:04 +00007122SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007123X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007124 // Create the TargetBlockAddressAddress node.
7125 unsigned char OpFlags =
7126 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007127 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007128 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007129 DebugLoc dl = Op.getDebugLoc();
7130 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7131 /*isTarget=*/true, OpFlags);
7132
Dan Gohmanf705adb2009-10-30 01:28:02 +00007133 if (Subtarget->isPICStyleRIPRel() &&
7134 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007135 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7136 else
7137 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007138
Dan Gohman29cbade2009-11-20 23:18:13 +00007139 // With PIC, the address is actually $g + Offset.
7140 if (isGlobalRelativeToPICBase(OpFlags)) {
7141 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7142 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7143 Result);
7144 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007145
7146 return Result;
7147}
7148
7149SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007150X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007151 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007152 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007153 // Create the TargetGlobalAddress node, folding in the constant
7154 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007155 unsigned char OpFlags =
7156 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007157 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007158 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007159 if (OpFlags == X86II::MO_NO_FLAG &&
7160 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007161 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007162 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007163 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007164 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007165 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007166 }
Eric Christopherfd179292009-08-27 18:07:15 +00007167
Chris Lattner4f066492009-07-11 20:29:19 +00007168 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007169 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007170 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7171 else
7172 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007173
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007174 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007175 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007176 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7177 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007178 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007179 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007180
Chris Lattner36c25012009-07-10 07:34:39 +00007181 // For globals that require a load from a stub to get the address, emit the
7182 // load.
7183 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007184 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007185 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007186
Dan Gohman6520e202008-10-18 02:06:02 +00007187 // If there was a non-zero offset that we didn't fold, create an explicit
7188 // addition for it.
7189 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007190 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007191 DAG.getConstant(Offset, getPointerTy()));
7192
Evan Cheng0db9fe62006-04-25 20:13:52 +00007193 return Result;
7194}
7195
Evan Chengda43bcf2008-09-24 00:05:32 +00007196SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007197X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007198 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007199 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007200 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007201}
7202
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007203static SDValue
7204GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007205 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007206 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007207 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007208 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007209 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007210 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007211 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007212 GA->getOffset(),
7213 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007214 if (InFlag) {
7215 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007216 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007217 } else {
7218 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007219 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007220 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007221
7222 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007223 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007224
Rafael Espindola15f1b662009-04-24 12:59:40 +00007225 SDValue Flag = Chain.getValue(1);
7226 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007227}
7228
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007229// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007230static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007231LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007232 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007233 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007234 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7235 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007236 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007237 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007238 InFlag = Chain.getValue(1);
7239
Chris Lattnerb903bed2009-06-26 21:20:29 +00007240 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007241}
7242
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007243// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007244static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007245LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007246 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007247 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7248 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007249}
7250
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007251// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7252// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007253static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007254 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007255 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007256 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007257
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007258 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7259 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7260 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007261
Michael J. Spencerec38de22010-10-10 22:04:20 +00007262 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007263 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007264 MachinePointerInfo(Ptr),
7265 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007266
Chris Lattnerb903bed2009-06-26 21:20:29 +00007267 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007268 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7269 // initialexec.
7270 unsigned WrapperKind = X86ISD::Wrapper;
7271 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007272 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007273 } else if (is64Bit) {
7274 assert(model == TLSModel::InitialExec);
7275 OperandFlags = X86II::MO_GOTTPOFF;
7276 WrapperKind = X86ISD::WrapperRIP;
7277 } else {
7278 assert(model == TLSModel::InitialExec);
7279 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007280 }
Eric Christopherfd179292009-08-27 18:07:15 +00007281
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007282 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7283 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007284 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007285 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007286 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007287 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007288
Rafael Espindola9a580232009-02-27 13:37:18 +00007289 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007290 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007291 MachinePointerInfo::getGOT(), false, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007292
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007293 // The address of the thread local variable is the add of the thread
7294 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007295 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007296}
7297
Dan Gohman475871a2008-07-27 21:46:04 +00007298SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007299X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007300
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007301 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007302 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007303
Eric Christopher30ef0e52010-06-03 04:07:48 +00007304 if (Subtarget->isTargetELF()) {
7305 // TODO: implement the "local dynamic" model
7306 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007307
Eric Christopher30ef0e52010-06-03 04:07:48 +00007308 // If GV is an alias then use the aliasee for determining
7309 // thread-localness.
7310 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7311 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007312
7313 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007314 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007315
Eric Christopher30ef0e52010-06-03 04:07:48 +00007316 switch (model) {
7317 case TLSModel::GeneralDynamic:
7318 case TLSModel::LocalDynamic: // not implemented
7319 if (Subtarget->is64Bit())
7320 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7321 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007322
Eric Christopher30ef0e52010-06-03 04:07:48 +00007323 case TLSModel::InitialExec:
7324 case TLSModel::LocalExec:
7325 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7326 Subtarget->is64Bit());
7327 }
7328 } else if (Subtarget->isTargetDarwin()) {
7329 // Darwin only has one model of TLS. Lower to that.
7330 unsigned char OpFlag = 0;
7331 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7332 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007333
Eric Christopher30ef0e52010-06-03 04:07:48 +00007334 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7335 // global base reg.
7336 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7337 !Subtarget->is64Bit();
7338 if (PIC32)
7339 OpFlag = X86II::MO_TLVP_PIC_BASE;
7340 else
7341 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007342 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007343 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007344 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007345 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007346 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007347
Eric Christopher30ef0e52010-06-03 04:07:48 +00007348 // With PIC32, the address is actually $g + Offset.
7349 if (PIC32)
7350 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7351 DAG.getNode(X86ISD::GlobalBaseReg,
7352 DebugLoc(), getPointerTy()),
7353 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007354
Eric Christopher30ef0e52010-06-03 04:07:48 +00007355 // Lowering the machine isd will make sure everything is in the right
7356 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007357 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007358 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007359 SDValue Args[] = { Chain, Offset };
7360 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007361
Eric Christopher30ef0e52010-06-03 04:07:48 +00007362 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7363 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7364 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007365
Eric Christopher30ef0e52010-06-03 04:07:48 +00007366 // And our return value (tls address) is in the standard call return value
7367 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007368 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007369 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7370 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007371 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007372
David Blaikie4d6ccb52012-01-20 21:51:11 +00007373 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007374}
7375
Evan Cheng0db9fe62006-04-25 20:13:52 +00007376
Chad Rosierb90d2a92012-01-03 23:19:12 +00007377/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7378/// and take a 2 x i32 value to shift plus a shift amount.
7379SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007380 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007381 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007382 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007383 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007384 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007385 SDValue ShOpLo = Op.getOperand(0);
7386 SDValue ShOpHi = Op.getOperand(1);
7387 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007388 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007389 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007390 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007391
Dan Gohman475871a2008-07-27 21:46:04 +00007392 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007393 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007394 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7395 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007396 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007397 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7398 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007399 }
Evan Chenge3413162006-01-09 18:33:28 +00007400
Owen Anderson825b72b2009-08-11 20:47:22 +00007401 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7402 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007403 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007404 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007405
Dan Gohman475871a2008-07-27 21:46:04 +00007406 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007407 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007408 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7409 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007410
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007411 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007412 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7413 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007414 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007415 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7416 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007417 }
7418
Dan Gohman475871a2008-07-27 21:46:04 +00007419 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007420 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007421}
Evan Chenga3195e82006-01-12 22:54:21 +00007422
Dan Gohmand858e902010-04-17 15:26:15 +00007423SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7424 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007425 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007426
Dale Johannesen0488fb62010-09-30 23:57:10 +00007427 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007428 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007429
Owen Anderson825b72b2009-08-11 20:47:22 +00007430 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007431 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007432
Eli Friedman36df4992009-05-27 00:47:34 +00007433 // These are really Legal; return the operand so the caller accepts it as
7434 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007435 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007436 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007437 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007438 Subtarget->is64Bit()) {
7439 return Op;
7440 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007441
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007442 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007443 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007444 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007445 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007446 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007447 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007448 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007449 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007450 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007451 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7452}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007453
Owen Andersone50ed302009-08-10 22:56:29 +00007454SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007455 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007456 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007457 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007458 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007459 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007460 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007461 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007462 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007463 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007464 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007465
Chris Lattner492a43e2010-09-22 01:28:21 +00007466 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007467
Stuart Hastings84be9582011-06-02 15:57:11 +00007468 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7469 MachineMemOperand *MMO;
7470 if (FI) {
7471 int SSFI = FI->getIndex();
7472 MMO =
7473 DAG.getMachineFunction()
7474 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7475 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7476 } else {
7477 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7478 StackSlot = StackSlot.getOperand(1);
7479 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007480 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007481 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7482 X86ISD::FILD, DL,
7483 Tys, Ops, array_lengthof(Ops),
7484 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007485
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007486 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007487 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007488 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007489
7490 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7491 // shouldn't be necessary except that RFP cannot be live across
7492 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007493 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007494 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7495 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007496 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007497 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007498 SDValue Ops[] = {
7499 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7500 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007501 MachineMemOperand *MMO =
7502 DAG.getMachineFunction()
7503 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007504 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007505
Chris Lattner492a43e2010-09-22 01:28:21 +00007506 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7507 Ops, array_lengthof(Ops),
7508 Op.getValueType(), MMO);
7509 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007510 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007511 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007512 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007513
Evan Cheng0db9fe62006-04-25 20:13:52 +00007514 return Result;
7515}
7516
Bill Wendling8b8a6362009-01-17 03:56:04 +00007517// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007518SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7519 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007520 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007521 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007522 movq %rax, %xmm0
7523 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7524 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7525 #ifdef __SSE3__
7526 haddpd %xmm0, %xmm0
7527 #else
7528 pshufd $0x4e, %xmm0, %xmm1
7529 addpd %xmm1, %xmm0
7530 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007531 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007532
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007533 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007534 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007535
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007536 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00007537 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
7538 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007539 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007540
Chris Lattner97484792012-01-25 09:56:22 +00007541 SmallVector<Constant*,2> CV1;
7542 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00007543 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00007544 CV1.push_back(
7545 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7546 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007547 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007548
Bill Wendling397ae212012-01-05 02:13:20 +00007549 // Load the 64-bit value into an XMM register.
7550 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
7551 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007552 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007553 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007554 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007555 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
7556 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
7557 CLod0);
7558
Owen Anderson825b72b2009-08-11 20:47:22 +00007559 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007560 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007561 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007562 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007563 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00007564 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007565
Craig Topperd0a31172012-01-10 06:37:29 +00007566 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00007567 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
7568 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
7569 } else {
7570 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
7571 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
7572 S2F, 0x4E, DAG);
7573 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
7574 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
7575 Sub);
7576 }
7577
7578 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007579 DAG.getIntPtrConstant(0));
7580}
7581
Bill Wendling8b8a6362009-01-17 03:56:04 +00007582// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007583SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7584 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007585 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007586 // FP constant to bias correct the final result.
7587 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007588 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007589
7590 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007591 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007592 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007593
Eli Friedmanf3704762011-08-29 21:15:46 +00007594 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00007595 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007596
Owen Anderson825b72b2009-08-11 20:47:22 +00007597 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007598 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007599 DAG.getIntPtrConstant(0));
7600
7601 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007602 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007603 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007604 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007605 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007606 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007607 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007608 MVT::v2f64, Bias)));
7609 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007610 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007611 DAG.getIntPtrConstant(0));
7612
7613 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007614 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007615
7616 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007617 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007618
Owen Anderson825b72b2009-08-11 20:47:22 +00007619 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007620 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007621 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007622 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007623 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007624 }
7625
7626 // Handle final rounding.
7627 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007628}
7629
Dan Gohmand858e902010-04-17 15:26:15 +00007630SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7631 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007632 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007633 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007634
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007635 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007636 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7637 // the optimization here.
7638 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007639 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007640
Owen Andersone50ed302009-08-10 22:56:29 +00007641 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007642 EVT DstVT = Op.getValueType();
7643 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007644 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007645 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007646 return LowerUINT_TO_FP_i32(Op, DAG);
Bill Wendlingf6c07472012-01-10 19:41:30 +00007647 else if (Subtarget->is64Bit() &&
7648 SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00007649 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00007650
7651 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007652 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007653 if (SrcVT == MVT::i32) {
7654 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7655 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7656 getPointerTy(), StackSlot, WordOff);
7657 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007658 StackSlot, MachinePointerInfo(),
7659 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007660 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007661 OffsetSlot, MachinePointerInfo(),
7662 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007663 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7664 return Fild;
7665 }
7666
7667 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7668 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00007669 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007670 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007671 // For i64 source, we need to add the appropriate power of 2 if the input
7672 // was negative. This is the same as the optimization in
7673 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7674 // we must be careful to do the computation in x87 extended precision, not
7675 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007676 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7677 MachineMemOperand *MMO =
7678 DAG.getMachineFunction()
7679 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7680 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007681
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007682 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7683 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007684 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7685 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007686
7687 APInt FF(32, 0x5F800000ULL);
7688
7689 // Check whether the sign bit is set.
7690 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7691 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7692 ISD::SETLT);
7693
7694 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7695 SDValue FudgePtr = DAG.getConstantPool(
7696 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7697 getPointerTy());
7698
7699 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7700 SDValue Zero = DAG.getIntPtrConstant(0);
7701 SDValue Four = DAG.getIntPtrConstant(4);
7702 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7703 Zero, Four);
7704 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7705
7706 // Load the value out, extending it from f32 to f80.
7707 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007708 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007709 FudgePtr, MachinePointerInfo::getConstantPool(),
7710 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007711 // Extend everything to 80 bits to force it to be done on x87.
7712 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7713 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007714}
7715
Dan Gohman475871a2008-07-27 21:46:04 +00007716std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007717FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007718 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007719
Owen Andersone50ed302009-08-10 22:56:29 +00007720 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007721
7722 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007723 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7724 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007725 }
7726
Owen Anderson825b72b2009-08-11 20:47:22 +00007727 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7728 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007729 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007730
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007731 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007732 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007733 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007734 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007735 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007736 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007737 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007738 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007739
Evan Cheng87c89352007-10-15 20:11:21 +00007740 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7741 // stack slot.
7742 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007743 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007744 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007745 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007746
Michael J. Spencerec38de22010-10-10 22:04:20 +00007747
7748
Evan Cheng0db9fe62006-04-25 20:13:52 +00007749 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007750 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007751 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007752 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7753 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7754 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007755 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007756
Dan Gohman475871a2008-07-27 21:46:04 +00007757 SDValue Chain = DAG.getEntryNode();
7758 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007759 EVT TheVT = Op.getOperand(0).getValueType();
7760 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007761 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007762 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007763 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007764 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007765 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007766 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007767 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007768 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007769
Chris Lattner492a43e2010-09-22 01:28:21 +00007770 MachineMemOperand *MMO =
7771 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7772 MachineMemOperand::MOLoad, MemSize, MemSize);
7773 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7774 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007775 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007776 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007777 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7778 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007779
Chris Lattner07290932010-09-22 01:05:16 +00007780 MachineMemOperand *MMO =
7781 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7782 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007783
Evan Cheng0db9fe62006-04-25 20:13:52 +00007784 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007785 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007786 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7787 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007788
Chris Lattner27a6c732007-11-24 07:07:01 +00007789 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007790}
7791
Dan Gohmand858e902010-04-17 15:26:15 +00007792SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7793 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007794 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007795 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007796
Eli Friedman948e95a2009-05-23 09:59:16 +00007797 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007798 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007799 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7800 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007801
Chris Lattner27a6c732007-11-24 07:07:01 +00007802 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007803 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007804 FIST, StackSlot, MachinePointerInfo(),
7805 false, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007806}
7807
Dan Gohmand858e902010-04-17 15:26:15 +00007808SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7809 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007810 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7811 SDValue FIST = Vals.first, StackSlot = Vals.second;
7812 assert(FIST.getNode() && "Unexpected failure");
7813
7814 // Load the result.
7815 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007816 FIST, StackSlot, MachinePointerInfo(),
7817 false, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007818}
7819
Dan Gohmand858e902010-04-17 15:26:15 +00007820SDValue X86TargetLowering::LowerFABS(SDValue Op,
7821 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007822 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007823 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007824 EVT VT = Op.getValueType();
7825 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007826 if (VT.isVector())
7827 EltVT = VT.getVectorElementType();
Chris Lattner4ca829e2012-01-25 06:02:56 +00007828 Constant *C;
Owen Anderson825b72b2009-08-11 20:47:22 +00007829 if (EltVT == MVT::f64) {
Chris Lattner4ca829e2012-01-25 06:02:56 +00007830 C = ConstantVector::getSplat(2,
7831 ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007832 } else {
Chris Lattner4ca829e2012-01-25 06:02:56 +00007833 C = ConstantVector::getSplat(4,
7834 ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007835 }
Evan Cheng1606e8e2009-03-13 07:51:59 +00007836 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007837 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007838 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007839 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007840 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007841}
7842
Dan Gohmand858e902010-04-17 15:26:15 +00007843SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007844 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007845 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007846 EVT VT = Op.getValueType();
7847 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00007848 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
7849 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007850 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00007851 NumElts = VT.getVectorNumElements();
7852 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00007853 Constant *C;
7854 if (EltVT == MVT::f64)
7855 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
7856 else
7857 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
7858 C = ConstantVector::getSplat(NumElts, C);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007859 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007860 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007861 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007862 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007863 if (VT.isVector()) {
Chad Rosiera860b182011-12-15 01:02:25 +00007864 MVT XORVT = VT.getSizeInBits() == 128 ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007865 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00007866 DAG.getNode(ISD::XOR, dl, XORVT,
7867 DAG.getNode(ISD::BITCAST, dl, XORVT,
Dale Johannesenace16102009-02-03 19:33:06 +00007868 Op.getOperand(0)),
Chad Rosiera860b182011-12-15 01:02:25 +00007869 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007870 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007871 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007872 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007873}
7874
Dan Gohmand858e902010-04-17 15:26:15 +00007875SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007876 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007877 SDValue Op0 = Op.getOperand(0);
7878 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007879 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007880 EVT VT = Op.getValueType();
7881 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007882
7883 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007884 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007885 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007886 SrcVT = VT;
7887 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007888 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007889 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007890 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007891 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007892 }
7893
7894 // At this point the operands and the result should have the same
7895 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007896
Evan Cheng68c47cb2007-01-05 07:55:56 +00007897 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00007898 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007899 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007900 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7901 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007902 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007903 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7904 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7905 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7906 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007907 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007908 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007909 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007910 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007911 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007912 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007913 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007914
7915 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007916 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007917 // Op0 is MVT::f32, Op1 is MVT::f64.
7918 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7919 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7920 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007921 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007922 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007923 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007924 }
7925
Evan Cheng73d6cf12007-01-05 21:37:56 +00007926 // Clear first operand sign bit.
7927 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007928 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007929 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7930 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007931 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007932 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7933 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7934 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7935 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007936 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007937 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007938 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007939 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007940 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007941 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007942 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007943
7944 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007945 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007946}
7947
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00007948SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
7949 SDValue N0 = Op.getOperand(0);
7950 DebugLoc dl = Op.getDebugLoc();
7951 EVT VT = Op.getValueType();
7952
7953 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
7954 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
7955 DAG.getConstant(1, VT));
7956 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
7957}
7958
Dan Gohman076aee32009-03-04 19:44:21 +00007959/// Emit nodes that will be selected as "test Op0,Op0", or something
7960/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007961SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007962 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007963 DebugLoc dl = Op.getDebugLoc();
7964
Dan Gohman31125812009-03-07 01:58:32 +00007965 // CF and OF aren't always set the way we want. Determine which
7966 // of these we need.
7967 bool NeedCF = false;
7968 bool NeedOF = false;
7969 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007970 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007971 case X86::COND_A: case X86::COND_AE:
7972 case X86::COND_B: case X86::COND_BE:
7973 NeedCF = true;
7974 break;
7975 case X86::COND_G: case X86::COND_GE:
7976 case X86::COND_L: case X86::COND_LE:
7977 case X86::COND_O: case X86::COND_NO:
7978 NeedOF = true;
7979 break;
Dan Gohman31125812009-03-07 01:58:32 +00007980 }
7981
Dan Gohman076aee32009-03-04 19:44:21 +00007982 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007983 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7984 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007985 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7986 // Emit a CMP with 0, which is the TEST pattern.
7987 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7988 DAG.getConstant(0, Op.getValueType()));
7989
7990 unsigned Opcode = 0;
7991 unsigned NumOperands = 0;
7992 switch (Op.getNode()->getOpcode()) {
7993 case ISD::ADD:
7994 // Due to an isel shortcoming, be conservative if this add is likely to be
7995 // selected as part of a load-modify-store instruction. When the root node
7996 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7997 // uses of other nodes in the match, such as the ADD in this case. This
7998 // leads to the ADD being left around and reselected, with the result being
7999 // two adds in the output. Alas, even if none our users are stores, that
8000 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8001 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8002 // climbing the DAG back to the root, and it doesn't seem to be worth the
8003 // effort.
8004 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008005 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8006 if (UI->getOpcode() != ISD::CopyToReg &&
8007 UI->getOpcode() != ISD::SETCC &&
8008 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008009 goto default_case;
8010
8011 if (ConstantSDNode *C =
8012 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8013 // An add of one will be selected as an INC.
8014 if (C->getAPIntValue() == 1) {
8015 Opcode = X86ISD::INC;
8016 NumOperands = 1;
8017 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008018 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008019
8020 // An add of negative one (subtract of one) will be selected as a DEC.
8021 if (C->getAPIntValue().isAllOnesValue()) {
8022 Opcode = X86ISD::DEC;
8023 NumOperands = 1;
8024 break;
8025 }
Dan Gohman076aee32009-03-04 19:44:21 +00008026 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008027
8028 // Otherwise use a regular EFLAGS-setting add.
8029 Opcode = X86ISD::ADD;
8030 NumOperands = 2;
8031 break;
8032 case ISD::AND: {
8033 // If the primary and result isn't used, don't bother using X86ISD::AND,
8034 // because a TEST instruction will be better.
8035 bool NonFlagUse = false;
8036 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8037 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8038 SDNode *User = *UI;
8039 unsigned UOpNo = UI.getOperandNo();
8040 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8041 // Look pass truncate.
8042 UOpNo = User->use_begin().getOperandNo();
8043 User = *User->use_begin();
8044 }
8045
8046 if (User->getOpcode() != ISD::BRCOND &&
8047 User->getOpcode() != ISD::SETCC &&
8048 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8049 NonFlagUse = true;
8050 break;
8051 }
Dan Gohman076aee32009-03-04 19:44:21 +00008052 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008053
8054 if (!NonFlagUse)
8055 break;
8056 }
8057 // FALL THROUGH
8058 case ISD::SUB:
8059 case ISD::OR:
8060 case ISD::XOR:
8061 // Due to the ISEL shortcoming noted above, be conservative if this op is
8062 // likely to be selected as part of a load-modify-store instruction.
8063 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8064 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8065 if (UI->getOpcode() == ISD::STORE)
8066 goto default_case;
8067
8068 // Otherwise use a regular EFLAGS-setting instruction.
8069 switch (Op.getNode()->getOpcode()) {
8070 default: llvm_unreachable("unexpected operator!");
8071 case ISD::SUB: Opcode = X86ISD::SUB; break;
8072 case ISD::OR: Opcode = X86ISD::OR; break;
8073 case ISD::XOR: Opcode = X86ISD::XOR; break;
8074 case ISD::AND: Opcode = X86ISD::AND; break;
8075 }
8076
8077 NumOperands = 2;
8078 break;
8079 case X86ISD::ADD:
8080 case X86ISD::SUB:
8081 case X86ISD::INC:
8082 case X86ISD::DEC:
8083 case X86ISD::OR:
8084 case X86ISD::XOR:
8085 case X86ISD::AND:
8086 return SDValue(Op.getNode(), 1);
8087 default:
8088 default_case:
8089 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008090 }
8091
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008092 if (Opcode == 0)
8093 // Emit a CMP with 0, which is the TEST pattern.
8094 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8095 DAG.getConstant(0, Op.getValueType()));
8096
8097 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8098 SmallVector<SDValue, 4> Ops;
8099 for (unsigned i = 0; i != NumOperands; ++i)
8100 Ops.push_back(Op.getOperand(i));
8101
8102 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8103 DAG.ReplaceAllUsesWith(Op, New);
8104 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008105}
8106
8107/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8108/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008109SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008110 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008111 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8112 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008113 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008114
8115 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008116 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008117}
8118
Evan Chengd40d03e2010-01-06 19:38:29 +00008119/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8120/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008121SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8122 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008123 SDValue Op0 = And.getOperand(0);
8124 SDValue Op1 = And.getOperand(1);
8125 if (Op0.getOpcode() == ISD::TRUNCATE)
8126 Op0 = Op0.getOperand(0);
8127 if (Op1.getOpcode() == ISD::TRUNCATE)
8128 Op1 = Op1.getOperand(0);
8129
Evan Chengd40d03e2010-01-06 19:38:29 +00008130 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008131 if (Op1.getOpcode() == ISD::SHL)
8132 std::swap(Op0, Op1);
8133 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008134 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8135 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008136 // If we looked past a truncate, check that it's only truncating away
8137 // known zeros.
8138 unsigned BitWidth = Op0.getValueSizeInBits();
8139 unsigned AndBitWidth = And.getValueSizeInBits();
8140 if (BitWidth > AndBitWidth) {
8141 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8142 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8143 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8144 return SDValue();
8145 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008146 LHS = Op1;
8147 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008148 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008149 } else if (Op1.getOpcode() == ISD::Constant) {
8150 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008151 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008152 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008153
8154 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008155 LHS = AndLHS.getOperand(0);
8156 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008157 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008158
8159 // Use BT if the immediate can't be encoded in a TEST instruction.
8160 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8161 LHS = AndLHS;
8162 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8163 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008164 }
Evan Cheng0488db92007-09-25 01:57:46 +00008165
Evan Chengd40d03e2010-01-06 19:38:29 +00008166 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008167 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008168 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008169 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008170 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008171 // Also promote i16 to i32 for performance / code size reason.
8172 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008173 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008174 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008175
Evan Chengd40d03e2010-01-06 19:38:29 +00008176 // If the operand types disagree, extend the shift amount to match. Since
8177 // BT ignores high bits (like shifts) we can use anyextend.
8178 if (LHS.getValueType() != RHS.getValueType())
8179 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008180
Evan Chengd40d03e2010-01-06 19:38:29 +00008181 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8182 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8183 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8184 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008185 }
8186
Evan Cheng54de3ea2010-01-05 06:52:31 +00008187 return SDValue();
8188}
8189
Dan Gohmand858e902010-04-17 15:26:15 +00008190SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008191
8192 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8193
Evan Cheng54de3ea2010-01-05 06:52:31 +00008194 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8195 SDValue Op0 = Op.getOperand(0);
8196 SDValue Op1 = Op.getOperand(1);
8197 DebugLoc dl = Op.getDebugLoc();
8198 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8199
8200 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008201 // Lower (X & (1 << N)) == 0 to BT(X, N).
8202 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8203 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008204 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008205 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008206 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008207 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8208 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8209 if (NewSetCC.getNode())
8210 return NewSetCC;
8211 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008212
Chris Lattner481eebc2010-12-19 21:23:48 +00008213 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8214 // these.
8215 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008216 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008217 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8218 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008219
Chris Lattner481eebc2010-12-19 21:23:48 +00008220 // If the input is a setcc, then reuse the input setcc or use a new one with
8221 // the inverted condition.
8222 if (Op0.getOpcode() == X86ISD::SETCC) {
8223 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8224 bool Invert = (CC == ISD::SETNE) ^
8225 cast<ConstantSDNode>(Op1)->isNullValue();
8226 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008227
Evan Cheng2c755ba2010-02-27 07:36:59 +00008228 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008229 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8230 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8231 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008232 }
8233
Evan Chenge5b51ac2010-04-17 06:13:15 +00008234 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008235 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008236 if (X86CC == X86::COND_INVALID)
8237 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008238
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008239 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008240 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008241 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008242}
8243
Craig Topper89af15e2011-09-18 08:03:58 +00008244// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008245// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008246static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008247 EVT VT = Op.getValueType();
8248
Duncan Sands28b77e92011-09-06 19:07:46 +00008249 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008250 "Unsupported value type for operation");
8251
8252 int NumElems = VT.getVectorNumElements();
8253 DebugLoc dl = Op.getDebugLoc();
8254 SDValue CC = Op.getOperand(2);
8255 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8256 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8257
8258 // Extract the LHS vectors
8259 SDValue LHS = Op.getOperand(0);
8260 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8261 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8262
8263 // Extract the RHS vectors
8264 SDValue RHS = Op.getOperand(1);
8265 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8266 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8267
8268 // Issue the operation on the smaller types and concatenate the result back
8269 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8270 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8271 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8272 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8273 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8274}
8275
8276
Dan Gohmand858e902010-04-17 15:26:15 +00008277SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008278 SDValue Cond;
8279 SDValue Op0 = Op.getOperand(0);
8280 SDValue Op1 = Op.getOperand(1);
8281 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008282 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008283 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8284 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008285 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008286
8287 if (isFP) {
8288 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008289 EVT EltVT = Op0.getValueType().getVectorElementType();
Duncan Sands5b8a1db2012-02-05 14:20:11 +00008290 assert(EltVT == MVT::f32 || EltVT == MVT::f64); (void)EltVT;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008291
Nate Begeman30a0de92008-07-17 16:51:19 +00008292 bool Swap = false;
8293
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008294 // SSE Condition code mapping:
8295 // 0 - EQ
8296 // 1 - LT
8297 // 2 - LE
8298 // 3 - UNORD
8299 // 4 - NEQ
8300 // 5 - NLT
8301 // 6 - NLE
8302 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008303 switch (SetCCOpcode) {
8304 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008305 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008306 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008307 case ISD::SETOGT:
8308 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008309 case ISD::SETLT:
8310 case ISD::SETOLT: SSECC = 1; break;
8311 case ISD::SETOGE:
8312 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008313 case ISD::SETLE:
8314 case ISD::SETOLE: SSECC = 2; break;
8315 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008316 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008317 case ISD::SETNE: SSECC = 4; break;
8318 case ISD::SETULE: Swap = true;
8319 case ISD::SETUGE: SSECC = 5; break;
8320 case ISD::SETULT: Swap = true;
8321 case ISD::SETUGT: SSECC = 6; break;
8322 case ISD::SETO: SSECC = 7; break;
8323 }
8324 if (Swap)
8325 std::swap(Op0, Op1);
8326
Nate Begemanfb8ead02008-07-25 19:05:58 +00008327 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008328 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008329 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008330 SDValue UNORD, EQ;
Craig Topper1906d322012-01-22 23:36:02 +00008331 UNORD = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8332 DAG.getConstant(3, MVT::i8));
8333 EQ = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8334 DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008335 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Craig Topper0a150352011-11-09 08:06:13 +00008336 } else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008337 SDValue ORD, NEQ;
Craig Topper1906d322012-01-22 23:36:02 +00008338 ORD = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8339 DAG.getConstant(7, MVT::i8));
8340 NEQ = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8341 DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008342 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008343 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008344 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008345 }
8346 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008347 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8348 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008349 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008350
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008351 // Break 256-bit integer vector compare into smaller ones.
Craig Topper0a150352011-11-09 08:06:13 +00008352 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008353 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008354
Nate Begeman30a0de92008-07-17 16:51:19 +00008355 // We are handling one of the integer comparisons here. Since SSE only has
8356 // GT and EQ comparisons for integer, swapping operands and multiple
8357 // operations may be required for some comparisons.
Craig Topper67609fd2012-01-22 22:42:16 +00008358 unsigned Opc = 0;
Nate Begeman30a0de92008-07-17 16:51:19 +00008359 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008360
Nate Begeman30a0de92008-07-17 16:51:19 +00008361 switch (SetCCOpcode) {
8362 default: break;
8363 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008364 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008365 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008366 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008367 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008368 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008369 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008370 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008371 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008372 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008373 }
8374 if (Swap)
8375 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008376
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008377 // Check that the operation in question is available (most are plain SSE2,
8378 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper67609fd2012-01-22 22:42:16 +00008379 if (Opc == X86ISD::PCMPGT && VT == MVT::v2i64 && !Subtarget->hasSSE42())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008380 return SDValue();
Craig Topper67609fd2012-01-22 22:42:16 +00008381 if (Opc == X86ISD::PCMPEQ && VT == MVT::v2i64 && !Subtarget->hasSSE41())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008382 return SDValue();
8383
Nate Begeman30a0de92008-07-17 16:51:19 +00008384 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8385 // bits of the inputs before performing those operations.
8386 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008387 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008388 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8389 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008390 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008391 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8392 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008393 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8394 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008395 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008396
Dale Johannesenace16102009-02-03 19:33:06 +00008397 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008398
8399 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008400 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008401 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008402
Nate Begeman30a0de92008-07-17 16:51:19 +00008403 return Result;
8404}
Evan Cheng0488db92007-09-25 01:57:46 +00008405
Evan Cheng370e5342008-12-03 08:38:43 +00008406// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008407static bool isX86LogicalCmp(SDValue Op) {
8408 unsigned Opc = Op.getNode()->getOpcode();
8409 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8410 return true;
8411 if (Op.getResNo() == 1 &&
8412 (Opc == X86ISD::ADD ||
8413 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008414 Opc == X86ISD::ADC ||
8415 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008416 Opc == X86ISD::SMUL ||
8417 Opc == X86ISD::UMUL ||
8418 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008419 Opc == X86ISD::DEC ||
8420 Opc == X86ISD::OR ||
8421 Opc == X86ISD::XOR ||
8422 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008423 return true;
8424
Chris Lattner9637d5b2010-12-05 07:49:54 +00008425 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8426 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008427
Dan Gohman076aee32009-03-04 19:44:21 +00008428 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008429}
8430
Chris Lattnera2b56002010-12-05 01:23:24 +00008431static bool isZero(SDValue V) {
8432 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8433 return C && C->isNullValue();
8434}
8435
Chris Lattner96908b12010-12-05 02:00:51 +00008436static bool isAllOnes(SDValue V) {
8437 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8438 return C && C->isAllOnesValue();
8439}
8440
Dan Gohmand858e902010-04-17 15:26:15 +00008441SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008442 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008443 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008444 SDValue Op1 = Op.getOperand(1);
8445 SDValue Op2 = Op.getOperand(2);
8446 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008447 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008448
Dan Gohman1a492952009-10-20 16:22:37 +00008449 if (Cond.getOpcode() == ISD::SETCC) {
8450 SDValue NewCond = LowerSETCC(Cond, DAG);
8451 if (NewCond.getNode())
8452 Cond = NewCond;
8453 }
Evan Cheng734503b2006-09-11 02:19:56 +00008454
Chris Lattnera2b56002010-12-05 01:23:24 +00008455 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008456 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008457 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008458 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008459 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008460 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8461 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008462 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008463
Chris Lattnera2b56002010-12-05 01:23:24 +00008464 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008465
8466 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008467 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8468 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008469
8470 SDValue CmpOp0 = Cmp.getOperand(0);
8471 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8472 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008473
Chris Lattner96908b12010-12-05 02:00:51 +00008474 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008475 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8476 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008477
Chris Lattner96908b12010-12-05 02:00:51 +00008478 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8479 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008480
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008481 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008482 if (N2C == 0 || !N2C->isNullValue())
8483 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8484 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008485 }
8486 }
8487
Chris Lattnera2b56002010-12-05 01:23:24 +00008488 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008489 if (Cond.getOpcode() == ISD::AND &&
8490 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8491 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008492 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008493 Cond = Cond.getOperand(0);
8494 }
8495
Evan Cheng3f41d662007-10-08 22:16:29 +00008496 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8497 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008498 unsigned CondOpcode = Cond.getOpcode();
8499 if (CondOpcode == X86ISD::SETCC ||
8500 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008501 CC = Cond.getOperand(0);
8502
Dan Gohman475871a2008-07-27 21:46:04 +00008503 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008504 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008505 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008506
Evan Cheng3f41d662007-10-08 22:16:29 +00008507 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008508 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008509 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008510 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008511
Chris Lattnerd1980a52009-03-12 06:52:53 +00008512 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8513 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008514 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008515 addTest = false;
8516 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008517 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8518 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8519 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8520 Cond.getOperand(0).getValueType() != MVT::i8)) {
8521 SDValue LHS = Cond.getOperand(0);
8522 SDValue RHS = Cond.getOperand(1);
8523 unsigned X86Opcode;
8524 unsigned X86Cond;
8525 SDVTList VTs;
8526 switch (CondOpcode) {
8527 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8528 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8529 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8530 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8531 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8532 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8533 default: llvm_unreachable("unexpected overflowing operator");
8534 }
8535 if (CondOpcode == ISD::UMULO)
8536 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8537 MVT::i32);
8538 else
8539 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8540
8541 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8542
8543 if (CondOpcode == ISD::UMULO)
8544 Cond = X86Op.getValue(2);
8545 else
8546 Cond = X86Op.getValue(1);
8547
8548 CC = DAG.getConstant(X86Cond, MVT::i8);
8549 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008550 }
8551
8552 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008553 // Look pass the truncate.
8554 if (Cond.getOpcode() == ISD::TRUNCATE)
8555 Cond = Cond.getOperand(0);
8556
8557 // We know the result of AND is compared against zero. Try to match
8558 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008559 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008560 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008561 if (NewSetCC.getNode()) {
8562 CC = NewSetCC.getOperand(0);
8563 Cond = NewSetCC.getOperand(1);
8564 addTest = false;
8565 }
8566 }
8567 }
8568
8569 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008570 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008571 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008572 }
8573
Benjamin Kramere915ff32010-12-22 23:09:28 +00008574 // a < b ? -1 : 0 -> RES = ~setcc_carry
8575 // a < b ? 0 : -1 -> RES = setcc_carry
8576 // a >= b ? -1 : 0 -> RES = setcc_carry
8577 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8578 if (Cond.getOpcode() == X86ISD::CMP) {
8579 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8580
8581 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8582 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8583 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8584 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8585 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8586 return DAG.getNOT(DL, Res, Res.getValueType());
8587 return Res;
8588 }
8589 }
8590
Evan Cheng0488db92007-09-25 01:57:46 +00008591 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8592 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008593 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008594 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008595 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008596}
8597
Evan Cheng370e5342008-12-03 08:38:43 +00008598// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8599// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8600// from the AND / OR.
8601static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8602 Opc = Op.getOpcode();
8603 if (Opc != ISD::OR && Opc != ISD::AND)
8604 return false;
8605 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8606 Op.getOperand(0).hasOneUse() &&
8607 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8608 Op.getOperand(1).hasOneUse());
8609}
8610
Evan Cheng961d6d42009-02-02 08:19:07 +00008611// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8612// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008613static bool isXor1OfSetCC(SDValue Op) {
8614 if (Op.getOpcode() != ISD::XOR)
8615 return false;
8616 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8617 if (N1C && N1C->getAPIntValue() == 1) {
8618 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8619 Op.getOperand(0).hasOneUse();
8620 }
8621 return false;
8622}
8623
Dan Gohmand858e902010-04-17 15:26:15 +00008624SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008625 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008626 SDValue Chain = Op.getOperand(0);
8627 SDValue Cond = Op.getOperand(1);
8628 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008629 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008630 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008631 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008632
Dan Gohman1a492952009-10-20 16:22:37 +00008633 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008634 // Check for setcc([su]{add,sub,mul}o == 0).
8635 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8636 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8637 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8638 Cond.getOperand(0).getResNo() == 1 &&
8639 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
8640 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
8641 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
8642 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
8643 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
8644 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
8645 Inverted = true;
8646 Cond = Cond.getOperand(0);
8647 } else {
8648 SDValue NewCond = LowerSETCC(Cond, DAG);
8649 if (NewCond.getNode())
8650 Cond = NewCond;
8651 }
Dan Gohman1a492952009-10-20 16:22:37 +00008652 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008653#if 0
8654 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008655 else if (Cond.getOpcode() == X86ISD::ADD ||
8656 Cond.getOpcode() == X86ISD::SUB ||
8657 Cond.getOpcode() == X86ISD::SMUL ||
8658 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008659 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008660#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008661
Evan Chengad9c0a32009-12-15 00:53:42 +00008662 // Look pass (and (setcc_carry (cmp ...)), 1).
8663 if (Cond.getOpcode() == ISD::AND &&
8664 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8665 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008666 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008667 Cond = Cond.getOperand(0);
8668 }
8669
Evan Cheng3f41d662007-10-08 22:16:29 +00008670 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8671 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008672 unsigned CondOpcode = Cond.getOpcode();
8673 if (CondOpcode == X86ISD::SETCC ||
8674 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008675 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008676
Dan Gohman475871a2008-07-27 21:46:04 +00008677 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008678 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008679 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008680 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008681 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008682 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008683 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008684 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008685 default: break;
8686 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008687 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008688 // These can only come from an arithmetic instruction with overflow,
8689 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008690 Cond = Cond.getNode()->getOperand(1);
8691 addTest = false;
8692 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008693 }
Evan Cheng0488db92007-09-25 01:57:46 +00008694 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008695 }
8696 CondOpcode = Cond.getOpcode();
8697 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8698 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8699 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8700 Cond.getOperand(0).getValueType() != MVT::i8)) {
8701 SDValue LHS = Cond.getOperand(0);
8702 SDValue RHS = Cond.getOperand(1);
8703 unsigned X86Opcode;
8704 unsigned X86Cond;
8705 SDVTList VTs;
8706 switch (CondOpcode) {
8707 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8708 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8709 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8710 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8711 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8712 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8713 default: llvm_unreachable("unexpected overflowing operator");
8714 }
8715 if (Inverted)
8716 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
8717 if (CondOpcode == ISD::UMULO)
8718 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8719 MVT::i32);
8720 else
8721 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8722
8723 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
8724
8725 if (CondOpcode == ISD::UMULO)
8726 Cond = X86Op.getValue(2);
8727 else
8728 Cond = X86Op.getValue(1);
8729
8730 CC = DAG.getConstant(X86Cond, MVT::i8);
8731 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00008732 } else {
8733 unsigned CondOpc;
8734 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8735 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008736 if (CondOpc == ISD::OR) {
8737 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8738 // two branches instead of an explicit OR instruction with a
8739 // separate test.
8740 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008741 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008742 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008743 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008744 Chain, Dest, CC, Cmp);
8745 CC = Cond.getOperand(1).getOperand(0);
8746 Cond = Cmp;
8747 addTest = false;
8748 }
8749 } else { // ISD::AND
8750 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8751 // two branches instead of an explicit AND instruction with a
8752 // separate test. However, we only do this if this block doesn't
8753 // have a fall-through edge, because this requires an explicit
8754 // jmp when the condition is false.
8755 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008756 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008757 Op.getNode()->hasOneUse()) {
8758 X86::CondCode CCode =
8759 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8760 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008761 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008762 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008763 // Look for an unconditional branch following this conditional branch.
8764 // We need this because we need to reverse the successors in order
8765 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008766 if (User->getOpcode() == ISD::BR) {
8767 SDValue FalseBB = User->getOperand(1);
8768 SDNode *NewBR =
8769 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008770 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008771 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008772 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008773
Dale Johannesene4d209d2009-02-03 20:21:25 +00008774 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008775 Chain, Dest, CC, Cmp);
8776 X86::CondCode CCode =
8777 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8778 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008779 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008780 Cond = Cmp;
8781 addTest = false;
8782 }
8783 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008784 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008785 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8786 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8787 // It should be transformed during dag combiner except when the condition
8788 // is set by a arithmetics with overflow node.
8789 X86::CondCode CCode =
8790 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8791 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008792 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008793 Cond = Cond.getOperand(0).getOperand(1);
8794 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00008795 } else if (Cond.getOpcode() == ISD::SETCC &&
8796 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
8797 // For FCMP_OEQ, we can emit
8798 // two branches instead of an explicit AND instruction with a
8799 // separate test. However, we only do this if this block doesn't
8800 // have a fall-through edge, because this requires an explicit
8801 // jmp when the condition is false.
8802 if (Op.getNode()->hasOneUse()) {
8803 SDNode *User = *Op.getNode()->use_begin();
8804 // Look for an unconditional branch following this conditional branch.
8805 // We need this because we need to reverse the successors in order
8806 // to implement FCMP_OEQ.
8807 if (User->getOpcode() == ISD::BR) {
8808 SDValue FalseBB = User->getOperand(1);
8809 SDNode *NewBR =
8810 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8811 assert(NewBR == User);
8812 (void)NewBR;
8813 Dest = FalseBB;
8814
8815 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
8816 Cond.getOperand(0), Cond.getOperand(1));
8817 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8818 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8819 Chain, Dest, CC, Cmp);
8820 CC = DAG.getConstant(X86::COND_P, MVT::i8);
8821 Cond = Cmp;
8822 addTest = false;
8823 }
8824 }
8825 } else if (Cond.getOpcode() == ISD::SETCC &&
8826 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
8827 // For FCMP_UNE, we can emit
8828 // two branches instead of an explicit AND instruction with a
8829 // separate test. However, we only do this if this block doesn't
8830 // have a fall-through edge, because this requires an explicit
8831 // jmp when the condition is false.
8832 if (Op.getNode()->hasOneUse()) {
8833 SDNode *User = *Op.getNode()->use_begin();
8834 // Look for an unconditional branch following this conditional branch.
8835 // We need this because we need to reverse the successors in order
8836 // to implement FCMP_UNE.
8837 if (User->getOpcode() == ISD::BR) {
8838 SDValue FalseBB = User->getOperand(1);
8839 SDNode *NewBR =
8840 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8841 assert(NewBR == User);
8842 (void)NewBR;
8843
8844 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
8845 Cond.getOperand(0), Cond.getOperand(1));
8846 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8847 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8848 Chain, Dest, CC, Cmp);
8849 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
8850 Cond = Cmp;
8851 addTest = false;
8852 Dest = FalseBB;
8853 }
8854 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008855 }
Evan Cheng0488db92007-09-25 01:57:46 +00008856 }
8857
8858 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008859 // Look pass the truncate.
8860 if (Cond.getOpcode() == ISD::TRUNCATE)
8861 Cond = Cond.getOperand(0);
8862
8863 // We know the result of AND is compared against zero. Try to match
8864 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008865 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008866 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8867 if (NewSetCC.getNode()) {
8868 CC = NewSetCC.getOperand(0);
8869 Cond = NewSetCC.getOperand(1);
8870 addTest = false;
8871 }
8872 }
8873 }
8874
8875 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008876 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008877 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008878 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008879 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008880 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008881}
8882
Anton Korobeynikove060b532007-04-17 19:34:00 +00008883
8884// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8885// Calls to _alloca is needed to probe the stack when allocating more than 4k
8886// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8887// that the guard pages used by the OS virtual memory manager are allocated in
8888// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008889SDValue
8890X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008891 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008892 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008893 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008894 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008895 "are being used");
8896 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008897 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008898
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008899 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008900 SDValue Chain = Op.getOperand(0);
8901 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008902 // FIXME: Ensure alignment here
8903
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008904 bool Is64Bit = Subtarget->is64Bit();
8905 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008906
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008907 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008908 MachineFunction &MF = DAG.getMachineFunction();
8909 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008910
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008911 if (Is64Bit) {
8912 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008913 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008914 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008915
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008916 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8917 I != E; I++)
8918 if (I->hasNestAttr())
8919 report_fatal_error("Cannot use segmented stacks with functions that "
8920 "have nested arguments.");
8921 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008922
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008923 const TargetRegisterClass *AddrRegClass =
8924 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8925 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8926 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8927 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8928 DAG.getRegister(Vreg, SPTy));
8929 SDValue Ops1[2] = { Value, Chain };
8930 return DAG.getMergeValues(Ops1, 2, dl);
8931 } else {
8932 SDValue Flag;
8933 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008934
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008935 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8936 Flag = Chain.getValue(1);
8937 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008938
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008939 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
8940 Flag = Chain.getValue(1);
8941
8942 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
8943
8944 SDValue Ops1[2] = { Chain.getValue(0), Chain };
8945 return DAG.getMergeValues(Ops1, 2, dl);
8946 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008947}
8948
Dan Gohmand858e902010-04-17 15:26:15 +00008949SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008950 MachineFunction &MF = DAG.getMachineFunction();
8951 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8952
Dan Gohman69de1932008-02-06 22:27:42 +00008953 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008954 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008955
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008956 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008957 // vastart just stores the address of the VarArgsFrameIndex slot into the
8958 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008959 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8960 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008961 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8962 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008963 }
8964
8965 // __va_list_tag:
8966 // gp_offset (0 - 6 * 8)
8967 // fp_offset (48 - 48 + 8 * 16)
8968 // overflow_arg_area (point to parameters coming in memory).
8969 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008970 SmallVector<SDValue, 8> MemOps;
8971 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008972 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008973 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008974 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8975 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008976 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008977 MemOps.push_back(Store);
8978
8979 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008980 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008981 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008982 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008983 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8984 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008985 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008986 MemOps.push_back(Store);
8987
8988 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008989 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008990 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008991 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8992 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008993 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8994 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008995 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008996 MemOps.push_back(Store);
8997
8998 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008999 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009000 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009001 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9002 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009003 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9004 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009005 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009006 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009007 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009008}
9009
Dan Gohmand858e902010-04-17 15:26:15 +00009010SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009011 assert(Subtarget->is64Bit() &&
9012 "LowerVAARG only handles 64-bit va_arg!");
9013 assert((Subtarget->isTargetLinux() ||
9014 Subtarget->isTargetDarwin()) &&
9015 "Unhandled target in LowerVAARG");
9016 assert(Op.getNode()->getNumOperands() == 4);
9017 SDValue Chain = Op.getOperand(0);
9018 SDValue SrcPtr = Op.getOperand(1);
9019 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9020 unsigned Align = Op.getConstantOperandVal(3);
9021 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009022
Dan Gohman320afb82010-10-12 18:00:49 +00009023 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009024 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009025 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9026 uint8_t ArgMode;
9027
9028 // Decide which area this value should be read from.
9029 // TODO: Implement the AMD64 ABI in its entirety. This simple
9030 // selection mechanism works only for the basic types.
9031 if (ArgVT == MVT::f80) {
9032 llvm_unreachable("va_arg for f80 not yet implemented");
9033 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9034 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9035 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9036 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9037 } else {
9038 llvm_unreachable("Unhandled argument type in LowerVAARG");
9039 }
9040
9041 if (ArgMode == 2) {
9042 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009043 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009044 !(DAG.getMachineFunction()
9045 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009046 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009047 }
9048
9049 // Insert VAARG_64 node into the DAG
9050 // VAARG_64 returns two values: Variable Argument Address, Chain
9051 SmallVector<SDValue, 11> InstOps;
9052 InstOps.push_back(Chain);
9053 InstOps.push_back(SrcPtr);
9054 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9055 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9056 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9057 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9058 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9059 VTs, &InstOps[0], InstOps.size(),
9060 MVT::i64,
9061 MachinePointerInfo(SV),
9062 /*Align=*/0,
9063 /*Volatile=*/false,
9064 /*ReadMem=*/true,
9065 /*WriteMem=*/true);
9066 Chain = VAARG.getValue(1);
9067
9068 // Load the next argument and return it
9069 return DAG.getLoad(ArgVT, dl,
9070 Chain,
9071 VAARG,
9072 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009073 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009074}
9075
Dan Gohmand858e902010-04-17 15:26:15 +00009076SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009077 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009078 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009079 SDValue Chain = Op.getOperand(0);
9080 SDValue DstPtr = Op.getOperand(1);
9081 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009082 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9083 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009084 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009085
Chris Lattnere72f2022010-09-21 05:40:29 +00009086 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009087 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009088 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009089 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009090}
9091
Craig Topper80e46362012-01-23 06:16:53 +00009092// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9093// may or may not be a constant. Takes immediate version of shift as input.
9094static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9095 SDValue SrcOp, SDValue ShAmt,
9096 SelectionDAG &DAG) {
9097 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9098
9099 if (isa<ConstantSDNode>(ShAmt)) {
9100 switch (Opc) {
9101 default: llvm_unreachable("Unknown target vector shift node");
9102 case X86ISD::VSHLI:
9103 case X86ISD::VSRLI:
9104 case X86ISD::VSRAI:
9105 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9106 }
9107 }
9108
9109 // Change opcode to non-immediate version
9110 switch (Opc) {
9111 default: llvm_unreachable("Unknown target vector shift node");
9112 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9113 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9114 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9115 }
9116
9117 // Need to build a vector containing shift amount
9118 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9119 SDValue ShOps[4];
9120 ShOps[0] = ShAmt;
9121 ShOps[1] = DAG.getConstant(0, MVT::i32);
9122 ShOps[2] = DAG.getUNDEF(MVT::i32);
9123 ShOps[3] = DAG.getUNDEF(MVT::i32);
9124 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
9125 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
9126 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9127}
9128
Dan Gohman475871a2008-07-27 21:46:04 +00009129SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009130X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009131 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009132 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009133 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009134 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009135 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009136 case Intrinsic::x86_sse_comieq_ss:
9137 case Intrinsic::x86_sse_comilt_ss:
9138 case Intrinsic::x86_sse_comile_ss:
9139 case Intrinsic::x86_sse_comigt_ss:
9140 case Intrinsic::x86_sse_comige_ss:
9141 case Intrinsic::x86_sse_comineq_ss:
9142 case Intrinsic::x86_sse_ucomieq_ss:
9143 case Intrinsic::x86_sse_ucomilt_ss:
9144 case Intrinsic::x86_sse_ucomile_ss:
9145 case Intrinsic::x86_sse_ucomigt_ss:
9146 case Intrinsic::x86_sse_ucomige_ss:
9147 case Intrinsic::x86_sse_ucomineq_ss:
9148 case Intrinsic::x86_sse2_comieq_sd:
9149 case Intrinsic::x86_sse2_comilt_sd:
9150 case Intrinsic::x86_sse2_comile_sd:
9151 case Intrinsic::x86_sse2_comigt_sd:
9152 case Intrinsic::x86_sse2_comige_sd:
9153 case Intrinsic::x86_sse2_comineq_sd:
9154 case Intrinsic::x86_sse2_ucomieq_sd:
9155 case Intrinsic::x86_sse2_ucomilt_sd:
9156 case Intrinsic::x86_sse2_ucomile_sd:
9157 case Intrinsic::x86_sse2_ucomigt_sd:
9158 case Intrinsic::x86_sse2_ucomige_sd:
9159 case Intrinsic::x86_sse2_ucomineq_sd: {
9160 unsigned Opc = 0;
9161 ISD::CondCode CC = ISD::SETCC_INVALID;
9162 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +00009163 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009164 case Intrinsic::x86_sse_comieq_ss:
9165 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009166 Opc = X86ISD::COMI;
9167 CC = ISD::SETEQ;
9168 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009169 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009170 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009171 Opc = X86ISD::COMI;
9172 CC = ISD::SETLT;
9173 break;
9174 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009175 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009176 Opc = X86ISD::COMI;
9177 CC = ISD::SETLE;
9178 break;
9179 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009180 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009181 Opc = X86ISD::COMI;
9182 CC = ISD::SETGT;
9183 break;
9184 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009185 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009186 Opc = X86ISD::COMI;
9187 CC = ISD::SETGE;
9188 break;
9189 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009190 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009191 Opc = X86ISD::COMI;
9192 CC = ISD::SETNE;
9193 break;
9194 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009195 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009196 Opc = X86ISD::UCOMI;
9197 CC = ISD::SETEQ;
9198 break;
9199 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009200 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009201 Opc = X86ISD::UCOMI;
9202 CC = ISD::SETLT;
9203 break;
9204 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009205 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009206 Opc = X86ISD::UCOMI;
9207 CC = ISD::SETLE;
9208 break;
9209 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009210 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009211 Opc = X86ISD::UCOMI;
9212 CC = ISD::SETGT;
9213 break;
9214 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009215 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009216 Opc = X86ISD::UCOMI;
9217 CC = ISD::SETGE;
9218 break;
9219 case Intrinsic::x86_sse_ucomineq_ss:
9220 case Intrinsic::x86_sse2_ucomineq_sd:
9221 Opc = X86ISD::UCOMI;
9222 CC = ISD::SETNE;
9223 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009224 }
Evan Cheng734503b2006-09-11 02:19:56 +00009225
Dan Gohman475871a2008-07-27 21:46:04 +00009226 SDValue LHS = Op.getOperand(1);
9227 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009228 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009229 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009230 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9231 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9232 DAG.getConstant(X86CC, MVT::i8), Cond);
9233 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009234 }
Craig Topper86c7c582012-01-30 01:10:15 +00009235 // XOP comparison intrinsics
9236 case Intrinsic::x86_xop_vpcomltb:
9237 case Intrinsic::x86_xop_vpcomltw:
9238 case Intrinsic::x86_xop_vpcomltd:
9239 case Intrinsic::x86_xop_vpcomltq:
9240 case Intrinsic::x86_xop_vpcomltub:
9241 case Intrinsic::x86_xop_vpcomltuw:
9242 case Intrinsic::x86_xop_vpcomltud:
9243 case Intrinsic::x86_xop_vpcomltuq:
9244 case Intrinsic::x86_xop_vpcomleb:
9245 case Intrinsic::x86_xop_vpcomlew:
9246 case Intrinsic::x86_xop_vpcomled:
9247 case Intrinsic::x86_xop_vpcomleq:
9248 case Intrinsic::x86_xop_vpcomleub:
9249 case Intrinsic::x86_xop_vpcomleuw:
9250 case Intrinsic::x86_xop_vpcomleud:
9251 case Intrinsic::x86_xop_vpcomleuq:
9252 case Intrinsic::x86_xop_vpcomgtb:
9253 case Intrinsic::x86_xop_vpcomgtw:
9254 case Intrinsic::x86_xop_vpcomgtd:
9255 case Intrinsic::x86_xop_vpcomgtq:
9256 case Intrinsic::x86_xop_vpcomgtub:
9257 case Intrinsic::x86_xop_vpcomgtuw:
9258 case Intrinsic::x86_xop_vpcomgtud:
9259 case Intrinsic::x86_xop_vpcomgtuq:
9260 case Intrinsic::x86_xop_vpcomgeb:
9261 case Intrinsic::x86_xop_vpcomgew:
9262 case Intrinsic::x86_xop_vpcomged:
9263 case Intrinsic::x86_xop_vpcomgeq:
9264 case Intrinsic::x86_xop_vpcomgeub:
9265 case Intrinsic::x86_xop_vpcomgeuw:
9266 case Intrinsic::x86_xop_vpcomgeud:
9267 case Intrinsic::x86_xop_vpcomgeuq:
9268 case Intrinsic::x86_xop_vpcomeqb:
9269 case Intrinsic::x86_xop_vpcomeqw:
9270 case Intrinsic::x86_xop_vpcomeqd:
9271 case Intrinsic::x86_xop_vpcomeqq:
9272 case Intrinsic::x86_xop_vpcomequb:
9273 case Intrinsic::x86_xop_vpcomequw:
9274 case Intrinsic::x86_xop_vpcomequd:
9275 case Intrinsic::x86_xop_vpcomequq:
9276 case Intrinsic::x86_xop_vpcomneb:
9277 case Intrinsic::x86_xop_vpcomnew:
9278 case Intrinsic::x86_xop_vpcomned:
9279 case Intrinsic::x86_xop_vpcomneq:
9280 case Intrinsic::x86_xop_vpcomneub:
9281 case Intrinsic::x86_xop_vpcomneuw:
9282 case Intrinsic::x86_xop_vpcomneud:
9283 case Intrinsic::x86_xop_vpcomneuq:
9284 case Intrinsic::x86_xop_vpcomfalseb:
9285 case Intrinsic::x86_xop_vpcomfalsew:
9286 case Intrinsic::x86_xop_vpcomfalsed:
9287 case Intrinsic::x86_xop_vpcomfalseq:
9288 case Intrinsic::x86_xop_vpcomfalseub:
9289 case Intrinsic::x86_xop_vpcomfalseuw:
9290 case Intrinsic::x86_xop_vpcomfalseud:
9291 case Intrinsic::x86_xop_vpcomfalseuq:
9292 case Intrinsic::x86_xop_vpcomtrueb:
9293 case Intrinsic::x86_xop_vpcomtruew:
9294 case Intrinsic::x86_xop_vpcomtrued:
9295 case Intrinsic::x86_xop_vpcomtrueq:
9296 case Intrinsic::x86_xop_vpcomtrueub:
9297 case Intrinsic::x86_xop_vpcomtrueuw:
9298 case Intrinsic::x86_xop_vpcomtrueud:
9299 case Intrinsic::x86_xop_vpcomtrueuq: {
9300 unsigned CC = 0;
9301 unsigned Opc = 0;
9302
9303 switch (IntNo) {
9304 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9305 case Intrinsic::x86_xop_vpcomltb:
9306 case Intrinsic::x86_xop_vpcomltw:
9307 case Intrinsic::x86_xop_vpcomltd:
9308 case Intrinsic::x86_xop_vpcomltq:
9309 CC = 0;
9310 Opc = X86ISD::VPCOM;
9311 break;
9312 case Intrinsic::x86_xop_vpcomltub:
9313 case Intrinsic::x86_xop_vpcomltuw:
9314 case Intrinsic::x86_xop_vpcomltud:
9315 case Intrinsic::x86_xop_vpcomltuq:
9316 CC = 0;
9317 Opc = X86ISD::VPCOMU;
9318 break;
9319 case Intrinsic::x86_xop_vpcomleb:
9320 case Intrinsic::x86_xop_vpcomlew:
9321 case Intrinsic::x86_xop_vpcomled:
9322 case Intrinsic::x86_xop_vpcomleq:
9323 CC = 1;
9324 Opc = X86ISD::VPCOM;
9325 break;
9326 case Intrinsic::x86_xop_vpcomleub:
9327 case Intrinsic::x86_xop_vpcomleuw:
9328 case Intrinsic::x86_xop_vpcomleud:
9329 case Intrinsic::x86_xop_vpcomleuq:
9330 CC = 1;
9331 Opc = X86ISD::VPCOMU;
9332 break;
9333 case Intrinsic::x86_xop_vpcomgtb:
9334 case Intrinsic::x86_xop_vpcomgtw:
9335 case Intrinsic::x86_xop_vpcomgtd:
9336 case Intrinsic::x86_xop_vpcomgtq:
9337 CC = 2;
9338 Opc = X86ISD::VPCOM;
9339 break;
9340 case Intrinsic::x86_xop_vpcomgtub:
9341 case Intrinsic::x86_xop_vpcomgtuw:
9342 case Intrinsic::x86_xop_vpcomgtud:
9343 case Intrinsic::x86_xop_vpcomgtuq:
9344 CC = 2;
9345 Opc = X86ISD::VPCOMU;
9346 break;
9347 case Intrinsic::x86_xop_vpcomgeb:
9348 case Intrinsic::x86_xop_vpcomgew:
9349 case Intrinsic::x86_xop_vpcomged:
9350 case Intrinsic::x86_xop_vpcomgeq:
9351 CC = 3;
9352 Opc = X86ISD::VPCOM;
9353 break;
9354 case Intrinsic::x86_xop_vpcomgeub:
9355 case Intrinsic::x86_xop_vpcomgeuw:
9356 case Intrinsic::x86_xop_vpcomgeud:
9357 case Intrinsic::x86_xop_vpcomgeuq:
9358 CC = 3;
9359 Opc = X86ISD::VPCOMU;
9360 break;
9361 case Intrinsic::x86_xop_vpcomeqb:
9362 case Intrinsic::x86_xop_vpcomeqw:
9363 case Intrinsic::x86_xop_vpcomeqd:
9364 case Intrinsic::x86_xop_vpcomeqq:
9365 CC = 4;
9366 Opc = X86ISD::VPCOM;
9367 break;
9368 case Intrinsic::x86_xop_vpcomequb:
9369 case Intrinsic::x86_xop_vpcomequw:
9370 case Intrinsic::x86_xop_vpcomequd:
9371 case Intrinsic::x86_xop_vpcomequq:
9372 CC = 4;
9373 Opc = X86ISD::VPCOMU;
9374 break;
9375 case Intrinsic::x86_xop_vpcomneb:
9376 case Intrinsic::x86_xop_vpcomnew:
9377 case Intrinsic::x86_xop_vpcomned:
9378 case Intrinsic::x86_xop_vpcomneq:
9379 CC = 5;
9380 Opc = X86ISD::VPCOM;
9381 break;
9382 case Intrinsic::x86_xop_vpcomneub:
9383 case Intrinsic::x86_xop_vpcomneuw:
9384 case Intrinsic::x86_xop_vpcomneud:
9385 case Intrinsic::x86_xop_vpcomneuq:
9386 CC = 5;
9387 Opc = X86ISD::VPCOMU;
9388 break;
9389 case Intrinsic::x86_xop_vpcomfalseb:
9390 case Intrinsic::x86_xop_vpcomfalsew:
9391 case Intrinsic::x86_xop_vpcomfalsed:
9392 case Intrinsic::x86_xop_vpcomfalseq:
9393 CC = 6;
9394 Opc = X86ISD::VPCOM;
9395 break;
9396 case Intrinsic::x86_xop_vpcomfalseub:
9397 case Intrinsic::x86_xop_vpcomfalseuw:
9398 case Intrinsic::x86_xop_vpcomfalseud:
9399 case Intrinsic::x86_xop_vpcomfalseuq:
9400 CC = 6;
9401 Opc = X86ISD::VPCOMU;
9402 break;
9403 case Intrinsic::x86_xop_vpcomtrueb:
9404 case Intrinsic::x86_xop_vpcomtruew:
9405 case Intrinsic::x86_xop_vpcomtrued:
9406 case Intrinsic::x86_xop_vpcomtrueq:
9407 CC = 7;
9408 Opc = X86ISD::VPCOM;
9409 break;
9410 case Intrinsic::x86_xop_vpcomtrueub:
9411 case Intrinsic::x86_xop_vpcomtrueuw:
9412 case Intrinsic::x86_xop_vpcomtrueud:
9413 case Intrinsic::x86_xop_vpcomtrueuq:
9414 CC = 7;
9415 Opc = X86ISD::VPCOMU;
9416 break;
9417 }
9418
9419 SDValue LHS = Op.getOperand(1);
9420 SDValue RHS = Op.getOperand(2);
9421 return DAG.getNode(Opc, dl, Op.getValueType(), LHS, RHS,
9422 DAG.getConstant(CC, MVT::i8));
9423 }
9424
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009425 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +00009426 case Intrinsic::x86_sse2_pmulu_dq:
9427 case Intrinsic::x86_avx2_pmulu_dq:
9428 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
9429 Op.getOperand(1), Op.getOperand(2));
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009430 case Intrinsic::x86_sse3_hadd_ps:
9431 case Intrinsic::x86_sse3_hadd_pd:
9432 case Intrinsic::x86_avx_hadd_ps_256:
9433 case Intrinsic::x86_avx_hadd_pd_256:
9434 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9435 Op.getOperand(1), Op.getOperand(2));
9436 case Intrinsic::x86_sse3_hsub_ps:
9437 case Intrinsic::x86_sse3_hsub_pd:
9438 case Intrinsic::x86_avx_hsub_ps_256:
9439 case Intrinsic::x86_avx_hsub_pd_256:
9440 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9441 Op.getOperand(1), Op.getOperand(2));
Craig Topper4bb3f342012-01-25 05:37:32 +00009442 case Intrinsic::x86_ssse3_phadd_w_128:
9443 case Intrinsic::x86_ssse3_phadd_d_128:
9444 case Intrinsic::x86_avx2_phadd_w:
9445 case Intrinsic::x86_avx2_phadd_d:
9446 return DAG.getNode(X86ISD::HADD, dl, Op.getValueType(),
9447 Op.getOperand(1), Op.getOperand(2));
9448 case Intrinsic::x86_ssse3_phsub_w_128:
9449 case Intrinsic::x86_ssse3_phsub_d_128:
9450 case Intrinsic::x86_avx2_phsub_w:
9451 case Intrinsic::x86_avx2_phsub_d:
9452 return DAG.getNode(X86ISD::HSUB, dl, Op.getValueType(),
9453 Op.getOperand(1), Op.getOperand(2));
Craig Topper98fc7292011-11-19 17:46:46 +00009454 case Intrinsic::x86_avx2_psllv_d:
9455 case Intrinsic::x86_avx2_psllv_q:
9456 case Intrinsic::x86_avx2_psllv_d_256:
9457 case Intrinsic::x86_avx2_psllv_q_256:
9458 return DAG.getNode(ISD::SHL, dl, Op.getValueType(),
9459 Op.getOperand(1), Op.getOperand(2));
9460 case Intrinsic::x86_avx2_psrlv_d:
9461 case Intrinsic::x86_avx2_psrlv_q:
9462 case Intrinsic::x86_avx2_psrlv_d_256:
9463 case Intrinsic::x86_avx2_psrlv_q_256:
9464 return DAG.getNode(ISD::SRL, dl, Op.getValueType(),
9465 Op.getOperand(1), Op.getOperand(2));
9466 case Intrinsic::x86_avx2_psrav_d:
9467 case Intrinsic::x86_avx2_psrav_d_256:
9468 return DAG.getNode(ISD::SRA, dl, Op.getValueType(),
9469 Op.getOperand(1), Op.getOperand(2));
Craig Topper969ba282012-01-25 06:43:11 +00009470 case Intrinsic::x86_ssse3_pshuf_b_128:
9471 case Intrinsic::x86_avx2_pshuf_b:
9472 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
9473 Op.getOperand(1), Op.getOperand(2));
9474 case Intrinsic::x86_ssse3_psign_b_128:
9475 case Intrinsic::x86_ssse3_psign_w_128:
9476 case Intrinsic::x86_ssse3_psign_d_128:
9477 case Intrinsic::x86_avx2_psign_b:
9478 case Intrinsic::x86_avx2_psign_w:
9479 case Intrinsic::x86_avx2_psign_d:
9480 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
9481 Op.getOperand(1), Op.getOperand(2));
Craig Toppere566cd02012-01-26 07:18:03 +00009482 case Intrinsic::x86_sse41_insertps:
9483 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
9484 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
9485 case Intrinsic::x86_avx_vperm2f128_ps_256:
9486 case Intrinsic::x86_avx_vperm2f128_pd_256:
9487 case Intrinsic::x86_avx_vperm2f128_si_256:
9488 case Intrinsic::x86_avx2_vperm2i128:
9489 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
9490 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper5a313bb2012-02-08 06:36:57 +00009491 case Intrinsic::x86_avx_vpermil_ps:
9492 case Intrinsic::x86_avx_vpermil_pd:
9493 case Intrinsic::x86_avx_vpermil_ps_256:
9494 case Intrinsic::x86_avx_vpermil_pd_256:
9495 return DAG.getNode(X86ISD::VPERMILP, dl, Op.getValueType(),
9496 Op.getOperand(1), Op.getOperand(2));
Craig Topper98fc7292011-11-19 17:46:46 +00009497
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009498 // ptest and testp intrinsics. The intrinsic these come from are designed to
9499 // return an integer value, not just an instruction so lower it to the ptest
9500 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009501 case Intrinsic::x86_sse41_ptestz:
9502 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009503 case Intrinsic::x86_sse41_ptestnzc:
9504 case Intrinsic::x86_avx_ptestz_256:
9505 case Intrinsic::x86_avx_ptestc_256:
9506 case Intrinsic::x86_avx_ptestnzc_256:
9507 case Intrinsic::x86_avx_vtestz_ps:
9508 case Intrinsic::x86_avx_vtestc_ps:
9509 case Intrinsic::x86_avx_vtestnzc_ps:
9510 case Intrinsic::x86_avx_vtestz_pd:
9511 case Intrinsic::x86_avx_vtestc_pd:
9512 case Intrinsic::x86_avx_vtestnzc_pd:
9513 case Intrinsic::x86_avx_vtestz_ps_256:
9514 case Intrinsic::x86_avx_vtestc_ps_256:
9515 case Intrinsic::x86_avx_vtestnzc_ps_256:
9516 case Intrinsic::x86_avx_vtestz_pd_256:
9517 case Intrinsic::x86_avx_vtestc_pd_256:
9518 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9519 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009520 unsigned X86CC = 0;
9521 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009522 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009523 case Intrinsic::x86_avx_vtestz_ps:
9524 case Intrinsic::x86_avx_vtestz_pd:
9525 case Intrinsic::x86_avx_vtestz_ps_256:
9526 case Intrinsic::x86_avx_vtestz_pd_256:
9527 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009528 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009529 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009530 // ZF = 1
9531 X86CC = X86::COND_E;
9532 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009533 case Intrinsic::x86_avx_vtestc_ps:
9534 case Intrinsic::x86_avx_vtestc_pd:
9535 case Intrinsic::x86_avx_vtestc_ps_256:
9536 case Intrinsic::x86_avx_vtestc_pd_256:
9537 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009538 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009539 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009540 // CF = 1
9541 X86CC = X86::COND_B;
9542 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009543 case Intrinsic::x86_avx_vtestnzc_ps:
9544 case Intrinsic::x86_avx_vtestnzc_pd:
9545 case Intrinsic::x86_avx_vtestnzc_ps_256:
9546 case Intrinsic::x86_avx_vtestnzc_pd_256:
9547 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009548 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009549 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009550 // ZF and CF = 0
9551 X86CC = X86::COND_A;
9552 break;
9553 }
Eric Christopherfd179292009-08-27 18:07:15 +00009554
Eric Christopher71c67532009-07-29 00:28:05 +00009555 SDValue LHS = Op.getOperand(1);
9556 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009557 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9558 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009559 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9560 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9561 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009562 }
Evan Cheng5759f972008-05-04 09:15:50 +00009563
Craig Topper80e46362012-01-23 06:16:53 +00009564 // SSE/AVX shift intrinsics
9565 case Intrinsic::x86_sse2_psll_w:
9566 case Intrinsic::x86_sse2_psll_d:
9567 case Intrinsic::x86_sse2_psll_q:
9568 case Intrinsic::x86_avx2_psll_w:
9569 case Intrinsic::x86_avx2_psll_d:
9570 case Intrinsic::x86_avx2_psll_q:
9571 return DAG.getNode(X86ISD::VSHL, dl, Op.getValueType(),
9572 Op.getOperand(1), Op.getOperand(2));
9573 case Intrinsic::x86_sse2_psrl_w:
9574 case Intrinsic::x86_sse2_psrl_d:
9575 case Intrinsic::x86_sse2_psrl_q:
9576 case Intrinsic::x86_avx2_psrl_w:
9577 case Intrinsic::x86_avx2_psrl_d:
9578 case Intrinsic::x86_avx2_psrl_q:
9579 return DAG.getNode(X86ISD::VSRL, dl, Op.getValueType(),
9580 Op.getOperand(1), Op.getOperand(2));
9581 case Intrinsic::x86_sse2_psra_w:
9582 case Intrinsic::x86_sse2_psra_d:
9583 case Intrinsic::x86_avx2_psra_w:
9584 case Intrinsic::x86_avx2_psra_d:
9585 return DAG.getNode(X86ISD::VSRA, dl, Op.getValueType(),
9586 Op.getOperand(1), Op.getOperand(2));
Evan Cheng5759f972008-05-04 09:15:50 +00009587 case Intrinsic::x86_sse2_pslli_w:
9588 case Intrinsic::x86_sse2_pslli_d:
9589 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009590 case Intrinsic::x86_avx2_pslli_w:
9591 case Intrinsic::x86_avx2_pslli_d:
9592 case Intrinsic::x86_avx2_pslli_q:
9593 return getTargetVShiftNode(X86ISD::VSHLI, dl, Op.getValueType(),
9594 Op.getOperand(1), Op.getOperand(2), DAG);
Evan Cheng5759f972008-05-04 09:15:50 +00009595 case Intrinsic::x86_sse2_psrli_w:
9596 case Intrinsic::x86_sse2_psrli_d:
9597 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009598 case Intrinsic::x86_avx2_psrli_w:
9599 case Intrinsic::x86_avx2_psrli_d:
9600 case Intrinsic::x86_avx2_psrli_q:
9601 return getTargetVShiftNode(X86ISD::VSRLI, dl, Op.getValueType(),
9602 Op.getOperand(1), Op.getOperand(2), DAG);
Evan Cheng5759f972008-05-04 09:15:50 +00009603 case Intrinsic::x86_sse2_psrai_w:
9604 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +00009605 case Intrinsic::x86_avx2_psrai_w:
9606 case Intrinsic::x86_avx2_psrai_d:
9607 return getTargetVShiftNode(X86ISD::VSRAI, dl, Op.getValueType(),
9608 Op.getOperand(1), Op.getOperand(2), DAG);
9609 // Fix vector shift instructions where the last operand is a non-immediate
9610 // i32 value.
Evan Cheng5759f972008-05-04 09:15:50 +00009611 case Intrinsic::x86_mmx_pslli_w:
9612 case Intrinsic::x86_mmx_pslli_d:
9613 case Intrinsic::x86_mmx_pslli_q:
9614 case Intrinsic::x86_mmx_psrli_w:
9615 case Intrinsic::x86_mmx_psrli_d:
9616 case Intrinsic::x86_mmx_psrli_q:
9617 case Intrinsic::x86_mmx_psrai_w:
9618 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009619 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009620 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009621 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009622
9623 unsigned NewIntNo = 0;
Evan Cheng5759f972008-05-04 09:15:50 +00009624 switch (IntNo) {
Craig Topper80e46362012-01-23 06:16:53 +00009625 case Intrinsic::x86_mmx_pslli_w:
9626 NewIntNo = Intrinsic::x86_mmx_psll_w;
Evan Cheng5759f972008-05-04 09:15:50 +00009627 break;
Craig Topper80e46362012-01-23 06:16:53 +00009628 case Intrinsic::x86_mmx_pslli_d:
9629 NewIntNo = Intrinsic::x86_mmx_psll_d;
Evan Cheng5759f972008-05-04 09:15:50 +00009630 break;
Craig Topper80e46362012-01-23 06:16:53 +00009631 case Intrinsic::x86_mmx_pslli_q:
9632 NewIntNo = Intrinsic::x86_mmx_psll_q;
Evan Cheng5759f972008-05-04 09:15:50 +00009633 break;
Craig Topper80e46362012-01-23 06:16:53 +00009634 case Intrinsic::x86_mmx_psrli_w:
9635 NewIntNo = Intrinsic::x86_mmx_psrl_w;
Evan Cheng5759f972008-05-04 09:15:50 +00009636 break;
Craig Topper80e46362012-01-23 06:16:53 +00009637 case Intrinsic::x86_mmx_psrli_d:
9638 NewIntNo = Intrinsic::x86_mmx_psrl_d;
Evan Cheng5759f972008-05-04 09:15:50 +00009639 break;
Craig Topper80e46362012-01-23 06:16:53 +00009640 case Intrinsic::x86_mmx_psrli_q:
9641 NewIntNo = Intrinsic::x86_mmx_psrl_q;
Evan Cheng5759f972008-05-04 09:15:50 +00009642 break;
Craig Topper80e46362012-01-23 06:16:53 +00009643 case Intrinsic::x86_mmx_psrai_w:
9644 NewIntNo = Intrinsic::x86_mmx_psra_w;
Evan Cheng5759f972008-05-04 09:15:50 +00009645 break;
Craig Topper80e46362012-01-23 06:16:53 +00009646 case Intrinsic::x86_mmx_psrai_d:
9647 NewIntNo = Intrinsic::x86_mmx_psra_d;
Evan Cheng5759f972008-05-04 09:15:50 +00009648 break;
Craig Topper80e46362012-01-23 06:16:53 +00009649 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009650 }
Mon P Wangefa42202009-09-03 19:56:25 +00009651
9652 // The vector shift intrinsics with scalars uses 32b shift amounts but
9653 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9654 // to be zero.
Craig Topper80e46362012-01-23 06:16:53 +00009655 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, ShAmt,
9656 DAG.getConstant(0, MVT::i32));
Dale Johannesen0488fb62010-09-30 23:57:10 +00009657// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009658
Owen Andersone50ed302009-08-10 22:56:29 +00009659 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009660 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009661 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009662 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009663 Op.getOperand(1), ShAmt);
9664 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009665 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009666}
Evan Cheng72261582005-12-20 06:22:03 +00009667
Dan Gohmand858e902010-04-17 15:26:15 +00009668SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9669 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009670 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9671 MFI->setReturnAddressIsTaken(true);
9672
Bill Wendling64e87322009-01-16 19:25:27 +00009673 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009674 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009675
9676 if (Depth > 0) {
9677 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9678 SDValue Offset =
9679 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009680 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009681 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009682 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009683 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009684 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009685 }
9686
9687 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009688 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009689 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009690 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009691}
9692
Dan Gohmand858e902010-04-17 15:26:15 +00009693SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009694 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9695 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009696
Owen Andersone50ed302009-08-10 22:56:29 +00009697 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009698 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009699 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9700 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009701 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009702 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009703 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9704 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009705 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009706 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009707}
9708
Dan Gohman475871a2008-07-27 21:46:04 +00009709SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009710 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009711 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009712}
9713
Dan Gohmand858e902010-04-17 15:26:15 +00009714SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009715 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009716 SDValue Chain = Op.getOperand(0);
9717 SDValue Offset = Op.getOperand(1);
9718 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009719 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009720
Dan Gohmand8816272010-08-11 18:14:00 +00009721 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9722 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9723 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009724 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009725
Dan Gohmand8816272010-08-11 18:14:00 +00009726 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9727 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009728 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009729 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9730 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009731 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009732 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009733
Dale Johannesene4d209d2009-02-03 20:21:25 +00009734 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009735 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009736 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009737}
9738
Duncan Sands4a544a72011-09-06 13:37:06 +00009739SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9740 SelectionDAG &DAG) const {
9741 return Op.getOperand(0);
9742}
9743
9744SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9745 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009746 SDValue Root = Op.getOperand(0);
9747 SDValue Trmp = Op.getOperand(1); // trampoline
9748 SDValue FPtr = Op.getOperand(2); // nested function
9749 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009750 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009751
Dan Gohman69de1932008-02-06 22:27:42 +00009752 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009753
9754 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009755 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009756
9757 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009758 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9759 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009760
Evan Cheng0e6a0522011-07-18 20:57:22 +00009761 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9762 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009763
9764 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9765
9766 // Load the pointer to the nested function into R11.
9767 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009768 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009769 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009770 Addr, MachinePointerInfo(TrmpAddr),
9771 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009772
Owen Anderson825b72b2009-08-11 20:47:22 +00009773 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9774 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009775 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9776 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009777 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009778
9779 // Load the 'nest' parameter value into R10.
9780 // R10 is specified in X86CallingConv.td
9781 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009782 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9783 DAG.getConstant(10, MVT::i64));
9784 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009785 Addr, MachinePointerInfo(TrmpAddr, 10),
9786 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009787
Owen Anderson825b72b2009-08-11 20:47:22 +00009788 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9789 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009790 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9791 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009792 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009793
9794 // Jump to the nested function.
9795 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009796 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9797 DAG.getConstant(20, MVT::i64));
9798 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009799 Addr, MachinePointerInfo(TrmpAddr, 20),
9800 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009801
9802 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009803 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9804 DAG.getConstant(22, MVT::i64));
9805 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009806 MachinePointerInfo(TrmpAddr, 22),
9807 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009808
Duncan Sands4a544a72011-09-06 13:37:06 +00009809 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009810 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009811 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009812 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009813 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009814 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009815
9816 switch (CC) {
9817 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009818 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009819 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009820 case CallingConv::X86_StdCall: {
9821 // Pass 'nest' parameter in ECX.
9822 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009823 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009824
9825 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009826 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009827 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009828
Chris Lattner58d74912008-03-12 17:45:29 +00009829 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009830 unsigned InRegCount = 0;
9831 unsigned Idx = 1;
9832
9833 for (FunctionType::param_iterator I = FTy->param_begin(),
9834 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009835 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009836 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009837 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009838
9839 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009840 report_fatal_error("Nest register in use - reduce number of inreg"
9841 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009842 }
9843 }
9844 break;
9845 }
9846 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009847 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009848 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009849 // Pass 'nest' parameter in EAX.
9850 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009851 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009852 break;
9853 }
9854
Dan Gohman475871a2008-07-27 21:46:04 +00009855 SDValue OutChains[4];
9856 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009857
Owen Anderson825b72b2009-08-11 20:47:22 +00009858 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9859 DAG.getConstant(10, MVT::i32));
9860 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009861
Chris Lattnera62fe662010-02-05 19:20:30 +00009862 // This is storing the opcode for MOV32ri.
9863 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009864 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009865 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009866 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009867 Trmp, MachinePointerInfo(TrmpAddr),
9868 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009869
Owen Anderson825b72b2009-08-11 20:47:22 +00009870 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9871 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009872 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9873 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009874 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009875
Chris Lattnera62fe662010-02-05 19:20:30 +00009876 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009877 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9878 DAG.getConstant(5, MVT::i32));
9879 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009880 MachinePointerInfo(TrmpAddr, 5),
9881 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009882
Owen Anderson825b72b2009-08-11 20:47:22 +00009883 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9884 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009885 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9886 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009887 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009888
Duncan Sands4a544a72011-09-06 13:37:06 +00009889 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009890 }
9891}
9892
Dan Gohmand858e902010-04-17 15:26:15 +00009893SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9894 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009895 /*
9896 The rounding mode is in bits 11:10 of FPSR, and has the following
9897 settings:
9898 00 Round to nearest
9899 01 Round to -inf
9900 10 Round to +inf
9901 11 Round to 0
9902
9903 FLT_ROUNDS, on the other hand, expects the following:
9904 -1 Undefined
9905 0 Round to 0
9906 1 Round to nearest
9907 2 Round to +inf
9908 3 Round to -inf
9909
9910 To perform the conversion, we do:
9911 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9912 */
9913
9914 MachineFunction &MF = DAG.getMachineFunction();
9915 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009916 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009917 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009918 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009919 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009920
9921 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009922 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009923 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009924
Michael J. Spencerec38de22010-10-10 22:04:20 +00009925
Chris Lattner2156b792010-09-22 01:11:26 +00009926 MachineMemOperand *MMO =
9927 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9928 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009929
Chris Lattner2156b792010-09-22 01:11:26 +00009930 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9931 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9932 DAG.getVTList(MVT::Other),
9933 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009934
9935 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009936 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009937 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009938
9939 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009940 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009941 DAG.getNode(ISD::SRL, DL, MVT::i16,
9942 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009943 CWD, DAG.getConstant(0x800, MVT::i16)),
9944 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009945 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009946 DAG.getNode(ISD::SRL, DL, MVT::i16,
9947 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009948 CWD, DAG.getConstant(0x400, MVT::i16)),
9949 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009950
Dan Gohman475871a2008-07-27 21:46:04 +00009951 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009952 DAG.getNode(ISD::AND, DL, MVT::i16,
9953 DAG.getNode(ISD::ADD, DL, MVT::i16,
9954 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009955 DAG.getConstant(1, MVT::i16)),
9956 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009957
9958
Duncan Sands83ec4b62008-06-06 12:08:01 +00009959 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009960 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009961}
9962
Dan Gohmand858e902010-04-17 15:26:15 +00009963SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009964 EVT VT = Op.getValueType();
9965 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009966 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009967 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009968
9969 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009970 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009971 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009972 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009973 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009974 }
Evan Cheng18efe262007-12-14 02:13:44 +00009975
Evan Cheng152804e2007-12-14 08:30:15 +00009976 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009977 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009978 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009979
9980 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009981 SDValue Ops[] = {
9982 Op,
9983 DAG.getConstant(NumBits+NumBits-1, OpVT),
9984 DAG.getConstant(X86::COND_E, MVT::i8),
9985 Op.getValue(1)
9986 };
9987 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009988
9989 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009990 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009991
Owen Anderson825b72b2009-08-11 20:47:22 +00009992 if (VT == MVT::i8)
9993 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009994 return Op;
9995}
9996
Chandler Carruthacc068e2011-12-24 10:55:54 +00009997SDValue X86TargetLowering::LowerCTLZ_ZERO_UNDEF(SDValue Op,
9998 SelectionDAG &DAG) const {
9999 EVT VT = Op.getValueType();
10000 EVT OpVT = VT;
10001 unsigned NumBits = VT.getSizeInBits();
10002 DebugLoc dl = Op.getDebugLoc();
10003
10004 Op = Op.getOperand(0);
10005 if (VT == MVT::i8) {
10006 // Zero extend to i32 since there is not an i8 bsr.
10007 OpVT = MVT::i32;
10008 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10009 }
10010
10011 // Issue a bsr (scan bits in reverse).
10012 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10013 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10014
10015 // And xor with NumBits-1.
10016 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10017
10018 if (VT == MVT::i8)
10019 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10020 return Op;
10021}
10022
Dan Gohmand858e902010-04-17 15:26:15 +000010023SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010024 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010025 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010026 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010027 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010028
10029 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010030 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010031 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010032
10033 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010034 SDValue Ops[] = {
10035 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010036 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010037 DAG.getConstant(X86::COND_E, MVT::i8),
10038 Op.getValue(1)
10039 };
Chandler Carruth77821022011-12-24 12:12:34 +000010040 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010041}
10042
Craig Topper13894fa2011-08-24 06:14:18 +000010043// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10044// ones, and then concatenate the result back.
10045static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010046 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010047
10048 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
10049 "Unsupported value type for operation");
10050
10051 int NumElems = VT.getVectorNumElements();
10052 DebugLoc dl = Op.getDebugLoc();
10053 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10054 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10055
10056 // Extract the LHS vectors
10057 SDValue LHS = Op.getOperand(0);
10058 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10059 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10060
10061 // Extract the RHS vectors
10062 SDValue RHS = Op.getOperand(1);
10063 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
10064 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
10065
10066 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10067 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10068
10069 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10070 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10071 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10072}
10073
10074SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
10075 assert(Op.getValueType().getSizeInBits() == 256 &&
10076 Op.getValueType().isInteger() &&
10077 "Only handle AVX 256-bit vector integer operation");
10078 return Lower256IntArith(Op, DAG);
10079}
10080
10081SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
10082 assert(Op.getValueType().getSizeInBits() == 256 &&
10083 Op.getValueType().isInteger() &&
10084 "Only handle AVX 256-bit vector integer operation");
10085 return Lower256IntArith(Op, DAG);
10086}
10087
10088SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10089 EVT VT = Op.getValueType();
10090
10091 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +000010092 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010093 return Lower256IntArith(Op, DAG);
10094
Craig Topper5b209e82012-02-05 03:14:49 +000010095 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10096 "Only know how to lower V2I64/V4I64 multiply");
10097
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010098 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010099
Craig Topper5b209e82012-02-05 03:14:49 +000010100 // Ahi = psrlqi(a, 32);
10101 // Bhi = psrlqi(b, 32);
10102 //
10103 // AloBlo = pmuludq(a, b);
10104 // AloBhi = pmuludq(a, Bhi);
10105 // AhiBlo = pmuludq(Ahi, b);
10106
10107 // AloBhi = psllqi(AloBhi, 32);
10108 // AhiBlo = psllqi(AhiBlo, 32);
10109 // return AloBlo + AloBhi + AhiBlo;
10110
Craig Topperaaa643c2011-11-09 07:28:55 +000010111 SDValue A = Op.getOperand(0);
10112 SDValue B = Op.getOperand(1);
10113
Craig Topper5b209e82012-02-05 03:14:49 +000010114 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010115
Craig Topper5b209e82012-02-05 03:14:49 +000010116 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
10117 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000010118
Craig Topper5b209e82012-02-05 03:14:49 +000010119 // Bit cast to 32-bit vectors for MULUDQ
10120 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
10121 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
10122 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
10123 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
10124 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000010125
Craig Topper5b209e82012-02-05 03:14:49 +000010126 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
10127 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
10128 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000010129
Craig Topper5b209e82012-02-05 03:14:49 +000010130 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
10131 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010132
Dale Johannesene4d209d2009-02-03 20:21:25 +000010133 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000010134 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010135}
10136
Nadav Rotem43012222011-05-11 08:12:09 +000010137SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10138
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010139 EVT VT = Op.getValueType();
10140 DebugLoc dl = Op.getDebugLoc();
10141 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010142 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010143 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010144
Craig Topper1accb7e2012-01-10 06:54:16 +000010145 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010146 return SDValue();
10147
Nadav Rotem43012222011-05-11 08:12:09 +000010148 // Optimize shl/srl/sra with constant shift amount.
10149 if (isSplatVector(Amt.getNode())) {
10150 SDValue SclrAmt = Amt->getOperand(0);
10151 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10152 uint64_t ShiftAmt = C->getZExtValue();
10153
Craig Toppered2e13d2012-01-22 19:15:14 +000010154 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10155 (Subtarget->hasAVX2() &&
10156 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10157 if (Op.getOpcode() == ISD::SHL)
10158 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10159 DAG.getConstant(ShiftAmt, MVT::i32));
10160 if (Op.getOpcode() == ISD::SRL)
10161 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10162 DAG.getConstant(ShiftAmt, MVT::i32));
10163 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10164 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10165 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010166 }
10167
Craig Toppered2e13d2012-01-22 19:15:14 +000010168 if (VT == MVT::v16i8) {
10169 if (Op.getOpcode() == ISD::SHL) {
10170 // Make a large shift.
10171 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10172 DAG.getConstant(ShiftAmt, MVT::i32));
10173 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10174 // Zero out the rightmost bits.
10175 SmallVector<SDValue, 16> V(16,
10176 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10177 MVT::i8));
10178 return DAG.getNode(ISD::AND, dl, VT, SHL,
10179 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010180 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010181 if (Op.getOpcode() == ISD::SRL) {
10182 // Make a large shift.
10183 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10184 DAG.getConstant(ShiftAmt, MVT::i32));
10185 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10186 // Zero out the leftmost bits.
10187 SmallVector<SDValue, 16> V(16,
10188 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10189 MVT::i8));
10190 return DAG.getNode(ISD::AND, dl, VT, SRL,
10191 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10192 }
10193 if (Op.getOpcode() == ISD::SRA) {
10194 if (ShiftAmt == 7) {
10195 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010196 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010197 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010198 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010199
Craig Toppered2e13d2012-01-22 19:15:14 +000010200 // R s>> a === ((R u>> a) ^ m) - m
10201 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10202 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10203 MVT::i8));
10204 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10205 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10206 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10207 return Res;
10208 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010209 }
Craig Topper46154eb2011-11-11 07:39:23 +000010210
Craig Topper0d86d462011-11-20 00:12:05 +000010211 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10212 if (Op.getOpcode() == ISD::SHL) {
10213 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010214 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10215 DAG.getConstant(ShiftAmt, MVT::i32));
10216 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010217 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010218 SmallVector<SDValue, 32> V(32,
10219 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10220 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010221 return DAG.getNode(ISD::AND, dl, VT, SHL,
10222 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010223 }
Craig Topper0d86d462011-11-20 00:12:05 +000010224 if (Op.getOpcode() == ISD::SRL) {
10225 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010226 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10227 DAG.getConstant(ShiftAmt, MVT::i32));
10228 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010229 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010230 SmallVector<SDValue, 32> V(32,
10231 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10232 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010233 return DAG.getNode(ISD::AND, dl, VT, SRL,
10234 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10235 }
10236 if (Op.getOpcode() == ISD::SRA) {
10237 if (ShiftAmt == 7) {
10238 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010239 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010240 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010241 }
10242
10243 // R s>> a === ((R u>> a) ^ m) - m
10244 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10245 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10246 MVT::i8));
10247 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10248 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10249 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10250 return Res;
10251 }
10252 }
Nadav Rotem43012222011-05-11 08:12:09 +000010253 }
10254 }
10255
10256 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010257 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010258 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
10259 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000010260
Chris Lattner7302d802012-02-06 21:56:39 +000010261 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
10262 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000010263 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10264 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010265 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010266 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010267
10268 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010269 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010270 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10271 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10272 }
Nadav Rotem43012222011-05-11 08:12:09 +000010273 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000010274 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000010275
Nate Begeman51409212010-07-28 00:21:48 +000010276 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000010277 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
10278 DAG.getConstant(5, MVT::i32));
10279 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010280
Lang Hames8b99c1e2011-12-17 01:08:46 +000010281 // Turn 'a' into a mask suitable for VSELECT
10282 SDValue VSelM = DAG.getConstant(0x80, VT);
10283 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010284 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010285
Lang Hames8b99c1e2011-12-17 01:08:46 +000010286 SDValue CM1 = DAG.getConstant(0x0f, VT);
10287 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010288
Lang Hames8b99c1e2011-12-17 01:08:46 +000010289 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10290 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000010291 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10292 DAG.getConstant(4, MVT::i32), DAG);
10293 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010294 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10295
Nate Begeman51409212010-07-28 00:21:48 +000010296 // a += a
10297 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010298 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010299 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010300
Lang Hames8b99c1e2011-12-17 01:08:46 +000010301 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10302 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000010303 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10304 DAG.getConstant(2, MVT::i32), DAG);
10305 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010306 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10307
Nate Begeman51409212010-07-28 00:21:48 +000010308 // a += a
10309 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010310 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010311 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010312
Lang Hames8b99c1e2011-12-17 01:08:46 +000010313 // return VSELECT(r, r+r, a);
10314 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010315 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010316 return R;
10317 }
Craig Topper46154eb2011-11-11 07:39:23 +000010318
10319 // Decompose 256-bit shifts into smaller 128-bit shifts.
10320 if (VT.getSizeInBits() == 256) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010321 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000010322 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10323 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10324
10325 // Extract the two vectors
10326 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10327 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10328 DAG, dl);
10329
10330 // Recreate the shift amount vectors
10331 SDValue Amt1, Amt2;
10332 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10333 // Constant shift amount
10334 SmallVector<SDValue, 4> Amt1Csts;
10335 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000010336 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010337 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000010338 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010339 Amt2Csts.push_back(Amt->getOperand(i));
10340
10341 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10342 &Amt1Csts[0], NumElems/2);
10343 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10344 &Amt2Csts[0], NumElems/2);
10345 } else {
10346 // Variable shift amount
10347 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10348 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10349 DAG, dl);
10350 }
10351
10352 // Issue new vector shifts for the smaller types
10353 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10354 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10355
10356 // Concatenate the result back
10357 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10358 }
10359
Nate Begeman51409212010-07-28 00:21:48 +000010360 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010361}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010362
Dan Gohmand858e902010-04-17 15:26:15 +000010363SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010364 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10365 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010366 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10367 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010368 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010369 SDValue LHS = N->getOperand(0);
10370 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010371 unsigned BaseOp = 0;
10372 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010373 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010374 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010375 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010376 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010377 // A subtract of one will be selected as a INC. Note that INC doesn't
10378 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010379 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10380 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010381 BaseOp = X86ISD::INC;
10382 Cond = X86::COND_O;
10383 break;
10384 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010385 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010386 Cond = X86::COND_O;
10387 break;
10388 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010389 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010390 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010391 break;
10392 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010393 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10394 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010395 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10396 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010397 BaseOp = X86ISD::DEC;
10398 Cond = X86::COND_O;
10399 break;
10400 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010401 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010402 Cond = X86::COND_O;
10403 break;
10404 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010405 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010406 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010407 break;
10408 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010409 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010410 Cond = X86::COND_O;
10411 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010412 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10413 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10414 MVT::i32);
10415 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010416
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010417 SDValue SetCC =
10418 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10419 DAG.getConstant(X86::COND_O, MVT::i32),
10420 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010421
Dan Gohman6e5fda22011-07-22 18:45:15 +000010422 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010423 }
Bill Wendling74c37652008-12-09 22:08:41 +000010424 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010425
Bill Wendling61edeb52008-12-02 01:06:39 +000010426 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010427 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010428 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010429
Bill Wendling61edeb52008-12-02 01:06:39 +000010430 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010431 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10432 DAG.getConstant(Cond, MVT::i32),
10433 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010434
Dan Gohman6e5fda22011-07-22 18:45:15 +000010435 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010436}
10437
Chad Rosier30450e82011-12-22 22:35:21 +000010438SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
10439 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010440 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010441 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10442 EVT VT = Op.getValueType();
10443
Craig Toppered2e13d2012-01-22 19:15:14 +000010444 if (!Subtarget->hasSSE2() || !VT.isVector())
10445 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010446
Craig Toppered2e13d2012-01-22 19:15:14 +000010447 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10448 ExtraVT.getScalarType().getSizeInBits();
10449 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10450
10451 switch (VT.getSimpleVT().SimpleTy) {
10452 default: return SDValue();
10453 case MVT::v8i32:
10454 case MVT::v16i16:
10455 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010456 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000010457 if (!Subtarget->hasAVX2()) {
10458 // needs to be split
10459 int NumElems = VT.getVectorNumElements();
10460 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10461 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
Craig Toppera124f942011-11-21 01:12:36 +000010462
Craig Toppered2e13d2012-01-22 19:15:14 +000010463 // Extract the LHS vectors
10464 SDValue LHS = Op.getOperand(0);
10465 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10466 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000010467
Craig Toppered2e13d2012-01-22 19:15:14 +000010468 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10469 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000010470
Craig Toppered2e13d2012-01-22 19:15:14 +000010471 EVT ExtraEltVT = ExtraVT.getVectorElementType();
10472 int ExtraNumElems = ExtraVT.getVectorNumElements();
10473 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10474 ExtraNumElems/2);
10475 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000010476
Craig Toppered2e13d2012-01-22 19:15:14 +000010477 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10478 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000010479
Craig Toppered2e13d2012-01-22 19:15:14 +000010480 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10481 }
10482 // fall through
10483 case MVT::v4i32:
10484 case MVT::v8i16: {
10485 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
10486 Op.getOperand(0), ShAmt, DAG);
10487 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010488 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010489 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010490}
10491
10492
Eric Christopher9a9d2752010-07-22 02:48:34 +000010493SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10494 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010495
Eric Christopher77ed1352011-07-08 00:04:56 +000010496 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10497 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000010498 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010499 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010500 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010501 SDValue Ops[] = {
10502 DAG.getRegister(X86::ESP, MVT::i32), // Base
10503 DAG.getTargetConstant(1, MVT::i8), // Scale
10504 DAG.getRegister(0, MVT::i32), // Index
10505 DAG.getTargetConstant(0, MVT::i32), // Disp
10506 DAG.getRegister(0, MVT::i32), // Segment.
10507 Zero,
10508 Chain
10509 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010510 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010511 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10512 array_lengthof(Ops));
10513 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010514 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010515
Eric Christopher9a9d2752010-07-22 02:48:34 +000010516 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010517 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010518 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010519
Chris Lattner132929a2010-08-14 17:26:09 +000010520 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10521 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10522 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10523 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010524
Chris Lattner132929a2010-08-14 17:26:09 +000010525 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10526 if (!Op1 && !Op2 && !Op3 && Op4)
10527 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010528
Chris Lattner132929a2010-08-14 17:26:09 +000010529 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10530 if (Op1 && !Op2 && !Op3 && !Op4)
10531 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010532
10533 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010534 // (MFENCE)>;
10535 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010536}
10537
Eli Friedman14648462011-07-27 22:21:52 +000010538SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10539 SelectionDAG &DAG) const {
10540 DebugLoc dl = Op.getDebugLoc();
10541 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10542 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10543 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10544 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10545
10546 // The only fence that needs an instruction is a sequentially-consistent
10547 // cross-thread fence.
10548 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10549 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10550 // no-sse2). There isn't any reason to disable it if the target processor
10551 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000010552 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010553 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10554
10555 SDValue Chain = Op.getOperand(0);
10556 SDValue Zero = DAG.getConstant(0, MVT::i32);
10557 SDValue Ops[] = {
10558 DAG.getRegister(X86::ESP, MVT::i32), // Base
10559 DAG.getTargetConstant(1, MVT::i8), // Scale
10560 DAG.getRegister(0, MVT::i32), // Index
10561 DAG.getTargetConstant(0, MVT::i32), // Disp
10562 DAG.getRegister(0, MVT::i32), // Segment.
10563 Zero,
10564 Chain
10565 };
10566 SDNode *Res =
10567 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10568 array_lengthof(Ops));
10569 return SDValue(Res, 0);
10570 }
10571
10572 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10573 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10574}
10575
10576
Dan Gohmand858e902010-04-17 15:26:15 +000010577SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010578 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010579 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010580 unsigned Reg = 0;
10581 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010582 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000010583 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010584 case MVT::i8: Reg = X86::AL; size = 1; break;
10585 case MVT::i16: Reg = X86::AX; size = 2; break;
10586 case MVT::i32: Reg = X86::EAX; size = 4; break;
10587 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010588 assert(Subtarget->is64Bit() && "Node not type legal!");
10589 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010590 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010591 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010592 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010593 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010594 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010595 Op.getOperand(1),
10596 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010597 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010598 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010599 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010600 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10601 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10602 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010603 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010604 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010605 return cpOut;
10606}
10607
Duncan Sands1607f052008-12-01 11:39:25 +000010608SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010609 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010610 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010611 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010612 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010613 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010614 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010615 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10616 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010617 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010618 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10619 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010620 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010621 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010622 rdx.getValue(1)
10623 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010624 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010625}
10626
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010627SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010628 SelectionDAG &DAG) const {
10629 EVT SrcVT = Op.getOperand(0).getValueType();
10630 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000010631 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010632 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010633 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010634 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010635 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010636 // i64 <=> MMX conversions are Legal.
10637 if (SrcVT==MVT::i64 && DstVT.isVector())
10638 return Op;
10639 if (DstVT==MVT::i64 && SrcVT.isVector())
10640 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010641 // MMX <=> MMX conversions are Legal.
10642 if (SrcVT.isVector() && DstVT.isVector())
10643 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010644 // All other conversions need to be expanded.
10645 return SDValue();
10646}
Chris Lattner5b856542010-12-20 00:59:46 +000010647
Dan Gohmand858e902010-04-17 15:26:15 +000010648SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010649 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010650 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010651 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010652 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010653 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010654 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010655 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010656 Node->getOperand(0),
10657 Node->getOperand(1), negOp,
10658 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010659 cast<AtomicSDNode>(Node)->getAlignment(),
10660 cast<AtomicSDNode>(Node)->getOrdering(),
10661 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010662}
10663
Eli Friedman327236c2011-08-24 20:50:09 +000010664static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10665 SDNode *Node = Op.getNode();
10666 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010667 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010668
10669 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010670 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10671 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10672 // (The only way to get a 16-byte store is cmpxchg16b)
10673 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10674 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10675 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010676 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10677 cast<AtomicSDNode>(Node)->getMemoryVT(),
10678 Node->getOperand(0),
10679 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010680 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010681 cast<AtomicSDNode>(Node)->getOrdering(),
10682 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010683 return Swap.getValue(1);
10684 }
10685 // Other atomic stores have a simple pattern.
10686 return Op;
10687}
10688
Chris Lattner5b856542010-12-20 00:59:46 +000010689static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10690 EVT VT = Op.getNode()->getValueType(0);
10691
10692 // Let legalize expand this if it isn't a legal type yet.
10693 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10694 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010695
Chris Lattner5b856542010-12-20 00:59:46 +000010696 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010697
Chris Lattner5b856542010-12-20 00:59:46 +000010698 unsigned Opc;
10699 bool ExtraOp = false;
10700 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000010701 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000010702 case ISD::ADDC: Opc = X86ISD::ADD; break;
10703 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10704 case ISD::SUBC: Opc = X86ISD::SUB; break;
10705 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10706 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010707
Chris Lattner5b856542010-12-20 00:59:46 +000010708 if (!ExtraOp)
10709 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10710 Op.getOperand(1));
10711 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10712 Op.getOperand(1), Op.getOperand(2));
10713}
10714
Evan Cheng0db9fe62006-04-25 20:13:52 +000010715/// LowerOperation - Provide custom lowering hooks for some operations.
10716///
Dan Gohmand858e902010-04-17 15:26:15 +000010717SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010718 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010719 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010720 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010721 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010722 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010723 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10724 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010725 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010726 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010727 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010728 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10729 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10730 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010731 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010732 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010733 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10734 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10735 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010736 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010737 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010738 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010739 case ISD::SHL_PARTS:
10740 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010741 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010742 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010743 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010744 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010745 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010746 case ISD::FABS: return LowerFABS(Op, DAG);
10747 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010748 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010749 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010750 case ISD::SETCC: return LowerSETCC(Op, DAG);
10751 case ISD::SELECT: return LowerSELECT(Op, DAG);
10752 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010753 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010754 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010755 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010756 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010757 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010758 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10759 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010760 case ISD::FRAME_TO_ARGS_OFFSET:
10761 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010762 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010763 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010764 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10765 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010766 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010767 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000010768 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010769 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010770 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010771 case ISD::SRA:
10772 case ISD::SRL:
10773 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010774 case ISD::SADDO:
10775 case ISD::UADDO:
10776 case ISD::SSUBO:
10777 case ISD::USUBO:
10778 case ISD::SMULO:
10779 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010780 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010781 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010782 case ISD::ADDC:
10783 case ISD::ADDE:
10784 case ISD::SUBC:
10785 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010786 case ISD::ADD: return LowerADD(Op, DAG);
10787 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010788 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010789}
10790
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010791static void ReplaceATOMIC_LOAD(SDNode *Node,
10792 SmallVectorImpl<SDValue> &Results,
10793 SelectionDAG &DAG) {
10794 DebugLoc dl = Node->getDebugLoc();
10795 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10796
10797 // Convert wide load -> cmpxchg8b/cmpxchg16b
10798 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10799 // (The only way to get a 16-byte load is cmpxchg16b)
10800 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010801 SDValue Zero = DAG.getConstant(0, VT);
10802 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010803 Node->getOperand(0),
10804 Node->getOperand(1), Zero, Zero,
10805 cast<AtomicSDNode>(Node)->getMemOperand(),
10806 cast<AtomicSDNode>(Node)->getOrdering(),
10807 cast<AtomicSDNode>(Node)->getSynchScope());
10808 Results.push_back(Swap.getValue(0));
10809 Results.push_back(Swap.getValue(1));
10810}
10811
Duncan Sands1607f052008-12-01 11:39:25 +000010812void X86TargetLowering::
10813ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010814 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010815 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000010816 assert (Node->getValueType(0) == MVT::i64 &&
10817 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010818
10819 SDValue Chain = Node->getOperand(0);
10820 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010821 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010822 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010823 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010824 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010825 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010826 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010827 SDValue Result =
10828 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10829 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010830 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010831 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010832 Results.push_back(Result.getValue(2));
10833}
10834
Duncan Sands126d9072008-07-04 11:47:58 +000010835/// ReplaceNodeResults - Replace a node with an illegal result type
10836/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010837void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10838 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010839 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010840 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010841 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010842 default:
Craig Topperabb94d02012-02-05 03:43:23 +000010843 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010844 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010845 case ISD::ADDC:
10846 case ISD::ADDE:
10847 case ISD::SUBC:
10848 case ISD::SUBE:
10849 // We don't want to expand or promote these.
10850 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010851 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010852 std::pair<SDValue,SDValue> Vals =
10853 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010854 SDValue FIST = Vals.first, StackSlot = Vals.second;
10855 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010856 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010857 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010858 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010859 MachinePointerInfo(),
10860 false, false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010861 }
10862 return;
10863 }
10864 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010865 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010866 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010867 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010868 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010869 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010870 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010871 eax.getValue(2));
10872 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10873 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010874 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010875 Results.push_back(edx.getValue(1));
10876 return;
10877 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010878 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010879 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010880 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010881 bool Regs64bit = T == MVT::i128;
10882 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010883 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010884 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10885 DAG.getConstant(0, HalfT));
10886 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10887 DAG.getConstant(1, HalfT));
10888 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10889 Regs64bit ? X86::RAX : X86::EAX,
10890 cpInL, SDValue());
10891 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10892 Regs64bit ? X86::RDX : X86::EDX,
10893 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010894 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010895 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10896 DAG.getConstant(0, HalfT));
10897 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10898 DAG.getConstant(1, HalfT));
10899 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10900 Regs64bit ? X86::RBX : X86::EBX,
10901 swapInL, cpInH.getValue(1));
10902 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10903 Regs64bit ? X86::RCX : X86::ECX,
10904 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010905 SDValue Ops[] = { swapInH.getValue(0),
10906 N->getOperand(1),
10907 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010908 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010909 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010910 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10911 X86ISD::LCMPXCHG8_DAG;
10912 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010913 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010914 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10915 Regs64bit ? X86::RAX : X86::EAX,
10916 HalfT, Result.getValue(1));
10917 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10918 Regs64bit ? X86::RDX : X86::EDX,
10919 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010920 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010921 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010922 Results.push_back(cpOutH.getValue(1));
10923 return;
10924 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010925 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010926 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10927 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010928 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010929 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10930 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010931 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010932 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10933 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010934 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010935 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10936 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010937 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010938 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10939 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010940 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010941 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10942 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010943 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010944 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10945 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010946 case ISD::ATOMIC_LOAD:
10947 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010948 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010949}
10950
Evan Cheng72261582005-12-20 06:22:03 +000010951const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10952 switch (Opcode) {
10953 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010954 case X86ISD::BSF: return "X86ISD::BSF";
10955 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010956 case X86ISD::SHLD: return "X86ISD::SHLD";
10957 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010958 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010959 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010960 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010961 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010962 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010963 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010964 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10965 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10966 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010967 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010968 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010969 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010970 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010971 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010972 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010973 case X86ISD::COMI: return "X86ISD::COMI";
10974 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010975 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010976 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010977 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10978 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010979 case X86ISD::CMOV: return "X86ISD::CMOV";
10980 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010981 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010982 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10983 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010984 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010985 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010986 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010987 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010988 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010989 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10990 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010991 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010992 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010993 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000010994 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000010995 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Craig Topperfe033152011-12-06 09:31:36 +000010996 case X86ISD::HADD: return "X86ISD::HADD";
10997 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000010998 case X86ISD::FHADD: return "X86ISD::FHADD";
10999 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011000 case X86ISD::FMAX: return "X86ISD::FMAX";
11001 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000011002 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11003 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011004 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011005 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011006 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011007 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011008 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011009 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11010 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011011 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11012 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11013 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11014 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11015 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11016 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011017 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
11018 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Craig Toppered2e13d2012-01-22 19:15:14 +000011019 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11020 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011021 case X86ISD::VSHL: return "X86ISD::VSHL";
11022 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011023 case X86ISD::VSRA: return "X86ISD::VSRA";
11024 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11025 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11026 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011027 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011028 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11029 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011030 case X86ISD::ADD: return "X86ISD::ADD";
11031 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011032 case X86ISD::ADC: return "X86ISD::ADC";
11033 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011034 case X86ISD::SMUL: return "X86ISD::SMUL";
11035 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011036 case X86ISD::INC: return "X86ISD::INC";
11037 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011038 case X86ISD::OR: return "X86ISD::OR";
11039 case X86ISD::XOR: return "X86ISD::XOR";
11040 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011041 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011042 case X86ISD::BLSI: return "X86ISD::BLSI";
11043 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11044 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011045 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011046 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011047 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011048 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11049 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11050 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011051 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000011052 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011053 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011054 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011055 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011056 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11057 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011058 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11059 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11060 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011061 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11062 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000011063 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
11064 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011065 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011066 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011067 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper5b209e82012-02-05 03:14:49 +000011068 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011069 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011070 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011071 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011072 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011073 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000011074 }
11075}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011076
Chris Lattnerc9addb72007-03-30 23:15:24 +000011077// isLegalAddressingMode - Return true if the addressing mode represented
11078// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011079bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011080 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011081 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011082 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011083 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011084
Chris Lattnerc9addb72007-03-30 23:15:24 +000011085 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011086 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011087 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011088
Chris Lattnerc9addb72007-03-30 23:15:24 +000011089 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011090 unsigned GVFlags =
11091 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011092
Chris Lattnerdfed4132009-07-10 07:38:24 +000011093 // If a reference to this global requires an extra load, we can't fold it.
11094 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011095 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011096
Chris Lattnerdfed4132009-07-10 07:38:24 +000011097 // If BaseGV requires a register for the PIC base, we cannot also have a
11098 // BaseReg specified.
11099 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011100 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011101
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011102 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011103 if ((M != CodeModel::Small || R != Reloc::Static) &&
11104 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011105 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011106 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011107
Chris Lattnerc9addb72007-03-30 23:15:24 +000011108 switch (AM.Scale) {
11109 case 0:
11110 case 1:
11111 case 2:
11112 case 4:
11113 case 8:
11114 // These scales always work.
11115 break;
11116 case 3:
11117 case 5:
11118 case 9:
11119 // These scales are formed with basereg+scalereg. Only accept if there is
11120 // no basereg yet.
11121 if (AM.HasBaseReg)
11122 return false;
11123 break;
11124 default: // Other stuff never works.
11125 return false;
11126 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011127
Chris Lattnerc9addb72007-03-30 23:15:24 +000011128 return true;
11129}
11130
11131
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011132bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011133 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011134 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011135 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11136 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011137 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011138 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011139 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011140}
11141
Owen Andersone50ed302009-08-10 22:56:29 +000011142bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011143 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011144 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011145 unsigned NumBits1 = VT1.getSizeInBits();
11146 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011147 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011148 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011149 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011150}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011151
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011152bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011153 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011154 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011155}
11156
Owen Andersone50ed302009-08-10 22:56:29 +000011157bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011158 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011159 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011160}
11161
Owen Andersone50ed302009-08-10 22:56:29 +000011162bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011163 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011164 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011165}
11166
Evan Cheng60c07e12006-07-05 22:17:51 +000011167/// isShuffleMaskLegal - Targets can use this to indicate that they only
11168/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11169/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11170/// are assumed to be legal.
11171bool
Eric Christopherfd179292009-08-27 18:07:15 +000011172X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011173 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011174 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011175 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011176 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011177
Nate Begemana09008b2009-10-19 02:17:23 +000011178 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011179 return (VT.getVectorNumElements() == 2 ||
11180 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11181 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011182 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011183 isPSHUFDMask(M, VT) ||
11184 isPSHUFHWMask(M, VT) ||
11185 isPSHUFLWMask(M, VT) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000011186 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000011187 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11188 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011189 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11190 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011191}
11192
Dan Gohman7d8143f2008-04-09 20:09:42 +000011193bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011194X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011195 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011196 unsigned NumElts = VT.getVectorNumElements();
11197 // FIXME: This collection of masks seems suspect.
11198 if (NumElts == 2)
11199 return true;
11200 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11201 return (isMOVLMask(Mask, VT) ||
11202 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011203 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
11204 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011205 }
11206 return false;
11207}
11208
11209//===----------------------------------------------------------------------===//
11210// X86 Scheduler Hooks
11211//===----------------------------------------------------------------------===//
11212
Mon P Wang63307c32008-05-05 19:05:59 +000011213// private utility function
11214MachineBasicBlock *
11215X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11216 MachineBasicBlock *MBB,
11217 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011218 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011219 unsigned LoadOpc,
11220 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011221 unsigned notOpc,
11222 unsigned EAXreg,
11223 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011224 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011225 // For the atomic bitwise operator, we generate
11226 // thisMBB:
11227 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011228 // ld t1 = [bitinstr.addr]
11229 // op t2 = t1, [bitinstr.val]
11230 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011231 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11232 // bz newMBB
11233 // fallthrough -->nextMBB
11234 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11235 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011236 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011237 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011238
Mon P Wang63307c32008-05-05 19:05:59 +000011239 /// First build the CFG
11240 MachineFunction *F = MBB->getParent();
11241 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011242 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11243 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11244 F->insert(MBBIter, newMBB);
11245 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011246
Dan Gohman14152b42010-07-06 20:24:04 +000011247 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11248 nextMBB->splice(nextMBB->begin(), thisMBB,
11249 llvm::next(MachineBasicBlock::iterator(bInstr)),
11250 thisMBB->end());
11251 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011252
Mon P Wang63307c32008-05-05 19:05:59 +000011253 // Update thisMBB to fall through to newMBB
11254 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011255
Mon P Wang63307c32008-05-05 19:05:59 +000011256 // newMBB jumps to itself and fall through to nextMBB
11257 newMBB->addSuccessor(nextMBB);
11258 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011259
Mon P Wang63307c32008-05-05 19:05:59 +000011260 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011261 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011262 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011263 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011264 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011265 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011266 int numArgs = bInstr->getNumOperands() - 1;
11267 for (int i=0; i < numArgs; ++i)
11268 argOpers[i] = &bInstr->getOperand(i+1);
11269
11270 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011271 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011272 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011273
Dale Johannesen140be2d2008-08-19 18:47:28 +000011274 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011275 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011276 for (int i=0; i <= lastAddrIndx; ++i)
11277 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011278
Dale Johannesen140be2d2008-08-19 18:47:28 +000011279 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011280 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011281 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011282 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011283 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011284 tt = t1;
11285
Dale Johannesen140be2d2008-08-19 18:47:28 +000011286 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011287 assert((argOpers[valArgIndx]->isReg() ||
11288 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011289 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011290 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011291 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011292 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011293 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011294 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011295 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011296
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011297 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011298 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011299
Dale Johannesene4d209d2009-02-03 20:21:25 +000011300 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011301 for (int i=0; i <= lastAddrIndx; ++i)
11302 (*MIB).addOperand(*argOpers[i]);
11303 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011304 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011305 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11306 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011307
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011308 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011309 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011310
Mon P Wang63307c32008-05-05 19:05:59 +000011311 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011312 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011313
Dan Gohman14152b42010-07-06 20:24:04 +000011314 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011315 return nextMBB;
11316}
11317
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011318// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011319MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011320X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11321 MachineBasicBlock *MBB,
11322 unsigned regOpcL,
11323 unsigned regOpcH,
11324 unsigned immOpcL,
11325 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011326 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011327 // For the atomic bitwise operator, we generate
11328 // thisMBB (instructions are in pairs, except cmpxchg8b)
11329 // ld t1,t2 = [bitinstr.addr]
11330 // newMBB:
11331 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11332 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011333 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011334 // mov ECX, EBX <- t5, t6
11335 // mov EAX, EDX <- t1, t2
11336 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11337 // mov t3, t4 <- EAX, EDX
11338 // bz newMBB
11339 // result in out1, out2
11340 // fallthrough -->nextMBB
11341
11342 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11343 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011344 const unsigned NotOpc = X86::NOT32r;
11345 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11346 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11347 MachineFunction::iterator MBBIter = MBB;
11348 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011349
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011350 /// First build the CFG
11351 MachineFunction *F = MBB->getParent();
11352 MachineBasicBlock *thisMBB = MBB;
11353 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11354 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11355 F->insert(MBBIter, newMBB);
11356 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011357
Dan Gohman14152b42010-07-06 20:24:04 +000011358 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11359 nextMBB->splice(nextMBB->begin(), thisMBB,
11360 llvm::next(MachineBasicBlock::iterator(bInstr)),
11361 thisMBB->end());
11362 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011363
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011364 // Update thisMBB to fall through to newMBB
11365 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011366
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011367 // newMBB jumps to itself and fall through to nextMBB
11368 newMBB->addSuccessor(nextMBB);
11369 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011370
Dale Johannesene4d209d2009-02-03 20:21:25 +000011371 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011372 // Insert instructions into newMBB based on incoming instruction
11373 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011374 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011375 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011376 MachineOperand& dest1Oper = bInstr->getOperand(0);
11377 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011378 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11379 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011380 argOpers[i] = &bInstr->getOperand(i+2);
11381
Dan Gohman71ea4e52010-05-14 21:01:44 +000011382 // We use some of the operands multiple times, so conservatively just
11383 // clear any kill flags that might be present.
11384 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11385 argOpers[i]->setIsKill(false);
11386 }
11387
Evan Chengad5b52f2010-01-08 19:14:57 +000011388 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011389 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011390
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011391 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011392 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011393 for (int i=0; i <= lastAddrIndx; ++i)
11394 (*MIB).addOperand(*argOpers[i]);
11395 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011396 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011397 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011398 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011399 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011400 MachineOperand newOp3 = *(argOpers[3]);
11401 if (newOp3.isImm())
11402 newOp3.setImm(newOp3.getImm()+4);
11403 else
11404 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011405 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011406 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011407
11408 // t3/4 are defined later, at the bottom of the loop
11409 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11410 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011411 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011412 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011413 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011414 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11415
Evan Cheng306b4ca2010-01-08 23:41:50 +000011416 // The subsequent operations should be using the destination registers of
11417 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011418 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011419 t1 = F->getRegInfo().createVirtualRegister(RC);
11420 t2 = F->getRegInfo().createVirtualRegister(RC);
11421 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11422 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011423 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011424 t1 = dest1Oper.getReg();
11425 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011426 }
11427
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011428 int valArgIndx = lastAddrIndx + 1;
11429 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011430 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011431 "invalid operand");
11432 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11433 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011434 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011435 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011436 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011437 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011438 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011439 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011440 (*MIB).addOperand(*argOpers[valArgIndx]);
11441 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011442 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011443 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011444 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011445 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011446 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011447 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011448 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011449 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011450 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011451 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011452
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011453 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011454 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011455 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011456 MIB.addReg(t2);
11457
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011458 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011459 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011460 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011461 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011462
Dale Johannesene4d209d2009-02-03 20:21:25 +000011463 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011464 for (int i=0; i <= lastAddrIndx; ++i)
11465 (*MIB).addOperand(*argOpers[i]);
11466
11467 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011468 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11469 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011470
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011471 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011472 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011473 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011474 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011475
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011476 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011477 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011478
Dan Gohman14152b42010-07-06 20:24:04 +000011479 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011480 return nextMBB;
11481}
11482
11483// private utility function
11484MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011485X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11486 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011487 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011488 // For the atomic min/max operator, we generate
11489 // thisMBB:
11490 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011491 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011492 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011493 // cmp t1, t2
11494 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011495 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011496 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11497 // bz newMBB
11498 // fallthrough -->nextMBB
11499 //
11500 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11501 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011502 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011503 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011504
Mon P Wang63307c32008-05-05 19:05:59 +000011505 /// First build the CFG
11506 MachineFunction *F = MBB->getParent();
11507 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011508 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11509 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11510 F->insert(MBBIter, newMBB);
11511 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011512
Dan Gohman14152b42010-07-06 20:24:04 +000011513 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11514 nextMBB->splice(nextMBB->begin(), thisMBB,
11515 llvm::next(MachineBasicBlock::iterator(mInstr)),
11516 thisMBB->end());
11517 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011518
Mon P Wang63307c32008-05-05 19:05:59 +000011519 // Update thisMBB to fall through to newMBB
11520 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011521
Mon P Wang63307c32008-05-05 19:05:59 +000011522 // newMBB jumps to newMBB and fall through to nextMBB
11523 newMBB->addSuccessor(nextMBB);
11524 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011525
Dale Johannesene4d209d2009-02-03 20:21:25 +000011526 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011527 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011528 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011529 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011530 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011531 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011532 int numArgs = mInstr->getNumOperands() - 1;
11533 for (int i=0; i < numArgs; ++i)
11534 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011535
Mon P Wang63307c32008-05-05 19:05:59 +000011536 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011537 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011538 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011539
Mon P Wangab3e7472008-05-05 22:56:23 +000011540 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011541 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011542 for (int i=0; i <= lastAddrIndx; ++i)
11543 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011544
Mon P Wang63307c32008-05-05 19:05:59 +000011545 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011546 assert((argOpers[valArgIndx]->isReg() ||
11547 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011548 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011549
11550 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011551 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011552 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011553 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011554 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011555 (*MIB).addOperand(*argOpers[valArgIndx]);
11556
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011557 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011558 MIB.addReg(t1);
11559
Dale Johannesene4d209d2009-02-03 20:21:25 +000011560 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011561 MIB.addReg(t1);
11562 MIB.addReg(t2);
11563
11564 // Generate movc
11565 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011566 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011567 MIB.addReg(t2);
11568 MIB.addReg(t1);
11569
11570 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011571 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011572 for (int i=0; i <= lastAddrIndx; ++i)
11573 (*MIB).addOperand(*argOpers[i]);
11574 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011575 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011576 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11577 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011578
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011579 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011580 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011581
Mon P Wang63307c32008-05-05 19:05:59 +000011582 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011583 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011584
Dan Gohman14152b42010-07-06 20:24:04 +000011585 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011586 return nextMBB;
11587}
11588
Eric Christopherf83a5de2009-08-27 18:08:16 +000011589// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011590// or XMM0_V32I8 in AVX all of this code can be replaced with that
11591// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011592MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011593X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011594 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000011595 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011596 "Target must have SSE4.2 or AVX features enabled");
11597
Eric Christopherb120ab42009-08-18 22:50:32 +000011598 DebugLoc dl = MI->getDebugLoc();
11599 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011600 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011601 if (!Subtarget->hasAVX()) {
11602 if (memArg)
11603 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11604 else
11605 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11606 } else {
11607 if (memArg)
11608 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11609 else
11610 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11611 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011612
Eric Christopher41c902f2010-11-30 08:20:21 +000011613 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011614 for (unsigned i = 0; i < numArgs; ++i) {
11615 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011616 if (!(Op.isReg() && Op.isImplicit()))
11617 MIB.addOperand(Op);
11618 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011619 BuildMI(*BB, MI, dl,
11620 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11621 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011622 .addReg(X86::XMM0);
11623
Dan Gohman14152b42010-07-06 20:24:04 +000011624 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011625 return BB;
11626}
11627
11628MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011629X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011630 DebugLoc dl = MI->getDebugLoc();
11631 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011632
Eric Christopher228232b2010-11-30 07:20:12 +000011633 // Address into RAX/EAX, other two args into ECX, EDX.
11634 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11635 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11636 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11637 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011638 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011639
Eric Christopher228232b2010-11-30 07:20:12 +000011640 unsigned ValOps = X86::AddrNumOperands;
11641 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11642 .addReg(MI->getOperand(ValOps).getReg());
11643 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11644 .addReg(MI->getOperand(ValOps+1).getReg());
11645
11646 // The instruction doesn't actually take any operands though.
11647 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011648
Eric Christopher228232b2010-11-30 07:20:12 +000011649 MI->eraseFromParent(); // The pseudo is gone now.
11650 return BB;
11651}
11652
11653MachineBasicBlock *
11654X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011655 DebugLoc dl = MI->getDebugLoc();
11656 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011657
Eric Christopher228232b2010-11-30 07:20:12 +000011658 // First arg in ECX, the second in EAX.
11659 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11660 .addReg(MI->getOperand(0).getReg());
11661 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11662 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011663
Eric Christopher228232b2010-11-30 07:20:12 +000011664 // The instruction doesn't actually take any operands though.
11665 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011666
Eric Christopher228232b2010-11-30 07:20:12 +000011667 MI->eraseFromParent(); // The pseudo is gone now.
11668 return BB;
11669}
11670
11671MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011672X86TargetLowering::EmitVAARG64WithCustomInserter(
11673 MachineInstr *MI,
11674 MachineBasicBlock *MBB) const {
11675 // Emit va_arg instruction on X86-64.
11676
11677 // Operands to this pseudo-instruction:
11678 // 0 ) Output : destination address (reg)
11679 // 1-5) Input : va_list address (addr, i64mem)
11680 // 6 ) ArgSize : Size (in bytes) of vararg type
11681 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11682 // 8 ) Align : Alignment of type
11683 // 9 ) EFLAGS (implicit-def)
11684
11685 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11686 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11687
11688 unsigned DestReg = MI->getOperand(0).getReg();
11689 MachineOperand &Base = MI->getOperand(1);
11690 MachineOperand &Scale = MI->getOperand(2);
11691 MachineOperand &Index = MI->getOperand(3);
11692 MachineOperand &Disp = MI->getOperand(4);
11693 MachineOperand &Segment = MI->getOperand(5);
11694 unsigned ArgSize = MI->getOperand(6).getImm();
11695 unsigned ArgMode = MI->getOperand(7).getImm();
11696 unsigned Align = MI->getOperand(8).getImm();
11697
11698 // Memory Reference
11699 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11700 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11701 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11702
11703 // Machine Information
11704 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11705 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11706 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11707 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11708 DebugLoc DL = MI->getDebugLoc();
11709
11710 // struct va_list {
11711 // i32 gp_offset
11712 // i32 fp_offset
11713 // i64 overflow_area (address)
11714 // i64 reg_save_area (address)
11715 // }
11716 // sizeof(va_list) = 24
11717 // alignment(va_list) = 8
11718
11719 unsigned TotalNumIntRegs = 6;
11720 unsigned TotalNumXMMRegs = 8;
11721 bool UseGPOffset = (ArgMode == 1);
11722 bool UseFPOffset = (ArgMode == 2);
11723 unsigned MaxOffset = TotalNumIntRegs * 8 +
11724 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11725
11726 /* Align ArgSize to a multiple of 8 */
11727 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11728 bool NeedsAlign = (Align > 8);
11729
11730 MachineBasicBlock *thisMBB = MBB;
11731 MachineBasicBlock *overflowMBB;
11732 MachineBasicBlock *offsetMBB;
11733 MachineBasicBlock *endMBB;
11734
11735 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11736 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11737 unsigned OffsetReg = 0;
11738
11739 if (!UseGPOffset && !UseFPOffset) {
11740 // If we only pull from the overflow region, we don't create a branch.
11741 // We don't need to alter control flow.
11742 OffsetDestReg = 0; // unused
11743 OverflowDestReg = DestReg;
11744
11745 offsetMBB = NULL;
11746 overflowMBB = thisMBB;
11747 endMBB = thisMBB;
11748 } else {
11749 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11750 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11751 // If not, pull from overflow_area. (branch to overflowMBB)
11752 //
11753 // thisMBB
11754 // | .
11755 // | .
11756 // offsetMBB overflowMBB
11757 // | .
11758 // | .
11759 // endMBB
11760
11761 // Registers for the PHI in endMBB
11762 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11763 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11764
11765 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11766 MachineFunction *MF = MBB->getParent();
11767 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11768 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11769 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11770
11771 MachineFunction::iterator MBBIter = MBB;
11772 ++MBBIter;
11773
11774 // Insert the new basic blocks
11775 MF->insert(MBBIter, offsetMBB);
11776 MF->insert(MBBIter, overflowMBB);
11777 MF->insert(MBBIter, endMBB);
11778
11779 // Transfer the remainder of MBB and its successor edges to endMBB.
11780 endMBB->splice(endMBB->begin(), thisMBB,
11781 llvm::next(MachineBasicBlock::iterator(MI)),
11782 thisMBB->end());
11783 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11784
11785 // Make offsetMBB and overflowMBB successors of thisMBB
11786 thisMBB->addSuccessor(offsetMBB);
11787 thisMBB->addSuccessor(overflowMBB);
11788
11789 // endMBB is a successor of both offsetMBB and overflowMBB
11790 offsetMBB->addSuccessor(endMBB);
11791 overflowMBB->addSuccessor(endMBB);
11792
11793 // Load the offset value into a register
11794 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11795 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11796 .addOperand(Base)
11797 .addOperand(Scale)
11798 .addOperand(Index)
11799 .addDisp(Disp, UseFPOffset ? 4 : 0)
11800 .addOperand(Segment)
11801 .setMemRefs(MMOBegin, MMOEnd);
11802
11803 // Check if there is enough room left to pull this argument.
11804 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11805 .addReg(OffsetReg)
11806 .addImm(MaxOffset + 8 - ArgSizeA8);
11807
11808 // Branch to "overflowMBB" if offset >= max
11809 // Fall through to "offsetMBB" otherwise
11810 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11811 .addMBB(overflowMBB);
11812 }
11813
11814 // In offsetMBB, emit code to use the reg_save_area.
11815 if (offsetMBB) {
11816 assert(OffsetReg != 0);
11817
11818 // Read the reg_save_area address.
11819 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11820 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11821 .addOperand(Base)
11822 .addOperand(Scale)
11823 .addOperand(Index)
11824 .addDisp(Disp, 16)
11825 .addOperand(Segment)
11826 .setMemRefs(MMOBegin, MMOEnd);
11827
11828 // Zero-extend the offset
11829 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11830 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11831 .addImm(0)
11832 .addReg(OffsetReg)
11833 .addImm(X86::sub_32bit);
11834
11835 // Add the offset to the reg_save_area to get the final address.
11836 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11837 .addReg(OffsetReg64)
11838 .addReg(RegSaveReg);
11839
11840 // Compute the offset for the next argument
11841 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11842 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11843 .addReg(OffsetReg)
11844 .addImm(UseFPOffset ? 16 : 8);
11845
11846 // Store it back into the va_list.
11847 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11848 .addOperand(Base)
11849 .addOperand(Scale)
11850 .addOperand(Index)
11851 .addDisp(Disp, UseFPOffset ? 4 : 0)
11852 .addOperand(Segment)
11853 .addReg(NextOffsetReg)
11854 .setMemRefs(MMOBegin, MMOEnd);
11855
11856 // Jump to endMBB
11857 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11858 .addMBB(endMBB);
11859 }
11860
11861 //
11862 // Emit code to use overflow area
11863 //
11864
11865 // Load the overflow_area address into a register.
11866 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11867 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11868 .addOperand(Base)
11869 .addOperand(Scale)
11870 .addOperand(Index)
11871 .addDisp(Disp, 8)
11872 .addOperand(Segment)
11873 .setMemRefs(MMOBegin, MMOEnd);
11874
11875 // If we need to align it, do so. Otherwise, just copy the address
11876 // to OverflowDestReg.
11877 if (NeedsAlign) {
11878 // Align the overflow address
11879 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11880 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11881
11882 // aligned_addr = (addr + (align-1)) & ~(align-1)
11883 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11884 .addReg(OverflowAddrReg)
11885 .addImm(Align-1);
11886
11887 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11888 .addReg(TmpReg)
11889 .addImm(~(uint64_t)(Align-1));
11890 } else {
11891 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11892 .addReg(OverflowAddrReg);
11893 }
11894
11895 // Compute the next overflow address after this argument.
11896 // (the overflow address should be kept 8-byte aligned)
11897 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11898 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11899 .addReg(OverflowDestReg)
11900 .addImm(ArgSizeA8);
11901
11902 // Store the new overflow address.
11903 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11904 .addOperand(Base)
11905 .addOperand(Scale)
11906 .addOperand(Index)
11907 .addDisp(Disp, 8)
11908 .addOperand(Segment)
11909 .addReg(NextAddrReg)
11910 .setMemRefs(MMOBegin, MMOEnd);
11911
11912 // If we branched, emit the PHI to the front of endMBB.
11913 if (offsetMBB) {
11914 BuildMI(*endMBB, endMBB->begin(), DL,
11915 TII->get(X86::PHI), DestReg)
11916 .addReg(OffsetDestReg).addMBB(offsetMBB)
11917 .addReg(OverflowDestReg).addMBB(overflowMBB);
11918 }
11919
11920 // Erase the pseudo instruction
11921 MI->eraseFromParent();
11922
11923 return endMBB;
11924}
11925
11926MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011927X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11928 MachineInstr *MI,
11929 MachineBasicBlock *MBB) const {
11930 // Emit code to save XMM registers to the stack. The ABI says that the
11931 // number of registers to save is given in %al, so it's theoretically
11932 // possible to do an indirect jump trick to avoid saving all of them,
11933 // however this code takes a simpler approach and just executes all
11934 // of the stores if %al is non-zero. It's less code, and it's probably
11935 // easier on the hardware branch predictor, and stores aren't all that
11936 // expensive anyway.
11937
11938 // Create the new basic blocks. One block contains all the XMM stores,
11939 // and one block is the final destination regardless of whether any
11940 // stores were performed.
11941 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11942 MachineFunction *F = MBB->getParent();
11943 MachineFunction::iterator MBBIter = MBB;
11944 ++MBBIter;
11945 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11946 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11947 F->insert(MBBIter, XMMSaveMBB);
11948 F->insert(MBBIter, EndMBB);
11949
Dan Gohman14152b42010-07-06 20:24:04 +000011950 // Transfer the remainder of MBB and its successor edges to EndMBB.
11951 EndMBB->splice(EndMBB->begin(), MBB,
11952 llvm::next(MachineBasicBlock::iterator(MI)),
11953 MBB->end());
11954 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11955
Dan Gohmand6708ea2009-08-15 01:38:56 +000011956 // The original block will now fall through to the XMM save block.
11957 MBB->addSuccessor(XMMSaveMBB);
11958 // The XMMSaveMBB will fall through to the end block.
11959 XMMSaveMBB->addSuccessor(EndMBB);
11960
11961 // Now add the instructions.
11962 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11963 DebugLoc DL = MI->getDebugLoc();
11964
11965 unsigned CountReg = MI->getOperand(0).getReg();
11966 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11967 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11968
11969 if (!Subtarget->isTargetWin64()) {
11970 // If %al is 0, branch around the XMM save block.
11971 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011972 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011973 MBB->addSuccessor(EndMBB);
11974 }
11975
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011976 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011977 // In the XMM save block, save all the XMM argument registers.
11978 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11979 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011980 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011981 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011982 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011983 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011984 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011985 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011986 .addFrameIndex(RegSaveFrameIndex)
11987 .addImm(/*Scale=*/1)
11988 .addReg(/*IndexReg=*/0)
11989 .addImm(/*Disp=*/Offset)
11990 .addReg(/*Segment=*/0)
11991 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011992 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011993 }
11994
Dan Gohman14152b42010-07-06 20:24:04 +000011995 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011996
11997 return EndMBB;
11998}
Mon P Wang63307c32008-05-05 19:05:59 +000011999
Lang Hames6e3f7e42012-02-03 01:13:49 +000012000// The EFLAGS operand of SelectItr might be missing a kill marker
12001// because there were multiple uses of EFLAGS, and ISel didn't know
12002// which to mark. Figure out whether SelectItr should have had a
12003// kill marker, and set it if it should. Returns the correct kill
12004// marker value.
12005static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
12006 MachineBasicBlock* BB,
12007 const TargetRegisterInfo* TRI) {
12008 // Scan forward through BB for a use/def of EFLAGS.
12009 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
12010 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000012011 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012012 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000012013 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012014 if (mi.definesRegister(X86::EFLAGS))
12015 break; // Should have kill-flag - update below.
12016 }
12017
12018 // If we hit the end of the block, check whether EFLAGS is live into a
12019 // successor.
12020 if (miI == BB->end()) {
12021 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
12022 sEnd = BB->succ_end();
12023 sItr != sEnd; ++sItr) {
12024 MachineBasicBlock* succ = *sItr;
12025 if (succ->isLiveIn(X86::EFLAGS))
12026 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000012027 }
12028 }
12029
Lang Hames6e3f7e42012-02-03 01:13:49 +000012030 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
12031 // out. SelectMI should have a kill flag on EFLAGS.
12032 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000012033 return true;
12034}
12035
Evan Cheng60c07e12006-07-05 22:17:51 +000012036MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012037X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012038 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012039 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12040 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012041
Chris Lattner52600972009-09-02 05:57:00 +000012042 // To "insert" a SELECT_CC instruction, we actually have to insert the
12043 // diamond control-flow pattern. The incoming instruction knows the
12044 // destination vreg to set, the condition code register to branch on, the
12045 // true/false values to select between, and a branch opcode to use.
12046 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12047 MachineFunction::iterator It = BB;
12048 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012049
Chris Lattner52600972009-09-02 05:57:00 +000012050 // thisMBB:
12051 // ...
12052 // TrueVal = ...
12053 // cmpTY ccX, r1, r2
12054 // bCC copy1MBB
12055 // fallthrough --> copy0MBB
12056 MachineBasicBlock *thisMBB = BB;
12057 MachineFunction *F = BB->getParent();
12058 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12059 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012060 F->insert(It, copy0MBB);
12061 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012062
Bill Wendling730c07e2010-06-25 20:48:10 +000012063 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12064 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000012065 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
12066 if (!MI->killsRegister(X86::EFLAGS) &&
12067 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
12068 copy0MBB->addLiveIn(X86::EFLAGS);
12069 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012070 }
12071
Dan Gohman14152b42010-07-06 20:24:04 +000012072 // Transfer the remainder of BB and its successor edges to sinkMBB.
12073 sinkMBB->splice(sinkMBB->begin(), BB,
12074 llvm::next(MachineBasicBlock::iterator(MI)),
12075 BB->end());
12076 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12077
12078 // Add the true and fallthrough blocks as its successors.
12079 BB->addSuccessor(copy0MBB);
12080 BB->addSuccessor(sinkMBB);
12081
12082 // Create the conditional branch instruction.
12083 unsigned Opc =
12084 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12085 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12086
Chris Lattner52600972009-09-02 05:57:00 +000012087 // copy0MBB:
12088 // %FalseValue = ...
12089 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012090 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012091
Chris Lattner52600972009-09-02 05:57:00 +000012092 // sinkMBB:
12093 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12094 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012095 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12096 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012097 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12098 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12099
Dan Gohman14152b42010-07-06 20:24:04 +000012100 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012101 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012102}
12103
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012104MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012105X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12106 bool Is64Bit) const {
12107 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12108 DebugLoc DL = MI->getDebugLoc();
12109 MachineFunction *MF = BB->getParent();
12110 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12111
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012112 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012113
12114 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12115 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12116
12117 // BB:
12118 // ... [Till the alloca]
12119 // If stacklet is not large enough, jump to mallocMBB
12120 //
12121 // bumpMBB:
12122 // Allocate by subtracting from RSP
12123 // Jump to continueMBB
12124 //
12125 // mallocMBB:
12126 // Allocate by call to runtime
12127 //
12128 // continueMBB:
12129 // ...
12130 // [rest of original BB]
12131 //
12132
12133 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12134 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12135 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12136
12137 MachineRegisterInfo &MRI = MF->getRegInfo();
12138 const TargetRegisterClass *AddrRegClass =
12139 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12140
12141 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12142 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12143 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012144 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012145 sizeVReg = MI->getOperand(1).getReg(),
12146 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12147
12148 MachineFunction::iterator MBBIter = BB;
12149 ++MBBIter;
12150
12151 MF->insert(MBBIter, bumpMBB);
12152 MF->insert(MBBIter, mallocMBB);
12153 MF->insert(MBBIter, continueMBB);
12154
12155 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12156 (MachineBasicBlock::iterator(MI)), BB->end());
12157 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12158
12159 // Add code to the main basic block to check if the stack limit has been hit,
12160 // and if so, jump to mallocMBB otherwise to bumpMBB.
12161 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012162 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012163 .addReg(tmpSPVReg).addReg(sizeVReg);
12164 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000012165 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012166 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012167 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12168
12169 // bumpMBB simply decreases the stack pointer, since we know the current
12170 // stacklet has enough space.
12171 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012172 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012173 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012174 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012175 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12176
12177 // Calls into a routine in libgcc to allocate more space from the heap.
12178 if (Is64Bit) {
12179 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12180 .addReg(sizeVReg);
12181 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12182 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12183 } else {
12184 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12185 .addImm(12);
12186 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12187 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12188 .addExternalSymbol("__morestack_allocate_stack_space");
12189 }
12190
12191 if (!Is64Bit)
12192 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12193 .addImm(16);
12194
12195 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12196 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12197 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12198
12199 // Set up the CFG correctly.
12200 BB->addSuccessor(bumpMBB);
12201 BB->addSuccessor(mallocMBB);
12202 mallocMBB->addSuccessor(continueMBB);
12203 bumpMBB->addSuccessor(continueMBB);
12204
12205 // Take care of the PHI nodes.
12206 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12207 MI->getOperand(0).getReg())
12208 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12209 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12210
12211 // Delete the original pseudo instruction.
12212 MI->eraseFromParent();
12213
12214 // And we're done.
12215 return continueMBB;
12216}
12217
12218MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012219X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012220 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012221 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12222 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012223
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012224 assert(!Subtarget->isTargetEnvMacho());
12225
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012226 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12227 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012228
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012229 if (Subtarget->isTargetWin64()) {
12230 if (Subtarget->isTargetCygMing()) {
12231 // ___chkstk(Mingw64):
12232 // Clobbers R10, R11, RAX and EFLAGS.
12233 // Updates RSP.
12234 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12235 .addExternalSymbol("___chkstk")
12236 .addReg(X86::RAX, RegState::Implicit)
12237 .addReg(X86::RSP, RegState::Implicit)
12238 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12239 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12240 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12241 } else {
12242 // __chkstk(MSVCRT): does not update stack pointer.
12243 // Clobbers R10, R11 and EFLAGS.
12244 // FIXME: RAX(allocated size) might be reused and not killed.
12245 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12246 .addExternalSymbol("__chkstk")
12247 .addReg(X86::RAX, RegState::Implicit)
12248 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12249 // RAX has the offset to subtracted from RSP.
12250 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12251 .addReg(X86::RSP)
12252 .addReg(X86::RAX);
12253 }
12254 } else {
12255 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012256 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12257
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012258 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12259 .addExternalSymbol(StackProbeSymbol)
12260 .addReg(X86::EAX, RegState::Implicit)
12261 .addReg(X86::ESP, RegState::Implicit)
12262 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12263 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12264 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12265 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012266
Dan Gohman14152b42010-07-06 20:24:04 +000012267 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012268 return BB;
12269}
Chris Lattner52600972009-09-02 05:57:00 +000012270
12271MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012272X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12273 MachineBasicBlock *BB) const {
12274 // This is pretty easy. We're taking the value that we received from
12275 // our load from the relocation, sticking it in either RDI (x86-64)
12276 // or EAX and doing an indirect call. The return value will then
12277 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012278 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012279 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012280 DebugLoc DL = MI->getDebugLoc();
12281 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012282
12283 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012284 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012285
Eric Christopher30ef0e52010-06-03 04:07:48 +000012286 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012287 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12288 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012289 .addReg(X86::RIP)
12290 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012291 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012292 MI->getOperand(3).getTargetFlags())
12293 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012294 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012295 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012296 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012297 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12298 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012299 .addReg(0)
12300 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012301 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012302 MI->getOperand(3).getTargetFlags())
12303 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012304 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012305 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012306 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012307 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12308 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012309 .addReg(TII->getGlobalBaseReg(F))
12310 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012311 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012312 MI->getOperand(3).getTargetFlags())
12313 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012314 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012315 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012316 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012317
Dan Gohman14152b42010-07-06 20:24:04 +000012318 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012319 return BB;
12320}
12321
12322MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012323X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012324 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012325 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012326 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012327 case X86::TAILJMPd64:
12328 case X86::TAILJMPr64:
12329 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000012330 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012331 case X86::TCRETURNdi64:
12332 case X86::TCRETURNri64:
12333 case X86::TCRETURNmi64:
12334 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12335 // On AMD64, additional defs should be added before register allocation.
12336 if (!Subtarget->isTargetWin64()) {
12337 MI->addRegisterDefined(X86::RSI);
12338 MI->addRegisterDefined(X86::RDI);
12339 MI->addRegisterDefined(X86::XMM6);
12340 MI->addRegisterDefined(X86::XMM7);
12341 MI->addRegisterDefined(X86::XMM8);
12342 MI->addRegisterDefined(X86::XMM9);
12343 MI->addRegisterDefined(X86::XMM10);
12344 MI->addRegisterDefined(X86::XMM11);
12345 MI->addRegisterDefined(X86::XMM12);
12346 MI->addRegisterDefined(X86::XMM13);
12347 MI->addRegisterDefined(X86::XMM14);
12348 MI->addRegisterDefined(X86::XMM15);
12349 }
12350 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012351 case X86::WIN_ALLOCA:
12352 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012353 case X86::SEG_ALLOCA_32:
12354 return EmitLoweredSegAlloca(MI, BB, false);
12355 case X86::SEG_ALLOCA_64:
12356 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012357 case X86::TLSCall_32:
12358 case X86::TLSCall_64:
12359 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012360 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012361 case X86::CMOV_FR32:
12362 case X86::CMOV_FR64:
12363 case X86::CMOV_V4F32:
12364 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012365 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012366 case X86::CMOV_V8F32:
12367 case X86::CMOV_V4F64:
12368 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012369 case X86::CMOV_GR16:
12370 case X86::CMOV_GR32:
12371 case X86::CMOV_RFP32:
12372 case X86::CMOV_RFP64:
12373 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012374 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012375
Dale Johannesen849f2142007-07-03 00:53:03 +000012376 case X86::FP32_TO_INT16_IN_MEM:
12377 case X86::FP32_TO_INT32_IN_MEM:
12378 case X86::FP32_TO_INT64_IN_MEM:
12379 case X86::FP64_TO_INT16_IN_MEM:
12380 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012381 case X86::FP64_TO_INT64_IN_MEM:
12382 case X86::FP80_TO_INT16_IN_MEM:
12383 case X86::FP80_TO_INT32_IN_MEM:
12384 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012385 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12386 DebugLoc DL = MI->getDebugLoc();
12387
Evan Cheng60c07e12006-07-05 22:17:51 +000012388 // Change the floating point control register to use "round towards zero"
12389 // mode when truncating to an integer value.
12390 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012391 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012392 addFrameReference(BuildMI(*BB, MI, DL,
12393 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012394
12395 // Load the old value of the high byte of the control word...
12396 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012397 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012398 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012399 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012400
12401 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012402 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012403 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012404
12405 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012406 addFrameReference(BuildMI(*BB, MI, DL,
12407 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012408
12409 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012410 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012411 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012412
12413 // Get the X86 opcode to use.
12414 unsigned Opc;
12415 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012416 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012417 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12418 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12419 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12420 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12421 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12422 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012423 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12424 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12425 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012426 }
12427
12428 X86AddressMode AM;
12429 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012430 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012431 AM.BaseType = X86AddressMode::RegBase;
12432 AM.Base.Reg = Op.getReg();
12433 } else {
12434 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012435 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012436 }
12437 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012438 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012439 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012440 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012441 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012442 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012443 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012444 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012445 AM.GV = Op.getGlobal();
12446 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012447 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012448 }
Dan Gohman14152b42010-07-06 20:24:04 +000012449 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012450 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012451
12452 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012453 addFrameReference(BuildMI(*BB, MI, DL,
12454 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012455
Dan Gohman14152b42010-07-06 20:24:04 +000012456 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012457 return BB;
12458 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012459 // String/text processing lowering.
12460 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012461 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012462 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12463 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012464 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012465 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12466 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012467 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012468 return EmitPCMP(MI, BB, 5, false /* in mem */);
12469 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012470 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012471 return EmitPCMP(MI, BB, 5, true /* in mem */);
12472
Eric Christopher228232b2010-11-30 07:20:12 +000012473 // Thread synchronization.
12474 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012475 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012476 case X86::MWAIT:
12477 return EmitMwait(MI, BB);
12478
Eric Christopherb120ab42009-08-18 22:50:32 +000012479 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012480 case X86::ATOMAND32:
12481 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012482 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012483 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012484 X86::NOT32r, X86::EAX,
12485 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012486 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012487 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12488 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012489 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012490 X86::NOT32r, X86::EAX,
12491 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012492 case X86::ATOMXOR32:
12493 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012494 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012495 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012496 X86::NOT32r, X86::EAX,
12497 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012498 case X86::ATOMNAND32:
12499 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012500 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012501 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012502 X86::NOT32r, X86::EAX,
12503 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012504 case X86::ATOMMIN32:
12505 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12506 case X86::ATOMMAX32:
12507 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12508 case X86::ATOMUMIN32:
12509 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12510 case X86::ATOMUMAX32:
12511 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012512
12513 case X86::ATOMAND16:
12514 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12515 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012516 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012517 X86::NOT16r, X86::AX,
12518 X86::GR16RegisterClass);
12519 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012520 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012521 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012522 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012523 X86::NOT16r, X86::AX,
12524 X86::GR16RegisterClass);
12525 case X86::ATOMXOR16:
12526 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12527 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012528 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012529 X86::NOT16r, X86::AX,
12530 X86::GR16RegisterClass);
12531 case X86::ATOMNAND16:
12532 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12533 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012534 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012535 X86::NOT16r, X86::AX,
12536 X86::GR16RegisterClass, true);
12537 case X86::ATOMMIN16:
12538 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12539 case X86::ATOMMAX16:
12540 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12541 case X86::ATOMUMIN16:
12542 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12543 case X86::ATOMUMAX16:
12544 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12545
12546 case X86::ATOMAND8:
12547 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12548 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012549 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012550 X86::NOT8r, X86::AL,
12551 X86::GR8RegisterClass);
12552 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012553 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012554 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012555 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012556 X86::NOT8r, X86::AL,
12557 X86::GR8RegisterClass);
12558 case X86::ATOMXOR8:
12559 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12560 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012561 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012562 X86::NOT8r, X86::AL,
12563 X86::GR8RegisterClass);
12564 case X86::ATOMNAND8:
12565 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12566 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012567 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012568 X86::NOT8r, X86::AL,
12569 X86::GR8RegisterClass, true);
12570 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012571 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012572 case X86::ATOMAND64:
12573 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012574 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012575 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012576 X86::NOT64r, X86::RAX,
12577 X86::GR64RegisterClass);
12578 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012579 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12580 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012581 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012582 X86::NOT64r, X86::RAX,
12583 X86::GR64RegisterClass);
12584 case X86::ATOMXOR64:
12585 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012586 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012587 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012588 X86::NOT64r, X86::RAX,
12589 X86::GR64RegisterClass);
12590 case X86::ATOMNAND64:
12591 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12592 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012593 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012594 X86::NOT64r, X86::RAX,
12595 X86::GR64RegisterClass, true);
12596 case X86::ATOMMIN64:
12597 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12598 case X86::ATOMMAX64:
12599 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12600 case X86::ATOMUMIN64:
12601 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12602 case X86::ATOMUMAX64:
12603 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012604
12605 // This group does 64-bit operations on a 32-bit host.
12606 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012607 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012608 X86::AND32rr, X86::AND32rr,
12609 X86::AND32ri, X86::AND32ri,
12610 false);
12611 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012612 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012613 X86::OR32rr, X86::OR32rr,
12614 X86::OR32ri, X86::OR32ri,
12615 false);
12616 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012617 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012618 X86::XOR32rr, X86::XOR32rr,
12619 X86::XOR32ri, X86::XOR32ri,
12620 false);
12621 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012622 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012623 X86::AND32rr, X86::AND32rr,
12624 X86::AND32ri, X86::AND32ri,
12625 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012626 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012627 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012628 X86::ADD32rr, X86::ADC32rr,
12629 X86::ADD32ri, X86::ADC32ri,
12630 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012631 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012632 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012633 X86::SUB32rr, X86::SBB32rr,
12634 X86::SUB32ri, X86::SBB32ri,
12635 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012636 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012637 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012638 X86::MOV32rr, X86::MOV32rr,
12639 X86::MOV32ri, X86::MOV32ri,
12640 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012641 case X86::VASTART_SAVE_XMM_REGS:
12642 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012643
12644 case X86::VAARG_64:
12645 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012646 }
12647}
12648
12649//===----------------------------------------------------------------------===//
12650// X86 Optimization Hooks
12651//===----------------------------------------------------------------------===//
12652
Dan Gohman475871a2008-07-27 21:46:04 +000012653void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012654 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012655 APInt &KnownZero,
12656 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012657 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012658 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012659 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012660 assert((Opc >= ISD::BUILTIN_OP_END ||
12661 Opc == ISD::INTRINSIC_WO_CHAIN ||
12662 Opc == ISD::INTRINSIC_W_CHAIN ||
12663 Opc == ISD::INTRINSIC_VOID) &&
12664 "Should use MaskedValueIsZero if you don't know whether Op"
12665 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012666
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012667 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012668 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012669 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012670 case X86ISD::ADD:
12671 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012672 case X86ISD::ADC:
12673 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012674 case X86ISD::SMUL:
12675 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012676 case X86ISD::INC:
12677 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012678 case X86ISD::OR:
12679 case X86ISD::XOR:
12680 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012681 // These nodes' second result is a boolean.
12682 if (Op.getResNo() == 0)
12683 break;
12684 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012685 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012686 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12687 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012688 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012689 case ISD::INTRINSIC_WO_CHAIN: {
12690 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12691 unsigned NumLoBits = 0;
12692 switch (IntId) {
12693 default: break;
12694 case Intrinsic::x86_sse_movmsk_ps:
12695 case Intrinsic::x86_avx_movmsk_ps_256:
12696 case Intrinsic::x86_sse2_movmsk_pd:
12697 case Intrinsic::x86_avx_movmsk_pd_256:
12698 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000012699 case Intrinsic::x86_sse2_pmovmskb_128:
12700 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000012701 // High bits of movmskp{s|d}, pmovmskb are known zero.
12702 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000012703 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000012704 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12705 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12706 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12707 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12708 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12709 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000012710 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012711 }
12712 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12713 Mask.getBitWidth() - NumLoBits);
12714 break;
12715 }
12716 }
12717 break;
12718 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012719 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012720}
Chris Lattner259e97c2006-01-31 19:43:35 +000012721
Owen Andersonbc146b02010-09-21 20:42:50 +000012722unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12723 unsigned Depth) const {
12724 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12725 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12726 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012727
Owen Andersonbc146b02010-09-21 20:42:50 +000012728 // Fallback case.
12729 return 1;
12730}
12731
Evan Cheng206ee9d2006-07-07 08:33:52 +000012732/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012733/// node is a GlobalAddress + offset.
12734bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012735 const GlobalValue* &GA,
12736 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012737 if (N->getOpcode() == X86ISD::Wrapper) {
12738 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012739 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012740 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012741 return true;
12742 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012743 }
Evan Chengad4196b2008-05-12 19:56:52 +000012744 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012745}
12746
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012747/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12748/// same as extracting the high 128-bit part of 256-bit vector and then
12749/// inserting the result into the low part of a new 256-bit vector
12750static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12751 EVT VT = SVOp->getValueType(0);
12752 int NumElems = VT.getVectorNumElements();
12753
12754 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12755 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12756 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12757 SVOp->getMaskElt(j) >= 0)
12758 return false;
12759
12760 return true;
12761}
12762
12763/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12764/// same as extracting the low 128-bit part of 256-bit vector and then
12765/// inserting the result into the high part of a new 256-bit vector
12766static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12767 EVT VT = SVOp->getValueType(0);
12768 int NumElems = VT.getVectorNumElements();
12769
12770 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12771 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12772 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12773 SVOp->getMaskElt(j) >= 0)
12774 return false;
12775
12776 return true;
12777}
12778
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012779/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12780static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000012781 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000012782 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012783 DebugLoc dl = N->getDebugLoc();
12784 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12785 SDValue V1 = SVOp->getOperand(0);
12786 SDValue V2 = SVOp->getOperand(1);
12787 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012788 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012789
12790 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12791 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12792 //
12793 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012794 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012795 // V UNDEF BUILD_VECTOR UNDEF
12796 // \ / \ /
12797 // CONCAT_VECTOR CONCAT_VECTOR
12798 // \ /
12799 // \ /
12800 // RESULT: V + zero extended
12801 //
12802 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12803 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12804 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12805 return SDValue();
12806
12807 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12808 return SDValue();
12809
12810 // To match the shuffle mask, the first half of the mask should
12811 // be exactly the first vector, and all the rest a splat with the
12812 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012813 for (int i = 0; i < NumElems/2; ++i)
12814 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12815 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12816 return SDValue();
12817
Chad Rosier3d1161e2012-01-03 21:05:52 +000012818 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
12819 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
12820 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
12821 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
12822 SDValue ResNode =
12823 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
12824 Ld->getMemoryVT(),
12825 Ld->getPointerInfo(),
12826 Ld->getAlignment(),
12827 false/*isVolatile*/, true/*ReadMem*/,
12828 false/*WriteMem*/);
12829 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
12830 }
12831
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012832 // Emit a zeroed vector and insert the desired subvector on its
12833 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000012834 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012835 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12836 DAG.getConstant(0, MVT::i32), DAG, dl);
12837 return DCI.CombineTo(N, InsV);
12838 }
12839
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012840 //===--------------------------------------------------------------------===//
12841 // Combine some shuffles into subvector extracts and inserts:
12842 //
12843
12844 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12845 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12846 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12847 DAG, dl);
12848 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12849 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12850 return DCI.CombineTo(N, InsV);
12851 }
12852
12853 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12854 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12855 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12856 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12857 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12858 return DCI.CombineTo(N, InsV);
12859 }
12860
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012861 return SDValue();
12862}
12863
12864/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012865static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012866 TargetLowering::DAGCombinerInfo &DCI,
12867 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012868 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012869 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012870
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012871 // Don't create instructions with illegal types after legalize types has run.
12872 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12873 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12874 return SDValue();
12875
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012876 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12877 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12878 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000012879 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012880
12881 // Only handle 128 wide vector from here on.
12882 if (VT.getSizeInBits() != 128)
12883 return SDValue();
12884
12885 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12886 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12887 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012888 SmallVector<SDValue, 16> Elts;
12889 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012890 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012891
Nate Begemanfdea31a2010-03-24 20:49:50 +000012892 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012893}
Evan Chengd880b972008-05-09 21:53:03 +000012894
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000012895
12896/// PerformTruncateCombine - Converts truncate operation to
12897/// a sequence of vector shuffle operations.
12898/// It is possible when we truncate 256-bit vector to 128-bit vector
12899
12900SDValue X86TargetLowering::PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
12901 DAGCombinerInfo &DCI) const {
12902 if (!DCI.isBeforeLegalizeOps())
12903 return SDValue();
12904
12905 if (!Subtarget->hasAVX()) return SDValue();
12906
12907 EVT VT = N->getValueType(0);
12908 SDValue Op = N->getOperand(0);
12909 EVT OpVT = Op.getValueType();
12910 DebugLoc dl = N->getDebugLoc();
12911
12912 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
12913
12914 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
12915 DAG.getIntPtrConstant(0));
12916
12917 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
12918 DAG.getIntPtrConstant(2));
12919
12920 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
12921 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
12922
12923 // PSHUFD
Elena Demikhovsky73252572012-02-01 10:33:05 +000012924 int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000012925
12926 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, DAG.getUNDEF(VT),
Elena Demikhovsky73252572012-02-01 10:33:05 +000012927 ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000012928 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, DAG.getUNDEF(VT),
Elena Demikhovsky73252572012-02-01 10:33:05 +000012929 ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000012930
12931 // MOVLHPS
Elena Demikhovsky73252572012-02-01 10:33:05 +000012932 int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000012933
Elena Demikhovsky73252572012-02-01 10:33:05 +000012934 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000012935 }
12936 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
12937
12938 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
12939 DAG.getIntPtrConstant(0));
12940
12941 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
12942 DAG.getIntPtrConstant(4));
12943
12944 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
12945 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
12946
12947 // PSHUFB
Elena Demikhovsky73252572012-02-01 10:33:05 +000012948 int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
12949 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000012950
12951 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo,
12952 DAG.getUNDEF(MVT::v16i8),
Elena Demikhovsky73252572012-02-01 10:33:05 +000012953 ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000012954 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi,
12955 DAG.getUNDEF(MVT::v16i8),
Elena Demikhovsky73252572012-02-01 10:33:05 +000012956 ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000012957
12958 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
12959 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
12960
12961 // MOVLHPS
Elena Demikhovsky73252572012-02-01 10:33:05 +000012962 int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000012963
Elena Demikhovsky73252572012-02-01 10:33:05 +000012964 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000012965 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000012966 }
12967
12968 return SDValue();
12969}
12970
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012971/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12972/// generation and convert it from being a bunch of shuffles and extracts
12973/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012974static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12975 const TargetLowering &TLI) {
12976 SDValue InputVector = N->getOperand(0);
12977
12978 // Only operate on vectors of 4 elements, where the alternative shuffling
12979 // gets to be more expensive.
12980 if (InputVector.getValueType() != MVT::v4i32)
12981 return SDValue();
12982
12983 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12984 // single use which is a sign-extend or zero-extend, and all elements are
12985 // used.
12986 SmallVector<SDNode *, 4> Uses;
12987 unsigned ExtractedElements = 0;
12988 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12989 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12990 if (UI.getUse().getResNo() != InputVector.getResNo())
12991 return SDValue();
12992
12993 SDNode *Extract = *UI;
12994 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12995 return SDValue();
12996
12997 if (Extract->getValueType(0) != MVT::i32)
12998 return SDValue();
12999 if (!Extract->hasOneUse())
13000 return SDValue();
13001 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
13002 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
13003 return SDValue();
13004 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
13005 return SDValue();
13006
13007 // Record which element was extracted.
13008 ExtractedElements |=
13009 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
13010
13011 Uses.push_back(Extract);
13012 }
13013
13014 // If not all the elements were used, this may not be worthwhile.
13015 if (ExtractedElements != 15)
13016 return SDValue();
13017
13018 // Ok, we've now decided to do the transformation.
13019 DebugLoc dl = InputVector.getDebugLoc();
13020
13021 // Store the value to a temporary stack slot.
13022 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013023 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13024 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013025
13026 // Replace each use (extract) with a load of the appropriate element.
13027 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13028 UE = Uses.end(); UI != UE; ++UI) {
13029 SDNode *Extract = *UI;
13030
Nadav Rotem86694292011-05-17 08:31:57 +000013031 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013032 SDValue Idx = Extract->getOperand(1);
13033 unsigned EltSize =
13034 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
13035 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
13036 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
13037
Nadav Rotem86694292011-05-17 08:31:57 +000013038 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013039 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013040
13041 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013042 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013043 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013044 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013045
13046 // Replace the exact with the load.
13047 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13048 }
13049
13050 // The replacement was made in place; don't return anything.
13051 return SDValue();
13052}
13053
Duncan Sands6bcd2192011-09-17 16:49:39 +000013054/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13055/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013056static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000013057 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013058 const X86Subtarget *Subtarget) {
13059 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013060 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013061 // Get the LHS/RHS of the select.
13062 SDValue LHS = N->getOperand(1);
13063 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013064 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013065
Dan Gohman670e5392009-09-21 18:03:22 +000013066 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013067 // instructions match the semantics of the common C idiom x<y?x:y but not
13068 // x<=y?x:y, because of how they handle negative zero (which can be
13069 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013070 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13071 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000013072 (Subtarget->hasSSE2() ||
13073 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013074 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013075
Chris Lattner47b4ce82009-03-11 05:48:52 +000013076 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013077 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013078 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13079 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013080 switch (CC) {
13081 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013082 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013083 // Converting this to a min would handle NaNs incorrectly, and swapping
13084 // the operands would cause it to handle comparisons between positive
13085 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013086 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013087 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013088 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13089 break;
13090 std::swap(LHS, RHS);
13091 }
Dan Gohman670e5392009-09-21 18:03:22 +000013092 Opcode = X86ISD::FMIN;
13093 break;
13094 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013095 // Converting this to a min would handle comparisons between positive
13096 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013097 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013098 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13099 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013100 Opcode = X86ISD::FMIN;
13101 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013102 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013103 // Converting this to a min would handle both negative zeros and NaNs
13104 // incorrectly, but we can swap the operands to fix both.
13105 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013106 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013107 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013108 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013109 Opcode = X86ISD::FMIN;
13110 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013111
Dan Gohman670e5392009-09-21 18:03:22 +000013112 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013113 // Converting this to a max would handle comparisons between positive
13114 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013115 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013116 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013117 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013118 Opcode = X86ISD::FMAX;
13119 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013120 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013121 // Converting this to a max would handle NaNs incorrectly, and swapping
13122 // the operands would cause it to handle comparisons between positive
13123 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013124 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013125 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013126 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13127 break;
13128 std::swap(LHS, RHS);
13129 }
Dan Gohman670e5392009-09-21 18:03:22 +000013130 Opcode = X86ISD::FMAX;
13131 break;
13132 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013133 // Converting this to a max would handle both negative zeros and NaNs
13134 // incorrectly, but we can swap the operands to fix both.
13135 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013136 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013137 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013138 case ISD::SETGE:
13139 Opcode = X86ISD::FMAX;
13140 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013141 }
Dan Gohman670e5392009-09-21 18:03:22 +000013142 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013143 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13144 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013145 switch (CC) {
13146 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013147 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013148 // Converting this to a min would handle comparisons between positive
13149 // and negative zero incorrectly, and swapping the operands would
13150 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013151 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013152 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013153 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013154 break;
13155 std::swap(LHS, RHS);
13156 }
Dan Gohman670e5392009-09-21 18:03:22 +000013157 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013158 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013159 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013160 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013161 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013162 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13163 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013164 Opcode = X86ISD::FMIN;
13165 break;
13166 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013167 // Converting this to a min would handle both negative zeros and NaNs
13168 // incorrectly, but we can swap the operands to fix both.
13169 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013170 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013171 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013172 case ISD::SETGE:
13173 Opcode = X86ISD::FMIN;
13174 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013175
Dan Gohman670e5392009-09-21 18:03:22 +000013176 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013177 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013178 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013179 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013180 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013181 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013182 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013183 // Converting this to a max would handle comparisons between positive
13184 // and negative zero incorrectly, and swapping the operands would
13185 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013186 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013187 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013188 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013189 break;
13190 std::swap(LHS, RHS);
13191 }
Dan Gohman670e5392009-09-21 18:03:22 +000013192 Opcode = X86ISD::FMAX;
13193 break;
13194 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013195 // Converting this to a max would handle both negative zeros and NaNs
13196 // incorrectly, but we can swap the operands to fix both.
13197 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013198 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013199 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013200 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013201 Opcode = X86ISD::FMAX;
13202 break;
13203 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013204 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013205
Chris Lattner47b4ce82009-03-11 05:48:52 +000013206 if (Opcode)
13207 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013208 }
Eric Christopherfd179292009-08-27 18:07:15 +000013209
Chris Lattnerd1980a52009-03-12 06:52:53 +000013210 // If this is a select between two integer constants, try to do some
13211 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013212 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13213 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013214 // Don't do this for crazy integer types.
13215 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13216 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013217 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013218 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013219
Chris Lattnercee56e72009-03-13 05:53:31 +000013220 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013221 // Efficiently invertible.
13222 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13223 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13224 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13225 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013226 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013227 }
Eric Christopherfd179292009-08-27 18:07:15 +000013228
Chris Lattnerd1980a52009-03-12 06:52:53 +000013229 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013230 if (FalseC->getAPIntValue() == 0 &&
13231 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013232 if (NeedsCondInvert) // Invert the condition if needed.
13233 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13234 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013235
Chris Lattnerd1980a52009-03-12 06:52:53 +000013236 // Zero extend the condition if needed.
13237 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013238
Chris Lattnercee56e72009-03-13 05:53:31 +000013239 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013240 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013241 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013242 }
Eric Christopherfd179292009-08-27 18:07:15 +000013243
Chris Lattner97a29a52009-03-13 05:22:11 +000013244 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013245 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013246 if (NeedsCondInvert) // Invert the condition if needed.
13247 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13248 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013249
Chris Lattner97a29a52009-03-13 05:22:11 +000013250 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013251 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13252 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013253 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013254 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013255 }
Eric Christopherfd179292009-08-27 18:07:15 +000013256
Chris Lattnercee56e72009-03-13 05:53:31 +000013257 // Optimize cases that will turn into an LEA instruction. This requires
13258 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013259 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013260 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013261 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013262
Chris Lattnercee56e72009-03-13 05:53:31 +000013263 bool isFastMultiplier = false;
13264 if (Diff < 10) {
13265 switch ((unsigned char)Diff) {
13266 default: break;
13267 case 1: // result = add base, cond
13268 case 2: // result = lea base( , cond*2)
13269 case 3: // result = lea base(cond, cond*2)
13270 case 4: // result = lea base( , cond*4)
13271 case 5: // result = lea base(cond, cond*4)
13272 case 8: // result = lea base( , cond*8)
13273 case 9: // result = lea base(cond, cond*8)
13274 isFastMultiplier = true;
13275 break;
13276 }
13277 }
Eric Christopherfd179292009-08-27 18:07:15 +000013278
Chris Lattnercee56e72009-03-13 05:53:31 +000013279 if (isFastMultiplier) {
13280 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13281 if (NeedsCondInvert) // Invert the condition if needed.
13282 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13283 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013284
Chris Lattnercee56e72009-03-13 05:53:31 +000013285 // Zero extend the condition if needed.
13286 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13287 Cond);
13288 // Scale the condition by the difference.
13289 if (Diff != 1)
13290 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13291 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013292
Chris Lattnercee56e72009-03-13 05:53:31 +000013293 // Add the base if non-zero.
13294 if (FalseC->getAPIntValue() != 0)
13295 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13296 SDValue(FalseC, 0));
13297 return Cond;
13298 }
Eric Christopherfd179292009-08-27 18:07:15 +000013299 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013300 }
13301 }
Eric Christopherfd179292009-08-27 18:07:15 +000013302
Evan Cheng56f582d2012-01-04 01:41:39 +000013303 // Canonicalize max and min:
13304 // (x > y) ? x : y -> (x >= y) ? x : y
13305 // (x < y) ? x : y -> (x <= y) ? x : y
13306 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
13307 // the need for an extra compare
13308 // against zero. e.g.
13309 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
13310 // subl %esi, %edi
13311 // testl %edi, %edi
13312 // movl $0, %eax
13313 // cmovgl %edi, %eax
13314 // =>
13315 // xorl %eax, %eax
13316 // subl %esi, $edi
13317 // cmovsl %eax, %edi
13318 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
13319 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13320 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
13321 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
13322 switch (CC) {
13323 default: break;
13324 case ISD::SETLT:
13325 case ISD::SETGT: {
13326 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
13327 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
13328 Cond.getOperand(0), Cond.getOperand(1), NewCC);
13329 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
13330 }
13331 }
13332 }
13333
Nadav Rotemcc616562012-01-15 19:27:55 +000013334 // If we know that this node is legal then we know that it is going to be
13335 // matched by one of the SSE/AVX BLEND instructions. These instructions only
13336 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
13337 // to simplify previous instructions.
13338 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13339 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
13340 !DCI.isBeforeLegalize() &&
13341 TLI.isOperationLegal(ISD::VSELECT, VT)) {
13342 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
13343 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
13344 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
13345
13346 APInt KnownZero, KnownOne;
13347 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
13348 DCI.isBeforeLegalizeOps());
13349 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
13350 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
13351 DCI.CommitTargetLoweringOpt(TLO);
13352 }
13353
Dan Gohman475871a2008-07-27 21:46:04 +000013354 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013355}
13356
Chris Lattnerd1980a52009-03-12 06:52:53 +000013357/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13358static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13359 TargetLowering::DAGCombinerInfo &DCI) {
13360 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013361
Chris Lattnerd1980a52009-03-12 06:52:53 +000013362 // If the flag operand isn't dead, don't touch this CMOV.
13363 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13364 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013365
Evan Chengb5a55d92011-05-24 01:48:22 +000013366 SDValue FalseOp = N->getOperand(0);
13367 SDValue TrueOp = N->getOperand(1);
13368 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13369 SDValue Cond = N->getOperand(3);
13370 if (CC == X86::COND_E || CC == X86::COND_NE) {
13371 switch (Cond.getOpcode()) {
13372 default: break;
13373 case X86ISD::BSR:
13374 case X86ISD::BSF:
13375 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13376 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13377 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13378 }
13379 }
13380
Chris Lattnerd1980a52009-03-12 06:52:53 +000013381 // If this is a select between two integer constants, try to do some
13382 // optimizations. Note that the operands are ordered the opposite of SELECT
13383 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013384 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13385 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013386 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13387 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013388 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13389 CC = X86::GetOppositeBranchCondition(CC);
13390 std::swap(TrueC, FalseC);
13391 }
Eric Christopherfd179292009-08-27 18:07:15 +000013392
Chris Lattnerd1980a52009-03-12 06:52:53 +000013393 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013394 // This is efficient for any integer data type (including i8/i16) and
13395 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013396 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013397 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13398 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013399
Chris Lattnerd1980a52009-03-12 06:52:53 +000013400 // Zero extend the condition if needed.
13401 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013402
Chris Lattnerd1980a52009-03-12 06:52:53 +000013403 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13404 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013405 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013406 if (N->getNumValues() == 2) // Dead flag value?
13407 return DCI.CombineTo(N, Cond, SDValue());
13408 return Cond;
13409 }
Eric Christopherfd179292009-08-27 18:07:15 +000013410
Chris Lattnercee56e72009-03-13 05:53:31 +000013411 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13412 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013413 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013414 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13415 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013416
Chris Lattner97a29a52009-03-13 05:22:11 +000013417 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013418 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13419 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013420 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13421 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013422
Chris Lattner97a29a52009-03-13 05:22:11 +000013423 if (N->getNumValues() == 2) // Dead flag value?
13424 return DCI.CombineTo(N, Cond, SDValue());
13425 return Cond;
13426 }
Eric Christopherfd179292009-08-27 18:07:15 +000013427
Chris Lattnercee56e72009-03-13 05:53:31 +000013428 // Optimize cases that will turn into an LEA instruction. This requires
13429 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013430 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013431 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013432 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013433
Chris Lattnercee56e72009-03-13 05:53:31 +000013434 bool isFastMultiplier = false;
13435 if (Diff < 10) {
13436 switch ((unsigned char)Diff) {
13437 default: break;
13438 case 1: // result = add base, cond
13439 case 2: // result = lea base( , cond*2)
13440 case 3: // result = lea base(cond, cond*2)
13441 case 4: // result = lea base( , cond*4)
13442 case 5: // result = lea base(cond, cond*4)
13443 case 8: // result = lea base( , cond*8)
13444 case 9: // result = lea base(cond, cond*8)
13445 isFastMultiplier = true;
13446 break;
13447 }
13448 }
Eric Christopherfd179292009-08-27 18:07:15 +000013449
Chris Lattnercee56e72009-03-13 05:53:31 +000013450 if (isFastMultiplier) {
13451 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013452 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13453 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013454 // Zero extend the condition if needed.
13455 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13456 Cond);
13457 // Scale the condition by the difference.
13458 if (Diff != 1)
13459 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13460 DAG.getConstant(Diff, Cond.getValueType()));
13461
13462 // Add the base if non-zero.
13463 if (FalseC->getAPIntValue() != 0)
13464 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13465 SDValue(FalseC, 0));
13466 if (N->getNumValues() == 2) // Dead flag value?
13467 return DCI.CombineTo(N, Cond, SDValue());
13468 return Cond;
13469 }
Eric Christopherfd179292009-08-27 18:07:15 +000013470 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013471 }
13472 }
13473 return SDValue();
13474}
13475
13476
Evan Cheng0b0cd912009-03-28 05:57:29 +000013477/// PerformMulCombine - Optimize a single multiply with constant into two
13478/// in order to implement it with two cheaper instructions, e.g.
13479/// LEA + SHL, LEA + LEA.
13480static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13481 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013482 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13483 return SDValue();
13484
Owen Andersone50ed302009-08-10 22:56:29 +000013485 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013486 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013487 return SDValue();
13488
13489 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13490 if (!C)
13491 return SDValue();
13492 uint64_t MulAmt = C->getZExtValue();
13493 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13494 return SDValue();
13495
13496 uint64_t MulAmt1 = 0;
13497 uint64_t MulAmt2 = 0;
13498 if ((MulAmt % 9) == 0) {
13499 MulAmt1 = 9;
13500 MulAmt2 = MulAmt / 9;
13501 } else if ((MulAmt % 5) == 0) {
13502 MulAmt1 = 5;
13503 MulAmt2 = MulAmt / 5;
13504 } else if ((MulAmt % 3) == 0) {
13505 MulAmt1 = 3;
13506 MulAmt2 = MulAmt / 3;
13507 }
13508 if (MulAmt2 &&
13509 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13510 DebugLoc DL = N->getDebugLoc();
13511
13512 if (isPowerOf2_64(MulAmt2) &&
13513 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13514 // If second multiplifer is pow2, issue it first. We want the multiply by
13515 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13516 // is an add.
13517 std::swap(MulAmt1, MulAmt2);
13518
13519 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013520 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013521 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013522 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013523 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013524 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013525 DAG.getConstant(MulAmt1, VT));
13526
Eric Christopherfd179292009-08-27 18:07:15 +000013527 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013528 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013529 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013530 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013531 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013532 DAG.getConstant(MulAmt2, VT));
13533
13534 // Do not add new nodes to DAG combiner worklist.
13535 DCI.CombineTo(N, NewMul, false);
13536 }
13537 return SDValue();
13538}
13539
Evan Chengad9c0a32009-12-15 00:53:42 +000013540static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13541 SDValue N0 = N->getOperand(0);
13542 SDValue N1 = N->getOperand(1);
13543 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13544 EVT VT = N0.getValueType();
13545
13546 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13547 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013548 if (VT.isInteger() && !VT.isVector() &&
13549 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013550 N0.getOperand(1).getOpcode() == ISD::Constant) {
13551 SDValue N00 = N0.getOperand(0);
13552 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13553 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13554 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13555 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13556 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13557 APInt ShAmt = N1C->getAPIntValue();
13558 Mask = Mask.shl(ShAmt);
13559 if (Mask != 0)
13560 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13561 N00, DAG.getConstant(Mask, VT));
13562 }
13563 }
13564
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013565
13566 // Hardware support for vector shifts is sparse which makes us scalarize the
13567 // vector operations in many cases. Also, on sandybridge ADD is faster than
13568 // shl.
13569 // (shl V, 1) -> add V,V
13570 if (isSplatVector(N1.getNode())) {
13571 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13572 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13573 // We shift all of the values by one. In many cases we do not have
13574 // hardware support for this operation. This is better expressed as an ADD
13575 // of two values.
13576 if (N1C && (1 == N1C->getZExtValue())) {
13577 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13578 }
13579 }
13580
Evan Chengad9c0a32009-12-15 00:53:42 +000013581 return SDValue();
13582}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013583
Nate Begeman740ab032009-01-26 00:52:55 +000013584/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13585/// when possible.
13586static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000013587 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000013588 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013589 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013590 if (N->getOpcode() == ISD::SHL) {
13591 SDValue V = PerformSHLCombine(N, DAG);
13592 if (V.getNode()) return V;
13593 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013594
Nate Begeman740ab032009-01-26 00:52:55 +000013595 // On X86 with SSE2 support, we can transform this to a vector shift if
13596 // all elements are shifted by the same amount. We can't do this in legalize
13597 // because the a constant vector is typically transformed to a constant pool
13598 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000013599 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013600 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013601
Craig Topper7be5dfd2011-11-12 09:58:49 +000013602 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
13603 (!Subtarget->hasAVX2() ||
13604 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013605 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013606
Mon P Wang3becd092009-01-28 08:12:05 +000013607 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013608 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013609 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013610 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013611 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13612 unsigned NumElts = VT.getVectorNumElements();
13613 unsigned i = 0;
13614 for (; i != NumElts; ++i) {
13615 SDValue Arg = ShAmtOp.getOperand(i);
13616 if (Arg.getOpcode() == ISD::UNDEF) continue;
13617 BaseShAmt = Arg;
13618 break;
13619 }
Craig Topper37c26772012-01-17 04:44:50 +000013620 // Handle the case where the build_vector is all undef
13621 // FIXME: Should DAG allow this?
13622 if (i == NumElts)
13623 return SDValue();
13624
Mon P Wang3becd092009-01-28 08:12:05 +000013625 for (; i != NumElts; ++i) {
13626 SDValue Arg = ShAmtOp.getOperand(i);
13627 if (Arg.getOpcode() == ISD::UNDEF) continue;
13628 if (Arg != BaseShAmt) {
13629 return SDValue();
13630 }
13631 }
13632 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013633 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013634 SDValue InVec = ShAmtOp.getOperand(0);
13635 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13636 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13637 unsigned i = 0;
13638 for (; i != NumElts; ++i) {
13639 SDValue Arg = InVec.getOperand(i);
13640 if (Arg.getOpcode() == ISD::UNDEF) continue;
13641 BaseShAmt = Arg;
13642 break;
13643 }
13644 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13645 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013646 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013647 if (C->getZExtValue() == SplatIdx)
13648 BaseShAmt = InVec.getOperand(1);
13649 }
13650 }
Mon P Wang845b1892012-02-01 22:15:20 +000013651 if (BaseShAmt.getNode() == 0) {
13652 // Don't create instructions with illegal types after legalize
13653 // types has run.
13654 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
13655 !DCI.isBeforeLegalize())
13656 return SDValue();
13657
Mon P Wangefa42202009-09-03 19:56:25 +000013658 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13659 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000013660 }
Mon P Wang3becd092009-01-28 08:12:05 +000013661 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013662 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013663
Mon P Wangefa42202009-09-03 19:56:25 +000013664 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013665 if (EltVT.bitsGT(MVT::i32))
13666 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13667 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013668 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013669
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013670 // The shift amount is identical so we can do a vector shift.
13671 SDValue ValOp = N->getOperand(0);
13672 switch (N->getOpcode()) {
13673 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013674 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013675 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000013676 switch (VT.getSimpleVT().SimpleTy) {
13677 default: return SDValue();
13678 case MVT::v2i64:
13679 case MVT::v4i32:
13680 case MVT::v8i16:
13681 case MVT::v4i64:
13682 case MVT::v8i32:
13683 case MVT::v16i16:
13684 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
13685 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013686 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000013687 switch (VT.getSimpleVT().SimpleTy) {
13688 default: return SDValue();
13689 case MVT::v4i32:
13690 case MVT::v8i16:
13691 case MVT::v8i32:
13692 case MVT::v16i16:
13693 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
13694 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013695 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000013696 switch (VT.getSimpleVT().SimpleTy) {
13697 default: return SDValue();
13698 case MVT::v2i64:
13699 case MVT::v4i32:
13700 case MVT::v8i16:
13701 case MVT::v4i64:
13702 case MVT::v8i32:
13703 case MVT::v16i16:
13704 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
13705 }
Nate Begeman740ab032009-01-26 00:52:55 +000013706 }
Nate Begeman740ab032009-01-26 00:52:55 +000013707}
13708
Nate Begemanb65c1752010-12-17 22:55:37 +000013709
Stuart Hastings865f0932011-06-03 23:53:54 +000013710// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13711// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13712// and friends. Likewise for OR -> CMPNEQSS.
13713static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13714 TargetLowering::DAGCombinerInfo &DCI,
13715 const X86Subtarget *Subtarget) {
13716 unsigned opcode;
13717
13718 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13719 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000013720 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013721 SDValue N0 = N->getOperand(0);
13722 SDValue N1 = N->getOperand(1);
13723 SDValue CMP0 = N0->getOperand(1);
13724 SDValue CMP1 = N1->getOperand(1);
13725 DebugLoc DL = N->getDebugLoc();
13726
13727 // The SETCCs should both refer to the same CMP.
13728 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13729 return SDValue();
13730
13731 SDValue CMP00 = CMP0->getOperand(0);
13732 SDValue CMP01 = CMP0->getOperand(1);
13733 EVT VT = CMP00.getValueType();
13734
13735 if (VT == MVT::f32 || VT == MVT::f64) {
13736 bool ExpectingFlags = false;
13737 // Check for any users that want flags:
13738 for (SDNode::use_iterator UI = N->use_begin(),
13739 UE = N->use_end();
13740 !ExpectingFlags && UI != UE; ++UI)
13741 switch (UI->getOpcode()) {
13742 default:
13743 case ISD::BR_CC:
13744 case ISD::BRCOND:
13745 case ISD::SELECT:
13746 ExpectingFlags = true;
13747 break;
13748 case ISD::CopyToReg:
13749 case ISD::SIGN_EXTEND:
13750 case ISD::ZERO_EXTEND:
13751 case ISD::ANY_EXTEND:
13752 break;
13753 }
13754
13755 if (!ExpectingFlags) {
13756 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13757 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13758
13759 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13760 X86::CondCode tmp = cc0;
13761 cc0 = cc1;
13762 cc1 = tmp;
13763 }
13764
13765 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13766 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13767 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13768 X86ISD::NodeType NTOperator = is64BitFP ?
13769 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13770 // FIXME: need symbolic constants for these magic numbers.
13771 // See X86ATTInstPrinter.cpp:printSSECC().
13772 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13773 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13774 DAG.getConstant(x86cc, MVT::i8));
13775 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13776 OnesOrZeroesF);
13777 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13778 DAG.getConstant(1, MVT::i32));
13779 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13780 return OneBitOfTruth;
13781 }
13782 }
13783 }
13784 }
13785 return SDValue();
13786}
13787
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013788/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13789/// so it can be folded inside ANDNP.
13790static bool CanFoldXORWithAllOnes(const SDNode *N) {
13791 EVT VT = N->getValueType(0);
13792
13793 // Match direct AllOnes for 128 and 256-bit vectors
13794 if (ISD::isBuildVectorAllOnes(N))
13795 return true;
13796
13797 // Look through a bit convert.
13798 if (N->getOpcode() == ISD::BITCAST)
13799 N = N->getOperand(0).getNode();
13800
13801 // Sometimes the operand may come from a insert_subvector building a 256-bit
13802 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013803 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013804 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13805 SDValue V1 = N->getOperand(0);
13806 SDValue V2 = N->getOperand(1);
13807
13808 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13809 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13810 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13811 ISD::isBuildVectorAllOnes(V2.getNode()))
13812 return true;
13813 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013814
13815 return false;
13816}
13817
Nate Begemanb65c1752010-12-17 22:55:37 +000013818static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13819 TargetLowering::DAGCombinerInfo &DCI,
13820 const X86Subtarget *Subtarget) {
13821 if (DCI.isBeforeLegalizeOps())
13822 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013823
Stuart Hastings865f0932011-06-03 23:53:54 +000013824 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13825 if (R.getNode())
13826 return R;
13827
Craig Topper54a11172011-10-14 07:06:56 +000013828 EVT VT = N->getValueType(0);
13829
Craig Topperb4c94572011-10-21 06:55:01 +000013830 // Create ANDN, BLSI, and BLSR instructions
13831 // BLSI is X & (-X)
13832 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013833 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13834 SDValue N0 = N->getOperand(0);
13835 SDValue N1 = N->getOperand(1);
13836 DebugLoc DL = N->getDebugLoc();
13837
13838 // Check LHS for not
13839 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13840 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13841 // Check RHS for not
13842 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13843 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13844
Craig Topperb4c94572011-10-21 06:55:01 +000013845 // Check LHS for neg
13846 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13847 isZero(N0.getOperand(0)))
13848 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13849
13850 // Check RHS for neg
13851 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13852 isZero(N1.getOperand(0)))
13853 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13854
13855 // Check LHS for X-1
13856 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13857 isAllOnes(N0.getOperand(1)))
13858 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13859
13860 // Check RHS for X-1
13861 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13862 isAllOnes(N1.getOperand(1)))
13863 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13864
Craig Topper54a11172011-10-14 07:06:56 +000013865 return SDValue();
13866 }
13867
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013868 // Want to form ANDNP nodes:
13869 // 1) In the hopes of then easily combining them with OR and AND nodes
13870 // to form PBLEND/PSIGN.
13871 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013872 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013873 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013874
Nate Begemanb65c1752010-12-17 22:55:37 +000013875 SDValue N0 = N->getOperand(0);
13876 SDValue N1 = N->getOperand(1);
13877 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013878
Nate Begemanb65c1752010-12-17 22:55:37 +000013879 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013880 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013881 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13882 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013883 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013884
13885 // Check RHS for vnot
13886 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013887 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13888 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013889 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013890
Nate Begemanb65c1752010-12-17 22:55:37 +000013891 return SDValue();
13892}
13893
Evan Cheng760d1942010-01-04 21:22:48 +000013894static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013895 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013896 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013897 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013898 return SDValue();
13899
Stuart Hastings865f0932011-06-03 23:53:54 +000013900 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13901 if (R.getNode())
13902 return R;
13903
Evan Cheng760d1942010-01-04 21:22:48 +000013904 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000013905
Evan Cheng760d1942010-01-04 21:22:48 +000013906 SDValue N0 = N->getOperand(0);
13907 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013908
Nate Begemanb65c1752010-12-17 22:55:37 +000013909 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000013910 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000013911 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000013912 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
13913 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013914
Craig Topper1666cb62011-11-19 07:07:26 +000013915 // Canonicalize pandn to RHS
13916 if (N0.getOpcode() == X86ISD::ANDNP)
13917 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000013918 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000013919 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
13920 SDValue Mask = N1.getOperand(0);
13921 SDValue X = N1.getOperand(1);
13922 SDValue Y;
13923 if (N0.getOperand(0) == Mask)
13924 Y = N0.getOperand(1);
13925 if (N0.getOperand(1) == Mask)
13926 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013927
Craig Topper1666cb62011-11-19 07:07:26 +000013928 // Check to see if the mask appeared in both the AND and ANDNP and
13929 if (!Y.getNode())
13930 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013931
Craig Topper1666cb62011-11-19 07:07:26 +000013932 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13933 if (Mask.getOpcode() != ISD::BITCAST ||
13934 X.getOpcode() != ISD::BITCAST ||
13935 Y.getOpcode() != ISD::BITCAST)
13936 return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013937
Craig Topper1666cb62011-11-19 07:07:26 +000013938 // Look through mask bitcast.
13939 Mask = Mask.getOperand(0);
13940 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013941
Craig Toppered2e13d2012-01-22 19:15:14 +000013942 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000013943 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13944 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000013945 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000013946 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000013947
13948 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000013949 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000013950 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13951 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13952 if ((SraAmt + 1) != EltBits)
13953 return SDValue();
13954
13955 DebugLoc DL = N->getDebugLoc();
13956
13957 // Now we know we at least have a plendvb with the mask val. See if
13958 // we can form a psignb/w/d.
13959 // psign = x.type == y.type == mask.type && y = sub(0, x);
13960 X = X.getOperand(0);
13961 Y = Y.getOperand(0);
13962 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13963 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000013964 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
13965 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
13966 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000013967 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000013968 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000013969 }
13970 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000013971 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000013972 return SDValue();
13973
13974 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
13975
13976 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
13977 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
13978 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000013979 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000013980 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000013981 }
13982 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013983
Craig Topper1666cb62011-11-19 07:07:26 +000013984 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
13985 return SDValue();
13986
Nate Begemanb65c1752010-12-17 22:55:37 +000013987 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013988 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13989 std::swap(N0, N1);
13990 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13991 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013992 if (!N0.hasOneUse() || !N1.hasOneUse())
13993 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013994
13995 SDValue ShAmt0 = N0.getOperand(1);
13996 if (ShAmt0.getValueType() != MVT::i8)
13997 return SDValue();
13998 SDValue ShAmt1 = N1.getOperand(1);
13999 if (ShAmt1.getValueType() != MVT::i8)
14000 return SDValue();
14001 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
14002 ShAmt0 = ShAmt0.getOperand(0);
14003 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
14004 ShAmt1 = ShAmt1.getOperand(0);
14005
14006 DebugLoc DL = N->getDebugLoc();
14007 unsigned Opc = X86ISD::SHLD;
14008 SDValue Op0 = N0.getOperand(0);
14009 SDValue Op1 = N1.getOperand(0);
14010 if (ShAmt0.getOpcode() == ISD::SUB) {
14011 Opc = X86ISD::SHRD;
14012 std::swap(Op0, Op1);
14013 std::swap(ShAmt0, ShAmt1);
14014 }
14015
Evan Cheng8b1190a2010-04-28 01:18:01 +000014016 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000014017 if (ShAmt1.getOpcode() == ISD::SUB) {
14018 SDValue Sum = ShAmt1.getOperand(0);
14019 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000014020 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
14021 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
14022 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
14023 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000014024 return DAG.getNode(Opc, DL, VT,
14025 Op0, Op1,
14026 DAG.getNode(ISD::TRUNCATE, DL,
14027 MVT::i8, ShAmt0));
14028 }
14029 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
14030 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
14031 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000014032 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000014033 return DAG.getNode(Opc, DL, VT,
14034 N0.getOperand(0), N1.getOperand(0),
14035 DAG.getNode(ISD::TRUNCATE, DL,
14036 MVT::i8, ShAmt0));
14037 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014038
Evan Cheng760d1942010-01-04 21:22:48 +000014039 return SDValue();
14040}
14041
Craig Topper3738ccd2011-12-27 06:27:23 +000014042// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000014043static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
14044 TargetLowering::DAGCombinerInfo &DCI,
14045 const X86Subtarget *Subtarget) {
14046 if (DCI.isBeforeLegalizeOps())
14047 return SDValue();
14048
14049 EVT VT = N->getValueType(0);
14050
14051 if (VT != MVT::i32 && VT != MVT::i64)
14052 return SDValue();
14053
Craig Topper3738ccd2011-12-27 06:27:23 +000014054 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
14055
Craig Topperb4c94572011-10-21 06:55:01 +000014056 // Create BLSMSK instructions by finding X ^ (X-1)
14057 SDValue N0 = N->getOperand(0);
14058 SDValue N1 = N->getOperand(1);
14059 DebugLoc DL = N->getDebugLoc();
14060
14061 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14062 isAllOnes(N0.getOperand(1)))
14063 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
14064
14065 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14066 isAllOnes(N1.getOperand(1)))
14067 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
14068
14069 return SDValue();
14070}
14071
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014072/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
14073static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
14074 const X86Subtarget *Subtarget) {
14075 LoadSDNode *Ld = cast<LoadSDNode>(N);
14076 EVT RegVT = Ld->getValueType(0);
14077 EVT MemVT = Ld->getMemoryVT();
14078 DebugLoc dl = Ld->getDebugLoc();
14079 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14080
14081 ISD::LoadExtType Ext = Ld->getExtensionType();
14082
Nadav Rotemca6f2962011-09-18 19:00:23 +000014083 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014084 // shuffle. We need SSE4 for the shuffles.
14085 // TODO: It is possible to support ZExt by zeroing the undef values
14086 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000014087 if (RegVT.isVector() && RegVT.isInteger() &&
14088 Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014089 assert(MemVT != RegVT && "Cannot extend to the same type");
14090 assert(MemVT.isVector() && "Must load a vector from memory");
14091
14092 unsigned NumElems = RegVT.getVectorNumElements();
14093 unsigned RegSz = RegVT.getSizeInBits();
14094 unsigned MemSz = MemVT.getSizeInBits();
14095 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000014096 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014097 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
14098
14099 // Attempt to load the original value using a single load op.
14100 // Find a scalar type which is equal to the loaded word size.
14101 MVT SclrLoadTy = MVT::i8;
14102 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14103 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14104 MVT Tp = (MVT::SimpleValueType)tp;
14105 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
14106 SclrLoadTy = Tp;
14107 break;
14108 }
14109 }
14110
14111 // Proceed if a load word is found.
14112 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
14113
14114 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
14115 RegSz/SclrLoadTy.getSizeInBits());
14116
14117 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
14118 RegSz/MemVT.getScalarType().getSizeInBits());
14119 // Can't shuffle using an illegal type.
14120 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14121
14122 // Perform a single load.
14123 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
14124 Ld->getBasePtr(),
14125 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014126 Ld->isNonTemporal(), Ld->isInvariant(),
14127 Ld->getAlignment());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014128
14129 // Insert the word loaded into a vector.
14130 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
14131 LoadUnitVecVT, ScalarLoad);
14132
14133 // Bitcast the loaded value to a vector of the original element type, in
14134 // the size of the target vector type.
Chad Rosierb90d2a92012-01-03 23:19:12 +000014135 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT,
14136 ScalarInVector);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014137 unsigned SizeRatio = RegSz/MemSz;
14138
14139 // Redistribute the loaded elements into the different locations.
14140 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14141 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
14142
14143 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
14144 DAG.getUNDEF(SlicedVec.getValueType()),
14145 ShuffleVec.data());
14146
14147 // Bitcast to the requested type.
14148 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
14149 // Replace the original load with the new sequence
14150 // and return the new chain.
14151 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
14152 return SDValue(ScalarLoad.getNode(), 1);
14153 }
14154
14155 return SDValue();
14156}
14157
Chris Lattner149a4e52008-02-22 02:09:43 +000014158/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014159static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000014160 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000014161 StoreSDNode *St = cast<StoreSDNode>(N);
14162 EVT VT = St->getValue().getValueType();
14163 EVT StVT = St->getMemoryVT();
14164 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000014165 SDValue StoredVal = St->getOperand(1);
14166 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14167
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014168 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000014169 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
14170 // 128-bit ones. If in the future the cost becomes only one memory access the
14171 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000014172 if (VT.getSizeInBits() == 256 &&
14173 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
14174 StoredVal.getNumOperands() == 2) {
14175
14176 SDValue Value0 = StoredVal.getOperand(0);
14177 SDValue Value1 = StoredVal.getOperand(1);
14178
14179 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
14180 SDValue Ptr0 = St->getBasePtr();
14181 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
14182
14183 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
14184 St->getPointerInfo(), St->isVolatile(),
14185 St->isNonTemporal(), St->getAlignment());
14186 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
14187 St->getPointerInfo(), St->isVolatile(),
14188 St->isNonTemporal(), St->getAlignment());
14189 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
14190 }
Nadav Rotem614061b2011-08-10 19:30:14 +000014191
14192 // Optimize trunc store (of multiple scalars) to shuffle and store.
14193 // First, pack all of the elements in one place. Next, store to memory
14194 // in fewer chunks.
14195 if (St->isTruncatingStore() && VT.isVector()) {
14196 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14197 unsigned NumElems = VT.getVectorNumElements();
14198 assert(StVT != VT && "Cannot truncate to the same type");
14199 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
14200 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
14201
14202 // From, To sizes and ElemCount must be pow of two
14203 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014204 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000014205 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014206 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014207
Nadav Rotem614061b2011-08-10 19:30:14 +000014208 unsigned SizeRatio = FromSz / ToSz;
14209
14210 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
14211
14212 // Create a type on which we perform the shuffle
14213 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
14214 StVT.getScalarType(), NumElems*SizeRatio);
14215
14216 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
14217
14218 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
14219 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14220 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
14221
14222 // Can't shuffle using an illegal type
14223 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14224
14225 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
14226 DAG.getUNDEF(WideVec.getValueType()),
14227 ShuffleVec.data());
14228 // At this point all of the data is stored at the bottom of the
14229 // register. We now need to save it to mem.
14230
14231 // Find the largest store unit
14232 MVT StoreType = MVT::i8;
14233 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14234 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14235 MVT Tp = (MVT::SimpleValueType)tp;
14236 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
14237 StoreType = Tp;
14238 }
14239
14240 // Bitcast the original vector into a vector of store-size units
14241 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
14242 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
14243 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
14244 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
14245 SmallVector<SDValue, 8> Chains;
14246 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
14247 TLI.getPointerTy());
14248 SDValue Ptr = St->getBasePtr();
14249
14250 // Perform one or more big stores into memory.
14251 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
14252 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14253 StoreType, ShuffWide,
14254 DAG.getIntPtrConstant(i));
14255 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14256 St->getPointerInfo(), St->isVolatile(),
14257 St->isNonTemporal(), St->getAlignment());
14258 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14259 Chains.push_back(Ch);
14260 }
14261
14262 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14263 Chains.size());
14264 }
14265
14266
Chris Lattner149a4e52008-02-22 02:09:43 +000014267 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14268 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014269 // A preferable solution to the general problem is to figure out the right
14270 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014271
14272 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014273 if (VT.getSizeInBits() != 64)
14274 return SDValue();
14275
Devang Patel578efa92009-06-05 21:57:13 +000014276 const Function *F = DAG.getMachineFunction().getFunction();
14277 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014278 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000014279 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000014280 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014281 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014282 isa<LoadSDNode>(St->getValue()) &&
14283 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14284 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014285 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014286 LoadSDNode *Ld = 0;
14287 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014288 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014289 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014290 // Must be a store of a load. We currently handle two cases: the load
14291 // is a direct child, and it's under an intervening TokenFactor. It is
14292 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014293 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014294 Ld = cast<LoadSDNode>(St->getChain());
14295 else if (St->getValue().hasOneUse() &&
14296 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000014297 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014298 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014299 TokenFactorIndex = i;
14300 Ld = cast<LoadSDNode>(St->getValue());
14301 } else
14302 Ops.push_back(ChainVal->getOperand(i));
14303 }
14304 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014305
Evan Cheng536e6672009-03-12 05:59:15 +000014306 if (!Ld || !ISD::isNormalLoad(Ld))
14307 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014308
Evan Cheng536e6672009-03-12 05:59:15 +000014309 // If this is not the MMX case, i.e. we are just turning i64 load/store
14310 // into f64 load/store, avoid the transformation if there are multiple
14311 // uses of the loaded value.
14312 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14313 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014314
Evan Cheng536e6672009-03-12 05:59:15 +000014315 DebugLoc LdDL = Ld->getDebugLoc();
14316 DebugLoc StDL = N->getDebugLoc();
14317 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14318 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14319 // pair instead.
14320 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014321 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014322 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14323 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014324 Ld->isNonTemporal(), Ld->isInvariant(),
14325 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014326 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014327 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014328 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014329 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014330 Ops.size());
14331 }
Evan Cheng536e6672009-03-12 05:59:15 +000014332 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014333 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014334 St->isVolatile(), St->isNonTemporal(),
14335 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014336 }
Evan Cheng536e6672009-03-12 05:59:15 +000014337
14338 // Otherwise, lower to two pairs of 32-bit loads / stores.
14339 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014340 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14341 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014342
Owen Anderson825b72b2009-08-11 20:47:22 +000014343 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014344 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014345 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014346 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014347 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014348 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014349 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014350 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014351 MinAlign(Ld->getAlignment(), 4));
14352
14353 SDValue NewChain = LoLd.getValue(1);
14354 if (TokenFactorIndex != -1) {
14355 Ops.push_back(LoLd);
14356 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014357 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014358 Ops.size());
14359 }
14360
14361 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014362 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14363 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014364
14365 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014366 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014367 St->isVolatile(), St->isNonTemporal(),
14368 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014369 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014370 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014371 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014372 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014373 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014374 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014375 }
Dan Gohman475871a2008-07-27 21:46:04 +000014376 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014377}
14378
Duncan Sands17470be2011-09-22 20:15:48 +000014379/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14380/// and return the operands for the horizontal operation in LHS and RHS. A
14381/// horizontal operation performs the binary operation on successive elements
14382/// of its first operand, then on successive elements of its second operand,
14383/// returning the resulting values in a vector. For example, if
14384/// A = < float a0, float a1, float a2, float a3 >
14385/// and
14386/// B = < float b0, float b1, float b2, float b3 >
14387/// then the result of doing a horizontal operation on A and B is
14388/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14389/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14390/// A horizontal-op B, for some already available A and B, and if so then LHS is
14391/// set to A, RHS to B, and the routine returns 'true'.
14392/// Note that the binary operation should have the property that if one of the
14393/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014394static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000014395 // Look for the following pattern: if
14396 // A = < float a0, float a1, float a2, float a3 >
14397 // B = < float b0, float b1, float b2, float b3 >
14398 // and
14399 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14400 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14401 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14402 // which is A horizontal-op B.
14403
14404 // At least one of the operands should be a vector shuffle.
14405 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14406 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14407 return false;
14408
14409 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000014410
14411 assert((VT.is128BitVector() || VT.is256BitVector()) &&
14412 "Unsupported vector type for horizontal add/sub");
14413
14414 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
14415 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000014416 unsigned NumElts = VT.getVectorNumElements();
14417 unsigned NumLanes = VT.getSizeInBits()/128;
14418 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000014419 assert((NumLaneElts % 2 == 0) &&
14420 "Vector type should have an even number of elements in each lane");
14421 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000014422
14423 // View LHS in the form
14424 // LHS = VECTOR_SHUFFLE A, B, LMask
14425 // If LHS is not a shuffle then pretend it is the shuffle
14426 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14427 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14428 // type VT.
14429 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000014430 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014431 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14432 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14433 A = LHS.getOperand(0);
14434 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14435 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000014436 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
14437 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000014438 } else {
14439 if (LHS.getOpcode() != ISD::UNDEF)
14440 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014441 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014442 LMask[i] = i;
14443 }
14444
14445 // Likewise, view RHS in the form
14446 // RHS = VECTOR_SHUFFLE C, D, RMask
14447 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000014448 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014449 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14450 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14451 C = RHS.getOperand(0);
14452 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14453 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000014454 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
14455 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000014456 } else {
14457 if (RHS.getOpcode() != ISD::UNDEF)
14458 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014459 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014460 RMask[i] = i;
14461 }
14462
14463 // Check that the shuffles are both shuffling the same vectors.
14464 if (!(A == C && B == D) && !(A == D && B == C))
14465 return false;
14466
14467 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14468 if (!A.getNode() && !B.getNode())
14469 return false;
14470
14471 // If A and B occur in reverse order in RHS, then "swap" them (which means
14472 // rewriting the mask).
14473 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000014474 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014475
14476 // At this point LHS and RHS are equivalent to
14477 // LHS = VECTOR_SHUFFLE A, B, LMask
14478 // RHS = VECTOR_SHUFFLE A, B, RMask
14479 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000014480 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000014481 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000014482
Craig Topperf8363302011-12-02 08:18:41 +000014483 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014484 if (LIdx < 0 || RIdx < 0 ||
14485 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
14486 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000014487 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000014488
Craig Topperf8363302011-12-02 08:18:41 +000014489 // Check that successive elements are being operated on. If not, this is
14490 // not a horizontal operation.
14491 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
14492 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000014493 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000014494 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000014495 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000014496 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000014497 }
14498
14499 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14500 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14501 return true;
14502}
14503
14504/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14505static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14506 const X86Subtarget *Subtarget) {
14507 EVT VT = N->getValueType(0);
14508 SDValue LHS = N->getOperand(0);
14509 SDValue RHS = N->getOperand(1);
14510
14511 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000014512 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000014513 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014514 isHorizontalBinOp(LHS, RHS, true))
14515 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14516 return SDValue();
14517}
14518
14519/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14520static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14521 const X86Subtarget *Subtarget) {
14522 EVT VT = N->getValueType(0);
14523 SDValue LHS = N->getOperand(0);
14524 SDValue RHS = N->getOperand(1);
14525
14526 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000014527 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000014528 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014529 isHorizontalBinOp(LHS, RHS, false))
14530 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14531 return SDValue();
14532}
14533
Chris Lattner6cf73262008-01-25 06:14:17 +000014534/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14535/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014536static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014537 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14538 // F[X]OR(0.0, x) -> x
14539 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014540 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14541 if (C->getValueAPF().isPosZero())
14542 return N->getOperand(1);
14543 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14544 if (C->getValueAPF().isPosZero())
14545 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014546 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014547}
14548
14549/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014550static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014551 // FAND(0.0, x) -> 0.0
14552 // FAND(x, 0.0) -> 0.0
14553 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14554 if (C->getValueAPF().isPosZero())
14555 return N->getOperand(0);
14556 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14557 if (C->getValueAPF().isPosZero())
14558 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014559 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014560}
14561
Dan Gohmane5af2d32009-01-29 01:59:02 +000014562static SDValue PerformBTCombine(SDNode *N,
14563 SelectionDAG &DAG,
14564 TargetLowering::DAGCombinerInfo &DCI) {
14565 // BT ignores high bits in the bit index operand.
14566 SDValue Op1 = N->getOperand(1);
14567 if (Op1.hasOneUse()) {
14568 unsigned BitWidth = Op1.getValueSizeInBits();
14569 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14570 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014571 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14572 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014573 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014574 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14575 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14576 DCI.CommitTargetLoweringOpt(TLO);
14577 }
14578 return SDValue();
14579}
Chris Lattner83e6c992006-10-04 06:57:07 +000014580
Eli Friedman7a5e5552009-06-07 06:52:44 +000014581static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14582 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014583 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014584 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014585 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014586 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014587 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014588 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014589 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014590 }
14591 return SDValue();
14592}
14593
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000014594static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
14595 TargetLowering::DAGCombinerInfo &DCI,
14596 const X86Subtarget *Subtarget) {
14597 if (!DCI.isBeforeLegalizeOps())
14598 return SDValue();
14599
Elena Demikhovskyf6020402012-02-08 08:37:26 +000014600 if (!Subtarget->hasAVX())
14601 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000014602
Elena Demikhovskyf6020402012-02-08 08:37:26 +000014603 // Optimize vectors in AVX mode
14604 // Sign extend v8i16 to v8i32 and
14605 // v4i32 to v4i64
14606 //
14607 // Divide input vector into two parts
14608 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
14609 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
14610 // concat the vectors to original VT
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000014611
14612 EVT VT = N->getValueType(0);
14613 SDValue Op = N->getOperand(0);
14614 EVT OpVT = Op.getValueType();
14615 DebugLoc dl = N->getDebugLoc();
14616
Elena Demikhovskyf6020402012-02-08 08:37:26 +000014617 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
14618 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000014619
14620 unsigned NumElems = OpVT.getVectorNumElements();
14621 SmallVector<int,8> ShufMask1(NumElems, -1);
Elena Demikhovskyf6020402012-02-08 08:37:26 +000014622 for (unsigned i = 0; i < NumElems/2; i++) ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000014623
14624 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, DAG.getUNDEF(OpVT),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000014625 ShufMask1.data());
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000014626
14627 SmallVector<int,8> ShufMask2(NumElems, -1);
Elena Demikhovskyf6020402012-02-08 08:37:26 +000014628 for (unsigned i = 0; i < NumElems/2; i++) ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000014629
14630 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, DAG.getUNDEF(OpVT),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000014631 ShufMask2.data());
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000014632
14633 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000014634 VT.getVectorNumElements()/2);
14635
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000014636 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
14637 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
14638
14639 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
14640 }
14641 return SDValue();
14642}
14643
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000014644static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
14645 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000014646 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14647 // (and (i32 x86isd::setcc_carry), 1)
14648 // This eliminates the zext. This transformation is necessary because
14649 // ISD::SETCC is always legalized to i8.
14650 DebugLoc dl = N->getDebugLoc();
14651 SDValue N0 = N->getOperand(0);
14652 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000014653 EVT OpVT = N0.getValueType();
14654
Evan Cheng2e489c42009-12-16 00:53:11 +000014655 if (N0.getOpcode() == ISD::AND &&
14656 N0.hasOneUse() &&
14657 N0.getOperand(0).hasOneUse()) {
14658 SDValue N00 = N0.getOperand(0);
14659 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14660 return SDValue();
14661 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14662 if (!C || C->getZExtValue() != 1)
14663 return SDValue();
14664 return DAG.getNode(ISD::AND, dl, VT,
14665 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14666 N00.getOperand(0), N00.getOperand(1)),
14667 DAG.getConstant(1, VT));
14668 }
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000014669 // Optimize vectors in AVX mode:
14670 //
14671 // v8i16 -> v8i32
14672 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
14673 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
14674 // Concat upper and lower parts.
14675 //
14676 // v4i32 -> v4i64
14677 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
14678 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
14679 // Concat upper and lower parts.
14680 //
14681 if (Subtarget->hasAVX()) {
14682
14683 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
14684 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
14685
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014686 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000014687 SDValue OpLo = getTargetShuffleNode(X86ISD::UNPCKL, dl, OpVT, N0, ZeroVec, DAG);
14688 SDValue OpHi = getTargetShuffleNode(X86ISD::UNPCKH, dl, OpVT, N0, ZeroVec, DAG);
14689
14690 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
14691 VT.getVectorNumElements()/2);
14692
14693 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
14694 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
14695
14696 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
14697 }
14698 }
14699
Evan Cheng2e489c42009-12-16 00:53:11 +000014700
14701 return SDValue();
14702}
14703
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014704// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14705static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14706 unsigned X86CC = N->getConstantOperandVal(0);
14707 SDValue EFLAG = N->getOperand(1);
14708 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014709
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014710 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14711 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14712 // cases.
14713 if (X86CC == X86::COND_B)
14714 return DAG.getNode(ISD::AND, DL, MVT::i8,
14715 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14716 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14717 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014718
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014719 return SDValue();
14720}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014721
Benjamin Kramer1396c402011-06-18 11:09:41 +000014722static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14723 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014724 SDValue Op0 = N->getOperand(0);
14725 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14726 // a 32-bit target where SSE doesn't support i64->FP operations.
14727 if (Op0.getOpcode() == ISD::LOAD) {
14728 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14729 EVT VT = Ld->getValueType(0);
14730 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14731 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14732 !XTLI->getSubtarget()->is64Bit() &&
14733 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014734 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14735 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014736 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14737 return FILDChain;
14738 }
14739 }
14740 return SDValue();
14741}
14742
Chris Lattner23a01992010-12-20 01:37:09 +000014743// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14744static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14745 X86TargetLowering::DAGCombinerInfo &DCI) {
14746 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14747 // the result is either zero or one (depending on the input carry bit).
14748 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14749 if (X86::isZeroNode(N->getOperand(0)) &&
14750 X86::isZeroNode(N->getOperand(1)) &&
14751 // We don't have a good way to replace an EFLAGS use, so only do this when
14752 // dead right now.
14753 SDValue(N, 1).use_empty()) {
14754 DebugLoc DL = N->getDebugLoc();
14755 EVT VT = N->getValueType(0);
14756 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14757 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14758 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14759 DAG.getConstant(X86::COND_B,MVT::i8),
14760 N->getOperand(2)),
14761 DAG.getConstant(1, VT));
14762 return DCI.CombineTo(N, Res1, CarryOut);
14763 }
14764
14765 return SDValue();
14766}
14767
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014768// fold (add Y, (sete X, 0)) -> adc 0, Y
14769// (add Y, (setne X, 0)) -> sbb -1, Y
14770// (sub (sete X, 0), Y) -> sbb 0, Y
14771// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014772static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014773 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014774
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014775 // Look through ZExts.
14776 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14777 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14778 return SDValue();
14779
14780 SDValue SetCC = Ext.getOperand(0);
14781 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14782 return SDValue();
14783
14784 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14785 if (CC != X86::COND_E && CC != X86::COND_NE)
14786 return SDValue();
14787
14788 SDValue Cmp = SetCC.getOperand(1);
14789 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014790 !X86::isZeroNode(Cmp.getOperand(1)) ||
14791 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014792 return SDValue();
14793
14794 SDValue CmpOp0 = Cmp.getOperand(0);
14795 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14796 DAG.getConstant(1, CmpOp0.getValueType()));
14797
14798 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14799 if (CC == X86::COND_NE)
14800 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14801 DL, OtherVal.getValueType(), OtherVal,
14802 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14803 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14804 DL, OtherVal.getValueType(), OtherVal,
14805 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14806}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014807
Craig Topper54f952a2011-11-19 09:02:40 +000014808/// PerformADDCombine - Do target-specific dag combines on integer adds.
14809static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
14810 const X86Subtarget *Subtarget) {
14811 EVT VT = N->getValueType(0);
14812 SDValue Op0 = N->getOperand(0);
14813 SDValue Op1 = N->getOperand(1);
14814
14815 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000014816 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000014817 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000014818 isHorizontalBinOp(Op0, Op1, true))
14819 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
14820
14821 return OptimizeConditionalInDecrement(N, DAG);
14822}
14823
14824static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
14825 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014826 SDValue Op0 = N->getOperand(0);
14827 SDValue Op1 = N->getOperand(1);
14828
14829 // X86 can't encode an immediate LHS of a sub. See if we can push the
14830 // negation into a preceding instruction.
14831 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014832 // If the RHS of the sub is a XOR with one use and a constant, invert the
14833 // immediate. Then add one to the LHS of the sub so we can turn
14834 // X-Y -> X+~Y+1, saving one register.
14835 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14836 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014837 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014838 EVT VT = Op0.getValueType();
14839 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14840 Op1.getOperand(0),
14841 DAG.getConstant(~XorC, VT));
14842 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014843 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014844 }
14845 }
14846
Craig Topper54f952a2011-11-19 09:02:40 +000014847 // Try to synthesize horizontal adds from adds of shuffles.
14848 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000014849 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000014850 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
14851 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000014852 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
14853
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014854 return OptimizeConditionalInDecrement(N, DAG);
14855}
14856
Dan Gohman475871a2008-07-27 21:46:04 +000014857SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014858 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014859 SelectionDAG &DAG = DCI.DAG;
14860 switch (N->getOpcode()) {
14861 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014862 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014863 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014864 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000014865 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014866 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Craig Topper54f952a2011-11-19 09:02:40 +000014867 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
14868 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000014869 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014870 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014871 case ISD::SHL:
14872 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000014873 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014874 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014875 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014876 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014877 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014878 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014879 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014880 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14881 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014882 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014883 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14884 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014885 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014886 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000014887 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000014888 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014889 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG, DCI);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014890 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Craig Topperb3982da2011-12-31 23:50:21 +000014891 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014892 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000014893 case X86ISD::UNPCKH:
14894 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014895 case X86ISD::MOVHLPS:
14896 case X86ISD::MOVLHPS:
14897 case X86ISD::PSHUFD:
14898 case X86ISD::PSHUFHW:
14899 case X86ISD::PSHUFLW:
14900 case X86ISD::MOVSS:
14901 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000014902 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000014903 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014904 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014905 }
14906
Dan Gohman475871a2008-07-27 21:46:04 +000014907 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014908}
14909
Evan Chenge5b51ac2010-04-17 06:13:15 +000014910/// isTypeDesirableForOp - Return true if the target has native support for
14911/// the specified value type and it is 'desirable' to use the type for the
14912/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14913/// instruction encodings are longer and some i16 instructions are slow.
14914bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14915 if (!isTypeLegal(VT))
14916 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014917 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014918 return true;
14919
14920 switch (Opc) {
14921 default:
14922 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014923 case ISD::LOAD:
14924 case ISD::SIGN_EXTEND:
14925 case ISD::ZERO_EXTEND:
14926 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014927 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014928 case ISD::SRL:
14929 case ISD::SUB:
14930 case ISD::ADD:
14931 case ISD::MUL:
14932 case ISD::AND:
14933 case ISD::OR:
14934 case ISD::XOR:
14935 return false;
14936 }
14937}
14938
14939/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014940/// beneficial for dag combiner to promote the specified node. If true, it
14941/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014942bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014943 EVT VT = Op.getValueType();
14944 if (VT != MVT::i16)
14945 return false;
14946
Evan Cheng4c26e932010-04-19 19:29:22 +000014947 bool Promote = false;
14948 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014949 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014950 default: break;
14951 case ISD::LOAD: {
14952 LoadSDNode *LD = cast<LoadSDNode>(Op);
14953 // If the non-extending load has a single use and it's not live out, then it
14954 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014955 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14956 Op.hasOneUse()*/) {
14957 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14958 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14959 // The only case where we'd want to promote LOAD (rather then it being
14960 // promoted as an operand is when it's only use is liveout.
14961 if (UI->getOpcode() != ISD::CopyToReg)
14962 return false;
14963 }
14964 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014965 Promote = true;
14966 break;
14967 }
14968 case ISD::SIGN_EXTEND:
14969 case ISD::ZERO_EXTEND:
14970 case ISD::ANY_EXTEND:
14971 Promote = true;
14972 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014973 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014974 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014975 SDValue N0 = Op.getOperand(0);
14976 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014977 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014978 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014979 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014980 break;
14981 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014982 case ISD::ADD:
14983 case ISD::MUL:
14984 case ISD::AND:
14985 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014986 case ISD::XOR:
14987 Commute = true;
14988 // fallthrough
14989 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014990 SDValue N0 = Op.getOperand(0);
14991 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014992 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014993 return false;
14994 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014995 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014996 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014997 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014998 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014999 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000015000 }
15001 }
15002
15003 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000015004 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000015005}
15006
Evan Cheng60c07e12006-07-05 22:17:51 +000015007//===----------------------------------------------------------------------===//
15008// X86 Inline Assembly Support
15009//===----------------------------------------------------------------------===//
15010
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015011namespace {
15012 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000015013 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015014 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015015
Benjamin Kramer0581ed72011-12-18 20:51:31 +000015016 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015017 StringRef piece(*args[i]);
15018 if (!s.startswith(piece)) // Check if the piece matches.
15019 return false;
15020
15021 s = s.substr(piece.size());
15022 StringRef::size_type pos = s.find_first_not_of(" \t");
15023 if (pos == 0) // We matched a prefix.
15024 return false;
15025
15026 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015027 }
15028
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015029 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015030 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000015031 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015032}
15033
Chris Lattnerb8105652009-07-20 17:51:36 +000015034bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
15035 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000015036
15037 std::string AsmStr = IA->getAsmString();
15038
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015039 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
15040 if (!Ty || Ty->getBitWidth() % 16 != 0)
15041 return false;
15042
Chris Lattnerb8105652009-07-20 17:51:36 +000015043 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000015044 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000015045 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000015046
15047 switch (AsmPieces.size()) {
15048 default: return false;
15049 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000015050 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015051 // we will turn this bswap into something that will be lowered to logical
15052 // ops instead of emitting the bswap asm. For now, we don't support 486 or
15053 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000015054 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015055 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
15056 matchAsm(AsmPieces[0], "bswapl", "$0") ||
15057 matchAsm(AsmPieces[0], "bswapq", "$0") ||
15058 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
15059 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
15060 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000015061 // No need to check constraints, nothing other than the equivalent of
15062 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000015063 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000015064 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015065
Chris Lattnerb8105652009-07-20 17:51:36 +000015066 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000015067 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015068 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015069 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
15070 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000015071 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000015072 const std::string &ConstraintsStr = IA->getConstraintString();
15073 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000015074 std::sort(AsmPieces.begin(), AsmPieces.end());
15075 if (AsmPieces.size() == 4 &&
15076 AsmPieces[0] == "~{cc}" &&
15077 AsmPieces[1] == "~{dirflag}" &&
15078 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015079 AsmPieces[3] == "~{fpsr}")
15080 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000015081 }
15082 break;
15083 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000015084 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015085 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015086 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
15087 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
15088 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015089 AsmPieces.clear();
15090 const std::string &ConstraintsStr = IA->getConstraintString();
15091 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
15092 std::sort(AsmPieces.begin(), AsmPieces.end());
15093 if (AsmPieces.size() == 4 &&
15094 AsmPieces[0] == "~{cc}" &&
15095 AsmPieces[1] == "~{dirflag}" &&
15096 AsmPieces[2] == "~{flags}" &&
15097 AsmPieces[3] == "~{fpsr}")
15098 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000015099 }
Evan Cheng55d42002011-01-08 01:24:27 +000015100
15101 if (CI->getType()->isIntegerTy(64)) {
15102 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
15103 if (Constraints.size() >= 2 &&
15104 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
15105 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
15106 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015107 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
15108 matchAsm(AsmPieces[1], "bswap", "%edx") &&
15109 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015110 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000015111 }
15112 }
15113 break;
15114 }
15115 return false;
15116}
15117
15118
15119
Chris Lattnerf4dff842006-07-11 02:54:03 +000015120/// getConstraintType - Given a constraint letter, return the type of
15121/// constraint it is for this target.
15122X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000015123X86TargetLowering::getConstraintType(const std::string &Constraint) const {
15124 if (Constraint.size() == 1) {
15125 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000015126 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000015127 case 'q':
15128 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000015129 case 'f':
15130 case 't':
15131 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000015132 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000015133 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000015134 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000015135 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000015136 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000015137 case 'a':
15138 case 'b':
15139 case 'c':
15140 case 'd':
15141 case 'S':
15142 case 'D':
15143 case 'A':
15144 return C_Register;
15145 case 'I':
15146 case 'J':
15147 case 'K':
15148 case 'L':
15149 case 'M':
15150 case 'N':
15151 case 'G':
15152 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000015153 case 'e':
15154 case 'Z':
15155 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000015156 default:
15157 break;
15158 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000015159 }
Chris Lattner4234f572007-03-25 02:14:49 +000015160 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000015161}
15162
John Thompson44ab89e2010-10-29 17:29:13 +000015163/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000015164/// This object must already have been set up with the operand type
15165/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000015166TargetLowering::ConstraintWeight
15167 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000015168 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000015169 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015170 Value *CallOperandVal = info.CallOperandVal;
15171 // If we don't have a value, we can't do a match,
15172 // but allow it at the lowest weight.
15173 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000015174 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015175 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000015176 // Look at the constraint type.
15177 switch (*constraint) {
15178 default:
John Thompson44ab89e2010-10-29 17:29:13 +000015179 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
15180 case 'R':
15181 case 'q':
15182 case 'Q':
15183 case 'a':
15184 case 'b':
15185 case 'c':
15186 case 'd':
15187 case 'S':
15188 case 'D':
15189 case 'A':
15190 if (CallOperandVal->getType()->isIntegerTy())
15191 weight = CW_SpecificReg;
15192 break;
15193 case 'f':
15194 case 't':
15195 case 'u':
15196 if (type->isFloatingPointTy())
15197 weight = CW_SpecificReg;
15198 break;
15199 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000015200 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000015201 weight = CW_SpecificReg;
15202 break;
15203 case 'x':
15204 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000015205 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000015206 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000015207 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015208 break;
15209 case 'I':
15210 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
15211 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000015212 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015213 }
15214 break;
John Thompson44ab89e2010-10-29 17:29:13 +000015215 case 'J':
15216 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15217 if (C->getZExtValue() <= 63)
15218 weight = CW_Constant;
15219 }
15220 break;
15221 case 'K':
15222 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15223 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
15224 weight = CW_Constant;
15225 }
15226 break;
15227 case 'L':
15228 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15229 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
15230 weight = CW_Constant;
15231 }
15232 break;
15233 case 'M':
15234 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15235 if (C->getZExtValue() <= 3)
15236 weight = CW_Constant;
15237 }
15238 break;
15239 case 'N':
15240 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15241 if (C->getZExtValue() <= 0xff)
15242 weight = CW_Constant;
15243 }
15244 break;
15245 case 'G':
15246 case 'C':
15247 if (dyn_cast<ConstantFP>(CallOperandVal)) {
15248 weight = CW_Constant;
15249 }
15250 break;
15251 case 'e':
15252 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15253 if ((C->getSExtValue() >= -0x80000000LL) &&
15254 (C->getSExtValue() <= 0x7fffffffLL))
15255 weight = CW_Constant;
15256 }
15257 break;
15258 case 'Z':
15259 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15260 if (C->getZExtValue() <= 0xffffffff)
15261 weight = CW_Constant;
15262 }
15263 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015264 }
15265 return weight;
15266}
15267
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015268/// LowerXConstraint - try to replace an X constraint, which matches anything,
15269/// with another that has more specific requirements based on the type of the
15270/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000015271const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000015272LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000015273 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
15274 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000015275 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000015276 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000015277 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000015278 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000015279 return "x";
15280 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015281
Chris Lattner5e764232008-04-26 23:02:14 +000015282 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015283}
15284
Chris Lattner48884cd2007-08-25 00:47:38 +000015285/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
15286/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000015287void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000015288 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000015289 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000015290 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000015291 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000015292
Eric Christopher100c8332011-06-02 23:16:42 +000015293 // Only support length 1 constraints for now.
15294 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000015295
Eric Christopher100c8332011-06-02 23:16:42 +000015296 char ConstraintLetter = Constraint[0];
15297 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015298 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000015299 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000015300 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015301 if (C->getZExtValue() <= 31) {
15302 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015303 break;
15304 }
Devang Patel84f7fd22007-03-17 00:13:28 +000015305 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015306 return;
Evan Cheng364091e2008-09-22 23:57:37 +000015307 case 'J':
15308 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015309 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000015310 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15311 break;
15312 }
15313 }
15314 return;
15315 case 'K':
15316 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015317 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000015318 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15319 break;
15320 }
15321 }
15322 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000015323 case 'N':
15324 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015325 if (C->getZExtValue() <= 255) {
15326 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015327 break;
15328 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000015329 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015330 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000015331 case 'e': {
15332 // 32-bit signed value
15333 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015334 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15335 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015336 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015337 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000015338 break;
15339 }
15340 // FIXME gcc accepts some relocatable values here too, but only in certain
15341 // memory models; it's complicated.
15342 }
15343 return;
15344 }
15345 case 'Z': {
15346 // 32-bit unsigned value
15347 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015348 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15349 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015350 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15351 break;
15352 }
15353 }
15354 // FIXME gcc accepts some relocatable values here too, but only in certain
15355 // memory models; it's complicated.
15356 return;
15357 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015358 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015359 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000015360 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015361 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015362 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000015363 break;
15364 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015365
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015366 // In any sort of PIC mode addresses need to be computed at runtime by
15367 // adding in a register or some sort of table lookup. These can't
15368 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000015369 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015370 return;
15371
Chris Lattnerdc43a882007-05-03 16:52:29 +000015372 // If we are in non-pic codegen mode, we allow the address of a global (with
15373 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000015374 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015375 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000015376
Chris Lattner49921962009-05-08 18:23:14 +000015377 // Match either (GA), (GA+C), (GA+C1+C2), etc.
15378 while (1) {
15379 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15380 Offset += GA->getOffset();
15381 break;
15382 } else if (Op.getOpcode() == ISD::ADD) {
15383 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15384 Offset += C->getZExtValue();
15385 Op = Op.getOperand(0);
15386 continue;
15387 }
15388 } else if (Op.getOpcode() == ISD::SUB) {
15389 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15390 Offset += -C->getZExtValue();
15391 Op = Op.getOperand(0);
15392 continue;
15393 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015394 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015395
Chris Lattner49921962009-05-08 18:23:14 +000015396 // Otherwise, this isn't something we can handle, reject it.
15397 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015398 }
Eric Christopherfd179292009-08-27 18:07:15 +000015399
Dan Gohman46510a72010-04-15 01:51:59 +000015400 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015401 // If we require an extra load to get this address, as in PIC mode, we
15402 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015403 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15404 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015405 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015406
Devang Patel0d881da2010-07-06 22:08:15 +000015407 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15408 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015409 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015410 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015411 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015412
Gabor Greifba36cb52008-08-28 21:40:38 +000015413 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015414 Ops.push_back(Result);
15415 return;
15416 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015417 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015418}
15419
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015420std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015421X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015422 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015423 // First, see if this is a constraint that directly corresponds to an LLVM
15424 // register class.
15425 if (Constraint.size() == 1) {
15426 // GCC Constraint Letters
15427 switch (Constraint[0]) {
15428 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015429 // TODO: Slight differences here in allocation order and leaving
15430 // RIP in the class. Do they matter any more here than they do
15431 // in the normal allocation?
15432 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15433 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015434 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015435 return std::make_pair(0U, X86::GR32RegisterClass);
15436 else if (VT == MVT::i16)
15437 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015438 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015439 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015440 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015441 return std::make_pair(0U, X86::GR64RegisterClass);
15442 break;
15443 }
15444 // 32-bit fallthrough
15445 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015446 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015447 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
15448 else if (VT == MVT::i16)
15449 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015450 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015451 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
15452 else if (VT == MVT::i64)
15453 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15454 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015455 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015456 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015457 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015458 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015459 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015460 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015461 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015462 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015463 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015464 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015465 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015466 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15467 if (VT == MVT::i16)
15468 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15469 if (VT == MVT::i32 || !Subtarget->is64Bit())
15470 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15471 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015472 case 'f': // FP Stack registers.
15473 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15474 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015475 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015476 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015477 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015478 return std::make_pair(0U, X86::RFP64RegisterClass);
15479 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015480 case 'y': // MMX_REGS if MMX allowed.
15481 if (!Subtarget->hasMMX()) break;
15482 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015483 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000015484 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015485 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000015486 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000015487 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015488
Owen Anderson825b72b2009-08-11 20:47:22 +000015489 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015490 default: break;
15491 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015492 case MVT::f32:
15493 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015494 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015495 case MVT::f64:
15496 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015497 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015498 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015499 case MVT::v16i8:
15500 case MVT::v8i16:
15501 case MVT::v4i32:
15502 case MVT::v2i64:
15503 case MVT::v4f32:
15504 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015505 return std::make_pair(0U, X86::VR128RegisterClass);
Eric Christopher55487552012-01-07 01:02:09 +000015506 // AVX types.
15507 case MVT::v32i8:
15508 case MVT::v16i16:
15509 case MVT::v8i32:
15510 case MVT::v4i64:
15511 case MVT::v8f32:
15512 case MVT::v4f64:
15513 return std::make_pair(0U, X86::VR256RegisterClass);
15514
Chris Lattner0f65cad2007-04-09 05:49:22 +000015515 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015516 break;
15517 }
15518 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015519
Chris Lattnerf76d1802006-07-31 23:26:50 +000015520 // Use the default implementation in TargetLowering to convert the register
15521 // constraint into a member of a register class.
15522 std::pair<unsigned, const TargetRegisterClass*> Res;
15523 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015524
15525 // Not found as a standard register?
15526 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015527 // Map st(0) -> st(7) -> ST0
15528 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15529 tolower(Constraint[1]) == 's' &&
15530 tolower(Constraint[2]) == 't' &&
15531 Constraint[3] == '(' &&
15532 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15533 Constraint[5] == ')' &&
15534 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015535
Chris Lattner56d77c72009-09-13 22:41:48 +000015536 Res.first = X86::ST0+Constraint[4]-'0';
15537 Res.second = X86::RFP80RegisterClass;
15538 return Res;
15539 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015540
Chris Lattner56d77c72009-09-13 22:41:48 +000015541 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015542 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015543 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015544 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015545 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015546 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015547
15548 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015549 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015550 Res.first = X86::EFLAGS;
15551 Res.second = X86::CCRRegisterClass;
15552 return Res;
15553 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015554
Dale Johannesen330169f2008-11-13 21:52:36 +000015555 // 'A' means EAX + EDX.
15556 if (Constraint == "A") {
15557 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015558 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015559 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015560 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015561 return Res;
15562 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015563
Chris Lattnerf76d1802006-07-31 23:26:50 +000015564 // Otherwise, check to see if this is a register class of the wrong value
15565 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15566 // turn into {ax},{dx}.
15567 if (Res.second->hasType(VT))
15568 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015569
Chris Lattnerf76d1802006-07-31 23:26:50 +000015570 // All of the single-register GCC register classes map their values onto
15571 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15572 // really want an 8-bit or 32-bit register, map to the appropriate register
15573 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015574 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015575 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015576 unsigned DestReg = 0;
15577 switch (Res.first) {
15578 default: break;
15579 case X86::AX: DestReg = X86::AL; break;
15580 case X86::DX: DestReg = X86::DL; break;
15581 case X86::CX: DestReg = X86::CL; break;
15582 case X86::BX: DestReg = X86::BL; break;
15583 }
15584 if (DestReg) {
15585 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015586 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015587 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015588 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015589 unsigned DestReg = 0;
15590 switch (Res.first) {
15591 default: break;
15592 case X86::AX: DestReg = X86::EAX; break;
15593 case X86::DX: DestReg = X86::EDX; break;
15594 case X86::CX: DestReg = X86::ECX; break;
15595 case X86::BX: DestReg = X86::EBX; break;
15596 case X86::SI: DestReg = X86::ESI; break;
15597 case X86::DI: DestReg = X86::EDI; break;
15598 case X86::BP: DestReg = X86::EBP; break;
15599 case X86::SP: DestReg = X86::ESP; break;
15600 }
15601 if (DestReg) {
15602 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015603 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015604 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015605 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015606 unsigned DestReg = 0;
15607 switch (Res.first) {
15608 default: break;
15609 case X86::AX: DestReg = X86::RAX; break;
15610 case X86::DX: DestReg = X86::RDX; break;
15611 case X86::CX: DestReg = X86::RCX; break;
15612 case X86::BX: DestReg = X86::RBX; break;
15613 case X86::SI: DestReg = X86::RSI; break;
15614 case X86::DI: DestReg = X86::RDI; break;
15615 case X86::BP: DestReg = X86::RBP; break;
15616 case X86::SP: DestReg = X86::RSP; break;
15617 }
15618 if (DestReg) {
15619 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015620 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015621 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015622 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015623 } else if (Res.second == X86::FR32RegisterClass ||
15624 Res.second == X86::FR64RegisterClass ||
15625 Res.second == X86::VR128RegisterClass) {
15626 // Handle references to XMM physical registers that got mapped into the
15627 // wrong class. This can happen with constraints like {xmm0} where the
15628 // target independent register mapper will just pick the first match it can
15629 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015630 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015631 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015632 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015633 Res.second = X86::FR64RegisterClass;
15634 else if (X86::VR128RegisterClass->hasType(VT))
15635 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015636 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015637
Chris Lattnerf76d1802006-07-31 23:26:50 +000015638 return Res;
15639}