blob: fb0d1adaadadfd9f5bca0a051a03b0c71abd00f5 [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"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000043#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000044#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000045#include "llvm/ADT/StringExtras.h"
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000046#include "llvm/ADT/VariadicFunction.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000047#include "llvm/Support/CallSite.h"
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +000048#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000054#include "llvm/Target/TargetOptions.h"
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.
181 if (Subtarget->is64Bit())
182 setSchedulingPreference(Sched::ILP);
183 else
184 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000185 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000186
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000187 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000188 // Setup Windows compiler runtime calls.
189 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000190 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000191 setLibcallName(RTLIB::SREM_I64, "_allrem");
192 setLibcallName(RTLIB::UREM_I64, "_aullrem");
193 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000194 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000195 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000196 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000197 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000198 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
199 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
200 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000201 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
202 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000203 }
204
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000205 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000206 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000207 setUseUnderscoreSetJmp(false);
208 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000209 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000210 // MS runtime is weird: it exports _setjmp, but longjmp!
211 setUseUnderscoreSetJmp(true);
212 setUseUnderscoreLongJmp(false);
213 } else {
214 setUseUnderscoreSetJmp(true);
215 setUseUnderscoreLongJmp(true);
216 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000217
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000218 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000220 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000221 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000222 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000223 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000224
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000226
Scott Michelfdc40a02009-02-17 22:15:04 +0000227 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000229 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000231 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
233 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000234
235 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
237 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
238 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
239 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
240 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
241 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000242
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000243 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
244 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
246 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
247 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000248
Evan Cheng25ab6902006-09-08 06:48:29 +0000249 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000251 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000252 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000253 // We have an algorithm for SSE2->double, and we turn this into a
254 // 64-bit FILD followed by conditional FADD for other targets.
255 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000256 // We have an algorithm for SSE2, and we turn this into a 64-bit
257 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000258 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000259 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000260
261 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
262 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
264 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000265
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000266 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000267 // SSE has no i16 to fp conversion, only i32
268 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000270 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000272 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
274 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000276 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
278 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000279 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000280
Dale Johannesen73328d12007-09-19 23:55:34 +0000281 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
282 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
284 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000285
Evan Cheng02568ff2006-01-30 22:13:22 +0000286 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
287 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
289 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000290
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000291 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000293 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000294 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000295 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
297 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000298 }
299
300 // Handle FP_TO_UINT by promoting the destination to a larger signed
301 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
303 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
304 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000305
Evan Cheng25ab6902006-09-08 06:48:29 +0000306 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
308 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000309 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000310 // Since AVX is a superset of SSE3, only check for SSE here.
311 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000312 // Expand FP_TO_UINT into a select.
313 // FIXME: We would like to use a Custom expander here eventually to do
314 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000316 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000317 // With SSE3 we can use fisttpll to convert to a signed i64; without
318 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000321
Chris Lattner399610a2006-12-05 18:22:22 +0000322 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000323 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000324 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
325 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000326 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000327 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000328 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000329 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000330 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000331 }
Chris Lattner21f66852005-12-23 05:15:23 +0000332
Dan Gohmanb00ee212008-02-18 19:34:53 +0000333 // Scalar integer divide and remainder are lowered to use operations that
334 // produce two results, to match the available instructions. This exposes
335 // the two-result form to trivial CSE, which is able to combine x/y and x%y
336 // into a single instruction.
337 //
338 // Scalar integer multiply-high is also lowered to use two-result
339 // operations, to match the available instructions. However, plain multiply
340 // (low) operations are left as Legal, as there are single-result
341 // instructions for this in x86. Using the two-result multiply instructions
342 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000343 for (unsigned i = 0, e = 4; i != e; ++i) {
344 MVT VT = IntVTs[i];
345 setOperationAction(ISD::MULHS, VT, Expand);
346 setOperationAction(ISD::MULHU, VT, Expand);
347 setOperationAction(ISD::SDIV, VT, Expand);
348 setOperationAction(ISD::UDIV, VT, Expand);
349 setOperationAction(ISD::SREM, VT, Expand);
350 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000351
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000352 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000353 setOperationAction(ISD::ADDC, VT, Custom);
354 setOperationAction(ISD::ADDE, VT, Custom);
355 setOperationAction(ISD::SUBC, VT, Custom);
356 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000357 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000358
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
360 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
361 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
362 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000363 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000364 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
365 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
366 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
367 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
368 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
369 setOperationAction(ISD::FREM , MVT::f32 , Expand);
370 setOperationAction(ISD::FREM , MVT::f64 , Expand);
371 setOperationAction(ISD::FREM , MVT::f80 , Expand);
372 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000373
Chandler Carruth77821022011-12-24 12:12:34 +0000374 // Promote the i8 variants and force them on up to i32 which has a shorter
375 // encoding.
376 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
377 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
378 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
379 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000380 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000381 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
382 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
383 if (Subtarget->is64Bit())
384 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000385 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000386 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
387 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
388 if (Subtarget->is64Bit())
389 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
390 }
Craig Topper37f21672011-10-11 06:44:02 +0000391
392 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000393 // When promoting the i8 variants, force them to i32 for a shorter
394 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000395 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000396 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
397 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
398 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000399 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
400 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
401 if (Subtarget->is64Bit())
402 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000403 } else {
404 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
405 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
406 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000407 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
408 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
409 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
410 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000411 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000412 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
413 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000414 }
415
Benjamin Kramer1292c222010-12-04 20:32:23 +0000416 if (Subtarget->hasPOPCNT()) {
417 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
418 } else {
419 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
420 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
421 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
422 if (Subtarget->is64Bit())
423 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
424 }
425
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
427 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000428
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000429 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000430 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000431 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000432 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000433 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
435 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
436 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
437 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
438 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000439 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
441 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
442 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
443 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000444 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000446 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000447 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000449
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000450 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000451 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
452 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
453 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
454 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000455 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
457 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000458 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000459 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
461 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
462 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
463 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000464 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000465 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000466 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
468 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
469 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000470 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
472 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
473 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000474 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000475
Craig Topper1accb7e2012-01-10 06:54:16 +0000476 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000477 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000478
Eric Christopher9a9d2752010-07-22 02:48:34 +0000479 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000480 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000481
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000482 // On X86 and X86-64, atomic operations are lowered to locked instructions.
483 // Locked instructions, in turn, have implicit fence semantics (all memory
484 // operations are flushed before issuing the locked instruction, and they
485 // are not buffered), so we can fold away the common pattern of
486 // fence-atomic-fence.
487 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000488
Mon P Wang63307c32008-05-05 19:05:59 +0000489 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000490 for (unsigned i = 0, e = 4; i != e; ++i) {
491 MVT VT = IntVTs[i];
492 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
493 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000494 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000495 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000496
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000497 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000498 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000499 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
500 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
501 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
502 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
503 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
504 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
505 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000506 }
507
Eli Friedman43f51ae2011-08-26 21:21:21 +0000508 if (Subtarget->hasCmpxchg16b()) {
509 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
510 }
511
Evan Cheng3c992d22006-03-07 02:02:57 +0000512 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000513 if (!Subtarget->isTargetDarwin() &&
514 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000515 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000517 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000518
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
520 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
521 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
522 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000523 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000524 setExceptionPointerRegister(X86::RAX);
525 setExceptionSelectorRegister(X86::RDX);
526 } else {
527 setExceptionPointerRegister(X86::EAX);
528 setExceptionSelectorRegister(X86::EDX);
529 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
531 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000532
Duncan Sands4a544a72011-09-06 13:37:06 +0000533 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
534 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000535
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000537
Nate Begemanacc398c2006-01-25 18:21:52 +0000538 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000539 setOperationAction(ISD::VASTART , MVT::Other, Custom);
540 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000541 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 setOperationAction(ISD::VAARG , MVT::Other, Custom);
543 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000544 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 setOperationAction(ISD::VAARG , MVT::Other, Expand);
546 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000547 }
Evan Chengae642192007-03-02 23:16:35 +0000548
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
550 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000551
552 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
553 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
554 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000555 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000556 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
557 MVT::i64 : MVT::i32, Custom);
558 else
559 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
560 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000561
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000562 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000563 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000564 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
566 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000567
Evan Cheng223547a2006-01-31 22:28:30 +0000568 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000569 setOperationAction(ISD::FABS , MVT::f64, Custom);
570 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000571
572 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 setOperationAction(ISD::FNEG , MVT::f64, Custom);
574 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000575
Evan Cheng68c47cb2007-01-05 07:55:56 +0000576 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
578 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000579
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000580 // Lower this to FGETSIGNx86 plus an AND.
581 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
582 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
583
Evan Chengd25e9e82006-02-02 00:28:23 +0000584 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setOperationAction(ISD::FSIN , MVT::f64, Expand);
586 setOperationAction(ISD::FCOS , MVT::f64, Expand);
587 setOperationAction(ISD::FSIN , MVT::f32, Expand);
588 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000589
Chris Lattnera54aa942006-01-29 06:26:08 +0000590 // Expand FP immediates into loads from the stack, except for the special
591 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592 addLegalFPImmediate(APFloat(+0.0)); // xorpd
593 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000594 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000595 // Use SSE for f32, x87 for f64.
596 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
598 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599
600 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602
603 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000605
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000607
608 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
610 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000611
612 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000613 setOperationAction(ISD::FSIN , MVT::f32, Expand);
614 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000615
Nate Begemane1795842008-02-14 08:57:00 +0000616 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000617 addLegalFPImmediate(APFloat(+0.0f)); // xorps
618 addLegalFPImmediate(APFloat(+0.0)); // FLD0
619 addLegalFPImmediate(APFloat(+1.0)); // FLD1
620 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
621 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
622
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000623 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
625 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000626 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000627 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000628 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000629 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
631 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000632
Owen Anderson825b72b2009-08-11 20:47:22 +0000633 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
634 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
635 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
636 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000637
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000638 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
640 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000641 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000642 addLegalFPImmediate(APFloat(+0.0)); // FLD0
643 addLegalFPImmediate(APFloat(+1.0)); // FLD1
644 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
645 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000646 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
647 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
648 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
649 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000650 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000651
Cameron Zwarich33390842011-07-08 21:39:21 +0000652 // We don't support FMA.
653 setOperationAction(ISD::FMA, MVT::f64, Expand);
654 setOperationAction(ISD::FMA, MVT::f32, Expand);
655
Dale Johannesen59a58732007-08-05 18:49:15 +0000656 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000657 if (!TM.Options.UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000658 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
659 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
660 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000661 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000662 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000663 addLegalFPImmediate(TmpFlt); // FLD0
664 TmpFlt.changeSign();
665 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000666
667 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000668 APFloat TmpFlt2(+1.0);
669 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
670 &ignored);
671 addLegalFPImmediate(TmpFlt2); // FLD1
672 TmpFlt2.changeSign();
673 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
674 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000675
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000676 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
678 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000679 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000680
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000681 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
682 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
683 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
684 setOperationAction(ISD::FRINT, MVT::f80, Expand);
685 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000686 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000687 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000688
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000689 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000690 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
691 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
692 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000693
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::FLOG, MVT::f80, Expand);
695 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
696 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
697 setOperationAction(ISD::FEXP, MVT::f80, Expand);
698 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000699
Mon P Wangf007a8b2008-11-06 05:31:54 +0000700 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000701 // (for widening) or expand (for scalarization). Then we will selectively
702 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
704 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
705 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000721 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
722 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000723 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000737 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000738 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000739 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000740 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
745 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000746 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
748 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
749 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
750 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
751 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
752 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
753 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
754 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
755 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000756 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000757 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
758 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
759 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
760 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000761 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000762 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
763 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
764 setTruncStoreAction((MVT::SimpleValueType)VT,
765 (MVT::SimpleValueType)InnerVT, Expand);
766 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
767 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
768 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000769 }
770
Evan Chengc7ce29b2009-02-13 22:36:38 +0000771 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
772 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000773 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000774 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000775 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000776 }
777
Dale Johannesen0488fb62010-09-30 23:57:10 +0000778 // MMX-sized vectors (other than x86mmx) are expected to be expanded
779 // into smaller operations.
780 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
781 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
782 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
783 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
784 setOperationAction(ISD::AND, MVT::v8i8, Expand);
785 setOperationAction(ISD::AND, MVT::v4i16, Expand);
786 setOperationAction(ISD::AND, MVT::v2i32, Expand);
787 setOperationAction(ISD::AND, MVT::v1i64, Expand);
788 setOperationAction(ISD::OR, MVT::v8i8, Expand);
789 setOperationAction(ISD::OR, MVT::v4i16, Expand);
790 setOperationAction(ISD::OR, MVT::v2i32, Expand);
791 setOperationAction(ISD::OR, MVT::v1i64, Expand);
792 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
793 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
794 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
795 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
796 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
797 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
798 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
799 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
800 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
801 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
802 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
803 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
804 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000805 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
806 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
807 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
808 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000809
Craig Topper1accb7e2012-01-10 06:54:16 +0000810 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000812
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
814 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
815 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
816 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
817 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
818 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
819 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
820 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
821 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
822 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
823 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000824 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000825 }
826
Craig Topper1accb7e2012-01-10 06:54:16 +0000827 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000828 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000829
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000830 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
831 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000832 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
833 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
834 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
835 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000836
Owen Anderson825b72b2009-08-11 20:47:22 +0000837 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
838 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
839 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
840 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
841 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
842 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
843 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
844 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
845 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
846 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
847 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
848 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
849 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
850 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
851 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
852 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000853
Nadav Rotem354efd82011-09-18 14:57:03 +0000854 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000855 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
856 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
857 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000858
Owen Anderson825b72b2009-08-11 20:47:22 +0000859 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
860 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
861 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
862 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
863 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000864
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000865 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
866 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
867 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
868 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
869 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
870
Evan Cheng2c3ae372006-04-12 21:21:57 +0000871 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000872 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
873 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000874 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000875 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000876 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000877 // Do not attempt to custom lower non-128-bit vectors
878 if (!VT.is128BitVector())
879 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 setOperationAction(ISD::BUILD_VECTOR,
881 VT.getSimpleVT().SimpleTy, Custom);
882 setOperationAction(ISD::VECTOR_SHUFFLE,
883 VT.getSimpleVT().SimpleTy, Custom);
884 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
885 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000886 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000887
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
889 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
890 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
891 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
892 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
893 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000894
Nate Begemancdd1eec2008-02-12 22:51:28 +0000895 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000896 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
897 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000898 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000899
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000900 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
902 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000903 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000904
905 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000906 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000907 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000908
Owen Andersond6662ad2009-08-10 20:46:15 +0000909 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000910 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000911 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000913 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000915 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000917 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000918 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000919 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000920
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000922
Evan Cheng2c3ae372006-04-12 21:21:57 +0000923 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000924 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
925 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
926 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
927 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000928
Owen Anderson825b72b2009-08-11 20:47:22 +0000929 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
930 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000931 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000932
Craig Topperd0a31172012-01-10 06:37:29 +0000933 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000934 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
935 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
936 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
937 setOperationAction(ISD::FRINT, MVT::f32, Legal);
938 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
939 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
940 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
941 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
942 setOperationAction(ISD::FRINT, MVT::f64, Legal);
943 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
944
Nate Begeman14d12ca2008-02-11 04:19:36 +0000945 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000947
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000948 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
949 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
950 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
951 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
952 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000953
Nate Begeman14d12ca2008-02-11 04:19:36 +0000954 // i8 and i16 vectors are custom , because the source register and source
955 // source memory operand types are not the same width. f32 vectors are
956 // custom since the immediate controlling the insert encodes additional
957 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000958 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
959 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
960 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
961 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000962
Owen Anderson825b72b2009-08-11 20:47:22 +0000963 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
964 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
965 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
966 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000967
Pete Coopera77214a2011-11-14 19:38:42 +0000968 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +0000969 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +0000970 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000971 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
972 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000973 }
974 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000975
Craig Topper1accb7e2012-01-10 06:54:16 +0000976 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000977 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000978 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000979
Nadav Rotem43012222011-05-11 08:12:09 +0000980 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000981 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000982
Nadav Rotem43012222011-05-11 08:12:09 +0000983 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000984 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000985
986 if (Subtarget->hasAVX2()) {
987 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
988 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
989
990 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
991 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
992
993 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
994 } else {
995 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
996 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
997
998 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
999 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1000
1001 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1002 }
Nadav Rotem43012222011-05-11 08:12:09 +00001003 }
1004
Craig Topperd0a31172012-01-10 06:37:29 +00001005 if (Subtarget->hasSSE42())
Duncan Sands28b77e92011-09-06 19:07:46 +00001006 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +00001007
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001008 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +00001009 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
1010 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
1011 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
1012 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
1013 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
1014 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +00001015
Owen Anderson825b72b2009-08-11 20:47:22 +00001016 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001017 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1018 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001019
Owen Anderson825b72b2009-08-11 20:47:22 +00001020 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1021 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1022 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1023 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1024 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
1025 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001026
Owen Anderson825b72b2009-08-11 20:47:22 +00001027 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1028 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1029 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1030 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1031 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1032 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001033
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001034 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1035 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001036 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001037
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00001038 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
1039 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
1040 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
1041 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
1042 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1043 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1044
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001045 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1046 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1047
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001048 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1049 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1050
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001051 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001052 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001053
Duncan Sands28b77e92011-09-06 19:07:46 +00001054 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1055 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1056 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1057 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001058
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001059 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1060 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1061 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1062
Craig Topperaaa643c2011-11-09 07:28:55 +00001063 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1064 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1065 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1066 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001067
Craig Topperaaa643c2011-11-09 07:28:55 +00001068 if (Subtarget->hasAVX2()) {
1069 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1070 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1071 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1072 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001073
Craig Topperaaa643c2011-11-09 07:28:55 +00001074 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1075 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1076 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1077 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001078
Craig Topperaaa643c2011-11-09 07:28:55 +00001079 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1080 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1081 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001082 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001083
1084 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001085
1086 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1087 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1088
1089 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1090 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1091
1092 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001093 } else {
1094 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1095 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1096 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1097 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1098
1099 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1100 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1101 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1102 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1103
1104 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1105 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1106 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1107 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001108
1109 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1110 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1111
1112 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1113 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1114
1115 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001116 }
Craig Topper13894fa2011-08-24 06:14:18 +00001117
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001118 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001119 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001120 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1121 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1122 EVT VT = SVT;
1123
1124 // Extract subvector is special because the value type
1125 // (result) is 128-bit but the source is 256-bit wide.
1126 if (VT.is128BitVector())
1127 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1128
1129 // Do not attempt to custom lower other non-256-bit vectors
1130 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001131 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001132
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001133 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1134 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1135 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1136 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001137 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001138 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001139 }
1140
David Greene54d8eba2011-01-27 22:38:56 +00001141 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001142 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1143 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1144 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001145
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001146 // Do not attempt to promote non-256-bit vectors
1147 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001148 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001149
1150 setOperationAction(ISD::AND, SVT, Promote);
1151 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1152 setOperationAction(ISD::OR, SVT, Promote);
1153 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1154 setOperationAction(ISD::XOR, SVT, Promote);
1155 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1156 setOperationAction(ISD::LOAD, SVT, Promote);
1157 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1158 setOperationAction(ISD::SELECT, SVT, Promote);
1159 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001160 }
David Greene9b9838d2009-06-29 16:47:10 +00001161 }
1162
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001163 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1164 // of this type with custom code.
1165 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1166 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001167 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1168 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001169 }
1170
Evan Cheng6be2c582006-04-05 23:38:46 +00001171 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001172 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001173
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001174
Eli Friedman962f5492010-06-02 19:35:46 +00001175 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1176 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001177 //
Eli Friedman962f5492010-06-02 19:35:46 +00001178 // FIXME: We really should do custom legalization for addition and
1179 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1180 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001181 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1182 // Add/Sub/Mul with overflow operations are custom lowered.
1183 MVT VT = IntVTs[i];
1184 setOperationAction(ISD::SADDO, VT, Custom);
1185 setOperationAction(ISD::UADDO, VT, Custom);
1186 setOperationAction(ISD::SSUBO, VT, Custom);
1187 setOperationAction(ISD::USUBO, VT, Custom);
1188 setOperationAction(ISD::SMULO, VT, Custom);
1189 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001190 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001191
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001192 // There are no 8-bit 3-address imul/mul instructions
1193 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1194 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001195
Evan Chengd54f2d52009-03-31 19:38:51 +00001196 if (!Subtarget->is64Bit()) {
1197 // These libcalls are not available in 32-bit.
1198 setLibcallName(RTLIB::SHL_I128, 0);
1199 setLibcallName(RTLIB::SRL_I128, 0);
1200 setLibcallName(RTLIB::SRA_I128, 0);
1201 }
1202
Evan Cheng206ee9d2006-07-07 08:33:52 +00001203 // We have target-specific dag combine patterns for the following nodes:
1204 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001205 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001206 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001207 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001208 setTargetDAGCombine(ISD::SHL);
1209 setTargetDAGCombine(ISD::SRA);
1210 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001211 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001212 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001213 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001214 setTargetDAGCombine(ISD::FADD);
1215 setTargetDAGCombine(ISD::FSUB);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001216 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001217 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001218 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001219 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001220 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001221 if (Subtarget->is64Bit())
1222 setTargetDAGCombine(ISD::MUL);
Craig Topperb4c94572011-10-21 06:55:01 +00001223 if (Subtarget->hasBMI())
1224 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001225
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001226 computeRegisterProperties();
1227
Evan Cheng05219282011-01-06 06:52:41 +00001228 // On Darwin, -Os means optimize for size without hurting performance,
1229 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001230 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001231 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001232 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001233 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1234 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1235 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001236 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001237 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001238
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001239 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001240}
1241
Scott Michel5b8f82e2008-03-10 15:42:14 +00001242
Duncan Sands28b77e92011-09-06 19:07:46 +00001243EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1244 if (!VT.isVector()) return MVT::i8;
1245 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001246}
1247
1248
Evan Cheng29286502008-01-23 23:17:41 +00001249/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1250/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001251static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001252 if (MaxAlign == 16)
1253 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001254 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001255 if (VTy->getBitWidth() == 128)
1256 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001257 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001258 unsigned EltAlign = 0;
1259 getMaxByValAlign(ATy->getElementType(), EltAlign);
1260 if (EltAlign > MaxAlign)
1261 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001262 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001263 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1264 unsigned EltAlign = 0;
1265 getMaxByValAlign(STy->getElementType(i), EltAlign);
1266 if (EltAlign > MaxAlign)
1267 MaxAlign = EltAlign;
1268 if (MaxAlign == 16)
1269 break;
1270 }
1271 }
1272 return;
1273}
1274
1275/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1276/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001277/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1278/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001279unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001280 if (Subtarget->is64Bit()) {
1281 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001282 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001283 if (TyAlign > 8)
1284 return TyAlign;
1285 return 8;
1286 }
1287
Evan Cheng29286502008-01-23 23:17:41 +00001288 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001289 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001290 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001291 return Align;
1292}
Chris Lattner2b02a442007-02-25 08:29:00 +00001293
Evan Chengf0df0312008-05-15 08:39:06 +00001294/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001295/// and store operations as a result of memset, memcpy, and memmove
1296/// lowering. If DstAlign is zero that means it's safe to destination
1297/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1298/// means there isn't a need to check it against alignment requirement,
1299/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001300/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001301/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1302/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1303/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001304/// It returns EVT::Other if the type should be determined using generic
1305/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001306EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001307X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1308 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001309 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001310 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001311 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001312 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1313 // linux. This is because the stack realignment code can't handle certain
1314 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001315 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001316 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001317 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001318 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001319 (Subtarget->isUnalignedMemAccessFast() ||
1320 ((DstAlign == 0 || DstAlign >= 16) &&
1321 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001322 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001323 if (Subtarget->getStackAlignment() >= 32) {
1324 if (Subtarget->hasAVX2())
1325 return MVT::v8i32;
1326 if (Subtarget->hasAVX())
1327 return MVT::v8f32;
1328 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001329 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001330 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001331 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001332 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001333 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001334 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001335 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001336 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001337 // Do not use f64 to lower memcpy if source is string constant. It's
1338 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001339 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001340 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001341 }
Evan Chengf0df0312008-05-15 08:39:06 +00001342 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001343 return MVT::i64;
1344 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001345}
1346
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001347/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1348/// current function. The returned value is a member of the
1349/// MachineJumpTableInfo::JTEntryKind enum.
1350unsigned X86TargetLowering::getJumpTableEncoding() const {
1351 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1352 // symbol.
1353 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1354 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001355 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001356
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001357 // Otherwise, use the normal jump table encoding heuristics.
1358 return TargetLowering::getJumpTableEncoding();
1359}
1360
Chris Lattnerc64daab2010-01-26 05:02:42 +00001361const MCExpr *
1362X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1363 const MachineBasicBlock *MBB,
1364 unsigned uid,MCContext &Ctx) const{
1365 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1366 Subtarget->isPICStyleGOT());
1367 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1368 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001369 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1370 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001371}
1372
Evan Chengcc415862007-11-09 01:32:10 +00001373/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1374/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001375SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001376 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001377 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001378 // This doesn't have DebugLoc associated with it, but is not really the
1379 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001380 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001381 return Table;
1382}
1383
Chris Lattner589c6f62010-01-26 06:28:43 +00001384/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1385/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1386/// MCExpr.
1387const MCExpr *X86TargetLowering::
1388getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1389 MCContext &Ctx) const {
1390 // X86-64 uses RIP relative addressing based on the jump table label.
1391 if (Subtarget->isPICStyleRIPRel())
1392 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1393
1394 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001395 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001396}
1397
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001398// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001399std::pair<const TargetRegisterClass*, uint8_t>
1400X86TargetLowering::findRepresentativeClass(EVT VT) const{
1401 const TargetRegisterClass *RRC = 0;
1402 uint8_t Cost = 1;
1403 switch (VT.getSimpleVT().SimpleTy) {
1404 default:
1405 return TargetLowering::findRepresentativeClass(VT);
1406 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1407 RRC = (Subtarget->is64Bit()
1408 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1409 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001410 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001411 RRC = X86::VR64RegisterClass;
1412 break;
1413 case MVT::f32: case MVT::f64:
1414 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1415 case MVT::v4f32: case MVT::v2f64:
1416 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1417 case MVT::v4f64:
1418 RRC = X86::VR128RegisterClass;
1419 break;
1420 }
1421 return std::make_pair(RRC, Cost);
1422}
1423
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001424bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1425 unsigned &Offset) const {
1426 if (!Subtarget->isTargetLinux())
1427 return false;
1428
1429 if (Subtarget->is64Bit()) {
1430 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1431 Offset = 0x28;
1432 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1433 AddressSpace = 256;
1434 else
1435 AddressSpace = 257;
1436 } else {
1437 // %gs:0x14 on i386
1438 Offset = 0x14;
1439 AddressSpace = 256;
1440 }
1441 return true;
1442}
1443
1444
Chris Lattner2b02a442007-02-25 08:29:00 +00001445//===----------------------------------------------------------------------===//
1446// Return Value Calling Convention Implementation
1447//===----------------------------------------------------------------------===//
1448
Chris Lattner59ed56b2007-02-28 04:55:35 +00001449#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001450
Michael J. Spencerec38de22010-10-10 22:04:20 +00001451bool
Eric Christopher471e4222011-06-08 23:55:35 +00001452X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1453 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001454 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001455 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001456 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001457 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001458 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001459 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001460}
1461
Dan Gohman98ca4f22009-08-05 01:29:28 +00001462SDValue
1463X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001464 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001465 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001466 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001467 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001468 MachineFunction &MF = DAG.getMachineFunction();
1469 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001470
Chris Lattner9774c912007-02-27 05:28:59 +00001471 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001472 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001473 RVLocs, *DAG.getContext());
1474 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001475
Evan Chengdcea1632010-02-04 02:40:39 +00001476 // Add the regs to the liveout set for the function.
1477 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1478 for (unsigned i = 0; i != RVLocs.size(); ++i)
1479 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1480 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001481
Dan Gohman475871a2008-07-27 21:46:04 +00001482 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001483
Dan Gohman475871a2008-07-27 21:46:04 +00001484 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001485 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1486 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001487 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1488 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001489
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001490 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001491 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1492 CCValAssign &VA = RVLocs[i];
1493 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001494 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001495 EVT ValVT = ValToCopy.getValueType();
1496
Dale Johannesenc4510512010-09-24 19:05:48 +00001497 // If this is x86-64, and we disabled SSE, we can't return FP values,
1498 // or SSE or MMX vectors.
1499 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1500 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001501 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001502 report_fatal_error("SSE register return with SSE disabled");
1503 }
1504 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1505 // llvm-gcc has never done it right and no one has noticed, so this
1506 // should be OK for now.
1507 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001508 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001509 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001510
Chris Lattner447ff682008-03-11 03:23:40 +00001511 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1512 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001513 if (VA.getLocReg() == X86::ST0 ||
1514 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001515 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1516 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001517 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001518 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001519 RetOps.push_back(ValToCopy);
1520 // Don't emit a copytoreg.
1521 continue;
1522 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001523
Evan Cheng242b38b2009-02-23 09:03:22 +00001524 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1525 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001526 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001527 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001528 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001529 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001530 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1531 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001532 // If we don't have SSE2 available, convert to v4f32 so the generated
1533 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001534 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001535 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001536 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001537 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001538 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001539
Dale Johannesendd64c412009-02-04 00:33:20 +00001540 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001541 Flag = Chain.getValue(1);
1542 }
Dan Gohman61a92132008-04-21 23:59:07 +00001543
1544 // The x86-64 ABI for returning structs by value requires that we copy
1545 // the sret argument into %rax for the return. We saved the argument into
1546 // a virtual register in the entry block, so now we copy the value out
1547 // and into %rax.
1548 if (Subtarget->is64Bit() &&
1549 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1550 MachineFunction &MF = DAG.getMachineFunction();
1551 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1552 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001553 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001554 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001555 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001556
Dale Johannesendd64c412009-02-04 00:33:20 +00001557 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001558 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001559
1560 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001561 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001562 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001563
Chris Lattner447ff682008-03-11 03:23:40 +00001564 RetOps[0] = Chain; // Update chain.
1565
1566 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001567 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001568 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001569
1570 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001571 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001572}
1573
Evan Cheng3d2125c2010-11-30 23:55:39 +00001574bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1575 if (N->getNumValues() != 1)
1576 return false;
1577 if (!N->hasNUsesOfValue(1, 0))
1578 return false;
1579
1580 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001581 if (Copy->getOpcode() != ISD::CopyToReg &&
1582 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001583 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001584
1585 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001586 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001587 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001588 if (UI->getOpcode() != X86ISD::RET_FLAG)
1589 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001590 HasRet = true;
1591 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001592
Evan Cheng1bf891a2010-12-01 22:59:46 +00001593 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001594}
1595
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001596EVT
1597X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001598 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001599 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001600 // TODO: Is this also valid on 32-bit?
1601 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001602 ReturnMVT = MVT::i8;
1603 else
1604 ReturnMVT = MVT::i32;
1605
1606 EVT MinVT = getRegisterType(Context, ReturnMVT);
1607 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001608}
1609
Dan Gohman98ca4f22009-08-05 01:29:28 +00001610/// LowerCallResult - Lower the result values of a call into the
1611/// appropriate copies out of appropriate physical registers.
1612///
1613SDValue
1614X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001615 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001616 const SmallVectorImpl<ISD::InputArg> &Ins,
1617 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001618 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001619
Chris Lattnere32bbf62007-02-28 07:09:55 +00001620 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001621 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001622 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001623 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1624 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001625 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001626
Chris Lattner3085e152007-02-25 08:59:22 +00001627 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001628 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001629 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001630 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001631
Torok Edwin3f142c32009-02-01 18:15:56 +00001632 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001633 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001634 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001635 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001636 }
1637
Evan Cheng79fb3b42009-02-20 20:43:02 +00001638 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001639
1640 // If this is a call to a function that returns an fp value on the floating
1641 // point stack, we must guarantee the the value is popped from the stack, so
1642 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001643 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001644 // instead.
1645 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1646 // If we prefer to use the value in xmm registers, copy it out as f80 and
1647 // use a truncate to move it from fp stack reg to xmm reg.
1648 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001649 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001650 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1651 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001652 Val = Chain.getValue(0);
1653
1654 // Round the f80 to the right size, which also moves it to the appropriate
1655 // xmm register.
1656 if (CopyVT != VA.getValVT())
1657 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1658 // This truncation won't change the value.
1659 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001660 } else {
1661 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1662 CopyVT, InFlag).getValue(1);
1663 Val = Chain.getValue(0);
1664 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001665 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001666 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001667 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001668
Dan Gohman98ca4f22009-08-05 01:29:28 +00001669 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001670}
1671
1672
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001673//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001674// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001675//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001676// StdCall calling convention seems to be standard for many Windows' API
1677// routines and around. It differs from C calling convention just a little:
1678// callee should clean up the stack, not caller. Symbols should be also
1679// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001680// For info on fast calling convention see Fast Calling Convention (tail call)
1681// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001682
Dan Gohman98ca4f22009-08-05 01:29:28 +00001683/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001684/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001685static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1686 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001687 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001688
Dan Gohman98ca4f22009-08-05 01:29:28 +00001689 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001690}
1691
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001692/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001693/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001694static bool
1695ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1696 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001697 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001698
Dan Gohman98ca4f22009-08-05 01:29:28 +00001699 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001700}
1701
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001702/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1703/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001704/// the specific parameter attribute. The copy will be passed as a byval
1705/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001706static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001707CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001708 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1709 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001710 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001711
Dale Johannesendd64c412009-02-04 00:33:20 +00001712 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001713 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001714 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001715}
1716
Chris Lattner29689432010-03-11 00:22:57 +00001717/// IsTailCallConvention - Return true if the calling convention is one that
1718/// supports tail call optimization.
1719static bool IsTailCallConvention(CallingConv::ID CC) {
1720 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1721}
1722
Evan Cheng485fafc2011-03-21 01:19:09 +00001723bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001724 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001725 return false;
1726
1727 CallSite CS(CI);
1728 CallingConv::ID CalleeCC = CS.getCallingConv();
1729 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1730 return false;
1731
1732 return true;
1733}
1734
Evan Cheng0c439eb2010-01-27 00:07:07 +00001735/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1736/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001737static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1738 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001739 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001740}
1741
Dan Gohman98ca4f22009-08-05 01:29:28 +00001742SDValue
1743X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001744 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001745 const SmallVectorImpl<ISD::InputArg> &Ins,
1746 DebugLoc dl, SelectionDAG &DAG,
1747 const CCValAssign &VA,
1748 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001749 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001750 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001751 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001752 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1753 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001754 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001755 EVT ValVT;
1756
1757 // If value is passed by pointer we have address passed instead of the value
1758 // itself.
1759 if (VA.getLocInfo() == CCValAssign::Indirect)
1760 ValVT = VA.getLocVT();
1761 else
1762 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001763
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001764 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001765 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001766 // In case of tail call optimization mark all arguments mutable. Since they
1767 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001768 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001769 unsigned Bytes = Flags.getByValSize();
1770 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1771 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001772 return DAG.getFrameIndex(FI, getPointerTy());
1773 } else {
1774 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001775 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001776 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1777 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001778 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001779 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001780 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001781}
1782
Dan Gohman475871a2008-07-27 21:46:04 +00001783SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001784X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001785 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001786 bool isVarArg,
1787 const SmallVectorImpl<ISD::InputArg> &Ins,
1788 DebugLoc dl,
1789 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001790 SmallVectorImpl<SDValue> &InVals)
1791 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001792 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001793 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001794
Gordon Henriksen86737662008-01-05 16:56:59 +00001795 const Function* Fn = MF.getFunction();
1796 if (Fn->hasExternalLinkage() &&
1797 Subtarget->isTargetCygMing() &&
1798 Fn->getName() == "main")
1799 FuncInfo->setForceFramePointer(true);
1800
Evan Cheng1bc78042006-04-26 01:20:17 +00001801 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001802 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001803 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001804 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001805
Chris Lattner29689432010-03-11 00:22:57 +00001806 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1807 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001808
Chris Lattner638402b2007-02-28 07:00:42 +00001809 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001810 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001811 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001812 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001813
1814 // Allocate shadow area for Win64
1815 if (IsWin64) {
1816 CCInfo.AllocateStack(32, 8);
1817 }
1818
Duncan Sands45907662010-10-31 13:21:44 +00001819 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001820
Chris Lattnerf39f7712007-02-28 05:46:49 +00001821 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001822 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001823 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1824 CCValAssign &VA = ArgLocs[i];
1825 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1826 // places.
1827 assert(VA.getValNo() != LastVal &&
1828 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001829 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001830 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001831
Chris Lattnerf39f7712007-02-28 05:46:49 +00001832 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001833 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001834 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001835 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001836 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001837 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001838 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001839 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001840 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001841 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001842 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001843 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1844 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001845 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001846 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001847 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001848 RC = X86::VR64RegisterClass;
1849 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001850 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001851
Devang Patel68e6bee2011-02-21 23:21:26 +00001852 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001853 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001854
Chris Lattnerf39f7712007-02-28 05:46:49 +00001855 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1856 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1857 // right size.
1858 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001859 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001860 DAG.getValueType(VA.getValVT()));
1861 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001862 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001863 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001864 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001865 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001866
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001867 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001868 // Handle MMX values passed in XMM regs.
1869 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001870 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1871 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001872 } else
1873 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001874 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001875 } else {
1876 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001877 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001878 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001879
1880 // If value is passed via pointer - do a load.
1881 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001882 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001883 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001884
Dan Gohman98ca4f22009-08-05 01:29:28 +00001885 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001886 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001887
Dan Gohman61a92132008-04-21 23:59:07 +00001888 // The x86-64 ABI for returning structs by value requires that we copy
1889 // the sret argument into %rax for the return. Save the argument into
1890 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001891 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001892 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1893 unsigned Reg = FuncInfo->getSRetReturnReg();
1894 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001895 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001896 FuncInfo->setSRetReturnReg(Reg);
1897 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001898 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001899 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001900 }
1901
Chris Lattnerf39f7712007-02-28 05:46:49 +00001902 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001903 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001904 if (FuncIsMadeTailCallSafe(CallConv,
1905 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001906 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001907
Evan Cheng1bc78042006-04-26 01:20:17 +00001908 // If the function takes variable number of arguments, make a frame index for
1909 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001910 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001911 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1912 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001913 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001914 }
1915 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001916 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1917
1918 // FIXME: We should really autogenerate these arrays
1919 static const unsigned GPR64ArgRegsWin64[] = {
1920 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001921 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001922 static const unsigned GPR64ArgRegs64Bit[] = {
1923 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1924 };
1925 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001926 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1927 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1928 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001929 const unsigned *GPR64ArgRegs;
1930 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001931
1932 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001933 // The XMM registers which might contain var arg parameters are shadowed
1934 // in their paired GPR. So we only need to save the GPR to their home
1935 // slots.
1936 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001937 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001938 } else {
1939 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1940 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001941
Chad Rosier30450e82011-12-22 22:35:21 +00001942 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
1943 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001944 }
1945 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1946 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001947
Devang Patel578efa92009-06-05 21:57:13 +00001948 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00001949 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001950 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001951 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
1952 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001953 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001954 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00001955 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001956 // Kernel mode asks for SSE to be disabled, so don't push them
1957 // on the stack.
1958 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001959
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001960 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001961 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001962 // Get to the caller-allocated home save location. Add 8 to account
1963 // for the return address.
1964 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001965 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001966 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001967 // Fixup to set vararg frame on shadow area (4 x i64).
1968 if (NumIntRegs < 4)
1969 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001970 } else {
1971 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00001972 // registers, then we must store them to their spots on the stack so
1973 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001974 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1975 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1976 FuncInfo->setRegSaveFrameIndex(
1977 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001978 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001979 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001980
Gordon Henriksen86737662008-01-05 16:56:59 +00001981 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001982 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001983 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1984 getPointerTy());
1985 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001986 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001987 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1988 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001989 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001990 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001991 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001992 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001993 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001994 MachinePointerInfo::getFixedStack(
1995 FuncInfo->getRegSaveFrameIndex(), Offset),
1996 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001997 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001998 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001999 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002000
Dan Gohmanface41a2009-08-16 21:24:25 +00002001 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2002 // Now store the XMM (fp + vector) parameter registers.
2003 SmallVector<SDValue, 11> SaveXMMOps;
2004 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002005
Devang Patel68e6bee2011-02-21 23:21:26 +00002006 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002007 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2008 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002009
Dan Gohman1e93df62010-04-17 14:41:14 +00002010 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2011 FuncInfo->getRegSaveFrameIndex()));
2012 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2013 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002014
Dan Gohmanface41a2009-08-16 21:24:25 +00002015 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002016 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00002017 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002018 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2019 SaveXMMOps.push_back(Val);
2020 }
2021 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2022 MVT::Other,
2023 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002024 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002025
2026 if (!MemOps.empty())
2027 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2028 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002029 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002030 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002031
Gordon Henriksen86737662008-01-05 16:56:59 +00002032 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002033 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2034 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002035 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002036 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002037 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002038 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002039 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
2040 ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00002041 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002042 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002043
Gordon Henriksen86737662008-01-05 16:56:59 +00002044 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002045 // RegSaveFrameIndex is X86-64 only.
2046 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002047 if (CallConv == CallingConv::X86_FastCall ||
2048 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002049 // fastcc functions can't have varargs.
2050 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002051 }
Evan Cheng25caf632006-05-23 21:06:34 +00002052
Rafael Espindola76927d752011-08-30 19:39:58 +00002053 FuncInfo->setArgumentStackSize(StackSize);
2054
Dan Gohman98ca4f22009-08-05 01:29:28 +00002055 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002056}
2057
Dan Gohman475871a2008-07-27 21:46:04 +00002058SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002059X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2060 SDValue StackPtr, SDValue Arg,
2061 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002062 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002063 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002064 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002065 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002066 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002067 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002068 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002069
2070 return DAG.getStore(Chain, dl, Arg, PtrOff,
2071 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002072 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002073}
2074
Bill Wendling64e87322009-01-16 19:25:27 +00002075/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002076/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002077SDValue
2078X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002079 SDValue &OutRetAddr, SDValue Chain,
2080 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002081 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002082 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002083 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002084 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002085
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002086 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002087 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002088 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002089 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002090}
2091
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002092/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002093/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002094static SDValue
2095EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002096 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002097 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002098 // Store the return address to the appropriate stack slot.
2099 if (!FPDiff) return Chain;
2100 // Calculate the new stack slot for the return address.
2101 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002102 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002103 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002104 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002105 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002106 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002107 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002108 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002109 return Chain;
2110}
2111
Dan Gohman98ca4f22009-08-05 01:29:28 +00002112SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002113X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002114 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002115 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002116 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002117 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002118 const SmallVectorImpl<ISD::InputArg> &Ins,
2119 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002120 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002121 MachineFunction &MF = DAG.getMachineFunction();
2122 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002123 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002124 bool IsWindows = Subtarget->isTargetWindows();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002125 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002126 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002127
Nick Lewycky22de16d2012-01-19 00:34:10 +00002128 if (MF.getTarget().Options.DisableTailCalls)
2129 isTailCall = false;
2130
Evan Cheng5f941932010-02-05 02:21:12 +00002131 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002132 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002133 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2134 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002135 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002136
2137 // Sibcalls are automatically detected tailcalls which do not require
2138 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002139 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002140 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002141
2142 if (isTailCall)
2143 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002144 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002145
Chris Lattner29689432010-03-11 00:22:57 +00002146 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2147 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002148
Chris Lattner638402b2007-02-28 07:00:42 +00002149 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002150 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002151 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002152 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002153
2154 // Allocate shadow area for Win64
2155 if (IsWin64) {
2156 CCInfo.AllocateStack(32, 8);
2157 }
2158
Duncan Sands45907662010-10-31 13:21:44 +00002159 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002160
Chris Lattner423c5f42007-02-28 05:31:48 +00002161 // Get a count of how many bytes are to be pushed on the stack.
2162 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002163 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002164 // This is a sibcall. The memory operands are available in caller's
2165 // own caller's stack.
2166 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002167 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2168 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002169 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002170
Gordon Henriksen86737662008-01-05 16:56:59 +00002171 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002172 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002173 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002174 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002175 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2176 FPDiff = NumBytesCallerPushed - NumBytes;
2177
2178 // Set the delta of movement of the returnaddr stackslot.
2179 // But only set if delta is greater than previous delta.
2180 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2181 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2182 }
2183
Evan Chengf22f9b32010-02-06 03:28:46 +00002184 if (!IsSibcall)
2185 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002186
Dan Gohman475871a2008-07-27 21:46:04 +00002187 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002188 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002189 if (isTailCall && FPDiff)
2190 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2191 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002192
Dan Gohman475871a2008-07-27 21:46:04 +00002193 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2194 SmallVector<SDValue, 8> MemOpChains;
2195 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002196
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002197 // Walk the register/memloc assignments, inserting copies/loads. In the case
2198 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002199 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2200 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002201 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002202 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002203 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002204 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002205
Chris Lattner423c5f42007-02-28 05:31:48 +00002206 // Promote the value if needed.
2207 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002208 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002209 case CCValAssign::Full: break;
2210 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002211 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002212 break;
2213 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002214 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002215 break;
2216 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002217 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2218 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002219 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002220 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2221 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002222 } else
2223 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2224 break;
2225 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002226 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002227 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002228 case CCValAssign::Indirect: {
2229 // Store the argument.
2230 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002231 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002232 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002233 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002234 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002235 Arg = SpillSlot;
2236 break;
2237 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002238 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002239
Chris Lattner423c5f42007-02-28 05:31:48 +00002240 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002241 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2242 if (isVarArg && IsWin64) {
2243 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2244 // shadow reg if callee is a varargs function.
2245 unsigned ShadowReg = 0;
2246 switch (VA.getLocReg()) {
2247 case X86::XMM0: ShadowReg = X86::RCX; break;
2248 case X86::XMM1: ShadowReg = X86::RDX; break;
2249 case X86::XMM2: ShadowReg = X86::R8; break;
2250 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002251 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002252 if (ShadowReg)
2253 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002254 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002255 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002256 assert(VA.isMemLoc());
2257 if (StackPtr.getNode() == 0)
2258 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2259 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2260 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002261 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002262 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002263
Evan Cheng32fe1032006-05-25 00:59:30 +00002264 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002265 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002266 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002267
Evan Cheng347d5f72006-04-28 21:29:37 +00002268 // Build a sequence of copy-to-reg nodes chained together with token chain
2269 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002270 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002271 // Tail call byval lowering might overwrite argument registers so in case of
2272 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002273 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002274 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002275 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002276 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002277 InFlag = Chain.getValue(1);
2278 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002279
Chris Lattner88e1fd52009-07-09 04:24:46 +00002280 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002281 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2282 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002283 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002284 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2285 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002286 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002287 InFlag);
2288 InFlag = Chain.getValue(1);
2289 } else {
2290 // If we are tail calling and generating PIC/GOT style code load the
2291 // address of the callee into ECX. The value in ecx is used as target of
2292 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2293 // for tail calls on PIC/GOT architectures. Normally we would just put the
2294 // address of GOT into ebx and then call target@PLT. But for tail calls
2295 // ebx would be restored (since ebx is callee saved) before jumping to the
2296 // target@PLT.
2297
2298 // Note: The actual moving to ECX is done further down.
2299 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2300 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2301 !G->getGlobal()->hasProtectedVisibility())
2302 Callee = LowerGlobalAddress(Callee, DAG);
2303 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002304 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002305 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002306 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002307
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002308 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002309 // From AMD64 ABI document:
2310 // For calls that may call functions that use varargs or stdargs
2311 // (prototype-less calls or calls to functions containing ellipsis (...) in
2312 // the declaration) %al is used as hidden argument to specify the number
2313 // of SSE registers used. The contents of %al do not need to match exactly
2314 // the number of registers, but must be an ubound on the number of SSE
2315 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002316
Gordon Henriksen86737662008-01-05 16:56:59 +00002317 // Count the number of XMM registers allocated.
2318 static const unsigned XMMArgRegs[] = {
2319 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2320 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2321 };
2322 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002323 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002324 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002325
Dale Johannesendd64c412009-02-04 00:33:20 +00002326 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002327 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002328 InFlag = Chain.getValue(1);
2329 }
2330
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002331
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002332 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002333 if (isTailCall) {
2334 // Force all the incoming stack arguments to be loaded from the stack
2335 // before any new outgoing arguments are stored to the stack, because the
2336 // outgoing stack slots may alias the incoming argument stack slots, and
2337 // the alias isn't otherwise explicit. This is slightly more conservative
2338 // than necessary, because it means that each store effectively depends
2339 // on every argument instead of just those arguments it would clobber.
2340 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2341
Dan Gohman475871a2008-07-27 21:46:04 +00002342 SmallVector<SDValue, 8> MemOpChains2;
2343 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002344 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002345 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002346 InFlag = SDValue();
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002347 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002348 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2349 CCValAssign &VA = ArgLocs[i];
2350 if (VA.isRegLoc())
2351 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002352 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002353 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002354 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002355 // Create frame index.
2356 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002357 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002358 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002359 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002360
Duncan Sands276dcbd2008-03-21 09:14:45 +00002361 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002362 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002363 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002364 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002365 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002366 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002367 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002368
Dan Gohman98ca4f22009-08-05 01:29:28 +00002369 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2370 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002371 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002372 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002373 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002374 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002375 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002376 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002377 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002378 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002379 }
2380 }
2381
2382 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002383 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002384 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002385
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002386 // Copy arguments to their registers.
2387 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002388 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002389 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002390 InFlag = Chain.getValue(1);
2391 }
Dan Gohman475871a2008-07-27 21:46:04 +00002392 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002393
Gordon Henriksen86737662008-01-05 16:56:59 +00002394 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002395 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002396 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002397 }
2398
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002399 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2400 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2401 // In the 64-bit large code model, we have to make all calls
2402 // through a register, since the call instruction's 32-bit
2403 // pc-relative offset may not be large enough to hold the whole
2404 // address.
2405 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002406 // If the callee is a GlobalAddress node (quite common, every direct call
2407 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2408 // it.
2409
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002410 // We should use extra load for direct calls to dllimported functions in
2411 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002412 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002413 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002414 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002415 bool ExtraLoad = false;
2416 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002417
Chris Lattner48a7d022009-07-09 05:02:21 +00002418 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2419 // external symbols most go through the PLT in PIC mode. If the symbol
2420 // has hidden or protected visibility, or if it is static or local, then
2421 // we don't need to use the PLT - we can directly call it.
2422 if (Subtarget->isTargetELF() &&
2423 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002424 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002425 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002426 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002427 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002428 (!Subtarget->getTargetTriple().isMacOSX() ||
2429 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002430 // PC-relative references to external symbols should go through $stub,
2431 // unless we're building with the leopard linker or later, which
2432 // automatically synthesizes these stubs.
2433 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002434 } else if (Subtarget->isPICStyleRIPRel() &&
2435 isa<Function>(GV) &&
2436 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2437 // If the function is marked as non-lazy, generate an indirect call
2438 // which loads from the GOT directly. This avoids runtime overhead
2439 // at the cost of eager binding (and one extra byte of encoding).
2440 OpFlags = X86II::MO_GOTPCREL;
2441 WrapperKind = X86ISD::WrapperRIP;
2442 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002443 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002444
Devang Patel0d881da2010-07-06 22:08:15 +00002445 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002446 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002447
2448 // Add a wrapper if needed.
2449 if (WrapperKind != ISD::DELETED_NODE)
2450 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2451 // Add extra indirection if needed.
2452 if (ExtraLoad)
2453 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2454 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002455 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002456 }
Bill Wendling056292f2008-09-16 21:48:12 +00002457 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002458 unsigned char OpFlags = 0;
2459
Evan Cheng1bf891a2010-12-01 22:59:46 +00002460 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2461 // external symbols should go through the PLT.
2462 if (Subtarget->isTargetELF() &&
2463 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2464 OpFlags = X86II::MO_PLT;
2465 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002466 (!Subtarget->getTargetTriple().isMacOSX() ||
2467 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002468 // PC-relative references to external symbols should go through $stub,
2469 // unless we're building with the leopard linker or later, which
2470 // automatically synthesizes these stubs.
2471 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002472 }
Eric Christopherfd179292009-08-27 18:07:15 +00002473
Chris Lattner48a7d022009-07-09 05:02:21 +00002474 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2475 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002476 }
2477
Chris Lattnerd96d0722007-02-25 06:40:16 +00002478 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002479 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002480 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002481
Evan Chengf22f9b32010-02-06 03:28:46 +00002482 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002483 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2484 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002485 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002486 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002487
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002488 Ops.push_back(Chain);
2489 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002490
Dan Gohman98ca4f22009-08-05 01:29:28 +00002491 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002492 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002493
Gordon Henriksen86737662008-01-05 16:56:59 +00002494 // Add argument registers to the end of the list so that they are known live
2495 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002496 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2497 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2498 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002499
Evan Cheng586ccac2008-03-18 23:36:35 +00002500 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002501 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002502 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2503
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002504 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002505 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002506 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002507
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002508 // Experimental: Add a register mask operand representing the call-preserved
2509 // registers.
2510 if (UseRegMask) {
2511 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2512 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2513 Ops.push_back(DAG.getRegisterMask(Mask));
2514 }
2515
Gabor Greifba36cb52008-08-28 21:40:38 +00002516 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002517 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002518
Dan Gohman98ca4f22009-08-05 01:29:28 +00002519 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002520 // We used to do:
2521 //// If this is the first return lowered for this function, add the regs
2522 //// to the liveout set for the function.
2523 // This isn't right, although it's probably harmless on x86; liveouts
2524 // should be computed from returns not tail calls. Consider a void
2525 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002526 return DAG.getNode(X86ISD::TC_RETURN, dl,
2527 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002528 }
2529
Dale Johannesenace16102009-02-03 19:33:06 +00002530 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002531 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002532
Chris Lattner2d297092006-05-23 18:50:38 +00002533 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002534 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002535 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2536 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002537 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002538 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
2539 IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002540 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002541 // pops the hidden struct pointer, so we have to push it back.
2542 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002543 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002544 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002545 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002546 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002547
Gordon Henriksenae636f82008-01-03 16:47:34 +00002548 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002549 if (!IsSibcall) {
2550 Chain = DAG.getCALLSEQ_END(Chain,
2551 DAG.getIntPtrConstant(NumBytes, true),
2552 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2553 true),
2554 InFlag);
2555 InFlag = Chain.getValue(1);
2556 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002557
Chris Lattner3085e152007-02-25 08:59:22 +00002558 // Handle result values, copying them out of physregs into vregs that we
2559 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002560 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2561 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002562}
2563
Evan Cheng25ab6902006-09-08 06:48:29 +00002564
2565//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002566// Fast Calling Convention (tail call) implementation
2567//===----------------------------------------------------------------------===//
2568
2569// Like std call, callee cleans arguments, convention except that ECX is
2570// reserved for storing the tail called function address. Only 2 registers are
2571// free for argument passing (inreg). Tail call optimization is performed
2572// provided:
2573// * tailcallopt is enabled
2574// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002575// On X86_64 architecture with GOT-style position independent code only local
2576// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002577// To keep the stack aligned according to platform abi the function
2578// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2579// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002580// If a tail called function callee has more arguments than the caller the
2581// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002582// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002583// original REtADDR, but before the saved framepointer or the spilled registers
2584// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2585// stack layout:
2586// arg1
2587// arg2
2588// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002589// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002590// move area ]
2591// (possible EBP)
2592// ESI
2593// EDI
2594// local1 ..
2595
2596/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2597/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002598unsigned
2599X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2600 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002601 MachineFunction &MF = DAG.getMachineFunction();
2602 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002603 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002604 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002605 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002606 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002607 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002608 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2609 // Number smaller than 12 so just add the difference.
2610 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2611 } else {
2612 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002613 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002614 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002615 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002616 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002617}
2618
Evan Cheng5f941932010-02-05 02:21:12 +00002619/// MatchingStackOffset - Return true if the given stack call argument is
2620/// already available in the same position (relatively) of the caller's
2621/// incoming argument stack.
2622static
2623bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2624 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2625 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002626 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2627 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002628 if (Arg.getOpcode() == ISD::CopyFromReg) {
2629 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002630 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002631 return false;
2632 MachineInstr *Def = MRI->getVRegDef(VR);
2633 if (!Def)
2634 return false;
2635 if (!Flags.isByVal()) {
2636 if (!TII->isLoadFromStackSlot(Def, FI))
2637 return false;
2638 } else {
2639 unsigned Opcode = Def->getOpcode();
2640 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2641 Def->getOperand(1).isFI()) {
2642 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002643 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002644 } else
2645 return false;
2646 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002647 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2648 if (Flags.isByVal())
2649 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002650 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002651 // define @foo(%struct.X* %A) {
2652 // tail call @bar(%struct.X* byval %A)
2653 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002654 return false;
2655 SDValue Ptr = Ld->getBasePtr();
2656 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2657 if (!FINode)
2658 return false;
2659 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002660 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002661 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002662 FI = FINode->getIndex();
2663 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002664 } else
2665 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002666
Evan Cheng4cae1332010-03-05 08:38:04 +00002667 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002668 if (!MFI->isFixedObjectIndex(FI))
2669 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002670 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002671}
2672
Dan Gohman98ca4f22009-08-05 01:29:28 +00002673/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2674/// for tail call optimization. Targets which want to do tail call
2675/// optimization should implement this function.
2676bool
2677X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002678 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002679 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002680 bool isCalleeStructRet,
2681 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002682 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002683 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002684 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002685 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002686 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002687 CalleeCC != CallingConv::C)
2688 return false;
2689
Evan Cheng7096ae42010-01-29 06:45:59 +00002690 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002691 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002692 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002693 CallingConv::ID CallerCC = CallerF->getCallingConv();
2694 bool CCMatch = CallerCC == CalleeCC;
2695
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002696 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002697 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002698 return true;
2699 return false;
2700 }
2701
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002702 // Look for obvious safe cases to perform tail call optimization that do not
2703 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002704
Evan Cheng2c12cb42010-03-26 16:26:03 +00002705 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2706 // emit a special epilogue.
2707 if (RegInfo->needsStackRealignment(MF))
2708 return false;
2709
Evan Chenga375d472010-03-15 18:54:48 +00002710 // Also avoid sibcall optimization if either caller or callee uses struct
2711 // return semantics.
2712 if (isCalleeStructRet || isCallerStructRet)
2713 return false;
2714
Chad Rosier2416da32011-06-24 21:15:36 +00002715 // An stdcall caller is expected to clean up its arguments; the callee
2716 // isn't going to do that.
2717 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2718 return false;
2719
Chad Rosier871f6642011-05-18 19:59:50 +00002720 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002721 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002722 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002723
2724 // Optimizing for varargs on Win64 is unlikely to be safe without
2725 // additional testing.
2726 if (Subtarget->isTargetWin64())
2727 return false;
2728
Chad Rosier871f6642011-05-18 19:59:50 +00002729 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002730 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2731 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002732
Chad Rosier871f6642011-05-18 19:59:50 +00002733 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2734 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2735 if (!ArgLocs[i].isRegLoc())
2736 return false;
2737 }
2738
Chad Rosier30450e82011-12-22 22:35:21 +00002739 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2740 // stack. Therefore, if it's not used by the call it is not safe to optimize
2741 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002742 bool Unused = false;
2743 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2744 if (!Ins[i].Used) {
2745 Unused = true;
2746 break;
2747 }
2748 }
2749 if (Unused) {
2750 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002751 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2752 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002753 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002754 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002755 CCValAssign &VA = RVLocs[i];
2756 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2757 return false;
2758 }
2759 }
2760
Evan Cheng13617962010-04-30 01:12:32 +00002761 // If the calling conventions do not match, then we'd better make sure the
2762 // results are returned in the same way as what the caller expects.
2763 if (!CCMatch) {
2764 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002765 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2766 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002767 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2768
2769 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002770 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2771 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002772 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2773
2774 if (RVLocs1.size() != RVLocs2.size())
2775 return false;
2776 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2777 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2778 return false;
2779 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2780 return false;
2781 if (RVLocs1[i].isRegLoc()) {
2782 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2783 return false;
2784 } else {
2785 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2786 return false;
2787 }
2788 }
2789 }
2790
Evan Chenga6bff982010-01-30 01:22:00 +00002791 // If the callee takes no arguments then go on to check the results of the
2792 // call.
2793 if (!Outs.empty()) {
2794 // Check if stack adjustment is needed. For now, do not do this if any
2795 // argument is passed on the stack.
2796 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002797 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2798 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002799
2800 // Allocate shadow area for Win64
2801 if (Subtarget->isTargetWin64()) {
2802 CCInfo.AllocateStack(32, 8);
2803 }
2804
Duncan Sands45907662010-10-31 13:21:44 +00002805 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002806 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002807 MachineFunction &MF = DAG.getMachineFunction();
2808 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2809 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002810
2811 // Check if the arguments are already laid out in the right way as
2812 // the caller's fixed stack objects.
2813 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002814 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2815 const X86InstrInfo *TII =
2816 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002817 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2818 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002819 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002820 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002821 if (VA.getLocInfo() == CCValAssign::Indirect)
2822 return false;
2823 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002824 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2825 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002826 return false;
2827 }
2828 }
2829 }
Evan Cheng9c044672010-05-29 01:35:22 +00002830
2831 // If the tailcall address may be in a register, then make sure it's
2832 // possible to register allocate for it. In 32-bit, the call address can
2833 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002834 // callee-saved registers are restored. These happen to be the same
2835 // registers used to pass 'inreg' arguments so watch out for those.
2836 if (!Subtarget->is64Bit() &&
2837 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002838 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002839 unsigned NumInRegs = 0;
2840 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2841 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002842 if (!VA.isRegLoc())
2843 continue;
2844 unsigned Reg = VA.getLocReg();
2845 switch (Reg) {
2846 default: break;
2847 case X86::EAX: case X86::EDX: case X86::ECX:
2848 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002849 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002850 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002851 }
2852 }
2853 }
Evan Chenga6bff982010-01-30 01:22:00 +00002854 }
Evan Chengb1712452010-01-27 06:25:16 +00002855
Evan Cheng86809cc2010-02-03 03:28:02 +00002856 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002857}
2858
Dan Gohman3df24e62008-09-03 23:12:08 +00002859FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002860X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2861 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002862}
2863
2864
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002865//===----------------------------------------------------------------------===//
2866// Other Lowering Hooks
2867//===----------------------------------------------------------------------===//
2868
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002869static bool MayFoldLoad(SDValue Op) {
2870 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2871}
2872
2873static bool MayFoldIntoStore(SDValue Op) {
2874 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2875}
2876
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002877static bool isTargetShuffle(unsigned Opcode) {
2878 switch(Opcode) {
2879 default: return false;
2880 case X86ISD::PSHUFD:
2881 case X86ISD::PSHUFHW:
2882 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002883 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002884 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002885 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002886 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002887 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002888 case X86ISD::MOVLPS:
2889 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002890 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002891 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002892 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002893 case X86ISD::MOVSS:
2894 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002895 case X86ISD::UNPCKL:
2896 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002897 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002898 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002899 return true;
2900 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002901}
2902
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002903static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002904 SDValue V1, SelectionDAG &DAG) {
2905 switch(Opc) {
2906 default: llvm_unreachable("Unknown x86 shuffle node");
2907 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002908 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002909 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002910 return DAG.getNode(Opc, dl, VT, V1);
2911 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002912}
2913
2914static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002915 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002916 switch(Opc) {
2917 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002918 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002919 case X86ISD::PSHUFHW:
2920 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002921 case X86ISD::VPERMILP:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002922 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2923 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002924}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002925
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002926static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2927 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2928 switch(Opc) {
2929 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002930 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00002931 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00002932 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002933 return DAG.getNode(Opc, dl, VT, V1, V2,
2934 DAG.getConstant(TargetMask, MVT::i8));
2935 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002936}
2937
2938static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2939 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2940 switch(Opc) {
2941 default: llvm_unreachable("Unknown x86 shuffle node");
2942 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002943 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002944 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002945 case X86ISD::MOVLPS:
2946 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002947 case X86ISD::MOVSS:
2948 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002949 case X86ISD::UNPCKL:
2950 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002951 return DAG.getNode(Opc, dl, VT, V1, V2);
2952 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002953}
2954
Dan Gohmand858e902010-04-17 15:26:15 +00002955SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002956 MachineFunction &MF = DAG.getMachineFunction();
2957 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2958 int ReturnAddrIndex = FuncInfo->getRAIndex();
2959
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002960 if (ReturnAddrIndex == 0) {
2961 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002962 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002963 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002964 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002965 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002966 }
2967
Evan Cheng25ab6902006-09-08 06:48:29 +00002968 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002969}
2970
2971
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002972bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2973 bool hasSymbolicDisplacement) {
2974 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002975 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002976 return false;
2977
2978 // If we don't have a symbolic displacement - we don't have any extra
2979 // restrictions.
2980 if (!hasSymbolicDisplacement)
2981 return true;
2982
2983 // FIXME: Some tweaks might be needed for medium code model.
2984 if (M != CodeModel::Small && M != CodeModel::Kernel)
2985 return false;
2986
2987 // For small code model we assume that latest object is 16MB before end of 31
2988 // bits boundary. We may also accept pretty large negative constants knowing
2989 // that all objects are in the positive half of address space.
2990 if (M == CodeModel::Small && Offset < 16*1024*1024)
2991 return true;
2992
2993 // For kernel code model we know that all object resist in the negative half
2994 // of 32bits address space. We may not accept negative offsets, since they may
2995 // be just off and we may accept pretty large positive ones.
2996 if (M == CodeModel::Kernel && Offset > 0)
2997 return true;
2998
2999 return false;
3000}
3001
Evan Chengef41ff62011-06-23 17:54:54 +00003002/// isCalleePop - Determines whether the callee is required to pop its
3003/// own arguments. Callee pop is necessary to support tail calls.
3004bool X86::isCalleePop(CallingConv::ID CallingConv,
3005 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3006 if (IsVarArg)
3007 return false;
3008
3009 switch (CallingConv) {
3010 default:
3011 return false;
3012 case CallingConv::X86_StdCall:
3013 return !is64Bit;
3014 case CallingConv::X86_FastCall:
3015 return !is64Bit;
3016 case CallingConv::X86_ThisCall:
3017 return !is64Bit;
3018 case CallingConv::Fast:
3019 return TailCallOpt;
3020 case CallingConv::GHC:
3021 return TailCallOpt;
3022 }
3023}
3024
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003025/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3026/// specific condition code, returning the condition code and the LHS/RHS of the
3027/// comparison to make.
3028static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3029 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003030 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003031 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3032 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3033 // X > -1 -> X == 0, jump !sign.
3034 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003035 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003036 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
3037 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003038 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00003039 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003040 // X < 1 -> X <= 0
3041 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003042 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003043 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003044 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003045
Evan Chengd9558e02006-01-06 00:43:03 +00003046 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003047 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003048 case ISD::SETEQ: return X86::COND_E;
3049 case ISD::SETGT: return X86::COND_G;
3050 case ISD::SETGE: return X86::COND_GE;
3051 case ISD::SETLT: return X86::COND_L;
3052 case ISD::SETLE: return X86::COND_LE;
3053 case ISD::SETNE: return X86::COND_NE;
3054 case ISD::SETULT: return X86::COND_B;
3055 case ISD::SETUGT: return X86::COND_A;
3056 case ISD::SETULE: return X86::COND_BE;
3057 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003058 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003059 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003060
Chris Lattner4c78e022008-12-23 23:42:27 +00003061 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003062
Chris Lattner4c78e022008-12-23 23:42:27 +00003063 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003064 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3065 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003066 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3067 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003068 }
3069
Chris Lattner4c78e022008-12-23 23:42:27 +00003070 switch (SetCCOpcode) {
3071 default: break;
3072 case ISD::SETOLT:
3073 case ISD::SETOLE:
3074 case ISD::SETUGT:
3075 case ISD::SETUGE:
3076 std::swap(LHS, RHS);
3077 break;
3078 }
3079
3080 // On a floating point condition, the flags are set as follows:
3081 // ZF PF CF op
3082 // 0 | 0 | 0 | X > Y
3083 // 0 | 0 | 1 | X < Y
3084 // 1 | 0 | 0 | X == Y
3085 // 1 | 1 | 1 | unordered
3086 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003087 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003088 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003089 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003090 case ISD::SETOLT: // flipped
3091 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003092 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003093 case ISD::SETOLE: // flipped
3094 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003095 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003096 case ISD::SETUGT: // flipped
3097 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003098 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003099 case ISD::SETUGE: // flipped
3100 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003101 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003102 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003103 case ISD::SETNE: return X86::COND_NE;
3104 case ISD::SETUO: return X86::COND_P;
3105 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003106 case ISD::SETOEQ:
3107 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003108 }
Evan Chengd9558e02006-01-06 00:43:03 +00003109}
3110
Evan Cheng4a460802006-01-11 00:33:36 +00003111/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3112/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003113/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003114static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003115 switch (X86CC) {
3116 default:
3117 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003118 case X86::COND_B:
3119 case X86::COND_BE:
3120 case X86::COND_E:
3121 case X86::COND_P:
3122 case X86::COND_A:
3123 case X86::COND_AE:
3124 case X86::COND_NE:
3125 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003126 return true;
3127 }
3128}
3129
Evan Chengeb2f9692009-10-27 19:56:55 +00003130/// isFPImmLegal - Returns true if the target can instruction select the
3131/// specified FP immediate natively. If false, the legalizer will
3132/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003133bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003134 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3135 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3136 return true;
3137 }
3138 return false;
3139}
3140
Nate Begeman9008ca62009-04-27 18:41:29 +00003141/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3142/// the specified range (L, H].
3143static bool isUndefOrInRange(int Val, int Low, int Hi) {
3144 return (Val < 0) || (Val >= Low && Val < Hi);
3145}
3146
3147/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3148/// specified value.
3149static bool isUndefOrEqual(int Val, int CmpVal) {
3150 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003151 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003152 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003153}
3154
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003155/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3156/// from position Pos and ending in Pos+Size, falls within the specified
3157/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003158static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003159 int Pos, int Size, int Low) {
3160 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3161 if (!isUndefOrEqual(Mask[i], Low))
3162 return false;
3163 return true;
3164}
3165
Nate Begeman9008ca62009-04-27 18:41:29 +00003166/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3167/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3168/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003169static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003170 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003171 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003172 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003173 return (Mask[0] < 2 && Mask[1] < 2);
3174 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003175}
3176
Nate Begeman9008ca62009-04-27 18:41:29 +00003177bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003178 return ::isPSHUFDMask(N->getMask(), N->getValueType(0));
Nate Begeman9008ca62009-04-27 18:41:29 +00003179}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003180
Nate Begeman9008ca62009-04-27 18:41:29 +00003181/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3182/// is suitable for input to PSHUFHW.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003183static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003184 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003185 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003186
Nate Begeman9008ca62009-04-27 18:41:29 +00003187 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003188 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3189 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003190
Evan Cheng506d3df2006-03-29 23:07:14 +00003191 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003192 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003193 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003194 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003195
Evan Cheng506d3df2006-03-29 23:07:14 +00003196 return true;
3197}
3198
Nate Begeman9008ca62009-04-27 18:41:29 +00003199bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003200 return ::isPSHUFHWMask(N->getMask(), N->getValueType(0));
Nate Begeman9008ca62009-04-27 18:41:29 +00003201}
Evan Cheng506d3df2006-03-29 23:07:14 +00003202
Nate Begeman9008ca62009-04-27 18:41:29 +00003203/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3204/// is suitable for input to PSHUFLW.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003205static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003206 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003207 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003208
Rafael Espindola15684b22009-04-24 12:40:33 +00003209 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003210 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3211 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003212
Rafael Espindola15684b22009-04-24 12:40:33 +00003213 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003214 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003215 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003216 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003217
Rafael Espindola15684b22009-04-24 12:40:33 +00003218 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003219}
3220
Nate Begeman9008ca62009-04-27 18:41:29 +00003221bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003222 return ::isPSHUFLWMask(N->getMask(), N->getValueType(0));
Nate Begeman9008ca62009-04-27 18:41:29 +00003223}
3224
Nate Begemana09008b2009-10-19 02:17:23 +00003225/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3226/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003227static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3228 const X86Subtarget *Subtarget) {
3229 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3230 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003231 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003232
Craig Topper0e2037b2012-01-20 05:53:00 +00003233 unsigned NumElts = VT.getVectorNumElements();
3234 unsigned NumLanes = VT.getSizeInBits()/128;
3235 unsigned NumLaneElts = NumElts/NumLanes;
3236
3237 // Do not handle 64-bit element shuffles with palignr.
3238 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003239 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003240
Craig Topper0e2037b2012-01-20 05:53:00 +00003241 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3242 unsigned i;
3243 for (i = 0; i != NumLaneElts; ++i) {
3244 if (Mask[i+l] >= 0)
3245 break;
3246 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003247
Craig Topper0e2037b2012-01-20 05:53:00 +00003248 // Lane is all undef, go to next lane
3249 if (i == NumLaneElts)
3250 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003251
Craig Topper0e2037b2012-01-20 05:53:00 +00003252 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003253
Craig Topper0e2037b2012-01-20 05:53:00 +00003254 // Make sure its in this lane in one of the sources
3255 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3256 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003257 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003258
3259 // If not lane 0, then we must match lane 0
3260 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3261 return false;
3262
3263 // Correct second source to be contiguous with first source
3264 if (Start >= (int)NumElts)
3265 Start -= NumElts - NumLaneElts;
3266
3267 // Make sure we're shifting in the right direction.
3268 if (Start <= (int)(i+l))
3269 return false;
3270
3271 Start -= i;
3272
3273 // Check the rest of the elements to see if they are consecutive.
3274 for (++i; i != NumLaneElts; ++i) {
3275 int Idx = Mask[i+l];
3276
3277 // Make sure its in this lane
3278 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3279 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3280 return false;
3281
3282 // If not lane 0, then we must match lane 0
3283 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3284 return false;
3285
3286 if (Idx >= (int)NumElts)
3287 Idx -= NumElts - NumLaneElts;
3288
3289 if (!isUndefOrEqual(Idx, Start+i))
3290 return false;
3291
3292 }
Nate Begemana09008b2009-10-19 02:17:23 +00003293 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003294
Nate Begemana09008b2009-10-19 02:17:23 +00003295 return true;
3296}
3297
Craig Topper1a7700a2012-01-19 08:19:12 +00003298/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3299/// the two vector operands have swapped position.
3300static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3301 unsigned NumElems) {
3302 for (unsigned i = 0; i != NumElems; ++i) {
3303 int idx = Mask[i];
3304 if (idx < 0)
3305 continue;
3306 else if (idx < (int)NumElems)
3307 Mask[i] = idx + NumElems;
3308 else
3309 Mask[i] = idx - NumElems;
3310 }
3311}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003312
Craig Topper1a7700a2012-01-19 08:19:12 +00003313/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3314/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3315/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3316/// reverse of what x86 shuffles want.
3317static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3318 bool Commuted = false) {
3319 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003320 return false;
3321
Craig Topper1a7700a2012-01-19 08:19:12 +00003322 unsigned NumElems = VT.getVectorNumElements();
3323 unsigned NumLanes = VT.getSizeInBits()/128;
3324 unsigned NumLaneElems = NumElems/NumLanes;
3325
3326 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003327 return false;
3328
3329 // VSHUFPSY divides the resulting vector into 4 chunks.
3330 // The sources are also splitted into 4 chunks, and each destination
3331 // chunk must come from a different source chunk.
3332 //
3333 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3334 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3335 //
3336 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3337 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3338 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003339 // VSHUFPDY divides the resulting vector into 4 chunks.
3340 // The sources are also splitted into 4 chunks, and each destination
3341 // chunk must come from a different source chunk.
3342 //
3343 // SRC1 => X3 X2 X1 X0
3344 // SRC2 => Y3 Y2 Y1 Y0
3345 //
3346 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3347 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003348 unsigned HalfLaneElems = NumLaneElems/2;
3349 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3350 for (unsigned i = 0; i != NumLaneElems; ++i) {
3351 int Idx = Mask[i+l];
3352 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3353 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3354 return false;
3355 // For VSHUFPSY, the mask of the second half must be the same as the
3356 // first but with the appropriate offsets. This works in the same way as
3357 // VPERMILPS works with masks.
3358 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3359 continue;
3360 if (!isUndefOrEqual(Idx, Mask[i]+l))
3361 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003362 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003363 }
3364
3365 return true;
3366}
3367
Craig Topper1a7700a2012-01-19 08:19:12 +00003368bool X86::isSHUFPMask(ShuffleVectorSDNode *N, bool HasAVX) {
3369 return ::isSHUFPMask(N->getMask(), N->getValueType(0), HasAVX);
Evan Cheng39623da2006-04-20 08:58:49 +00003370}
3371
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003372/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3373/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003374bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003375 EVT VT = N->getValueType(0);
3376 unsigned NumElems = VT.getVectorNumElements();
3377
3378 if (VT.getSizeInBits() != 128)
3379 return false;
3380
3381 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003382 return false;
3383
Evan Cheng2064a2b2006-03-28 06:50:32 +00003384 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003385 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3386 isUndefOrEqual(N->getMaskElt(1), 7) &&
3387 isUndefOrEqual(N->getMaskElt(2), 2) &&
3388 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003389}
3390
Nate Begeman0b10b912009-11-07 23:17:15 +00003391/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3392/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3393/// <2, 3, 2, 3>
3394bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003395 EVT VT = N->getValueType(0);
3396 unsigned NumElems = VT.getVectorNumElements();
3397
3398 if (VT.getSizeInBits() != 128)
3399 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003400
Nate Begeman0b10b912009-11-07 23:17:15 +00003401 if (NumElems != 4)
3402 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003403
Nate Begeman0b10b912009-11-07 23:17:15 +00003404 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003405 isUndefOrEqual(N->getMaskElt(1), 3) &&
3406 isUndefOrEqual(N->getMaskElt(2), 2) &&
3407 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003408}
3409
Evan Cheng5ced1d82006-04-06 23:23:56 +00003410/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3411/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003412bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003413 EVT VT = N->getValueType(0);
3414
3415 if (VT.getSizeInBits() != 128)
3416 return false;
3417
Nate Begeman9008ca62009-04-27 18:41:29 +00003418 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003419
Evan Cheng5ced1d82006-04-06 23:23:56 +00003420 if (NumElems != 2 && NumElems != 4)
3421 return false;
3422
Evan Chengc5cdff22006-04-07 21:53:05 +00003423 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003424 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003425 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003426
Evan Chengc5cdff22006-04-07 21:53:05 +00003427 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003428 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003429 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003430
3431 return true;
3432}
3433
Nate Begeman0b10b912009-11-07 23:17:15 +00003434/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3435/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3436bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003437 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003438
David Greenea20244d2011-03-02 17:23:43 +00003439 if ((NumElems != 2 && NumElems != 4)
3440 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003441 return false;
3442
Evan Chengc5cdff22006-04-07 21:53:05 +00003443 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003444 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003445 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003446
Nate Begeman9008ca62009-04-27 18:41:29 +00003447 for (unsigned i = 0; i < NumElems/2; ++i)
3448 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003449 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003450
3451 return true;
3452}
3453
Evan Cheng0038e592006-03-28 00:39:58 +00003454/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3455/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003456static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003457 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003458 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003459
3460 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3461 "Unsupported vector type for unpckh");
3462
Craig Topper6347e862011-11-21 06:57:39 +00003463 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003464 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003465 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003466
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003467 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3468 // independently on 128-bit lanes.
3469 unsigned NumLanes = VT.getSizeInBits()/128;
3470 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003471
Craig Topper94438ba2011-12-16 08:06:31 +00003472 for (unsigned l = 0; l != NumLanes; ++l) {
3473 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3474 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003475 i += 2, ++j) {
3476 int BitI = Mask[i];
3477 int BitI1 = Mask[i+1];
3478 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003479 return false;
David Greenea20244d2011-03-02 17:23:43 +00003480 if (V2IsSplat) {
3481 if (!isUndefOrEqual(BitI1, NumElts))
3482 return false;
3483 } else {
3484 if (!isUndefOrEqual(BitI1, j + NumElts))
3485 return false;
3486 }
Evan Cheng39623da2006-04-20 08:58:49 +00003487 }
Evan Cheng0038e592006-03-28 00:39:58 +00003488 }
David Greenea20244d2011-03-02 17:23:43 +00003489
Evan Cheng0038e592006-03-28 00:39:58 +00003490 return true;
3491}
3492
Craig Topper6347e862011-11-21 06:57:39 +00003493bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003494 return ::isUNPCKLMask(N->getMask(), N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003495}
3496
Evan Cheng4fcb9222006-03-28 02:43:26 +00003497/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3498/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003499static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003500 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003501 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003502
3503 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3504 "Unsupported vector type for unpckh");
3505
Craig Topper6347e862011-11-21 06:57:39 +00003506 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003507 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003508 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003509
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003510 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3511 // independently on 128-bit lanes.
3512 unsigned NumLanes = VT.getSizeInBits()/128;
3513 unsigned NumLaneElts = NumElts/NumLanes;
3514
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003515 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003516 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3517 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003518 int BitI = Mask[i];
3519 int BitI1 = Mask[i+1];
3520 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003521 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003522 if (V2IsSplat) {
3523 if (isUndefOrEqual(BitI1, NumElts))
3524 return false;
3525 } else {
3526 if (!isUndefOrEqual(BitI1, j+NumElts))
3527 return false;
3528 }
Evan Cheng39623da2006-04-20 08:58:49 +00003529 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003530 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003531 return true;
3532}
3533
Craig Topper6347e862011-11-21 06:57:39 +00003534bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003535 return ::isUNPCKHMask(N->getMask(), N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003536}
3537
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003538/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3539/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3540/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003541static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003542 bool HasAVX2) {
3543 unsigned NumElts = VT.getVectorNumElements();
3544
3545 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3546 "Unsupported vector type for unpckh");
3547
3548 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3549 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003550 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003551
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003552 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3553 // FIXME: Need a better way to get rid of this, there's no latency difference
3554 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3555 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003556 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003557 return false;
3558
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003559 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3560 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003561 unsigned NumLanes = VT.getSizeInBits()/128;
3562 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003563
Craig Topper94438ba2011-12-16 08:06:31 +00003564 for (unsigned l = 0; l != NumLanes; ++l) {
3565 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3566 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003567 i += 2, ++j) {
3568 int BitI = Mask[i];
3569 int BitI1 = Mask[i+1];
3570
3571 if (!isUndefOrEqual(BitI, j))
3572 return false;
3573 if (!isUndefOrEqual(BitI1, j))
3574 return false;
3575 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003576 }
David Greenea20244d2011-03-02 17:23:43 +00003577
Rafael Espindola15684b22009-04-24 12:40:33 +00003578 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003579}
3580
Craig Topper94438ba2011-12-16 08:06:31 +00003581bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N, bool HasAVX2) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003582 return ::isUNPCKL_v_undef_Mask(N->getMask(), N->getValueType(0), HasAVX2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003583}
3584
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003585/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3586/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3587/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003588static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003589 unsigned NumElts = VT.getVectorNumElements();
3590
3591 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3592 "Unsupported vector type for unpckh");
3593
3594 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3595 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003596 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003597
Craig Topper94438ba2011-12-16 08:06:31 +00003598 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3599 // independently on 128-bit lanes.
3600 unsigned NumLanes = VT.getSizeInBits()/128;
3601 unsigned NumLaneElts = NumElts/NumLanes;
3602
3603 for (unsigned l = 0; l != NumLanes; ++l) {
3604 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3605 i != (l+1)*NumLaneElts; i += 2, ++j) {
3606 int BitI = Mask[i];
3607 int BitI1 = Mask[i+1];
3608 if (!isUndefOrEqual(BitI, j))
3609 return false;
3610 if (!isUndefOrEqual(BitI1, j))
3611 return false;
3612 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003613 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003614 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003615}
3616
Craig Topper94438ba2011-12-16 08:06:31 +00003617bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N, bool HasAVX2) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003618 return ::isUNPCKH_v_undef_Mask(N->getMask(), N->getValueType(0), HasAVX2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003619}
3620
Evan Cheng017dcc62006-04-21 01:05:10 +00003621/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3622/// specifies a shuffle of elements that is suitable for input to MOVSS,
3623/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003624static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003625 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003626 return false;
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003627 if (VT.getSizeInBits() == 256)
3628 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003629
Craig Topperc612d792012-01-02 09:17:37 +00003630 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003631
Nate Begeman9008ca62009-04-27 18:41:29 +00003632 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003633 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003634
Craig Topperc612d792012-01-02 09:17:37 +00003635 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003636 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003637 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003638
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003639 return true;
3640}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003641
Nate Begeman9008ca62009-04-27 18:41:29 +00003642bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003643 return ::isMOVLMask(N->getMask(), N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003644}
3645
Craig Topper70b883b2011-11-28 10:14:51 +00003646/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003647/// as permutations between 128-bit chunks or halves. As an example: this
3648/// shuffle bellow:
3649/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3650/// The first half comes from the second half of V1 and the second half from the
3651/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003652static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003653 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003654 return false;
3655
3656 // The shuffle result is divided into half A and half B. In total the two
3657 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3658 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003659 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003660 bool MatchA = false, MatchB = false;
3661
3662 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003663 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003664 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3665 MatchA = true;
3666 break;
3667 }
3668 }
3669
3670 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003671 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003672 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3673 MatchB = true;
3674 break;
3675 }
3676 }
3677
3678 return MatchA && MatchB;
3679}
3680
Craig Topper70b883b2011-11-28 10:14:51 +00003681/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3682/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003683static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003684 EVT VT = SVOp->getValueType(0);
3685
Craig Topperc612d792012-01-02 09:17:37 +00003686 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003687
Craig Topperc612d792012-01-02 09:17:37 +00003688 unsigned FstHalf = 0, SndHalf = 0;
3689 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003690 if (SVOp->getMaskElt(i) > 0) {
3691 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3692 break;
3693 }
3694 }
Craig Topperc612d792012-01-02 09:17:37 +00003695 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003696 if (SVOp->getMaskElt(i) > 0) {
3697 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3698 break;
3699 }
3700 }
3701
3702 return (FstHalf | (SndHalf << 4));
3703}
3704
Craig Topper70b883b2011-11-28 10:14:51 +00003705/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003706/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3707/// Note that VPERMIL mask matching is different depending whether theunderlying
3708/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3709/// to the same elements of the low, but to the higher half of the source.
3710/// In VPERMILPD the two lanes could be shuffled independently of each other
3711/// with the same restriction that lanes can't be crossed.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003712static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003713 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003714 return false;
3715
Craig Topperc612d792012-01-02 09:17:37 +00003716 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003717 // Only match 256-bit with 32/64-bit types
3718 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003719 return false;
3720
Craig Topperc612d792012-01-02 09:17:37 +00003721 unsigned NumLanes = VT.getSizeInBits()/128;
3722 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003723 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003724 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003725 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003726 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003727 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003728 continue;
3729 // VPERMILPS handling
3730 if (Mask[i] < 0)
3731 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003732 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003733 return false;
3734 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003735 }
3736
3737 return true;
3738}
3739
Craig Topper70b883b2011-11-28 10:14:51 +00003740/// getShuffleVPERMILPImmediate - Return the appropriate immediate to shuffle
3741/// the specified VECTOR_MASK mask with VPERMILPS/D* instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003742static unsigned getShuffleVPERMILPImmediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003743 EVT VT = SVOp->getValueType(0);
3744
Craig Topperc612d792012-01-02 09:17:37 +00003745 unsigned NumElts = VT.getVectorNumElements();
3746 unsigned NumLanes = VT.getSizeInBits()/128;
3747 unsigned LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003748
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003749 // Although the mask is equal for both lanes do it twice to get the cases
3750 // where a mask will match because the same mask element is undef on the
3751 // first half but valid on the second. This would get pathological cases
3752 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Craig Topper70b883b2011-11-28 10:14:51 +00003753 unsigned Shift = (LaneSize == 4) ? 2 : 1;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003754 unsigned Mask = 0;
Craig Topperc612d792012-01-02 09:17:37 +00003755 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topper70b883b2011-11-28 10:14:51 +00003756 int MaskElt = SVOp->getMaskElt(i);
3757 if (MaskElt < 0)
3758 continue;
3759 MaskElt %= LaneSize;
3760 unsigned Shamt = i;
3761 // VPERMILPSY, the mask of the first half must be equal to the second one
3762 if (NumElts == 8) Shamt %= LaneSize;
3763 Mask |= MaskElt << (Shamt*Shift);
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003764 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003765
3766 return Mask;
3767}
3768
Evan Cheng017dcc62006-04-21 01:05:10 +00003769/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3770/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003771/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003772static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003773 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topperc612d792012-01-02 09:17:37 +00003774 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003775 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003776 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003777
Nate Begeman9008ca62009-04-27 18:41:29 +00003778 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003779 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003780
Craig Topperc612d792012-01-02 09:17:37 +00003781 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003782 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3783 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3784 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003785 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003786
Evan Cheng39623da2006-04-20 08:58:49 +00003787 return true;
3788}
3789
Nate Begeman9008ca62009-04-27 18:41:29 +00003790static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003791 bool V2IsUndef = false) {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003792 return isCommutedMOVLMask(N->getMask(), N->getValueType(0),
3793 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003794}
3795
Evan Chengd9539472006-04-14 21:59:03 +00003796/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3797/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003798/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3799bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3800 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003801 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003802 return false;
3803
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003804 // The second vector must be undef
3805 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3806 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003807
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003808 EVT VT = N->getValueType(0);
3809 unsigned NumElems = VT.getVectorNumElements();
3810
3811 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3812 (VT.getSizeInBits() == 256 && NumElems != 8))
3813 return false;
3814
3815 // "i+1" is the value the indexed mask element must have
3816 for (unsigned i = 0; i < NumElems; i += 2)
3817 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3818 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003819 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003820
3821 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003822}
3823
3824/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3825/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003826/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3827bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3828 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003829 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003830 return false;
3831
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003832 // The second vector must be undef
3833 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3834 return false;
3835
3836 EVT VT = N->getValueType(0);
3837 unsigned NumElems = VT.getVectorNumElements();
3838
3839 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3840 (VT.getSizeInBits() == 256 && NumElems != 8))
3841 return false;
3842
3843 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003844 for (unsigned i = 0; i != NumElems; i += 2)
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003845 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3846 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003847 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003848
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003849 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003850}
3851
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003852/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3853/// specifies a shuffle of elements that is suitable for input to 256-bit
3854/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003855static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topperc612d792012-01-02 09:17:37 +00003856 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003857
Craig Topperbeabc6c2011-12-05 06:56:46 +00003858 if (!HasAVX || VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003859 return false;
3860
Craig Topperc612d792012-01-02 09:17:37 +00003861 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003862 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003863 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003864 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003865 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003866 return false;
3867 return true;
3868}
3869
Evan Cheng0b457f02008-09-25 20:50:48 +00003870/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003871/// specifies a shuffle of elements that is suitable for input to 128-bit
3872/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003873bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003874 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003875
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003876 if (VT.getSizeInBits() != 128)
3877 return false;
3878
Craig Topperc612d792012-01-02 09:17:37 +00003879 unsigned e = VT.getVectorNumElements() / 2;
3880 for (unsigned i = 0; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003881 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003882 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003883 for (unsigned i = 0; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003884 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003885 return false;
3886 return true;
3887}
3888
David Greenec38a03e2011-02-03 15:50:00 +00003889/// isVEXTRACTF128Index - Return true if the specified
3890/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3891/// suitable for input to VEXTRACTF128.
3892bool X86::isVEXTRACTF128Index(SDNode *N) {
3893 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3894 return false;
3895
3896 // The index should be aligned on a 128-bit boundary.
3897 uint64_t Index =
3898 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3899
3900 unsigned VL = N->getValueType(0).getVectorNumElements();
3901 unsigned VBits = N->getValueType(0).getSizeInBits();
3902 unsigned ElSize = VBits / VL;
3903 bool Result = (Index * ElSize) % 128 == 0;
3904
3905 return Result;
3906}
3907
David Greeneccacdc12011-02-04 16:08:29 +00003908/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3909/// operand specifies a subvector insert that is suitable for input to
3910/// VINSERTF128.
3911bool X86::isVINSERTF128Index(SDNode *N) {
3912 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3913 return false;
3914
3915 // The index should be aligned on a 128-bit boundary.
3916 uint64_t Index =
3917 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3918
3919 unsigned VL = N->getValueType(0).getVectorNumElements();
3920 unsigned VBits = N->getValueType(0).getSizeInBits();
3921 unsigned ElSize = VBits / VL;
3922 bool Result = (Index * ElSize) % 128 == 0;
3923
3924 return Result;
3925}
3926
Evan Cheng63d33002006-03-22 08:01:21 +00003927/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003928/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00003929/// Handles 128-bit and 256-bit.
3930unsigned X86::getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
3931 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003932
Craig Topper1a7700a2012-01-19 08:19:12 +00003933 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3934 "Unsupported vector type for PSHUF/SHUFP");
3935
3936 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
3937 // independently on 128-bit lanes.
3938 unsigned NumElts = VT.getVectorNumElements();
3939 unsigned NumLanes = VT.getSizeInBits()/128;
3940 unsigned NumLaneElts = NumElts/NumLanes;
3941
3942 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
3943 "Only supports 2 or 4 elements per lane");
3944
3945 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00003946 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00003947 for (unsigned i = 0; i != NumElts; ++i) {
3948 int Elt = N->getMaskElt(i);
3949 if (Elt < 0) continue;
3950 Elt %= NumLaneElts;
3951 unsigned ShAmt = i << Shift;
3952 if (ShAmt >= 8) ShAmt -= 8;
3953 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00003954 }
Craig Topper1a7700a2012-01-19 08:19:12 +00003955
Evan Cheng63d33002006-03-22 08:01:21 +00003956 return Mask;
3957}
3958
Evan Cheng506d3df2006-03-29 23:07:14 +00003959/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003960/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003961unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003962 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003963 unsigned Mask = 0;
3964 // 8 nodes, but we only care about the last 4.
3965 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003966 int Val = SVOp->getMaskElt(i);
3967 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003968 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003969 if (i != 4)
3970 Mask <<= 2;
3971 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003972 return Mask;
3973}
3974
3975/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003976/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003977unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003978 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003979 unsigned Mask = 0;
3980 // 8 nodes, but we only care about the first 4.
3981 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003982 int Val = SVOp->getMaskElt(i);
3983 if (Val >= 0)
3984 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003985 if (i != 0)
3986 Mask <<= 2;
3987 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003988 return Mask;
3989}
3990
Nate Begemana09008b2009-10-19 02:17:23 +00003991/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3992/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00003993static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
3994 EVT VT = SVOp->getValueType(0);
3995 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00003996
Craig Topper0e2037b2012-01-20 05:53:00 +00003997 unsigned NumElts = VT.getVectorNumElements();
3998 unsigned NumLanes = VT.getSizeInBits()/128;
3999 unsigned NumLaneElts = NumElts/NumLanes;
4000
4001 int Val = 0;
4002 unsigned i;
4003 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004004 Val = SVOp->getMaskElt(i);
4005 if (Val >= 0)
4006 break;
4007 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004008 if (Val >= (int)NumElts)
4009 Val -= NumElts - NumLaneElts;
4010
Eli Friedman63f8dde2011-07-25 21:36:45 +00004011 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004012 return (Val - i) * EltSize;
4013}
4014
David Greenec38a03e2011-02-03 15:50:00 +00004015/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4016/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4017/// instructions.
4018unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4019 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4020 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4021
4022 uint64_t Index =
4023 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4024
4025 EVT VecVT = N->getOperand(0).getValueType();
4026 EVT ElVT = VecVT.getVectorElementType();
4027
4028 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004029 return Index / NumElemsPerChunk;
4030}
4031
David Greeneccacdc12011-02-04 16:08:29 +00004032/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4033/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4034/// instructions.
4035unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4036 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4037 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4038
4039 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004040 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004041
4042 EVT VecVT = N->getValueType(0);
4043 EVT ElVT = VecVT.getVectorElementType();
4044
4045 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004046 return Index / NumElemsPerChunk;
4047}
4048
Evan Cheng37b73872009-07-30 08:33:02 +00004049/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4050/// constant +0.0.
4051bool X86::isZeroNode(SDValue Elt) {
4052 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004053 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004054 (isa<ConstantFPSDNode>(Elt) &&
4055 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4056}
4057
Nate Begeman9008ca62009-04-27 18:41:29 +00004058/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4059/// their permute mask.
4060static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4061 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004062 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004063 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004064 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004065
Nate Begeman5a5ca152009-04-29 05:20:52 +00004066 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004067 int idx = SVOp->getMaskElt(i);
4068 if (idx < 0)
4069 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004070 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004071 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004072 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004073 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004074 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004075 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4076 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004077}
4078
Evan Cheng533a0aa2006-04-19 20:35:22 +00004079/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4080/// match movhlps. The lower half elements should come from upper half of
4081/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004082/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004083static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004084 EVT VT = Op->getValueType(0);
4085 if (VT.getSizeInBits() != 128)
4086 return false;
4087 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004088 return false;
4089 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004090 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004091 return false;
4092 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004093 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004094 return false;
4095 return true;
4096}
4097
Evan Cheng5ced1d82006-04-06 23:23:56 +00004098/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004099/// is promoted to a vector. It also returns the LoadSDNode by reference if
4100/// required.
4101static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004102 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4103 return false;
4104 N = N->getOperand(0).getNode();
4105 if (!ISD::isNON_EXTLoad(N))
4106 return false;
4107 if (LD)
4108 *LD = cast<LoadSDNode>(N);
4109 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004110}
4111
Dan Gohman65fd6562011-11-03 21:49:52 +00004112// Test whether the given value is a vector value which will be legalized
4113// into a load.
4114static bool WillBeConstantPoolLoad(SDNode *N) {
4115 if (N->getOpcode() != ISD::BUILD_VECTOR)
4116 return false;
4117
4118 // Check for any non-constant elements.
4119 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4120 switch (N->getOperand(i).getNode()->getOpcode()) {
4121 case ISD::UNDEF:
4122 case ISD::ConstantFP:
4123 case ISD::Constant:
4124 break;
4125 default:
4126 return false;
4127 }
4128
4129 // Vectors of all-zeros and all-ones are materialized with special
4130 // instructions rather than being loaded.
4131 return !ISD::isBuildVectorAllZeros(N) &&
4132 !ISD::isBuildVectorAllOnes(N);
4133}
4134
Evan Cheng533a0aa2006-04-19 20:35:22 +00004135/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4136/// match movlp{s|d}. The lower half elements should come from lower half of
4137/// V1 (and in order), and the upper half elements should come from the upper
4138/// half of V2 (and in order). And since V1 will become the source of the
4139/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004140static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4141 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004142 EVT VT = Op->getValueType(0);
4143 if (VT.getSizeInBits() != 128)
4144 return false;
4145
Evan Cheng466685d2006-10-09 20:57:25 +00004146 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004147 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004148 // Is V2 is a vector load, don't do this transformation. We will try to use
4149 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004150 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004151 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004152
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004153 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004154
Evan Cheng533a0aa2006-04-19 20:35:22 +00004155 if (NumElems != 2 && NumElems != 4)
4156 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004157 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004158 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004159 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004160 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004161 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004162 return false;
4163 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004164}
4165
Evan Cheng39623da2006-04-20 08:58:49 +00004166/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4167/// all the same.
4168static bool isSplatVector(SDNode *N) {
4169 if (N->getOpcode() != ISD::BUILD_VECTOR)
4170 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004171
Dan Gohman475871a2008-07-27 21:46:04 +00004172 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004173 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4174 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004175 return false;
4176 return true;
4177}
4178
Evan Cheng213d2cf2007-05-17 18:45:50 +00004179/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004180/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004181/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004182static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004183 SDValue V1 = N->getOperand(0);
4184 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004185 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4186 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004187 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004188 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004189 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004190 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4191 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004192 if (Opc != ISD::BUILD_VECTOR ||
4193 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004194 return false;
4195 } else if (Idx >= 0) {
4196 unsigned Opc = V1.getOpcode();
4197 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4198 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004199 if (Opc != ISD::BUILD_VECTOR ||
4200 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004201 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004202 }
4203 }
4204 return true;
4205}
4206
4207/// getZeroVector - Returns a vector of specified type with all zero elements.
4208///
Craig Topper12216172012-01-13 08:12:35 +00004209static SDValue getZeroVector(EVT VT, bool HasSSE2, bool HasAVX2,
4210 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004211 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004212
Dale Johannesen0488fb62010-09-30 23:57:10 +00004213 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004214 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004215 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004216 if (VT.getSizeInBits() == 128) { // SSE
Craig Topper1accb7e2012-01-10 06:54:16 +00004217 if (HasSSE2) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004218 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4219 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4220 } else { // SSE1
4221 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4222 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4223 }
4224 } else if (VT.getSizeInBits() == 256) { // AVX
Craig Topper12216172012-01-13 08:12:35 +00004225 if (HasAVX2) { // AVX2
4226 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4227 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4228 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4229 } else {
4230 // 256-bit logic and arithmetic instructions in AVX are all
4231 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4232 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4233 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4234 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4235 }
Evan Chengf0df0312008-05-15 08:39:06 +00004236 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004237 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004238}
4239
Chris Lattner8a594482007-11-25 00:24:49 +00004240/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004241/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4242/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4243/// Then bitcast to their original type, ensuring they get CSE'd.
4244static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4245 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004246 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004247 assert((VT.is128BitVector() || VT.is256BitVector())
4248 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004249
Owen Anderson825b72b2009-08-11 20:47:22 +00004250 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004251 SDValue Vec;
4252 if (VT.getSizeInBits() == 256) {
4253 if (HasAVX2) { // AVX2
4254 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4255 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4256 } else { // AVX
4257 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4258 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4259 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4260 Vec = Insert128BitVector(InsV, Vec,
4261 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4262 }
4263 } else {
4264 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004265 }
4266
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004267 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004268}
4269
Evan Cheng39623da2006-04-20 08:58:49 +00004270/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4271/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004272static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004273 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004274 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004275
Evan Cheng39623da2006-04-20 08:58:49 +00004276 bool Changed = false;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004277 SmallVector<int, 8> MaskVec(SVOp->getMask().begin(), SVOp->getMask().end());
Eric Christopherfd179292009-08-27 18:07:15 +00004278
Nate Begeman5a5ca152009-04-29 05:20:52 +00004279 for (unsigned i = 0; i != NumElems; ++i) {
4280 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004281 MaskVec[i] = NumElems;
4282 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004283 }
Evan Cheng39623da2006-04-20 08:58:49 +00004284 }
Evan Cheng39623da2006-04-20 08:58:49 +00004285 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004286 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4287 SVOp->getOperand(1), &MaskVec[0]);
4288 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004289}
4290
Evan Cheng017dcc62006-04-21 01:05:10 +00004291/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4292/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004293static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004294 SDValue V2) {
4295 unsigned NumElems = VT.getVectorNumElements();
4296 SmallVector<int, 8> Mask;
4297 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004298 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004299 Mask.push_back(i);
4300 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004301}
4302
Nate Begeman9008ca62009-04-27 18:41:29 +00004303/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004304static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004305 SDValue V2) {
4306 unsigned NumElems = VT.getVectorNumElements();
4307 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004308 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004309 Mask.push_back(i);
4310 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004311 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004312 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004313}
4314
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004315/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004316static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004317 SDValue V2) {
4318 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004319 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004320 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004321 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004322 Mask.push_back(i + Half);
4323 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004324 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004325 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004326}
4327
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004328// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004329// a generic shuffle instruction because the target has no such instructions.
4330// Generate shuffles which repeat i16 and i8 several times until they can be
4331// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004332static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004333 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004334 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004335 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004336
Nate Begeman9008ca62009-04-27 18:41:29 +00004337 while (NumElems > 4) {
4338 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004339 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004340 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004341 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004342 EltNo -= NumElems/2;
4343 }
4344 NumElems >>= 1;
4345 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004346 return V;
4347}
Eric Christopherfd179292009-08-27 18:07:15 +00004348
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004349/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4350static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4351 EVT VT = V.getValueType();
4352 DebugLoc dl = V.getDebugLoc();
4353 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4354 && "Vector size not supported");
4355
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004356 if (VT.getSizeInBits() == 128) {
4357 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004358 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004359 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4360 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004361 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004362 // To use VPERMILPS to splat scalars, the second half of indicies must
4363 // refer to the higher part, which is a duplication of the lower one,
4364 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004365 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4366 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004367
4368 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4369 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4370 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004371 }
4372
4373 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4374}
4375
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004376/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004377static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4378 EVT SrcVT = SV->getValueType(0);
4379 SDValue V1 = SV->getOperand(0);
4380 DebugLoc dl = SV->getDebugLoc();
4381
4382 int EltNo = SV->getSplatIndex();
4383 int NumElems = SrcVT.getVectorNumElements();
4384 unsigned Size = SrcVT.getSizeInBits();
4385
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004386 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4387 "Unknown how to promote splat for type");
4388
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004389 // Extract the 128-bit part containing the splat element and update
4390 // the splat element index when it refers to the higher register.
4391 if (Size == 256) {
Nadav Rotemd2070b02012-01-12 15:31:55 +00004392 unsigned Idx = (EltNo >= NumElems/2) ? NumElems/2 : 0;
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004393 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4394 if (Idx > 0)
4395 EltNo -= NumElems/2;
4396 }
4397
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004398 // All i16 and i8 vector types can't be used directly by a generic shuffle
4399 // instruction because the target has no such instruction. Generate shuffles
4400 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004401 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004402 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004403 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004404 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004405
4406 // Recreate the 256-bit vector and place the same 128-bit vector
4407 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004408 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004409 if (Size == 256) {
4410 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4411 DAG.getConstant(0, MVT::i32), DAG, dl);
4412 V1 = Insert128BitVector(InsV, V1,
4413 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4414 }
4415
4416 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004417}
4418
Evan Chengba05f722006-04-21 23:03:30 +00004419/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004420/// vector of zero or undef vector. This produces a shuffle where the low
4421/// element of V2 is swizzled into the zero/undef vector, landing at element
4422/// 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 +00004423static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004424 bool IsZero,
4425 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004426 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004427 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004428 SDValue V1 = IsZero
4429 ? getZeroVector(VT, Subtarget->hasSSE2(), Subtarget->hasAVX2(), DAG,
4430 V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004431 unsigned NumElems = VT.getVectorNumElements();
4432 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004433 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004434 // If this is the insertion idx, put the low elt of V2 here.
4435 MaskVec.push_back(i == Idx ? NumElems : i);
4436 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004437}
4438
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004439/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4440/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004441static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4442 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004443 if (Depth == 6)
4444 return SDValue(); // Limit search depth.
4445
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004446 SDValue V = SDValue(N, 0);
4447 EVT VT = V.getValueType();
4448 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004449
4450 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4451 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4452 Index = SV->getMaskElt(Index);
4453
4454 if (Index < 0)
4455 return DAG.getUNDEF(VT.getVectorElementType());
4456
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004457 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004458 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004459 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004460 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004461
4462 // Recurse into target specific vector shuffles to find scalars.
4463 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004464 int NumElems = VT.getVectorNumElements();
4465 SmallVector<unsigned, 16> ShuffleMask;
4466 SDValue ImmN;
4467
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004468 switch(Opcode) {
Craig Topperb3982da2011-12-31 23:50:21 +00004469 case X86ISD::SHUFP:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004470 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper36e36ac2011-11-29 07:49:05 +00004471 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4472 ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004473 break;
Craig Topper34671b82011-12-06 08:21:25 +00004474 case X86ISD::UNPCKH:
Craig Topper3d8c2ce2011-12-06 05:31:16 +00004475 DecodeUNPCKHMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004476 break;
Craig Topper34671b82011-12-06 08:21:25 +00004477 case X86ISD::UNPCKL:
Craig Topper3d8c2ce2011-12-06 05:31:16 +00004478 DecodeUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004479 break;
4480 case X86ISD::MOVHLPS:
4481 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4482 break;
4483 case X86ISD::MOVLHPS:
4484 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4485 break;
4486 case X86ISD::PSHUFD:
4487 ImmN = N->getOperand(N->getNumOperands()-1);
4488 DecodePSHUFMask(NumElems,
4489 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4490 ShuffleMask);
4491 break;
4492 case X86ISD::PSHUFHW:
4493 ImmN = N->getOperand(N->getNumOperands()-1);
4494 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4495 ShuffleMask);
4496 break;
4497 case X86ISD::PSHUFLW:
4498 ImmN = N->getOperand(N->getNumOperands()-1);
4499 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4500 ShuffleMask);
4501 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004502 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004503 case X86ISD::MOVSD: {
4504 // The index 0 always comes from the first element of the second source,
4505 // this is why MOVSS and MOVSD are used in the first place. The other
4506 // elements come from the other positions of the first source vector.
4507 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004508 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4509 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004510 }
Craig Topper316cd2a2011-11-30 06:25:25 +00004511 case X86ISD::VPERMILP:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004512 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper316cd2a2011-11-30 06:25:25 +00004513 DecodeVPERMILPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004514 ShuffleMask);
4515 break;
Craig Topperec24e612011-11-30 07:47:51 +00004516 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004517 ImmN = N->getOperand(N->getNumOperands()-1);
4518 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4519 ShuffleMask);
4520 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004521 case X86ISD::MOVDDUP:
4522 case X86ISD::MOVLHPD:
4523 case X86ISD::MOVLPD:
4524 case X86ISD::MOVLPS:
4525 case X86ISD::MOVSHDUP:
4526 case X86ISD::MOVSLDUP:
4527 case X86ISD::PALIGN:
4528 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004529 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004530 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004531 return SDValue();
4532 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004533
4534 Index = ShuffleMask[Index];
4535 if (Index < 0)
4536 return DAG.getUNDEF(VT.getVectorElementType());
4537
4538 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4539 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4540 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004541 }
4542
4543 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004544 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004545 V = V.getOperand(0);
4546 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004547 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004548
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004549 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004550 return SDValue();
4551 }
4552
4553 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4554 return (Index == 0) ? V.getOperand(0)
4555 : DAG.getUNDEF(VT.getVectorElementType());
4556
4557 if (V.getOpcode() == ISD::BUILD_VECTOR)
4558 return V.getOperand(Index);
4559
4560 return SDValue();
4561}
4562
4563/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4564/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004565/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004566static
4567unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4568 bool ZerosFromLeft, SelectionDAG &DAG) {
4569 int i = 0;
4570
4571 while (i < NumElems) {
4572 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004573 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004574 if (!(Elt.getNode() &&
4575 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4576 break;
4577 ++i;
4578 }
4579
4580 return i;
4581}
4582
4583/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4584/// MaskE correspond consecutively to elements from one of the vector operands,
4585/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4586static
4587bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4588 int OpIdx, int NumElems, unsigned &OpNum) {
4589 bool SeenV1 = false;
4590 bool SeenV2 = false;
4591
4592 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4593 int Idx = SVOp->getMaskElt(i);
4594 // Ignore undef indicies
4595 if (Idx < 0)
4596 continue;
4597
4598 if (Idx < NumElems)
4599 SeenV1 = true;
4600 else
4601 SeenV2 = true;
4602
4603 // Only accept consecutive elements from the same vector
4604 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4605 return false;
4606 }
4607
4608 OpNum = SeenV1 ? 0 : 1;
4609 return true;
4610}
4611
4612/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4613/// logical left shift of a vector.
4614static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4615 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4616 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4617 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4618 false /* check zeros from right */, DAG);
4619 unsigned OpSrc;
4620
4621 if (!NumZeros)
4622 return false;
4623
4624 // Considering the elements in the mask that are not consecutive zeros,
4625 // check if they consecutively come from only one of the source vectors.
4626 //
4627 // V1 = {X, A, B, C} 0
4628 // \ \ \ /
4629 // vector_shuffle V1, V2 <1, 2, 3, X>
4630 //
4631 if (!isShuffleMaskConsecutive(SVOp,
4632 0, // Mask Start Index
4633 NumElems-NumZeros-1, // Mask End Index
4634 NumZeros, // Where to start looking in the src vector
4635 NumElems, // Number of elements in vector
4636 OpSrc)) // Which source operand ?
4637 return false;
4638
4639 isLeft = false;
4640 ShAmt = NumZeros;
4641 ShVal = SVOp->getOperand(OpSrc);
4642 return true;
4643}
4644
4645/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4646/// logical left shift of a vector.
4647static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4648 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4649 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4650 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4651 true /* check zeros from left */, DAG);
4652 unsigned OpSrc;
4653
4654 if (!NumZeros)
4655 return false;
4656
4657 // Considering the elements in the mask that are not consecutive zeros,
4658 // check if they consecutively come from only one of the source vectors.
4659 //
4660 // 0 { A, B, X, X } = V2
4661 // / \ / /
4662 // vector_shuffle V1, V2 <X, X, 4, 5>
4663 //
4664 if (!isShuffleMaskConsecutive(SVOp,
4665 NumZeros, // Mask Start Index
4666 NumElems-1, // Mask End Index
4667 0, // Where to start looking in the src vector
4668 NumElems, // Number of elements in vector
4669 OpSrc)) // Which source operand ?
4670 return false;
4671
4672 isLeft = true;
4673 ShAmt = NumZeros;
4674 ShVal = SVOp->getOperand(OpSrc);
4675 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004676}
4677
4678/// isVectorShift - Returns true if the shuffle can be implemented as a
4679/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004680static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004681 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004682 // Although the logic below support any bitwidth size, there are no
4683 // shift instructions which handle more than 128-bit vectors.
4684 if (SVOp->getValueType(0).getSizeInBits() > 128)
4685 return false;
4686
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004687 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4688 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4689 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004690
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004691 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004692}
4693
Evan Chengc78d3b42006-04-24 18:01:45 +00004694/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4695///
Dan Gohman475871a2008-07-27 21:46:04 +00004696static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004697 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004698 SelectionDAG &DAG,
4699 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004700 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004701 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004702
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004703 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004704 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004705 bool First = true;
4706 for (unsigned i = 0; i < 16; ++i) {
4707 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4708 if (ThisIsNonZero && First) {
4709 if (NumZero)
Craig Topper12216172012-01-13 08:12:35 +00004710 V = getZeroVector(MVT::v8i16, /*HasSSE2*/ true, /*HasAVX2*/ false,
4711 DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004712 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004713 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004714 First = false;
4715 }
4716
4717 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004718 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004719 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4720 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004721 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004722 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004723 }
4724 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004725 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4726 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4727 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004728 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004729 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004730 } else
4731 ThisElt = LastElt;
4732
Gabor Greifba36cb52008-08-28 21:40:38 +00004733 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004734 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004735 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004736 }
4737 }
4738
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004739 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004740}
4741
Bill Wendlinga348c562007-03-22 18:42:45 +00004742/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004743///
Dan Gohman475871a2008-07-27 21:46:04 +00004744static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004745 unsigned NumNonZero, unsigned NumZero,
4746 SelectionDAG &DAG,
4747 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004748 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004749 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004750
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004751 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004752 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004753 bool First = true;
4754 for (unsigned i = 0; i < 8; ++i) {
4755 bool isNonZero = (NonZeros & (1 << i)) != 0;
4756 if (isNonZero) {
4757 if (First) {
4758 if (NumZero)
Craig Topper12216172012-01-13 08:12:35 +00004759 V = getZeroVector(MVT::v8i16, /*HasSSE2*/ true, /*HasAVX2*/ false,
4760 DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004761 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004762 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004763 First = false;
4764 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004765 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004766 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004767 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004768 }
4769 }
4770
4771 return V;
4772}
4773
Evan Chengf26ffe92008-05-29 08:22:04 +00004774/// getVShift - Return a vector logical shift node.
4775///
Owen Andersone50ed302009-08-10 22:56:29 +00004776static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004777 unsigned NumBits, SelectionDAG &DAG,
4778 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004779 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004780 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004781 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004782 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4783 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004784 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004785 DAG.getConstant(NumBits,
4786 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004787}
4788
Dan Gohman475871a2008-07-27 21:46:04 +00004789SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004790X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004791 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004792
Evan Chengc3630942009-12-09 21:00:30 +00004793 // Check if the scalar load can be widened into a vector load. And if
4794 // the address is "base + cst" see if the cst can be "absorbed" into
4795 // the shuffle mask.
4796 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4797 SDValue Ptr = LD->getBasePtr();
4798 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4799 return SDValue();
4800 EVT PVT = LD->getValueType(0);
4801 if (PVT != MVT::i32 && PVT != MVT::f32)
4802 return SDValue();
4803
4804 int FI = -1;
4805 int64_t Offset = 0;
4806 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4807 FI = FINode->getIndex();
4808 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004809 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004810 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4811 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4812 Offset = Ptr.getConstantOperandVal(1);
4813 Ptr = Ptr.getOperand(0);
4814 } else {
4815 return SDValue();
4816 }
4817
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004818 // FIXME: 256-bit vector instructions don't require a strict alignment,
4819 // improve this code to support it better.
4820 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004821 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004822 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004823 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004824 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004825 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004826 // Can't change the alignment. FIXME: It's possible to compute
4827 // the exact stack offset and reference FI + adjust offset instead.
4828 // If someone *really* cares about this. That's the way to implement it.
4829 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004830 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004831 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004832 }
4833 }
4834
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004835 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004836 // Ptr + (Offset & ~15).
4837 if (Offset < 0)
4838 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004839 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004840 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004841 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004842 if (StartOffset)
4843 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4844 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4845
4846 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004847 int NumElems = VT.getVectorNumElements();
4848
4849 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4850 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4851 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004852 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004853 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004854
4855 // Canonicalize it to a v4i32 or v8i32 shuffle.
4856 SmallVector<int, 8> Mask;
4857 for (int i = 0; i < NumElems; ++i)
4858 Mask.push_back(EltNo);
4859
4860 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4861 return DAG.getNode(ISD::BITCAST, dl, NVT,
4862 DAG.getVectorShuffle(CanonVT, dl, V1,
4863 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004864 }
4865
4866 return SDValue();
4867}
4868
Michael J. Spencerec38de22010-10-10 22:04:20 +00004869/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4870/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004871/// load which has the same value as a build_vector whose operands are 'elts'.
4872///
4873/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004874///
Nate Begeman1449f292010-03-24 22:19:06 +00004875/// FIXME: we'd also like to handle the case where the last elements are zero
4876/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4877/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004878static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004879 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004880 EVT EltVT = VT.getVectorElementType();
4881 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004882
Nate Begemanfdea31a2010-03-24 20:49:50 +00004883 LoadSDNode *LDBase = NULL;
4884 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004885
Nate Begeman1449f292010-03-24 22:19:06 +00004886 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004887 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004888 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004889 for (unsigned i = 0; i < NumElems; ++i) {
4890 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004891
Nate Begemanfdea31a2010-03-24 20:49:50 +00004892 if (!Elt.getNode() ||
4893 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4894 return SDValue();
4895 if (!LDBase) {
4896 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4897 return SDValue();
4898 LDBase = cast<LoadSDNode>(Elt.getNode());
4899 LastLoadedElt = i;
4900 continue;
4901 }
4902 if (Elt.getOpcode() == ISD::UNDEF)
4903 continue;
4904
4905 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4906 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4907 return SDValue();
4908 LastLoadedElt = i;
4909 }
Nate Begeman1449f292010-03-24 22:19:06 +00004910
4911 // If we have found an entire vector of loads and undefs, then return a large
4912 // load of the entire vector width starting at the base pointer. If we found
4913 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004914 if (LastLoadedElt == NumElems - 1) {
4915 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004916 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004917 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004918 LDBase->isVolatile(), LDBase->isNonTemporal(),
4919 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004920 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004921 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004922 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004923 LDBase->isInvariant(), LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00004924 } else if (NumElems == 4 && LastLoadedElt == 1 &&
4925 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004926 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4927 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00004928 SDValue ResNode =
4929 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
4930 LDBase->getPointerInfo(),
4931 LDBase->getAlignment(),
4932 false/*isVolatile*/, true/*ReadMem*/,
4933 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004934 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004935 }
4936 return SDValue();
4937}
4938
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004939/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
4940/// a vbroadcast node. We support two patterns:
4941/// 1. A splat BUILD_VECTOR which uses a single scalar load.
4942/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
4943/// a scalar load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004944/// The scalar load node is returned when a pattern is found,
4945/// or SDValue() otherwise.
Craig Toppera9376332012-01-10 08:23:59 +00004946static SDValue isVectorBroadcast(SDValue &Op, const X86Subtarget *Subtarget) {
4947 if (!Subtarget->hasAVX())
4948 return SDValue();
4949
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004950 EVT VT = Op.getValueType();
4951 SDValue V = Op;
4952
4953 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
4954 V = V.getOperand(0);
4955
4956 //A suspected load to be broadcasted.
4957 SDValue Ld;
4958
4959 switch (V.getOpcode()) {
4960 default:
4961 // Unknown pattern found.
4962 return SDValue();
4963
4964 case ISD::BUILD_VECTOR: {
4965 // The BUILD_VECTOR node must be a splat.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004966 if (!isSplatVector(V.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004967 return SDValue();
4968
4969 Ld = V.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004970
4971 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004972 // of its users are from the BUILD_VECTOR node.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004973 if (!Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004974 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004975 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004976 }
4977
4978 case ISD::VECTOR_SHUFFLE: {
4979 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
4980
4981 // Shuffles must have a splat mask where the first element is
4982 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004983 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004984 return SDValue();
4985
4986 SDValue Sc = Op.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004987 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004988 return SDValue();
4989
4990 Ld = Sc.getOperand(0);
4991
4992 // The scalar_to_vector node and the suspected
4993 // load node must have exactly one user.
4994 if (!Sc.hasOneUse() || !Ld.hasOneUse())
4995 return SDValue();
4996 break;
4997 }
4998 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004999
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005000 // The scalar source must be a normal load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005001 if (!ISD::isNormalLoad(Ld.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005002 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005003
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005004 bool Is256 = VT.getSizeInBits() == 256;
5005 bool Is128 = VT.getSizeInBits() == 128;
5006 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5007
5008 // VBroadcast to YMM
5009 if (Is256 && (ScalarSize == 32 || ScalarSize == 64))
5010 return Ld;
5011
5012 // VBroadcast to XMM
5013 if (Is128 && (ScalarSize == 32))
5014 return Ld;
5015
Craig Toppera9376332012-01-10 08:23:59 +00005016 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
5017 // double since there is vbroadcastsd xmm
5018 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
5019 // VBroadcast to YMM
5020 if (Is256 && (ScalarSize == 8 || ScalarSize == 16))
5021 return Ld;
5022
5023 // VBroadcast to XMM
5024 if (Is128 && (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64))
5025 return Ld;
5026 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005027
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005028 // Unsupported broadcast.
5029 return SDValue();
5030}
5031
Evan Chengc3630942009-12-09 21:00:30 +00005032SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005033X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005034 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005035
David Greenef125a292011-02-08 19:04:41 +00005036 EVT VT = Op.getValueType();
5037 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005038 unsigned NumElems = Op.getNumOperands();
5039
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005040 // Vectors containing all zeros can be matched by pxor and xorps later
5041 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5042 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5043 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005044 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005045 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005046
Craig Topper07a27622012-01-22 03:07:48 +00005047 return getZeroVector(VT, Subtarget->hasSSE2(),
Craig Topper12216172012-01-13 08:12:35 +00005048 Subtarget->hasAVX2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005049 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005050
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005051 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005052 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5053 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005054 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005055 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005056 return Op;
5057
Craig Topper07a27622012-01-22 03:07:48 +00005058 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005059 }
5060
Craig Toppera9376332012-01-10 08:23:59 +00005061 SDValue LD = isVectorBroadcast(Op, Subtarget);
5062 if (LD.getNode())
5063 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005064
Owen Andersone50ed302009-08-10 22:56:29 +00005065 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005066
Evan Cheng0db9fe62006-04-25 20:13:52 +00005067 unsigned NumZero = 0;
5068 unsigned NumNonZero = 0;
5069 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005070 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005071 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005072 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005073 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005074 if (Elt.getOpcode() == ISD::UNDEF)
5075 continue;
5076 Values.insert(Elt);
5077 if (Elt.getOpcode() != ISD::Constant &&
5078 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005079 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005080 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005081 NumZero++;
5082 else {
5083 NonZeros |= (1 << i);
5084 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005085 }
5086 }
5087
Chris Lattner97a2a562010-08-26 05:24:29 +00005088 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5089 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005090 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005091
Chris Lattner67f453a2008-03-09 05:42:06 +00005092 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005093 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005094 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005095 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005096
Chris Lattner62098042008-03-09 01:05:04 +00005097 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5098 // the value are obviously zero, truncate the value to i32 and do the
5099 // insertion that way. Only do this if the value is non-constant or if the
5100 // value is a constant being inserted into element 0. It is cheaper to do
5101 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005102 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005103 (!IsAllConstants || Idx == 0)) {
5104 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005105 // Handle SSE only.
5106 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5107 EVT VecVT = MVT::v4i32;
5108 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005109
Chris Lattner62098042008-03-09 01:05:04 +00005110 // Truncate the value (which may itself be a constant) to i32, and
5111 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005112 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005113 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005114 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005115
Chris Lattner62098042008-03-09 01:05:04 +00005116 // Now we have our 32-bit value zero extended in the low element of
5117 // a vector. If Idx != 0, swizzle it into place.
5118 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005119 SmallVector<int, 4> Mask;
5120 Mask.push_back(Idx);
5121 for (unsigned i = 1; i != VecElts; ++i)
5122 Mask.push_back(i);
5123 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005124 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005125 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005126 }
Craig Topper07a27622012-01-22 03:07:48 +00005127 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005128 }
5129 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005130
Chris Lattner19f79692008-03-08 22:59:52 +00005131 // If we have a constant or non-constant insertion into the low element of
5132 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5133 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005134 // depending on what the source datatype is.
5135 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005136 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005137 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005138
5139 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005140 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005141 if (VT.getSizeInBits() == 256) {
Craig Topper12216172012-01-13 08:12:35 +00005142 SDValue ZeroVec = getZeroVector(VT, Subtarget->hasSSE2(),
5143 Subtarget->hasAVX2(), DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005144 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5145 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005146 }
Craig Topperd62c16e2011-12-29 03:20:51 +00005147 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005148 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5149 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005150 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005151 }
5152
5153 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005154 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005155 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper19ec2a92011-12-29 03:34:54 +00005156 if (VT.getSizeInBits() == 256) {
Craig Topper12216172012-01-13 08:12:35 +00005157 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget->hasSSE2(),
5158 Subtarget->hasAVX2(), DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005159 Item = Insert128BitVector(ZeroVec, Item, DAG.getConstant(0, MVT::i32),
5160 DAG, dl);
5161 } else {
5162 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005163 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005164 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005165 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005166 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005167 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005168
5169 // Is it a vector logical left shift?
5170 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005171 X86::isZeroNode(Op.getOperand(0)) &&
5172 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005173 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005174 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005175 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005176 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005177 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005178 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005179
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005180 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005181 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005182
Chris Lattner19f79692008-03-08 22:59:52 +00005183 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5184 // is a non-constant being inserted into an element other than the low one,
5185 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5186 // movd/movss) to move this into the low element, then shuffle it into
5187 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005188 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005189 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005190
Evan Cheng0db9fe62006-04-25 20:13:52 +00005191 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005192 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005193 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005194 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005195 MaskVec.push_back(i == Idx ? 0 : 1);
5196 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005197 }
5198 }
5199
Chris Lattner67f453a2008-03-09 05:42:06 +00005200 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005201 if (Values.size() == 1) {
5202 if (EVTBits == 32) {
5203 // Instead of a shuffle like this:
5204 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5205 // Check if it's possible to issue this instead.
5206 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5207 unsigned Idx = CountTrailingZeros_32(NonZeros);
5208 SDValue Item = Op.getOperand(Idx);
5209 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5210 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5211 }
Dan Gohman475871a2008-07-27 21:46:04 +00005212 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005213 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005214
Dan Gohmana3941172007-07-24 22:55:08 +00005215 // A vector full of immediates; various special cases are already
5216 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005217 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005218 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005219
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005220 // For AVX-length vectors, build the individual 128-bit pieces and use
5221 // shuffles to put them in place.
5222 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5223 SmallVector<SDValue, 32> V;
5224 for (unsigned i = 0; i < NumElems; ++i)
5225 V.push_back(Op.getOperand(i));
5226
5227 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5228
5229 // Build both the lower and upper subvector.
5230 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5231 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5232 NumElems/2);
5233
5234 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005235 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5236 DAG.getConstant(0, MVT::i32), DAG, dl);
5237 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005238 DAG, dl);
5239 }
5240
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005241 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005242 if (EVTBits == 64) {
5243 if (NumNonZero == 1) {
5244 // One half is zero or undef.
5245 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005246 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005247 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005248 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005249 }
Dan Gohman475871a2008-07-27 21:46:04 +00005250 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005251 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005252
5253 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005254 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005255 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005256 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005257 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005258 }
5259
Bill Wendling826f36f2007-03-28 00:57:11 +00005260 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005261 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005262 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005263 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005264 }
5265
5266 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005267 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005268 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005269 if (NumElems == 4 && NumZero > 0) {
5270 for (unsigned i = 0; i < 4; ++i) {
5271 bool isZero = !(NonZeros & (1 << i));
5272 if (isZero)
Craig Topper12216172012-01-13 08:12:35 +00005273 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), Subtarget->hasAVX2(),
5274 DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005275 else
Dale Johannesenace16102009-02-03 19:33:06 +00005276 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005277 }
5278
5279 for (unsigned i = 0; i < 2; ++i) {
5280 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5281 default: break;
5282 case 0:
5283 V[i] = V[i*2]; // Must be a zero vector.
5284 break;
5285 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005286 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005287 break;
5288 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005289 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005290 break;
5291 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005292 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005293 break;
5294 }
5295 }
5296
Nate Begeman9008ca62009-04-27 18:41:29 +00005297 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005298 bool Reverse = (NonZeros & 0x3) == 2;
5299 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005300 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005301 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5302 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005303 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5304 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005305 }
5306
Nate Begemanfdea31a2010-03-24 20:49:50 +00005307 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5308 // Check for a build vector of consecutive loads.
5309 for (unsigned i = 0; i < NumElems; ++i)
5310 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005311
Nate Begemanfdea31a2010-03-24 20:49:50 +00005312 // Check for elements which are consecutive loads.
5313 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5314 if (LD.getNode())
5315 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005316
5317 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005318 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005319 SDValue Result;
5320 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5321 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5322 else
5323 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005324
Chris Lattner24faf612010-08-28 17:59:08 +00005325 for (unsigned i = 1; i < NumElems; ++i) {
5326 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5327 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005328 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005329 }
5330 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005331 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005332
Chris Lattner6e80e442010-08-28 17:15:43 +00005333 // Otherwise, expand into a number of unpckl*, start by extending each of
5334 // our (non-undef) elements to the full vector width with the element in the
5335 // bottom slot of the vector (which generates no code for SSE).
5336 for (unsigned i = 0; i < NumElems; ++i) {
5337 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5338 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5339 else
5340 V[i] = DAG.getUNDEF(VT);
5341 }
5342
5343 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005344 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5345 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5346 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005347 unsigned EltStride = NumElems >> 1;
5348 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005349 for (unsigned i = 0; i < EltStride; ++i) {
5350 // If V[i+EltStride] is undef and this is the first round of mixing,
5351 // then it is safe to just drop this shuffle: V[i] is already in the
5352 // right place, the one element (since it's the first round) being
5353 // inserted as undef can be dropped. This isn't safe for successive
5354 // rounds because they will permute elements within both vectors.
5355 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5356 EltStride == NumElems/2)
5357 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005358
Chris Lattner6e80e442010-08-28 17:15:43 +00005359 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005360 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005361 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005362 }
5363 return V[0];
5364 }
Dan Gohman475871a2008-07-27 21:46:04 +00005365 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005366}
5367
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005368// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5369// them in a MMX register. This is better than doing a stack convert.
5370static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005371 DebugLoc dl = Op.getDebugLoc();
5372 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005373
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005374 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5375 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5376 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005377 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005378 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5379 InVec = Op.getOperand(1);
5380 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5381 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005382 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005383 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5384 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5385 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005386 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005387 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5388 Mask[0] = 0; Mask[1] = 2;
5389 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5390 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005391 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005392}
5393
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005394// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5395// to create 256-bit vectors from two other 128-bit ones.
5396static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5397 DebugLoc dl = Op.getDebugLoc();
5398 EVT ResVT = Op.getValueType();
5399
5400 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5401
5402 SDValue V1 = Op.getOperand(0);
5403 SDValue V2 = Op.getOperand(1);
5404 unsigned NumElems = ResVT.getVectorNumElements();
5405
5406 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5407 DAG.getConstant(0, MVT::i32), DAG, dl);
5408 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5409 DAG, dl);
5410}
5411
5412SDValue
5413X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005414 EVT ResVT = Op.getValueType();
5415
5416 assert(Op.getNumOperands() == 2);
5417 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5418 "Unsupported CONCAT_VECTORS for value type");
5419
5420 // We support concatenate two MMX registers and place them in a MMX register.
5421 // This is better than doing a stack convert.
5422 if (ResVT.is128BitVector())
5423 return LowerMMXCONCAT_VECTORS(Op, DAG);
5424
5425 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5426 // from two other 128-bit ones.
5427 return LowerAVXCONCAT_VECTORS(Op, DAG);
5428}
5429
Nate Begemanb9a47b82009-02-23 08:49:38 +00005430// v8i16 shuffles - Prefer shuffles in the following order:
5431// 1. [all] pshuflw, pshufhw, optional move
5432// 2. [ssse3] 1 x pshufb
5433// 3. [ssse3] 2 x pshufb + 1 x por
5434// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005435SDValue
5436X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5437 SelectionDAG &DAG) const {
5438 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005439 SDValue V1 = SVOp->getOperand(0);
5440 SDValue V2 = SVOp->getOperand(1);
5441 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005442 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005443
Nate Begemanb9a47b82009-02-23 08:49:38 +00005444 // Determine if more than 1 of the words in each of the low and high quadwords
5445 // of the result come from the same quadword of one of the two inputs. Undef
5446 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005447 unsigned LoQuad[] = { 0, 0, 0, 0 };
5448 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005449 BitVector InputQuads(4);
5450 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005451 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005452 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005453 MaskVals.push_back(EltIdx);
5454 if (EltIdx < 0) {
5455 ++Quad[0];
5456 ++Quad[1];
5457 ++Quad[2];
5458 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005459 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005460 }
5461 ++Quad[EltIdx / 4];
5462 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005463 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005464
Nate Begemanb9a47b82009-02-23 08:49:38 +00005465 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005466 unsigned MaxQuad = 1;
5467 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005468 if (LoQuad[i] > MaxQuad) {
5469 BestLoQuad = i;
5470 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005471 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005472 }
5473
Nate Begemanb9a47b82009-02-23 08:49:38 +00005474 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005475 MaxQuad = 1;
5476 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005477 if (HiQuad[i] > MaxQuad) {
5478 BestHiQuad = i;
5479 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005480 }
5481 }
5482
Nate Begemanb9a47b82009-02-23 08:49:38 +00005483 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005484 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005485 // single pshufb instruction is necessary. If There are more than 2 input
5486 // quads, disable the next transformation since it does not help SSSE3.
5487 bool V1Used = InputQuads[0] || InputQuads[1];
5488 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005489 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005490 if (InputQuads.count() == 2 && V1Used && V2Used) {
5491 BestLoQuad = InputQuads.find_first();
5492 BestHiQuad = InputQuads.find_next(BestLoQuad);
5493 }
5494 if (InputQuads.count() > 2) {
5495 BestLoQuad = -1;
5496 BestHiQuad = -1;
5497 }
5498 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005499
Nate Begemanb9a47b82009-02-23 08:49:38 +00005500 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5501 // the shuffle mask. If a quad is scored as -1, that means that it contains
5502 // words from all 4 input quadwords.
5503 SDValue NewV;
5504 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005505 SmallVector<int, 8> MaskV;
5506 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5507 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005508 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005509 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5510 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5511 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005512
Nate Begemanb9a47b82009-02-23 08:49:38 +00005513 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5514 // source words for the shuffle, to aid later transformations.
5515 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005516 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005517 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005518 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005519 if (idx != (int)i)
5520 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005521 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005522 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005523 AllWordsInNewV = false;
5524 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005525 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005526
Nate Begemanb9a47b82009-02-23 08:49:38 +00005527 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5528 if (AllWordsInNewV) {
5529 for (int i = 0; i != 8; ++i) {
5530 int idx = MaskVals[i];
5531 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005532 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005533 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005534 if ((idx != i) && idx < 4)
5535 pshufhw = false;
5536 if ((idx != i) && idx > 3)
5537 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005538 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005539 V1 = NewV;
5540 V2Used = false;
5541 BestLoQuad = 0;
5542 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005543 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005544
Nate Begemanb9a47b82009-02-23 08:49:38 +00005545 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5546 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005547 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005548 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5549 unsigned TargetMask = 0;
5550 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005551 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005552 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5553 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5554 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005555 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005556 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005557 }
Eric Christopherfd179292009-08-27 18:07:15 +00005558
Nate Begemanb9a47b82009-02-23 08:49:38 +00005559 // If we have SSSE3, and all words of the result are from 1 input vector,
5560 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5561 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005562 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005563 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005564
Nate Begemanb9a47b82009-02-23 08:49:38 +00005565 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005566 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005567 // mask, and elements that come from V1 in the V2 mask, so that the two
5568 // results can be OR'd together.
5569 bool TwoInputs = V1Used && V2Used;
5570 for (unsigned i = 0; i != 8; ++i) {
5571 int EltIdx = MaskVals[i] * 2;
5572 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005573 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5574 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005575 continue;
5576 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005577 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5578 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005579 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005580 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005581 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005582 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005583 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005584 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005585 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005586
Nate Begemanb9a47b82009-02-23 08:49:38 +00005587 // Calculate the shuffle mask for the second input, shuffle it, and
5588 // OR it with the first shuffled input.
5589 pshufbMask.clear();
5590 for (unsigned i = 0; i != 8; ++i) {
5591 int EltIdx = MaskVals[i] * 2;
5592 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005593 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5594 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005595 continue;
5596 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005597 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5598 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005599 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005600 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005601 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005602 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005603 MVT::v16i8, &pshufbMask[0], 16));
5604 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005605 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005606 }
5607
5608 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5609 // and update MaskVals with new element order.
5610 BitVector InOrder(8);
5611 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005612 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005613 for (int i = 0; i != 4; ++i) {
5614 int idx = MaskVals[i];
5615 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005616 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005617 InOrder.set(i);
5618 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005619 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005620 InOrder.set(i);
5621 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005622 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005623 }
5624 }
5625 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005626 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005627 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005628 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005629
Craig Topperd0a31172012-01-10 06:37:29 +00005630 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005631 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5632 NewV.getOperand(0),
5633 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5634 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005635 }
Eric Christopherfd179292009-08-27 18:07:15 +00005636
Nate Begemanb9a47b82009-02-23 08:49:38 +00005637 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5638 // and update MaskVals with the new element order.
5639 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005640 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005641 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005642 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005643 for (unsigned i = 4; i != 8; ++i) {
5644 int idx = MaskVals[i];
5645 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005646 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005647 InOrder.set(i);
5648 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005649 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005650 InOrder.set(i);
5651 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005652 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005653 }
5654 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005655 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005656 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005657
Craig Topperd0a31172012-01-10 06:37:29 +00005658 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005659 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5660 NewV.getOperand(0),
5661 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5662 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005663 }
Eric Christopherfd179292009-08-27 18:07:15 +00005664
Nate Begemanb9a47b82009-02-23 08:49:38 +00005665 // In case BestHi & BestLo were both -1, which means each quadword has a word
5666 // from each of the four input quadwords, calculate the InOrder bitvector now
5667 // before falling through to the insert/extract cleanup.
5668 if (BestLoQuad == -1 && BestHiQuad == -1) {
5669 NewV = V1;
5670 for (int i = 0; i != 8; ++i)
5671 if (MaskVals[i] < 0 || MaskVals[i] == i)
5672 InOrder.set(i);
5673 }
Eric Christopherfd179292009-08-27 18:07:15 +00005674
Nate Begemanb9a47b82009-02-23 08:49:38 +00005675 // The other elements are put in the right place using pextrw and pinsrw.
5676 for (unsigned i = 0; i != 8; ++i) {
5677 if (InOrder[i])
5678 continue;
5679 int EltIdx = MaskVals[i];
5680 if (EltIdx < 0)
5681 continue;
5682 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005683 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005684 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005685 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005686 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005687 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005688 DAG.getIntPtrConstant(i));
5689 }
5690 return NewV;
5691}
5692
5693// v16i8 shuffles - Prefer shuffles in the following order:
5694// 1. [ssse3] 1 x pshufb
5695// 2. [ssse3] 2 x pshufb + 1 x por
5696// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5697static
Nate Begeman9008ca62009-04-27 18:41:29 +00005698SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005699 SelectionDAG &DAG,
5700 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005701 SDValue V1 = SVOp->getOperand(0);
5702 SDValue V2 = SVOp->getOperand(1);
5703 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005704 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005705
Nate Begemanb9a47b82009-02-23 08:49:38 +00005706 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005707 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005708 // present, fall back to case 3.
5709 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5710 bool V1Only = true;
5711 bool V2Only = true;
5712 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005713 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005714 if (EltIdx < 0)
5715 continue;
5716 if (EltIdx < 16)
5717 V2Only = false;
5718 else
5719 V1Only = false;
5720 }
Eric Christopherfd179292009-08-27 18:07:15 +00005721
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005723 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005724 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005725
Nate Begemanb9a47b82009-02-23 08:49:38 +00005726 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005727 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005728 //
5729 // Otherwise, we have elements from both input vectors, and must zero out
5730 // elements that come from V2 in the first mask, and V1 in the second mask
5731 // so that we can OR them together.
5732 bool TwoInputs = !(V1Only || V2Only);
5733 for (unsigned i = 0; i != 16; ++i) {
5734 int EltIdx = MaskVals[i];
5735 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005736 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005737 continue;
5738 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005739 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005740 }
5741 // If all the elements are from V2, assign it to V1 and return after
5742 // building the first pshufb.
5743 if (V2Only)
5744 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005745 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005746 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005747 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005748 if (!TwoInputs)
5749 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005750
Nate Begemanb9a47b82009-02-23 08:49:38 +00005751 // Calculate the shuffle mask for the second input, shuffle it, and
5752 // OR it with the first shuffled input.
5753 pshufbMask.clear();
5754 for (unsigned i = 0; i != 16; ++i) {
5755 int EltIdx = MaskVals[i];
5756 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005757 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005758 continue;
5759 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005760 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005761 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005762 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005763 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005764 MVT::v16i8, &pshufbMask[0], 16));
5765 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005766 }
Eric Christopherfd179292009-08-27 18:07:15 +00005767
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 // No SSSE3 - Calculate in place words and then fix all out of place words
5769 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5770 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005771 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5772 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005773 SDValue NewV = V2Only ? V2 : V1;
5774 for (int i = 0; i != 8; ++i) {
5775 int Elt0 = MaskVals[i*2];
5776 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005777
Nate Begemanb9a47b82009-02-23 08:49:38 +00005778 // This word of the result is all undef, skip it.
5779 if (Elt0 < 0 && Elt1 < 0)
5780 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005781
Nate Begemanb9a47b82009-02-23 08:49:38 +00005782 // This word of the result is already in the correct place, skip it.
5783 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5784 continue;
5785 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5786 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005787
Nate Begemanb9a47b82009-02-23 08:49:38 +00005788 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5789 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5790 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005791
5792 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5793 // using a single extract together, load it and store it.
5794 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005795 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005796 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005797 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005798 DAG.getIntPtrConstant(i));
5799 continue;
5800 }
5801
Nate Begemanb9a47b82009-02-23 08:49:38 +00005802 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005803 // source byte is not also odd, shift the extracted word left 8 bits
5804 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005805 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005806 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005807 DAG.getIntPtrConstant(Elt1 / 2));
5808 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005809 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005810 DAG.getConstant(8,
5811 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005812 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005813 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5814 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005815 }
5816 // If Elt0 is defined, extract it from the appropriate source. If the
5817 // source byte is not also even, shift the extracted word right 8 bits. If
5818 // Elt1 was also defined, OR the extracted values together before
5819 // inserting them in the result.
5820 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005821 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005822 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5823 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005824 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005825 DAG.getConstant(8,
5826 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005827 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005828 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5829 DAG.getConstant(0x00FF, MVT::i16));
5830 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005831 : InsElt0;
5832 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005833 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005834 DAG.getIntPtrConstant(i));
5835 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005836 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005837}
5838
Evan Cheng7a831ce2007-12-15 03:00:47 +00005839/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005840/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005841/// done when every pair / quad of shuffle mask elements point to elements in
5842/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005843/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005844static
Nate Begeman9008ca62009-04-27 18:41:29 +00005845SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005846 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005847 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005848 SDValue V1 = SVOp->getOperand(0);
5849 SDValue V2 = SVOp->getOperand(1);
5850 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005851 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005852 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005853 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005854 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005855 case MVT::v4f32: NewVT = MVT::v2f64; break;
5856 case MVT::v4i32: NewVT = MVT::v2i64; break;
5857 case MVT::v8i16: NewVT = MVT::v4i32; break;
5858 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005859 }
5860
Nate Begeman9008ca62009-04-27 18:41:29 +00005861 int Scale = NumElems / NewWidth;
5862 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005863 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005864 int StartIdx = -1;
5865 for (int j = 0; j < Scale; ++j) {
5866 int EltIdx = SVOp->getMaskElt(i+j);
5867 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005868 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005869 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005870 StartIdx = EltIdx - (EltIdx % Scale);
5871 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005872 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005873 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005874 if (StartIdx == -1)
5875 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005876 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005877 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005878 }
5879
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005880 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5881 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005882 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005883}
5884
Evan Chengd880b972008-05-09 21:53:03 +00005885/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005886///
Owen Andersone50ed302009-08-10 22:56:29 +00005887static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005888 SDValue SrcOp, SelectionDAG &DAG,
5889 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005890 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005891 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005892 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005893 LD = dyn_cast<LoadSDNode>(SrcOp);
5894 if (!LD) {
5895 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5896 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005897 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005898 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005899 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005900 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005901 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005902 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005903 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005904 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005905 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5906 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5907 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005908 SrcOp.getOperand(0)
5909 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005910 }
5911 }
5912 }
5913
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005914 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005915 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005916 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005917 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005918}
5919
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005920/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5921/// which could not be matched by any known target speficic shuffle
5922static SDValue
5923LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Craig Topper8f35c132012-01-20 09:29:03 +00005924 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005925
Craig Topper8f35c132012-01-20 09:29:03 +00005926 unsigned NumElems = VT.getVectorNumElements();
5927 unsigned NumLaneElems = NumElems / 2;
5928
5929 int MinRange[2][2] = { { static_cast<int>(NumElems),
5930 static_cast<int>(NumElems) },
5931 { static_cast<int>(NumElems),
5932 static_cast<int>(NumElems) } };
5933 int MaxRange[2][2] = { { -1, -1 }, { -1, -1 } };
5934
5935 // Collect used ranges for each source in each lane
5936 for (unsigned l = 0; l < 2; ++l) {
5937 unsigned LaneStart = l*NumLaneElems;
5938 for (unsigned i = 0; i != NumLaneElems; ++i) {
5939 int Idx = SVOp->getMaskElt(i+LaneStart);
5940 if (Idx < 0)
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005941 continue;
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005942
Craig Topper8f35c132012-01-20 09:29:03 +00005943 int Input = 0;
5944 if (Idx >= (int)NumElems) {
5945 Idx -= NumElems;
5946 Input = 1;
5947 }
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005948
Craig Topper8f35c132012-01-20 09:29:03 +00005949 if (Idx > MaxRange[l][Input])
5950 MaxRange[l][Input] = Idx;
5951 if (Idx < MinRange[l][Input])
5952 MinRange[l][Input] = Idx;
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005953 }
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005954 }
5955
Craig Topper8f35c132012-01-20 09:29:03 +00005956 // Make sure each range is 128-bits
5957 int ExtractIdx[2][2] = { { -1, -1 }, { -1, -1 } };
5958 for (unsigned l = 0; l < 2; ++l) {
5959 for (unsigned Input = 0; Input < 2; ++Input) {
5960 if (MinRange[l][Input] == (int)NumElems && MaxRange[l][Input] < 0)
5961 continue;
5962
Craig Topperd9ec7252012-01-21 08:49:33 +00005963 if (MinRange[l][Input] >= 0 && MaxRange[l][Input] < (int)NumLaneElems)
Craig Topper8f35c132012-01-20 09:29:03 +00005964 ExtractIdx[l][Input] = 0;
5965 else if (MinRange[l][Input] >= (int)NumLaneElems &&
Craig Topperd9ec7252012-01-21 08:49:33 +00005966 MaxRange[l][Input] < (int)NumElems)
Craig Topper8f35c132012-01-20 09:29:03 +00005967 ExtractIdx[l][Input] = NumLaneElems;
5968 else
5969 return SDValue();
5970 }
5971 }
5972
5973 DebugLoc dl = SVOp->getDebugLoc();
5974 MVT EltVT = VT.getVectorElementType().getSimpleVT();
5975 EVT NVT = MVT::getVectorVT(EltVT, NumElems/2);
5976
5977 SDValue Ops[2][2];
5978 for (unsigned l = 0; l < 2; ++l) {
5979 for (unsigned Input = 0; Input < 2; ++Input) {
5980 if (ExtractIdx[l][Input] >= 0)
5981 Ops[l][Input] = Extract128BitVector(SVOp->getOperand(Input),
5982 DAG.getConstant(ExtractIdx[l][Input], MVT::i32),
5983 DAG, dl);
5984 else
5985 Ops[l][Input] = DAG.getUNDEF(NVT);
5986 }
5987 }
5988
5989 // Generate 128-bit shuffles
5990 SmallVector<int, 16> Mask1, Mask2;
5991 for (unsigned i = 0; i != NumLaneElems; ++i) {
5992 int Elt = SVOp->getMaskElt(i);
5993 if (Elt >= (int)NumElems) {
5994 Elt %= NumLaneElems;
5995 Elt += NumLaneElems;
5996 } else if (Elt >= 0) {
5997 Elt %= NumLaneElems;
5998 }
5999 Mask1.push_back(Elt);
6000 }
6001 for (unsigned i = NumLaneElems; i != NumElems; ++i) {
6002 int Elt = SVOp->getMaskElt(i);
6003 if (Elt >= (int)NumElems) {
6004 Elt %= NumLaneElems;
6005 Elt += NumLaneElems;
6006 } else if (Elt >= 0) {
6007 Elt %= NumLaneElems;
6008 }
6009 Mask2.push_back(Elt);
6010 }
6011
6012 SDValue Shuf1 = DAG.getVectorShuffle(NVT, dl, Ops[0][0], Ops[0][1], &Mask1[0]);
6013 SDValue Shuf2 = DAG.getVectorShuffle(NVT, dl, Ops[1][0], Ops[1][1], &Mask2[0]);
6014
6015 // Concatenate the result back
6016 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Shuf1,
6017 DAG.getConstant(0, MVT::i32), DAG, dl);
6018 return Insert128BitVector(V, Shuf2, DAG.getConstant(NumElems/2, MVT::i32),
6019 DAG, dl);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006020}
6021
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006022/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6023/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006024static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006025LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006026 SDValue V1 = SVOp->getOperand(0);
6027 SDValue V2 = SVOp->getOperand(1);
6028 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006029 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006030
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006031 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6032
Evan Chengace3c172008-07-22 21:13:36 +00006033 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006034 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006035 SmallVector<int, 8> Mask1(4U, -1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006036 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006037
Evan Chengace3c172008-07-22 21:13:36 +00006038 unsigned NumHi = 0;
6039 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006040 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006041 int Idx = PermMask[i];
6042 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006043 Locs[i] = std::make_pair(-1, -1);
6044 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006045 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6046 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006047 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006048 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006049 NumLo++;
6050 } else {
6051 Locs[i] = std::make_pair(1, NumHi);
6052 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006053 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006054 NumHi++;
6055 }
6056 }
6057 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006058
Evan Chengace3c172008-07-22 21:13:36 +00006059 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006060 // If no more than two elements come from either vector. This can be
6061 // implemented with two shuffles. First shuffle gather the elements.
6062 // The second shuffle, which takes the first shuffle as both of its
6063 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006064 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006065
Nate Begeman9008ca62009-04-27 18:41:29 +00006066 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006067
Evan Chengace3c172008-07-22 21:13:36 +00006068 for (unsigned i = 0; i != 4; ++i) {
6069 if (Locs[i].first == -1)
6070 continue;
6071 else {
6072 unsigned Idx = (i < 2) ? 0 : 4;
6073 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006074 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006075 }
6076 }
6077
Nate Begeman9008ca62009-04-27 18:41:29 +00006078 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006079 } else if (NumLo == 3 || NumHi == 3) {
6080 // Otherwise, we must have three elements from one vector, call it X, and
6081 // one element from the other, call it Y. First, use a shufps to build an
6082 // intermediate vector with the one element from Y and the element from X
6083 // that will be in the same half in the final destination (the indexes don't
6084 // matter). Then, use a shufps to build the final vector, taking the half
6085 // containing the element from Y from the intermediate, and the other half
6086 // from X.
6087 if (NumHi == 3) {
6088 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006089 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006090 std::swap(V1, V2);
6091 }
6092
6093 // Find the element from V2.
6094 unsigned HiIndex;
6095 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006096 int Val = PermMask[HiIndex];
6097 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006098 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006099 if (Val >= 4)
6100 break;
6101 }
6102
Nate Begeman9008ca62009-04-27 18:41:29 +00006103 Mask1[0] = PermMask[HiIndex];
6104 Mask1[1] = -1;
6105 Mask1[2] = PermMask[HiIndex^1];
6106 Mask1[3] = -1;
6107 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006108
6109 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006110 Mask1[0] = PermMask[0];
6111 Mask1[1] = PermMask[1];
6112 Mask1[2] = HiIndex & 1 ? 6 : 4;
6113 Mask1[3] = HiIndex & 1 ? 4 : 6;
6114 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006115 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006116 Mask1[0] = HiIndex & 1 ? 2 : 0;
6117 Mask1[1] = HiIndex & 1 ? 0 : 2;
6118 Mask1[2] = PermMask[2];
6119 Mask1[3] = PermMask[3];
6120 if (Mask1[2] >= 0)
6121 Mask1[2] += 4;
6122 if (Mask1[3] >= 0)
6123 Mask1[3] += 4;
6124 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006125 }
Evan Chengace3c172008-07-22 21:13:36 +00006126 }
6127
6128 // Break it into (shuffle shuffle_hi, shuffle_lo).
6129 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006130 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006131 SmallVector<int,8> LoMask(4U, -1);
6132 SmallVector<int,8> HiMask(4U, -1);
6133
6134 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006135 unsigned MaskIdx = 0;
6136 unsigned LoIdx = 0;
6137 unsigned HiIdx = 2;
6138 for (unsigned i = 0; i != 4; ++i) {
6139 if (i == 2) {
6140 MaskPtr = &HiMask;
6141 MaskIdx = 1;
6142 LoIdx = 0;
6143 HiIdx = 2;
6144 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006145 int Idx = PermMask[i];
6146 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006147 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006148 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006149 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006150 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006151 LoIdx++;
6152 } else {
6153 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006154 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006155 HiIdx++;
6156 }
6157 }
6158
Nate Begeman9008ca62009-04-27 18:41:29 +00006159 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6160 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6161 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006162 for (unsigned i = 0; i != 4; ++i) {
6163 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006164 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006165 } else {
6166 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006167 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006168 }
6169 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006170 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006171}
6172
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006173static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006174 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006175 V = V.getOperand(0);
6176 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6177 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006178 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6179 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6180 // BUILD_VECTOR (load), undef
6181 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006182 if (MayFoldLoad(V))
6183 return true;
6184 return false;
6185}
6186
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006187// FIXME: the version above should always be used. Since there's
6188// a bug where several vector shuffles can't be folded because the
6189// DAG is not updated during lowering and a node claims to have two
6190// uses while it only has one, use this version, and let isel match
6191// another instruction if the load really happens to have more than
6192// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006193// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006194static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006195 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006196 V = V.getOperand(0);
6197 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6198 V = V.getOperand(0);
6199 if (ISD::isNormalLoad(V.getNode()))
6200 return true;
6201 return false;
6202}
6203
6204/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6205/// a vector extract, and if both can be later optimized into a single load.
6206/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6207/// here because otherwise a target specific shuffle node is going to be
6208/// emitted for this shuffle, and the optimization not done.
6209/// FIXME: This is probably not the best approach, but fix the problem
6210/// until the right path is decided.
6211static
6212bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6213 const TargetLowering &TLI) {
6214 EVT VT = V.getValueType();
6215 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6216
6217 // Be sure that the vector shuffle is present in a pattern like this:
6218 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6219 if (!V.hasOneUse())
6220 return false;
6221
6222 SDNode *N = *V.getNode()->use_begin();
6223 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6224 return false;
6225
6226 SDValue EltNo = N->getOperand(1);
6227 if (!isa<ConstantSDNode>(EltNo))
6228 return false;
6229
6230 // If the bit convert changed the number of elements, it is unsafe
6231 // to examine the mask.
6232 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006233 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006234 EVT SrcVT = V.getOperand(0).getValueType();
6235 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6236 return false;
6237 V = V.getOperand(0);
6238 HasShuffleIntoBitcast = true;
6239 }
6240
6241 // Select the input vector, guarding against out of range extract vector.
6242 unsigned NumElems = VT.getVectorNumElements();
6243 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6244 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6245 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6246
Nadav Rotem0b94b5f2012-01-17 09:13:19 +00006247 // If we are accessing the upper part of a YMM register
6248 // then the EXTRACT_VECTOR_ELT is likely to be legalized to a sequence of
6249 // EXTRACT_SUBVECTOR + EXTRACT_VECTOR_ELT, which are not detected at this point
6250 // because the legalization of N did not happen yet.
Nadav Rotema16d4412012-01-17 09:31:09 +00006251 if (Idx >= (int)NumElems/2 && VT.getSizeInBits() == 256)
Nadav Rotem0b94b5f2012-01-17 09:13:19 +00006252 return false;
6253
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006254 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006255 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006256 V = V.getOperand(0);
6257
Craig Toppera51bb3a2012-01-02 08:46:48 +00006258 if (!ISD::isNormalLoad(V.getNode()))
6259 return false;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006260
Craig Toppera51bb3a2012-01-02 08:46:48 +00006261 // Is the original load suitable?
6262 LoadSDNode *LN0 = cast<LoadSDNode>(V);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006263
Craig Toppera51bb3a2012-01-02 08:46:48 +00006264 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
6265 return false;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006266
Craig Toppera51bb3a2012-01-02 08:46:48 +00006267 if (!HasShuffleIntoBitcast)
6268 return true;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006269
Craig Toppera51bb3a2012-01-02 08:46:48 +00006270 // If there's a bitcast before the shuffle, check if the load type and
6271 // alignment is valid.
6272 unsigned Align = LN0->getAlignment();
6273 unsigned NewAlign =
6274 TLI.getTargetData()->getABITypeAlignment(
6275 VT.getTypeForEVT(*DAG.getContext()));
6276
6277 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6278 return false;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006279
6280 return true;
6281}
6282
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006283static
Evan Cheng835580f2010-10-07 20:50:20 +00006284SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6285 EVT VT = Op.getValueType();
6286
6287 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006288 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6289 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006290 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6291 V1, DAG));
6292}
6293
6294static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006295SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006296 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006297 SDValue V1 = Op.getOperand(0);
6298 SDValue V2 = Op.getOperand(1);
6299 EVT VT = Op.getValueType();
6300
6301 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6302
Craig Topper1accb7e2012-01-10 06:54:16 +00006303 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006304 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6305
Evan Cheng0899f5c2011-08-31 02:05:24 +00006306 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6307 return DAG.getNode(ISD::BITCAST, dl, VT,
6308 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6309 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6310 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006311}
6312
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006313static
6314SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6315 SDValue V1 = Op.getOperand(0);
6316 SDValue V2 = Op.getOperand(1);
6317 EVT VT = Op.getValueType();
6318
6319 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6320 "unsupported shuffle type");
6321
6322 if (V2.getOpcode() == ISD::UNDEF)
6323 V2 = V1;
6324
6325 // v4i32 or v4f32
6326 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6327}
6328
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006329static
Craig Topper1accb7e2012-01-10 06:54:16 +00006330SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006331 SDValue V1 = Op.getOperand(0);
6332 SDValue V2 = Op.getOperand(1);
6333 EVT VT = Op.getValueType();
6334 unsigned NumElems = VT.getVectorNumElements();
6335
6336 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6337 // operand of these instructions is only memory, so check if there's a
6338 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6339 // same masks.
6340 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006341
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006342 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006343 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006344 CanFoldLoad = true;
6345
6346 // When V1 is a load, it can be folded later into a store in isel, example:
6347 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6348 // turns into:
6349 // (MOVLPSmr addr:$src1, VR128:$src2)
6350 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006351 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006352 CanFoldLoad = true;
6353
Dan Gohman65fd6562011-11-03 21:49:52 +00006354 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006355 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006356 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006357 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6358
6359 if (NumElems == 4)
Dan Gohman65fd6562011-11-03 21:49:52 +00006360 // If we don't care about the second element, procede to use movss.
6361 if (SVOp->getMaskElt(1) != -1)
6362 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006363 }
6364
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006365 // movl and movlp will both match v2i64, but v2i64 is never matched by
6366 // movl earlier because we make it strict to avoid messing with the movlp load
6367 // folding logic (see the code above getMOVLP call). Match it here then,
6368 // this is horrible, but will stay like this until we move all shuffle
6369 // matching to x86 specific nodes. Note that for the 1st condition all
6370 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006371 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006372 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6373 // as to remove this logic from here, as much as possible
6374 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006375 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006376 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006377 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006378
6379 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6380
6381 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006382 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006383 X86::getShuffleSHUFImmediate(SVOp), DAG);
6384}
6385
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006386static
6387SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006388 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006389 const X86Subtarget *Subtarget) {
6390 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6391 EVT VT = Op.getValueType();
6392 DebugLoc dl = Op.getDebugLoc();
6393 SDValue V1 = Op.getOperand(0);
6394 SDValue V2 = Op.getOperand(1);
6395
6396 if (isZeroShuffle(SVOp))
Craig Topper12216172012-01-13 08:12:35 +00006397 return getZeroVector(VT, Subtarget->hasSSE2(), Subtarget->hasAVX2(),
6398 DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006399
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006400 // Handle splat operations
6401 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006402 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006403 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006404 // Special case, this is the only place now where it's allowed to return
6405 // a vector_shuffle operation without using a target specific node, because
6406 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6407 // this be moved to DAGCombine instead?
6408 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006409 return Op;
6410
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006411 // Use vbroadcast whenever the splat comes from a foldable load
Craig Toppera9376332012-01-10 08:23:59 +00006412 SDValue LD = isVectorBroadcast(Op, Subtarget);
6413 if (LD.getNode())
Nadav Rotemf8c10e52011-11-15 22:50:37 +00006414 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006415
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006416 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006417 if ((Size == 128 && NumElem <= 4) ||
6418 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006419 return SDValue();
6420
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006421 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006422 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006423 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006424
6425 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6426 // do it!
6427 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6428 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6429 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006430 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006431 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006432 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006433 // FIXME: Figure out a cleaner way to do this.
6434 // Try to make use of movq to zero out the top part.
6435 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6436 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6437 if (NewOp.getNode()) {
6438 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6439 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6440 DAG, Subtarget, dl);
6441 }
6442 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6443 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6444 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6445 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6446 DAG, Subtarget, dl);
6447 }
6448 }
6449 return SDValue();
6450}
6451
Dan Gohman475871a2008-07-27 21:46:04 +00006452SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006453X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006454 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006455 SDValue V1 = Op.getOperand(0);
6456 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006457 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006458 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006459 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006460 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006461 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006462 bool V1IsSplat = false;
6463 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006464 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006465 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006466 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006467 MachineFunction &MF = DAG.getMachineFunction();
6468 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006469
Craig Topper3426a3e2011-11-14 06:46:21 +00006470 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006471
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006472 if (V1IsUndef && V2IsUndef)
6473 return DAG.getUNDEF(VT);
6474
6475 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006476
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006477 // Vector shuffle lowering takes 3 steps:
6478 //
6479 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6480 // narrowing and commutation of operands should be handled.
6481 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6482 // shuffle nodes.
6483 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6484 // so the shuffle can be broken into other shuffles and the legalizer can
6485 // try the lowering again.
6486 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006487 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006488 // be matched during isel, all of them must be converted to a target specific
6489 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006490
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006491 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6492 // narrowing and commutation of operands should be handled. The actual code
6493 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006494 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006495 if (NewOp.getNode())
6496 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006497
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006498 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6499 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper94438ba2011-12-16 08:06:31 +00006500 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006501 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006502 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006503 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006504
Craig Topperd0a31172012-01-10 06:37:29 +00006505 if (X86::isMOVDDUPMask(SVOp) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006506 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006507 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006508
Dale Johannesen0488fb62010-09-30 23:57:10 +00006509 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006510 return getMOVHighToLow(Op, dl, DAG);
6511
6512 // Use to match splats
Craig Topper1accb7e2012-01-10 06:54:16 +00006513 if (HasSSE2 && X86::isUNPCKHMask(SVOp, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006514 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006515 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006516
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006517 if (X86::isPSHUFDMask(SVOp)) {
6518 // The actual implementation will match the mask in the if above and then
6519 // during isel it can match several different instructions, not only pshufd
6520 // as its name says, sad but true, emulate the behavior for now...
6521 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6522 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6523
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006524 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6525
Craig Topper1accb7e2012-01-10 06:54:16 +00006526 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006527 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6528
Craig Topperb3982da2011-12-31 23:50:21 +00006529 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006530 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006531 }
Eric Christopherfd179292009-08-27 18:07:15 +00006532
Evan Chengf26ffe92008-05-29 08:22:04 +00006533 // Check if this can be converted into a logical shift.
6534 bool isLeft = false;
6535 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006536 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006537 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006538 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006539 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006540 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006541 EVT EltVT = VT.getVectorElementType();
6542 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006543 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006544 }
Eric Christopherfd179292009-08-27 18:07:15 +00006545
Nate Begeman9008ca62009-04-27 18:41:29 +00006546 if (X86::isMOVLMask(SVOp)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006547 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006548 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006549 if (!X86::isMOVLPMask(SVOp)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006550 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006551 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6552
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006553 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006554 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6555 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006556 }
Eric Christopherfd179292009-08-27 18:07:15 +00006557
Nate Begeman9008ca62009-04-27 18:41:29 +00006558 // FIXME: fold these into legal mask.
Craig Topperc0d82852011-11-22 00:44:41 +00006559 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006560 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006561
Dale Johannesen0488fb62010-09-30 23:57:10 +00006562 if (X86::isMOVHLPSMask(SVOp))
6563 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006564
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006565 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006566 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006567
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006568 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006569 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006570
Dale Johannesen0488fb62010-09-30 23:57:10 +00006571 if (X86::isMOVLPMask(SVOp))
Craig Topper1accb7e2012-01-10 06:54:16 +00006572 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006573
Nate Begeman9008ca62009-04-27 18:41:29 +00006574 if (ShouldXformToMOVHLPS(SVOp) ||
6575 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6576 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006577
Evan Chengf26ffe92008-05-29 08:22:04 +00006578 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006579 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006580 EVT EltVT = VT.getVectorElementType();
6581 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006582 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006583 }
Eric Christopherfd179292009-08-27 18:07:15 +00006584
Evan Cheng9eca5e82006-10-25 21:49:50 +00006585 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006586 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6587 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006588 V1IsSplat = isSplatVector(V1.getNode());
6589 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006590
Chris Lattner8a594482007-11-25 00:24:49 +00006591 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper38034c52011-11-26 22:55:48 +00006592 if (V1IsSplat && !V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006593 Op = CommuteVectorShuffle(SVOp, DAG);
6594 SVOp = cast<ShuffleVectorSDNode>(Op);
6595 V1 = SVOp->getOperand(0);
6596 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006597 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006598 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006599 }
6600
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006601 ArrayRef<int> M = SVOp->getMask();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006602
6603 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006604 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006605 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006606 return V1;
6607 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6608 // the instruction selector will not match, so get a canonical MOVL with
6609 // swapped operands to undo the commute.
6610 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006611 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006612
Craig Topperbeabc6c2011-12-05 06:56:46 +00006613 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006614 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006615
Craig Topperbeabc6c2011-12-05 06:56:46 +00006616 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006617 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006618
Evan Cheng9bbbb982006-10-25 20:48:19 +00006619 if (V2IsSplat) {
6620 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006621 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006622 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006623 SDValue NewMask = NormalizeMask(SVOp, DAG);
6624 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6625 if (NSVOp != SVOp) {
Craig Topperc0d82852011-11-22 00:44:41 +00006626 if (X86::isUNPCKLMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006627 return NewMask;
Craig Topperc0d82852011-11-22 00:44:41 +00006628 } else if (X86::isUNPCKHMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006629 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006630 }
6631 }
6632 }
6633
Evan Cheng9eca5e82006-10-25 21:49:50 +00006634 if (Commuted) {
6635 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006636 // FIXME: this seems wrong.
6637 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6638 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006639
Craig Topperc0d82852011-11-22 00:44:41 +00006640 if (X86::isUNPCKLMask(NewSVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006641 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006642
Craig Topperc0d82852011-11-22 00:44:41 +00006643 if (X86::isUNPCKHMask(NewSVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006644 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006645 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006646
Nate Begeman9008ca62009-04-27 18:41:29 +00006647 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006648 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006649 return CommuteVectorShuffle(SVOp, DAG);
6650
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006651 // The checks below are all present in isShuffleMaskLegal, but they are
6652 // inlined here right now to enable us to directly emit target specific
6653 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006654
Craig Topper0e2037b2012-01-20 05:53:00 +00006655 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006656 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006657 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006658 DAG);
6659
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006660 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6661 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006662 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006663 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006664 }
6665
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006666 if (isPSHUFHWMask(M, VT))
6667 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6668 X86::getShufflePSHUFHWImmediate(SVOp),
6669 DAG);
6670
6671 if (isPSHUFLWMask(M, VT))
6672 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6673 X86::getShufflePSHUFLWImmediate(SVOp),
6674 DAG);
6675
Craig Topper1a7700a2012-01-19 08:19:12 +00006676 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006677 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006678 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006679
Craig Topper94438ba2011-12-16 08:06:31 +00006680 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006681 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006682 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006683 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006684
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006685 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006686 // Generate target specific nodes for 128 or 256-bit shuffles only
6687 // supported in the AVX instruction set.
6688 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006689
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006690 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006691 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006692 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6693
Craig Topper70b883b2011-11-28 10:14:51 +00006694 // Handle VPERMILPS/D* permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006695 if (isVPERMILPMask(M, VT, HasAVX))
Craig Topper316cd2a2011-11-30 06:25:25 +00006696 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006697 getShuffleVPERMILPImmediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006698
Craig Topper70b883b2011-11-28 10:14:51 +00006699 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006700 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006701 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006702 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006703
6704 //===--------------------------------------------------------------------===//
6705 // Since no target specific shuffle was selected for this generic one,
6706 // lower it into other known shuffles. FIXME: this isn't true yet, but
6707 // this is the plan.
6708 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006709
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006710 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6711 if (VT == MVT::v8i16) {
6712 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6713 if (NewOp.getNode())
6714 return NewOp;
6715 }
6716
6717 if (VT == MVT::v16i8) {
6718 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6719 if (NewOp.getNode())
6720 return NewOp;
6721 }
6722
6723 // Handle all 128-bit wide vectors with 4 elements, and match them with
6724 // several different shuffle types.
6725 if (NumElems == 4 && VT.getSizeInBits() == 128)
6726 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6727
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006728 // Handle general 256-bit shuffles
6729 if (VT.is256BitVector())
6730 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6731
Dan Gohman475871a2008-07-27 21:46:04 +00006732 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006733}
6734
Dan Gohman475871a2008-07-27 21:46:04 +00006735SDValue
6736X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006737 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006738 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006739 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006740
6741 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6742 return SDValue();
6743
Duncan Sands83ec4b62008-06-06 12:08:01 +00006744 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006745 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006746 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006747 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006748 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006749 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006750 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006751 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6752 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6753 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006754 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6755 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006756 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006757 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006758 Op.getOperand(0)),
6759 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006760 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006761 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006762 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006763 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006764 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006765 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006766 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6767 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006768 // result has a single use which is a store or a bitcast to i32. And in
6769 // the case of a store, it's not worth it if the index is a constant 0,
6770 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006771 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006772 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006773 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006774 if ((User->getOpcode() != ISD::STORE ||
6775 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6776 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006777 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006778 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006779 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006780 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006781 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006782 Op.getOperand(0)),
6783 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006784 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Pete Coopera77214a2011-11-14 19:38:42 +00006785 } else if (VT == MVT::i32 || VT == MVT::i64) {
6786 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006787 if (isa<ConstantSDNode>(Op.getOperand(1)))
6788 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006789 }
Dan Gohman475871a2008-07-27 21:46:04 +00006790 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006791}
6792
6793
Dan Gohman475871a2008-07-27 21:46:04 +00006794SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006795X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6796 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006797 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006798 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006799
David Greene74a579d2011-02-10 16:57:36 +00006800 SDValue Vec = Op.getOperand(0);
6801 EVT VecVT = Vec.getValueType();
6802
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006803 // If this is a 256-bit vector result, first extract the 128-bit vector and
6804 // then extract the element from the 128-bit vector.
6805 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006806 DebugLoc dl = Op.getNode()->getDebugLoc();
6807 unsigned NumElems = VecVT.getVectorNumElements();
6808 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006809 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6810
6811 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006812 bool Upper = IdxVal >= NumElems/2;
6813 Vec = Extract128BitVector(Vec,
6814 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006815
David Greene74a579d2011-02-10 16:57:36 +00006816 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006817 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006818 }
6819
6820 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6821
Craig Topperd0a31172012-01-10 06:37:29 +00006822 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006823 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006824 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006825 return Res;
6826 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006827
Owen Andersone50ed302009-08-10 22:56:29 +00006828 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006829 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006830 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006831 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006832 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006833 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006834 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006835 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6836 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006837 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006838 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006839 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006840 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006841 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006842 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006843 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006844 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006845 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006846 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006847 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006848 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006849 if (Idx == 0)
6850 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006851
Evan Cheng0db9fe62006-04-25 20:13:52 +00006852 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006853 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006854 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006855 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006856 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006857 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006858 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006859 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006860 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6861 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6862 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006863 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006864 if (Idx == 0)
6865 return Op;
6866
6867 // UNPCKHPD the element to the lowest double word, then movsd.
6868 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6869 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006870 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006871 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006872 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006873 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006874 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006875 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006876 }
6877
Dan Gohman475871a2008-07-27 21:46:04 +00006878 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006879}
6880
Dan Gohman475871a2008-07-27 21:46:04 +00006881SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006882X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6883 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006884 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006885 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006886 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006887
Dan Gohman475871a2008-07-27 21:46:04 +00006888 SDValue N0 = Op.getOperand(0);
6889 SDValue N1 = Op.getOperand(1);
6890 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006891
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006892 if (VT.getSizeInBits() == 256)
6893 return SDValue();
6894
Dan Gohman8a55ce42009-09-23 21:02:20 +00006895 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006896 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006897 unsigned Opc;
6898 if (VT == MVT::v8i16)
6899 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006900 else if (VT == MVT::v16i8)
6901 Opc = X86ISD::PINSRB;
6902 else
6903 Opc = X86ISD::PINSRB;
6904
Nate Begeman14d12ca2008-02-11 04:19:36 +00006905 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6906 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006907 if (N1.getValueType() != MVT::i32)
6908 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6909 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006910 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006911 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006912 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006913 // Bits [7:6] of the constant are the source select. This will always be
6914 // zero here. The DAG Combiner may combine an extract_elt index into these
6915 // bits. For example (insert (extract, 3), 2) could be matched by putting
6916 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006917 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006918 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006919 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006920 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006921 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006922 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006923 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006924 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Pete Coopera77214a2011-11-14 19:38:42 +00006925 } else if ((EltVT == MVT::i32 || EltVT == MVT::i64) &&
6926 isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006927 // PINSR* works with constant index.
6928 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006929 }
Dan Gohman475871a2008-07-27 21:46:04 +00006930 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006931}
6932
Dan Gohman475871a2008-07-27 21:46:04 +00006933SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006934X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006935 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006936 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006937
David Greene6b381262011-02-09 15:32:06 +00006938 DebugLoc dl = Op.getDebugLoc();
6939 SDValue N0 = Op.getOperand(0);
6940 SDValue N1 = Op.getOperand(1);
6941 SDValue N2 = Op.getOperand(2);
6942
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006943 // If this is a 256-bit vector result, first extract the 128-bit vector,
6944 // insert the element into the extracted half and then place it back.
6945 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00006946 if (!isa<ConstantSDNode>(N2))
6947 return SDValue();
6948
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006949 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00006950 unsigned NumElems = VT.getVectorNumElements();
6951 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006952 bool Upper = IdxVal >= NumElems/2;
6953 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
6954 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00006955
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006956 // Insert the element into the desired half.
6957 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
6958 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00006959
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006960 // Insert the changed part back to the 256-bit vector
6961 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00006962 }
6963
Craig Topperd0a31172012-01-10 06:37:29 +00006964 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00006965 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6966
Dan Gohman8a55ce42009-09-23 21:02:20 +00006967 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006968 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006969
Dan Gohman8a55ce42009-09-23 21:02:20 +00006970 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006971 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6972 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006973 if (N1.getValueType() != MVT::i32)
6974 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6975 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006976 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006977 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006978 }
Dan Gohman475871a2008-07-27 21:46:04 +00006979 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006980}
6981
Dan Gohman475871a2008-07-27 21:46:04 +00006982SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006983X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00006984 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006985 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006986 EVT OpVT = Op.getValueType();
6987
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00006988 // If this is a 256-bit vector result, first insert into a 128-bit
6989 // vector and then insert into the 256-bit vector.
6990 if (OpVT.getSizeInBits() > 128) {
6991 // Insert into a 128-bit vector.
6992 EVT VT128 = EVT::getVectorVT(*Context,
6993 OpVT.getVectorElementType(),
6994 OpVT.getVectorNumElements() / 2);
6995
6996 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6997
6998 // Insert the 128-bit vector.
6999 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7000 DAG.getConstant(0, MVT::i32),
7001 DAG, dl);
7002 }
7003
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007004 if (Op.getValueType() == MVT::v1i64 &&
7005 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007006 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007007
Owen Anderson825b72b2009-08-11 20:47:22 +00007008 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007009 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7010 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007011 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007012 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007013}
7014
David Greene91585092011-01-26 15:38:49 +00007015// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7016// a simple subregister reference or explicit instructions to grab
7017// upper bits of a vector.
7018SDValue
7019X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7020 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007021 DebugLoc dl = Op.getNode()->getDebugLoc();
7022 SDValue Vec = Op.getNode()->getOperand(0);
7023 SDValue Idx = Op.getNode()->getOperand(1);
7024
7025 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7026 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7027 return Extract128BitVector(Vec, Idx, DAG, dl);
7028 }
David Greene91585092011-01-26 15:38:49 +00007029 }
7030 return SDValue();
7031}
7032
David Greenecfe33c42011-01-26 19:13:22 +00007033// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7034// simple superregister reference or explicit instructions to insert
7035// the upper bits of a vector.
7036SDValue
7037X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7038 if (Subtarget->hasAVX()) {
7039 DebugLoc dl = Op.getNode()->getDebugLoc();
7040 SDValue Vec = Op.getNode()->getOperand(0);
7041 SDValue SubVec = Op.getNode()->getOperand(1);
7042 SDValue Idx = Op.getNode()->getOperand(2);
7043
7044 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7045 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007046 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007047 }
7048 }
7049 return SDValue();
7050}
7051
Bill Wendling056292f2008-09-16 21:48:12 +00007052// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7053// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7054// one of the above mentioned nodes. It has to be wrapped because otherwise
7055// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7056// be used to form addressing mode. These wrapped nodes will be selected
7057// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007058SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007059X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007060 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007061
Chris Lattner41621a22009-06-26 19:22:52 +00007062 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7063 // global base reg.
7064 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007065 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007066 CodeModel::Model M = getTargetMachine().getCodeModel();
7067
Chris Lattner4f066492009-07-11 20:29:19 +00007068 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007069 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007070 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007071 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007072 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007073 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007074 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007075
Evan Cheng1606e8e2009-03-13 07:51:59 +00007076 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007077 CP->getAlignment(),
7078 CP->getOffset(), OpFlag);
7079 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007080 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007081 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007082 if (OpFlag) {
7083 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007084 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007085 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007086 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007087 }
7088
7089 return Result;
7090}
7091
Dan Gohmand858e902010-04-17 15:26:15 +00007092SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007093 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007094
Chris Lattner18c59872009-06-27 04:16:01 +00007095 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7096 // global base reg.
7097 unsigned char OpFlag = 0;
7098 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007099 CodeModel::Model M = getTargetMachine().getCodeModel();
7100
Chris Lattner4f066492009-07-11 20:29:19 +00007101 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007102 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007103 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007104 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007105 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007106 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007107 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007108
Chris Lattner18c59872009-06-27 04:16:01 +00007109 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7110 OpFlag);
7111 DebugLoc DL = JT->getDebugLoc();
7112 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007113
Chris Lattner18c59872009-06-27 04:16:01 +00007114 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007115 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007116 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7117 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007118 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007119 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007120
Chris Lattner18c59872009-06-27 04:16:01 +00007121 return Result;
7122}
7123
7124SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007125X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007126 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007127
Chris Lattner18c59872009-06-27 04:16:01 +00007128 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7129 // global base reg.
7130 unsigned char OpFlag = 0;
7131 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007132 CodeModel::Model M = getTargetMachine().getCodeModel();
7133
Chris Lattner4f066492009-07-11 20:29:19 +00007134 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007135 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7136 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7137 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007138 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007139 } else if (Subtarget->isPICStyleGOT()) {
7140 OpFlag = X86II::MO_GOT;
7141 } else if (Subtarget->isPICStyleStubPIC()) {
7142 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7143 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7144 OpFlag = X86II::MO_DARWIN_NONLAZY;
7145 }
Eric Christopherfd179292009-08-27 18:07:15 +00007146
Chris Lattner18c59872009-06-27 04:16:01 +00007147 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007148
Chris Lattner18c59872009-06-27 04:16:01 +00007149 DebugLoc DL = Op.getDebugLoc();
7150 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007151
7152
Chris Lattner18c59872009-06-27 04:16:01 +00007153 // With PIC, the address is actually $g + Offset.
7154 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007155 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007156 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7157 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007158 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007159 Result);
7160 }
Eric Christopherfd179292009-08-27 18:07:15 +00007161
Eli Friedman586272d2011-08-11 01:48:05 +00007162 // For symbols that require a load from a stub to get the address, emit the
7163 // load.
7164 if (isGlobalStubReference(OpFlag))
7165 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007166 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007167
Chris Lattner18c59872009-06-27 04:16:01 +00007168 return Result;
7169}
7170
Dan Gohman475871a2008-07-27 21:46:04 +00007171SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007172X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007173 // Create the TargetBlockAddressAddress node.
7174 unsigned char OpFlags =
7175 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007176 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007177 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007178 DebugLoc dl = Op.getDebugLoc();
7179 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7180 /*isTarget=*/true, OpFlags);
7181
Dan Gohmanf705adb2009-10-30 01:28:02 +00007182 if (Subtarget->isPICStyleRIPRel() &&
7183 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007184 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7185 else
7186 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007187
Dan Gohman29cbade2009-11-20 23:18:13 +00007188 // With PIC, the address is actually $g + Offset.
7189 if (isGlobalRelativeToPICBase(OpFlags)) {
7190 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7191 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7192 Result);
7193 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007194
7195 return Result;
7196}
7197
7198SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007199X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007200 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007201 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007202 // Create the TargetGlobalAddress node, folding in the constant
7203 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007204 unsigned char OpFlags =
7205 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007206 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007207 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007208 if (OpFlags == X86II::MO_NO_FLAG &&
7209 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007210 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007211 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007212 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007213 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007214 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007215 }
Eric Christopherfd179292009-08-27 18:07:15 +00007216
Chris Lattner4f066492009-07-11 20:29:19 +00007217 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007218 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007219 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7220 else
7221 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007222
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007223 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007224 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007225 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7226 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007227 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007228 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007229
Chris Lattner36c25012009-07-10 07:34:39 +00007230 // For globals that require a load from a stub to get the address, emit the
7231 // load.
7232 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007233 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007234 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007235
Dan Gohman6520e202008-10-18 02:06:02 +00007236 // If there was a non-zero offset that we didn't fold, create an explicit
7237 // addition for it.
7238 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007239 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007240 DAG.getConstant(Offset, getPointerTy()));
7241
Evan Cheng0db9fe62006-04-25 20:13:52 +00007242 return Result;
7243}
7244
Evan Chengda43bcf2008-09-24 00:05:32 +00007245SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007246X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007247 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007248 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007249 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007250}
7251
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007252static SDValue
7253GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007254 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007255 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007256 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007257 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007258 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007259 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007260 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007261 GA->getOffset(),
7262 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007263 if (InFlag) {
7264 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007265 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007266 } else {
7267 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007268 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007269 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007270
7271 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007272 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007273
Rafael Espindola15f1b662009-04-24 12:59:40 +00007274 SDValue Flag = Chain.getValue(1);
7275 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007276}
7277
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007278// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007279static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007280LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007281 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007282 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007283 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7284 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007285 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007286 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007287 InFlag = Chain.getValue(1);
7288
Chris Lattnerb903bed2009-06-26 21:20:29 +00007289 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007290}
7291
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007292// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007293static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007294LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007295 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007296 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7297 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007298}
7299
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007300// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7301// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007302static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007303 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007304 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007305 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007306
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007307 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7308 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7309 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007310
Michael J. Spencerec38de22010-10-10 22:04:20 +00007311 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007312 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007313 MachinePointerInfo(Ptr),
7314 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007315
Chris Lattnerb903bed2009-06-26 21:20:29 +00007316 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007317 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7318 // initialexec.
7319 unsigned WrapperKind = X86ISD::Wrapper;
7320 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007321 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007322 } else if (is64Bit) {
7323 assert(model == TLSModel::InitialExec);
7324 OperandFlags = X86II::MO_GOTTPOFF;
7325 WrapperKind = X86ISD::WrapperRIP;
7326 } else {
7327 assert(model == TLSModel::InitialExec);
7328 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007329 }
Eric Christopherfd179292009-08-27 18:07:15 +00007330
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007331 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7332 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007333 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007334 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007335 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007336 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007337
Rafael Espindola9a580232009-02-27 13:37:18 +00007338 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007339 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007340 MachinePointerInfo::getGOT(), false, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007341
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007342 // The address of the thread local variable is the add of the thread
7343 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007344 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007345}
7346
Dan Gohman475871a2008-07-27 21:46:04 +00007347SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007348X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007349
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007350 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007351 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007352
Eric Christopher30ef0e52010-06-03 04:07:48 +00007353 if (Subtarget->isTargetELF()) {
7354 // TODO: implement the "local dynamic" model
7355 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007356
Eric Christopher30ef0e52010-06-03 04:07:48 +00007357 // If GV is an alias then use the aliasee for determining
7358 // thread-localness.
7359 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7360 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007361
7362 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007363 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007364
Eric Christopher30ef0e52010-06-03 04:07:48 +00007365 switch (model) {
7366 case TLSModel::GeneralDynamic:
7367 case TLSModel::LocalDynamic: // not implemented
7368 if (Subtarget->is64Bit())
7369 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7370 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007371
Eric Christopher30ef0e52010-06-03 04:07:48 +00007372 case TLSModel::InitialExec:
7373 case TLSModel::LocalExec:
7374 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7375 Subtarget->is64Bit());
7376 }
7377 } else if (Subtarget->isTargetDarwin()) {
7378 // Darwin only has one model of TLS. Lower to that.
7379 unsigned char OpFlag = 0;
7380 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7381 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007382
Eric Christopher30ef0e52010-06-03 04:07:48 +00007383 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7384 // global base reg.
7385 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7386 !Subtarget->is64Bit();
7387 if (PIC32)
7388 OpFlag = X86II::MO_TLVP_PIC_BASE;
7389 else
7390 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007391 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007392 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007393 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007394 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007395 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007396
Eric Christopher30ef0e52010-06-03 04:07:48 +00007397 // With PIC32, the address is actually $g + Offset.
7398 if (PIC32)
7399 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7400 DAG.getNode(X86ISD::GlobalBaseReg,
7401 DebugLoc(), getPointerTy()),
7402 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007403
Eric Christopher30ef0e52010-06-03 04:07:48 +00007404 // Lowering the machine isd will make sure everything is in the right
7405 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007406 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007407 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007408 SDValue Args[] = { Chain, Offset };
7409 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007410
Eric Christopher30ef0e52010-06-03 04:07:48 +00007411 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7412 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7413 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007414
Eric Christopher30ef0e52010-06-03 04:07:48 +00007415 // And our return value (tls address) is in the standard call return value
7416 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007417 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007418 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7419 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007420 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007421
David Blaikie4d6ccb52012-01-20 21:51:11 +00007422 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007423}
7424
Evan Cheng0db9fe62006-04-25 20:13:52 +00007425
Chad Rosierb90d2a92012-01-03 23:19:12 +00007426/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7427/// and take a 2 x i32 value to shift plus a shift amount.
7428SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007429 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007430 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007431 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007432 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007433 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007434 SDValue ShOpLo = Op.getOperand(0);
7435 SDValue ShOpHi = Op.getOperand(1);
7436 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007437 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007438 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007439 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007440
Dan Gohman475871a2008-07-27 21:46:04 +00007441 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007442 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007443 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7444 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007445 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007446 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7447 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007448 }
Evan Chenge3413162006-01-09 18:33:28 +00007449
Owen Anderson825b72b2009-08-11 20:47:22 +00007450 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7451 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007452 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007453 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007454
Dan Gohman475871a2008-07-27 21:46:04 +00007455 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007456 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007457 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7458 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007459
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007460 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007461 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7462 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007463 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007464 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7465 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007466 }
7467
Dan Gohman475871a2008-07-27 21:46:04 +00007468 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007469 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007470}
Evan Chenga3195e82006-01-12 22:54:21 +00007471
Dan Gohmand858e902010-04-17 15:26:15 +00007472SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7473 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007474 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007475
Dale Johannesen0488fb62010-09-30 23:57:10 +00007476 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007477 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007478
Owen Anderson825b72b2009-08-11 20:47:22 +00007479 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007480 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007481
Eli Friedman36df4992009-05-27 00:47:34 +00007482 // These are really Legal; return the operand so the caller accepts it as
7483 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007484 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007485 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007486 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007487 Subtarget->is64Bit()) {
7488 return Op;
7489 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007490
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007491 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007492 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007493 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007494 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007495 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007496 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007497 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007498 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007499 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007500 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7501}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007502
Owen Andersone50ed302009-08-10 22:56:29 +00007503SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007504 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007505 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007506 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007507 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007508 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007509 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007510 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007511 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007512 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007513 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007514
Chris Lattner492a43e2010-09-22 01:28:21 +00007515 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007516
Stuart Hastings84be9582011-06-02 15:57:11 +00007517 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7518 MachineMemOperand *MMO;
7519 if (FI) {
7520 int SSFI = FI->getIndex();
7521 MMO =
7522 DAG.getMachineFunction()
7523 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7524 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7525 } else {
7526 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7527 StackSlot = StackSlot.getOperand(1);
7528 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007529 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007530 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7531 X86ISD::FILD, DL,
7532 Tys, Ops, array_lengthof(Ops),
7533 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007534
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007535 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007536 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007537 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007538
7539 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7540 // shouldn't be necessary except that RFP cannot be live across
7541 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007542 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007543 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7544 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007545 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007546 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007547 SDValue Ops[] = {
7548 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7549 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007550 MachineMemOperand *MMO =
7551 DAG.getMachineFunction()
7552 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007553 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007554
Chris Lattner492a43e2010-09-22 01:28:21 +00007555 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7556 Ops, array_lengthof(Ops),
7557 Op.getValueType(), MMO);
7558 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007559 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007560 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007561 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007562
Evan Cheng0db9fe62006-04-25 20:13:52 +00007563 return Result;
7564}
7565
Bill Wendling8b8a6362009-01-17 03:56:04 +00007566// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007567SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7568 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007569 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007570 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007571 movq %rax, %xmm0
7572 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7573 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7574 #ifdef __SSE3__
7575 haddpd %xmm0, %xmm0
7576 #else
7577 pshufd $0x4e, %xmm0, %xmm1
7578 addpd %xmm1, %xmm0
7579 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007580 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007581
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007582 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007583 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007584
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007585 // Build some magic constants.
Chad Rosier01d426e2011-12-15 01:16:09 +00007586 SmallVector<Constant*,4> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007587 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
Bill Wendling397ae212012-01-05 02:13:20 +00007588 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
Owen Andersoneed707b2009-07-24 23:12:02 +00007589 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7590 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007591 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007592 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007593
Chris Lattner97484792012-01-25 09:56:22 +00007594 SmallVector<Constant*,2> CV1;
7595 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00007596 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00007597 CV1.push_back(
7598 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7599 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007600 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007601
Bill Wendling397ae212012-01-05 02:13:20 +00007602 // Load the 64-bit value into an XMM register.
7603 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
7604 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007605 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007606 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007607 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007608 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
7609 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
7610 CLod0);
7611
Owen Anderson825b72b2009-08-11 20:47:22 +00007612 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007613 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007614 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007615 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007616 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00007617 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007618
Craig Topperd0a31172012-01-10 06:37:29 +00007619 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00007620 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
7621 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
7622 } else {
7623 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
7624 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
7625 S2F, 0x4E, DAG);
7626 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
7627 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
7628 Sub);
7629 }
7630
7631 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007632 DAG.getIntPtrConstant(0));
7633}
7634
Bill Wendling8b8a6362009-01-17 03:56:04 +00007635// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007636SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7637 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007638 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007639 // FP constant to bias correct the final result.
7640 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007641 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007642
7643 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007644 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007645 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007646
Eli Friedmanf3704762011-08-29 21:15:46 +00007647 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00007648 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007649
Owen Anderson825b72b2009-08-11 20:47:22 +00007650 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007651 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007652 DAG.getIntPtrConstant(0));
7653
7654 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007655 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007656 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007657 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007658 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007659 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007660 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007661 MVT::v2f64, Bias)));
7662 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007663 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007664 DAG.getIntPtrConstant(0));
7665
7666 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007667 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007668
7669 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007670 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007671
Owen Anderson825b72b2009-08-11 20:47:22 +00007672 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007673 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007674 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007675 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007676 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007677 }
7678
7679 // Handle final rounding.
7680 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007681}
7682
Dan Gohmand858e902010-04-17 15:26:15 +00007683SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7684 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007685 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007686 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007687
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007688 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007689 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7690 // the optimization here.
7691 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007692 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007693
Owen Andersone50ed302009-08-10 22:56:29 +00007694 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007695 EVT DstVT = Op.getValueType();
7696 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007697 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007698 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007699 return LowerUINT_TO_FP_i32(Op, DAG);
Bill Wendlingf6c07472012-01-10 19:41:30 +00007700 else if (Subtarget->is64Bit() &&
7701 SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00007702 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00007703
7704 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007705 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007706 if (SrcVT == MVT::i32) {
7707 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7708 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7709 getPointerTy(), StackSlot, WordOff);
7710 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007711 StackSlot, MachinePointerInfo(),
7712 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007713 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007714 OffsetSlot, MachinePointerInfo(),
7715 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007716 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7717 return Fild;
7718 }
7719
7720 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7721 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00007722 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007723 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007724 // For i64 source, we need to add the appropriate power of 2 if the input
7725 // was negative. This is the same as the optimization in
7726 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7727 // we must be careful to do the computation in x87 extended precision, not
7728 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007729 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7730 MachineMemOperand *MMO =
7731 DAG.getMachineFunction()
7732 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7733 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007734
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007735 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7736 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007737 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7738 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007739
7740 APInt FF(32, 0x5F800000ULL);
7741
7742 // Check whether the sign bit is set.
7743 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7744 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7745 ISD::SETLT);
7746
7747 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7748 SDValue FudgePtr = DAG.getConstantPool(
7749 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7750 getPointerTy());
7751
7752 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7753 SDValue Zero = DAG.getIntPtrConstant(0);
7754 SDValue Four = DAG.getIntPtrConstant(4);
7755 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7756 Zero, Four);
7757 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7758
7759 // Load the value out, extending it from f32 to f80.
7760 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007761 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007762 FudgePtr, MachinePointerInfo::getConstantPool(),
7763 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007764 // Extend everything to 80 bits to force it to be done on x87.
7765 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7766 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007767}
7768
Dan Gohman475871a2008-07-27 21:46:04 +00007769std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007770FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007771 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007772
Owen Andersone50ed302009-08-10 22:56:29 +00007773 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007774
7775 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007776 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7777 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007778 }
7779
Owen Anderson825b72b2009-08-11 20:47:22 +00007780 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7781 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007782 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007783
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007784 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007785 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007786 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007787 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007788 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007789 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007790 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007791 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007792
Evan Cheng87c89352007-10-15 20:11:21 +00007793 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7794 // stack slot.
7795 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007796 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007797 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007798 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007799
Michael J. Spencerec38de22010-10-10 22:04:20 +00007800
7801
Evan Cheng0db9fe62006-04-25 20:13:52 +00007802 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007803 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007804 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007805 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7806 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7807 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007808 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007809
Dan Gohman475871a2008-07-27 21:46:04 +00007810 SDValue Chain = DAG.getEntryNode();
7811 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007812 EVT TheVT = Op.getOperand(0).getValueType();
7813 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007814 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007815 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007816 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007817 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007818 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007819 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007820 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007821 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007822
Chris Lattner492a43e2010-09-22 01:28:21 +00007823 MachineMemOperand *MMO =
7824 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7825 MachineMemOperand::MOLoad, MemSize, MemSize);
7826 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7827 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007828 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007829 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007830 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7831 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007832
Chris Lattner07290932010-09-22 01:05:16 +00007833 MachineMemOperand *MMO =
7834 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7835 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007836
Evan Cheng0db9fe62006-04-25 20:13:52 +00007837 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007838 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007839 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7840 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007841
Chris Lattner27a6c732007-11-24 07:07:01 +00007842 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007843}
7844
Dan Gohmand858e902010-04-17 15:26:15 +00007845SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7846 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007847 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007848 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007849
Eli Friedman948e95a2009-05-23 09:59:16 +00007850 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007851 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007852 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7853 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007854
Chris Lattner27a6c732007-11-24 07:07:01 +00007855 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007856 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007857 FIST, StackSlot, MachinePointerInfo(),
7858 false, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007859}
7860
Dan Gohmand858e902010-04-17 15:26:15 +00007861SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7862 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007863 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7864 SDValue FIST = Vals.first, StackSlot = Vals.second;
7865 assert(FIST.getNode() && "Unexpected failure");
7866
7867 // Load the result.
7868 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007869 FIST, StackSlot, MachinePointerInfo(),
7870 false, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007871}
7872
Dan Gohmand858e902010-04-17 15:26:15 +00007873SDValue X86TargetLowering::LowerFABS(SDValue Op,
7874 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007875 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007876 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007877 EVT VT = Op.getValueType();
7878 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007879 if (VT.isVector())
7880 EltVT = VT.getVectorElementType();
Chris Lattner4ca829e2012-01-25 06:02:56 +00007881 Constant *C;
Owen Anderson825b72b2009-08-11 20:47:22 +00007882 if (EltVT == MVT::f64) {
Chris Lattner4ca829e2012-01-25 06:02:56 +00007883 C = ConstantVector::getSplat(2,
7884 ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007885 } else {
Chris Lattner4ca829e2012-01-25 06:02:56 +00007886 C = ConstantVector::getSplat(4,
7887 ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007888 }
Evan Cheng1606e8e2009-03-13 07:51:59 +00007889 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007890 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007891 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007892 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007893 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007894}
7895
Dan Gohmand858e902010-04-17 15:26:15 +00007896SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007897 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007898 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007899 EVT VT = Op.getValueType();
7900 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00007901 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
7902 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007903 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00007904 NumElts = VT.getVectorNumElements();
7905 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00007906 Constant *C;
7907 if (EltVT == MVT::f64)
7908 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
7909 else
7910 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
7911 C = ConstantVector::getSplat(NumElts, C);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007912 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007913 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007914 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007915 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007916 if (VT.isVector()) {
Chad Rosiera860b182011-12-15 01:02:25 +00007917 MVT XORVT = VT.getSizeInBits() == 128 ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007918 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00007919 DAG.getNode(ISD::XOR, dl, XORVT,
7920 DAG.getNode(ISD::BITCAST, dl, XORVT,
Dale Johannesenace16102009-02-03 19:33:06 +00007921 Op.getOperand(0)),
Chad Rosiera860b182011-12-15 01:02:25 +00007922 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007923 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007924 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007925 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007926}
7927
Dan Gohmand858e902010-04-17 15:26:15 +00007928SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007929 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007930 SDValue Op0 = Op.getOperand(0);
7931 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007932 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007933 EVT VT = Op.getValueType();
7934 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007935
7936 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007937 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007938 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007939 SrcVT = VT;
7940 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007941 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007942 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007943 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007944 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007945 }
7946
7947 // At this point the operands and the result should have the same
7948 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007949
Evan Cheng68c47cb2007-01-05 07:55:56 +00007950 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00007951 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007952 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007953 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7954 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007955 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007956 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7957 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7958 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7959 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007960 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007961 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007962 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007963 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007964 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007965 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007966 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007967
7968 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007969 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007970 // Op0 is MVT::f32, Op1 is MVT::f64.
7971 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7972 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7973 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007974 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007975 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007976 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007977 }
7978
Evan Cheng73d6cf12007-01-05 21:37:56 +00007979 // Clear first operand sign bit.
7980 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007981 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007982 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7983 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007984 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007985 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7986 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7987 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7988 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007989 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007990 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007991 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007992 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007993 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007994 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007995 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007996
7997 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007998 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007999}
8000
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008001SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8002 SDValue N0 = Op.getOperand(0);
8003 DebugLoc dl = Op.getDebugLoc();
8004 EVT VT = Op.getValueType();
8005
8006 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8007 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8008 DAG.getConstant(1, VT));
8009 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8010}
8011
Dan Gohman076aee32009-03-04 19:44:21 +00008012/// Emit nodes that will be selected as "test Op0,Op0", or something
8013/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008014SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008015 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008016 DebugLoc dl = Op.getDebugLoc();
8017
Dan Gohman31125812009-03-07 01:58:32 +00008018 // CF and OF aren't always set the way we want. Determine which
8019 // of these we need.
8020 bool NeedCF = false;
8021 bool NeedOF = false;
8022 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008023 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008024 case X86::COND_A: case X86::COND_AE:
8025 case X86::COND_B: case X86::COND_BE:
8026 NeedCF = true;
8027 break;
8028 case X86::COND_G: case X86::COND_GE:
8029 case X86::COND_L: case X86::COND_LE:
8030 case X86::COND_O: case X86::COND_NO:
8031 NeedOF = true;
8032 break;
Dan Gohman31125812009-03-07 01:58:32 +00008033 }
8034
Dan Gohman076aee32009-03-04 19:44:21 +00008035 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008036 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8037 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008038 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8039 // Emit a CMP with 0, which is the TEST pattern.
8040 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8041 DAG.getConstant(0, Op.getValueType()));
8042
8043 unsigned Opcode = 0;
8044 unsigned NumOperands = 0;
8045 switch (Op.getNode()->getOpcode()) {
8046 case ISD::ADD:
8047 // Due to an isel shortcoming, be conservative if this add is likely to be
8048 // selected as part of a load-modify-store instruction. When the root node
8049 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8050 // uses of other nodes in the match, such as the ADD in this case. This
8051 // leads to the ADD being left around and reselected, with the result being
8052 // two adds in the output. Alas, even if none our users are stores, that
8053 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8054 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8055 // climbing the DAG back to the root, and it doesn't seem to be worth the
8056 // effort.
8057 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008058 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8059 if (UI->getOpcode() != ISD::CopyToReg &&
8060 UI->getOpcode() != ISD::SETCC &&
8061 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008062 goto default_case;
8063
8064 if (ConstantSDNode *C =
8065 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8066 // An add of one will be selected as an INC.
8067 if (C->getAPIntValue() == 1) {
8068 Opcode = X86ISD::INC;
8069 NumOperands = 1;
8070 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008071 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008072
8073 // An add of negative one (subtract of one) will be selected as a DEC.
8074 if (C->getAPIntValue().isAllOnesValue()) {
8075 Opcode = X86ISD::DEC;
8076 NumOperands = 1;
8077 break;
8078 }
Dan Gohman076aee32009-03-04 19:44:21 +00008079 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008080
8081 // Otherwise use a regular EFLAGS-setting add.
8082 Opcode = X86ISD::ADD;
8083 NumOperands = 2;
8084 break;
8085 case ISD::AND: {
8086 // If the primary and result isn't used, don't bother using X86ISD::AND,
8087 // because a TEST instruction will be better.
8088 bool NonFlagUse = false;
8089 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8090 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8091 SDNode *User = *UI;
8092 unsigned UOpNo = UI.getOperandNo();
8093 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8094 // Look pass truncate.
8095 UOpNo = User->use_begin().getOperandNo();
8096 User = *User->use_begin();
8097 }
8098
8099 if (User->getOpcode() != ISD::BRCOND &&
8100 User->getOpcode() != ISD::SETCC &&
8101 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8102 NonFlagUse = true;
8103 break;
8104 }
Dan Gohman076aee32009-03-04 19:44:21 +00008105 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008106
8107 if (!NonFlagUse)
8108 break;
8109 }
8110 // FALL THROUGH
8111 case ISD::SUB:
8112 case ISD::OR:
8113 case ISD::XOR:
8114 // Due to the ISEL shortcoming noted above, be conservative if this op is
8115 // likely to be selected as part of a load-modify-store instruction.
8116 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8117 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8118 if (UI->getOpcode() == ISD::STORE)
8119 goto default_case;
8120
8121 // Otherwise use a regular EFLAGS-setting instruction.
8122 switch (Op.getNode()->getOpcode()) {
8123 default: llvm_unreachable("unexpected operator!");
8124 case ISD::SUB: Opcode = X86ISD::SUB; break;
8125 case ISD::OR: Opcode = X86ISD::OR; break;
8126 case ISD::XOR: Opcode = X86ISD::XOR; break;
8127 case ISD::AND: Opcode = X86ISD::AND; break;
8128 }
8129
8130 NumOperands = 2;
8131 break;
8132 case X86ISD::ADD:
8133 case X86ISD::SUB:
8134 case X86ISD::INC:
8135 case X86ISD::DEC:
8136 case X86ISD::OR:
8137 case X86ISD::XOR:
8138 case X86ISD::AND:
8139 return SDValue(Op.getNode(), 1);
8140 default:
8141 default_case:
8142 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008143 }
8144
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008145 if (Opcode == 0)
8146 // Emit a CMP with 0, which is the TEST pattern.
8147 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8148 DAG.getConstant(0, Op.getValueType()));
8149
8150 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8151 SmallVector<SDValue, 4> Ops;
8152 for (unsigned i = 0; i != NumOperands; ++i)
8153 Ops.push_back(Op.getOperand(i));
8154
8155 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8156 DAG.ReplaceAllUsesWith(Op, New);
8157 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008158}
8159
8160/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8161/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008162SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008163 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008164 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8165 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008166 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008167
8168 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008169 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008170}
8171
Evan Chengd40d03e2010-01-06 19:38:29 +00008172/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8173/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008174SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8175 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008176 SDValue Op0 = And.getOperand(0);
8177 SDValue Op1 = And.getOperand(1);
8178 if (Op0.getOpcode() == ISD::TRUNCATE)
8179 Op0 = Op0.getOperand(0);
8180 if (Op1.getOpcode() == ISD::TRUNCATE)
8181 Op1 = Op1.getOperand(0);
8182
Evan Chengd40d03e2010-01-06 19:38:29 +00008183 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008184 if (Op1.getOpcode() == ISD::SHL)
8185 std::swap(Op0, Op1);
8186 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008187 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8188 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008189 // If we looked past a truncate, check that it's only truncating away
8190 // known zeros.
8191 unsigned BitWidth = Op0.getValueSizeInBits();
8192 unsigned AndBitWidth = And.getValueSizeInBits();
8193 if (BitWidth > AndBitWidth) {
8194 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8195 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8196 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8197 return SDValue();
8198 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008199 LHS = Op1;
8200 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008201 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008202 } else if (Op1.getOpcode() == ISD::Constant) {
8203 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008204 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008205 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008206
8207 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008208 LHS = AndLHS.getOperand(0);
8209 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008210 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008211
8212 // Use BT if the immediate can't be encoded in a TEST instruction.
8213 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8214 LHS = AndLHS;
8215 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8216 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008217 }
Evan Cheng0488db92007-09-25 01:57:46 +00008218
Evan Chengd40d03e2010-01-06 19:38:29 +00008219 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008220 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008221 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008222 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008223 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008224 // Also promote i16 to i32 for performance / code size reason.
8225 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008226 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008227 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008228
Evan Chengd40d03e2010-01-06 19:38:29 +00008229 // If the operand types disagree, extend the shift amount to match. Since
8230 // BT ignores high bits (like shifts) we can use anyextend.
8231 if (LHS.getValueType() != RHS.getValueType())
8232 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008233
Evan Chengd40d03e2010-01-06 19:38:29 +00008234 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8235 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8236 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8237 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008238 }
8239
Evan Cheng54de3ea2010-01-05 06:52:31 +00008240 return SDValue();
8241}
8242
Dan Gohmand858e902010-04-17 15:26:15 +00008243SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008244
8245 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8246
Evan Cheng54de3ea2010-01-05 06:52:31 +00008247 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8248 SDValue Op0 = Op.getOperand(0);
8249 SDValue Op1 = Op.getOperand(1);
8250 DebugLoc dl = Op.getDebugLoc();
8251 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8252
8253 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008254 // Lower (X & (1 << N)) == 0 to BT(X, N).
8255 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8256 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008257 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008258 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008259 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008260 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8261 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8262 if (NewSetCC.getNode())
8263 return NewSetCC;
8264 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008265
Chris Lattner481eebc2010-12-19 21:23:48 +00008266 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8267 // these.
8268 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008269 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008270 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8271 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008272
Chris Lattner481eebc2010-12-19 21:23:48 +00008273 // If the input is a setcc, then reuse the input setcc or use a new one with
8274 // the inverted condition.
8275 if (Op0.getOpcode() == X86ISD::SETCC) {
8276 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8277 bool Invert = (CC == ISD::SETNE) ^
8278 cast<ConstantSDNode>(Op1)->isNullValue();
8279 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008280
Evan Cheng2c755ba2010-02-27 07:36:59 +00008281 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008282 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8283 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8284 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008285 }
8286
Evan Chenge5b51ac2010-04-17 06:13:15 +00008287 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008288 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008289 if (X86CC == X86::COND_INVALID)
8290 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008291
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008292 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008293 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008294 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008295}
8296
Craig Topper89af15e2011-09-18 08:03:58 +00008297// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008298// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008299static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008300 EVT VT = Op.getValueType();
8301
Duncan Sands28b77e92011-09-06 19:07:46 +00008302 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008303 "Unsupported value type for operation");
8304
8305 int NumElems = VT.getVectorNumElements();
8306 DebugLoc dl = Op.getDebugLoc();
8307 SDValue CC = Op.getOperand(2);
8308 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8309 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8310
8311 // Extract the LHS vectors
8312 SDValue LHS = Op.getOperand(0);
8313 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8314 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8315
8316 // Extract the RHS vectors
8317 SDValue RHS = Op.getOperand(1);
8318 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8319 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8320
8321 // Issue the operation on the smaller types and concatenate the result back
8322 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8323 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8324 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8325 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8326 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8327}
8328
8329
Dan Gohmand858e902010-04-17 15:26:15 +00008330SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008331 SDValue Cond;
8332 SDValue Op0 = Op.getOperand(0);
8333 SDValue Op1 = Op.getOperand(1);
8334 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008335 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008336 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8337 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008338 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008339
8340 if (isFP) {
8341 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008342 EVT EltVT = Op0.getValueType().getVectorElementType();
8343 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8344
Nate Begeman30a0de92008-07-17 16:51:19 +00008345 bool Swap = false;
8346
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008347 // SSE Condition code mapping:
8348 // 0 - EQ
8349 // 1 - LT
8350 // 2 - LE
8351 // 3 - UNORD
8352 // 4 - NEQ
8353 // 5 - NLT
8354 // 6 - NLE
8355 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008356 switch (SetCCOpcode) {
8357 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008358 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008359 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008360 case ISD::SETOGT:
8361 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008362 case ISD::SETLT:
8363 case ISD::SETOLT: SSECC = 1; break;
8364 case ISD::SETOGE:
8365 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008366 case ISD::SETLE:
8367 case ISD::SETOLE: SSECC = 2; break;
8368 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008369 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008370 case ISD::SETNE: SSECC = 4; break;
8371 case ISD::SETULE: Swap = true;
8372 case ISD::SETUGE: SSECC = 5; break;
8373 case ISD::SETULT: Swap = true;
8374 case ISD::SETUGT: SSECC = 6; break;
8375 case ISD::SETO: SSECC = 7; break;
8376 }
8377 if (Swap)
8378 std::swap(Op0, Op1);
8379
Nate Begemanfb8ead02008-07-25 19:05:58 +00008380 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008381 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008382 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008383 SDValue UNORD, EQ;
Craig Topper1906d322012-01-22 23:36:02 +00008384 UNORD = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8385 DAG.getConstant(3, MVT::i8));
8386 EQ = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8387 DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008388 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Craig Topper0a150352011-11-09 08:06:13 +00008389 } else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008390 SDValue ORD, NEQ;
Craig Topper1906d322012-01-22 23:36:02 +00008391 ORD = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8392 DAG.getConstant(7, MVT::i8));
8393 NEQ = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8394 DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008395 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008396 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008397 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008398 }
8399 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008400 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8401 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008402 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008403
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008404 // Break 256-bit integer vector compare into smaller ones.
Craig Topper0a150352011-11-09 08:06:13 +00008405 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008406 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008407
Nate Begeman30a0de92008-07-17 16:51:19 +00008408 // We are handling one of the integer comparisons here. Since SSE only has
8409 // GT and EQ comparisons for integer, swapping operands and multiple
8410 // operations may be required for some comparisons.
Craig Topper67609fd2012-01-22 22:42:16 +00008411 unsigned Opc = 0;
Nate Begeman30a0de92008-07-17 16:51:19 +00008412 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008413
Nate Begeman30a0de92008-07-17 16:51:19 +00008414 switch (SetCCOpcode) {
8415 default: break;
8416 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008417 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008418 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008419 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008420 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008421 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008422 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008423 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008424 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008425 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008426 }
8427 if (Swap)
8428 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008429
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008430 // Check that the operation in question is available (most are plain SSE2,
8431 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper67609fd2012-01-22 22:42:16 +00008432 if (Opc == X86ISD::PCMPGT && VT == MVT::v2i64 && !Subtarget->hasSSE42())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008433 return SDValue();
Craig Topper67609fd2012-01-22 22:42:16 +00008434 if (Opc == X86ISD::PCMPEQ && VT == MVT::v2i64 && !Subtarget->hasSSE41())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008435 return SDValue();
8436
Nate Begeman30a0de92008-07-17 16:51:19 +00008437 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8438 // bits of the inputs before performing those operations.
8439 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008440 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008441 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8442 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008443 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008444 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8445 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008446 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8447 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008448 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008449
Dale Johannesenace16102009-02-03 19:33:06 +00008450 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008451
8452 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008453 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008454 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008455
Nate Begeman30a0de92008-07-17 16:51:19 +00008456 return Result;
8457}
Evan Cheng0488db92007-09-25 01:57:46 +00008458
Evan Cheng370e5342008-12-03 08:38:43 +00008459// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008460static bool isX86LogicalCmp(SDValue Op) {
8461 unsigned Opc = Op.getNode()->getOpcode();
8462 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8463 return true;
8464 if (Op.getResNo() == 1 &&
8465 (Opc == X86ISD::ADD ||
8466 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008467 Opc == X86ISD::ADC ||
8468 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008469 Opc == X86ISD::SMUL ||
8470 Opc == X86ISD::UMUL ||
8471 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008472 Opc == X86ISD::DEC ||
8473 Opc == X86ISD::OR ||
8474 Opc == X86ISD::XOR ||
8475 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008476 return true;
8477
Chris Lattner9637d5b2010-12-05 07:49:54 +00008478 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8479 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008480
Dan Gohman076aee32009-03-04 19:44:21 +00008481 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008482}
8483
Chris Lattnera2b56002010-12-05 01:23:24 +00008484static bool isZero(SDValue V) {
8485 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8486 return C && C->isNullValue();
8487}
8488
Chris Lattner96908b12010-12-05 02:00:51 +00008489static bool isAllOnes(SDValue V) {
8490 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8491 return C && C->isAllOnesValue();
8492}
8493
Dan Gohmand858e902010-04-17 15:26:15 +00008494SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008495 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008496 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008497 SDValue Op1 = Op.getOperand(1);
8498 SDValue Op2 = Op.getOperand(2);
8499 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008500 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008501
Dan Gohman1a492952009-10-20 16:22:37 +00008502 if (Cond.getOpcode() == ISD::SETCC) {
8503 SDValue NewCond = LowerSETCC(Cond, DAG);
8504 if (NewCond.getNode())
8505 Cond = NewCond;
8506 }
Evan Cheng734503b2006-09-11 02:19:56 +00008507
Chris Lattnera2b56002010-12-05 01:23:24 +00008508 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008509 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008510 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008511 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008512 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008513 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8514 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008515 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008516
Chris Lattnera2b56002010-12-05 01:23:24 +00008517 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008518
8519 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008520 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8521 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008522
8523 SDValue CmpOp0 = Cmp.getOperand(0);
8524 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8525 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008526
Chris Lattner96908b12010-12-05 02:00:51 +00008527 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008528 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8529 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008530
Chris Lattner96908b12010-12-05 02:00:51 +00008531 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8532 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008533
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008534 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008535 if (N2C == 0 || !N2C->isNullValue())
8536 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8537 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008538 }
8539 }
8540
Chris Lattnera2b56002010-12-05 01:23:24 +00008541 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008542 if (Cond.getOpcode() == ISD::AND &&
8543 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8544 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008545 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008546 Cond = Cond.getOperand(0);
8547 }
8548
Evan Cheng3f41d662007-10-08 22:16:29 +00008549 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8550 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008551 unsigned CondOpcode = Cond.getOpcode();
8552 if (CondOpcode == X86ISD::SETCC ||
8553 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008554 CC = Cond.getOperand(0);
8555
Dan Gohman475871a2008-07-27 21:46:04 +00008556 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008557 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008558 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008559
Evan Cheng3f41d662007-10-08 22:16:29 +00008560 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008561 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008562 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008563 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008564
Chris Lattnerd1980a52009-03-12 06:52:53 +00008565 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8566 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008567 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008568 addTest = false;
8569 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008570 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8571 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8572 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8573 Cond.getOperand(0).getValueType() != MVT::i8)) {
8574 SDValue LHS = Cond.getOperand(0);
8575 SDValue RHS = Cond.getOperand(1);
8576 unsigned X86Opcode;
8577 unsigned X86Cond;
8578 SDVTList VTs;
8579 switch (CondOpcode) {
8580 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8581 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8582 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8583 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8584 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8585 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8586 default: llvm_unreachable("unexpected overflowing operator");
8587 }
8588 if (CondOpcode == ISD::UMULO)
8589 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8590 MVT::i32);
8591 else
8592 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8593
8594 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8595
8596 if (CondOpcode == ISD::UMULO)
8597 Cond = X86Op.getValue(2);
8598 else
8599 Cond = X86Op.getValue(1);
8600
8601 CC = DAG.getConstant(X86Cond, MVT::i8);
8602 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008603 }
8604
8605 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008606 // Look pass the truncate.
8607 if (Cond.getOpcode() == ISD::TRUNCATE)
8608 Cond = Cond.getOperand(0);
8609
8610 // We know the result of AND is compared against zero. Try to match
8611 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008612 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008613 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008614 if (NewSetCC.getNode()) {
8615 CC = NewSetCC.getOperand(0);
8616 Cond = NewSetCC.getOperand(1);
8617 addTest = false;
8618 }
8619 }
8620 }
8621
8622 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008623 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008624 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008625 }
8626
Benjamin Kramere915ff32010-12-22 23:09:28 +00008627 // a < b ? -1 : 0 -> RES = ~setcc_carry
8628 // a < b ? 0 : -1 -> RES = setcc_carry
8629 // a >= b ? -1 : 0 -> RES = setcc_carry
8630 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8631 if (Cond.getOpcode() == X86ISD::CMP) {
8632 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8633
8634 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8635 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8636 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8637 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8638 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8639 return DAG.getNOT(DL, Res, Res.getValueType());
8640 return Res;
8641 }
8642 }
8643
Evan Cheng0488db92007-09-25 01:57:46 +00008644 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8645 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008646 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008647 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008648 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008649}
8650
Evan Cheng370e5342008-12-03 08:38:43 +00008651// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8652// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8653// from the AND / OR.
8654static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8655 Opc = Op.getOpcode();
8656 if (Opc != ISD::OR && Opc != ISD::AND)
8657 return false;
8658 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8659 Op.getOperand(0).hasOneUse() &&
8660 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8661 Op.getOperand(1).hasOneUse());
8662}
8663
Evan Cheng961d6d42009-02-02 08:19:07 +00008664// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8665// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008666static bool isXor1OfSetCC(SDValue Op) {
8667 if (Op.getOpcode() != ISD::XOR)
8668 return false;
8669 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8670 if (N1C && N1C->getAPIntValue() == 1) {
8671 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8672 Op.getOperand(0).hasOneUse();
8673 }
8674 return false;
8675}
8676
Dan Gohmand858e902010-04-17 15:26:15 +00008677SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008678 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008679 SDValue Chain = Op.getOperand(0);
8680 SDValue Cond = Op.getOperand(1);
8681 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008682 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008683 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008684 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008685
Dan Gohman1a492952009-10-20 16:22:37 +00008686 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008687 // Check for setcc([su]{add,sub,mul}o == 0).
8688 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8689 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8690 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8691 Cond.getOperand(0).getResNo() == 1 &&
8692 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
8693 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
8694 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
8695 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
8696 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
8697 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
8698 Inverted = true;
8699 Cond = Cond.getOperand(0);
8700 } else {
8701 SDValue NewCond = LowerSETCC(Cond, DAG);
8702 if (NewCond.getNode())
8703 Cond = NewCond;
8704 }
Dan Gohman1a492952009-10-20 16:22:37 +00008705 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008706#if 0
8707 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008708 else if (Cond.getOpcode() == X86ISD::ADD ||
8709 Cond.getOpcode() == X86ISD::SUB ||
8710 Cond.getOpcode() == X86ISD::SMUL ||
8711 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008712 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008713#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008714
Evan Chengad9c0a32009-12-15 00:53:42 +00008715 // Look pass (and (setcc_carry (cmp ...)), 1).
8716 if (Cond.getOpcode() == ISD::AND &&
8717 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8718 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008719 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008720 Cond = Cond.getOperand(0);
8721 }
8722
Evan Cheng3f41d662007-10-08 22:16:29 +00008723 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8724 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008725 unsigned CondOpcode = Cond.getOpcode();
8726 if (CondOpcode == X86ISD::SETCC ||
8727 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008728 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008729
Dan Gohman475871a2008-07-27 21:46:04 +00008730 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008731 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008732 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008733 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008734 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008735 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008736 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008737 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008738 default: break;
8739 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008740 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008741 // These can only come from an arithmetic instruction with overflow,
8742 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008743 Cond = Cond.getNode()->getOperand(1);
8744 addTest = false;
8745 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008746 }
Evan Cheng0488db92007-09-25 01:57:46 +00008747 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008748 }
8749 CondOpcode = Cond.getOpcode();
8750 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8751 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8752 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8753 Cond.getOperand(0).getValueType() != MVT::i8)) {
8754 SDValue LHS = Cond.getOperand(0);
8755 SDValue RHS = Cond.getOperand(1);
8756 unsigned X86Opcode;
8757 unsigned X86Cond;
8758 SDVTList VTs;
8759 switch (CondOpcode) {
8760 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8761 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8762 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8763 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8764 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8765 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8766 default: llvm_unreachable("unexpected overflowing operator");
8767 }
8768 if (Inverted)
8769 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
8770 if (CondOpcode == ISD::UMULO)
8771 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8772 MVT::i32);
8773 else
8774 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8775
8776 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
8777
8778 if (CondOpcode == ISD::UMULO)
8779 Cond = X86Op.getValue(2);
8780 else
8781 Cond = X86Op.getValue(1);
8782
8783 CC = DAG.getConstant(X86Cond, MVT::i8);
8784 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00008785 } else {
8786 unsigned CondOpc;
8787 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8788 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008789 if (CondOpc == ISD::OR) {
8790 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8791 // two branches instead of an explicit OR instruction with a
8792 // separate test.
8793 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008794 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008795 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008796 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008797 Chain, Dest, CC, Cmp);
8798 CC = Cond.getOperand(1).getOperand(0);
8799 Cond = Cmp;
8800 addTest = false;
8801 }
8802 } else { // ISD::AND
8803 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8804 // two branches instead of an explicit AND instruction with a
8805 // separate test. However, we only do this if this block doesn't
8806 // have a fall-through edge, because this requires an explicit
8807 // jmp when the condition is false.
8808 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008809 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008810 Op.getNode()->hasOneUse()) {
8811 X86::CondCode CCode =
8812 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8813 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008814 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008815 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008816 // Look for an unconditional branch following this conditional branch.
8817 // We need this because we need to reverse the successors in order
8818 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008819 if (User->getOpcode() == ISD::BR) {
8820 SDValue FalseBB = User->getOperand(1);
8821 SDNode *NewBR =
8822 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008823 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008824 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008825 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008826
Dale Johannesene4d209d2009-02-03 20:21:25 +00008827 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008828 Chain, Dest, CC, Cmp);
8829 X86::CondCode CCode =
8830 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8831 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008832 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008833 Cond = Cmp;
8834 addTest = false;
8835 }
8836 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008837 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008838 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8839 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8840 // It should be transformed during dag combiner except when the condition
8841 // is set by a arithmetics with overflow node.
8842 X86::CondCode CCode =
8843 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8844 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008845 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008846 Cond = Cond.getOperand(0).getOperand(1);
8847 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00008848 } else if (Cond.getOpcode() == ISD::SETCC &&
8849 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
8850 // For FCMP_OEQ, we can emit
8851 // two branches instead of an explicit AND instruction with a
8852 // separate test. However, we only do this if this block doesn't
8853 // have a fall-through edge, because this requires an explicit
8854 // jmp when the condition is false.
8855 if (Op.getNode()->hasOneUse()) {
8856 SDNode *User = *Op.getNode()->use_begin();
8857 // Look for an unconditional branch following this conditional branch.
8858 // We need this because we need to reverse the successors in order
8859 // to implement FCMP_OEQ.
8860 if (User->getOpcode() == ISD::BR) {
8861 SDValue FalseBB = User->getOperand(1);
8862 SDNode *NewBR =
8863 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8864 assert(NewBR == User);
8865 (void)NewBR;
8866 Dest = FalseBB;
8867
8868 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
8869 Cond.getOperand(0), Cond.getOperand(1));
8870 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8871 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8872 Chain, Dest, CC, Cmp);
8873 CC = DAG.getConstant(X86::COND_P, MVT::i8);
8874 Cond = Cmp;
8875 addTest = false;
8876 }
8877 }
8878 } else if (Cond.getOpcode() == ISD::SETCC &&
8879 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
8880 // For FCMP_UNE, we can emit
8881 // two branches instead of an explicit AND instruction with a
8882 // separate test. However, we only do this if this block doesn't
8883 // have a fall-through edge, because this requires an explicit
8884 // jmp when the condition is false.
8885 if (Op.getNode()->hasOneUse()) {
8886 SDNode *User = *Op.getNode()->use_begin();
8887 // Look for an unconditional branch following this conditional branch.
8888 // We need this because we need to reverse the successors in order
8889 // to implement FCMP_UNE.
8890 if (User->getOpcode() == ISD::BR) {
8891 SDValue FalseBB = User->getOperand(1);
8892 SDNode *NewBR =
8893 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8894 assert(NewBR == User);
8895 (void)NewBR;
8896
8897 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
8898 Cond.getOperand(0), Cond.getOperand(1));
8899 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8900 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8901 Chain, Dest, CC, Cmp);
8902 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
8903 Cond = Cmp;
8904 addTest = false;
8905 Dest = FalseBB;
8906 }
8907 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008908 }
Evan Cheng0488db92007-09-25 01:57:46 +00008909 }
8910
8911 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008912 // Look pass the truncate.
8913 if (Cond.getOpcode() == ISD::TRUNCATE)
8914 Cond = Cond.getOperand(0);
8915
8916 // We know the result of AND is compared against zero. Try to match
8917 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008918 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008919 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8920 if (NewSetCC.getNode()) {
8921 CC = NewSetCC.getOperand(0);
8922 Cond = NewSetCC.getOperand(1);
8923 addTest = false;
8924 }
8925 }
8926 }
8927
8928 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008929 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008930 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008931 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008932 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008933 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008934}
8935
Anton Korobeynikove060b532007-04-17 19:34:00 +00008936
8937// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8938// Calls to _alloca is needed to probe the stack when allocating more than 4k
8939// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8940// that the guard pages used by the OS virtual memory manager are allocated in
8941// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008942SDValue
8943X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008944 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008945 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008946 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008947 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008948 "are being used");
8949 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008950 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008951
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008952 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008953 SDValue Chain = Op.getOperand(0);
8954 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008955 // FIXME: Ensure alignment here
8956
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008957 bool Is64Bit = Subtarget->is64Bit();
8958 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008959
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008960 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008961 MachineFunction &MF = DAG.getMachineFunction();
8962 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008963
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008964 if (Is64Bit) {
8965 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008966 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008967 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008968
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008969 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8970 I != E; I++)
8971 if (I->hasNestAttr())
8972 report_fatal_error("Cannot use segmented stacks with functions that "
8973 "have nested arguments.");
8974 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008975
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008976 const TargetRegisterClass *AddrRegClass =
8977 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8978 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8979 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8980 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8981 DAG.getRegister(Vreg, SPTy));
8982 SDValue Ops1[2] = { Value, Chain };
8983 return DAG.getMergeValues(Ops1, 2, dl);
8984 } else {
8985 SDValue Flag;
8986 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008987
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008988 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8989 Flag = Chain.getValue(1);
8990 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008991
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008992 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
8993 Flag = Chain.getValue(1);
8994
8995 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
8996
8997 SDValue Ops1[2] = { Chain.getValue(0), Chain };
8998 return DAG.getMergeValues(Ops1, 2, dl);
8999 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009000}
9001
Dan Gohmand858e902010-04-17 15:26:15 +00009002SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009003 MachineFunction &MF = DAG.getMachineFunction();
9004 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9005
Dan Gohman69de1932008-02-06 22:27:42 +00009006 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009007 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009008
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009009 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009010 // vastart just stores the address of the VarArgsFrameIndex slot into the
9011 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009012 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9013 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009014 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9015 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009016 }
9017
9018 // __va_list_tag:
9019 // gp_offset (0 - 6 * 8)
9020 // fp_offset (48 - 48 + 8 * 16)
9021 // overflow_arg_area (point to parameters coming in memory).
9022 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009023 SmallVector<SDValue, 8> MemOps;
9024 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009025 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009026 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009027 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9028 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009029 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009030 MemOps.push_back(Store);
9031
9032 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009033 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009034 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009035 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009036 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9037 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009038 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009039 MemOps.push_back(Store);
9040
9041 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009042 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009043 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009044 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9045 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009046 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9047 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009048 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009049 MemOps.push_back(Store);
9050
9051 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009052 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009053 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009054 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9055 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009056 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9057 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009058 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009059 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009060 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009061}
9062
Dan Gohmand858e902010-04-17 15:26:15 +00009063SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009064 assert(Subtarget->is64Bit() &&
9065 "LowerVAARG only handles 64-bit va_arg!");
9066 assert((Subtarget->isTargetLinux() ||
9067 Subtarget->isTargetDarwin()) &&
9068 "Unhandled target in LowerVAARG");
9069 assert(Op.getNode()->getNumOperands() == 4);
9070 SDValue Chain = Op.getOperand(0);
9071 SDValue SrcPtr = Op.getOperand(1);
9072 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9073 unsigned Align = Op.getConstantOperandVal(3);
9074 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009075
Dan Gohman320afb82010-10-12 18:00:49 +00009076 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009077 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009078 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9079 uint8_t ArgMode;
9080
9081 // Decide which area this value should be read from.
9082 // TODO: Implement the AMD64 ABI in its entirety. This simple
9083 // selection mechanism works only for the basic types.
9084 if (ArgVT == MVT::f80) {
9085 llvm_unreachable("va_arg for f80 not yet implemented");
9086 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9087 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9088 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9089 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9090 } else {
9091 llvm_unreachable("Unhandled argument type in LowerVAARG");
9092 }
9093
9094 if (ArgMode == 2) {
9095 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009096 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009097 !(DAG.getMachineFunction()
9098 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009099 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009100 }
9101
9102 // Insert VAARG_64 node into the DAG
9103 // VAARG_64 returns two values: Variable Argument Address, Chain
9104 SmallVector<SDValue, 11> InstOps;
9105 InstOps.push_back(Chain);
9106 InstOps.push_back(SrcPtr);
9107 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9108 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9109 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9110 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9111 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9112 VTs, &InstOps[0], InstOps.size(),
9113 MVT::i64,
9114 MachinePointerInfo(SV),
9115 /*Align=*/0,
9116 /*Volatile=*/false,
9117 /*ReadMem=*/true,
9118 /*WriteMem=*/true);
9119 Chain = VAARG.getValue(1);
9120
9121 // Load the next argument and return it
9122 return DAG.getLoad(ArgVT, dl,
9123 Chain,
9124 VAARG,
9125 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009126 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009127}
9128
Dan Gohmand858e902010-04-17 15:26:15 +00009129SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009130 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009131 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009132 SDValue Chain = Op.getOperand(0);
9133 SDValue DstPtr = Op.getOperand(1);
9134 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009135 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9136 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009137 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009138
Chris Lattnere72f2022010-09-21 05:40:29 +00009139 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009140 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009141 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009142 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009143}
9144
Craig Topper80e46362012-01-23 06:16:53 +00009145// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9146// may or may not be a constant. Takes immediate version of shift as input.
9147static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9148 SDValue SrcOp, SDValue ShAmt,
9149 SelectionDAG &DAG) {
9150 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9151
9152 if (isa<ConstantSDNode>(ShAmt)) {
9153 switch (Opc) {
9154 default: llvm_unreachable("Unknown target vector shift node");
9155 case X86ISD::VSHLI:
9156 case X86ISD::VSRLI:
9157 case X86ISD::VSRAI:
9158 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9159 }
9160 }
9161
9162 // Change opcode to non-immediate version
9163 switch (Opc) {
9164 default: llvm_unreachable("Unknown target vector shift node");
9165 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9166 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9167 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9168 }
9169
9170 // Need to build a vector containing shift amount
9171 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9172 SDValue ShOps[4];
9173 ShOps[0] = ShAmt;
9174 ShOps[1] = DAG.getConstant(0, MVT::i32);
9175 ShOps[2] = DAG.getUNDEF(MVT::i32);
9176 ShOps[3] = DAG.getUNDEF(MVT::i32);
9177 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
9178 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
9179 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9180}
9181
Dan Gohman475871a2008-07-27 21:46:04 +00009182SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009183X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009184 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009185 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009186 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009187 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009188 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009189 case Intrinsic::x86_sse_comieq_ss:
9190 case Intrinsic::x86_sse_comilt_ss:
9191 case Intrinsic::x86_sse_comile_ss:
9192 case Intrinsic::x86_sse_comigt_ss:
9193 case Intrinsic::x86_sse_comige_ss:
9194 case Intrinsic::x86_sse_comineq_ss:
9195 case Intrinsic::x86_sse_ucomieq_ss:
9196 case Intrinsic::x86_sse_ucomilt_ss:
9197 case Intrinsic::x86_sse_ucomile_ss:
9198 case Intrinsic::x86_sse_ucomigt_ss:
9199 case Intrinsic::x86_sse_ucomige_ss:
9200 case Intrinsic::x86_sse_ucomineq_ss:
9201 case Intrinsic::x86_sse2_comieq_sd:
9202 case Intrinsic::x86_sse2_comilt_sd:
9203 case Intrinsic::x86_sse2_comile_sd:
9204 case Intrinsic::x86_sse2_comigt_sd:
9205 case Intrinsic::x86_sse2_comige_sd:
9206 case Intrinsic::x86_sse2_comineq_sd:
9207 case Intrinsic::x86_sse2_ucomieq_sd:
9208 case Intrinsic::x86_sse2_ucomilt_sd:
9209 case Intrinsic::x86_sse2_ucomile_sd:
9210 case Intrinsic::x86_sse2_ucomigt_sd:
9211 case Intrinsic::x86_sse2_ucomige_sd:
9212 case Intrinsic::x86_sse2_ucomineq_sd: {
9213 unsigned Opc = 0;
9214 ISD::CondCode CC = ISD::SETCC_INVALID;
9215 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +00009216 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009217 case Intrinsic::x86_sse_comieq_ss:
9218 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009219 Opc = X86ISD::COMI;
9220 CC = ISD::SETEQ;
9221 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009222 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009223 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009224 Opc = X86ISD::COMI;
9225 CC = ISD::SETLT;
9226 break;
9227 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009228 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009229 Opc = X86ISD::COMI;
9230 CC = ISD::SETLE;
9231 break;
9232 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009233 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009234 Opc = X86ISD::COMI;
9235 CC = ISD::SETGT;
9236 break;
9237 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009238 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009239 Opc = X86ISD::COMI;
9240 CC = ISD::SETGE;
9241 break;
9242 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009243 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009244 Opc = X86ISD::COMI;
9245 CC = ISD::SETNE;
9246 break;
9247 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009248 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009249 Opc = X86ISD::UCOMI;
9250 CC = ISD::SETEQ;
9251 break;
9252 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009253 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009254 Opc = X86ISD::UCOMI;
9255 CC = ISD::SETLT;
9256 break;
9257 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009258 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009259 Opc = X86ISD::UCOMI;
9260 CC = ISD::SETLE;
9261 break;
9262 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009263 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009264 Opc = X86ISD::UCOMI;
9265 CC = ISD::SETGT;
9266 break;
9267 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009268 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009269 Opc = X86ISD::UCOMI;
9270 CC = ISD::SETGE;
9271 break;
9272 case Intrinsic::x86_sse_ucomineq_ss:
9273 case Intrinsic::x86_sse2_ucomineq_sd:
9274 Opc = X86ISD::UCOMI;
9275 CC = ISD::SETNE;
9276 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009277 }
Evan Cheng734503b2006-09-11 02:19:56 +00009278
Dan Gohman475871a2008-07-27 21:46:04 +00009279 SDValue LHS = Op.getOperand(1);
9280 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009281 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009282 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009283 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9284 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9285 DAG.getConstant(X86CC, MVT::i8), Cond);
9286 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009287 }
Craig Topper86c7c582012-01-30 01:10:15 +00009288 // XOP comparison intrinsics
9289 case Intrinsic::x86_xop_vpcomltb:
9290 case Intrinsic::x86_xop_vpcomltw:
9291 case Intrinsic::x86_xop_vpcomltd:
9292 case Intrinsic::x86_xop_vpcomltq:
9293 case Intrinsic::x86_xop_vpcomltub:
9294 case Intrinsic::x86_xop_vpcomltuw:
9295 case Intrinsic::x86_xop_vpcomltud:
9296 case Intrinsic::x86_xop_vpcomltuq:
9297 case Intrinsic::x86_xop_vpcomleb:
9298 case Intrinsic::x86_xop_vpcomlew:
9299 case Intrinsic::x86_xop_vpcomled:
9300 case Intrinsic::x86_xop_vpcomleq:
9301 case Intrinsic::x86_xop_vpcomleub:
9302 case Intrinsic::x86_xop_vpcomleuw:
9303 case Intrinsic::x86_xop_vpcomleud:
9304 case Intrinsic::x86_xop_vpcomleuq:
9305 case Intrinsic::x86_xop_vpcomgtb:
9306 case Intrinsic::x86_xop_vpcomgtw:
9307 case Intrinsic::x86_xop_vpcomgtd:
9308 case Intrinsic::x86_xop_vpcomgtq:
9309 case Intrinsic::x86_xop_vpcomgtub:
9310 case Intrinsic::x86_xop_vpcomgtuw:
9311 case Intrinsic::x86_xop_vpcomgtud:
9312 case Intrinsic::x86_xop_vpcomgtuq:
9313 case Intrinsic::x86_xop_vpcomgeb:
9314 case Intrinsic::x86_xop_vpcomgew:
9315 case Intrinsic::x86_xop_vpcomged:
9316 case Intrinsic::x86_xop_vpcomgeq:
9317 case Intrinsic::x86_xop_vpcomgeub:
9318 case Intrinsic::x86_xop_vpcomgeuw:
9319 case Intrinsic::x86_xop_vpcomgeud:
9320 case Intrinsic::x86_xop_vpcomgeuq:
9321 case Intrinsic::x86_xop_vpcomeqb:
9322 case Intrinsic::x86_xop_vpcomeqw:
9323 case Intrinsic::x86_xop_vpcomeqd:
9324 case Intrinsic::x86_xop_vpcomeqq:
9325 case Intrinsic::x86_xop_vpcomequb:
9326 case Intrinsic::x86_xop_vpcomequw:
9327 case Intrinsic::x86_xop_vpcomequd:
9328 case Intrinsic::x86_xop_vpcomequq:
9329 case Intrinsic::x86_xop_vpcomneb:
9330 case Intrinsic::x86_xop_vpcomnew:
9331 case Intrinsic::x86_xop_vpcomned:
9332 case Intrinsic::x86_xop_vpcomneq:
9333 case Intrinsic::x86_xop_vpcomneub:
9334 case Intrinsic::x86_xop_vpcomneuw:
9335 case Intrinsic::x86_xop_vpcomneud:
9336 case Intrinsic::x86_xop_vpcomneuq:
9337 case Intrinsic::x86_xop_vpcomfalseb:
9338 case Intrinsic::x86_xop_vpcomfalsew:
9339 case Intrinsic::x86_xop_vpcomfalsed:
9340 case Intrinsic::x86_xop_vpcomfalseq:
9341 case Intrinsic::x86_xop_vpcomfalseub:
9342 case Intrinsic::x86_xop_vpcomfalseuw:
9343 case Intrinsic::x86_xop_vpcomfalseud:
9344 case Intrinsic::x86_xop_vpcomfalseuq:
9345 case Intrinsic::x86_xop_vpcomtrueb:
9346 case Intrinsic::x86_xop_vpcomtruew:
9347 case Intrinsic::x86_xop_vpcomtrued:
9348 case Intrinsic::x86_xop_vpcomtrueq:
9349 case Intrinsic::x86_xop_vpcomtrueub:
9350 case Intrinsic::x86_xop_vpcomtrueuw:
9351 case Intrinsic::x86_xop_vpcomtrueud:
9352 case Intrinsic::x86_xop_vpcomtrueuq: {
9353 unsigned CC = 0;
9354 unsigned Opc = 0;
9355
9356 switch (IntNo) {
9357 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9358 case Intrinsic::x86_xop_vpcomltb:
9359 case Intrinsic::x86_xop_vpcomltw:
9360 case Intrinsic::x86_xop_vpcomltd:
9361 case Intrinsic::x86_xop_vpcomltq:
9362 CC = 0;
9363 Opc = X86ISD::VPCOM;
9364 break;
9365 case Intrinsic::x86_xop_vpcomltub:
9366 case Intrinsic::x86_xop_vpcomltuw:
9367 case Intrinsic::x86_xop_vpcomltud:
9368 case Intrinsic::x86_xop_vpcomltuq:
9369 CC = 0;
9370 Opc = X86ISD::VPCOMU;
9371 break;
9372 case Intrinsic::x86_xop_vpcomleb:
9373 case Intrinsic::x86_xop_vpcomlew:
9374 case Intrinsic::x86_xop_vpcomled:
9375 case Intrinsic::x86_xop_vpcomleq:
9376 CC = 1;
9377 Opc = X86ISD::VPCOM;
9378 break;
9379 case Intrinsic::x86_xop_vpcomleub:
9380 case Intrinsic::x86_xop_vpcomleuw:
9381 case Intrinsic::x86_xop_vpcomleud:
9382 case Intrinsic::x86_xop_vpcomleuq:
9383 CC = 1;
9384 Opc = X86ISD::VPCOMU;
9385 break;
9386 case Intrinsic::x86_xop_vpcomgtb:
9387 case Intrinsic::x86_xop_vpcomgtw:
9388 case Intrinsic::x86_xop_vpcomgtd:
9389 case Intrinsic::x86_xop_vpcomgtq:
9390 CC = 2;
9391 Opc = X86ISD::VPCOM;
9392 break;
9393 case Intrinsic::x86_xop_vpcomgtub:
9394 case Intrinsic::x86_xop_vpcomgtuw:
9395 case Intrinsic::x86_xop_vpcomgtud:
9396 case Intrinsic::x86_xop_vpcomgtuq:
9397 CC = 2;
9398 Opc = X86ISD::VPCOMU;
9399 break;
9400 case Intrinsic::x86_xop_vpcomgeb:
9401 case Intrinsic::x86_xop_vpcomgew:
9402 case Intrinsic::x86_xop_vpcomged:
9403 case Intrinsic::x86_xop_vpcomgeq:
9404 CC = 3;
9405 Opc = X86ISD::VPCOM;
9406 break;
9407 case Intrinsic::x86_xop_vpcomgeub:
9408 case Intrinsic::x86_xop_vpcomgeuw:
9409 case Intrinsic::x86_xop_vpcomgeud:
9410 case Intrinsic::x86_xop_vpcomgeuq:
9411 CC = 3;
9412 Opc = X86ISD::VPCOMU;
9413 break;
9414 case Intrinsic::x86_xop_vpcomeqb:
9415 case Intrinsic::x86_xop_vpcomeqw:
9416 case Intrinsic::x86_xop_vpcomeqd:
9417 case Intrinsic::x86_xop_vpcomeqq:
9418 CC = 4;
9419 Opc = X86ISD::VPCOM;
9420 break;
9421 case Intrinsic::x86_xop_vpcomequb:
9422 case Intrinsic::x86_xop_vpcomequw:
9423 case Intrinsic::x86_xop_vpcomequd:
9424 case Intrinsic::x86_xop_vpcomequq:
9425 CC = 4;
9426 Opc = X86ISD::VPCOMU;
9427 break;
9428 case Intrinsic::x86_xop_vpcomneb:
9429 case Intrinsic::x86_xop_vpcomnew:
9430 case Intrinsic::x86_xop_vpcomned:
9431 case Intrinsic::x86_xop_vpcomneq:
9432 CC = 5;
9433 Opc = X86ISD::VPCOM;
9434 break;
9435 case Intrinsic::x86_xop_vpcomneub:
9436 case Intrinsic::x86_xop_vpcomneuw:
9437 case Intrinsic::x86_xop_vpcomneud:
9438 case Intrinsic::x86_xop_vpcomneuq:
9439 CC = 5;
9440 Opc = X86ISD::VPCOMU;
9441 break;
9442 case Intrinsic::x86_xop_vpcomfalseb:
9443 case Intrinsic::x86_xop_vpcomfalsew:
9444 case Intrinsic::x86_xop_vpcomfalsed:
9445 case Intrinsic::x86_xop_vpcomfalseq:
9446 CC = 6;
9447 Opc = X86ISD::VPCOM;
9448 break;
9449 case Intrinsic::x86_xop_vpcomfalseub:
9450 case Intrinsic::x86_xop_vpcomfalseuw:
9451 case Intrinsic::x86_xop_vpcomfalseud:
9452 case Intrinsic::x86_xop_vpcomfalseuq:
9453 CC = 6;
9454 Opc = X86ISD::VPCOMU;
9455 break;
9456 case Intrinsic::x86_xop_vpcomtrueb:
9457 case Intrinsic::x86_xop_vpcomtruew:
9458 case Intrinsic::x86_xop_vpcomtrued:
9459 case Intrinsic::x86_xop_vpcomtrueq:
9460 CC = 7;
9461 Opc = X86ISD::VPCOM;
9462 break;
9463 case Intrinsic::x86_xop_vpcomtrueub:
9464 case Intrinsic::x86_xop_vpcomtrueuw:
9465 case Intrinsic::x86_xop_vpcomtrueud:
9466 case Intrinsic::x86_xop_vpcomtrueuq:
9467 CC = 7;
9468 Opc = X86ISD::VPCOMU;
9469 break;
9470 }
9471
9472 SDValue LHS = Op.getOperand(1);
9473 SDValue RHS = Op.getOperand(2);
9474 return DAG.getNode(Opc, dl, Op.getValueType(), LHS, RHS,
9475 DAG.getConstant(CC, MVT::i8));
9476 }
9477
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009478 // Arithmetic intrinsics.
9479 case Intrinsic::x86_sse3_hadd_ps:
9480 case Intrinsic::x86_sse3_hadd_pd:
9481 case Intrinsic::x86_avx_hadd_ps_256:
9482 case Intrinsic::x86_avx_hadd_pd_256:
9483 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9484 Op.getOperand(1), Op.getOperand(2));
9485 case Intrinsic::x86_sse3_hsub_ps:
9486 case Intrinsic::x86_sse3_hsub_pd:
9487 case Intrinsic::x86_avx_hsub_ps_256:
9488 case Intrinsic::x86_avx_hsub_pd_256:
9489 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9490 Op.getOperand(1), Op.getOperand(2));
Craig Topper4bb3f342012-01-25 05:37:32 +00009491 case Intrinsic::x86_ssse3_phadd_w_128:
9492 case Intrinsic::x86_ssse3_phadd_d_128:
9493 case Intrinsic::x86_avx2_phadd_w:
9494 case Intrinsic::x86_avx2_phadd_d:
9495 return DAG.getNode(X86ISD::HADD, dl, Op.getValueType(),
9496 Op.getOperand(1), Op.getOperand(2));
9497 case Intrinsic::x86_ssse3_phsub_w_128:
9498 case Intrinsic::x86_ssse3_phsub_d_128:
9499 case Intrinsic::x86_avx2_phsub_w:
9500 case Intrinsic::x86_avx2_phsub_d:
9501 return DAG.getNode(X86ISD::HSUB, dl, Op.getValueType(),
9502 Op.getOperand(1), Op.getOperand(2));
Craig Topper98fc7292011-11-19 17:46:46 +00009503 case Intrinsic::x86_avx2_psllv_d:
9504 case Intrinsic::x86_avx2_psllv_q:
9505 case Intrinsic::x86_avx2_psllv_d_256:
9506 case Intrinsic::x86_avx2_psllv_q_256:
9507 return DAG.getNode(ISD::SHL, dl, Op.getValueType(),
9508 Op.getOperand(1), Op.getOperand(2));
9509 case Intrinsic::x86_avx2_psrlv_d:
9510 case Intrinsic::x86_avx2_psrlv_q:
9511 case Intrinsic::x86_avx2_psrlv_d_256:
9512 case Intrinsic::x86_avx2_psrlv_q_256:
9513 return DAG.getNode(ISD::SRL, dl, Op.getValueType(),
9514 Op.getOperand(1), Op.getOperand(2));
9515 case Intrinsic::x86_avx2_psrav_d:
9516 case Intrinsic::x86_avx2_psrav_d_256:
9517 return DAG.getNode(ISD::SRA, dl, Op.getValueType(),
9518 Op.getOperand(1), Op.getOperand(2));
Craig Topper7925e252012-01-23 08:18:28 +00009519 case Intrinsic::x86_sse2_pcmpeq_b:
9520 case Intrinsic::x86_sse2_pcmpeq_w:
9521 case Intrinsic::x86_sse2_pcmpeq_d:
9522 case Intrinsic::x86_sse41_pcmpeqq:
9523 case Intrinsic::x86_avx2_pcmpeq_b:
9524 case Intrinsic::x86_avx2_pcmpeq_w:
9525 case Intrinsic::x86_avx2_pcmpeq_d:
9526 case Intrinsic::x86_avx2_pcmpeq_q:
9527 return DAG.getNode(X86ISD::PCMPEQ, dl, Op.getValueType(),
9528 Op.getOperand(1), Op.getOperand(2));
9529 case Intrinsic::x86_sse2_pcmpgt_b:
9530 case Intrinsic::x86_sse2_pcmpgt_w:
9531 case Intrinsic::x86_sse2_pcmpgt_d:
9532 case Intrinsic::x86_sse42_pcmpgtq:
9533 case Intrinsic::x86_avx2_pcmpgt_b:
9534 case Intrinsic::x86_avx2_pcmpgt_w:
9535 case Intrinsic::x86_avx2_pcmpgt_d:
9536 case Intrinsic::x86_avx2_pcmpgt_q:
9537 return DAG.getNode(X86ISD::PCMPGT, dl, Op.getValueType(),
9538 Op.getOperand(1), Op.getOperand(2));
Craig Topper969ba282012-01-25 06:43:11 +00009539 case Intrinsic::x86_ssse3_pshuf_b_128:
9540 case Intrinsic::x86_avx2_pshuf_b:
9541 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
9542 Op.getOperand(1), Op.getOperand(2));
9543 case Intrinsic::x86_ssse3_psign_b_128:
9544 case Intrinsic::x86_ssse3_psign_w_128:
9545 case Intrinsic::x86_ssse3_psign_d_128:
9546 case Intrinsic::x86_avx2_psign_b:
9547 case Intrinsic::x86_avx2_psign_w:
9548 case Intrinsic::x86_avx2_psign_d:
9549 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
9550 Op.getOperand(1), Op.getOperand(2));
Craig Toppere566cd02012-01-26 07:18:03 +00009551 case Intrinsic::x86_sse41_insertps:
9552 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
9553 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
9554 case Intrinsic::x86_avx_vperm2f128_ps_256:
9555 case Intrinsic::x86_avx_vperm2f128_pd_256:
9556 case Intrinsic::x86_avx_vperm2f128_si_256:
9557 case Intrinsic::x86_avx2_vperm2i128:
9558 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
9559 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper98fc7292011-11-19 17:46:46 +00009560
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009561 // ptest and testp intrinsics. The intrinsic these come from are designed to
9562 // return an integer value, not just an instruction so lower it to the ptest
9563 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009564 case Intrinsic::x86_sse41_ptestz:
9565 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009566 case Intrinsic::x86_sse41_ptestnzc:
9567 case Intrinsic::x86_avx_ptestz_256:
9568 case Intrinsic::x86_avx_ptestc_256:
9569 case Intrinsic::x86_avx_ptestnzc_256:
9570 case Intrinsic::x86_avx_vtestz_ps:
9571 case Intrinsic::x86_avx_vtestc_ps:
9572 case Intrinsic::x86_avx_vtestnzc_ps:
9573 case Intrinsic::x86_avx_vtestz_pd:
9574 case Intrinsic::x86_avx_vtestc_pd:
9575 case Intrinsic::x86_avx_vtestnzc_pd:
9576 case Intrinsic::x86_avx_vtestz_ps_256:
9577 case Intrinsic::x86_avx_vtestc_ps_256:
9578 case Intrinsic::x86_avx_vtestnzc_ps_256:
9579 case Intrinsic::x86_avx_vtestz_pd_256:
9580 case Intrinsic::x86_avx_vtestc_pd_256:
9581 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9582 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009583 unsigned X86CC = 0;
9584 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009585 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009586 case Intrinsic::x86_avx_vtestz_ps:
9587 case Intrinsic::x86_avx_vtestz_pd:
9588 case Intrinsic::x86_avx_vtestz_ps_256:
9589 case Intrinsic::x86_avx_vtestz_pd_256:
9590 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009591 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009592 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009593 // ZF = 1
9594 X86CC = X86::COND_E;
9595 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009596 case Intrinsic::x86_avx_vtestc_ps:
9597 case Intrinsic::x86_avx_vtestc_pd:
9598 case Intrinsic::x86_avx_vtestc_ps_256:
9599 case Intrinsic::x86_avx_vtestc_pd_256:
9600 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009601 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009602 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009603 // CF = 1
9604 X86CC = X86::COND_B;
9605 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009606 case Intrinsic::x86_avx_vtestnzc_ps:
9607 case Intrinsic::x86_avx_vtestnzc_pd:
9608 case Intrinsic::x86_avx_vtestnzc_ps_256:
9609 case Intrinsic::x86_avx_vtestnzc_pd_256:
9610 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009611 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009612 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009613 // ZF and CF = 0
9614 X86CC = X86::COND_A;
9615 break;
9616 }
Eric Christopherfd179292009-08-27 18:07:15 +00009617
Eric Christopher71c67532009-07-29 00:28:05 +00009618 SDValue LHS = Op.getOperand(1);
9619 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009620 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9621 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009622 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9623 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9624 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009625 }
Evan Cheng5759f972008-05-04 09:15:50 +00009626
Craig Topper80e46362012-01-23 06:16:53 +00009627 // SSE/AVX shift intrinsics
9628 case Intrinsic::x86_sse2_psll_w:
9629 case Intrinsic::x86_sse2_psll_d:
9630 case Intrinsic::x86_sse2_psll_q:
9631 case Intrinsic::x86_avx2_psll_w:
9632 case Intrinsic::x86_avx2_psll_d:
9633 case Intrinsic::x86_avx2_psll_q:
9634 return DAG.getNode(X86ISD::VSHL, dl, Op.getValueType(),
9635 Op.getOperand(1), Op.getOperand(2));
9636 case Intrinsic::x86_sse2_psrl_w:
9637 case Intrinsic::x86_sse2_psrl_d:
9638 case Intrinsic::x86_sse2_psrl_q:
9639 case Intrinsic::x86_avx2_psrl_w:
9640 case Intrinsic::x86_avx2_psrl_d:
9641 case Intrinsic::x86_avx2_psrl_q:
9642 return DAG.getNode(X86ISD::VSRL, dl, Op.getValueType(),
9643 Op.getOperand(1), Op.getOperand(2));
9644 case Intrinsic::x86_sse2_psra_w:
9645 case Intrinsic::x86_sse2_psra_d:
9646 case Intrinsic::x86_avx2_psra_w:
9647 case Intrinsic::x86_avx2_psra_d:
9648 return DAG.getNode(X86ISD::VSRA, dl, Op.getValueType(),
9649 Op.getOperand(1), Op.getOperand(2));
Evan Cheng5759f972008-05-04 09:15:50 +00009650 case Intrinsic::x86_sse2_pslli_w:
9651 case Intrinsic::x86_sse2_pslli_d:
9652 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009653 case Intrinsic::x86_avx2_pslli_w:
9654 case Intrinsic::x86_avx2_pslli_d:
9655 case Intrinsic::x86_avx2_pslli_q:
9656 return getTargetVShiftNode(X86ISD::VSHLI, dl, Op.getValueType(),
9657 Op.getOperand(1), Op.getOperand(2), DAG);
Evan Cheng5759f972008-05-04 09:15:50 +00009658 case Intrinsic::x86_sse2_psrli_w:
9659 case Intrinsic::x86_sse2_psrli_d:
9660 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009661 case Intrinsic::x86_avx2_psrli_w:
9662 case Intrinsic::x86_avx2_psrli_d:
9663 case Intrinsic::x86_avx2_psrli_q:
9664 return getTargetVShiftNode(X86ISD::VSRLI, dl, Op.getValueType(),
9665 Op.getOperand(1), Op.getOperand(2), DAG);
Evan Cheng5759f972008-05-04 09:15:50 +00009666 case Intrinsic::x86_sse2_psrai_w:
9667 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +00009668 case Intrinsic::x86_avx2_psrai_w:
9669 case Intrinsic::x86_avx2_psrai_d:
9670 return getTargetVShiftNode(X86ISD::VSRAI, dl, Op.getValueType(),
9671 Op.getOperand(1), Op.getOperand(2), DAG);
9672 // Fix vector shift instructions where the last operand is a non-immediate
9673 // i32 value.
Evan Cheng5759f972008-05-04 09:15:50 +00009674 case Intrinsic::x86_mmx_pslli_w:
9675 case Intrinsic::x86_mmx_pslli_d:
9676 case Intrinsic::x86_mmx_pslli_q:
9677 case Intrinsic::x86_mmx_psrli_w:
9678 case Intrinsic::x86_mmx_psrli_d:
9679 case Intrinsic::x86_mmx_psrli_q:
9680 case Intrinsic::x86_mmx_psrai_w:
9681 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009682 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009683 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009684 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009685
9686 unsigned NewIntNo = 0;
Evan Cheng5759f972008-05-04 09:15:50 +00009687 switch (IntNo) {
Craig Topper80e46362012-01-23 06:16:53 +00009688 case Intrinsic::x86_mmx_pslli_w:
9689 NewIntNo = Intrinsic::x86_mmx_psll_w;
Evan Cheng5759f972008-05-04 09:15:50 +00009690 break;
Craig Topper80e46362012-01-23 06:16:53 +00009691 case Intrinsic::x86_mmx_pslli_d:
9692 NewIntNo = Intrinsic::x86_mmx_psll_d;
Evan Cheng5759f972008-05-04 09:15:50 +00009693 break;
Craig Topper80e46362012-01-23 06:16:53 +00009694 case Intrinsic::x86_mmx_pslli_q:
9695 NewIntNo = Intrinsic::x86_mmx_psll_q;
Evan Cheng5759f972008-05-04 09:15:50 +00009696 break;
Craig Topper80e46362012-01-23 06:16:53 +00009697 case Intrinsic::x86_mmx_psrli_w:
9698 NewIntNo = Intrinsic::x86_mmx_psrl_w;
Evan Cheng5759f972008-05-04 09:15:50 +00009699 break;
Craig Topper80e46362012-01-23 06:16:53 +00009700 case Intrinsic::x86_mmx_psrli_d:
9701 NewIntNo = Intrinsic::x86_mmx_psrl_d;
Evan Cheng5759f972008-05-04 09:15:50 +00009702 break;
Craig Topper80e46362012-01-23 06:16:53 +00009703 case Intrinsic::x86_mmx_psrli_q:
9704 NewIntNo = Intrinsic::x86_mmx_psrl_q;
Evan Cheng5759f972008-05-04 09:15:50 +00009705 break;
Craig Topper80e46362012-01-23 06:16:53 +00009706 case Intrinsic::x86_mmx_psrai_w:
9707 NewIntNo = Intrinsic::x86_mmx_psra_w;
Evan Cheng5759f972008-05-04 09:15:50 +00009708 break;
Craig Topper80e46362012-01-23 06:16:53 +00009709 case Intrinsic::x86_mmx_psrai_d:
9710 NewIntNo = Intrinsic::x86_mmx_psra_d;
Evan Cheng5759f972008-05-04 09:15:50 +00009711 break;
Craig Topper80e46362012-01-23 06:16:53 +00009712 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009713 }
Mon P Wangefa42202009-09-03 19:56:25 +00009714
9715 // The vector shift intrinsics with scalars uses 32b shift amounts but
9716 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9717 // to be zero.
Craig Topper80e46362012-01-23 06:16:53 +00009718 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, ShAmt,
9719 DAG.getConstant(0, MVT::i32));
Dale Johannesen0488fb62010-09-30 23:57:10 +00009720// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009721
Owen Andersone50ed302009-08-10 22:56:29 +00009722 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009723 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009724 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009725 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009726 Op.getOperand(1), ShAmt);
9727 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009728 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009729}
Evan Cheng72261582005-12-20 06:22:03 +00009730
Dan Gohmand858e902010-04-17 15:26:15 +00009731SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9732 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009733 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9734 MFI->setReturnAddressIsTaken(true);
9735
Bill Wendling64e87322009-01-16 19:25:27 +00009736 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009737 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009738
9739 if (Depth > 0) {
9740 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9741 SDValue Offset =
9742 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009743 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009744 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009745 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009746 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009747 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009748 }
9749
9750 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009751 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009752 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009753 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009754}
9755
Dan Gohmand858e902010-04-17 15:26:15 +00009756SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009757 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9758 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009759
Owen Andersone50ed302009-08-10 22:56:29 +00009760 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009761 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009762 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9763 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009764 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009765 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009766 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9767 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009768 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009769 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009770}
9771
Dan Gohman475871a2008-07-27 21:46:04 +00009772SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009773 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009774 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009775}
9776
Dan Gohmand858e902010-04-17 15:26:15 +00009777SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009778 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009779 SDValue Chain = Op.getOperand(0);
9780 SDValue Offset = Op.getOperand(1);
9781 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009782 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009783
Dan Gohmand8816272010-08-11 18:14:00 +00009784 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9785 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9786 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009787 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009788
Dan Gohmand8816272010-08-11 18:14:00 +00009789 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9790 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009791 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009792 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9793 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009794 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009795 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009796
Dale Johannesene4d209d2009-02-03 20:21:25 +00009797 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009798 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009799 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009800}
9801
Duncan Sands4a544a72011-09-06 13:37:06 +00009802SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9803 SelectionDAG &DAG) const {
9804 return Op.getOperand(0);
9805}
9806
9807SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9808 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009809 SDValue Root = Op.getOperand(0);
9810 SDValue Trmp = Op.getOperand(1); // trampoline
9811 SDValue FPtr = Op.getOperand(2); // nested function
9812 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009813 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009814
Dan Gohman69de1932008-02-06 22:27:42 +00009815 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009816
9817 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009818 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009819
9820 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009821 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9822 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009823
Evan Cheng0e6a0522011-07-18 20:57:22 +00009824 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9825 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009826
9827 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9828
9829 // Load the pointer to the nested function into R11.
9830 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009831 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009832 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009833 Addr, MachinePointerInfo(TrmpAddr),
9834 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009835
Owen Anderson825b72b2009-08-11 20:47:22 +00009836 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9837 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009838 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9839 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009840 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009841
9842 // Load the 'nest' parameter value into R10.
9843 // R10 is specified in X86CallingConv.td
9844 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009845 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9846 DAG.getConstant(10, MVT::i64));
9847 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009848 Addr, MachinePointerInfo(TrmpAddr, 10),
9849 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009850
Owen Anderson825b72b2009-08-11 20:47:22 +00009851 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9852 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009853 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9854 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009855 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009856
9857 // Jump to the nested function.
9858 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009859 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9860 DAG.getConstant(20, MVT::i64));
9861 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009862 Addr, MachinePointerInfo(TrmpAddr, 20),
9863 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009864
9865 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009866 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9867 DAG.getConstant(22, MVT::i64));
9868 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009869 MachinePointerInfo(TrmpAddr, 22),
9870 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009871
Duncan Sands4a544a72011-09-06 13:37:06 +00009872 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009873 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009874 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009875 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009876 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009877 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009878
9879 switch (CC) {
9880 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009881 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009882 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009883 case CallingConv::X86_StdCall: {
9884 // Pass 'nest' parameter in ECX.
9885 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009886 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009887
9888 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009889 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009890 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009891
Chris Lattner58d74912008-03-12 17:45:29 +00009892 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009893 unsigned InRegCount = 0;
9894 unsigned Idx = 1;
9895
9896 for (FunctionType::param_iterator I = FTy->param_begin(),
9897 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009898 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009899 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009900 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009901
9902 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009903 report_fatal_error("Nest register in use - reduce number of inreg"
9904 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009905 }
9906 }
9907 break;
9908 }
9909 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009910 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009911 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009912 // Pass 'nest' parameter in EAX.
9913 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009914 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009915 break;
9916 }
9917
Dan Gohman475871a2008-07-27 21:46:04 +00009918 SDValue OutChains[4];
9919 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009920
Owen Anderson825b72b2009-08-11 20:47:22 +00009921 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9922 DAG.getConstant(10, MVT::i32));
9923 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009924
Chris Lattnera62fe662010-02-05 19:20:30 +00009925 // This is storing the opcode for MOV32ri.
9926 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009927 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009928 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009929 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009930 Trmp, MachinePointerInfo(TrmpAddr),
9931 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009932
Owen Anderson825b72b2009-08-11 20:47:22 +00009933 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9934 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009935 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9936 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009937 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009938
Chris Lattnera62fe662010-02-05 19:20:30 +00009939 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009940 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9941 DAG.getConstant(5, MVT::i32));
9942 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009943 MachinePointerInfo(TrmpAddr, 5),
9944 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009945
Owen Anderson825b72b2009-08-11 20:47:22 +00009946 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9947 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009948 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9949 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009950 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009951
Duncan Sands4a544a72011-09-06 13:37:06 +00009952 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009953 }
9954}
9955
Dan Gohmand858e902010-04-17 15:26:15 +00009956SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9957 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009958 /*
9959 The rounding mode is in bits 11:10 of FPSR, and has the following
9960 settings:
9961 00 Round to nearest
9962 01 Round to -inf
9963 10 Round to +inf
9964 11 Round to 0
9965
9966 FLT_ROUNDS, on the other hand, expects the following:
9967 -1 Undefined
9968 0 Round to 0
9969 1 Round to nearest
9970 2 Round to +inf
9971 3 Round to -inf
9972
9973 To perform the conversion, we do:
9974 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9975 */
9976
9977 MachineFunction &MF = DAG.getMachineFunction();
9978 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009979 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009980 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009981 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009982 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009983
9984 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009985 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009986 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009987
Michael J. Spencerec38de22010-10-10 22:04:20 +00009988
Chris Lattner2156b792010-09-22 01:11:26 +00009989 MachineMemOperand *MMO =
9990 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9991 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009992
Chris Lattner2156b792010-09-22 01:11:26 +00009993 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9994 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9995 DAG.getVTList(MVT::Other),
9996 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009997
9998 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009999 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010000 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010001
10002 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010003 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010004 DAG.getNode(ISD::SRL, DL, MVT::i16,
10005 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010006 CWD, DAG.getConstant(0x800, MVT::i16)),
10007 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010008 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010009 DAG.getNode(ISD::SRL, DL, MVT::i16,
10010 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010011 CWD, DAG.getConstant(0x400, MVT::i16)),
10012 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010013
Dan Gohman475871a2008-07-27 21:46:04 +000010014 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010015 DAG.getNode(ISD::AND, DL, MVT::i16,
10016 DAG.getNode(ISD::ADD, DL, MVT::i16,
10017 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010018 DAG.getConstant(1, MVT::i16)),
10019 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010020
10021
Duncan Sands83ec4b62008-06-06 12:08:01 +000010022 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010023 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010024}
10025
Dan Gohmand858e902010-04-17 15:26:15 +000010026SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010027 EVT VT = Op.getValueType();
10028 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010029 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010030 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010031
10032 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010033 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010034 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010035 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010036 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010037 }
Evan Cheng18efe262007-12-14 02:13:44 +000010038
Evan Cheng152804e2007-12-14 08:30:15 +000010039 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010040 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010041 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010042
10043 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010044 SDValue Ops[] = {
10045 Op,
10046 DAG.getConstant(NumBits+NumBits-1, OpVT),
10047 DAG.getConstant(X86::COND_E, MVT::i8),
10048 Op.getValue(1)
10049 };
10050 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010051
10052 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010053 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010054
Owen Anderson825b72b2009-08-11 20:47:22 +000010055 if (VT == MVT::i8)
10056 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010057 return Op;
10058}
10059
Chandler Carruthacc068e2011-12-24 10:55:54 +000010060SDValue X86TargetLowering::LowerCTLZ_ZERO_UNDEF(SDValue Op,
10061 SelectionDAG &DAG) const {
10062 EVT VT = Op.getValueType();
10063 EVT OpVT = VT;
10064 unsigned NumBits = VT.getSizeInBits();
10065 DebugLoc dl = Op.getDebugLoc();
10066
10067 Op = Op.getOperand(0);
10068 if (VT == MVT::i8) {
10069 // Zero extend to i32 since there is not an i8 bsr.
10070 OpVT = MVT::i32;
10071 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10072 }
10073
10074 // Issue a bsr (scan bits in reverse).
10075 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10076 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10077
10078 // And xor with NumBits-1.
10079 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10080
10081 if (VT == MVT::i8)
10082 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10083 return Op;
10084}
10085
Dan Gohmand858e902010-04-17 15:26:15 +000010086SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010087 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010088 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010089 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010090 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010091
10092 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010093 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010094 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010095
10096 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010097 SDValue Ops[] = {
10098 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010099 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010100 DAG.getConstant(X86::COND_E, MVT::i8),
10101 Op.getValue(1)
10102 };
Chandler Carruth77821022011-12-24 12:12:34 +000010103 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010104}
10105
Craig Topper13894fa2011-08-24 06:14:18 +000010106// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10107// ones, and then concatenate the result back.
10108static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010109 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010110
10111 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
10112 "Unsupported value type for operation");
10113
10114 int NumElems = VT.getVectorNumElements();
10115 DebugLoc dl = Op.getDebugLoc();
10116 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10117 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10118
10119 // Extract the LHS vectors
10120 SDValue LHS = Op.getOperand(0);
10121 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10122 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10123
10124 // Extract the RHS vectors
10125 SDValue RHS = Op.getOperand(1);
10126 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
10127 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
10128
10129 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10130 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10131
10132 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10133 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10134 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10135}
10136
10137SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
10138 assert(Op.getValueType().getSizeInBits() == 256 &&
10139 Op.getValueType().isInteger() &&
10140 "Only handle AVX 256-bit vector integer operation");
10141 return Lower256IntArith(Op, DAG);
10142}
10143
10144SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
10145 assert(Op.getValueType().getSizeInBits() == 256 &&
10146 Op.getValueType().isInteger() &&
10147 "Only handle AVX 256-bit vector integer operation");
10148 return Lower256IntArith(Op, DAG);
10149}
10150
10151SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10152 EVT VT = Op.getValueType();
10153
10154 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +000010155 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010156 return Lower256IntArith(Op, DAG);
10157
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010158 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010159
Craig Topperaaa643c2011-11-09 07:28:55 +000010160 SDValue A = Op.getOperand(0);
10161 SDValue B = Op.getOperand(1);
10162
10163 if (VT == MVT::v4i64) {
10164 assert(Subtarget->hasAVX2() && "Lowering v4i64 multiply requires AVX2");
10165
10166 // ulong2 Ahi = __builtin_ia32_psrlqi256( a, 32);
10167 // ulong2 Bhi = __builtin_ia32_psrlqi256( b, 32);
10168 // ulong2 AloBlo = __builtin_ia32_pmuludq256( a, b );
10169 // ulong2 AloBhi = __builtin_ia32_pmuludq256( a, Bhi );
10170 // ulong2 AhiBlo = __builtin_ia32_pmuludq256( Ahi, b );
10171 //
10172 // AloBhi = __builtin_ia32_psllqi256( AloBhi, 32 );
10173 // AhiBlo = __builtin_ia32_psllqi256( AhiBlo, 32 );
10174 // return AloBlo + AloBhi + AhiBlo;
10175
Craig Topper7fb8b0c2012-01-23 06:46:22 +000010176 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A,
10177 DAG.getConstant(32, MVT::i32));
10178 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B,
10179 DAG.getConstant(32, MVT::i32));
Craig Topperaaa643c2011-11-09 07:28:55 +000010180 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10181 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10182 A, B);
10183 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10184 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10185 A, Bhi);
10186 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10187 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10188 Ahi, B);
Craig Topper7fb8b0c2012-01-23 06:46:22 +000010189 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi,
10190 DAG.getConstant(32, MVT::i32));
10191 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo,
10192 DAG.getConstant(32, MVT::i32));
Craig Topperaaa643c2011-11-09 07:28:55 +000010193 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10194 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
10195 return Res;
10196 }
10197
10198 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
10199
Mon P Wangaf9b9522008-12-18 21:42:19 +000010200 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
10201 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
10202 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
10203 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
10204 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
10205 //
10206 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
10207 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
10208 // return AloBlo + AloBhi + AhiBlo;
10209
Craig Topper7fb8b0c2012-01-23 06:46:22 +000010210 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A,
10211 DAG.getConstant(32, MVT::i32));
10212 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B,
10213 DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010214 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010215 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010216 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010217 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010218 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010219 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010220 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010221 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010222 Ahi, B);
Craig Topper7fb8b0c2012-01-23 06:46:22 +000010223 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi,
10224 DAG.getConstant(32, MVT::i32));
10225 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo,
10226 DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010227 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10228 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010229 return Res;
10230}
10231
Nadav Rotem43012222011-05-11 08:12:09 +000010232SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10233
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010234 EVT VT = Op.getValueType();
10235 DebugLoc dl = Op.getDebugLoc();
10236 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010237 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010238 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010239
Craig Topper1accb7e2012-01-10 06:54:16 +000010240 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010241 return SDValue();
10242
Nadav Rotem43012222011-05-11 08:12:09 +000010243 // Optimize shl/srl/sra with constant shift amount.
10244 if (isSplatVector(Amt.getNode())) {
10245 SDValue SclrAmt = Amt->getOperand(0);
10246 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10247 uint64_t ShiftAmt = C->getZExtValue();
10248
Craig Toppered2e13d2012-01-22 19:15:14 +000010249 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10250 (Subtarget->hasAVX2() &&
10251 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10252 if (Op.getOpcode() == ISD::SHL)
10253 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10254 DAG.getConstant(ShiftAmt, MVT::i32));
10255 if (Op.getOpcode() == ISD::SRL)
10256 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10257 DAG.getConstant(ShiftAmt, MVT::i32));
10258 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10259 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10260 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010261 }
10262
Craig Toppered2e13d2012-01-22 19:15:14 +000010263 if (VT == MVT::v16i8) {
10264 if (Op.getOpcode() == ISD::SHL) {
10265 // Make a large shift.
10266 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10267 DAG.getConstant(ShiftAmt, MVT::i32));
10268 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10269 // Zero out the rightmost bits.
10270 SmallVector<SDValue, 16> V(16,
10271 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10272 MVT::i8));
10273 return DAG.getNode(ISD::AND, dl, VT, SHL,
10274 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010275 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010276 if (Op.getOpcode() == ISD::SRL) {
10277 // Make a large shift.
10278 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10279 DAG.getConstant(ShiftAmt, MVT::i32));
10280 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10281 // Zero out the leftmost bits.
10282 SmallVector<SDValue, 16> V(16,
10283 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10284 MVT::i8));
10285 return DAG.getNode(ISD::AND, dl, VT, SRL,
10286 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10287 }
10288 if (Op.getOpcode() == ISD::SRA) {
10289 if (ShiftAmt == 7) {
10290 // R s>> 7 === R s< 0
10291 SDValue Zeros = getZeroVector(VT, /* HasSSE2 */true,
10292 /* HasAVX2 */false, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010293 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010294 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010295
Craig Toppered2e13d2012-01-22 19:15:14 +000010296 // R s>> a === ((R u>> a) ^ m) - m
10297 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10298 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10299 MVT::i8));
10300 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10301 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10302 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10303 return Res;
10304 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010305 }
Craig Topper46154eb2011-11-11 07:39:23 +000010306
Craig Topper0d86d462011-11-20 00:12:05 +000010307 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10308 if (Op.getOpcode() == ISD::SHL) {
10309 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010310 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10311 DAG.getConstant(ShiftAmt, MVT::i32));
10312 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010313 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010314 SmallVector<SDValue, 32> V(32,
10315 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10316 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010317 return DAG.getNode(ISD::AND, dl, VT, SHL,
10318 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010319 }
Craig Topper0d86d462011-11-20 00:12:05 +000010320 if (Op.getOpcode() == ISD::SRL) {
10321 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010322 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10323 DAG.getConstant(ShiftAmt, MVT::i32));
10324 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010325 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010326 SmallVector<SDValue, 32> V(32,
10327 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10328 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010329 return DAG.getNode(ISD::AND, dl, VT, SRL,
10330 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10331 }
10332 if (Op.getOpcode() == ISD::SRA) {
10333 if (ShiftAmt == 7) {
10334 // R s>> 7 === R s< 0
Craig Topper12216172012-01-13 08:12:35 +000010335 SDValue Zeros = getZeroVector(VT, true /* HasSSE2 */,
10336 true /* HasAVX2 */, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010337 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010338 }
10339
10340 // R s>> a === ((R u>> a) ^ m) - m
10341 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10342 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10343 MVT::i8));
10344 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10345 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10346 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10347 return Res;
10348 }
10349 }
Nadav Rotem43012222011-05-11 08:12:09 +000010350 }
10351 }
10352
10353 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010354 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010355 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
10356 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000010357
10358 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Chris Lattner4ca829e2012-01-25 06:02:56 +000010359 Constant *C = ConstantVector::getSplat(4, CI);
Nate Begeman51409212010-07-28 00:21:48 +000010360 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10361 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010362 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010363 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010364
10365 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010366 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010367 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10368 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10369 }
Nadav Rotem43012222011-05-11 08:12:09 +000010370 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000010371 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000010372
Nate Begeman51409212010-07-28 00:21:48 +000010373 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000010374 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
10375 DAG.getConstant(5, MVT::i32));
10376 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010377
Lang Hames8b99c1e2011-12-17 01:08:46 +000010378 // Turn 'a' into a mask suitable for VSELECT
10379 SDValue VSelM = DAG.getConstant(0x80, VT);
10380 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010381 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010382
Lang Hames8b99c1e2011-12-17 01:08:46 +000010383 SDValue CM1 = DAG.getConstant(0x0f, VT);
10384 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010385
Lang Hames8b99c1e2011-12-17 01:08:46 +000010386 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10387 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000010388 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10389 DAG.getConstant(4, MVT::i32), DAG);
10390 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010391 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10392
Nate Begeman51409212010-07-28 00:21:48 +000010393 // a += a
10394 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010395 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010396 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010397
Lang Hames8b99c1e2011-12-17 01:08:46 +000010398 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10399 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000010400 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10401 DAG.getConstant(2, MVT::i32), DAG);
10402 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010403 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10404
Nate Begeman51409212010-07-28 00:21:48 +000010405 // a += a
10406 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010407 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010408 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010409
Lang Hames8b99c1e2011-12-17 01:08:46 +000010410 // return VSELECT(r, r+r, a);
10411 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010412 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010413 return R;
10414 }
Craig Topper46154eb2011-11-11 07:39:23 +000010415
10416 // Decompose 256-bit shifts into smaller 128-bit shifts.
10417 if (VT.getSizeInBits() == 256) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010418 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000010419 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10420 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10421
10422 // Extract the two vectors
10423 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10424 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10425 DAG, dl);
10426
10427 // Recreate the shift amount vectors
10428 SDValue Amt1, Amt2;
10429 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10430 // Constant shift amount
10431 SmallVector<SDValue, 4> Amt1Csts;
10432 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000010433 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010434 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000010435 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010436 Amt2Csts.push_back(Amt->getOperand(i));
10437
10438 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10439 &Amt1Csts[0], NumElems/2);
10440 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10441 &Amt2Csts[0], NumElems/2);
10442 } else {
10443 // Variable shift amount
10444 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10445 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10446 DAG, dl);
10447 }
10448
10449 // Issue new vector shifts for the smaller types
10450 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10451 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10452
10453 // Concatenate the result back
10454 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10455 }
10456
Nate Begeman51409212010-07-28 00:21:48 +000010457 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010458}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010459
Dan Gohmand858e902010-04-17 15:26:15 +000010460SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010461 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10462 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010463 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10464 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010465 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010466 SDValue LHS = N->getOperand(0);
10467 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010468 unsigned BaseOp = 0;
10469 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010470 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010471 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010472 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010473 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010474 // A subtract of one will be selected as a INC. Note that INC doesn't
10475 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010476 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10477 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010478 BaseOp = X86ISD::INC;
10479 Cond = X86::COND_O;
10480 break;
10481 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010482 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010483 Cond = X86::COND_O;
10484 break;
10485 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010486 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010487 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010488 break;
10489 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010490 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10491 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010492 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10493 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010494 BaseOp = X86ISD::DEC;
10495 Cond = X86::COND_O;
10496 break;
10497 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010498 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010499 Cond = X86::COND_O;
10500 break;
10501 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010502 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010503 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010504 break;
10505 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010506 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010507 Cond = X86::COND_O;
10508 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010509 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10510 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10511 MVT::i32);
10512 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010513
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010514 SDValue SetCC =
10515 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10516 DAG.getConstant(X86::COND_O, MVT::i32),
10517 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010518
Dan Gohman6e5fda22011-07-22 18:45:15 +000010519 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010520 }
Bill Wendling74c37652008-12-09 22:08:41 +000010521 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010522
Bill Wendling61edeb52008-12-02 01:06:39 +000010523 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010524 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010525 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010526
Bill Wendling61edeb52008-12-02 01:06:39 +000010527 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010528 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10529 DAG.getConstant(Cond, MVT::i32),
10530 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010531
Dan Gohman6e5fda22011-07-22 18:45:15 +000010532 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010533}
10534
Chad Rosier30450e82011-12-22 22:35:21 +000010535SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
10536 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010537 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010538 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10539 EVT VT = Op.getValueType();
10540
Craig Toppered2e13d2012-01-22 19:15:14 +000010541 if (!Subtarget->hasSSE2() || !VT.isVector())
10542 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010543
Craig Toppered2e13d2012-01-22 19:15:14 +000010544 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10545 ExtraVT.getScalarType().getSizeInBits();
10546 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10547
10548 switch (VT.getSimpleVT().SimpleTy) {
10549 default: return SDValue();
10550 case MVT::v8i32:
10551 case MVT::v16i16:
10552 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010553 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000010554 if (!Subtarget->hasAVX2()) {
10555 // needs to be split
10556 int NumElems = VT.getVectorNumElements();
10557 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10558 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
Craig Toppera124f942011-11-21 01:12:36 +000010559
Craig Toppered2e13d2012-01-22 19:15:14 +000010560 // Extract the LHS vectors
10561 SDValue LHS = Op.getOperand(0);
10562 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10563 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000010564
Craig Toppered2e13d2012-01-22 19:15:14 +000010565 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10566 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000010567
Craig Toppered2e13d2012-01-22 19:15:14 +000010568 EVT ExtraEltVT = ExtraVT.getVectorElementType();
10569 int ExtraNumElems = ExtraVT.getVectorNumElements();
10570 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10571 ExtraNumElems/2);
10572 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000010573
Craig Toppered2e13d2012-01-22 19:15:14 +000010574 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10575 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000010576
Craig Toppered2e13d2012-01-22 19:15:14 +000010577 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10578 }
10579 // fall through
10580 case MVT::v4i32:
10581 case MVT::v8i16: {
10582 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
10583 Op.getOperand(0), ShAmt, DAG);
10584 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010585 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010586 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010587}
10588
10589
Eric Christopher9a9d2752010-07-22 02:48:34 +000010590SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10591 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010592
Eric Christopher77ed1352011-07-08 00:04:56 +000010593 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10594 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000010595 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010596 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010597 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010598 SDValue Ops[] = {
10599 DAG.getRegister(X86::ESP, MVT::i32), // Base
10600 DAG.getTargetConstant(1, MVT::i8), // Scale
10601 DAG.getRegister(0, MVT::i32), // Index
10602 DAG.getTargetConstant(0, MVT::i32), // Disp
10603 DAG.getRegister(0, MVT::i32), // Segment.
10604 Zero,
10605 Chain
10606 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010607 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010608 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10609 array_lengthof(Ops));
10610 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010611 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010612
Eric Christopher9a9d2752010-07-22 02:48:34 +000010613 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010614 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010615 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010616
Chris Lattner132929a2010-08-14 17:26:09 +000010617 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10618 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10619 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10620 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010621
Chris Lattner132929a2010-08-14 17:26:09 +000010622 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10623 if (!Op1 && !Op2 && !Op3 && Op4)
10624 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010625
Chris Lattner132929a2010-08-14 17:26:09 +000010626 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10627 if (Op1 && !Op2 && !Op3 && !Op4)
10628 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010629
10630 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010631 // (MFENCE)>;
10632 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010633}
10634
Eli Friedman14648462011-07-27 22:21:52 +000010635SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10636 SelectionDAG &DAG) const {
10637 DebugLoc dl = Op.getDebugLoc();
10638 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10639 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10640 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10641 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10642
10643 // The only fence that needs an instruction is a sequentially-consistent
10644 // cross-thread fence.
10645 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10646 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10647 // no-sse2). There isn't any reason to disable it if the target processor
10648 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000010649 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010650 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10651
10652 SDValue Chain = Op.getOperand(0);
10653 SDValue Zero = DAG.getConstant(0, MVT::i32);
10654 SDValue Ops[] = {
10655 DAG.getRegister(X86::ESP, MVT::i32), // Base
10656 DAG.getTargetConstant(1, MVT::i8), // Scale
10657 DAG.getRegister(0, MVT::i32), // Index
10658 DAG.getTargetConstant(0, MVT::i32), // Disp
10659 DAG.getRegister(0, MVT::i32), // Segment.
10660 Zero,
10661 Chain
10662 };
10663 SDNode *Res =
10664 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10665 array_lengthof(Ops));
10666 return SDValue(Res, 0);
10667 }
10668
10669 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10670 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10671}
10672
10673
Dan Gohmand858e902010-04-17 15:26:15 +000010674SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010675 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010676 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010677 unsigned Reg = 0;
10678 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010679 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010680 default:
10681 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010682 case MVT::i8: Reg = X86::AL; size = 1; break;
10683 case MVT::i16: Reg = X86::AX; size = 2; break;
10684 case MVT::i32: Reg = X86::EAX; size = 4; break;
10685 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010686 assert(Subtarget->is64Bit() && "Node not type legal!");
10687 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010688 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010689 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010690 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010691 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010692 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010693 Op.getOperand(1),
10694 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010695 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010696 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010697 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010698 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10699 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10700 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010701 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010702 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010703 return cpOut;
10704}
10705
Duncan Sands1607f052008-12-01 11:39:25 +000010706SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010707 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010708 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010709 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010710 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010711 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010712 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010713 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10714 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010715 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010716 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10717 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010718 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010719 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010720 rdx.getValue(1)
10721 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010722 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010723}
10724
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010725SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010726 SelectionDAG &DAG) const {
10727 EVT SrcVT = Op.getOperand(0).getValueType();
10728 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000010729 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010730 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010731 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010732 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010733 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010734 // i64 <=> MMX conversions are Legal.
10735 if (SrcVT==MVT::i64 && DstVT.isVector())
10736 return Op;
10737 if (DstVT==MVT::i64 && SrcVT.isVector())
10738 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010739 // MMX <=> MMX conversions are Legal.
10740 if (SrcVT.isVector() && DstVT.isVector())
10741 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010742 // All other conversions need to be expanded.
10743 return SDValue();
10744}
Chris Lattner5b856542010-12-20 00:59:46 +000010745
Dan Gohmand858e902010-04-17 15:26:15 +000010746SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010747 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010748 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010749 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010750 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010751 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010752 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010753 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010754 Node->getOperand(0),
10755 Node->getOperand(1), negOp,
10756 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010757 cast<AtomicSDNode>(Node)->getAlignment(),
10758 cast<AtomicSDNode>(Node)->getOrdering(),
10759 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010760}
10761
Eli Friedman327236c2011-08-24 20:50:09 +000010762static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10763 SDNode *Node = Op.getNode();
10764 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010765 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010766
10767 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010768 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10769 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10770 // (The only way to get a 16-byte store is cmpxchg16b)
10771 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10772 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10773 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010774 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10775 cast<AtomicSDNode>(Node)->getMemoryVT(),
10776 Node->getOperand(0),
10777 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010778 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010779 cast<AtomicSDNode>(Node)->getOrdering(),
10780 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010781 return Swap.getValue(1);
10782 }
10783 // Other atomic stores have a simple pattern.
10784 return Op;
10785}
10786
Chris Lattner5b856542010-12-20 00:59:46 +000010787static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10788 EVT VT = Op.getNode()->getValueType(0);
10789
10790 // Let legalize expand this if it isn't a legal type yet.
10791 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10792 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010793
Chris Lattner5b856542010-12-20 00:59:46 +000010794 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010795
Chris Lattner5b856542010-12-20 00:59:46 +000010796 unsigned Opc;
10797 bool ExtraOp = false;
10798 switch (Op.getOpcode()) {
10799 default: assert(0 && "Invalid code");
10800 case ISD::ADDC: Opc = X86ISD::ADD; break;
10801 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10802 case ISD::SUBC: Opc = X86ISD::SUB; break;
10803 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10804 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010805
Chris Lattner5b856542010-12-20 00:59:46 +000010806 if (!ExtraOp)
10807 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10808 Op.getOperand(1));
10809 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10810 Op.getOperand(1), Op.getOperand(2));
10811}
10812
Evan Cheng0db9fe62006-04-25 20:13:52 +000010813/// LowerOperation - Provide custom lowering hooks for some operations.
10814///
Dan Gohmand858e902010-04-17 15:26:15 +000010815SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010816 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010817 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010818 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010819 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010820 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010821 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10822 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010823 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010824 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010825 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010826 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10827 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10828 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010829 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010830 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010831 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10832 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10833 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010834 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010835 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010836 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010837 case ISD::SHL_PARTS:
10838 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010839 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010840 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010841 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010842 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010843 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010844 case ISD::FABS: return LowerFABS(Op, DAG);
10845 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010846 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010847 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010848 case ISD::SETCC: return LowerSETCC(Op, DAG);
10849 case ISD::SELECT: return LowerSELECT(Op, DAG);
10850 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010851 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010852 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010853 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010854 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010855 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010856 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10857 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010858 case ISD::FRAME_TO_ARGS_OFFSET:
10859 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010860 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010861 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010862 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10863 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010864 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010865 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000010866 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010867 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010868 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010869 case ISD::SRA:
10870 case ISD::SRL:
10871 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010872 case ISD::SADDO:
10873 case ISD::UADDO:
10874 case ISD::SSUBO:
10875 case ISD::USUBO:
10876 case ISD::SMULO:
10877 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010878 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010879 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010880 case ISD::ADDC:
10881 case ISD::ADDE:
10882 case ISD::SUBC:
10883 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010884 case ISD::ADD: return LowerADD(Op, DAG);
10885 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010886 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010887}
10888
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010889static void ReplaceATOMIC_LOAD(SDNode *Node,
10890 SmallVectorImpl<SDValue> &Results,
10891 SelectionDAG &DAG) {
10892 DebugLoc dl = Node->getDebugLoc();
10893 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10894
10895 // Convert wide load -> cmpxchg8b/cmpxchg16b
10896 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10897 // (The only way to get a 16-byte load is cmpxchg16b)
10898 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010899 SDValue Zero = DAG.getConstant(0, VT);
10900 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010901 Node->getOperand(0),
10902 Node->getOperand(1), Zero, Zero,
10903 cast<AtomicSDNode>(Node)->getMemOperand(),
10904 cast<AtomicSDNode>(Node)->getOrdering(),
10905 cast<AtomicSDNode>(Node)->getSynchScope());
10906 Results.push_back(Swap.getValue(0));
10907 Results.push_back(Swap.getValue(1));
10908}
10909
Duncan Sands1607f052008-12-01 11:39:25 +000010910void X86TargetLowering::
10911ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010912 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010913 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000010914 assert (Node->getValueType(0) == MVT::i64 &&
10915 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010916
10917 SDValue Chain = Node->getOperand(0);
10918 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010919 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010920 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010921 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010922 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010923 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010924 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010925 SDValue Result =
10926 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10927 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010928 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010929 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010930 Results.push_back(Result.getValue(2));
10931}
10932
Duncan Sands126d9072008-07-04 11:47:58 +000010933/// ReplaceNodeResults - Replace a node with an illegal result type
10934/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010935void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10936 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010937 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010938 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010939 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010940 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010941 assert(false && "Do not know how to custom type legalize this operation!");
10942 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010943 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010944 case ISD::ADDC:
10945 case ISD::ADDE:
10946 case ISD::SUBC:
10947 case ISD::SUBE:
10948 // We don't want to expand or promote these.
10949 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010950 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010951 std::pair<SDValue,SDValue> Vals =
10952 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010953 SDValue FIST = Vals.first, StackSlot = Vals.second;
10954 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010955 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010956 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010957 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010958 MachinePointerInfo(),
10959 false, false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010960 }
10961 return;
10962 }
10963 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010964 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010965 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010966 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010967 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010968 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010969 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010970 eax.getValue(2));
10971 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10972 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010973 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010974 Results.push_back(edx.getValue(1));
10975 return;
10976 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010977 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010978 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010979 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010980 bool Regs64bit = T == MVT::i128;
10981 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010982 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010983 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10984 DAG.getConstant(0, HalfT));
10985 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10986 DAG.getConstant(1, HalfT));
10987 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10988 Regs64bit ? X86::RAX : X86::EAX,
10989 cpInL, SDValue());
10990 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10991 Regs64bit ? X86::RDX : X86::EDX,
10992 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010993 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010994 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10995 DAG.getConstant(0, HalfT));
10996 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10997 DAG.getConstant(1, HalfT));
10998 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10999 Regs64bit ? X86::RBX : X86::EBX,
11000 swapInL, cpInH.getValue(1));
11001 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
11002 Regs64bit ? X86::RCX : X86::ECX,
11003 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011004 SDValue Ops[] = { swapInH.getValue(0),
11005 N->getOperand(1),
11006 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011007 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011008 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011009 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11010 X86ISD::LCMPXCHG8_DAG;
11011 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011012 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011013 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11014 Regs64bit ? X86::RAX : X86::EAX,
11015 HalfT, Result.getValue(1));
11016 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11017 Regs64bit ? X86::RDX : X86::EDX,
11018 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011019 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011020 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011021 Results.push_back(cpOutH.getValue(1));
11022 return;
11023 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011024 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000011025 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
11026 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011027 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000011028 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
11029 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011030 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000011031 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
11032 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011033 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000011034 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
11035 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011036 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000011037 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
11038 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011039 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000011040 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
11041 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011042 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000011043 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
11044 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011045 case ISD::ATOMIC_LOAD:
11046 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011047 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011048}
11049
Evan Cheng72261582005-12-20 06:22:03 +000011050const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11051 switch (Opcode) {
11052 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011053 case X86ISD::BSF: return "X86ISD::BSF";
11054 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011055 case X86ISD::SHLD: return "X86ISD::SHLD";
11056 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011057 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011058 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011059 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011060 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011061 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011062 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011063 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11064 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11065 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011066 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011067 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011068 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011069 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011070 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011071 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011072 case X86ISD::COMI: return "X86ISD::COMI";
11073 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011074 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011075 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011076 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11077 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011078 case X86ISD::CMOV: return "X86ISD::CMOV";
11079 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011080 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011081 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11082 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011083 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011084 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011085 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011086 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011087 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011088 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11089 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011090 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011091 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011092 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011093 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011094 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Craig Topperfe033152011-12-06 09:31:36 +000011095 case X86ISD::HADD: return "X86ISD::HADD";
11096 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011097 case X86ISD::FHADD: return "X86ISD::FHADD";
11098 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011099 case X86ISD::FMAX: return "X86ISD::FMAX";
11100 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000011101 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11102 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011103 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011104 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011105 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011106 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011107 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011108 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11109 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011110 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11111 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11112 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11113 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11114 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11115 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011116 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
11117 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Craig Toppered2e13d2012-01-22 19:15:14 +000011118 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11119 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011120 case X86ISD::VSHL: return "X86ISD::VSHL";
11121 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011122 case X86ISD::VSRA: return "X86ISD::VSRA";
11123 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11124 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11125 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011126 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011127 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11128 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011129 case X86ISD::ADD: return "X86ISD::ADD";
11130 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011131 case X86ISD::ADC: return "X86ISD::ADC";
11132 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011133 case X86ISD::SMUL: return "X86ISD::SMUL";
11134 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011135 case X86ISD::INC: return "X86ISD::INC";
11136 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011137 case X86ISD::OR: return "X86ISD::OR";
11138 case X86ISD::XOR: return "X86ISD::XOR";
11139 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011140 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011141 case X86ISD::BLSI: return "X86ISD::BLSI";
11142 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11143 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011144 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011145 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011146 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011147 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11148 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11149 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011150 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000011151 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011152 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011153 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011154 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011155 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11156 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011157 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11158 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11159 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011160 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11161 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000011162 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
11163 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011164 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011165 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011166 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011167 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011168 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011169 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011170 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011171 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000011172 }
11173}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011174
Chris Lattnerc9addb72007-03-30 23:15:24 +000011175// isLegalAddressingMode - Return true if the addressing mode represented
11176// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011177bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011178 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011179 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011180 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011181 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011182
Chris Lattnerc9addb72007-03-30 23:15:24 +000011183 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011184 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011185 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011186
Chris Lattnerc9addb72007-03-30 23:15:24 +000011187 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011188 unsigned GVFlags =
11189 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011190
Chris Lattnerdfed4132009-07-10 07:38:24 +000011191 // If a reference to this global requires an extra load, we can't fold it.
11192 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011193 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011194
Chris Lattnerdfed4132009-07-10 07:38:24 +000011195 // If BaseGV requires a register for the PIC base, we cannot also have a
11196 // BaseReg specified.
11197 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011198 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011199
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011200 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011201 if ((M != CodeModel::Small || R != Reloc::Static) &&
11202 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011203 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011204 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011205
Chris Lattnerc9addb72007-03-30 23:15:24 +000011206 switch (AM.Scale) {
11207 case 0:
11208 case 1:
11209 case 2:
11210 case 4:
11211 case 8:
11212 // These scales always work.
11213 break;
11214 case 3:
11215 case 5:
11216 case 9:
11217 // These scales are formed with basereg+scalereg. Only accept if there is
11218 // no basereg yet.
11219 if (AM.HasBaseReg)
11220 return false;
11221 break;
11222 default: // Other stuff never works.
11223 return false;
11224 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011225
Chris Lattnerc9addb72007-03-30 23:15:24 +000011226 return true;
11227}
11228
11229
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011230bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011231 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011232 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011233 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11234 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011235 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011236 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011237 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011238}
11239
Owen Andersone50ed302009-08-10 22:56:29 +000011240bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011241 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011242 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011243 unsigned NumBits1 = VT1.getSizeInBits();
11244 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011245 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011246 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011247 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011248}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011249
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011250bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011251 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011252 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011253}
11254
Owen Andersone50ed302009-08-10 22:56:29 +000011255bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011256 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011257 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011258}
11259
Owen Andersone50ed302009-08-10 22:56:29 +000011260bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011261 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011262 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011263}
11264
Evan Cheng60c07e12006-07-05 22:17:51 +000011265/// isShuffleMaskLegal - Targets can use this to indicate that they only
11266/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11267/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11268/// are assumed to be legal.
11269bool
Eric Christopherfd179292009-08-27 18:07:15 +000011270X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011271 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011272 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011273 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011274 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011275
Nate Begemana09008b2009-10-19 02:17:23 +000011276 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011277 return (VT.getVectorNumElements() == 2 ||
11278 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11279 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011280 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011281 isPSHUFDMask(M, VT) ||
11282 isPSHUFHWMask(M, VT) ||
11283 isPSHUFLWMask(M, VT) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000011284 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000011285 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11286 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011287 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11288 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011289}
11290
Dan Gohman7d8143f2008-04-09 20:09:42 +000011291bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011292X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011293 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011294 unsigned NumElts = VT.getVectorNumElements();
11295 // FIXME: This collection of masks seems suspect.
11296 if (NumElts == 2)
11297 return true;
11298 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11299 return (isMOVLMask(Mask, VT) ||
11300 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011301 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
11302 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011303 }
11304 return false;
11305}
11306
11307//===----------------------------------------------------------------------===//
11308// X86 Scheduler Hooks
11309//===----------------------------------------------------------------------===//
11310
Mon P Wang63307c32008-05-05 19:05:59 +000011311// private utility function
11312MachineBasicBlock *
11313X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11314 MachineBasicBlock *MBB,
11315 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011316 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011317 unsigned LoadOpc,
11318 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011319 unsigned notOpc,
11320 unsigned EAXreg,
11321 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011322 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011323 // For the atomic bitwise operator, we generate
11324 // thisMBB:
11325 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011326 // ld t1 = [bitinstr.addr]
11327 // op t2 = t1, [bitinstr.val]
11328 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011329 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11330 // bz newMBB
11331 // fallthrough -->nextMBB
11332 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11333 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011334 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011335 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011336
Mon P Wang63307c32008-05-05 19:05:59 +000011337 /// First build the CFG
11338 MachineFunction *F = MBB->getParent();
11339 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011340 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11341 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11342 F->insert(MBBIter, newMBB);
11343 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011344
Dan Gohman14152b42010-07-06 20:24:04 +000011345 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11346 nextMBB->splice(nextMBB->begin(), thisMBB,
11347 llvm::next(MachineBasicBlock::iterator(bInstr)),
11348 thisMBB->end());
11349 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011350
Mon P Wang63307c32008-05-05 19:05:59 +000011351 // Update thisMBB to fall through to newMBB
11352 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011353
Mon P Wang63307c32008-05-05 19:05:59 +000011354 // newMBB jumps to itself and fall through to nextMBB
11355 newMBB->addSuccessor(nextMBB);
11356 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011357
Mon P Wang63307c32008-05-05 19:05:59 +000011358 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011359 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011360 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011361 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011362 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011363 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011364 int numArgs = bInstr->getNumOperands() - 1;
11365 for (int i=0; i < numArgs; ++i)
11366 argOpers[i] = &bInstr->getOperand(i+1);
11367
11368 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011369 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011370 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011371
Dale Johannesen140be2d2008-08-19 18:47:28 +000011372 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011373 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011374 for (int i=0; i <= lastAddrIndx; ++i)
11375 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011376
Dale Johannesen140be2d2008-08-19 18:47:28 +000011377 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011378 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011379 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011380 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011381 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011382 tt = t1;
11383
Dale Johannesen140be2d2008-08-19 18:47:28 +000011384 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011385 assert((argOpers[valArgIndx]->isReg() ||
11386 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011387 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011388 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011389 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011390 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011391 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011392 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011393 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011394
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011395 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011396 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011397
Dale Johannesene4d209d2009-02-03 20:21:25 +000011398 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011399 for (int i=0; i <= lastAddrIndx; ++i)
11400 (*MIB).addOperand(*argOpers[i]);
11401 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011402 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011403 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11404 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011405
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011406 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011407 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011408
Mon P Wang63307c32008-05-05 19:05:59 +000011409 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011410 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011411
Dan Gohman14152b42010-07-06 20:24:04 +000011412 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011413 return nextMBB;
11414}
11415
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011416// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011417MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011418X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11419 MachineBasicBlock *MBB,
11420 unsigned regOpcL,
11421 unsigned regOpcH,
11422 unsigned immOpcL,
11423 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011424 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011425 // For the atomic bitwise operator, we generate
11426 // thisMBB (instructions are in pairs, except cmpxchg8b)
11427 // ld t1,t2 = [bitinstr.addr]
11428 // newMBB:
11429 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11430 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011431 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011432 // mov ECX, EBX <- t5, t6
11433 // mov EAX, EDX <- t1, t2
11434 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11435 // mov t3, t4 <- EAX, EDX
11436 // bz newMBB
11437 // result in out1, out2
11438 // fallthrough -->nextMBB
11439
11440 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11441 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011442 const unsigned NotOpc = X86::NOT32r;
11443 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11444 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11445 MachineFunction::iterator MBBIter = MBB;
11446 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011447
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011448 /// First build the CFG
11449 MachineFunction *F = MBB->getParent();
11450 MachineBasicBlock *thisMBB = MBB;
11451 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11452 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11453 F->insert(MBBIter, newMBB);
11454 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011455
Dan Gohman14152b42010-07-06 20:24:04 +000011456 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11457 nextMBB->splice(nextMBB->begin(), thisMBB,
11458 llvm::next(MachineBasicBlock::iterator(bInstr)),
11459 thisMBB->end());
11460 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011461
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011462 // Update thisMBB to fall through to newMBB
11463 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011464
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011465 // newMBB jumps to itself and fall through to nextMBB
11466 newMBB->addSuccessor(nextMBB);
11467 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011468
Dale Johannesene4d209d2009-02-03 20:21:25 +000011469 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011470 // Insert instructions into newMBB based on incoming instruction
11471 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011472 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011473 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011474 MachineOperand& dest1Oper = bInstr->getOperand(0);
11475 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011476 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11477 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011478 argOpers[i] = &bInstr->getOperand(i+2);
11479
Dan Gohman71ea4e52010-05-14 21:01:44 +000011480 // We use some of the operands multiple times, so conservatively just
11481 // clear any kill flags that might be present.
11482 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11483 argOpers[i]->setIsKill(false);
11484 }
11485
Evan Chengad5b52f2010-01-08 19:14:57 +000011486 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011487 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011488
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011489 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011490 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011491 for (int i=0; i <= lastAddrIndx; ++i)
11492 (*MIB).addOperand(*argOpers[i]);
11493 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011494 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011495 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011496 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011497 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011498 MachineOperand newOp3 = *(argOpers[3]);
11499 if (newOp3.isImm())
11500 newOp3.setImm(newOp3.getImm()+4);
11501 else
11502 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011503 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011504 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011505
11506 // t3/4 are defined later, at the bottom of the loop
11507 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11508 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011509 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011510 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011511 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011512 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11513
Evan Cheng306b4ca2010-01-08 23:41:50 +000011514 // The subsequent operations should be using the destination registers of
11515 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011516 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011517 t1 = F->getRegInfo().createVirtualRegister(RC);
11518 t2 = F->getRegInfo().createVirtualRegister(RC);
11519 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11520 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011521 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011522 t1 = dest1Oper.getReg();
11523 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011524 }
11525
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011526 int valArgIndx = lastAddrIndx + 1;
11527 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011528 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011529 "invalid operand");
11530 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11531 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011532 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011533 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011534 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011535 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011536 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011537 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011538 (*MIB).addOperand(*argOpers[valArgIndx]);
11539 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011540 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011541 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011542 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011543 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011544 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011545 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011546 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011547 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011548 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011549 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011550
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011551 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011552 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011553 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011554 MIB.addReg(t2);
11555
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011556 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011557 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011558 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011559 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011560
Dale Johannesene4d209d2009-02-03 20:21:25 +000011561 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011562 for (int i=0; i <= lastAddrIndx; ++i)
11563 (*MIB).addOperand(*argOpers[i]);
11564
11565 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011566 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11567 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011568
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011569 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011570 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011571 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011572 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011573
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011574 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011575 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011576
Dan Gohman14152b42010-07-06 20:24:04 +000011577 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011578 return nextMBB;
11579}
11580
11581// private utility function
11582MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011583X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11584 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011585 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011586 // For the atomic min/max operator, we generate
11587 // thisMBB:
11588 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011589 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011590 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011591 // cmp t1, t2
11592 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011593 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011594 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11595 // bz newMBB
11596 // fallthrough -->nextMBB
11597 //
11598 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11599 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011600 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011601 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011602
Mon P Wang63307c32008-05-05 19:05:59 +000011603 /// First build the CFG
11604 MachineFunction *F = MBB->getParent();
11605 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011606 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11607 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11608 F->insert(MBBIter, newMBB);
11609 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011610
Dan Gohman14152b42010-07-06 20:24:04 +000011611 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11612 nextMBB->splice(nextMBB->begin(), thisMBB,
11613 llvm::next(MachineBasicBlock::iterator(mInstr)),
11614 thisMBB->end());
11615 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011616
Mon P Wang63307c32008-05-05 19:05:59 +000011617 // Update thisMBB to fall through to newMBB
11618 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011619
Mon P Wang63307c32008-05-05 19:05:59 +000011620 // newMBB jumps to newMBB and fall through to nextMBB
11621 newMBB->addSuccessor(nextMBB);
11622 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011623
Dale Johannesene4d209d2009-02-03 20:21:25 +000011624 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011625 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011626 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011627 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011628 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011629 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011630 int numArgs = mInstr->getNumOperands() - 1;
11631 for (int i=0; i < numArgs; ++i)
11632 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011633
Mon P Wang63307c32008-05-05 19:05:59 +000011634 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011635 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011636 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011637
Mon P Wangab3e7472008-05-05 22:56:23 +000011638 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011639 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011640 for (int i=0; i <= lastAddrIndx; ++i)
11641 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011642
Mon P Wang63307c32008-05-05 19:05:59 +000011643 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011644 assert((argOpers[valArgIndx]->isReg() ||
11645 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011646 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011647
11648 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011649 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011650 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011651 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011652 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011653 (*MIB).addOperand(*argOpers[valArgIndx]);
11654
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011655 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011656 MIB.addReg(t1);
11657
Dale Johannesene4d209d2009-02-03 20:21:25 +000011658 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011659 MIB.addReg(t1);
11660 MIB.addReg(t2);
11661
11662 // Generate movc
11663 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011664 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011665 MIB.addReg(t2);
11666 MIB.addReg(t1);
11667
11668 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011669 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011670 for (int i=0; i <= lastAddrIndx; ++i)
11671 (*MIB).addOperand(*argOpers[i]);
11672 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011673 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011674 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11675 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011676
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011677 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011678 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011679
Mon P Wang63307c32008-05-05 19:05:59 +000011680 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011681 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011682
Dan Gohman14152b42010-07-06 20:24:04 +000011683 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011684 return nextMBB;
11685}
11686
Eric Christopherf83a5de2009-08-27 18:08:16 +000011687// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011688// or XMM0_V32I8 in AVX all of this code can be replaced with that
11689// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011690MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011691X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011692 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000011693 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011694 "Target must have SSE4.2 or AVX features enabled");
11695
Eric Christopherb120ab42009-08-18 22:50:32 +000011696 DebugLoc dl = MI->getDebugLoc();
11697 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011698 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011699 if (!Subtarget->hasAVX()) {
11700 if (memArg)
11701 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11702 else
11703 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11704 } else {
11705 if (memArg)
11706 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11707 else
11708 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11709 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011710
Eric Christopher41c902f2010-11-30 08:20:21 +000011711 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011712 for (unsigned i = 0; i < numArgs; ++i) {
11713 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011714 if (!(Op.isReg() && Op.isImplicit()))
11715 MIB.addOperand(Op);
11716 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011717 BuildMI(*BB, MI, dl,
11718 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11719 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011720 .addReg(X86::XMM0);
11721
Dan Gohman14152b42010-07-06 20:24:04 +000011722 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011723 return BB;
11724}
11725
11726MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011727X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011728 DebugLoc dl = MI->getDebugLoc();
11729 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011730
Eric Christopher228232b2010-11-30 07:20:12 +000011731 // Address into RAX/EAX, other two args into ECX, EDX.
11732 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11733 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11734 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11735 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011736 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011737
Eric Christopher228232b2010-11-30 07:20:12 +000011738 unsigned ValOps = X86::AddrNumOperands;
11739 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11740 .addReg(MI->getOperand(ValOps).getReg());
11741 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11742 .addReg(MI->getOperand(ValOps+1).getReg());
11743
11744 // The instruction doesn't actually take any operands though.
11745 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011746
Eric Christopher228232b2010-11-30 07:20:12 +000011747 MI->eraseFromParent(); // The pseudo is gone now.
11748 return BB;
11749}
11750
11751MachineBasicBlock *
11752X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011753 DebugLoc dl = MI->getDebugLoc();
11754 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011755
Eric Christopher228232b2010-11-30 07:20:12 +000011756 // First arg in ECX, the second in EAX.
11757 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11758 .addReg(MI->getOperand(0).getReg());
11759 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11760 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011761
Eric Christopher228232b2010-11-30 07:20:12 +000011762 // The instruction doesn't actually take any operands though.
11763 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011764
Eric Christopher228232b2010-11-30 07:20:12 +000011765 MI->eraseFromParent(); // The pseudo is gone now.
11766 return BB;
11767}
11768
11769MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011770X86TargetLowering::EmitVAARG64WithCustomInserter(
11771 MachineInstr *MI,
11772 MachineBasicBlock *MBB) const {
11773 // Emit va_arg instruction on X86-64.
11774
11775 // Operands to this pseudo-instruction:
11776 // 0 ) Output : destination address (reg)
11777 // 1-5) Input : va_list address (addr, i64mem)
11778 // 6 ) ArgSize : Size (in bytes) of vararg type
11779 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11780 // 8 ) Align : Alignment of type
11781 // 9 ) EFLAGS (implicit-def)
11782
11783 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11784 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11785
11786 unsigned DestReg = MI->getOperand(0).getReg();
11787 MachineOperand &Base = MI->getOperand(1);
11788 MachineOperand &Scale = MI->getOperand(2);
11789 MachineOperand &Index = MI->getOperand(3);
11790 MachineOperand &Disp = MI->getOperand(4);
11791 MachineOperand &Segment = MI->getOperand(5);
11792 unsigned ArgSize = MI->getOperand(6).getImm();
11793 unsigned ArgMode = MI->getOperand(7).getImm();
11794 unsigned Align = MI->getOperand(8).getImm();
11795
11796 // Memory Reference
11797 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11798 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11799 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11800
11801 // Machine Information
11802 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11803 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11804 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11805 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11806 DebugLoc DL = MI->getDebugLoc();
11807
11808 // struct va_list {
11809 // i32 gp_offset
11810 // i32 fp_offset
11811 // i64 overflow_area (address)
11812 // i64 reg_save_area (address)
11813 // }
11814 // sizeof(va_list) = 24
11815 // alignment(va_list) = 8
11816
11817 unsigned TotalNumIntRegs = 6;
11818 unsigned TotalNumXMMRegs = 8;
11819 bool UseGPOffset = (ArgMode == 1);
11820 bool UseFPOffset = (ArgMode == 2);
11821 unsigned MaxOffset = TotalNumIntRegs * 8 +
11822 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11823
11824 /* Align ArgSize to a multiple of 8 */
11825 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11826 bool NeedsAlign = (Align > 8);
11827
11828 MachineBasicBlock *thisMBB = MBB;
11829 MachineBasicBlock *overflowMBB;
11830 MachineBasicBlock *offsetMBB;
11831 MachineBasicBlock *endMBB;
11832
11833 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11834 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11835 unsigned OffsetReg = 0;
11836
11837 if (!UseGPOffset && !UseFPOffset) {
11838 // If we only pull from the overflow region, we don't create a branch.
11839 // We don't need to alter control flow.
11840 OffsetDestReg = 0; // unused
11841 OverflowDestReg = DestReg;
11842
11843 offsetMBB = NULL;
11844 overflowMBB = thisMBB;
11845 endMBB = thisMBB;
11846 } else {
11847 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11848 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11849 // If not, pull from overflow_area. (branch to overflowMBB)
11850 //
11851 // thisMBB
11852 // | .
11853 // | .
11854 // offsetMBB overflowMBB
11855 // | .
11856 // | .
11857 // endMBB
11858
11859 // Registers for the PHI in endMBB
11860 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11861 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11862
11863 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11864 MachineFunction *MF = MBB->getParent();
11865 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11866 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11867 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11868
11869 MachineFunction::iterator MBBIter = MBB;
11870 ++MBBIter;
11871
11872 // Insert the new basic blocks
11873 MF->insert(MBBIter, offsetMBB);
11874 MF->insert(MBBIter, overflowMBB);
11875 MF->insert(MBBIter, endMBB);
11876
11877 // Transfer the remainder of MBB and its successor edges to endMBB.
11878 endMBB->splice(endMBB->begin(), thisMBB,
11879 llvm::next(MachineBasicBlock::iterator(MI)),
11880 thisMBB->end());
11881 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11882
11883 // Make offsetMBB and overflowMBB successors of thisMBB
11884 thisMBB->addSuccessor(offsetMBB);
11885 thisMBB->addSuccessor(overflowMBB);
11886
11887 // endMBB is a successor of both offsetMBB and overflowMBB
11888 offsetMBB->addSuccessor(endMBB);
11889 overflowMBB->addSuccessor(endMBB);
11890
11891 // Load the offset value into a register
11892 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11893 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11894 .addOperand(Base)
11895 .addOperand(Scale)
11896 .addOperand(Index)
11897 .addDisp(Disp, UseFPOffset ? 4 : 0)
11898 .addOperand(Segment)
11899 .setMemRefs(MMOBegin, MMOEnd);
11900
11901 // Check if there is enough room left to pull this argument.
11902 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11903 .addReg(OffsetReg)
11904 .addImm(MaxOffset + 8 - ArgSizeA8);
11905
11906 // Branch to "overflowMBB" if offset >= max
11907 // Fall through to "offsetMBB" otherwise
11908 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11909 .addMBB(overflowMBB);
11910 }
11911
11912 // In offsetMBB, emit code to use the reg_save_area.
11913 if (offsetMBB) {
11914 assert(OffsetReg != 0);
11915
11916 // Read the reg_save_area address.
11917 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11918 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11919 .addOperand(Base)
11920 .addOperand(Scale)
11921 .addOperand(Index)
11922 .addDisp(Disp, 16)
11923 .addOperand(Segment)
11924 .setMemRefs(MMOBegin, MMOEnd);
11925
11926 // Zero-extend the offset
11927 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11928 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11929 .addImm(0)
11930 .addReg(OffsetReg)
11931 .addImm(X86::sub_32bit);
11932
11933 // Add the offset to the reg_save_area to get the final address.
11934 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11935 .addReg(OffsetReg64)
11936 .addReg(RegSaveReg);
11937
11938 // Compute the offset for the next argument
11939 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11940 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11941 .addReg(OffsetReg)
11942 .addImm(UseFPOffset ? 16 : 8);
11943
11944 // Store it back into the va_list.
11945 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11946 .addOperand(Base)
11947 .addOperand(Scale)
11948 .addOperand(Index)
11949 .addDisp(Disp, UseFPOffset ? 4 : 0)
11950 .addOperand(Segment)
11951 .addReg(NextOffsetReg)
11952 .setMemRefs(MMOBegin, MMOEnd);
11953
11954 // Jump to endMBB
11955 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11956 .addMBB(endMBB);
11957 }
11958
11959 //
11960 // Emit code to use overflow area
11961 //
11962
11963 // Load the overflow_area address into a register.
11964 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11965 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11966 .addOperand(Base)
11967 .addOperand(Scale)
11968 .addOperand(Index)
11969 .addDisp(Disp, 8)
11970 .addOperand(Segment)
11971 .setMemRefs(MMOBegin, MMOEnd);
11972
11973 // If we need to align it, do so. Otherwise, just copy the address
11974 // to OverflowDestReg.
11975 if (NeedsAlign) {
11976 // Align the overflow address
11977 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11978 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11979
11980 // aligned_addr = (addr + (align-1)) & ~(align-1)
11981 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11982 .addReg(OverflowAddrReg)
11983 .addImm(Align-1);
11984
11985 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11986 .addReg(TmpReg)
11987 .addImm(~(uint64_t)(Align-1));
11988 } else {
11989 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11990 .addReg(OverflowAddrReg);
11991 }
11992
11993 // Compute the next overflow address after this argument.
11994 // (the overflow address should be kept 8-byte aligned)
11995 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11996 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11997 .addReg(OverflowDestReg)
11998 .addImm(ArgSizeA8);
11999
12000 // Store the new overflow address.
12001 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12002 .addOperand(Base)
12003 .addOperand(Scale)
12004 .addOperand(Index)
12005 .addDisp(Disp, 8)
12006 .addOperand(Segment)
12007 .addReg(NextAddrReg)
12008 .setMemRefs(MMOBegin, MMOEnd);
12009
12010 // If we branched, emit the PHI to the front of endMBB.
12011 if (offsetMBB) {
12012 BuildMI(*endMBB, endMBB->begin(), DL,
12013 TII->get(X86::PHI), DestReg)
12014 .addReg(OffsetDestReg).addMBB(offsetMBB)
12015 .addReg(OverflowDestReg).addMBB(overflowMBB);
12016 }
12017
12018 // Erase the pseudo instruction
12019 MI->eraseFromParent();
12020
12021 return endMBB;
12022}
12023
12024MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012025X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12026 MachineInstr *MI,
12027 MachineBasicBlock *MBB) const {
12028 // Emit code to save XMM registers to the stack. The ABI says that the
12029 // number of registers to save is given in %al, so it's theoretically
12030 // possible to do an indirect jump trick to avoid saving all of them,
12031 // however this code takes a simpler approach and just executes all
12032 // of the stores if %al is non-zero. It's less code, and it's probably
12033 // easier on the hardware branch predictor, and stores aren't all that
12034 // expensive anyway.
12035
12036 // Create the new basic blocks. One block contains all the XMM stores,
12037 // and one block is the final destination regardless of whether any
12038 // stores were performed.
12039 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12040 MachineFunction *F = MBB->getParent();
12041 MachineFunction::iterator MBBIter = MBB;
12042 ++MBBIter;
12043 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12044 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12045 F->insert(MBBIter, XMMSaveMBB);
12046 F->insert(MBBIter, EndMBB);
12047
Dan Gohman14152b42010-07-06 20:24:04 +000012048 // Transfer the remainder of MBB and its successor edges to EndMBB.
12049 EndMBB->splice(EndMBB->begin(), MBB,
12050 llvm::next(MachineBasicBlock::iterator(MI)),
12051 MBB->end());
12052 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12053
Dan Gohmand6708ea2009-08-15 01:38:56 +000012054 // The original block will now fall through to the XMM save block.
12055 MBB->addSuccessor(XMMSaveMBB);
12056 // The XMMSaveMBB will fall through to the end block.
12057 XMMSaveMBB->addSuccessor(EndMBB);
12058
12059 // Now add the instructions.
12060 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12061 DebugLoc DL = MI->getDebugLoc();
12062
12063 unsigned CountReg = MI->getOperand(0).getReg();
12064 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12065 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12066
12067 if (!Subtarget->isTargetWin64()) {
12068 // If %al is 0, branch around the XMM save block.
12069 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012070 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012071 MBB->addSuccessor(EndMBB);
12072 }
12073
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012074 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012075 // In the XMM save block, save all the XMM argument registers.
12076 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12077 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012078 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012079 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012080 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012081 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012082 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012083 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012084 .addFrameIndex(RegSaveFrameIndex)
12085 .addImm(/*Scale=*/1)
12086 .addReg(/*IndexReg=*/0)
12087 .addImm(/*Disp=*/Offset)
12088 .addReg(/*Segment=*/0)
12089 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012090 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012091 }
12092
Dan Gohman14152b42010-07-06 20:24:04 +000012093 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012094
12095 return EndMBB;
12096}
Mon P Wang63307c32008-05-05 19:05:59 +000012097
Evan Cheng60c07e12006-07-05 22:17:51 +000012098MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012099X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012100 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012101 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12102 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012103
Chris Lattner52600972009-09-02 05:57:00 +000012104 // To "insert" a SELECT_CC instruction, we actually have to insert the
12105 // diamond control-flow pattern. The incoming instruction knows the
12106 // destination vreg to set, the condition code register to branch on, the
12107 // true/false values to select between, and a branch opcode to use.
12108 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12109 MachineFunction::iterator It = BB;
12110 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012111
Chris Lattner52600972009-09-02 05:57:00 +000012112 // thisMBB:
12113 // ...
12114 // TrueVal = ...
12115 // cmpTY ccX, r1, r2
12116 // bCC copy1MBB
12117 // fallthrough --> copy0MBB
12118 MachineBasicBlock *thisMBB = BB;
12119 MachineFunction *F = BB->getParent();
12120 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12121 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012122 F->insert(It, copy0MBB);
12123 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012124
Bill Wendling730c07e2010-06-25 20:48:10 +000012125 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12126 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000012127 if (!MI->killsRegister(X86::EFLAGS)) {
12128 copy0MBB->addLiveIn(X86::EFLAGS);
12129 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012130 }
12131
Dan Gohman14152b42010-07-06 20:24:04 +000012132 // Transfer the remainder of BB and its successor edges to sinkMBB.
12133 sinkMBB->splice(sinkMBB->begin(), BB,
12134 llvm::next(MachineBasicBlock::iterator(MI)),
12135 BB->end());
12136 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12137
12138 // Add the true and fallthrough blocks as its successors.
12139 BB->addSuccessor(copy0MBB);
12140 BB->addSuccessor(sinkMBB);
12141
12142 // Create the conditional branch instruction.
12143 unsigned Opc =
12144 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12145 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12146
Chris Lattner52600972009-09-02 05:57:00 +000012147 // copy0MBB:
12148 // %FalseValue = ...
12149 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012150 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012151
Chris Lattner52600972009-09-02 05:57:00 +000012152 // sinkMBB:
12153 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12154 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012155 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12156 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012157 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12158 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12159
Dan Gohman14152b42010-07-06 20:24:04 +000012160 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012161 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012162}
12163
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012164MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012165X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12166 bool Is64Bit) const {
12167 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12168 DebugLoc DL = MI->getDebugLoc();
12169 MachineFunction *MF = BB->getParent();
12170 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12171
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012172 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012173
12174 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12175 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12176
12177 // BB:
12178 // ... [Till the alloca]
12179 // If stacklet is not large enough, jump to mallocMBB
12180 //
12181 // bumpMBB:
12182 // Allocate by subtracting from RSP
12183 // Jump to continueMBB
12184 //
12185 // mallocMBB:
12186 // Allocate by call to runtime
12187 //
12188 // continueMBB:
12189 // ...
12190 // [rest of original BB]
12191 //
12192
12193 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12194 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12195 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12196
12197 MachineRegisterInfo &MRI = MF->getRegInfo();
12198 const TargetRegisterClass *AddrRegClass =
12199 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12200
12201 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12202 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12203 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012204 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012205 sizeVReg = MI->getOperand(1).getReg(),
12206 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12207
12208 MachineFunction::iterator MBBIter = BB;
12209 ++MBBIter;
12210
12211 MF->insert(MBBIter, bumpMBB);
12212 MF->insert(MBBIter, mallocMBB);
12213 MF->insert(MBBIter, continueMBB);
12214
12215 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12216 (MachineBasicBlock::iterator(MI)), BB->end());
12217 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12218
12219 // Add code to the main basic block to check if the stack limit has been hit,
12220 // and if so, jump to mallocMBB otherwise to bumpMBB.
12221 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012222 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012223 .addReg(tmpSPVReg).addReg(sizeVReg);
12224 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000012225 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012226 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012227 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12228
12229 // bumpMBB simply decreases the stack pointer, since we know the current
12230 // stacklet has enough space.
12231 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012232 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012233 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012234 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012235 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12236
12237 // Calls into a routine in libgcc to allocate more space from the heap.
12238 if (Is64Bit) {
12239 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12240 .addReg(sizeVReg);
12241 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12242 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12243 } else {
12244 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12245 .addImm(12);
12246 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12247 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12248 .addExternalSymbol("__morestack_allocate_stack_space");
12249 }
12250
12251 if (!Is64Bit)
12252 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12253 .addImm(16);
12254
12255 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12256 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12257 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12258
12259 // Set up the CFG correctly.
12260 BB->addSuccessor(bumpMBB);
12261 BB->addSuccessor(mallocMBB);
12262 mallocMBB->addSuccessor(continueMBB);
12263 bumpMBB->addSuccessor(continueMBB);
12264
12265 // Take care of the PHI nodes.
12266 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12267 MI->getOperand(0).getReg())
12268 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12269 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12270
12271 // Delete the original pseudo instruction.
12272 MI->eraseFromParent();
12273
12274 // And we're done.
12275 return continueMBB;
12276}
12277
12278MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012279X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012280 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012281 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12282 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012283
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012284 assert(!Subtarget->isTargetEnvMacho());
12285
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012286 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12287 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012288
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012289 if (Subtarget->isTargetWin64()) {
12290 if (Subtarget->isTargetCygMing()) {
12291 // ___chkstk(Mingw64):
12292 // Clobbers R10, R11, RAX and EFLAGS.
12293 // Updates RSP.
12294 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12295 .addExternalSymbol("___chkstk")
12296 .addReg(X86::RAX, RegState::Implicit)
12297 .addReg(X86::RSP, RegState::Implicit)
12298 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12299 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12300 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12301 } else {
12302 // __chkstk(MSVCRT): does not update stack pointer.
12303 // Clobbers R10, R11 and EFLAGS.
12304 // FIXME: RAX(allocated size) might be reused and not killed.
12305 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12306 .addExternalSymbol("__chkstk")
12307 .addReg(X86::RAX, RegState::Implicit)
12308 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12309 // RAX has the offset to subtracted from RSP.
12310 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12311 .addReg(X86::RSP)
12312 .addReg(X86::RAX);
12313 }
12314 } else {
12315 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012316 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12317
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012318 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12319 .addExternalSymbol(StackProbeSymbol)
12320 .addReg(X86::EAX, RegState::Implicit)
12321 .addReg(X86::ESP, RegState::Implicit)
12322 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12323 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12324 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12325 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012326
Dan Gohman14152b42010-07-06 20:24:04 +000012327 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012328 return BB;
12329}
Chris Lattner52600972009-09-02 05:57:00 +000012330
12331MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012332X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12333 MachineBasicBlock *BB) const {
12334 // This is pretty easy. We're taking the value that we received from
12335 // our load from the relocation, sticking it in either RDI (x86-64)
12336 // or EAX and doing an indirect call. The return value will then
12337 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012338 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012339 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012340 DebugLoc DL = MI->getDebugLoc();
12341 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012342
12343 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012344 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012345
Eric Christopher30ef0e52010-06-03 04:07:48 +000012346 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012347 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12348 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012349 .addReg(X86::RIP)
12350 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012351 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012352 MI->getOperand(3).getTargetFlags())
12353 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012354 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012355 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012356 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012357 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12358 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012359 .addReg(0)
12360 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012361 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012362 MI->getOperand(3).getTargetFlags())
12363 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012364 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012365 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012366 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012367 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12368 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012369 .addReg(TII->getGlobalBaseReg(F))
12370 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012371 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012372 MI->getOperand(3).getTargetFlags())
12373 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012374 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012375 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012376 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012377
Dan Gohman14152b42010-07-06 20:24:04 +000012378 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012379 return BB;
12380}
12381
12382MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012383X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012384 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012385 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000012386 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012387 case X86::TAILJMPd64:
12388 case X86::TAILJMPr64:
12389 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000012390 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012391 case X86::TCRETURNdi64:
12392 case X86::TCRETURNri64:
12393 case X86::TCRETURNmi64:
12394 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12395 // On AMD64, additional defs should be added before register allocation.
12396 if (!Subtarget->isTargetWin64()) {
12397 MI->addRegisterDefined(X86::RSI);
12398 MI->addRegisterDefined(X86::RDI);
12399 MI->addRegisterDefined(X86::XMM6);
12400 MI->addRegisterDefined(X86::XMM7);
12401 MI->addRegisterDefined(X86::XMM8);
12402 MI->addRegisterDefined(X86::XMM9);
12403 MI->addRegisterDefined(X86::XMM10);
12404 MI->addRegisterDefined(X86::XMM11);
12405 MI->addRegisterDefined(X86::XMM12);
12406 MI->addRegisterDefined(X86::XMM13);
12407 MI->addRegisterDefined(X86::XMM14);
12408 MI->addRegisterDefined(X86::XMM15);
12409 }
12410 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012411 case X86::WIN_ALLOCA:
12412 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012413 case X86::SEG_ALLOCA_32:
12414 return EmitLoweredSegAlloca(MI, BB, false);
12415 case X86::SEG_ALLOCA_64:
12416 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012417 case X86::TLSCall_32:
12418 case X86::TLSCall_64:
12419 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012420 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012421 case X86::CMOV_FR32:
12422 case X86::CMOV_FR64:
12423 case X86::CMOV_V4F32:
12424 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012425 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012426 case X86::CMOV_V8F32:
12427 case X86::CMOV_V4F64:
12428 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012429 case X86::CMOV_GR16:
12430 case X86::CMOV_GR32:
12431 case X86::CMOV_RFP32:
12432 case X86::CMOV_RFP64:
12433 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012434 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012435
Dale Johannesen849f2142007-07-03 00:53:03 +000012436 case X86::FP32_TO_INT16_IN_MEM:
12437 case X86::FP32_TO_INT32_IN_MEM:
12438 case X86::FP32_TO_INT64_IN_MEM:
12439 case X86::FP64_TO_INT16_IN_MEM:
12440 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012441 case X86::FP64_TO_INT64_IN_MEM:
12442 case X86::FP80_TO_INT16_IN_MEM:
12443 case X86::FP80_TO_INT32_IN_MEM:
12444 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012445 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12446 DebugLoc DL = MI->getDebugLoc();
12447
Evan Cheng60c07e12006-07-05 22:17:51 +000012448 // Change the floating point control register to use "round towards zero"
12449 // mode when truncating to an integer value.
12450 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012451 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012452 addFrameReference(BuildMI(*BB, MI, DL,
12453 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012454
12455 // Load the old value of the high byte of the control word...
12456 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012457 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012458 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012459 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012460
12461 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012462 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012463 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012464
12465 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012466 addFrameReference(BuildMI(*BB, MI, DL,
12467 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012468
12469 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012470 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012471 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012472
12473 // Get the X86 opcode to use.
12474 unsigned Opc;
12475 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012476 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012477 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12478 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12479 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12480 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12481 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12482 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012483 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12484 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12485 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012486 }
12487
12488 X86AddressMode AM;
12489 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012490 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012491 AM.BaseType = X86AddressMode::RegBase;
12492 AM.Base.Reg = Op.getReg();
12493 } else {
12494 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012495 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012496 }
12497 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012498 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012499 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012500 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012501 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012502 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012503 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012504 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012505 AM.GV = Op.getGlobal();
12506 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012507 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012508 }
Dan Gohman14152b42010-07-06 20:24:04 +000012509 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012510 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012511
12512 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012513 addFrameReference(BuildMI(*BB, MI, DL,
12514 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012515
Dan Gohman14152b42010-07-06 20:24:04 +000012516 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012517 return BB;
12518 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012519 // String/text processing lowering.
12520 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012521 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012522 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12523 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012524 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012525 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12526 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012527 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012528 return EmitPCMP(MI, BB, 5, false /* in mem */);
12529 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012530 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012531 return EmitPCMP(MI, BB, 5, true /* in mem */);
12532
Eric Christopher228232b2010-11-30 07:20:12 +000012533 // Thread synchronization.
12534 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012535 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012536 case X86::MWAIT:
12537 return EmitMwait(MI, BB);
12538
Eric Christopherb120ab42009-08-18 22:50:32 +000012539 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012540 case X86::ATOMAND32:
12541 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012542 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012543 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012544 X86::NOT32r, X86::EAX,
12545 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012546 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012547 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12548 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012549 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012550 X86::NOT32r, X86::EAX,
12551 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012552 case X86::ATOMXOR32:
12553 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012554 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012555 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012556 X86::NOT32r, X86::EAX,
12557 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012558 case X86::ATOMNAND32:
12559 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012560 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012561 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012562 X86::NOT32r, X86::EAX,
12563 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012564 case X86::ATOMMIN32:
12565 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12566 case X86::ATOMMAX32:
12567 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12568 case X86::ATOMUMIN32:
12569 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12570 case X86::ATOMUMAX32:
12571 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012572
12573 case X86::ATOMAND16:
12574 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12575 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012576 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012577 X86::NOT16r, X86::AX,
12578 X86::GR16RegisterClass);
12579 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012580 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012581 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012582 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012583 X86::NOT16r, X86::AX,
12584 X86::GR16RegisterClass);
12585 case X86::ATOMXOR16:
12586 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12587 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012588 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012589 X86::NOT16r, X86::AX,
12590 X86::GR16RegisterClass);
12591 case X86::ATOMNAND16:
12592 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12593 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012594 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012595 X86::NOT16r, X86::AX,
12596 X86::GR16RegisterClass, true);
12597 case X86::ATOMMIN16:
12598 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12599 case X86::ATOMMAX16:
12600 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12601 case X86::ATOMUMIN16:
12602 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12603 case X86::ATOMUMAX16:
12604 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12605
12606 case X86::ATOMAND8:
12607 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12608 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012609 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012610 X86::NOT8r, X86::AL,
12611 X86::GR8RegisterClass);
12612 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012613 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012614 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012615 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012616 X86::NOT8r, X86::AL,
12617 X86::GR8RegisterClass);
12618 case X86::ATOMXOR8:
12619 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12620 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012621 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012622 X86::NOT8r, X86::AL,
12623 X86::GR8RegisterClass);
12624 case X86::ATOMNAND8:
12625 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12626 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012627 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012628 X86::NOT8r, X86::AL,
12629 X86::GR8RegisterClass, true);
12630 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012631 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012632 case X86::ATOMAND64:
12633 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012634 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012635 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012636 X86::NOT64r, X86::RAX,
12637 X86::GR64RegisterClass);
12638 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012639 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12640 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012641 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012642 X86::NOT64r, X86::RAX,
12643 X86::GR64RegisterClass);
12644 case X86::ATOMXOR64:
12645 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012646 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012647 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012648 X86::NOT64r, X86::RAX,
12649 X86::GR64RegisterClass);
12650 case X86::ATOMNAND64:
12651 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12652 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012653 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012654 X86::NOT64r, X86::RAX,
12655 X86::GR64RegisterClass, true);
12656 case X86::ATOMMIN64:
12657 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12658 case X86::ATOMMAX64:
12659 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12660 case X86::ATOMUMIN64:
12661 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12662 case X86::ATOMUMAX64:
12663 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012664
12665 // This group does 64-bit operations on a 32-bit host.
12666 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012667 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012668 X86::AND32rr, X86::AND32rr,
12669 X86::AND32ri, X86::AND32ri,
12670 false);
12671 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012672 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012673 X86::OR32rr, X86::OR32rr,
12674 X86::OR32ri, X86::OR32ri,
12675 false);
12676 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012677 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012678 X86::XOR32rr, X86::XOR32rr,
12679 X86::XOR32ri, X86::XOR32ri,
12680 false);
12681 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012682 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012683 X86::AND32rr, X86::AND32rr,
12684 X86::AND32ri, X86::AND32ri,
12685 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012686 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012687 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012688 X86::ADD32rr, X86::ADC32rr,
12689 X86::ADD32ri, X86::ADC32ri,
12690 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012691 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012692 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012693 X86::SUB32rr, X86::SBB32rr,
12694 X86::SUB32ri, X86::SBB32ri,
12695 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012696 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012697 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012698 X86::MOV32rr, X86::MOV32rr,
12699 X86::MOV32ri, X86::MOV32ri,
12700 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012701 case X86::VASTART_SAVE_XMM_REGS:
12702 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012703
12704 case X86::VAARG_64:
12705 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012706 }
12707}
12708
12709//===----------------------------------------------------------------------===//
12710// X86 Optimization Hooks
12711//===----------------------------------------------------------------------===//
12712
Dan Gohman475871a2008-07-27 21:46:04 +000012713void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012714 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012715 APInt &KnownZero,
12716 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012717 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012718 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012719 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012720 assert((Opc >= ISD::BUILTIN_OP_END ||
12721 Opc == ISD::INTRINSIC_WO_CHAIN ||
12722 Opc == ISD::INTRINSIC_W_CHAIN ||
12723 Opc == ISD::INTRINSIC_VOID) &&
12724 "Should use MaskedValueIsZero if you don't know whether Op"
12725 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012726
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012727 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012728 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012729 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012730 case X86ISD::ADD:
12731 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012732 case X86ISD::ADC:
12733 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012734 case X86ISD::SMUL:
12735 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012736 case X86ISD::INC:
12737 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012738 case X86ISD::OR:
12739 case X86ISD::XOR:
12740 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012741 // These nodes' second result is a boolean.
12742 if (Op.getResNo() == 0)
12743 break;
12744 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012745 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012746 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12747 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012748 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012749 case ISD::INTRINSIC_WO_CHAIN: {
12750 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12751 unsigned NumLoBits = 0;
12752 switch (IntId) {
12753 default: break;
12754 case Intrinsic::x86_sse_movmsk_ps:
12755 case Intrinsic::x86_avx_movmsk_ps_256:
12756 case Intrinsic::x86_sse2_movmsk_pd:
12757 case Intrinsic::x86_avx_movmsk_pd_256:
12758 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000012759 case Intrinsic::x86_sse2_pmovmskb_128:
12760 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000012761 // High bits of movmskp{s|d}, pmovmskb are known zero.
12762 switch (IntId) {
12763 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12764 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12765 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12766 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12767 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12768 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000012769 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012770 }
12771 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12772 Mask.getBitWidth() - NumLoBits);
12773 break;
12774 }
12775 }
12776 break;
12777 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012778 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012779}
Chris Lattner259e97c2006-01-31 19:43:35 +000012780
Owen Andersonbc146b02010-09-21 20:42:50 +000012781unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12782 unsigned Depth) const {
12783 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12784 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12785 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012786
Owen Andersonbc146b02010-09-21 20:42:50 +000012787 // Fallback case.
12788 return 1;
12789}
12790
Evan Cheng206ee9d2006-07-07 08:33:52 +000012791/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012792/// node is a GlobalAddress + offset.
12793bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012794 const GlobalValue* &GA,
12795 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012796 if (N->getOpcode() == X86ISD::Wrapper) {
12797 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012798 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012799 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012800 return true;
12801 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012802 }
Evan Chengad4196b2008-05-12 19:56:52 +000012803 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012804}
12805
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012806/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12807/// same as extracting the high 128-bit part of 256-bit vector and then
12808/// inserting the result into the low part of a new 256-bit vector
12809static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12810 EVT VT = SVOp->getValueType(0);
12811 int NumElems = VT.getVectorNumElements();
12812
12813 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12814 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12815 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12816 SVOp->getMaskElt(j) >= 0)
12817 return false;
12818
12819 return true;
12820}
12821
12822/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12823/// same as extracting the low 128-bit part of 256-bit vector and then
12824/// inserting the result into the high part of a new 256-bit vector
12825static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12826 EVT VT = SVOp->getValueType(0);
12827 int NumElems = VT.getVectorNumElements();
12828
12829 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12830 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12831 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12832 SVOp->getMaskElt(j) >= 0)
12833 return false;
12834
12835 return true;
12836}
12837
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012838/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12839static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000012840 TargetLowering::DAGCombinerInfo &DCI,
12841 bool HasAVX2) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012842 DebugLoc dl = N->getDebugLoc();
12843 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12844 SDValue V1 = SVOp->getOperand(0);
12845 SDValue V2 = SVOp->getOperand(1);
12846 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012847 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012848
12849 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12850 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12851 //
12852 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012853 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012854 // V UNDEF BUILD_VECTOR UNDEF
12855 // \ / \ /
12856 // CONCAT_VECTOR CONCAT_VECTOR
12857 // \ /
12858 // \ /
12859 // RESULT: V + zero extended
12860 //
12861 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12862 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12863 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12864 return SDValue();
12865
12866 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12867 return SDValue();
12868
12869 // To match the shuffle mask, the first half of the mask should
12870 // be exactly the first vector, and all the rest a splat with the
12871 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012872 for (int i = 0; i < NumElems/2; ++i)
12873 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12874 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12875 return SDValue();
12876
Chad Rosier3d1161e2012-01-03 21:05:52 +000012877 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
12878 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
12879 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
12880 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
12881 SDValue ResNode =
12882 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
12883 Ld->getMemoryVT(),
12884 Ld->getPointerInfo(),
12885 Ld->getAlignment(),
12886 false/*isVolatile*/, true/*ReadMem*/,
12887 false/*WriteMem*/);
12888 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
12889 }
12890
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012891 // Emit a zeroed vector and insert the desired subvector on its
12892 // first half.
Craig Topper12216172012-01-13 08:12:35 +000012893 SDValue Zeros = getZeroVector(VT, true /* HasSSE2 */, HasAVX2, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012894 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12895 DAG.getConstant(0, MVT::i32), DAG, dl);
12896 return DCI.CombineTo(N, InsV);
12897 }
12898
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012899 //===--------------------------------------------------------------------===//
12900 // Combine some shuffles into subvector extracts and inserts:
12901 //
12902
12903 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12904 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12905 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12906 DAG, dl);
12907 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12908 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12909 return DCI.CombineTo(N, InsV);
12910 }
12911
12912 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12913 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12914 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12915 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12916 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12917 return DCI.CombineTo(N, InsV);
12918 }
12919
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012920 return SDValue();
12921}
12922
12923/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012924static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012925 TargetLowering::DAGCombinerInfo &DCI,
12926 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012927 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012928 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012929
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012930 // Don't create instructions with illegal types after legalize types has run.
12931 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12932 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12933 return SDValue();
12934
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012935 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12936 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12937 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Craig Topper12216172012-01-13 08:12:35 +000012938 return PerformShuffleCombine256(N, DAG, DCI, Subtarget->hasAVX2());
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012939
12940 // Only handle 128 wide vector from here on.
12941 if (VT.getSizeInBits() != 128)
12942 return SDValue();
12943
12944 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12945 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12946 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012947 SmallVector<SDValue, 16> Elts;
12948 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012949 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012950
Nate Begemanfdea31a2010-03-24 20:49:50 +000012951 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012952}
Evan Chengd880b972008-05-09 21:53:03 +000012953
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012954/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12955/// generation and convert it from being a bunch of shuffles and extracts
12956/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012957static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12958 const TargetLowering &TLI) {
12959 SDValue InputVector = N->getOperand(0);
12960
12961 // Only operate on vectors of 4 elements, where the alternative shuffling
12962 // gets to be more expensive.
12963 if (InputVector.getValueType() != MVT::v4i32)
12964 return SDValue();
12965
12966 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12967 // single use which is a sign-extend or zero-extend, and all elements are
12968 // used.
12969 SmallVector<SDNode *, 4> Uses;
12970 unsigned ExtractedElements = 0;
12971 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12972 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12973 if (UI.getUse().getResNo() != InputVector.getResNo())
12974 return SDValue();
12975
12976 SDNode *Extract = *UI;
12977 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12978 return SDValue();
12979
12980 if (Extract->getValueType(0) != MVT::i32)
12981 return SDValue();
12982 if (!Extract->hasOneUse())
12983 return SDValue();
12984 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12985 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12986 return SDValue();
12987 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12988 return SDValue();
12989
12990 // Record which element was extracted.
12991 ExtractedElements |=
12992 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12993
12994 Uses.push_back(Extract);
12995 }
12996
12997 // If not all the elements were used, this may not be worthwhile.
12998 if (ExtractedElements != 15)
12999 return SDValue();
13000
13001 // Ok, we've now decided to do the transformation.
13002 DebugLoc dl = InputVector.getDebugLoc();
13003
13004 // Store the value to a temporary stack slot.
13005 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013006 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13007 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013008
13009 // Replace each use (extract) with a load of the appropriate element.
13010 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13011 UE = Uses.end(); UI != UE; ++UI) {
13012 SDNode *Extract = *UI;
13013
Nadav Rotem86694292011-05-17 08:31:57 +000013014 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013015 SDValue Idx = Extract->getOperand(1);
13016 unsigned EltSize =
13017 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
13018 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
13019 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
13020
Nadav Rotem86694292011-05-17 08:31:57 +000013021 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013022 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013023
13024 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013025 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013026 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013027 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013028
13029 // Replace the exact with the load.
13030 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13031 }
13032
13033 // The replacement was made in place; don't return anything.
13034 return SDValue();
13035}
13036
Duncan Sands6bcd2192011-09-17 16:49:39 +000013037/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13038/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013039static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000013040 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013041 const X86Subtarget *Subtarget) {
13042 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013043 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013044 // Get the LHS/RHS of the select.
13045 SDValue LHS = N->getOperand(1);
13046 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013047 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013048
Dan Gohman670e5392009-09-21 18:03:22 +000013049 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013050 // instructions match the semantics of the common C idiom x<y?x:y but not
13051 // x<=y?x:y, because of how they handle negative zero (which can be
13052 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013053 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13054 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000013055 (Subtarget->hasSSE2() ||
13056 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013057 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013058
Chris Lattner47b4ce82009-03-11 05:48:52 +000013059 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013060 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013061 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13062 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013063 switch (CC) {
13064 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013065 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013066 // Converting this to a min would handle NaNs incorrectly, and swapping
13067 // the operands would cause it to handle comparisons between positive
13068 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013069 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013070 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013071 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13072 break;
13073 std::swap(LHS, RHS);
13074 }
Dan Gohman670e5392009-09-21 18:03:22 +000013075 Opcode = X86ISD::FMIN;
13076 break;
13077 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013078 // Converting this to a min would handle comparisons between positive
13079 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013080 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013081 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13082 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013083 Opcode = X86ISD::FMIN;
13084 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013085 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013086 // Converting this to a min would handle both negative zeros and NaNs
13087 // incorrectly, but we can swap the operands to fix both.
13088 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013089 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013090 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013091 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013092 Opcode = X86ISD::FMIN;
13093 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013094
Dan Gohman670e5392009-09-21 18:03:22 +000013095 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013096 // Converting this to a max would handle comparisons between positive
13097 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013098 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013099 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013100 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013101 Opcode = X86ISD::FMAX;
13102 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013103 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013104 // Converting this to a max would handle NaNs incorrectly, and swapping
13105 // the operands would cause it to handle comparisons between positive
13106 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013107 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013108 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013109 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13110 break;
13111 std::swap(LHS, RHS);
13112 }
Dan Gohman670e5392009-09-21 18:03:22 +000013113 Opcode = X86ISD::FMAX;
13114 break;
13115 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013116 // Converting this to a max would handle both negative zeros and NaNs
13117 // incorrectly, but we can swap the operands to fix both.
13118 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013119 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013120 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013121 case ISD::SETGE:
13122 Opcode = X86ISD::FMAX;
13123 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013124 }
Dan Gohman670e5392009-09-21 18:03:22 +000013125 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013126 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13127 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013128 switch (CC) {
13129 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013130 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013131 // Converting this to a min would handle comparisons between positive
13132 // and negative zero incorrectly, and swapping the operands would
13133 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013134 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013135 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013136 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013137 break;
13138 std::swap(LHS, RHS);
13139 }
Dan Gohman670e5392009-09-21 18:03:22 +000013140 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013141 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013142 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013143 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013144 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013145 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13146 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013147 Opcode = X86ISD::FMIN;
13148 break;
13149 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013150 // Converting this to a min would handle both negative zeros and NaNs
13151 // incorrectly, but we can swap the operands to fix both.
13152 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013153 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013154 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013155 case ISD::SETGE:
13156 Opcode = X86ISD::FMIN;
13157 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013158
Dan Gohman670e5392009-09-21 18:03:22 +000013159 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013160 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013161 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013162 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013163 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013164 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013165 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013166 // Converting this to a max would handle comparisons between positive
13167 // and negative zero incorrectly, and swapping the operands would
13168 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013169 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013170 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013171 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013172 break;
13173 std::swap(LHS, RHS);
13174 }
Dan Gohman670e5392009-09-21 18:03:22 +000013175 Opcode = X86ISD::FMAX;
13176 break;
13177 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013178 // Converting this to a max would handle both negative zeros and NaNs
13179 // incorrectly, but we can swap the operands to fix both.
13180 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013181 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013182 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013183 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013184 Opcode = X86ISD::FMAX;
13185 break;
13186 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013187 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013188
Chris Lattner47b4ce82009-03-11 05:48:52 +000013189 if (Opcode)
13190 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013191 }
Eric Christopherfd179292009-08-27 18:07:15 +000013192
Chris Lattnerd1980a52009-03-12 06:52:53 +000013193 // If this is a select between two integer constants, try to do some
13194 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013195 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13196 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013197 // Don't do this for crazy integer types.
13198 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13199 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013200 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013201 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013202
Chris Lattnercee56e72009-03-13 05:53:31 +000013203 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013204 // Efficiently invertible.
13205 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13206 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13207 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13208 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013209 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013210 }
Eric Christopherfd179292009-08-27 18:07:15 +000013211
Chris Lattnerd1980a52009-03-12 06:52:53 +000013212 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013213 if (FalseC->getAPIntValue() == 0 &&
13214 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013215 if (NeedsCondInvert) // Invert the condition if needed.
13216 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13217 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013218
Chris Lattnerd1980a52009-03-12 06:52:53 +000013219 // Zero extend the condition if needed.
13220 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013221
Chris Lattnercee56e72009-03-13 05:53:31 +000013222 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013223 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013224 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013225 }
Eric Christopherfd179292009-08-27 18:07:15 +000013226
Chris Lattner97a29a52009-03-13 05:22:11 +000013227 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013228 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013229 if (NeedsCondInvert) // Invert the condition if needed.
13230 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13231 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013232
Chris Lattner97a29a52009-03-13 05:22:11 +000013233 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013234 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13235 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013236 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013237 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013238 }
Eric Christopherfd179292009-08-27 18:07:15 +000013239
Chris Lattnercee56e72009-03-13 05:53:31 +000013240 // Optimize cases that will turn into an LEA instruction. This requires
13241 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013242 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013243 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013244 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013245
Chris Lattnercee56e72009-03-13 05:53:31 +000013246 bool isFastMultiplier = false;
13247 if (Diff < 10) {
13248 switch ((unsigned char)Diff) {
13249 default: break;
13250 case 1: // result = add base, cond
13251 case 2: // result = lea base( , cond*2)
13252 case 3: // result = lea base(cond, cond*2)
13253 case 4: // result = lea base( , cond*4)
13254 case 5: // result = lea base(cond, cond*4)
13255 case 8: // result = lea base( , cond*8)
13256 case 9: // result = lea base(cond, cond*8)
13257 isFastMultiplier = true;
13258 break;
13259 }
13260 }
Eric Christopherfd179292009-08-27 18:07:15 +000013261
Chris Lattnercee56e72009-03-13 05:53:31 +000013262 if (isFastMultiplier) {
13263 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13264 if (NeedsCondInvert) // Invert the condition if needed.
13265 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13266 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013267
Chris Lattnercee56e72009-03-13 05:53:31 +000013268 // Zero extend the condition if needed.
13269 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13270 Cond);
13271 // Scale the condition by the difference.
13272 if (Diff != 1)
13273 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13274 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013275
Chris Lattnercee56e72009-03-13 05:53:31 +000013276 // Add the base if non-zero.
13277 if (FalseC->getAPIntValue() != 0)
13278 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13279 SDValue(FalseC, 0));
13280 return Cond;
13281 }
Eric Christopherfd179292009-08-27 18:07:15 +000013282 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013283 }
13284 }
Eric Christopherfd179292009-08-27 18:07:15 +000013285
Evan Cheng56f582d2012-01-04 01:41:39 +000013286 // Canonicalize max and min:
13287 // (x > y) ? x : y -> (x >= y) ? x : y
13288 // (x < y) ? x : y -> (x <= y) ? x : y
13289 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
13290 // the need for an extra compare
13291 // against zero. e.g.
13292 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
13293 // subl %esi, %edi
13294 // testl %edi, %edi
13295 // movl $0, %eax
13296 // cmovgl %edi, %eax
13297 // =>
13298 // xorl %eax, %eax
13299 // subl %esi, $edi
13300 // cmovsl %eax, %edi
13301 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
13302 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13303 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
13304 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
13305 switch (CC) {
13306 default: break;
13307 case ISD::SETLT:
13308 case ISD::SETGT: {
13309 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
13310 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
13311 Cond.getOperand(0), Cond.getOperand(1), NewCC);
13312 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
13313 }
13314 }
13315 }
13316
Nadav Rotemcc616562012-01-15 19:27:55 +000013317 // If we know that this node is legal then we know that it is going to be
13318 // matched by one of the SSE/AVX BLEND instructions. These instructions only
13319 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
13320 // to simplify previous instructions.
13321 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13322 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
13323 !DCI.isBeforeLegalize() &&
13324 TLI.isOperationLegal(ISD::VSELECT, VT)) {
13325 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
13326 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
13327 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
13328
13329 APInt KnownZero, KnownOne;
13330 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
13331 DCI.isBeforeLegalizeOps());
13332 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
13333 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
13334 DCI.CommitTargetLoweringOpt(TLO);
13335 }
13336
Dan Gohman475871a2008-07-27 21:46:04 +000013337 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013338}
13339
Chris Lattnerd1980a52009-03-12 06:52:53 +000013340/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13341static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13342 TargetLowering::DAGCombinerInfo &DCI) {
13343 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013344
Chris Lattnerd1980a52009-03-12 06:52:53 +000013345 // If the flag operand isn't dead, don't touch this CMOV.
13346 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13347 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013348
Evan Chengb5a55d92011-05-24 01:48:22 +000013349 SDValue FalseOp = N->getOperand(0);
13350 SDValue TrueOp = N->getOperand(1);
13351 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13352 SDValue Cond = N->getOperand(3);
13353 if (CC == X86::COND_E || CC == X86::COND_NE) {
13354 switch (Cond.getOpcode()) {
13355 default: break;
13356 case X86ISD::BSR:
13357 case X86ISD::BSF:
13358 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13359 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13360 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13361 }
13362 }
13363
Chris Lattnerd1980a52009-03-12 06:52:53 +000013364 // If this is a select between two integer constants, try to do some
13365 // optimizations. Note that the operands are ordered the opposite of SELECT
13366 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013367 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13368 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013369 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13370 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013371 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13372 CC = X86::GetOppositeBranchCondition(CC);
13373 std::swap(TrueC, FalseC);
13374 }
Eric Christopherfd179292009-08-27 18:07:15 +000013375
Chris Lattnerd1980a52009-03-12 06:52:53 +000013376 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013377 // This is efficient for any integer data type (including i8/i16) and
13378 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013379 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013380 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13381 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013382
Chris Lattnerd1980a52009-03-12 06:52:53 +000013383 // Zero extend the condition if needed.
13384 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013385
Chris Lattnerd1980a52009-03-12 06:52:53 +000013386 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13387 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013388 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013389 if (N->getNumValues() == 2) // Dead flag value?
13390 return DCI.CombineTo(N, Cond, SDValue());
13391 return Cond;
13392 }
Eric Christopherfd179292009-08-27 18:07:15 +000013393
Chris Lattnercee56e72009-03-13 05:53:31 +000013394 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13395 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013396 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
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 Lattner97a29a52009-03-13 05:22:11 +000013400 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013401 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13402 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013403 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13404 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013405
Chris Lattner97a29a52009-03-13 05:22:11 +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 cases that will turn into an LEA instruction. This requires
13412 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013413 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013414 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013415 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013416
Chris Lattnercee56e72009-03-13 05:53:31 +000013417 bool isFastMultiplier = false;
13418 if (Diff < 10) {
13419 switch ((unsigned char)Diff) {
13420 default: break;
13421 case 1: // result = add base, cond
13422 case 2: // result = lea base( , cond*2)
13423 case 3: // result = lea base(cond, cond*2)
13424 case 4: // result = lea base( , cond*4)
13425 case 5: // result = lea base(cond, cond*4)
13426 case 8: // result = lea base( , cond*8)
13427 case 9: // result = lea base(cond, cond*8)
13428 isFastMultiplier = true;
13429 break;
13430 }
13431 }
Eric Christopherfd179292009-08-27 18:07:15 +000013432
Chris Lattnercee56e72009-03-13 05:53:31 +000013433 if (isFastMultiplier) {
13434 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013435 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13436 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013437 // Zero extend the condition if needed.
13438 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13439 Cond);
13440 // Scale the condition by the difference.
13441 if (Diff != 1)
13442 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13443 DAG.getConstant(Diff, Cond.getValueType()));
13444
13445 // Add the base if non-zero.
13446 if (FalseC->getAPIntValue() != 0)
13447 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13448 SDValue(FalseC, 0));
13449 if (N->getNumValues() == 2) // Dead flag value?
13450 return DCI.CombineTo(N, Cond, SDValue());
13451 return Cond;
13452 }
Eric Christopherfd179292009-08-27 18:07:15 +000013453 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013454 }
13455 }
13456 return SDValue();
13457}
13458
13459
Evan Cheng0b0cd912009-03-28 05:57:29 +000013460/// PerformMulCombine - Optimize a single multiply with constant into two
13461/// in order to implement it with two cheaper instructions, e.g.
13462/// LEA + SHL, LEA + LEA.
13463static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13464 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013465 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13466 return SDValue();
13467
Owen Andersone50ed302009-08-10 22:56:29 +000013468 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013469 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013470 return SDValue();
13471
13472 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13473 if (!C)
13474 return SDValue();
13475 uint64_t MulAmt = C->getZExtValue();
13476 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13477 return SDValue();
13478
13479 uint64_t MulAmt1 = 0;
13480 uint64_t MulAmt2 = 0;
13481 if ((MulAmt % 9) == 0) {
13482 MulAmt1 = 9;
13483 MulAmt2 = MulAmt / 9;
13484 } else if ((MulAmt % 5) == 0) {
13485 MulAmt1 = 5;
13486 MulAmt2 = MulAmt / 5;
13487 } else if ((MulAmt % 3) == 0) {
13488 MulAmt1 = 3;
13489 MulAmt2 = MulAmt / 3;
13490 }
13491 if (MulAmt2 &&
13492 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13493 DebugLoc DL = N->getDebugLoc();
13494
13495 if (isPowerOf2_64(MulAmt2) &&
13496 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13497 // If second multiplifer is pow2, issue it first. We want the multiply by
13498 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13499 // is an add.
13500 std::swap(MulAmt1, MulAmt2);
13501
13502 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013503 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013504 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013505 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013506 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013507 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013508 DAG.getConstant(MulAmt1, VT));
13509
Eric Christopherfd179292009-08-27 18:07:15 +000013510 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013511 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013512 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013513 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013514 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013515 DAG.getConstant(MulAmt2, VT));
13516
13517 // Do not add new nodes to DAG combiner worklist.
13518 DCI.CombineTo(N, NewMul, false);
13519 }
13520 return SDValue();
13521}
13522
Evan Chengad9c0a32009-12-15 00:53:42 +000013523static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13524 SDValue N0 = N->getOperand(0);
13525 SDValue N1 = N->getOperand(1);
13526 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13527 EVT VT = N0.getValueType();
13528
13529 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13530 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013531 if (VT.isInteger() && !VT.isVector() &&
13532 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013533 N0.getOperand(1).getOpcode() == ISD::Constant) {
13534 SDValue N00 = N0.getOperand(0);
13535 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13536 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13537 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13538 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13539 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13540 APInt ShAmt = N1C->getAPIntValue();
13541 Mask = Mask.shl(ShAmt);
13542 if (Mask != 0)
13543 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13544 N00, DAG.getConstant(Mask, VT));
13545 }
13546 }
13547
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013548
13549 // Hardware support for vector shifts is sparse which makes us scalarize the
13550 // vector operations in many cases. Also, on sandybridge ADD is faster than
13551 // shl.
13552 // (shl V, 1) -> add V,V
13553 if (isSplatVector(N1.getNode())) {
13554 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13555 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13556 // We shift all of the values by one. In many cases we do not have
13557 // hardware support for this operation. This is better expressed as an ADD
13558 // of two values.
13559 if (N1C && (1 == N1C->getZExtValue())) {
13560 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13561 }
13562 }
13563
Evan Chengad9c0a32009-12-15 00:53:42 +000013564 return SDValue();
13565}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013566
Nate Begeman740ab032009-01-26 00:52:55 +000013567/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13568/// when possible.
13569static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13570 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013571 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013572 if (N->getOpcode() == ISD::SHL) {
13573 SDValue V = PerformSHLCombine(N, DAG);
13574 if (V.getNode()) return V;
13575 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013576
Nate Begeman740ab032009-01-26 00:52:55 +000013577 // On X86 with SSE2 support, we can transform this to a vector shift if
13578 // all elements are shifted by the same amount. We can't do this in legalize
13579 // because the a constant vector is typically transformed to a constant pool
13580 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000013581 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013582 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013583
Craig Topper7be5dfd2011-11-12 09:58:49 +000013584 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
13585 (!Subtarget->hasAVX2() ||
13586 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013587 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013588
Mon P Wang3becd092009-01-28 08:12:05 +000013589 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013590 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013591 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013592 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013593 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13594 unsigned NumElts = VT.getVectorNumElements();
13595 unsigned i = 0;
13596 for (; i != NumElts; ++i) {
13597 SDValue Arg = ShAmtOp.getOperand(i);
13598 if (Arg.getOpcode() == ISD::UNDEF) continue;
13599 BaseShAmt = Arg;
13600 break;
13601 }
Craig Topper37c26772012-01-17 04:44:50 +000013602 // Handle the case where the build_vector is all undef
13603 // FIXME: Should DAG allow this?
13604 if (i == NumElts)
13605 return SDValue();
13606
Mon P Wang3becd092009-01-28 08:12:05 +000013607 for (; i != NumElts; ++i) {
13608 SDValue Arg = ShAmtOp.getOperand(i);
13609 if (Arg.getOpcode() == ISD::UNDEF) continue;
13610 if (Arg != BaseShAmt) {
13611 return SDValue();
13612 }
13613 }
13614 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013615 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013616 SDValue InVec = ShAmtOp.getOperand(0);
13617 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13618 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13619 unsigned i = 0;
13620 for (; i != NumElts; ++i) {
13621 SDValue Arg = InVec.getOperand(i);
13622 if (Arg.getOpcode() == ISD::UNDEF) continue;
13623 BaseShAmt = Arg;
13624 break;
13625 }
13626 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13627 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013628 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013629 if (C->getZExtValue() == SplatIdx)
13630 BaseShAmt = InVec.getOperand(1);
13631 }
13632 }
13633 if (BaseShAmt.getNode() == 0)
13634 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13635 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013636 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013637 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013638
Mon P Wangefa42202009-09-03 19:56:25 +000013639 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013640 if (EltVT.bitsGT(MVT::i32))
13641 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13642 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013643 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013644
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013645 // The shift amount is identical so we can do a vector shift.
13646 SDValue ValOp = N->getOperand(0);
13647 switch (N->getOpcode()) {
13648 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013649 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013650 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000013651 switch (VT.getSimpleVT().SimpleTy) {
13652 default: return SDValue();
13653 case MVT::v2i64:
13654 case MVT::v4i32:
13655 case MVT::v8i16:
13656 case MVT::v4i64:
13657 case MVT::v8i32:
13658 case MVT::v16i16:
13659 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
13660 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013661 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000013662 switch (VT.getSimpleVT().SimpleTy) {
13663 default: return SDValue();
13664 case MVT::v4i32:
13665 case MVT::v8i16:
13666 case MVT::v8i32:
13667 case MVT::v16i16:
13668 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
13669 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013670 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000013671 switch (VT.getSimpleVT().SimpleTy) {
13672 default: return SDValue();
13673 case MVT::v2i64:
13674 case MVT::v4i32:
13675 case MVT::v8i16:
13676 case MVT::v4i64:
13677 case MVT::v8i32:
13678 case MVT::v16i16:
13679 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
13680 }
Nate Begeman740ab032009-01-26 00:52:55 +000013681 }
Nate Begeman740ab032009-01-26 00:52:55 +000013682}
13683
Nate Begemanb65c1752010-12-17 22:55:37 +000013684
Stuart Hastings865f0932011-06-03 23:53:54 +000013685// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13686// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13687// and friends. Likewise for OR -> CMPNEQSS.
13688static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13689 TargetLowering::DAGCombinerInfo &DCI,
13690 const X86Subtarget *Subtarget) {
13691 unsigned opcode;
13692
13693 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13694 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000013695 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013696 SDValue N0 = N->getOperand(0);
13697 SDValue N1 = N->getOperand(1);
13698 SDValue CMP0 = N0->getOperand(1);
13699 SDValue CMP1 = N1->getOperand(1);
13700 DebugLoc DL = N->getDebugLoc();
13701
13702 // The SETCCs should both refer to the same CMP.
13703 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13704 return SDValue();
13705
13706 SDValue CMP00 = CMP0->getOperand(0);
13707 SDValue CMP01 = CMP0->getOperand(1);
13708 EVT VT = CMP00.getValueType();
13709
13710 if (VT == MVT::f32 || VT == MVT::f64) {
13711 bool ExpectingFlags = false;
13712 // Check for any users that want flags:
13713 for (SDNode::use_iterator UI = N->use_begin(),
13714 UE = N->use_end();
13715 !ExpectingFlags && UI != UE; ++UI)
13716 switch (UI->getOpcode()) {
13717 default:
13718 case ISD::BR_CC:
13719 case ISD::BRCOND:
13720 case ISD::SELECT:
13721 ExpectingFlags = true;
13722 break;
13723 case ISD::CopyToReg:
13724 case ISD::SIGN_EXTEND:
13725 case ISD::ZERO_EXTEND:
13726 case ISD::ANY_EXTEND:
13727 break;
13728 }
13729
13730 if (!ExpectingFlags) {
13731 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13732 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13733
13734 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13735 X86::CondCode tmp = cc0;
13736 cc0 = cc1;
13737 cc1 = tmp;
13738 }
13739
13740 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13741 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13742 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13743 X86ISD::NodeType NTOperator = is64BitFP ?
13744 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13745 // FIXME: need symbolic constants for these magic numbers.
13746 // See X86ATTInstPrinter.cpp:printSSECC().
13747 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13748 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13749 DAG.getConstant(x86cc, MVT::i8));
13750 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13751 OnesOrZeroesF);
13752 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13753 DAG.getConstant(1, MVT::i32));
13754 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13755 return OneBitOfTruth;
13756 }
13757 }
13758 }
13759 }
13760 return SDValue();
13761}
13762
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013763/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13764/// so it can be folded inside ANDNP.
13765static bool CanFoldXORWithAllOnes(const SDNode *N) {
13766 EVT VT = N->getValueType(0);
13767
13768 // Match direct AllOnes for 128 and 256-bit vectors
13769 if (ISD::isBuildVectorAllOnes(N))
13770 return true;
13771
13772 // Look through a bit convert.
13773 if (N->getOpcode() == ISD::BITCAST)
13774 N = N->getOperand(0).getNode();
13775
13776 // Sometimes the operand may come from a insert_subvector building a 256-bit
13777 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013778 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013779 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13780 SDValue V1 = N->getOperand(0);
13781 SDValue V2 = N->getOperand(1);
13782
13783 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13784 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13785 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13786 ISD::isBuildVectorAllOnes(V2.getNode()))
13787 return true;
13788 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013789
13790 return false;
13791}
13792
Nate Begemanb65c1752010-12-17 22:55:37 +000013793static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13794 TargetLowering::DAGCombinerInfo &DCI,
13795 const X86Subtarget *Subtarget) {
13796 if (DCI.isBeforeLegalizeOps())
13797 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013798
Stuart Hastings865f0932011-06-03 23:53:54 +000013799 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13800 if (R.getNode())
13801 return R;
13802
Craig Topper54a11172011-10-14 07:06:56 +000013803 EVT VT = N->getValueType(0);
13804
Craig Topperb4c94572011-10-21 06:55:01 +000013805 // Create ANDN, BLSI, and BLSR instructions
13806 // BLSI is X & (-X)
13807 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013808 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13809 SDValue N0 = N->getOperand(0);
13810 SDValue N1 = N->getOperand(1);
13811 DebugLoc DL = N->getDebugLoc();
13812
13813 // Check LHS for not
13814 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13815 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13816 // Check RHS for not
13817 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13818 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13819
Craig Topperb4c94572011-10-21 06:55:01 +000013820 // Check LHS for neg
13821 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13822 isZero(N0.getOperand(0)))
13823 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13824
13825 // Check RHS for neg
13826 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13827 isZero(N1.getOperand(0)))
13828 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13829
13830 // Check LHS for X-1
13831 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13832 isAllOnes(N0.getOperand(1)))
13833 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13834
13835 // Check RHS for X-1
13836 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13837 isAllOnes(N1.getOperand(1)))
13838 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13839
Craig Topper54a11172011-10-14 07:06:56 +000013840 return SDValue();
13841 }
13842
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013843 // Want to form ANDNP nodes:
13844 // 1) In the hopes of then easily combining them with OR and AND nodes
13845 // to form PBLEND/PSIGN.
13846 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013847 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013848 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013849
Nate Begemanb65c1752010-12-17 22:55:37 +000013850 SDValue N0 = N->getOperand(0);
13851 SDValue N1 = N->getOperand(1);
13852 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013853
Nate Begemanb65c1752010-12-17 22:55:37 +000013854 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013855 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013856 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13857 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013858 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013859
13860 // Check RHS for vnot
13861 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013862 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13863 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013864 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013865
Nate Begemanb65c1752010-12-17 22:55:37 +000013866 return SDValue();
13867}
13868
Evan Cheng760d1942010-01-04 21:22:48 +000013869static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013870 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013871 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013872 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013873 return SDValue();
13874
Stuart Hastings865f0932011-06-03 23:53:54 +000013875 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13876 if (R.getNode())
13877 return R;
13878
Evan Cheng760d1942010-01-04 21:22:48 +000013879 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000013880
Evan Cheng760d1942010-01-04 21:22:48 +000013881 SDValue N0 = N->getOperand(0);
13882 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013883
Nate Begemanb65c1752010-12-17 22:55:37 +000013884 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000013885 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000013886 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000013887 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
13888 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013889
Craig Topper1666cb62011-11-19 07:07:26 +000013890 // Canonicalize pandn to RHS
13891 if (N0.getOpcode() == X86ISD::ANDNP)
13892 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000013893 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000013894 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
13895 SDValue Mask = N1.getOperand(0);
13896 SDValue X = N1.getOperand(1);
13897 SDValue Y;
13898 if (N0.getOperand(0) == Mask)
13899 Y = N0.getOperand(1);
13900 if (N0.getOperand(1) == Mask)
13901 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013902
Craig Topper1666cb62011-11-19 07:07:26 +000013903 // Check to see if the mask appeared in both the AND and ANDNP and
13904 if (!Y.getNode())
13905 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013906
Craig Topper1666cb62011-11-19 07:07:26 +000013907 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13908 if (Mask.getOpcode() != ISD::BITCAST ||
13909 X.getOpcode() != ISD::BITCAST ||
13910 Y.getOpcode() != ISD::BITCAST)
13911 return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013912
Craig Topper1666cb62011-11-19 07:07:26 +000013913 // Look through mask bitcast.
13914 Mask = Mask.getOperand(0);
13915 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013916
Craig Toppered2e13d2012-01-22 19:15:14 +000013917 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000013918 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13919 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000013920 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000013921 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000013922
13923 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000013924 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000013925 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13926 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13927 if ((SraAmt + 1) != EltBits)
13928 return SDValue();
13929
13930 DebugLoc DL = N->getDebugLoc();
13931
13932 // Now we know we at least have a plendvb with the mask val. See if
13933 // we can form a psignb/w/d.
13934 // psign = x.type == y.type == mask.type && y = sub(0, x);
13935 X = X.getOperand(0);
13936 Y = Y.getOperand(0);
13937 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13938 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000013939 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
13940 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
13941 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000013942 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000013943 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000013944 }
13945 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000013946 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000013947 return SDValue();
13948
13949 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
13950
13951 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
13952 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
13953 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000013954 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000013955 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000013956 }
13957 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013958
Craig Topper1666cb62011-11-19 07:07:26 +000013959 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
13960 return SDValue();
13961
Nate Begemanb65c1752010-12-17 22:55:37 +000013962 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013963 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13964 std::swap(N0, N1);
13965 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13966 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013967 if (!N0.hasOneUse() || !N1.hasOneUse())
13968 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013969
13970 SDValue ShAmt0 = N0.getOperand(1);
13971 if (ShAmt0.getValueType() != MVT::i8)
13972 return SDValue();
13973 SDValue ShAmt1 = N1.getOperand(1);
13974 if (ShAmt1.getValueType() != MVT::i8)
13975 return SDValue();
13976 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13977 ShAmt0 = ShAmt0.getOperand(0);
13978 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13979 ShAmt1 = ShAmt1.getOperand(0);
13980
13981 DebugLoc DL = N->getDebugLoc();
13982 unsigned Opc = X86ISD::SHLD;
13983 SDValue Op0 = N0.getOperand(0);
13984 SDValue Op1 = N1.getOperand(0);
13985 if (ShAmt0.getOpcode() == ISD::SUB) {
13986 Opc = X86ISD::SHRD;
13987 std::swap(Op0, Op1);
13988 std::swap(ShAmt0, ShAmt1);
13989 }
13990
Evan Cheng8b1190a2010-04-28 01:18:01 +000013991 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013992 if (ShAmt1.getOpcode() == ISD::SUB) {
13993 SDValue Sum = ShAmt1.getOperand(0);
13994 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013995 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13996 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13997 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13998 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013999 return DAG.getNode(Opc, DL, VT,
14000 Op0, Op1,
14001 DAG.getNode(ISD::TRUNCATE, DL,
14002 MVT::i8, ShAmt0));
14003 }
14004 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
14005 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
14006 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000014007 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000014008 return DAG.getNode(Opc, DL, VT,
14009 N0.getOperand(0), N1.getOperand(0),
14010 DAG.getNode(ISD::TRUNCATE, DL,
14011 MVT::i8, ShAmt0));
14012 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014013
Evan Cheng760d1942010-01-04 21:22:48 +000014014 return SDValue();
14015}
14016
Craig Topper3738ccd2011-12-27 06:27:23 +000014017// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000014018static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
14019 TargetLowering::DAGCombinerInfo &DCI,
14020 const X86Subtarget *Subtarget) {
14021 if (DCI.isBeforeLegalizeOps())
14022 return SDValue();
14023
14024 EVT VT = N->getValueType(0);
14025
14026 if (VT != MVT::i32 && VT != MVT::i64)
14027 return SDValue();
14028
Craig Topper3738ccd2011-12-27 06:27:23 +000014029 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
14030
Craig Topperb4c94572011-10-21 06:55:01 +000014031 // Create BLSMSK instructions by finding X ^ (X-1)
14032 SDValue N0 = N->getOperand(0);
14033 SDValue N1 = N->getOperand(1);
14034 DebugLoc DL = N->getDebugLoc();
14035
14036 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14037 isAllOnes(N0.getOperand(1)))
14038 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
14039
14040 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14041 isAllOnes(N1.getOperand(1)))
14042 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
14043
14044 return SDValue();
14045}
14046
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014047/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
14048static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
14049 const X86Subtarget *Subtarget) {
14050 LoadSDNode *Ld = cast<LoadSDNode>(N);
14051 EVT RegVT = Ld->getValueType(0);
14052 EVT MemVT = Ld->getMemoryVT();
14053 DebugLoc dl = Ld->getDebugLoc();
14054 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14055
14056 ISD::LoadExtType Ext = Ld->getExtensionType();
14057
Nadav Rotemca6f2962011-09-18 19:00:23 +000014058 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014059 // shuffle. We need SSE4 for the shuffles.
14060 // TODO: It is possible to support ZExt by zeroing the undef values
14061 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000014062 if (RegVT.isVector() && RegVT.isInteger() &&
14063 Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014064 assert(MemVT != RegVT && "Cannot extend to the same type");
14065 assert(MemVT.isVector() && "Must load a vector from memory");
14066
14067 unsigned NumElems = RegVT.getVectorNumElements();
14068 unsigned RegSz = RegVT.getSizeInBits();
14069 unsigned MemSz = MemVT.getSizeInBits();
14070 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000014071 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014072 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
14073
14074 // Attempt to load the original value using a single load op.
14075 // Find a scalar type which is equal to the loaded word size.
14076 MVT SclrLoadTy = MVT::i8;
14077 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14078 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14079 MVT Tp = (MVT::SimpleValueType)tp;
14080 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
14081 SclrLoadTy = Tp;
14082 break;
14083 }
14084 }
14085
14086 // Proceed if a load word is found.
14087 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
14088
14089 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
14090 RegSz/SclrLoadTy.getSizeInBits());
14091
14092 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
14093 RegSz/MemVT.getScalarType().getSizeInBits());
14094 // Can't shuffle using an illegal type.
14095 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14096
14097 // Perform a single load.
14098 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
14099 Ld->getBasePtr(),
14100 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014101 Ld->isNonTemporal(), Ld->isInvariant(),
14102 Ld->getAlignment());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014103
14104 // Insert the word loaded into a vector.
14105 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
14106 LoadUnitVecVT, ScalarLoad);
14107
14108 // Bitcast the loaded value to a vector of the original element type, in
14109 // the size of the target vector type.
Chad Rosierb90d2a92012-01-03 23:19:12 +000014110 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT,
14111 ScalarInVector);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014112 unsigned SizeRatio = RegSz/MemSz;
14113
14114 // Redistribute the loaded elements into the different locations.
14115 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14116 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
14117
14118 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
14119 DAG.getUNDEF(SlicedVec.getValueType()),
14120 ShuffleVec.data());
14121
14122 // Bitcast to the requested type.
14123 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
14124 // Replace the original load with the new sequence
14125 // and return the new chain.
14126 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
14127 return SDValue(ScalarLoad.getNode(), 1);
14128 }
14129
14130 return SDValue();
14131}
14132
Chris Lattner149a4e52008-02-22 02:09:43 +000014133/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014134static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000014135 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000014136 StoreSDNode *St = cast<StoreSDNode>(N);
14137 EVT VT = St->getValue().getValueType();
14138 EVT StVT = St->getMemoryVT();
14139 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000014140 SDValue StoredVal = St->getOperand(1);
14141 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14142
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014143 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000014144 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
14145 // 128-bit ones. If in the future the cost becomes only one memory access the
14146 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000014147 if (VT.getSizeInBits() == 256 &&
14148 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
14149 StoredVal.getNumOperands() == 2) {
14150
14151 SDValue Value0 = StoredVal.getOperand(0);
14152 SDValue Value1 = StoredVal.getOperand(1);
14153
14154 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
14155 SDValue Ptr0 = St->getBasePtr();
14156 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
14157
14158 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
14159 St->getPointerInfo(), St->isVolatile(),
14160 St->isNonTemporal(), St->getAlignment());
14161 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
14162 St->getPointerInfo(), St->isVolatile(),
14163 St->isNonTemporal(), St->getAlignment());
14164 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
14165 }
Nadav Rotem614061b2011-08-10 19:30:14 +000014166
14167 // Optimize trunc store (of multiple scalars) to shuffle and store.
14168 // First, pack all of the elements in one place. Next, store to memory
14169 // in fewer chunks.
14170 if (St->isTruncatingStore() && VT.isVector()) {
14171 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14172 unsigned NumElems = VT.getVectorNumElements();
14173 assert(StVT != VT && "Cannot truncate to the same type");
14174 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
14175 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
14176
14177 // From, To sizes and ElemCount must be pow of two
14178 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014179 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000014180 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014181 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014182
Nadav Rotem614061b2011-08-10 19:30:14 +000014183 unsigned SizeRatio = FromSz / ToSz;
14184
14185 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
14186
14187 // Create a type on which we perform the shuffle
14188 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
14189 StVT.getScalarType(), NumElems*SizeRatio);
14190
14191 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
14192
14193 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
14194 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14195 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
14196
14197 // Can't shuffle using an illegal type
14198 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14199
14200 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
14201 DAG.getUNDEF(WideVec.getValueType()),
14202 ShuffleVec.data());
14203 // At this point all of the data is stored at the bottom of the
14204 // register. We now need to save it to mem.
14205
14206 // Find the largest store unit
14207 MVT StoreType = MVT::i8;
14208 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14209 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14210 MVT Tp = (MVT::SimpleValueType)tp;
14211 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
14212 StoreType = Tp;
14213 }
14214
14215 // Bitcast the original vector into a vector of store-size units
14216 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
14217 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
14218 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
14219 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
14220 SmallVector<SDValue, 8> Chains;
14221 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
14222 TLI.getPointerTy());
14223 SDValue Ptr = St->getBasePtr();
14224
14225 // Perform one or more big stores into memory.
14226 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
14227 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14228 StoreType, ShuffWide,
14229 DAG.getIntPtrConstant(i));
14230 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14231 St->getPointerInfo(), St->isVolatile(),
14232 St->isNonTemporal(), St->getAlignment());
14233 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14234 Chains.push_back(Ch);
14235 }
14236
14237 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14238 Chains.size());
14239 }
14240
14241
Chris Lattner149a4e52008-02-22 02:09:43 +000014242 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14243 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014244 // A preferable solution to the general problem is to figure out the right
14245 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014246
14247 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014248 if (VT.getSizeInBits() != 64)
14249 return SDValue();
14250
Devang Patel578efa92009-06-05 21:57:13 +000014251 const Function *F = DAG.getMachineFunction().getFunction();
14252 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014253 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000014254 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000014255 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014256 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014257 isa<LoadSDNode>(St->getValue()) &&
14258 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14259 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014260 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014261 LoadSDNode *Ld = 0;
14262 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014263 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014264 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014265 // Must be a store of a load. We currently handle two cases: the load
14266 // is a direct child, and it's under an intervening TokenFactor. It is
14267 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014268 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014269 Ld = cast<LoadSDNode>(St->getChain());
14270 else if (St->getValue().hasOneUse() &&
14271 ChainVal->getOpcode() == ISD::TokenFactor) {
14272 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014273 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014274 TokenFactorIndex = i;
14275 Ld = cast<LoadSDNode>(St->getValue());
14276 } else
14277 Ops.push_back(ChainVal->getOperand(i));
14278 }
14279 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014280
Evan Cheng536e6672009-03-12 05:59:15 +000014281 if (!Ld || !ISD::isNormalLoad(Ld))
14282 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014283
Evan Cheng536e6672009-03-12 05:59:15 +000014284 // If this is not the MMX case, i.e. we are just turning i64 load/store
14285 // into f64 load/store, avoid the transformation if there are multiple
14286 // uses of the loaded value.
14287 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14288 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014289
Evan Cheng536e6672009-03-12 05:59:15 +000014290 DebugLoc LdDL = Ld->getDebugLoc();
14291 DebugLoc StDL = N->getDebugLoc();
14292 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14293 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14294 // pair instead.
14295 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014296 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014297 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14298 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014299 Ld->isNonTemporal(), Ld->isInvariant(),
14300 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014301 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014302 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014303 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014304 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014305 Ops.size());
14306 }
Evan Cheng536e6672009-03-12 05:59:15 +000014307 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014308 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014309 St->isVolatile(), St->isNonTemporal(),
14310 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014311 }
Evan Cheng536e6672009-03-12 05:59:15 +000014312
14313 // Otherwise, lower to two pairs of 32-bit loads / stores.
14314 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014315 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14316 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014317
Owen Anderson825b72b2009-08-11 20:47:22 +000014318 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014319 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014320 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014321 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014322 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014323 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014324 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014325 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014326 MinAlign(Ld->getAlignment(), 4));
14327
14328 SDValue NewChain = LoLd.getValue(1);
14329 if (TokenFactorIndex != -1) {
14330 Ops.push_back(LoLd);
14331 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014332 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014333 Ops.size());
14334 }
14335
14336 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014337 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14338 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014339
14340 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014341 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014342 St->isVolatile(), St->isNonTemporal(),
14343 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014344 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014345 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014346 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014347 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014348 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014349 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014350 }
Dan Gohman475871a2008-07-27 21:46:04 +000014351 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014352}
14353
Duncan Sands17470be2011-09-22 20:15:48 +000014354/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14355/// and return the operands for the horizontal operation in LHS and RHS. A
14356/// horizontal operation performs the binary operation on successive elements
14357/// of its first operand, then on successive elements of its second operand,
14358/// returning the resulting values in a vector. For example, if
14359/// A = < float a0, float a1, float a2, float a3 >
14360/// and
14361/// B = < float b0, float b1, float b2, float b3 >
14362/// then the result of doing a horizontal operation on A and B is
14363/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14364/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14365/// A horizontal-op B, for some already available A and B, and if so then LHS is
14366/// set to A, RHS to B, and the routine returns 'true'.
14367/// Note that the binary operation should have the property that if one of the
14368/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014369static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000014370 // Look for the following pattern: if
14371 // A = < float a0, float a1, float a2, float a3 >
14372 // B = < float b0, float b1, float b2, float b3 >
14373 // and
14374 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14375 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14376 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14377 // which is A horizontal-op B.
14378
14379 // At least one of the operands should be a vector shuffle.
14380 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14381 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14382 return false;
14383
14384 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000014385
14386 assert((VT.is128BitVector() || VT.is256BitVector()) &&
14387 "Unsupported vector type for horizontal add/sub");
14388
14389 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
14390 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000014391 unsigned NumElts = VT.getVectorNumElements();
14392 unsigned NumLanes = VT.getSizeInBits()/128;
14393 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000014394 assert((NumLaneElts % 2 == 0) &&
14395 "Vector type should have an even number of elements in each lane");
14396 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000014397
14398 // View LHS in the form
14399 // LHS = VECTOR_SHUFFLE A, B, LMask
14400 // If LHS is not a shuffle then pretend it is the shuffle
14401 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14402 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14403 // type VT.
14404 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000014405 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014406 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14407 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14408 A = LHS.getOperand(0);
14409 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14410 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000014411 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
14412 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000014413 } else {
14414 if (LHS.getOpcode() != ISD::UNDEF)
14415 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014416 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014417 LMask[i] = i;
14418 }
14419
14420 // Likewise, view RHS in the form
14421 // RHS = VECTOR_SHUFFLE C, D, RMask
14422 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000014423 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014424 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14425 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14426 C = RHS.getOperand(0);
14427 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14428 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000014429 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
14430 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000014431 } else {
14432 if (RHS.getOpcode() != ISD::UNDEF)
14433 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014434 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014435 RMask[i] = i;
14436 }
14437
14438 // Check that the shuffles are both shuffling the same vectors.
14439 if (!(A == C && B == D) && !(A == D && B == C))
14440 return false;
14441
14442 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14443 if (!A.getNode() && !B.getNode())
14444 return false;
14445
14446 // If A and B occur in reverse order in RHS, then "swap" them (which means
14447 // rewriting the mask).
14448 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000014449 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014450
14451 // At this point LHS and RHS are equivalent to
14452 // LHS = VECTOR_SHUFFLE A, B, LMask
14453 // RHS = VECTOR_SHUFFLE A, B, RMask
14454 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000014455 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000014456 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000014457
Craig Topperf8363302011-12-02 08:18:41 +000014458 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014459 if (LIdx < 0 || RIdx < 0 ||
14460 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
14461 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000014462 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000014463
Craig Topperf8363302011-12-02 08:18:41 +000014464 // Check that successive elements are being operated on. If not, this is
14465 // not a horizontal operation.
14466 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
14467 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000014468 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000014469 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000014470 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000014471 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000014472 }
14473
14474 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14475 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14476 return true;
14477}
14478
14479/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14480static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14481 const X86Subtarget *Subtarget) {
14482 EVT VT = N->getValueType(0);
14483 SDValue LHS = N->getOperand(0);
14484 SDValue RHS = N->getOperand(1);
14485
14486 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000014487 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000014488 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014489 isHorizontalBinOp(LHS, RHS, true))
14490 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14491 return SDValue();
14492}
14493
14494/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14495static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14496 const X86Subtarget *Subtarget) {
14497 EVT VT = N->getValueType(0);
14498 SDValue LHS = N->getOperand(0);
14499 SDValue RHS = N->getOperand(1);
14500
14501 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000014502 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000014503 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014504 isHorizontalBinOp(LHS, RHS, false))
14505 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14506 return SDValue();
14507}
14508
Chris Lattner6cf73262008-01-25 06:14:17 +000014509/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14510/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014511static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014512 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14513 // F[X]OR(0.0, x) -> x
14514 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014515 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14516 if (C->getValueAPF().isPosZero())
14517 return N->getOperand(1);
14518 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14519 if (C->getValueAPF().isPosZero())
14520 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014521 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014522}
14523
14524/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014525static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014526 // FAND(0.0, x) -> 0.0
14527 // FAND(x, 0.0) -> 0.0
14528 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14529 if (C->getValueAPF().isPosZero())
14530 return N->getOperand(0);
14531 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14532 if (C->getValueAPF().isPosZero())
14533 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014534 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014535}
14536
Dan Gohmane5af2d32009-01-29 01:59:02 +000014537static SDValue PerformBTCombine(SDNode *N,
14538 SelectionDAG &DAG,
14539 TargetLowering::DAGCombinerInfo &DCI) {
14540 // BT ignores high bits in the bit index operand.
14541 SDValue Op1 = N->getOperand(1);
14542 if (Op1.hasOneUse()) {
14543 unsigned BitWidth = Op1.getValueSizeInBits();
14544 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14545 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014546 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14547 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014548 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014549 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14550 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14551 DCI.CommitTargetLoweringOpt(TLO);
14552 }
14553 return SDValue();
14554}
Chris Lattner83e6c992006-10-04 06:57:07 +000014555
Eli Friedman7a5e5552009-06-07 06:52:44 +000014556static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14557 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014558 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014559 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014560 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014561 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014562 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014563 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014564 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014565 }
14566 return SDValue();
14567}
14568
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000014569static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
14570 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000014571 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14572 // (and (i32 x86isd::setcc_carry), 1)
14573 // This eliminates the zext. This transformation is necessary because
14574 // ISD::SETCC is always legalized to i8.
14575 DebugLoc dl = N->getDebugLoc();
14576 SDValue N0 = N->getOperand(0);
14577 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000014578 EVT OpVT = N0.getValueType();
14579
Evan Cheng2e489c42009-12-16 00:53:11 +000014580 if (N0.getOpcode() == ISD::AND &&
14581 N0.hasOneUse() &&
14582 N0.getOperand(0).hasOneUse()) {
14583 SDValue N00 = N0.getOperand(0);
14584 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14585 return SDValue();
14586 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14587 if (!C || C->getZExtValue() != 1)
14588 return SDValue();
14589 return DAG.getNode(ISD::AND, dl, VT,
14590 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14591 N00.getOperand(0), N00.getOperand(1)),
14592 DAG.getConstant(1, VT));
14593 }
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000014594 // Optimize vectors in AVX mode:
14595 //
14596 // v8i16 -> v8i32
14597 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
14598 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
14599 // Concat upper and lower parts.
14600 //
14601 // v4i32 -> v4i64
14602 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
14603 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
14604 // Concat upper and lower parts.
14605 //
14606 if (Subtarget->hasAVX()) {
14607
14608 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
14609 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
14610
14611 SDValue ZeroVec = getZeroVector(OpVT, Subtarget->hasSSE2(), Subtarget->hasAVX2(),
14612 DAG, dl);
14613 SDValue OpLo = getTargetShuffleNode(X86ISD::UNPCKL, dl, OpVT, N0, ZeroVec, DAG);
14614 SDValue OpHi = getTargetShuffleNode(X86ISD::UNPCKH, dl, OpVT, N0, ZeroVec, DAG);
14615
14616 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
14617 VT.getVectorNumElements()/2);
14618
14619 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
14620 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
14621
14622 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
14623 }
14624 }
14625
Evan Cheng2e489c42009-12-16 00:53:11 +000014626
14627 return SDValue();
14628}
14629
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014630// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14631static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14632 unsigned X86CC = N->getConstantOperandVal(0);
14633 SDValue EFLAG = N->getOperand(1);
14634 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014635
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014636 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14637 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14638 // cases.
14639 if (X86CC == X86::COND_B)
14640 return DAG.getNode(ISD::AND, DL, MVT::i8,
14641 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14642 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14643 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014644
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014645 return SDValue();
14646}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014647
Benjamin Kramer1396c402011-06-18 11:09:41 +000014648static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14649 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014650 SDValue Op0 = N->getOperand(0);
14651 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14652 // a 32-bit target where SSE doesn't support i64->FP operations.
14653 if (Op0.getOpcode() == ISD::LOAD) {
14654 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14655 EVT VT = Ld->getValueType(0);
14656 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14657 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14658 !XTLI->getSubtarget()->is64Bit() &&
14659 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014660 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14661 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014662 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14663 return FILDChain;
14664 }
14665 }
14666 return SDValue();
14667}
14668
Chris Lattner23a01992010-12-20 01:37:09 +000014669// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14670static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14671 X86TargetLowering::DAGCombinerInfo &DCI) {
14672 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14673 // the result is either zero or one (depending on the input carry bit).
14674 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14675 if (X86::isZeroNode(N->getOperand(0)) &&
14676 X86::isZeroNode(N->getOperand(1)) &&
14677 // We don't have a good way to replace an EFLAGS use, so only do this when
14678 // dead right now.
14679 SDValue(N, 1).use_empty()) {
14680 DebugLoc DL = N->getDebugLoc();
14681 EVT VT = N->getValueType(0);
14682 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14683 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14684 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14685 DAG.getConstant(X86::COND_B,MVT::i8),
14686 N->getOperand(2)),
14687 DAG.getConstant(1, VT));
14688 return DCI.CombineTo(N, Res1, CarryOut);
14689 }
14690
14691 return SDValue();
14692}
14693
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014694// fold (add Y, (sete X, 0)) -> adc 0, Y
14695// (add Y, (setne X, 0)) -> sbb -1, Y
14696// (sub (sete X, 0), Y) -> sbb 0, Y
14697// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014698static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014699 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014700
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014701 // Look through ZExts.
14702 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14703 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14704 return SDValue();
14705
14706 SDValue SetCC = Ext.getOperand(0);
14707 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14708 return SDValue();
14709
14710 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14711 if (CC != X86::COND_E && CC != X86::COND_NE)
14712 return SDValue();
14713
14714 SDValue Cmp = SetCC.getOperand(1);
14715 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014716 !X86::isZeroNode(Cmp.getOperand(1)) ||
14717 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014718 return SDValue();
14719
14720 SDValue CmpOp0 = Cmp.getOperand(0);
14721 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14722 DAG.getConstant(1, CmpOp0.getValueType()));
14723
14724 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14725 if (CC == X86::COND_NE)
14726 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14727 DL, OtherVal.getValueType(), OtherVal,
14728 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14729 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14730 DL, OtherVal.getValueType(), OtherVal,
14731 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14732}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014733
Craig Topper54f952a2011-11-19 09:02:40 +000014734/// PerformADDCombine - Do target-specific dag combines on integer adds.
14735static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
14736 const X86Subtarget *Subtarget) {
14737 EVT VT = N->getValueType(0);
14738 SDValue Op0 = N->getOperand(0);
14739 SDValue Op1 = N->getOperand(1);
14740
14741 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000014742 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000014743 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000014744 isHorizontalBinOp(Op0, Op1, true))
14745 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
14746
14747 return OptimizeConditionalInDecrement(N, DAG);
14748}
14749
14750static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
14751 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014752 SDValue Op0 = N->getOperand(0);
14753 SDValue Op1 = N->getOperand(1);
14754
14755 // X86 can't encode an immediate LHS of a sub. See if we can push the
14756 // negation into a preceding instruction.
14757 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014758 // If the RHS of the sub is a XOR with one use and a constant, invert the
14759 // immediate. Then add one to the LHS of the sub so we can turn
14760 // X-Y -> X+~Y+1, saving one register.
14761 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14762 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014763 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014764 EVT VT = Op0.getValueType();
14765 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14766 Op1.getOperand(0),
14767 DAG.getConstant(~XorC, VT));
14768 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014769 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014770 }
14771 }
14772
Craig Topper54f952a2011-11-19 09:02:40 +000014773 // Try to synthesize horizontal adds from adds of shuffles.
14774 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000014775 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000014776 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
14777 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000014778 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
14779
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014780 return OptimizeConditionalInDecrement(N, DAG);
14781}
14782
Dan Gohman475871a2008-07-27 21:46:04 +000014783SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014784 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014785 SelectionDAG &DAG = DCI.DAG;
14786 switch (N->getOpcode()) {
14787 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014788 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014789 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014790 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000014791 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014792 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Craig Topper54f952a2011-11-19 09:02:40 +000014793 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
14794 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000014795 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014796 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014797 case ISD::SHL:
14798 case ISD::SRA:
14799 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014800 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014801 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014802 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014803 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014804 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014805 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014806 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14807 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014808 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014809 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14810 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014811 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014812 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000014813 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, Subtarget);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014814 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Craig Topperb3982da2011-12-31 23:50:21 +000014815 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014816 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000014817 case X86ISD::UNPCKH:
14818 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014819 case X86ISD::MOVHLPS:
14820 case X86ISD::MOVLHPS:
14821 case X86ISD::PSHUFD:
14822 case X86ISD::PSHUFHW:
14823 case X86ISD::PSHUFLW:
14824 case X86ISD::MOVSS:
14825 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000014826 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000014827 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014828 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014829 }
14830
Dan Gohman475871a2008-07-27 21:46:04 +000014831 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014832}
14833
Evan Chenge5b51ac2010-04-17 06:13:15 +000014834/// isTypeDesirableForOp - Return true if the target has native support for
14835/// the specified value type and it is 'desirable' to use the type for the
14836/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14837/// instruction encodings are longer and some i16 instructions are slow.
14838bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14839 if (!isTypeLegal(VT))
14840 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014841 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014842 return true;
14843
14844 switch (Opc) {
14845 default:
14846 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014847 case ISD::LOAD:
14848 case ISD::SIGN_EXTEND:
14849 case ISD::ZERO_EXTEND:
14850 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014851 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014852 case ISD::SRL:
14853 case ISD::SUB:
14854 case ISD::ADD:
14855 case ISD::MUL:
14856 case ISD::AND:
14857 case ISD::OR:
14858 case ISD::XOR:
14859 return false;
14860 }
14861}
14862
14863/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014864/// beneficial for dag combiner to promote the specified node. If true, it
14865/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014866bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014867 EVT VT = Op.getValueType();
14868 if (VT != MVT::i16)
14869 return false;
14870
Evan Cheng4c26e932010-04-19 19:29:22 +000014871 bool Promote = false;
14872 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014873 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014874 default: break;
14875 case ISD::LOAD: {
14876 LoadSDNode *LD = cast<LoadSDNode>(Op);
14877 // If the non-extending load has a single use and it's not live out, then it
14878 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014879 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14880 Op.hasOneUse()*/) {
14881 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14882 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14883 // The only case where we'd want to promote LOAD (rather then it being
14884 // promoted as an operand is when it's only use is liveout.
14885 if (UI->getOpcode() != ISD::CopyToReg)
14886 return false;
14887 }
14888 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014889 Promote = true;
14890 break;
14891 }
14892 case ISD::SIGN_EXTEND:
14893 case ISD::ZERO_EXTEND:
14894 case ISD::ANY_EXTEND:
14895 Promote = true;
14896 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014897 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014898 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014899 SDValue N0 = Op.getOperand(0);
14900 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014901 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014902 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014903 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014904 break;
14905 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014906 case ISD::ADD:
14907 case ISD::MUL:
14908 case ISD::AND:
14909 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014910 case ISD::XOR:
14911 Commute = true;
14912 // fallthrough
14913 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014914 SDValue N0 = Op.getOperand(0);
14915 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014916 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014917 return false;
14918 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014919 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014920 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014921 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014922 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014923 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014924 }
14925 }
14926
14927 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014928 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014929}
14930
Evan Cheng60c07e12006-07-05 22:17:51 +000014931//===----------------------------------------------------------------------===//
14932// X86 Inline Assembly Support
14933//===----------------------------------------------------------------------===//
14934
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000014935namespace {
14936 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000014937 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000014938 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014939
Benjamin Kramer0581ed72011-12-18 20:51:31 +000014940 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000014941 StringRef piece(*args[i]);
14942 if (!s.startswith(piece)) // Check if the piece matches.
14943 return false;
14944
14945 s = s.substr(piece.size());
14946 StringRef::size_type pos = s.find_first_not_of(" \t");
14947 if (pos == 0) // We matched a prefix.
14948 return false;
14949
14950 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014951 }
14952
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000014953 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014954 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000014955 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014956}
14957
Chris Lattnerb8105652009-07-20 17:51:36 +000014958bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14959 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014960
14961 std::string AsmStr = IA->getAsmString();
14962
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014963 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
14964 if (!Ty || Ty->getBitWidth() % 16 != 0)
14965 return false;
14966
Chris Lattnerb8105652009-07-20 17:51:36 +000014967 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014968 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014969 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014970
14971 switch (AsmPieces.size()) {
14972 default: return false;
14973 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014974 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014975 // we will turn this bswap into something that will be lowered to logical
14976 // ops instead of emitting the bswap asm. For now, we don't support 486 or
14977 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014978 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000014979 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
14980 matchAsm(AsmPieces[0], "bswapl", "$0") ||
14981 matchAsm(AsmPieces[0], "bswapq", "$0") ||
14982 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
14983 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
14984 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000014985 // No need to check constraints, nothing other than the equivalent of
14986 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000014987 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014988 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014989
Chris Lattnerb8105652009-07-20 17:51:36 +000014990 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014991 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014992 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000014993 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
14994 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000014995 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014996 const std::string &ConstraintsStr = IA->getConstraintString();
14997 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014998 std::sort(AsmPieces.begin(), AsmPieces.end());
14999 if (AsmPieces.size() == 4 &&
15000 AsmPieces[0] == "~{cc}" &&
15001 AsmPieces[1] == "~{dirflag}" &&
15002 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015003 AsmPieces[3] == "~{fpsr}")
15004 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000015005 }
15006 break;
15007 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000015008 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015009 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015010 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
15011 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
15012 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015013 AsmPieces.clear();
15014 const std::string &ConstraintsStr = IA->getConstraintString();
15015 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
15016 std::sort(AsmPieces.begin(), AsmPieces.end());
15017 if (AsmPieces.size() == 4 &&
15018 AsmPieces[0] == "~{cc}" &&
15019 AsmPieces[1] == "~{dirflag}" &&
15020 AsmPieces[2] == "~{flags}" &&
15021 AsmPieces[3] == "~{fpsr}")
15022 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000015023 }
Evan Cheng55d42002011-01-08 01:24:27 +000015024
15025 if (CI->getType()->isIntegerTy(64)) {
15026 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
15027 if (Constraints.size() >= 2 &&
15028 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
15029 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
15030 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015031 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
15032 matchAsm(AsmPieces[1], "bswap", "%edx") &&
15033 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015034 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000015035 }
15036 }
15037 break;
15038 }
15039 return false;
15040}
15041
15042
15043
Chris Lattnerf4dff842006-07-11 02:54:03 +000015044/// getConstraintType - Given a constraint letter, return the type of
15045/// constraint it is for this target.
15046X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000015047X86TargetLowering::getConstraintType(const std::string &Constraint) const {
15048 if (Constraint.size() == 1) {
15049 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000015050 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000015051 case 'q':
15052 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000015053 case 'f':
15054 case 't':
15055 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000015056 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000015057 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000015058 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000015059 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000015060 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000015061 case 'a':
15062 case 'b':
15063 case 'c':
15064 case 'd':
15065 case 'S':
15066 case 'D':
15067 case 'A':
15068 return C_Register;
15069 case 'I':
15070 case 'J':
15071 case 'K':
15072 case 'L':
15073 case 'M':
15074 case 'N':
15075 case 'G':
15076 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000015077 case 'e':
15078 case 'Z':
15079 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000015080 default:
15081 break;
15082 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000015083 }
Chris Lattner4234f572007-03-25 02:14:49 +000015084 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000015085}
15086
John Thompson44ab89e2010-10-29 17:29:13 +000015087/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000015088/// This object must already have been set up with the operand type
15089/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000015090TargetLowering::ConstraintWeight
15091 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000015092 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000015093 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015094 Value *CallOperandVal = info.CallOperandVal;
15095 // If we don't have a value, we can't do a match,
15096 // but allow it at the lowest weight.
15097 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000015098 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015099 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000015100 // Look at the constraint type.
15101 switch (*constraint) {
15102 default:
John Thompson44ab89e2010-10-29 17:29:13 +000015103 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
15104 case 'R':
15105 case 'q':
15106 case 'Q':
15107 case 'a':
15108 case 'b':
15109 case 'c':
15110 case 'd':
15111 case 'S':
15112 case 'D':
15113 case 'A':
15114 if (CallOperandVal->getType()->isIntegerTy())
15115 weight = CW_SpecificReg;
15116 break;
15117 case 'f':
15118 case 't':
15119 case 'u':
15120 if (type->isFloatingPointTy())
15121 weight = CW_SpecificReg;
15122 break;
15123 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000015124 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000015125 weight = CW_SpecificReg;
15126 break;
15127 case 'x':
15128 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000015129 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000015130 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000015131 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015132 break;
15133 case 'I':
15134 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
15135 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000015136 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015137 }
15138 break;
John Thompson44ab89e2010-10-29 17:29:13 +000015139 case 'J':
15140 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15141 if (C->getZExtValue() <= 63)
15142 weight = CW_Constant;
15143 }
15144 break;
15145 case 'K':
15146 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15147 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
15148 weight = CW_Constant;
15149 }
15150 break;
15151 case 'L':
15152 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15153 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
15154 weight = CW_Constant;
15155 }
15156 break;
15157 case 'M':
15158 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15159 if (C->getZExtValue() <= 3)
15160 weight = CW_Constant;
15161 }
15162 break;
15163 case 'N':
15164 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15165 if (C->getZExtValue() <= 0xff)
15166 weight = CW_Constant;
15167 }
15168 break;
15169 case 'G':
15170 case 'C':
15171 if (dyn_cast<ConstantFP>(CallOperandVal)) {
15172 weight = CW_Constant;
15173 }
15174 break;
15175 case 'e':
15176 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15177 if ((C->getSExtValue() >= -0x80000000LL) &&
15178 (C->getSExtValue() <= 0x7fffffffLL))
15179 weight = CW_Constant;
15180 }
15181 break;
15182 case 'Z':
15183 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15184 if (C->getZExtValue() <= 0xffffffff)
15185 weight = CW_Constant;
15186 }
15187 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015188 }
15189 return weight;
15190}
15191
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015192/// LowerXConstraint - try to replace an X constraint, which matches anything,
15193/// with another that has more specific requirements based on the type of the
15194/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000015195const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000015196LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000015197 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
15198 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000015199 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000015200 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000015201 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000015202 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000015203 return "x";
15204 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015205
Chris Lattner5e764232008-04-26 23:02:14 +000015206 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015207}
15208
Chris Lattner48884cd2007-08-25 00:47:38 +000015209/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
15210/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000015211void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000015212 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000015213 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000015214 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000015215 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000015216
Eric Christopher100c8332011-06-02 23:16:42 +000015217 // Only support length 1 constraints for now.
15218 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000015219
Eric Christopher100c8332011-06-02 23:16:42 +000015220 char ConstraintLetter = Constraint[0];
15221 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015222 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000015223 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000015224 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015225 if (C->getZExtValue() <= 31) {
15226 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015227 break;
15228 }
Devang Patel84f7fd22007-03-17 00:13:28 +000015229 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015230 return;
Evan Cheng364091e2008-09-22 23:57:37 +000015231 case 'J':
15232 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015233 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000015234 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15235 break;
15236 }
15237 }
15238 return;
15239 case 'K':
15240 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015241 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000015242 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15243 break;
15244 }
15245 }
15246 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000015247 case 'N':
15248 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015249 if (C->getZExtValue() <= 255) {
15250 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015251 break;
15252 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000015253 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015254 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000015255 case 'e': {
15256 // 32-bit signed value
15257 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015258 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15259 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015260 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015261 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000015262 break;
15263 }
15264 // FIXME gcc accepts some relocatable values here too, but only in certain
15265 // memory models; it's complicated.
15266 }
15267 return;
15268 }
15269 case 'Z': {
15270 // 32-bit unsigned value
15271 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015272 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15273 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015274 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15275 break;
15276 }
15277 }
15278 // FIXME gcc accepts some relocatable values here too, but only in certain
15279 // memory models; it's complicated.
15280 return;
15281 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015282 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015283 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000015284 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015285 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015286 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000015287 break;
15288 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015289
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015290 // In any sort of PIC mode addresses need to be computed at runtime by
15291 // adding in a register or some sort of table lookup. These can't
15292 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000015293 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015294 return;
15295
Chris Lattnerdc43a882007-05-03 16:52:29 +000015296 // If we are in non-pic codegen mode, we allow the address of a global (with
15297 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000015298 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015299 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000015300
Chris Lattner49921962009-05-08 18:23:14 +000015301 // Match either (GA), (GA+C), (GA+C1+C2), etc.
15302 while (1) {
15303 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15304 Offset += GA->getOffset();
15305 break;
15306 } else if (Op.getOpcode() == ISD::ADD) {
15307 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15308 Offset += C->getZExtValue();
15309 Op = Op.getOperand(0);
15310 continue;
15311 }
15312 } else if (Op.getOpcode() == ISD::SUB) {
15313 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15314 Offset += -C->getZExtValue();
15315 Op = Op.getOperand(0);
15316 continue;
15317 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015318 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015319
Chris Lattner49921962009-05-08 18:23:14 +000015320 // Otherwise, this isn't something we can handle, reject it.
15321 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015322 }
Eric Christopherfd179292009-08-27 18:07:15 +000015323
Dan Gohman46510a72010-04-15 01:51:59 +000015324 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015325 // If we require an extra load to get this address, as in PIC mode, we
15326 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015327 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15328 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015329 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015330
Devang Patel0d881da2010-07-06 22:08:15 +000015331 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15332 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015333 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015334 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015335 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015336
Gabor Greifba36cb52008-08-28 21:40:38 +000015337 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015338 Ops.push_back(Result);
15339 return;
15340 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015341 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015342}
15343
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015344std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015345X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015346 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015347 // First, see if this is a constraint that directly corresponds to an LLVM
15348 // register class.
15349 if (Constraint.size() == 1) {
15350 // GCC Constraint Letters
15351 switch (Constraint[0]) {
15352 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015353 // TODO: Slight differences here in allocation order and leaving
15354 // RIP in the class. Do they matter any more here than they do
15355 // in the normal allocation?
15356 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15357 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015358 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015359 return std::make_pair(0U, X86::GR32RegisterClass);
15360 else if (VT == MVT::i16)
15361 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015362 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015363 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015364 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015365 return std::make_pair(0U, X86::GR64RegisterClass);
15366 break;
15367 }
15368 // 32-bit fallthrough
15369 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015370 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015371 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
15372 else if (VT == MVT::i16)
15373 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015374 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015375 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
15376 else if (VT == MVT::i64)
15377 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15378 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015379 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015380 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015381 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015382 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015383 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015384 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015385 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015386 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015387 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015388 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015389 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015390 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15391 if (VT == MVT::i16)
15392 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15393 if (VT == MVT::i32 || !Subtarget->is64Bit())
15394 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15395 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015396 case 'f': // FP Stack registers.
15397 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15398 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015399 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015400 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015401 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015402 return std::make_pair(0U, X86::RFP64RegisterClass);
15403 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015404 case 'y': // MMX_REGS if MMX allowed.
15405 if (!Subtarget->hasMMX()) break;
15406 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015407 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000015408 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015409 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000015410 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000015411 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015412
Owen Anderson825b72b2009-08-11 20:47:22 +000015413 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015414 default: break;
15415 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015416 case MVT::f32:
15417 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015418 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015419 case MVT::f64:
15420 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015421 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015422 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015423 case MVT::v16i8:
15424 case MVT::v8i16:
15425 case MVT::v4i32:
15426 case MVT::v2i64:
15427 case MVT::v4f32:
15428 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015429 return std::make_pair(0U, X86::VR128RegisterClass);
Eric Christopher55487552012-01-07 01:02:09 +000015430 // AVX types.
15431 case MVT::v32i8:
15432 case MVT::v16i16:
15433 case MVT::v8i32:
15434 case MVT::v4i64:
15435 case MVT::v8f32:
15436 case MVT::v4f64:
15437 return std::make_pair(0U, X86::VR256RegisterClass);
15438
Chris Lattner0f65cad2007-04-09 05:49:22 +000015439 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015440 break;
15441 }
15442 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015443
Chris Lattnerf76d1802006-07-31 23:26:50 +000015444 // Use the default implementation in TargetLowering to convert the register
15445 // constraint into a member of a register class.
15446 std::pair<unsigned, const TargetRegisterClass*> Res;
15447 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015448
15449 // Not found as a standard register?
15450 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015451 // Map st(0) -> st(7) -> ST0
15452 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15453 tolower(Constraint[1]) == 's' &&
15454 tolower(Constraint[2]) == 't' &&
15455 Constraint[3] == '(' &&
15456 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15457 Constraint[5] == ')' &&
15458 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015459
Chris Lattner56d77c72009-09-13 22:41:48 +000015460 Res.first = X86::ST0+Constraint[4]-'0';
15461 Res.second = X86::RFP80RegisterClass;
15462 return Res;
15463 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015464
Chris Lattner56d77c72009-09-13 22:41:48 +000015465 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015466 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015467 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015468 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015469 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015470 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015471
15472 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015473 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015474 Res.first = X86::EFLAGS;
15475 Res.second = X86::CCRRegisterClass;
15476 return Res;
15477 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015478
Dale Johannesen330169f2008-11-13 21:52:36 +000015479 // 'A' means EAX + EDX.
15480 if (Constraint == "A") {
15481 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015482 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015483 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015484 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015485 return Res;
15486 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015487
Chris Lattnerf76d1802006-07-31 23:26:50 +000015488 // Otherwise, check to see if this is a register class of the wrong value
15489 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15490 // turn into {ax},{dx}.
15491 if (Res.second->hasType(VT))
15492 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015493
Chris Lattnerf76d1802006-07-31 23:26:50 +000015494 // All of the single-register GCC register classes map their values onto
15495 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15496 // really want an 8-bit or 32-bit register, map to the appropriate register
15497 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015498 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015499 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015500 unsigned DestReg = 0;
15501 switch (Res.first) {
15502 default: break;
15503 case X86::AX: DestReg = X86::AL; break;
15504 case X86::DX: DestReg = X86::DL; break;
15505 case X86::CX: DestReg = X86::CL; break;
15506 case X86::BX: DestReg = X86::BL; break;
15507 }
15508 if (DestReg) {
15509 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015510 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015511 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015512 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015513 unsigned DestReg = 0;
15514 switch (Res.first) {
15515 default: break;
15516 case X86::AX: DestReg = X86::EAX; break;
15517 case X86::DX: DestReg = X86::EDX; break;
15518 case X86::CX: DestReg = X86::ECX; break;
15519 case X86::BX: DestReg = X86::EBX; break;
15520 case X86::SI: DestReg = X86::ESI; break;
15521 case X86::DI: DestReg = X86::EDI; break;
15522 case X86::BP: DestReg = X86::EBP; break;
15523 case X86::SP: DestReg = X86::ESP; break;
15524 }
15525 if (DestReg) {
15526 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015527 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015528 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015529 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015530 unsigned DestReg = 0;
15531 switch (Res.first) {
15532 default: break;
15533 case X86::AX: DestReg = X86::RAX; break;
15534 case X86::DX: DestReg = X86::RDX; break;
15535 case X86::CX: DestReg = X86::RCX; break;
15536 case X86::BX: DestReg = X86::RBX; break;
15537 case X86::SI: DestReg = X86::RSI; break;
15538 case X86::DI: DestReg = X86::RDI; break;
15539 case X86::BP: DestReg = X86::RBP; break;
15540 case X86::SP: DestReg = X86::RSP; break;
15541 }
15542 if (DestReg) {
15543 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015544 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015545 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015546 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015547 } else if (Res.second == X86::FR32RegisterClass ||
15548 Res.second == X86::FR64RegisterClass ||
15549 Res.second == X86::VR128RegisterClass) {
15550 // Handle references to XMM physical registers that got mapped into the
15551 // wrong class. This can happen with constraints like {xmm0} where the
15552 // target independent register mapper will just pick the first match it can
15553 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015554 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015555 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015556 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015557 Res.second = X86::FR64RegisterClass;
15558 else if (X86::VR128RegisterClass->hasType(VT))
15559 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015560 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015561
Chris Lattnerf76d1802006-07-31 23:26:50 +000015562 return Res;
15563}