blob: dc1fd7fc40bdaf53d0612d7c8c99882b20066009 [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
Chad Rosier01d426e2011-12-15 01:16:09 +00007594 SmallVector<Constant*,2> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007595 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007596 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Bill Wendling397ae212012-01-05 02:13:20 +00007597 CV1.push_back(
7598 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007599 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();
Chad Rosier01d426e2011-12-15 01:16:09 +00007881 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007882 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007883 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Chad Rosier01d426e2011-12-15 01:16:09 +00007884 CV.assign(2, C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007885 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007886 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Chad Rosier01d426e2011-12-15 01:16:09 +00007887 CV.assign(4, C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007888 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007889 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007890 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007891 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007892 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007893 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007894 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007895}
7896
Dan Gohmand858e902010-04-17 15:26:15 +00007897SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007898 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007899 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007900 EVT VT = Op.getValueType();
7901 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00007902 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
7903 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007904 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00007905 NumElts = VT.getVectorNumElements();
7906 }
7907 SmallVector<Constant*,8> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007908 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007909 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Chad Rosiera860b182011-12-15 01:02:25 +00007910 CV.assign(NumElts, C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007911 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007912 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Chad Rosiera860b182011-12-15 01:02:25 +00007913 CV.assign(NumElts, C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007914 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007915 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007916 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007917 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007918 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007919 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007920 if (VT.isVector()) {
Chad Rosiera860b182011-12-15 01:02:25 +00007921 MVT XORVT = VT.getSizeInBits() == 128 ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007922 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00007923 DAG.getNode(ISD::XOR, dl, XORVT,
7924 DAG.getNode(ISD::BITCAST, dl, XORVT,
Dale Johannesenace16102009-02-03 19:33:06 +00007925 Op.getOperand(0)),
Chad Rosiera860b182011-12-15 01:02:25 +00007926 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007927 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007928 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007929 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007930}
7931
Dan Gohmand858e902010-04-17 15:26:15 +00007932SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007933 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007934 SDValue Op0 = Op.getOperand(0);
7935 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007936 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007937 EVT VT = Op.getValueType();
7938 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007939
7940 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007941 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007942 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007943 SrcVT = VT;
7944 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007945 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007946 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007947 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007948 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007949 }
7950
7951 // At this point the operands and the result should have the same
7952 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007953
Evan Cheng68c47cb2007-01-05 07:55:56 +00007954 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00007955 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007956 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007957 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7958 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007959 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007960 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7961 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7962 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7963 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007964 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007965 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007966 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007967 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007968 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007969 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007970 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007971
7972 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007973 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007974 // Op0 is MVT::f32, Op1 is MVT::f64.
7975 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7976 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7977 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007978 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007979 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007980 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007981 }
7982
Evan Cheng73d6cf12007-01-05 21:37:56 +00007983 // Clear first operand sign bit.
7984 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007985 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007986 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7987 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007988 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007989 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7990 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7991 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7992 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007993 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007994 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007995 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007996 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007997 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007998 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007999 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008000
8001 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008002 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008003}
8004
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008005SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8006 SDValue N0 = Op.getOperand(0);
8007 DebugLoc dl = Op.getDebugLoc();
8008 EVT VT = Op.getValueType();
8009
8010 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8011 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8012 DAG.getConstant(1, VT));
8013 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8014}
8015
Dan Gohman076aee32009-03-04 19:44:21 +00008016/// Emit nodes that will be selected as "test Op0,Op0", or something
8017/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008018SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008019 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008020 DebugLoc dl = Op.getDebugLoc();
8021
Dan Gohman31125812009-03-07 01:58:32 +00008022 // CF and OF aren't always set the way we want. Determine which
8023 // of these we need.
8024 bool NeedCF = false;
8025 bool NeedOF = false;
8026 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008027 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008028 case X86::COND_A: case X86::COND_AE:
8029 case X86::COND_B: case X86::COND_BE:
8030 NeedCF = true;
8031 break;
8032 case X86::COND_G: case X86::COND_GE:
8033 case X86::COND_L: case X86::COND_LE:
8034 case X86::COND_O: case X86::COND_NO:
8035 NeedOF = true;
8036 break;
Dan Gohman31125812009-03-07 01:58:32 +00008037 }
8038
Dan Gohman076aee32009-03-04 19:44:21 +00008039 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008040 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8041 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008042 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8043 // Emit a CMP with 0, which is the TEST pattern.
8044 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8045 DAG.getConstant(0, Op.getValueType()));
8046
8047 unsigned Opcode = 0;
8048 unsigned NumOperands = 0;
8049 switch (Op.getNode()->getOpcode()) {
8050 case ISD::ADD:
8051 // Due to an isel shortcoming, be conservative if this add is likely to be
8052 // selected as part of a load-modify-store instruction. When the root node
8053 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8054 // uses of other nodes in the match, such as the ADD in this case. This
8055 // leads to the ADD being left around and reselected, with the result being
8056 // two adds in the output. Alas, even if none our users are stores, that
8057 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8058 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8059 // climbing the DAG back to the root, and it doesn't seem to be worth the
8060 // effort.
8061 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008062 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8063 if (UI->getOpcode() != ISD::CopyToReg &&
8064 UI->getOpcode() != ISD::SETCC &&
8065 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008066 goto default_case;
8067
8068 if (ConstantSDNode *C =
8069 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8070 // An add of one will be selected as an INC.
8071 if (C->getAPIntValue() == 1) {
8072 Opcode = X86ISD::INC;
8073 NumOperands = 1;
8074 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008075 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008076
8077 // An add of negative one (subtract of one) will be selected as a DEC.
8078 if (C->getAPIntValue().isAllOnesValue()) {
8079 Opcode = X86ISD::DEC;
8080 NumOperands = 1;
8081 break;
8082 }
Dan Gohman076aee32009-03-04 19:44:21 +00008083 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008084
8085 // Otherwise use a regular EFLAGS-setting add.
8086 Opcode = X86ISD::ADD;
8087 NumOperands = 2;
8088 break;
8089 case ISD::AND: {
8090 // If the primary and result isn't used, don't bother using X86ISD::AND,
8091 // because a TEST instruction will be better.
8092 bool NonFlagUse = false;
8093 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8094 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8095 SDNode *User = *UI;
8096 unsigned UOpNo = UI.getOperandNo();
8097 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8098 // Look pass truncate.
8099 UOpNo = User->use_begin().getOperandNo();
8100 User = *User->use_begin();
8101 }
8102
8103 if (User->getOpcode() != ISD::BRCOND &&
8104 User->getOpcode() != ISD::SETCC &&
8105 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8106 NonFlagUse = true;
8107 break;
8108 }
Dan Gohman076aee32009-03-04 19:44:21 +00008109 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008110
8111 if (!NonFlagUse)
8112 break;
8113 }
8114 // FALL THROUGH
8115 case ISD::SUB:
8116 case ISD::OR:
8117 case ISD::XOR:
8118 // Due to the ISEL shortcoming noted above, be conservative if this op is
8119 // likely to be selected as part of a load-modify-store instruction.
8120 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8121 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8122 if (UI->getOpcode() == ISD::STORE)
8123 goto default_case;
8124
8125 // Otherwise use a regular EFLAGS-setting instruction.
8126 switch (Op.getNode()->getOpcode()) {
8127 default: llvm_unreachable("unexpected operator!");
8128 case ISD::SUB: Opcode = X86ISD::SUB; break;
8129 case ISD::OR: Opcode = X86ISD::OR; break;
8130 case ISD::XOR: Opcode = X86ISD::XOR; break;
8131 case ISD::AND: Opcode = X86ISD::AND; break;
8132 }
8133
8134 NumOperands = 2;
8135 break;
8136 case X86ISD::ADD:
8137 case X86ISD::SUB:
8138 case X86ISD::INC:
8139 case X86ISD::DEC:
8140 case X86ISD::OR:
8141 case X86ISD::XOR:
8142 case X86ISD::AND:
8143 return SDValue(Op.getNode(), 1);
8144 default:
8145 default_case:
8146 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008147 }
8148
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008149 if (Opcode == 0)
8150 // Emit a CMP with 0, which is the TEST pattern.
8151 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8152 DAG.getConstant(0, Op.getValueType()));
8153
8154 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8155 SmallVector<SDValue, 4> Ops;
8156 for (unsigned i = 0; i != NumOperands; ++i)
8157 Ops.push_back(Op.getOperand(i));
8158
8159 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8160 DAG.ReplaceAllUsesWith(Op, New);
8161 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008162}
8163
8164/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8165/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008166SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008167 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008168 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8169 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008170 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008171
8172 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008173 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008174}
8175
Evan Chengd40d03e2010-01-06 19:38:29 +00008176/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8177/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008178SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8179 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008180 SDValue Op0 = And.getOperand(0);
8181 SDValue Op1 = And.getOperand(1);
8182 if (Op0.getOpcode() == ISD::TRUNCATE)
8183 Op0 = Op0.getOperand(0);
8184 if (Op1.getOpcode() == ISD::TRUNCATE)
8185 Op1 = Op1.getOperand(0);
8186
Evan Chengd40d03e2010-01-06 19:38:29 +00008187 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008188 if (Op1.getOpcode() == ISD::SHL)
8189 std::swap(Op0, Op1);
8190 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008191 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8192 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008193 // If we looked past a truncate, check that it's only truncating away
8194 // known zeros.
8195 unsigned BitWidth = Op0.getValueSizeInBits();
8196 unsigned AndBitWidth = And.getValueSizeInBits();
8197 if (BitWidth > AndBitWidth) {
8198 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8199 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8200 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8201 return SDValue();
8202 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008203 LHS = Op1;
8204 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008205 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008206 } else if (Op1.getOpcode() == ISD::Constant) {
8207 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008208 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008209 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008210
8211 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008212 LHS = AndLHS.getOperand(0);
8213 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008214 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008215
8216 // Use BT if the immediate can't be encoded in a TEST instruction.
8217 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8218 LHS = AndLHS;
8219 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8220 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008221 }
Evan Cheng0488db92007-09-25 01:57:46 +00008222
Evan Chengd40d03e2010-01-06 19:38:29 +00008223 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008224 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008225 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008226 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008227 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008228 // Also promote i16 to i32 for performance / code size reason.
8229 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008230 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008231 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008232
Evan Chengd40d03e2010-01-06 19:38:29 +00008233 // If the operand types disagree, extend the shift amount to match. Since
8234 // BT ignores high bits (like shifts) we can use anyextend.
8235 if (LHS.getValueType() != RHS.getValueType())
8236 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008237
Evan Chengd40d03e2010-01-06 19:38:29 +00008238 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8239 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8240 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8241 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008242 }
8243
Evan Cheng54de3ea2010-01-05 06:52:31 +00008244 return SDValue();
8245}
8246
Dan Gohmand858e902010-04-17 15:26:15 +00008247SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008248
8249 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8250
Evan Cheng54de3ea2010-01-05 06:52:31 +00008251 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8252 SDValue Op0 = Op.getOperand(0);
8253 SDValue Op1 = Op.getOperand(1);
8254 DebugLoc dl = Op.getDebugLoc();
8255 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8256
8257 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008258 // Lower (X & (1 << N)) == 0 to BT(X, N).
8259 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8260 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008261 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008262 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008263 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008264 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8265 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8266 if (NewSetCC.getNode())
8267 return NewSetCC;
8268 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008269
Chris Lattner481eebc2010-12-19 21:23:48 +00008270 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8271 // these.
8272 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008273 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008274 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8275 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008276
Chris Lattner481eebc2010-12-19 21:23:48 +00008277 // If the input is a setcc, then reuse the input setcc or use a new one with
8278 // the inverted condition.
8279 if (Op0.getOpcode() == X86ISD::SETCC) {
8280 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8281 bool Invert = (CC == ISD::SETNE) ^
8282 cast<ConstantSDNode>(Op1)->isNullValue();
8283 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008284
Evan Cheng2c755ba2010-02-27 07:36:59 +00008285 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008286 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8287 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8288 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008289 }
8290
Evan Chenge5b51ac2010-04-17 06:13:15 +00008291 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008292 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008293 if (X86CC == X86::COND_INVALID)
8294 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008295
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008296 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008297 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008298 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008299}
8300
Craig Topper89af15e2011-09-18 08:03:58 +00008301// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008302// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008303static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008304 EVT VT = Op.getValueType();
8305
Duncan Sands28b77e92011-09-06 19:07:46 +00008306 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008307 "Unsupported value type for operation");
8308
8309 int NumElems = VT.getVectorNumElements();
8310 DebugLoc dl = Op.getDebugLoc();
8311 SDValue CC = Op.getOperand(2);
8312 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8313 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8314
8315 // Extract the LHS vectors
8316 SDValue LHS = Op.getOperand(0);
8317 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8318 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8319
8320 // Extract the RHS vectors
8321 SDValue RHS = Op.getOperand(1);
8322 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8323 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8324
8325 // Issue the operation on the smaller types and concatenate the result back
8326 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8327 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8328 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8329 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8330 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8331}
8332
8333
Dan Gohmand858e902010-04-17 15:26:15 +00008334SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008335 SDValue Cond;
8336 SDValue Op0 = Op.getOperand(0);
8337 SDValue Op1 = Op.getOperand(1);
8338 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008339 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008340 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8341 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008342 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008343
8344 if (isFP) {
8345 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008346 EVT EltVT = Op0.getValueType().getVectorElementType();
8347 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8348
Nate Begeman30a0de92008-07-17 16:51:19 +00008349 bool Swap = false;
8350
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008351 // SSE Condition code mapping:
8352 // 0 - EQ
8353 // 1 - LT
8354 // 2 - LE
8355 // 3 - UNORD
8356 // 4 - NEQ
8357 // 5 - NLT
8358 // 6 - NLE
8359 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008360 switch (SetCCOpcode) {
8361 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008362 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008363 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008364 case ISD::SETOGT:
8365 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008366 case ISD::SETLT:
8367 case ISD::SETOLT: SSECC = 1; break;
8368 case ISD::SETOGE:
8369 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008370 case ISD::SETLE:
8371 case ISD::SETOLE: SSECC = 2; break;
8372 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008373 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008374 case ISD::SETNE: SSECC = 4; break;
8375 case ISD::SETULE: Swap = true;
8376 case ISD::SETUGE: SSECC = 5; break;
8377 case ISD::SETULT: Swap = true;
8378 case ISD::SETUGT: SSECC = 6; break;
8379 case ISD::SETO: SSECC = 7; break;
8380 }
8381 if (Swap)
8382 std::swap(Op0, Op1);
8383
Nate Begemanfb8ead02008-07-25 19:05:58 +00008384 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008385 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008386 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008387 SDValue UNORD, EQ;
Craig Topper1906d322012-01-22 23:36:02 +00008388 UNORD = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8389 DAG.getConstant(3, MVT::i8));
8390 EQ = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8391 DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008392 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Craig Topper0a150352011-11-09 08:06:13 +00008393 } else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008394 SDValue ORD, NEQ;
Craig Topper1906d322012-01-22 23:36:02 +00008395 ORD = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8396 DAG.getConstant(7, MVT::i8));
8397 NEQ = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8398 DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008399 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008400 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008401 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008402 }
8403 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008404 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8405 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008406 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008407
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008408 // Break 256-bit integer vector compare into smaller ones.
Craig Topper0a150352011-11-09 08:06:13 +00008409 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008410 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008411
Nate Begeman30a0de92008-07-17 16:51:19 +00008412 // We are handling one of the integer comparisons here. Since SSE only has
8413 // GT and EQ comparisons for integer, swapping operands and multiple
8414 // operations may be required for some comparisons.
Craig Topper67609fd2012-01-22 22:42:16 +00008415 unsigned Opc = 0;
Nate Begeman30a0de92008-07-17 16:51:19 +00008416 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008417
Nate Begeman30a0de92008-07-17 16:51:19 +00008418 switch (SetCCOpcode) {
8419 default: break;
8420 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008421 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008422 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008423 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008424 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008425 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008426 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008427 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008428 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008429 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008430 }
8431 if (Swap)
8432 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008433
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008434 // Check that the operation in question is available (most are plain SSE2,
8435 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper67609fd2012-01-22 22:42:16 +00008436 if (Opc == X86ISD::PCMPGT && VT == MVT::v2i64 && !Subtarget->hasSSE42())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008437 return SDValue();
Craig Topper67609fd2012-01-22 22:42:16 +00008438 if (Opc == X86ISD::PCMPEQ && VT == MVT::v2i64 && !Subtarget->hasSSE41())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008439 return SDValue();
8440
Nate Begeman30a0de92008-07-17 16:51:19 +00008441 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8442 // bits of the inputs before performing those operations.
8443 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008444 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008445 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8446 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008447 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008448 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8449 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008450 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8451 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008452 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008453
Dale Johannesenace16102009-02-03 19:33:06 +00008454 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008455
8456 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008457 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008458 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008459
Nate Begeman30a0de92008-07-17 16:51:19 +00008460 return Result;
8461}
Evan Cheng0488db92007-09-25 01:57:46 +00008462
Evan Cheng370e5342008-12-03 08:38:43 +00008463// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008464static bool isX86LogicalCmp(SDValue Op) {
8465 unsigned Opc = Op.getNode()->getOpcode();
8466 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8467 return true;
8468 if (Op.getResNo() == 1 &&
8469 (Opc == X86ISD::ADD ||
8470 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008471 Opc == X86ISD::ADC ||
8472 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008473 Opc == X86ISD::SMUL ||
8474 Opc == X86ISD::UMUL ||
8475 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008476 Opc == X86ISD::DEC ||
8477 Opc == X86ISD::OR ||
8478 Opc == X86ISD::XOR ||
8479 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008480 return true;
8481
Chris Lattner9637d5b2010-12-05 07:49:54 +00008482 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8483 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008484
Dan Gohman076aee32009-03-04 19:44:21 +00008485 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008486}
8487
Chris Lattnera2b56002010-12-05 01:23:24 +00008488static bool isZero(SDValue V) {
8489 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8490 return C && C->isNullValue();
8491}
8492
Chris Lattner96908b12010-12-05 02:00:51 +00008493static bool isAllOnes(SDValue V) {
8494 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8495 return C && C->isAllOnesValue();
8496}
8497
Dan Gohmand858e902010-04-17 15:26:15 +00008498SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008499 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008500 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008501 SDValue Op1 = Op.getOperand(1);
8502 SDValue Op2 = Op.getOperand(2);
8503 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008504 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008505
Dan Gohman1a492952009-10-20 16:22:37 +00008506 if (Cond.getOpcode() == ISD::SETCC) {
8507 SDValue NewCond = LowerSETCC(Cond, DAG);
8508 if (NewCond.getNode())
8509 Cond = NewCond;
8510 }
Evan Cheng734503b2006-09-11 02:19:56 +00008511
Chris Lattnera2b56002010-12-05 01:23:24 +00008512 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008513 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008514 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008515 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008516 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008517 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8518 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008519 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008520
Chris Lattnera2b56002010-12-05 01:23:24 +00008521 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008522
8523 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008524 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8525 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008526
8527 SDValue CmpOp0 = Cmp.getOperand(0);
8528 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8529 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008530
Chris Lattner96908b12010-12-05 02:00:51 +00008531 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008532 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8533 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008534
Chris Lattner96908b12010-12-05 02:00:51 +00008535 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8536 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008537
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008538 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008539 if (N2C == 0 || !N2C->isNullValue())
8540 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8541 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008542 }
8543 }
8544
Chris Lattnera2b56002010-12-05 01:23:24 +00008545 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008546 if (Cond.getOpcode() == ISD::AND &&
8547 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8548 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008549 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008550 Cond = Cond.getOperand(0);
8551 }
8552
Evan Cheng3f41d662007-10-08 22:16:29 +00008553 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8554 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008555 unsigned CondOpcode = Cond.getOpcode();
8556 if (CondOpcode == X86ISD::SETCC ||
8557 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008558 CC = Cond.getOperand(0);
8559
Dan Gohman475871a2008-07-27 21:46:04 +00008560 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008561 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008562 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008563
Evan Cheng3f41d662007-10-08 22:16:29 +00008564 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008565 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008566 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008567 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008568
Chris Lattnerd1980a52009-03-12 06:52:53 +00008569 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8570 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008571 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008572 addTest = false;
8573 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008574 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8575 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8576 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8577 Cond.getOperand(0).getValueType() != MVT::i8)) {
8578 SDValue LHS = Cond.getOperand(0);
8579 SDValue RHS = Cond.getOperand(1);
8580 unsigned X86Opcode;
8581 unsigned X86Cond;
8582 SDVTList VTs;
8583 switch (CondOpcode) {
8584 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8585 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8586 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8587 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8588 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8589 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8590 default: llvm_unreachable("unexpected overflowing operator");
8591 }
8592 if (CondOpcode == ISD::UMULO)
8593 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8594 MVT::i32);
8595 else
8596 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8597
8598 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8599
8600 if (CondOpcode == ISD::UMULO)
8601 Cond = X86Op.getValue(2);
8602 else
8603 Cond = X86Op.getValue(1);
8604
8605 CC = DAG.getConstant(X86Cond, MVT::i8);
8606 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008607 }
8608
8609 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008610 // Look pass the truncate.
8611 if (Cond.getOpcode() == ISD::TRUNCATE)
8612 Cond = Cond.getOperand(0);
8613
8614 // We know the result of AND is compared against zero. Try to match
8615 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008616 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008617 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008618 if (NewSetCC.getNode()) {
8619 CC = NewSetCC.getOperand(0);
8620 Cond = NewSetCC.getOperand(1);
8621 addTest = false;
8622 }
8623 }
8624 }
8625
8626 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008627 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008628 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008629 }
8630
Benjamin Kramere915ff32010-12-22 23:09:28 +00008631 // a < b ? -1 : 0 -> RES = ~setcc_carry
8632 // a < b ? 0 : -1 -> RES = setcc_carry
8633 // a >= b ? -1 : 0 -> RES = setcc_carry
8634 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8635 if (Cond.getOpcode() == X86ISD::CMP) {
8636 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8637
8638 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8639 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8640 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8641 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8642 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8643 return DAG.getNOT(DL, Res, Res.getValueType());
8644 return Res;
8645 }
8646 }
8647
Evan Cheng0488db92007-09-25 01:57:46 +00008648 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8649 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008650 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008651 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008652 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008653}
8654
Evan Cheng370e5342008-12-03 08:38:43 +00008655// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8656// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8657// from the AND / OR.
8658static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8659 Opc = Op.getOpcode();
8660 if (Opc != ISD::OR && Opc != ISD::AND)
8661 return false;
8662 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8663 Op.getOperand(0).hasOneUse() &&
8664 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8665 Op.getOperand(1).hasOneUse());
8666}
8667
Evan Cheng961d6d42009-02-02 08:19:07 +00008668// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8669// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008670static bool isXor1OfSetCC(SDValue Op) {
8671 if (Op.getOpcode() != ISD::XOR)
8672 return false;
8673 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8674 if (N1C && N1C->getAPIntValue() == 1) {
8675 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8676 Op.getOperand(0).hasOneUse();
8677 }
8678 return false;
8679}
8680
Dan Gohmand858e902010-04-17 15:26:15 +00008681SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008682 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008683 SDValue Chain = Op.getOperand(0);
8684 SDValue Cond = Op.getOperand(1);
8685 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008686 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008687 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008688 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008689
Dan Gohman1a492952009-10-20 16:22:37 +00008690 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008691 // Check for setcc([su]{add,sub,mul}o == 0).
8692 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8693 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8694 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8695 Cond.getOperand(0).getResNo() == 1 &&
8696 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
8697 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
8698 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
8699 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
8700 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
8701 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
8702 Inverted = true;
8703 Cond = Cond.getOperand(0);
8704 } else {
8705 SDValue NewCond = LowerSETCC(Cond, DAG);
8706 if (NewCond.getNode())
8707 Cond = NewCond;
8708 }
Dan Gohman1a492952009-10-20 16:22:37 +00008709 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008710#if 0
8711 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008712 else if (Cond.getOpcode() == X86ISD::ADD ||
8713 Cond.getOpcode() == X86ISD::SUB ||
8714 Cond.getOpcode() == X86ISD::SMUL ||
8715 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008716 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008717#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008718
Evan Chengad9c0a32009-12-15 00:53:42 +00008719 // Look pass (and (setcc_carry (cmp ...)), 1).
8720 if (Cond.getOpcode() == ISD::AND &&
8721 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8722 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008723 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008724 Cond = Cond.getOperand(0);
8725 }
8726
Evan Cheng3f41d662007-10-08 22:16:29 +00008727 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8728 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008729 unsigned CondOpcode = Cond.getOpcode();
8730 if (CondOpcode == X86ISD::SETCC ||
8731 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008732 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008733
Dan Gohman475871a2008-07-27 21:46:04 +00008734 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008735 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008736 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008737 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008738 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008739 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008740 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008741 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008742 default: break;
8743 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008744 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008745 // These can only come from an arithmetic instruction with overflow,
8746 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008747 Cond = Cond.getNode()->getOperand(1);
8748 addTest = false;
8749 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008750 }
Evan Cheng0488db92007-09-25 01:57:46 +00008751 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008752 }
8753 CondOpcode = Cond.getOpcode();
8754 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8755 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8756 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8757 Cond.getOperand(0).getValueType() != MVT::i8)) {
8758 SDValue LHS = Cond.getOperand(0);
8759 SDValue RHS = Cond.getOperand(1);
8760 unsigned X86Opcode;
8761 unsigned X86Cond;
8762 SDVTList VTs;
8763 switch (CondOpcode) {
8764 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8765 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8766 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8767 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8768 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8769 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8770 default: llvm_unreachable("unexpected overflowing operator");
8771 }
8772 if (Inverted)
8773 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
8774 if (CondOpcode == ISD::UMULO)
8775 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8776 MVT::i32);
8777 else
8778 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8779
8780 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
8781
8782 if (CondOpcode == ISD::UMULO)
8783 Cond = X86Op.getValue(2);
8784 else
8785 Cond = X86Op.getValue(1);
8786
8787 CC = DAG.getConstant(X86Cond, MVT::i8);
8788 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00008789 } else {
8790 unsigned CondOpc;
8791 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8792 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008793 if (CondOpc == ISD::OR) {
8794 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8795 // two branches instead of an explicit OR instruction with a
8796 // separate test.
8797 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008798 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008799 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008800 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008801 Chain, Dest, CC, Cmp);
8802 CC = Cond.getOperand(1).getOperand(0);
8803 Cond = Cmp;
8804 addTest = false;
8805 }
8806 } else { // ISD::AND
8807 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8808 // two branches instead of an explicit AND instruction with a
8809 // separate test. However, we only do this if this block doesn't
8810 // have a fall-through edge, because this requires an explicit
8811 // jmp when the condition is false.
8812 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008813 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008814 Op.getNode()->hasOneUse()) {
8815 X86::CondCode CCode =
8816 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8817 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008818 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008819 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008820 // Look for an unconditional branch following this conditional branch.
8821 // We need this because we need to reverse the successors in order
8822 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008823 if (User->getOpcode() == ISD::BR) {
8824 SDValue FalseBB = User->getOperand(1);
8825 SDNode *NewBR =
8826 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008827 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008828 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008829 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008830
Dale Johannesene4d209d2009-02-03 20:21:25 +00008831 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008832 Chain, Dest, CC, Cmp);
8833 X86::CondCode CCode =
8834 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8835 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008836 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008837 Cond = Cmp;
8838 addTest = false;
8839 }
8840 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008841 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008842 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8843 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8844 // It should be transformed during dag combiner except when the condition
8845 // is set by a arithmetics with overflow node.
8846 X86::CondCode CCode =
8847 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8848 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008849 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008850 Cond = Cond.getOperand(0).getOperand(1);
8851 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00008852 } else if (Cond.getOpcode() == ISD::SETCC &&
8853 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
8854 // For FCMP_OEQ, we can emit
8855 // two branches instead of an explicit AND instruction with a
8856 // separate test. However, we only do this if this block doesn't
8857 // have a fall-through edge, because this requires an explicit
8858 // jmp when the condition is false.
8859 if (Op.getNode()->hasOneUse()) {
8860 SDNode *User = *Op.getNode()->use_begin();
8861 // Look for an unconditional branch following this conditional branch.
8862 // We need this because we need to reverse the successors in order
8863 // to implement FCMP_OEQ.
8864 if (User->getOpcode() == ISD::BR) {
8865 SDValue FalseBB = User->getOperand(1);
8866 SDNode *NewBR =
8867 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8868 assert(NewBR == User);
8869 (void)NewBR;
8870 Dest = FalseBB;
8871
8872 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
8873 Cond.getOperand(0), Cond.getOperand(1));
8874 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8875 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8876 Chain, Dest, CC, Cmp);
8877 CC = DAG.getConstant(X86::COND_P, MVT::i8);
8878 Cond = Cmp;
8879 addTest = false;
8880 }
8881 }
8882 } else if (Cond.getOpcode() == ISD::SETCC &&
8883 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
8884 // For FCMP_UNE, we can emit
8885 // two branches instead of an explicit AND instruction with a
8886 // separate test. However, we only do this if this block doesn't
8887 // have a fall-through edge, because this requires an explicit
8888 // jmp when the condition is false.
8889 if (Op.getNode()->hasOneUse()) {
8890 SDNode *User = *Op.getNode()->use_begin();
8891 // Look for an unconditional branch following this conditional branch.
8892 // We need this because we need to reverse the successors in order
8893 // to implement FCMP_UNE.
8894 if (User->getOpcode() == ISD::BR) {
8895 SDValue FalseBB = User->getOperand(1);
8896 SDNode *NewBR =
8897 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8898 assert(NewBR == User);
8899 (void)NewBR;
8900
8901 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
8902 Cond.getOperand(0), Cond.getOperand(1));
8903 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8904 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8905 Chain, Dest, CC, Cmp);
8906 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
8907 Cond = Cmp;
8908 addTest = false;
8909 Dest = FalseBB;
8910 }
8911 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008912 }
Evan Cheng0488db92007-09-25 01:57:46 +00008913 }
8914
8915 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008916 // Look pass the truncate.
8917 if (Cond.getOpcode() == ISD::TRUNCATE)
8918 Cond = Cond.getOperand(0);
8919
8920 // We know the result of AND is compared against zero. Try to match
8921 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008922 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008923 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8924 if (NewSetCC.getNode()) {
8925 CC = NewSetCC.getOperand(0);
8926 Cond = NewSetCC.getOperand(1);
8927 addTest = false;
8928 }
8929 }
8930 }
8931
8932 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008933 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008934 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008935 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008936 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008937 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008938}
8939
Anton Korobeynikove060b532007-04-17 19:34:00 +00008940
8941// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8942// Calls to _alloca is needed to probe the stack when allocating more than 4k
8943// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8944// that the guard pages used by the OS virtual memory manager are allocated in
8945// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008946SDValue
8947X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008948 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008949 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008950 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008951 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008952 "are being used");
8953 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008954 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008955
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008956 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008957 SDValue Chain = Op.getOperand(0);
8958 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008959 // FIXME: Ensure alignment here
8960
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008961 bool Is64Bit = Subtarget->is64Bit();
8962 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008963
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008964 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008965 MachineFunction &MF = DAG.getMachineFunction();
8966 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008967
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008968 if (Is64Bit) {
8969 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008970 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008971 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008972
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008973 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8974 I != E; I++)
8975 if (I->hasNestAttr())
8976 report_fatal_error("Cannot use segmented stacks with functions that "
8977 "have nested arguments.");
8978 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008979
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008980 const TargetRegisterClass *AddrRegClass =
8981 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8982 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8983 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8984 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8985 DAG.getRegister(Vreg, SPTy));
8986 SDValue Ops1[2] = { Value, Chain };
8987 return DAG.getMergeValues(Ops1, 2, dl);
8988 } else {
8989 SDValue Flag;
8990 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008991
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008992 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8993 Flag = Chain.getValue(1);
8994 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008995
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008996 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
8997 Flag = Chain.getValue(1);
8998
8999 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9000
9001 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9002 return DAG.getMergeValues(Ops1, 2, dl);
9003 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009004}
9005
Dan Gohmand858e902010-04-17 15:26:15 +00009006SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009007 MachineFunction &MF = DAG.getMachineFunction();
9008 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9009
Dan Gohman69de1932008-02-06 22:27:42 +00009010 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009011 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009012
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009013 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009014 // vastart just stores the address of the VarArgsFrameIndex slot into the
9015 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009016 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9017 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009018 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9019 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009020 }
9021
9022 // __va_list_tag:
9023 // gp_offset (0 - 6 * 8)
9024 // fp_offset (48 - 48 + 8 * 16)
9025 // overflow_arg_area (point to parameters coming in memory).
9026 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009027 SmallVector<SDValue, 8> MemOps;
9028 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009029 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009030 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009031 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9032 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009033 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009034 MemOps.push_back(Store);
9035
9036 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009037 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009038 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009039 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009040 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9041 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009042 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009043 MemOps.push_back(Store);
9044
9045 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009046 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009047 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009048 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9049 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009050 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9051 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009052 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009053 MemOps.push_back(Store);
9054
9055 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009056 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009057 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009058 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9059 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009060 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9061 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009062 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009063 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009064 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009065}
9066
Dan Gohmand858e902010-04-17 15:26:15 +00009067SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009068 assert(Subtarget->is64Bit() &&
9069 "LowerVAARG only handles 64-bit va_arg!");
9070 assert((Subtarget->isTargetLinux() ||
9071 Subtarget->isTargetDarwin()) &&
9072 "Unhandled target in LowerVAARG");
9073 assert(Op.getNode()->getNumOperands() == 4);
9074 SDValue Chain = Op.getOperand(0);
9075 SDValue SrcPtr = Op.getOperand(1);
9076 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9077 unsigned Align = Op.getConstantOperandVal(3);
9078 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009079
Dan Gohman320afb82010-10-12 18:00:49 +00009080 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009081 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009082 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9083 uint8_t ArgMode;
9084
9085 // Decide which area this value should be read from.
9086 // TODO: Implement the AMD64 ABI in its entirety. This simple
9087 // selection mechanism works only for the basic types.
9088 if (ArgVT == MVT::f80) {
9089 llvm_unreachable("va_arg for f80 not yet implemented");
9090 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9091 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9092 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9093 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9094 } else {
9095 llvm_unreachable("Unhandled argument type in LowerVAARG");
9096 }
9097
9098 if (ArgMode == 2) {
9099 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009100 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009101 !(DAG.getMachineFunction()
9102 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009103 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009104 }
9105
9106 // Insert VAARG_64 node into the DAG
9107 // VAARG_64 returns two values: Variable Argument Address, Chain
9108 SmallVector<SDValue, 11> InstOps;
9109 InstOps.push_back(Chain);
9110 InstOps.push_back(SrcPtr);
9111 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9112 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9113 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9114 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9115 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9116 VTs, &InstOps[0], InstOps.size(),
9117 MVT::i64,
9118 MachinePointerInfo(SV),
9119 /*Align=*/0,
9120 /*Volatile=*/false,
9121 /*ReadMem=*/true,
9122 /*WriteMem=*/true);
9123 Chain = VAARG.getValue(1);
9124
9125 // Load the next argument and return it
9126 return DAG.getLoad(ArgVT, dl,
9127 Chain,
9128 VAARG,
9129 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009130 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009131}
9132
Dan Gohmand858e902010-04-17 15:26:15 +00009133SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009134 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009135 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009136 SDValue Chain = Op.getOperand(0);
9137 SDValue DstPtr = Op.getOperand(1);
9138 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009139 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9140 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009141 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009142
Chris Lattnere72f2022010-09-21 05:40:29 +00009143 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009144 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009145 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009146 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009147}
9148
Craig Topper80e46362012-01-23 06:16:53 +00009149// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9150// may or may not be a constant. Takes immediate version of shift as input.
9151static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9152 SDValue SrcOp, SDValue ShAmt,
9153 SelectionDAG &DAG) {
9154 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9155
9156 if (isa<ConstantSDNode>(ShAmt)) {
9157 switch (Opc) {
9158 default: llvm_unreachable("Unknown target vector shift node");
9159 case X86ISD::VSHLI:
9160 case X86ISD::VSRLI:
9161 case X86ISD::VSRAI:
9162 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9163 }
9164 }
9165
9166 // Change opcode to non-immediate version
9167 switch (Opc) {
9168 default: llvm_unreachable("Unknown target vector shift node");
9169 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9170 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9171 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9172 }
9173
9174 // Need to build a vector containing shift amount
9175 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9176 SDValue ShOps[4];
9177 ShOps[0] = ShAmt;
9178 ShOps[1] = DAG.getConstant(0, MVT::i32);
9179 ShOps[2] = DAG.getUNDEF(MVT::i32);
9180 ShOps[3] = DAG.getUNDEF(MVT::i32);
9181 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
9182 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
9183 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9184}
9185
Dan Gohman475871a2008-07-27 21:46:04 +00009186SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009187X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009188 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009189 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009190 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009191 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009192 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009193 case Intrinsic::x86_sse_comieq_ss:
9194 case Intrinsic::x86_sse_comilt_ss:
9195 case Intrinsic::x86_sse_comile_ss:
9196 case Intrinsic::x86_sse_comigt_ss:
9197 case Intrinsic::x86_sse_comige_ss:
9198 case Intrinsic::x86_sse_comineq_ss:
9199 case Intrinsic::x86_sse_ucomieq_ss:
9200 case Intrinsic::x86_sse_ucomilt_ss:
9201 case Intrinsic::x86_sse_ucomile_ss:
9202 case Intrinsic::x86_sse_ucomigt_ss:
9203 case Intrinsic::x86_sse_ucomige_ss:
9204 case Intrinsic::x86_sse_ucomineq_ss:
9205 case Intrinsic::x86_sse2_comieq_sd:
9206 case Intrinsic::x86_sse2_comilt_sd:
9207 case Intrinsic::x86_sse2_comile_sd:
9208 case Intrinsic::x86_sse2_comigt_sd:
9209 case Intrinsic::x86_sse2_comige_sd:
9210 case Intrinsic::x86_sse2_comineq_sd:
9211 case Intrinsic::x86_sse2_ucomieq_sd:
9212 case Intrinsic::x86_sse2_ucomilt_sd:
9213 case Intrinsic::x86_sse2_ucomile_sd:
9214 case Intrinsic::x86_sse2_ucomigt_sd:
9215 case Intrinsic::x86_sse2_ucomige_sd:
9216 case Intrinsic::x86_sse2_ucomineq_sd: {
9217 unsigned Opc = 0;
9218 ISD::CondCode CC = ISD::SETCC_INVALID;
9219 switch (IntNo) {
9220 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009221 case Intrinsic::x86_sse_comieq_ss:
9222 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009223 Opc = X86ISD::COMI;
9224 CC = ISD::SETEQ;
9225 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009226 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009227 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009228 Opc = X86ISD::COMI;
9229 CC = ISD::SETLT;
9230 break;
9231 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009232 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009233 Opc = X86ISD::COMI;
9234 CC = ISD::SETLE;
9235 break;
9236 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009237 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009238 Opc = X86ISD::COMI;
9239 CC = ISD::SETGT;
9240 break;
9241 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009242 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009243 Opc = X86ISD::COMI;
9244 CC = ISD::SETGE;
9245 break;
9246 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009247 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009248 Opc = X86ISD::COMI;
9249 CC = ISD::SETNE;
9250 break;
9251 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009252 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009253 Opc = X86ISD::UCOMI;
9254 CC = ISD::SETEQ;
9255 break;
9256 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009257 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009258 Opc = X86ISD::UCOMI;
9259 CC = ISD::SETLT;
9260 break;
9261 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009262 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009263 Opc = X86ISD::UCOMI;
9264 CC = ISD::SETLE;
9265 break;
9266 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009267 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009268 Opc = X86ISD::UCOMI;
9269 CC = ISD::SETGT;
9270 break;
9271 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009272 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009273 Opc = X86ISD::UCOMI;
9274 CC = ISD::SETGE;
9275 break;
9276 case Intrinsic::x86_sse_ucomineq_ss:
9277 case Intrinsic::x86_sse2_ucomineq_sd:
9278 Opc = X86ISD::UCOMI;
9279 CC = ISD::SETNE;
9280 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009281 }
Evan Cheng734503b2006-09-11 02:19:56 +00009282
Dan Gohman475871a2008-07-27 21:46:04 +00009283 SDValue LHS = Op.getOperand(1);
9284 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009285 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009286 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009287 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9288 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9289 DAG.getConstant(X86CC, MVT::i8), Cond);
9290 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009291 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009292 // Arithmetic intrinsics.
9293 case Intrinsic::x86_sse3_hadd_ps:
9294 case Intrinsic::x86_sse3_hadd_pd:
9295 case Intrinsic::x86_avx_hadd_ps_256:
9296 case Intrinsic::x86_avx_hadd_pd_256:
9297 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9298 Op.getOperand(1), Op.getOperand(2));
9299 case Intrinsic::x86_sse3_hsub_ps:
9300 case Intrinsic::x86_sse3_hsub_pd:
9301 case Intrinsic::x86_avx_hsub_ps_256:
9302 case Intrinsic::x86_avx_hsub_pd_256:
9303 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9304 Op.getOperand(1), Op.getOperand(2));
Craig Topper98fc7292011-11-19 17:46:46 +00009305 case Intrinsic::x86_avx2_psllv_d:
9306 case Intrinsic::x86_avx2_psllv_q:
9307 case Intrinsic::x86_avx2_psllv_d_256:
9308 case Intrinsic::x86_avx2_psllv_q_256:
9309 return DAG.getNode(ISD::SHL, dl, Op.getValueType(),
9310 Op.getOperand(1), Op.getOperand(2));
9311 case Intrinsic::x86_avx2_psrlv_d:
9312 case Intrinsic::x86_avx2_psrlv_q:
9313 case Intrinsic::x86_avx2_psrlv_d_256:
9314 case Intrinsic::x86_avx2_psrlv_q_256:
9315 return DAG.getNode(ISD::SRL, dl, Op.getValueType(),
9316 Op.getOperand(1), Op.getOperand(2));
9317 case Intrinsic::x86_avx2_psrav_d:
9318 case Intrinsic::x86_avx2_psrav_d_256:
9319 return DAG.getNode(ISD::SRA, dl, Op.getValueType(),
9320 Op.getOperand(1), Op.getOperand(2));
Craig Topper7925e252012-01-23 08:18:28 +00009321 case Intrinsic::x86_sse2_pcmpeq_b:
9322 case Intrinsic::x86_sse2_pcmpeq_w:
9323 case Intrinsic::x86_sse2_pcmpeq_d:
9324 case Intrinsic::x86_sse41_pcmpeqq:
9325 case Intrinsic::x86_avx2_pcmpeq_b:
9326 case Intrinsic::x86_avx2_pcmpeq_w:
9327 case Intrinsic::x86_avx2_pcmpeq_d:
9328 case Intrinsic::x86_avx2_pcmpeq_q:
9329 return DAG.getNode(X86ISD::PCMPEQ, dl, Op.getValueType(),
9330 Op.getOperand(1), Op.getOperand(2));
9331 case Intrinsic::x86_sse2_pcmpgt_b:
9332 case Intrinsic::x86_sse2_pcmpgt_w:
9333 case Intrinsic::x86_sse2_pcmpgt_d:
9334 case Intrinsic::x86_sse42_pcmpgtq:
9335 case Intrinsic::x86_avx2_pcmpgt_b:
9336 case Intrinsic::x86_avx2_pcmpgt_w:
9337 case Intrinsic::x86_avx2_pcmpgt_d:
9338 case Intrinsic::x86_avx2_pcmpgt_q:
9339 return DAG.getNode(X86ISD::PCMPGT, dl, Op.getValueType(),
9340 Op.getOperand(1), Op.getOperand(2));
Craig Topper98fc7292011-11-19 17:46:46 +00009341
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009342 // ptest and testp intrinsics. The intrinsic these come from are designed to
9343 // return an integer value, not just an instruction so lower it to the ptest
9344 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009345 case Intrinsic::x86_sse41_ptestz:
9346 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009347 case Intrinsic::x86_sse41_ptestnzc:
9348 case Intrinsic::x86_avx_ptestz_256:
9349 case Intrinsic::x86_avx_ptestc_256:
9350 case Intrinsic::x86_avx_ptestnzc_256:
9351 case Intrinsic::x86_avx_vtestz_ps:
9352 case Intrinsic::x86_avx_vtestc_ps:
9353 case Intrinsic::x86_avx_vtestnzc_ps:
9354 case Intrinsic::x86_avx_vtestz_pd:
9355 case Intrinsic::x86_avx_vtestc_pd:
9356 case Intrinsic::x86_avx_vtestnzc_pd:
9357 case Intrinsic::x86_avx_vtestz_ps_256:
9358 case Intrinsic::x86_avx_vtestc_ps_256:
9359 case Intrinsic::x86_avx_vtestnzc_ps_256:
9360 case Intrinsic::x86_avx_vtestz_pd_256:
9361 case Intrinsic::x86_avx_vtestc_pd_256:
9362 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9363 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009364 unsigned X86CC = 0;
9365 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009366 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009367 case Intrinsic::x86_avx_vtestz_ps:
9368 case Intrinsic::x86_avx_vtestz_pd:
9369 case Intrinsic::x86_avx_vtestz_ps_256:
9370 case Intrinsic::x86_avx_vtestz_pd_256:
9371 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009372 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009373 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009374 // ZF = 1
9375 X86CC = X86::COND_E;
9376 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009377 case Intrinsic::x86_avx_vtestc_ps:
9378 case Intrinsic::x86_avx_vtestc_pd:
9379 case Intrinsic::x86_avx_vtestc_ps_256:
9380 case Intrinsic::x86_avx_vtestc_pd_256:
9381 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009382 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009383 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009384 // CF = 1
9385 X86CC = X86::COND_B;
9386 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009387 case Intrinsic::x86_avx_vtestnzc_ps:
9388 case Intrinsic::x86_avx_vtestnzc_pd:
9389 case Intrinsic::x86_avx_vtestnzc_ps_256:
9390 case Intrinsic::x86_avx_vtestnzc_pd_256:
9391 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009392 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009393 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009394 // ZF and CF = 0
9395 X86CC = X86::COND_A;
9396 break;
9397 }
Eric Christopherfd179292009-08-27 18:07:15 +00009398
Eric Christopher71c67532009-07-29 00:28:05 +00009399 SDValue LHS = Op.getOperand(1);
9400 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009401 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9402 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009403 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9404 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9405 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009406 }
Evan Cheng5759f972008-05-04 09:15:50 +00009407
Craig Topper80e46362012-01-23 06:16:53 +00009408 // SSE/AVX shift intrinsics
9409 case Intrinsic::x86_sse2_psll_w:
9410 case Intrinsic::x86_sse2_psll_d:
9411 case Intrinsic::x86_sse2_psll_q:
9412 case Intrinsic::x86_avx2_psll_w:
9413 case Intrinsic::x86_avx2_psll_d:
9414 case Intrinsic::x86_avx2_psll_q:
9415 return DAG.getNode(X86ISD::VSHL, dl, Op.getValueType(),
9416 Op.getOperand(1), Op.getOperand(2));
9417 case Intrinsic::x86_sse2_psrl_w:
9418 case Intrinsic::x86_sse2_psrl_d:
9419 case Intrinsic::x86_sse2_psrl_q:
9420 case Intrinsic::x86_avx2_psrl_w:
9421 case Intrinsic::x86_avx2_psrl_d:
9422 case Intrinsic::x86_avx2_psrl_q:
9423 return DAG.getNode(X86ISD::VSRL, dl, Op.getValueType(),
9424 Op.getOperand(1), Op.getOperand(2));
9425 case Intrinsic::x86_sse2_psra_w:
9426 case Intrinsic::x86_sse2_psra_d:
9427 case Intrinsic::x86_avx2_psra_w:
9428 case Intrinsic::x86_avx2_psra_d:
9429 return DAG.getNode(X86ISD::VSRA, dl, Op.getValueType(),
9430 Op.getOperand(1), Op.getOperand(2));
Evan Cheng5759f972008-05-04 09:15:50 +00009431 case Intrinsic::x86_sse2_pslli_w:
9432 case Intrinsic::x86_sse2_pslli_d:
9433 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009434 case Intrinsic::x86_avx2_pslli_w:
9435 case Intrinsic::x86_avx2_pslli_d:
9436 case Intrinsic::x86_avx2_pslli_q:
9437 return getTargetVShiftNode(X86ISD::VSHLI, dl, Op.getValueType(),
9438 Op.getOperand(1), Op.getOperand(2), DAG);
Evan Cheng5759f972008-05-04 09:15:50 +00009439 case Intrinsic::x86_sse2_psrli_w:
9440 case Intrinsic::x86_sse2_psrli_d:
9441 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009442 case Intrinsic::x86_avx2_psrli_w:
9443 case Intrinsic::x86_avx2_psrli_d:
9444 case Intrinsic::x86_avx2_psrli_q:
9445 return getTargetVShiftNode(X86ISD::VSRLI, dl, Op.getValueType(),
9446 Op.getOperand(1), Op.getOperand(2), DAG);
Evan Cheng5759f972008-05-04 09:15:50 +00009447 case Intrinsic::x86_sse2_psrai_w:
9448 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +00009449 case Intrinsic::x86_avx2_psrai_w:
9450 case Intrinsic::x86_avx2_psrai_d:
9451 return getTargetVShiftNode(X86ISD::VSRAI, dl, Op.getValueType(),
9452 Op.getOperand(1), Op.getOperand(2), DAG);
9453 // Fix vector shift instructions where the last operand is a non-immediate
9454 // i32 value.
Evan Cheng5759f972008-05-04 09:15:50 +00009455 case Intrinsic::x86_mmx_pslli_w:
9456 case Intrinsic::x86_mmx_pslli_d:
9457 case Intrinsic::x86_mmx_pslli_q:
9458 case Intrinsic::x86_mmx_psrli_w:
9459 case Intrinsic::x86_mmx_psrli_d:
9460 case Intrinsic::x86_mmx_psrli_q:
9461 case Intrinsic::x86_mmx_psrai_w:
9462 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009463 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009464 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009465 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009466
9467 unsigned NewIntNo = 0;
Evan Cheng5759f972008-05-04 09:15:50 +00009468 switch (IntNo) {
Craig Topper80e46362012-01-23 06:16:53 +00009469 case Intrinsic::x86_mmx_pslli_w:
9470 NewIntNo = Intrinsic::x86_mmx_psll_w;
Evan Cheng5759f972008-05-04 09:15:50 +00009471 break;
Craig Topper80e46362012-01-23 06:16:53 +00009472 case Intrinsic::x86_mmx_pslli_d:
9473 NewIntNo = Intrinsic::x86_mmx_psll_d;
Evan Cheng5759f972008-05-04 09:15:50 +00009474 break;
Craig Topper80e46362012-01-23 06:16:53 +00009475 case Intrinsic::x86_mmx_pslli_q:
9476 NewIntNo = Intrinsic::x86_mmx_psll_q;
Evan Cheng5759f972008-05-04 09:15:50 +00009477 break;
Craig Topper80e46362012-01-23 06:16:53 +00009478 case Intrinsic::x86_mmx_psrli_w:
9479 NewIntNo = Intrinsic::x86_mmx_psrl_w;
Evan Cheng5759f972008-05-04 09:15:50 +00009480 break;
Craig Topper80e46362012-01-23 06:16:53 +00009481 case Intrinsic::x86_mmx_psrli_d:
9482 NewIntNo = Intrinsic::x86_mmx_psrl_d;
Evan Cheng5759f972008-05-04 09:15:50 +00009483 break;
Craig Topper80e46362012-01-23 06:16:53 +00009484 case Intrinsic::x86_mmx_psrli_q:
9485 NewIntNo = Intrinsic::x86_mmx_psrl_q;
Evan Cheng5759f972008-05-04 09:15:50 +00009486 break;
Craig Topper80e46362012-01-23 06:16:53 +00009487 case Intrinsic::x86_mmx_psrai_w:
9488 NewIntNo = Intrinsic::x86_mmx_psra_w;
Evan Cheng5759f972008-05-04 09:15:50 +00009489 break;
Craig Topper80e46362012-01-23 06:16:53 +00009490 case Intrinsic::x86_mmx_psrai_d:
9491 NewIntNo = Intrinsic::x86_mmx_psra_d;
Evan Cheng5759f972008-05-04 09:15:50 +00009492 break;
Craig Topper80e46362012-01-23 06:16:53 +00009493 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009494 }
Mon P Wangefa42202009-09-03 19:56:25 +00009495
9496 // The vector shift intrinsics with scalars uses 32b shift amounts but
9497 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9498 // to be zero.
Craig Topper80e46362012-01-23 06:16:53 +00009499 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, ShAmt,
9500 DAG.getConstant(0, MVT::i32));
Dale Johannesen0488fb62010-09-30 23:57:10 +00009501// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009502
Owen Andersone50ed302009-08-10 22:56:29 +00009503 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009504 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009505 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009506 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009507 Op.getOperand(1), ShAmt);
9508 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009509 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009510}
Evan Cheng72261582005-12-20 06:22:03 +00009511
Dan Gohmand858e902010-04-17 15:26:15 +00009512SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9513 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009514 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9515 MFI->setReturnAddressIsTaken(true);
9516
Bill Wendling64e87322009-01-16 19:25:27 +00009517 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009518 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009519
9520 if (Depth > 0) {
9521 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9522 SDValue Offset =
9523 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009524 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009525 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009526 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009527 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009528 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009529 }
9530
9531 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009532 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009533 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009534 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009535}
9536
Dan Gohmand858e902010-04-17 15:26:15 +00009537SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009538 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9539 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009540
Owen Andersone50ed302009-08-10 22:56:29 +00009541 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009542 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009543 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9544 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009545 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009546 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009547 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9548 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009549 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009550 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009551}
9552
Dan Gohman475871a2008-07-27 21:46:04 +00009553SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009554 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009555 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009556}
9557
Dan Gohmand858e902010-04-17 15:26:15 +00009558SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009559 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009560 SDValue Chain = Op.getOperand(0);
9561 SDValue Offset = Op.getOperand(1);
9562 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009563 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009564
Dan Gohmand8816272010-08-11 18:14:00 +00009565 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9566 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9567 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009568 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009569
Dan Gohmand8816272010-08-11 18:14:00 +00009570 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9571 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009572 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009573 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9574 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009575 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009576 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009577
Dale Johannesene4d209d2009-02-03 20:21:25 +00009578 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009579 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009580 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009581}
9582
Duncan Sands4a544a72011-09-06 13:37:06 +00009583SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9584 SelectionDAG &DAG) const {
9585 return Op.getOperand(0);
9586}
9587
9588SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9589 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009590 SDValue Root = Op.getOperand(0);
9591 SDValue Trmp = Op.getOperand(1); // trampoline
9592 SDValue FPtr = Op.getOperand(2); // nested function
9593 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009594 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009595
Dan Gohman69de1932008-02-06 22:27:42 +00009596 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009597
9598 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009599 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009600
9601 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009602 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9603 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009604
Evan Cheng0e6a0522011-07-18 20:57:22 +00009605 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9606 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009607
9608 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9609
9610 // Load the pointer to the nested function into R11.
9611 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009612 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009613 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009614 Addr, MachinePointerInfo(TrmpAddr),
9615 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009616
Owen Anderson825b72b2009-08-11 20:47:22 +00009617 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9618 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009619 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9620 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009621 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009622
9623 // Load the 'nest' parameter value into R10.
9624 // R10 is specified in X86CallingConv.td
9625 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009626 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9627 DAG.getConstant(10, MVT::i64));
9628 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009629 Addr, MachinePointerInfo(TrmpAddr, 10),
9630 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009631
Owen Anderson825b72b2009-08-11 20:47:22 +00009632 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9633 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009634 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9635 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009636 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009637
9638 // Jump to the nested function.
9639 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009640 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9641 DAG.getConstant(20, MVT::i64));
9642 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009643 Addr, MachinePointerInfo(TrmpAddr, 20),
9644 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009645
9646 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009647 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9648 DAG.getConstant(22, MVT::i64));
9649 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009650 MachinePointerInfo(TrmpAddr, 22),
9651 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009652
Duncan Sands4a544a72011-09-06 13:37:06 +00009653 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009654 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009655 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009656 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009657 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009658 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009659
9660 switch (CC) {
9661 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009662 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009663 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009664 case CallingConv::X86_StdCall: {
9665 // Pass 'nest' parameter in ECX.
9666 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009667 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009668
9669 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009670 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009671 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009672
Chris Lattner58d74912008-03-12 17:45:29 +00009673 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009674 unsigned InRegCount = 0;
9675 unsigned Idx = 1;
9676
9677 for (FunctionType::param_iterator I = FTy->param_begin(),
9678 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009679 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009680 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009681 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009682
9683 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009684 report_fatal_error("Nest register in use - reduce number of inreg"
9685 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009686 }
9687 }
9688 break;
9689 }
9690 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009691 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009692 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009693 // Pass 'nest' parameter in EAX.
9694 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009695 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009696 break;
9697 }
9698
Dan Gohman475871a2008-07-27 21:46:04 +00009699 SDValue OutChains[4];
9700 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009701
Owen Anderson825b72b2009-08-11 20:47:22 +00009702 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9703 DAG.getConstant(10, MVT::i32));
9704 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009705
Chris Lattnera62fe662010-02-05 19:20:30 +00009706 // This is storing the opcode for MOV32ri.
9707 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009708 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009709 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009710 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009711 Trmp, MachinePointerInfo(TrmpAddr),
9712 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009713
Owen Anderson825b72b2009-08-11 20:47:22 +00009714 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9715 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009716 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9717 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009718 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009719
Chris Lattnera62fe662010-02-05 19:20:30 +00009720 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009721 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9722 DAG.getConstant(5, MVT::i32));
9723 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009724 MachinePointerInfo(TrmpAddr, 5),
9725 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009726
Owen Anderson825b72b2009-08-11 20:47:22 +00009727 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9728 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009729 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9730 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009731 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009732
Duncan Sands4a544a72011-09-06 13:37:06 +00009733 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009734 }
9735}
9736
Dan Gohmand858e902010-04-17 15:26:15 +00009737SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9738 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009739 /*
9740 The rounding mode is in bits 11:10 of FPSR, and has the following
9741 settings:
9742 00 Round to nearest
9743 01 Round to -inf
9744 10 Round to +inf
9745 11 Round to 0
9746
9747 FLT_ROUNDS, on the other hand, expects the following:
9748 -1 Undefined
9749 0 Round to 0
9750 1 Round to nearest
9751 2 Round to +inf
9752 3 Round to -inf
9753
9754 To perform the conversion, we do:
9755 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9756 */
9757
9758 MachineFunction &MF = DAG.getMachineFunction();
9759 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009760 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009761 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009762 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009763 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009764
9765 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009766 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009767 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009768
Michael J. Spencerec38de22010-10-10 22:04:20 +00009769
Chris Lattner2156b792010-09-22 01:11:26 +00009770 MachineMemOperand *MMO =
9771 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9772 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009773
Chris Lattner2156b792010-09-22 01:11:26 +00009774 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9775 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9776 DAG.getVTList(MVT::Other),
9777 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009778
9779 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009780 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009781 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009782
9783 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009784 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009785 DAG.getNode(ISD::SRL, DL, MVT::i16,
9786 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009787 CWD, DAG.getConstant(0x800, MVT::i16)),
9788 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009789 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009790 DAG.getNode(ISD::SRL, DL, MVT::i16,
9791 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009792 CWD, DAG.getConstant(0x400, MVT::i16)),
9793 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009794
Dan Gohman475871a2008-07-27 21:46:04 +00009795 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009796 DAG.getNode(ISD::AND, DL, MVT::i16,
9797 DAG.getNode(ISD::ADD, DL, MVT::i16,
9798 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009799 DAG.getConstant(1, MVT::i16)),
9800 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009801
9802
Duncan Sands83ec4b62008-06-06 12:08:01 +00009803 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009804 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009805}
9806
Dan Gohmand858e902010-04-17 15:26:15 +00009807SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009808 EVT VT = Op.getValueType();
9809 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009810 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009811 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009812
9813 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009814 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009815 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009816 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009817 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009818 }
Evan Cheng18efe262007-12-14 02:13:44 +00009819
Evan Cheng152804e2007-12-14 08:30:15 +00009820 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009821 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009822 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009823
9824 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009825 SDValue Ops[] = {
9826 Op,
9827 DAG.getConstant(NumBits+NumBits-1, OpVT),
9828 DAG.getConstant(X86::COND_E, MVT::i8),
9829 Op.getValue(1)
9830 };
9831 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009832
9833 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009834 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009835
Owen Anderson825b72b2009-08-11 20:47:22 +00009836 if (VT == MVT::i8)
9837 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009838 return Op;
9839}
9840
Chandler Carruthacc068e2011-12-24 10:55:54 +00009841SDValue X86TargetLowering::LowerCTLZ_ZERO_UNDEF(SDValue Op,
9842 SelectionDAG &DAG) const {
9843 EVT VT = Op.getValueType();
9844 EVT OpVT = VT;
9845 unsigned NumBits = VT.getSizeInBits();
9846 DebugLoc dl = Op.getDebugLoc();
9847
9848 Op = Op.getOperand(0);
9849 if (VT == MVT::i8) {
9850 // Zero extend to i32 since there is not an i8 bsr.
9851 OpVT = MVT::i32;
9852 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
9853 }
9854
9855 // Issue a bsr (scan bits in reverse).
9856 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
9857 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
9858
9859 // And xor with NumBits-1.
9860 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
9861
9862 if (VT == MVT::i8)
9863 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
9864 return Op;
9865}
9866
Dan Gohmand858e902010-04-17 15:26:15 +00009867SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009868 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00009869 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009870 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009871 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +00009872
9873 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +00009874 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009875 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009876
9877 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009878 SDValue Ops[] = {
9879 Op,
Chandler Carruth77821022011-12-24 12:12:34 +00009880 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009881 DAG.getConstant(X86::COND_E, MVT::i8),
9882 Op.getValue(1)
9883 };
Chandler Carruth77821022011-12-24 12:12:34 +00009884 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +00009885}
9886
Craig Topper13894fa2011-08-24 06:14:18 +00009887// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9888// ones, and then concatenate the result back.
9889static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009890 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009891
9892 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9893 "Unsupported value type for operation");
9894
9895 int NumElems = VT.getVectorNumElements();
9896 DebugLoc dl = Op.getDebugLoc();
9897 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9898 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9899
9900 // Extract the LHS vectors
9901 SDValue LHS = Op.getOperand(0);
9902 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9903 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9904
9905 // Extract the RHS vectors
9906 SDValue RHS = Op.getOperand(1);
9907 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9908 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9909
9910 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9911 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9912
9913 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9914 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9915 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9916}
9917
9918SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9919 assert(Op.getValueType().getSizeInBits() == 256 &&
9920 Op.getValueType().isInteger() &&
9921 "Only handle AVX 256-bit vector integer operation");
9922 return Lower256IntArith(Op, DAG);
9923}
9924
9925SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9926 assert(Op.getValueType().getSizeInBits() == 256 &&
9927 Op.getValueType().isInteger() &&
9928 "Only handle AVX 256-bit vector integer operation");
9929 return Lower256IntArith(Op, DAG);
9930}
9931
9932SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9933 EVT VT = Op.getValueType();
9934
9935 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +00009936 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +00009937 return Lower256IntArith(Op, DAG);
9938
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009939 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009940
Craig Topperaaa643c2011-11-09 07:28:55 +00009941 SDValue A = Op.getOperand(0);
9942 SDValue B = Op.getOperand(1);
9943
9944 if (VT == MVT::v4i64) {
9945 assert(Subtarget->hasAVX2() && "Lowering v4i64 multiply requires AVX2");
9946
9947 // ulong2 Ahi = __builtin_ia32_psrlqi256( a, 32);
9948 // ulong2 Bhi = __builtin_ia32_psrlqi256( b, 32);
9949 // ulong2 AloBlo = __builtin_ia32_pmuludq256( a, b );
9950 // ulong2 AloBhi = __builtin_ia32_pmuludq256( a, Bhi );
9951 // ulong2 AhiBlo = __builtin_ia32_pmuludq256( Ahi, b );
9952 //
9953 // AloBhi = __builtin_ia32_psllqi256( AloBhi, 32 );
9954 // AhiBlo = __builtin_ia32_psllqi256( AhiBlo, 32 );
9955 // return AloBlo + AloBhi + AhiBlo;
9956
Craig Topper7fb8b0c2012-01-23 06:46:22 +00009957 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A,
9958 DAG.getConstant(32, MVT::i32));
9959 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B,
9960 DAG.getConstant(32, MVT::i32));
Craig Topperaaa643c2011-11-09 07:28:55 +00009961 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9962 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9963 A, B);
9964 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9965 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9966 A, Bhi);
9967 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9968 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9969 Ahi, B);
Craig Topper7fb8b0c2012-01-23 06:46:22 +00009970 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi,
9971 DAG.getConstant(32, MVT::i32));
9972 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo,
9973 DAG.getConstant(32, MVT::i32));
Craig Topperaaa643c2011-11-09 07:28:55 +00009974 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9975 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
9976 return Res;
9977 }
9978
9979 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
9980
Mon P Wangaf9b9522008-12-18 21:42:19 +00009981 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9982 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9983 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9984 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9985 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9986 //
9987 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9988 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9989 // return AloBlo + AloBhi + AhiBlo;
9990
Craig Topper7fb8b0c2012-01-23 06:46:22 +00009991 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A,
9992 DAG.getConstant(32, MVT::i32));
9993 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B,
9994 DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009995 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009996 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009997 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009998 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009999 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010000 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010001 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010002 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010003 Ahi, B);
Craig Topper7fb8b0c2012-01-23 06:46:22 +000010004 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi,
10005 DAG.getConstant(32, MVT::i32));
10006 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo,
10007 DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010008 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10009 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010010 return Res;
10011}
10012
Nadav Rotem43012222011-05-11 08:12:09 +000010013SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10014
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010015 EVT VT = Op.getValueType();
10016 DebugLoc dl = Op.getDebugLoc();
10017 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010018 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010019 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010020
Craig Topper1accb7e2012-01-10 06:54:16 +000010021 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010022 return SDValue();
10023
Nadav Rotem43012222011-05-11 08:12:09 +000010024 // Optimize shl/srl/sra with constant shift amount.
10025 if (isSplatVector(Amt.getNode())) {
10026 SDValue SclrAmt = Amt->getOperand(0);
10027 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10028 uint64_t ShiftAmt = C->getZExtValue();
10029
Craig Toppered2e13d2012-01-22 19:15:14 +000010030 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10031 (Subtarget->hasAVX2() &&
10032 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10033 if (Op.getOpcode() == ISD::SHL)
10034 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10035 DAG.getConstant(ShiftAmt, MVT::i32));
10036 if (Op.getOpcode() == ISD::SRL)
10037 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10038 DAG.getConstant(ShiftAmt, MVT::i32));
10039 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10040 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10041 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010042 }
10043
Craig Toppered2e13d2012-01-22 19:15:14 +000010044 if (VT == MVT::v16i8) {
10045 if (Op.getOpcode() == ISD::SHL) {
10046 // Make a large shift.
10047 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10048 DAG.getConstant(ShiftAmt, MVT::i32));
10049 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10050 // Zero out the rightmost bits.
10051 SmallVector<SDValue, 16> V(16,
10052 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10053 MVT::i8));
10054 return DAG.getNode(ISD::AND, dl, VT, SHL,
10055 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010056 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010057 if (Op.getOpcode() == ISD::SRL) {
10058 // Make a large shift.
10059 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10060 DAG.getConstant(ShiftAmt, MVT::i32));
10061 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10062 // Zero out the leftmost bits.
10063 SmallVector<SDValue, 16> V(16,
10064 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10065 MVT::i8));
10066 return DAG.getNode(ISD::AND, dl, VT, SRL,
10067 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10068 }
10069 if (Op.getOpcode() == ISD::SRA) {
10070 if (ShiftAmt == 7) {
10071 // R s>> 7 === R s< 0
10072 SDValue Zeros = getZeroVector(VT, /* HasSSE2 */true,
10073 /* HasAVX2 */false, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010074 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010075 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010076
Craig Toppered2e13d2012-01-22 19:15:14 +000010077 // R s>> a === ((R u>> a) ^ m) - m
10078 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10079 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10080 MVT::i8));
10081 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10082 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10083 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10084 return Res;
10085 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010086 }
Craig Topper46154eb2011-11-11 07:39:23 +000010087
Craig Topper0d86d462011-11-20 00:12:05 +000010088 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10089 if (Op.getOpcode() == ISD::SHL) {
10090 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010091 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10092 DAG.getConstant(ShiftAmt, MVT::i32));
10093 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010094 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010095 SmallVector<SDValue, 32> V(32,
10096 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10097 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010098 return DAG.getNode(ISD::AND, dl, VT, SHL,
10099 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010100 }
Craig Topper0d86d462011-11-20 00:12:05 +000010101 if (Op.getOpcode() == ISD::SRL) {
10102 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010103 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10104 DAG.getConstant(ShiftAmt, MVT::i32));
10105 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010106 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010107 SmallVector<SDValue, 32> V(32,
10108 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10109 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010110 return DAG.getNode(ISD::AND, dl, VT, SRL,
10111 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10112 }
10113 if (Op.getOpcode() == ISD::SRA) {
10114 if (ShiftAmt == 7) {
10115 // R s>> 7 === R s< 0
Craig Topper12216172012-01-13 08:12:35 +000010116 SDValue Zeros = getZeroVector(VT, true /* HasSSE2 */,
10117 true /* HasAVX2 */, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010118 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010119 }
10120
10121 // R s>> a === ((R u>> a) ^ m) - m
10122 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10123 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10124 MVT::i8));
10125 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10126 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10127 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10128 return Res;
10129 }
10130 }
Nadav Rotem43012222011-05-11 08:12:09 +000010131 }
10132 }
10133
10134 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010135 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010136 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
10137 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000010138
10139 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010140
Nate Begeman51409212010-07-28 00:21:48 +000010141 std::vector<Constant*> CV(4, CI);
10142 Constant *C = ConstantVector::get(CV);
10143 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10144 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010145 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010146 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010147
10148 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010149 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010150 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10151 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10152 }
Nadav Rotem43012222011-05-11 08:12:09 +000010153 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000010154 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000010155
Nate Begeman51409212010-07-28 00:21:48 +000010156 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000010157 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
10158 DAG.getConstant(5, MVT::i32));
10159 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010160
Lang Hames8b99c1e2011-12-17 01:08:46 +000010161 // Turn 'a' into a mask suitable for VSELECT
10162 SDValue VSelM = DAG.getConstant(0x80, VT);
10163 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010164 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010165
Lang Hames8b99c1e2011-12-17 01:08:46 +000010166 SDValue CM1 = DAG.getConstant(0x0f, VT);
10167 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010168
Lang Hames8b99c1e2011-12-17 01:08:46 +000010169 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10170 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000010171 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10172 DAG.getConstant(4, MVT::i32), DAG);
10173 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010174 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10175
Nate Begeman51409212010-07-28 00:21:48 +000010176 // a += a
10177 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010178 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010179 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010180
Lang Hames8b99c1e2011-12-17 01:08:46 +000010181 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10182 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000010183 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10184 DAG.getConstant(2, MVT::i32), DAG);
10185 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010186 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10187
Nate Begeman51409212010-07-28 00:21:48 +000010188 // a += a
10189 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010190 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010191 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010192
Lang Hames8b99c1e2011-12-17 01:08:46 +000010193 // return VSELECT(r, r+r, a);
10194 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010195 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010196 return R;
10197 }
Craig Topper46154eb2011-11-11 07:39:23 +000010198
10199 // Decompose 256-bit shifts into smaller 128-bit shifts.
10200 if (VT.getSizeInBits() == 256) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010201 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000010202 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10203 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10204
10205 // Extract the two vectors
10206 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10207 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10208 DAG, dl);
10209
10210 // Recreate the shift amount vectors
10211 SDValue Amt1, Amt2;
10212 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10213 // Constant shift amount
10214 SmallVector<SDValue, 4> Amt1Csts;
10215 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000010216 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010217 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000010218 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010219 Amt2Csts.push_back(Amt->getOperand(i));
10220
10221 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10222 &Amt1Csts[0], NumElems/2);
10223 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10224 &Amt2Csts[0], NumElems/2);
10225 } else {
10226 // Variable shift amount
10227 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10228 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10229 DAG, dl);
10230 }
10231
10232 // Issue new vector shifts for the smaller types
10233 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10234 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10235
10236 // Concatenate the result back
10237 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10238 }
10239
Nate Begeman51409212010-07-28 00:21:48 +000010240 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010241}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010242
Dan Gohmand858e902010-04-17 15:26:15 +000010243SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010244 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10245 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010246 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10247 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010248 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010249 SDValue LHS = N->getOperand(0);
10250 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010251 unsigned BaseOp = 0;
10252 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010253 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010254 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010255 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010256 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010257 // A subtract of one will be selected as a INC. Note that INC doesn't
10258 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010259 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10260 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010261 BaseOp = X86ISD::INC;
10262 Cond = X86::COND_O;
10263 break;
10264 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010265 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010266 Cond = X86::COND_O;
10267 break;
10268 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010269 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010270 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010271 break;
10272 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010273 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10274 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010275 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10276 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010277 BaseOp = X86ISD::DEC;
10278 Cond = X86::COND_O;
10279 break;
10280 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010281 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010282 Cond = X86::COND_O;
10283 break;
10284 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010285 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010286 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010287 break;
10288 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010289 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010290 Cond = X86::COND_O;
10291 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010292 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10293 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10294 MVT::i32);
10295 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010296
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010297 SDValue SetCC =
10298 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10299 DAG.getConstant(X86::COND_O, MVT::i32),
10300 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010301
Dan Gohman6e5fda22011-07-22 18:45:15 +000010302 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010303 }
Bill Wendling74c37652008-12-09 22:08:41 +000010304 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010305
Bill Wendling61edeb52008-12-02 01:06:39 +000010306 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010307 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010308 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010309
Bill Wendling61edeb52008-12-02 01:06:39 +000010310 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010311 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10312 DAG.getConstant(Cond, MVT::i32),
10313 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010314
Dan Gohman6e5fda22011-07-22 18:45:15 +000010315 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010316}
10317
Chad Rosier30450e82011-12-22 22:35:21 +000010318SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
10319 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010320 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010321 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10322 EVT VT = Op.getValueType();
10323
Craig Toppered2e13d2012-01-22 19:15:14 +000010324 if (!Subtarget->hasSSE2() || !VT.isVector())
10325 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010326
Craig Toppered2e13d2012-01-22 19:15:14 +000010327 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10328 ExtraVT.getScalarType().getSizeInBits();
10329 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10330
10331 switch (VT.getSimpleVT().SimpleTy) {
10332 default: return SDValue();
10333 case MVT::v8i32:
10334 case MVT::v16i16:
10335 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010336 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000010337 if (!Subtarget->hasAVX2()) {
10338 // needs to be split
10339 int NumElems = VT.getVectorNumElements();
10340 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10341 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
Craig Toppera124f942011-11-21 01:12:36 +000010342
Craig Toppered2e13d2012-01-22 19:15:14 +000010343 // Extract the LHS vectors
10344 SDValue LHS = Op.getOperand(0);
10345 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10346 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000010347
Craig Toppered2e13d2012-01-22 19:15:14 +000010348 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10349 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000010350
Craig Toppered2e13d2012-01-22 19:15:14 +000010351 EVT ExtraEltVT = ExtraVT.getVectorElementType();
10352 int ExtraNumElems = ExtraVT.getVectorNumElements();
10353 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10354 ExtraNumElems/2);
10355 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000010356
Craig Toppered2e13d2012-01-22 19:15:14 +000010357 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10358 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000010359
Craig Toppered2e13d2012-01-22 19:15:14 +000010360 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10361 }
10362 // fall through
10363 case MVT::v4i32:
10364 case MVT::v8i16: {
10365 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
10366 Op.getOperand(0), ShAmt, DAG);
10367 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010368 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010369 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010370}
10371
10372
Eric Christopher9a9d2752010-07-22 02:48:34 +000010373SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10374 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010375
Eric Christopher77ed1352011-07-08 00:04:56 +000010376 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10377 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000010378 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010379 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010380 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010381 SDValue Ops[] = {
10382 DAG.getRegister(X86::ESP, MVT::i32), // Base
10383 DAG.getTargetConstant(1, MVT::i8), // Scale
10384 DAG.getRegister(0, MVT::i32), // Index
10385 DAG.getTargetConstant(0, MVT::i32), // Disp
10386 DAG.getRegister(0, MVT::i32), // Segment.
10387 Zero,
10388 Chain
10389 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010390 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010391 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10392 array_lengthof(Ops));
10393 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010394 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010395
Eric Christopher9a9d2752010-07-22 02:48:34 +000010396 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010397 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010398 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010399
Chris Lattner132929a2010-08-14 17:26:09 +000010400 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10401 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10402 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10403 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010404
Chris Lattner132929a2010-08-14 17:26:09 +000010405 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10406 if (!Op1 && !Op2 && !Op3 && Op4)
10407 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010408
Chris Lattner132929a2010-08-14 17:26:09 +000010409 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10410 if (Op1 && !Op2 && !Op3 && !Op4)
10411 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010412
10413 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010414 // (MFENCE)>;
10415 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010416}
10417
Eli Friedman14648462011-07-27 22:21:52 +000010418SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10419 SelectionDAG &DAG) const {
10420 DebugLoc dl = Op.getDebugLoc();
10421 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10422 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10423 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10424 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10425
10426 // The only fence that needs an instruction is a sequentially-consistent
10427 // cross-thread fence.
10428 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10429 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10430 // no-sse2). There isn't any reason to disable it if the target processor
10431 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000010432 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010433 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10434
10435 SDValue Chain = Op.getOperand(0);
10436 SDValue Zero = DAG.getConstant(0, MVT::i32);
10437 SDValue Ops[] = {
10438 DAG.getRegister(X86::ESP, MVT::i32), // Base
10439 DAG.getTargetConstant(1, MVT::i8), // Scale
10440 DAG.getRegister(0, MVT::i32), // Index
10441 DAG.getTargetConstant(0, MVT::i32), // Disp
10442 DAG.getRegister(0, MVT::i32), // Segment.
10443 Zero,
10444 Chain
10445 };
10446 SDNode *Res =
10447 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10448 array_lengthof(Ops));
10449 return SDValue(Res, 0);
10450 }
10451
10452 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10453 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10454}
10455
10456
Dan Gohmand858e902010-04-17 15:26:15 +000010457SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010458 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010459 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010460 unsigned Reg = 0;
10461 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010462 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010463 default:
10464 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010465 case MVT::i8: Reg = X86::AL; size = 1; break;
10466 case MVT::i16: Reg = X86::AX; size = 2; break;
10467 case MVT::i32: Reg = X86::EAX; size = 4; break;
10468 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010469 assert(Subtarget->is64Bit() && "Node not type legal!");
10470 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010471 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010472 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010473 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010474 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010475 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010476 Op.getOperand(1),
10477 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010478 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010479 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010480 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010481 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10482 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10483 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010484 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010485 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010486 return cpOut;
10487}
10488
Duncan Sands1607f052008-12-01 11:39:25 +000010489SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010490 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010491 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010492 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010493 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010494 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010495 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010496 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10497 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010498 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010499 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10500 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010501 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010502 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010503 rdx.getValue(1)
10504 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010505 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010506}
10507
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010508SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010509 SelectionDAG &DAG) const {
10510 EVT SrcVT = Op.getOperand(0).getValueType();
10511 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000010512 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010513 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010514 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010515 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010516 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010517 // i64 <=> MMX conversions are Legal.
10518 if (SrcVT==MVT::i64 && DstVT.isVector())
10519 return Op;
10520 if (DstVT==MVT::i64 && SrcVT.isVector())
10521 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010522 // MMX <=> MMX conversions are Legal.
10523 if (SrcVT.isVector() && DstVT.isVector())
10524 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010525 // All other conversions need to be expanded.
10526 return SDValue();
10527}
Chris Lattner5b856542010-12-20 00:59:46 +000010528
Dan Gohmand858e902010-04-17 15:26:15 +000010529SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010530 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010531 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010532 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010533 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010534 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010535 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010536 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010537 Node->getOperand(0),
10538 Node->getOperand(1), negOp,
10539 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010540 cast<AtomicSDNode>(Node)->getAlignment(),
10541 cast<AtomicSDNode>(Node)->getOrdering(),
10542 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010543}
10544
Eli Friedman327236c2011-08-24 20:50:09 +000010545static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10546 SDNode *Node = Op.getNode();
10547 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010548 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010549
10550 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010551 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10552 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10553 // (The only way to get a 16-byte store is cmpxchg16b)
10554 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10555 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10556 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010557 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10558 cast<AtomicSDNode>(Node)->getMemoryVT(),
10559 Node->getOperand(0),
10560 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010561 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010562 cast<AtomicSDNode>(Node)->getOrdering(),
10563 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010564 return Swap.getValue(1);
10565 }
10566 // Other atomic stores have a simple pattern.
10567 return Op;
10568}
10569
Chris Lattner5b856542010-12-20 00:59:46 +000010570static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10571 EVT VT = Op.getNode()->getValueType(0);
10572
10573 // Let legalize expand this if it isn't a legal type yet.
10574 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10575 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010576
Chris Lattner5b856542010-12-20 00:59:46 +000010577 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010578
Chris Lattner5b856542010-12-20 00:59:46 +000010579 unsigned Opc;
10580 bool ExtraOp = false;
10581 switch (Op.getOpcode()) {
10582 default: assert(0 && "Invalid code");
10583 case ISD::ADDC: Opc = X86ISD::ADD; break;
10584 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10585 case ISD::SUBC: Opc = X86ISD::SUB; break;
10586 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10587 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010588
Chris Lattner5b856542010-12-20 00:59:46 +000010589 if (!ExtraOp)
10590 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10591 Op.getOperand(1));
10592 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10593 Op.getOperand(1), Op.getOperand(2));
10594}
10595
Evan Cheng0db9fe62006-04-25 20:13:52 +000010596/// LowerOperation - Provide custom lowering hooks for some operations.
10597///
Dan Gohmand858e902010-04-17 15:26:15 +000010598SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010599 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010600 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010601 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010602 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010603 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010604 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10605 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010606 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010607 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010608 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010609 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10610 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10611 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010612 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010613 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010614 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10615 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10616 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010617 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010618 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010619 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010620 case ISD::SHL_PARTS:
10621 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010622 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010623 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010624 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010625 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010626 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010627 case ISD::FABS: return LowerFABS(Op, DAG);
10628 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010629 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010630 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010631 case ISD::SETCC: return LowerSETCC(Op, DAG);
10632 case ISD::SELECT: return LowerSELECT(Op, DAG);
10633 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010634 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010635 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010636 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010637 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010638 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010639 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10640 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010641 case ISD::FRAME_TO_ARGS_OFFSET:
10642 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010643 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010644 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010645 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10646 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010647 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010648 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000010649 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010650 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010651 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010652 case ISD::SRA:
10653 case ISD::SRL:
10654 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010655 case ISD::SADDO:
10656 case ISD::UADDO:
10657 case ISD::SSUBO:
10658 case ISD::USUBO:
10659 case ISD::SMULO:
10660 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010661 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010662 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010663 case ISD::ADDC:
10664 case ISD::ADDE:
10665 case ISD::SUBC:
10666 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010667 case ISD::ADD: return LowerADD(Op, DAG);
10668 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010669 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010670}
10671
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010672static void ReplaceATOMIC_LOAD(SDNode *Node,
10673 SmallVectorImpl<SDValue> &Results,
10674 SelectionDAG &DAG) {
10675 DebugLoc dl = Node->getDebugLoc();
10676 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10677
10678 // Convert wide load -> cmpxchg8b/cmpxchg16b
10679 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10680 // (The only way to get a 16-byte load is cmpxchg16b)
10681 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010682 SDValue Zero = DAG.getConstant(0, VT);
10683 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010684 Node->getOperand(0),
10685 Node->getOperand(1), Zero, Zero,
10686 cast<AtomicSDNode>(Node)->getMemOperand(),
10687 cast<AtomicSDNode>(Node)->getOrdering(),
10688 cast<AtomicSDNode>(Node)->getSynchScope());
10689 Results.push_back(Swap.getValue(0));
10690 Results.push_back(Swap.getValue(1));
10691}
10692
Duncan Sands1607f052008-12-01 11:39:25 +000010693void X86TargetLowering::
10694ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010695 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010696 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000010697 assert (Node->getValueType(0) == MVT::i64 &&
10698 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010699
10700 SDValue Chain = Node->getOperand(0);
10701 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010702 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010703 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010704 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010705 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010706 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010707 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010708 SDValue Result =
10709 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10710 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010711 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010712 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010713 Results.push_back(Result.getValue(2));
10714}
10715
Duncan Sands126d9072008-07-04 11:47:58 +000010716/// ReplaceNodeResults - Replace a node with an illegal result type
10717/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010718void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10719 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010720 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010721 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010722 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010723 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010724 assert(false && "Do not know how to custom type legalize this operation!");
10725 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010726 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010727 case ISD::ADDC:
10728 case ISD::ADDE:
10729 case ISD::SUBC:
10730 case ISD::SUBE:
10731 // We don't want to expand or promote these.
10732 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010733 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010734 std::pair<SDValue,SDValue> Vals =
10735 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010736 SDValue FIST = Vals.first, StackSlot = Vals.second;
10737 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010738 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010739 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010740 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010741 MachinePointerInfo(),
10742 false, false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010743 }
10744 return;
10745 }
10746 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010747 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010748 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010749 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010750 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010751 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010752 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010753 eax.getValue(2));
10754 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10755 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010756 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010757 Results.push_back(edx.getValue(1));
10758 return;
10759 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010760 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010761 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010762 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010763 bool Regs64bit = T == MVT::i128;
10764 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010765 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010766 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10767 DAG.getConstant(0, HalfT));
10768 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10769 DAG.getConstant(1, HalfT));
10770 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10771 Regs64bit ? X86::RAX : X86::EAX,
10772 cpInL, SDValue());
10773 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10774 Regs64bit ? X86::RDX : X86::EDX,
10775 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010776 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010777 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10778 DAG.getConstant(0, HalfT));
10779 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10780 DAG.getConstant(1, HalfT));
10781 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10782 Regs64bit ? X86::RBX : X86::EBX,
10783 swapInL, cpInH.getValue(1));
10784 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10785 Regs64bit ? X86::RCX : X86::ECX,
10786 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010787 SDValue Ops[] = { swapInH.getValue(0),
10788 N->getOperand(1),
10789 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010790 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010791 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010792 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10793 X86ISD::LCMPXCHG8_DAG;
10794 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010795 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010796 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10797 Regs64bit ? X86::RAX : X86::EAX,
10798 HalfT, Result.getValue(1));
10799 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10800 Regs64bit ? X86::RDX : X86::EDX,
10801 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010802 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010803 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010804 Results.push_back(cpOutH.getValue(1));
10805 return;
10806 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010807 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010808 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10809 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010810 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010811 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10812 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010813 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010814 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10815 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010816 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010817 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10818 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010819 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010820 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10821 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010822 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010823 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10824 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010825 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010826 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10827 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010828 case ISD::ATOMIC_LOAD:
10829 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010830 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010831}
10832
Evan Cheng72261582005-12-20 06:22:03 +000010833const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10834 switch (Opcode) {
10835 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010836 case X86ISD::BSF: return "X86ISD::BSF";
10837 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010838 case X86ISD::SHLD: return "X86ISD::SHLD";
10839 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010840 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010841 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010842 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010843 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010844 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010845 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010846 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10847 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10848 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010849 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010850 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010851 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010852 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010853 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010854 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010855 case X86ISD::COMI: return "X86ISD::COMI";
10856 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010857 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010858 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010859 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10860 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010861 case X86ISD::CMOV: return "X86ISD::CMOV";
10862 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010863 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010864 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10865 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010866 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010867 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010868 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010869 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010870 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010871 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10872 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010873 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010874 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010875 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000010876 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000010877 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Craig Topperfe033152011-12-06 09:31:36 +000010878 case X86ISD::HADD: return "X86ISD::HADD";
10879 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000010880 case X86ISD::FHADD: return "X86ISD::FHADD";
10881 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000010882 case X86ISD::FMAX: return "X86ISD::FMAX";
10883 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010884 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10885 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010886 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010887 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010888 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010889 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010890 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010891 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10892 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010893 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10894 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10895 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10896 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10897 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10898 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010899 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10900 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Craig Toppered2e13d2012-01-22 19:15:14 +000010901 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
10902 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000010903 case X86ISD::VSHL: return "X86ISD::VSHL";
10904 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000010905 case X86ISD::VSRA: return "X86ISD::VSRA";
10906 case X86ISD::VSHLI: return "X86ISD::VSHLI";
10907 case X86ISD::VSRLI: return "X86ISD::VSRLI";
10908 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000010909 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000010910 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
10911 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010912 case X86ISD::ADD: return "X86ISD::ADD";
10913 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010914 case X86ISD::ADC: return "X86ISD::ADC";
10915 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010916 case X86ISD::SMUL: return "X86ISD::SMUL";
10917 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010918 case X86ISD::INC: return "X86ISD::INC";
10919 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010920 case X86ISD::OR: return "X86ISD::OR";
10921 case X86ISD::XOR: return "X86ISD::XOR";
10922 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000010923 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000010924 case X86ISD::BLSI: return "X86ISD::BLSI";
10925 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
10926 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000010927 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010928 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010929 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010930 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10931 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10932 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010933 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000010934 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010935 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010936 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010937 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010938 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10939 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010940 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10941 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10942 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010943 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10944 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000010945 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
10946 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010947 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000010948 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000010949 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010950 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010951 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010952 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010953 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010954 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010955 }
10956}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010957
Chris Lattnerc9addb72007-03-30 23:15:24 +000010958// isLegalAddressingMode - Return true if the addressing mode represented
10959// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010960bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010961 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010962 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010963 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010964 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010965
Chris Lattnerc9addb72007-03-30 23:15:24 +000010966 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010967 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010968 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010969
Chris Lattnerc9addb72007-03-30 23:15:24 +000010970 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010971 unsigned GVFlags =
10972 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010973
Chris Lattnerdfed4132009-07-10 07:38:24 +000010974 // If a reference to this global requires an extra load, we can't fold it.
10975 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010976 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010977
Chris Lattnerdfed4132009-07-10 07:38:24 +000010978 // If BaseGV requires a register for the PIC base, we cannot also have a
10979 // BaseReg specified.
10980 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000010981 return false;
Evan Cheng52787842007-08-01 23:46:47 +000010982
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010983 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000010984 if ((M != CodeModel::Small || R != Reloc::Static) &&
10985 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010986 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000010987 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010988
Chris Lattnerc9addb72007-03-30 23:15:24 +000010989 switch (AM.Scale) {
10990 case 0:
10991 case 1:
10992 case 2:
10993 case 4:
10994 case 8:
10995 // These scales always work.
10996 break;
10997 case 3:
10998 case 5:
10999 case 9:
11000 // These scales are formed with basereg+scalereg. Only accept if there is
11001 // no basereg yet.
11002 if (AM.HasBaseReg)
11003 return false;
11004 break;
11005 default: // Other stuff never works.
11006 return false;
11007 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011008
Chris Lattnerc9addb72007-03-30 23:15:24 +000011009 return true;
11010}
11011
11012
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011013bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011014 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011015 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011016 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11017 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011018 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011019 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011020 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011021}
11022
Owen Andersone50ed302009-08-10 22:56:29 +000011023bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011024 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011025 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011026 unsigned NumBits1 = VT1.getSizeInBits();
11027 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011028 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011029 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011030 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011031}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011032
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011033bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011034 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011035 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011036}
11037
Owen Andersone50ed302009-08-10 22:56:29 +000011038bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011039 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011040 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011041}
11042
Owen Andersone50ed302009-08-10 22:56:29 +000011043bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011044 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011045 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011046}
11047
Evan Cheng60c07e12006-07-05 22:17:51 +000011048/// isShuffleMaskLegal - Targets can use this to indicate that they only
11049/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11050/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11051/// are assumed to be legal.
11052bool
Eric Christopherfd179292009-08-27 18:07:15 +000011053X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011054 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011055 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011056 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011057 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011058
Nate Begemana09008b2009-10-19 02:17:23 +000011059 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011060 return (VT.getVectorNumElements() == 2 ||
11061 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11062 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011063 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011064 isPSHUFDMask(M, VT) ||
11065 isPSHUFHWMask(M, VT) ||
11066 isPSHUFLWMask(M, VT) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000011067 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000011068 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11069 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011070 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11071 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011072}
11073
Dan Gohman7d8143f2008-04-09 20:09:42 +000011074bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011075X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011076 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011077 unsigned NumElts = VT.getVectorNumElements();
11078 // FIXME: This collection of masks seems suspect.
11079 if (NumElts == 2)
11080 return true;
11081 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11082 return (isMOVLMask(Mask, VT) ||
11083 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011084 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
11085 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011086 }
11087 return false;
11088}
11089
11090//===----------------------------------------------------------------------===//
11091// X86 Scheduler Hooks
11092//===----------------------------------------------------------------------===//
11093
Mon P Wang63307c32008-05-05 19:05:59 +000011094// private utility function
11095MachineBasicBlock *
11096X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11097 MachineBasicBlock *MBB,
11098 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011099 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011100 unsigned LoadOpc,
11101 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011102 unsigned notOpc,
11103 unsigned EAXreg,
11104 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011105 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011106 // For the atomic bitwise operator, we generate
11107 // thisMBB:
11108 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011109 // ld t1 = [bitinstr.addr]
11110 // op t2 = t1, [bitinstr.val]
11111 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011112 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11113 // bz newMBB
11114 // fallthrough -->nextMBB
11115 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11116 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011117 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011118 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011119
Mon P Wang63307c32008-05-05 19:05:59 +000011120 /// First build the CFG
11121 MachineFunction *F = MBB->getParent();
11122 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011123 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11124 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11125 F->insert(MBBIter, newMBB);
11126 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011127
Dan Gohman14152b42010-07-06 20:24:04 +000011128 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11129 nextMBB->splice(nextMBB->begin(), thisMBB,
11130 llvm::next(MachineBasicBlock::iterator(bInstr)),
11131 thisMBB->end());
11132 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011133
Mon P Wang63307c32008-05-05 19:05:59 +000011134 // Update thisMBB to fall through to newMBB
11135 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011136
Mon P Wang63307c32008-05-05 19:05:59 +000011137 // newMBB jumps to itself and fall through to nextMBB
11138 newMBB->addSuccessor(nextMBB);
11139 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011140
Mon P Wang63307c32008-05-05 19:05:59 +000011141 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011142 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011143 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011144 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011145 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011146 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011147 int numArgs = bInstr->getNumOperands() - 1;
11148 for (int i=0; i < numArgs; ++i)
11149 argOpers[i] = &bInstr->getOperand(i+1);
11150
11151 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011152 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011153 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011154
Dale Johannesen140be2d2008-08-19 18:47:28 +000011155 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011156 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011157 for (int i=0; i <= lastAddrIndx; ++i)
11158 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011159
Dale Johannesen140be2d2008-08-19 18:47:28 +000011160 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011161 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011162 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011163 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011164 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011165 tt = t1;
11166
Dale Johannesen140be2d2008-08-19 18:47:28 +000011167 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011168 assert((argOpers[valArgIndx]->isReg() ||
11169 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011170 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011171 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011172 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011173 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011174 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011175 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011176 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011177
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011178 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011179 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011180
Dale Johannesene4d209d2009-02-03 20:21:25 +000011181 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011182 for (int i=0; i <= lastAddrIndx; ++i)
11183 (*MIB).addOperand(*argOpers[i]);
11184 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011185 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011186 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11187 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011188
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011189 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011190 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011191
Mon P Wang63307c32008-05-05 19:05:59 +000011192 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011193 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011194
Dan Gohman14152b42010-07-06 20:24:04 +000011195 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011196 return nextMBB;
11197}
11198
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011199// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011200MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011201X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11202 MachineBasicBlock *MBB,
11203 unsigned regOpcL,
11204 unsigned regOpcH,
11205 unsigned immOpcL,
11206 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011207 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011208 // For the atomic bitwise operator, we generate
11209 // thisMBB (instructions are in pairs, except cmpxchg8b)
11210 // ld t1,t2 = [bitinstr.addr]
11211 // newMBB:
11212 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11213 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011214 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011215 // mov ECX, EBX <- t5, t6
11216 // mov EAX, EDX <- t1, t2
11217 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11218 // mov t3, t4 <- EAX, EDX
11219 // bz newMBB
11220 // result in out1, out2
11221 // fallthrough -->nextMBB
11222
11223 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11224 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011225 const unsigned NotOpc = X86::NOT32r;
11226 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11227 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11228 MachineFunction::iterator MBBIter = MBB;
11229 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011230
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011231 /// First build the CFG
11232 MachineFunction *F = MBB->getParent();
11233 MachineBasicBlock *thisMBB = MBB;
11234 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11235 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11236 F->insert(MBBIter, newMBB);
11237 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011238
Dan Gohman14152b42010-07-06 20:24:04 +000011239 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11240 nextMBB->splice(nextMBB->begin(), thisMBB,
11241 llvm::next(MachineBasicBlock::iterator(bInstr)),
11242 thisMBB->end());
11243 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011244
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011245 // Update thisMBB to fall through to newMBB
11246 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011247
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011248 // newMBB jumps to itself and fall through to nextMBB
11249 newMBB->addSuccessor(nextMBB);
11250 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011251
Dale Johannesene4d209d2009-02-03 20:21:25 +000011252 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011253 // Insert instructions into newMBB based on incoming instruction
11254 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011255 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011256 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011257 MachineOperand& dest1Oper = bInstr->getOperand(0);
11258 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011259 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11260 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011261 argOpers[i] = &bInstr->getOperand(i+2);
11262
Dan Gohman71ea4e52010-05-14 21:01:44 +000011263 // We use some of the operands multiple times, so conservatively just
11264 // clear any kill flags that might be present.
11265 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11266 argOpers[i]->setIsKill(false);
11267 }
11268
Evan Chengad5b52f2010-01-08 19:14:57 +000011269 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011270 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011271
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011272 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011273 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011274 for (int i=0; i <= lastAddrIndx; ++i)
11275 (*MIB).addOperand(*argOpers[i]);
11276 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011277 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011278 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011279 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011280 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011281 MachineOperand newOp3 = *(argOpers[3]);
11282 if (newOp3.isImm())
11283 newOp3.setImm(newOp3.getImm()+4);
11284 else
11285 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011286 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011287 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011288
11289 // t3/4 are defined later, at the bottom of the loop
11290 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11291 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011292 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011293 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011294 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011295 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11296
Evan Cheng306b4ca2010-01-08 23:41:50 +000011297 // The subsequent operations should be using the destination registers of
11298 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011299 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011300 t1 = F->getRegInfo().createVirtualRegister(RC);
11301 t2 = F->getRegInfo().createVirtualRegister(RC);
11302 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11303 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011304 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011305 t1 = dest1Oper.getReg();
11306 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011307 }
11308
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011309 int valArgIndx = lastAddrIndx + 1;
11310 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011311 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011312 "invalid operand");
11313 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11314 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011315 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011316 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011317 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011318 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011319 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011320 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011321 (*MIB).addOperand(*argOpers[valArgIndx]);
11322 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011323 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011324 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011325 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011326 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011327 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011328 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011329 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011330 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011331 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011332 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011333
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011334 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011335 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011336 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011337 MIB.addReg(t2);
11338
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011339 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011340 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011341 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011342 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011343
Dale Johannesene4d209d2009-02-03 20:21:25 +000011344 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011345 for (int i=0; i <= lastAddrIndx; ++i)
11346 (*MIB).addOperand(*argOpers[i]);
11347
11348 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011349 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11350 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011351
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011352 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011353 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011354 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011355 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011356
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011357 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011358 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011359
Dan Gohman14152b42010-07-06 20:24:04 +000011360 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011361 return nextMBB;
11362}
11363
11364// private utility function
11365MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011366X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11367 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011368 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011369 // For the atomic min/max operator, we generate
11370 // thisMBB:
11371 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011372 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011373 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011374 // cmp t1, t2
11375 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011376 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011377 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11378 // bz newMBB
11379 // fallthrough -->nextMBB
11380 //
11381 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11382 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011383 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011384 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011385
Mon P Wang63307c32008-05-05 19:05:59 +000011386 /// First build the CFG
11387 MachineFunction *F = MBB->getParent();
11388 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011389 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11390 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11391 F->insert(MBBIter, newMBB);
11392 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011393
Dan Gohman14152b42010-07-06 20:24:04 +000011394 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11395 nextMBB->splice(nextMBB->begin(), thisMBB,
11396 llvm::next(MachineBasicBlock::iterator(mInstr)),
11397 thisMBB->end());
11398 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011399
Mon P Wang63307c32008-05-05 19:05:59 +000011400 // Update thisMBB to fall through to newMBB
11401 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011402
Mon P Wang63307c32008-05-05 19:05:59 +000011403 // newMBB jumps to newMBB and fall through to nextMBB
11404 newMBB->addSuccessor(nextMBB);
11405 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011406
Dale Johannesene4d209d2009-02-03 20:21:25 +000011407 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011408 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011409 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011410 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011411 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011412 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011413 int numArgs = mInstr->getNumOperands() - 1;
11414 for (int i=0; i < numArgs; ++i)
11415 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011416
Mon P Wang63307c32008-05-05 19:05:59 +000011417 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011418 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011419 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011420
Mon P Wangab3e7472008-05-05 22:56:23 +000011421 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011422 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011423 for (int i=0; i <= lastAddrIndx; ++i)
11424 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011425
Mon P Wang63307c32008-05-05 19:05:59 +000011426 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011427 assert((argOpers[valArgIndx]->isReg() ||
11428 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011429 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011430
11431 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011432 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011433 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011434 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011435 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011436 (*MIB).addOperand(*argOpers[valArgIndx]);
11437
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011438 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011439 MIB.addReg(t1);
11440
Dale Johannesene4d209d2009-02-03 20:21:25 +000011441 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011442 MIB.addReg(t1);
11443 MIB.addReg(t2);
11444
11445 // Generate movc
11446 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011447 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011448 MIB.addReg(t2);
11449 MIB.addReg(t1);
11450
11451 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011452 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011453 for (int i=0; i <= lastAddrIndx; ++i)
11454 (*MIB).addOperand(*argOpers[i]);
11455 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011456 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011457 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11458 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011459
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011460 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011461 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011462
Mon P Wang63307c32008-05-05 19:05:59 +000011463 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011464 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011465
Dan Gohman14152b42010-07-06 20:24:04 +000011466 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011467 return nextMBB;
11468}
11469
Eric Christopherf83a5de2009-08-27 18:08:16 +000011470// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011471// or XMM0_V32I8 in AVX all of this code can be replaced with that
11472// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011473MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011474X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011475 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000011476 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011477 "Target must have SSE4.2 or AVX features enabled");
11478
Eric Christopherb120ab42009-08-18 22:50:32 +000011479 DebugLoc dl = MI->getDebugLoc();
11480 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011481 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011482 if (!Subtarget->hasAVX()) {
11483 if (memArg)
11484 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11485 else
11486 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11487 } else {
11488 if (memArg)
11489 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11490 else
11491 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11492 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011493
Eric Christopher41c902f2010-11-30 08:20:21 +000011494 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011495 for (unsigned i = 0; i < numArgs; ++i) {
11496 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011497 if (!(Op.isReg() && Op.isImplicit()))
11498 MIB.addOperand(Op);
11499 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011500 BuildMI(*BB, MI, dl,
11501 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11502 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011503 .addReg(X86::XMM0);
11504
Dan Gohman14152b42010-07-06 20:24:04 +000011505 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011506 return BB;
11507}
11508
11509MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011510X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011511 DebugLoc dl = MI->getDebugLoc();
11512 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011513
Eric Christopher228232b2010-11-30 07:20:12 +000011514 // Address into RAX/EAX, other two args into ECX, EDX.
11515 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11516 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11517 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11518 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011519 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011520
Eric Christopher228232b2010-11-30 07:20:12 +000011521 unsigned ValOps = X86::AddrNumOperands;
11522 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11523 .addReg(MI->getOperand(ValOps).getReg());
11524 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11525 .addReg(MI->getOperand(ValOps+1).getReg());
11526
11527 // The instruction doesn't actually take any operands though.
11528 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011529
Eric Christopher228232b2010-11-30 07:20:12 +000011530 MI->eraseFromParent(); // The pseudo is gone now.
11531 return BB;
11532}
11533
11534MachineBasicBlock *
11535X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011536 DebugLoc dl = MI->getDebugLoc();
11537 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011538
Eric Christopher228232b2010-11-30 07:20:12 +000011539 // First arg in ECX, the second in EAX.
11540 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11541 .addReg(MI->getOperand(0).getReg());
11542 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11543 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011544
Eric Christopher228232b2010-11-30 07:20:12 +000011545 // The instruction doesn't actually take any operands though.
11546 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011547
Eric Christopher228232b2010-11-30 07:20:12 +000011548 MI->eraseFromParent(); // The pseudo is gone now.
11549 return BB;
11550}
11551
11552MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011553X86TargetLowering::EmitVAARG64WithCustomInserter(
11554 MachineInstr *MI,
11555 MachineBasicBlock *MBB) const {
11556 // Emit va_arg instruction on X86-64.
11557
11558 // Operands to this pseudo-instruction:
11559 // 0 ) Output : destination address (reg)
11560 // 1-5) Input : va_list address (addr, i64mem)
11561 // 6 ) ArgSize : Size (in bytes) of vararg type
11562 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11563 // 8 ) Align : Alignment of type
11564 // 9 ) EFLAGS (implicit-def)
11565
11566 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11567 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11568
11569 unsigned DestReg = MI->getOperand(0).getReg();
11570 MachineOperand &Base = MI->getOperand(1);
11571 MachineOperand &Scale = MI->getOperand(2);
11572 MachineOperand &Index = MI->getOperand(3);
11573 MachineOperand &Disp = MI->getOperand(4);
11574 MachineOperand &Segment = MI->getOperand(5);
11575 unsigned ArgSize = MI->getOperand(6).getImm();
11576 unsigned ArgMode = MI->getOperand(7).getImm();
11577 unsigned Align = MI->getOperand(8).getImm();
11578
11579 // Memory Reference
11580 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11581 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11582 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11583
11584 // Machine Information
11585 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11586 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11587 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11588 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11589 DebugLoc DL = MI->getDebugLoc();
11590
11591 // struct va_list {
11592 // i32 gp_offset
11593 // i32 fp_offset
11594 // i64 overflow_area (address)
11595 // i64 reg_save_area (address)
11596 // }
11597 // sizeof(va_list) = 24
11598 // alignment(va_list) = 8
11599
11600 unsigned TotalNumIntRegs = 6;
11601 unsigned TotalNumXMMRegs = 8;
11602 bool UseGPOffset = (ArgMode == 1);
11603 bool UseFPOffset = (ArgMode == 2);
11604 unsigned MaxOffset = TotalNumIntRegs * 8 +
11605 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11606
11607 /* Align ArgSize to a multiple of 8 */
11608 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11609 bool NeedsAlign = (Align > 8);
11610
11611 MachineBasicBlock *thisMBB = MBB;
11612 MachineBasicBlock *overflowMBB;
11613 MachineBasicBlock *offsetMBB;
11614 MachineBasicBlock *endMBB;
11615
11616 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11617 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11618 unsigned OffsetReg = 0;
11619
11620 if (!UseGPOffset && !UseFPOffset) {
11621 // If we only pull from the overflow region, we don't create a branch.
11622 // We don't need to alter control flow.
11623 OffsetDestReg = 0; // unused
11624 OverflowDestReg = DestReg;
11625
11626 offsetMBB = NULL;
11627 overflowMBB = thisMBB;
11628 endMBB = thisMBB;
11629 } else {
11630 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11631 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11632 // If not, pull from overflow_area. (branch to overflowMBB)
11633 //
11634 // thisMBB
11635 // | .
11636 // | .
11637 // offsetMBB overflowMBB
11638 // | .
11639 // | .
11640 // endMBB
11641
11642 // Registers for the PHI in endMBB
11643 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11644 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11645
11646 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11647 MachineFunction *MF = MBB->getParent();
11648 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11649 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11650 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11651
11652 MachineFunction::iterator MBBIter = MBB;
11653 ++MBBIter;
11654
11655 // Insert the new basic blocks
11656 MF->insert(MBBIter, offsetMBB);
11657 MF->insert(MBBIter, overflowMBB);
11658 MF->insert(MBBIter, endMBB);
11659
11660 // Transfer the remainder of MBB and its successor edges to endMBB.
11661 endMBB->splice(endMBB->begin(), thisMBB,
11662 llvm::next(MachineBasicBlock::iterator(MI)),
11663 thisMBB->end());
11664 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11665
11666 // Make offsetMBB and overflowMBB successors of thisMBB
11667 thisMBB->addSuccessor(offsetMBB);
11668 thisMBB->addSuccessor(overflowMBB);
11669
11670 // endMBB is a successor of both offsetMBB and overflowMBB
11671 offsetMBB->addSuccessor(endMBB);
11672 overflowMBB->addSuccessor(endMBB);
11673
11674 // Load the offset value into a register
11675 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11676 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11677 .addOperand(Base)
11678 .addOperand(Scale)
11679 .addOperand(Index)
11680 .addDisp(Disp, UseFPOffset ? 4 : 0)
11681 .addOperand(Segment)
11682 .setMemRefs(MMOBegin, MMOEnd);
11683
11684 // Check if there is enough room left to pull this argument.
11685 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11686 .addReg(OffsetReg)
11687 .addImm(MaxOffset + 8 - ArgSizeA8);
11688
11689 // Branch to "overflowMBB" if offset >= max
11690 // Fall through to "offsetMBB" otherwise
11691 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11692 .addMBB(overflowMBB);
11693 }
11694
11695 // In offsetMBB, emit code to use the reg_save_area.
11696 if (offsetMBB) {
11697 assert(OffsetReg != 0);
11698
11699 // Read the reg_save_area address.
11700 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11701 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11702 .addOperand(Base)
11703 .addOperand(Scale)
11704 .addOperand(Index)
11705 .addDisp(Disp, 16)
11706 .addOperand(Segment)
11707 .setMemRefs(MMOBegin, MMOEnd);
11708
11709 // Zero-extend the offset
11710 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11711 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11712 .addImm(0)
11713 .addReg(OffsetReg)
11714 .addImm(X86::sub_32bit);
11715
11716 // Add the offset to the reg_save_area to get the final address.
11717 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11718 .addReg(OffsetReg64)
11719 .addReg(RegSaveReg);
11720
11721 // Compute the offset for the next argument
11722 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11723 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11724 .addReg(OffsetReg)
11725 .addImm(UseFPOffset ? 16 : 8);
11726
11727 // Store it back into the va_list.
11728 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11729 .addOperand(Base)
11730 .addOperand(Scale)
11731 .addOperand(Index)
11732 .addDisp(Disp, UseFPOffset ? 4 : 0)
11733 .addOperand(Segment)
11734 .addReg(NextOffsetReg)
11735 .setMemRefs(MMOBegin, MMOEnd);
11736
11737 // Jump to endMBB
11738 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11739 .addMBB(endMBB);
11740 }
11741
11742 //
11743 // Emit code to use overflow area
11744 //
11745
11746 // Load the overflow_area address into a register.
11747 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11748 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11749 .addOperand(Base)
11750 .addOperand(Scale)
11751 .addOperand(Index)
11752 .addDisp(Disp, 8)
11753 .addOperand(Segment)
11754 .setMemRefs(MMOBegin, MMOEnd);
11755
11756 // If we need to align it, do so. Otherwise, just copy the address
11757 // to OverflowDestReg.
11758 if (NeedsAlign) {
11759 // Align the overflow address
11760 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11761 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11762
11763 // aligned_addr = (addr + (align-1)) & ~(align-1)
11764 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11765 .addReg(OverflowAddrReg)
11766 .addImm(Align-1);
11767
11768 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11769 .addReg(TmpReg)
11770 .addImm(~(uint64_t)(Align-1));
11771 } else {
11772 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11773 .addReg(OverflowAddrReg);
11774 }
11775
11776 // Compute the next overflow address after this argument.
11777 // (the overflow address should be kept 8-byte aligned)
11778 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11779 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11780 .addReg(OverflowDestReg)
11781 .addImm(ArgSizeA8);
11782
11783 // Store the new overflow address.
11784 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11785 .addOperand(Base)
11786 .addOperand(Scale)
11787 .addOperand(Index)
11788 .addDisp(Disp, 8)
11789 .addOperand(Segment)
11790 .addReg(NextAddrReg)
11791 .setMemRefs(MMOBegin, MMOEnd);
11792
11793 // If we branched, emit the PHI to the front of endMBB.
11794 if (offsetMBB) {
11795 BuildMI(*endMBB, endMBB->begin(), DL,
11796 TII->get(X86::PHI), DestReg)
11797 .addReg(OffsetDestReg).addMBB(offsetMBB)
11798 .addReg(OverflowDestReg).addMBB(overflowMBB);
11799 }
11800
11801 // Erase the pseudo instruction
11802 MI->eraseFromParent();
11803
11804 return endMBB;
11805}
11806
11807MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011808X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11809 MachineInstr *MI,
11810 MachineBasicBlock *MBB) const {
11811 // Emit code to save XMM registers to the stack. The ABI says that the
11812 // number of registers to save is given in %al, so it's theoretically
11813 // possible to do an indirect jump trick to avoid saving all of them,
11814 // however this code takes a simpler approach and just executes all
11815 // of the stores if %al is non-zero. It's less code, and it's probably
11816 // easier on the hardware branch predictor, and stores aren't all that
11817 // expensive anyway.
11818
11819 // Create the new basic blocks. One block contains all the XMM stores,
11820 // and one block is the final destination regardless of whether any
11821 // stores were performed.
11822 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11823 MachineFunction *F = MBB->getParent();
11824 MachineFunction::iterator MBBIter = MBB;
11825 ++MBBIter;
11826 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11827 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11828 F->insert(MBBIter, XMMSaveMBB);
11829 F->insert(MBBIter, EndMBB);
11830
Dan Gohman14152b42010-07-06 20:24:04 +000011831 // Transfer the remainder of MBB and its successor edges to EndMBB.
11832 EndMBB->splice(EndMBB->begin(), MBB,
11833 llvm::next(MachineBasicBlock::iterator(MI)),
11834 MBB->end());
11835 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11836
Dan Gohmand6708ea2009-08-15 01:38:56 +000011837 // The original block will now fall through to the XMM save block.
11838 MBB->addSuccessor(XMMSaveMBB);
11839 // The XMMSaveMBB will fall through to the end block.
11840 XMMSaveMBB->addSuccessor(EndMBB);
11841
11842 // Now add the instructions.
11843 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11844 DebugLoc DL = MI->getDebugLoc();
11845
11846 unsigned CountReg = MI->getOperand(0).getReg();
11847 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11848 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11849
11850 if (!Subtarget->isTargetWin64()) {
11851 // If %al is 0, branch around the XMM save block.
11852 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011853 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011854 MBB->addSuccessor(EndMBB);
11855 }
11856
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011857 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011858 // In the XMM save block, save all the XMM argument registers.
11859 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11860 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011861 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011862 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011863 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011864 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011865 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011866 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011867 .addFrameIndex(RegSaveFrameIndex)
11868 .addImm(/*Scale=*/1)
11869 .addReg(/*IndexReg=*/0)
11870 .addImm(/*Disp=*/Offset)
11871 .addReg(/*Segment=*/0)
11872 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011873 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011874 }
11875
Dan Gohman14152b42010-07-06 20:24:04 +000011876 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011877
11878 return EndMBB;
11879}
Mon P Wang63307c32008-05-05 19:05:59 +000011880
Evan Cheng60c07e12006-07-05 22:17:51 +000011881MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011882X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011883 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011884 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11885 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011886
Chris Lattner52600972009-09-02 05:57:00 +000011887 // To "insert" a SELECT_CC instruction, we actually have to insert the
11888 // diamond control-flow pattern. The incoming instruction knows the
11889 // destination vreg to set, the condition code register to branch on, the
11890 // true/false values to select between, and a branch opcode to use.
11891 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11892 MachineFunction::iterator It = BB;
11893 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011894
Chris Lattner52600972009-09-02 05:57:00 +000011895 // thisMBB:
11896 // ...
11897 // TrueVal = ...
11898 // cmpTY ccX, r1, r2
11899 // bCC copy1MBB
11900 // fallthrough --> copy0MBB
11901 MachineBasicBlock *thisMBB = BB;
11902 MachineFunction *F = BB->getParent();
11903 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11904 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011905 F->insert(It, copy0MBB);
11906 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011907
Bill Wendling730c07e2010-06-25 20:48:10 +000011908 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11909 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011910 if (!MI->killsRegister(X86::EFLAGS)) {
11911 copy0MBB->addLiveIn(X86::EFLAGS);
11912 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011913 }
11914
Dan Gohman14152b42010-07-06 20:24:04 +000011915 // Transfer the remainder of BB and its successor edges to sinkMBB.
11916 sinkMBB->splice(sinkMBB->begin(), BB,
11917 llvm::next(MachineBasicBlock::iterator(MI)),
11918 BB->end());
11919 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11920
11921 // Add the true and fallthrough blocks as its successors.
11922 BB->addSuccessor(copy0MBB);
11923 BB->addSuccessor(sinkMBB);
11924
11925 // Create the conditional branch instruction.
11926 unsigned Opc =
11927 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11928 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11929
Chris Lattner52600972009-09-02 05:57:00 +000011930 // copy0MBB:
11931 // %FalseValue = ...
11932 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011933 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011934
Chris Lattner52600972009-09-02 05:57:00 +000011935 // sinkMBB:
11936 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11937 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011938 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11939 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011940 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11941 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11942
Dan Gohman14152b42010-07-06 20:24:04 +000011943 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011944 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011945}
11946
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011947MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011948X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11949 bool Is64Bit) const {
11950 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11951 DebugLoc DL = MI->getDebugLoc();
11952 MachineFunction *MF = BB->getParent();
11953 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11954
Nick Lewycky8a8d4792011-12-02 22:16:29 +000011955 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011956
11957 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11958 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11959
11960 // BB:
11961 // ... [Till the alloca]
11962 // If stacklet is not large enough, jump to mallocMBB
11963 //
11964 // bumpMBB:
11965 // Allocate by subtracting from RSP
11966 // Jump to continueMBB
11967 //
11968 // mallocMBB:
11969 // Allocate by call to runtime
11970 //
11971 // continueMBB:
11972 // ...
11973 // [rest of original BB]
11974 //
11975
11976 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11977 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11978 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11979
11980 MachineRegisterInfo &MRI = MF->getRegInfo();
11981 const TargetRegisterClass *AddrRegClass =
11982 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
11983
11984 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11985 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11986 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000011987 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011988 sizeVReg = MI->getOperand(1).getReg(),
11989 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
11990
11991 MachineFunction::iterator MBBIter = BB;
11992 ++MBBIter;
11993
11994 MF->insert(MBBIter, bumpMBB);
11995 MF->insert(MBBIter, mallocMBB);
11996 MF->insert(MBBIter, continueMBB);
11997
11998 continueMBB->splice(continueMBB->begin(), BB, llvm::next
11999 (MachineBasicBlock::iterator(MI)), BB->end());
12000 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12001
12002 // Add code to the main basic block to check if the stack limit has been hit,
12003 // and if so, jump to mallocMBB otherwise to bumpMBB.
12004 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012005 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012006 .addReg(tmpSPVReg).addReg(sizeVReg);
12007 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000012008 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012009 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012010 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12011
12012 // bumpMBB simply decreases the stack pointer, since we know the current
12013 // stacklet has enough space.
12014 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012015 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012016 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012017 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012018 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12019
12020 // Calls into a routine in libgcc to allocate more space from the heap.
12021 if (Is64Bit) {
12022 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12023 .addReg(sizeVReg);
12024 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12025 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12026 } else {
12027 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12028 .addImm(12);
12029 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12030 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12031 .addExternalSymbol("__morestack_allocate_stack_space");
12032 }
12033
12034 if (!Is64Bit)
12035 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12036 .addImm(16);
12037
12038 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12039 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12040 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12041
12042 // Set up the CFG correctly.
12043 BB->addSuccessor(bumpMBB);
12044 BB->addSuccessor(mallocMBB);
12045 mallocMBB->addSuccessor(continueMBB);
12046 bumpMBB->addSuccessor(continueMBB);
12047
12048 // Take care of the PHI nodes.
12049 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12050 MI->getOperand(0).getReg())
12051 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12052 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12053
12054 // Delete the original pseudo instruction.
12055 MI->eraseFromParent();
12056
12057 // And we're done.
12058 return continueMBB;
12059}
12060
12061MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012062X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012063 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012064 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12065 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012066
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012067 assert(!Subtarget->isTargetEnvMacho());
12068
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012069 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12070 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012071
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012072 if (Subtarget->isTargetWin64()) {
12073 if (Subtarget->isTargetCygMing()) {
12074 // ___chkstk(Mingw64):
12075 // Clobbers R10, R11, RAX and EFLAGS.
12076 // Updates RSP.
12077 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12078 .addExternalSymbol("___chkstk")
12079 .addReg(X86::RAX, RegState::Implicit)
12080 .addReg(X86::RSP, RegState::Implicit)
12081 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12082 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12083 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12084 } else {
12085 // __chkstk(MSVCRT): does not update stack pointer.
12086 // Clobbers R10, R11 and EFLAGS.
12087 // FIXME: RAX(allocated size) might be reused and not killed.
12088 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12089 .addExternalSymbol("__chkstk")
12090 .addReg(X86::RAX, RegState::Implicit)
12091 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12092 // RAX has the offset to subtracted from RSP.
12093 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12094 .addReg(X86::RSP)
12095 .addReg(X86::RAX);
12096 }
12097 } else {
12098 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012099 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12100
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012101 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12102 .addExternalSymbol(StackProbeSymbol)
12103 .addReg(X86::EAX, RegState::Implicit)
12104 .addReg(X86::ESP, RegState::Implicit)
12105 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12106 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12107 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12108 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012109
Dan Gohman14152b42010-07-06 20:24:04 +000012110 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012111 return BB;
12112}
Chris Lattner52600972009-09-02 05:57:00 +000012113
12114MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012115X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12116 MachineBasicBlock *BB) const {
12117 // This is pretty easy. We're taking the value that we received from
12118 // our load from the relocation, sticking it in either RDI (x86-64)
12119 // or EAX and doing an indirect call. The return value will then
12120 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012121 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012122 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012123 DebugLoc DL = MI->getDebugLoc();
12124 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012125
12126 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012127 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012128
Eric Christopher30ef0e52010-06-03 04:07:48 +000012129 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012130 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12131 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012132 .addReg(X86::RIP)
12133 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012134 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012135 MI->getOperand(3).getTargetFlags())
12136 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012137 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012138 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012139 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012140 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12141 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012142 .addReg(0)
12143 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012144 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012145 MI->getOperand(3).getTargetFlags())
12146 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012147 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012148 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012149 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012150 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12151 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012152 .addReg(TII->getGlobalBaseReg(F))
12153 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012154 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012155 MI->getOperand(3).getTargetFlags())
12156 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012157 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012158 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012159 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012160
Dan Gohman14152b42010-07-06 20:24:04 +000012161 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012162 return BB;
12163}
12164
12165MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012166X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012167 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012168 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000012169 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012170 case X86::TAILJMPd64:
12171 case X86::TAILJMPr64:
12172 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000012173 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012174 case X86::TCRETURNdi64:
12175 case X86::TCRETURNri64:
12176 case X86::TCRETURNmi64:
12177 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12178 // On AMD64, additional defs should be added before register allocation.
12179 if (!Subtarget->isTargetWin64()) {
12180 MI->addRegisterDefined(X86::RSI);
12181 MI->addRegisterDefined(X86::RDI);
12182 MI->addRegisterDefined(X86::XMM6);
12183 MI->addRegisterDefined(X86::XMM7);
12184 MI->addRegisterDefined(X86::XMM8);
12185 MI->addRegisterDefined(X86::XMM9);
12186 MI->addRegisterDefined(X86::XMM10);
12187 MI->addRegisterDefined(X86::XMM11);
12188 MI->addRegisterDefined(X86::XMM12);
12189 MI->addRegisterDefined(X86::XMM13);
12190 MI->addRegisterDefined(X86::XMM14);
12191 MI->addRegisterDefined(X86::XMM15);
12192 }
12193 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012194 case X86::WIN_ALLOCA:
12195 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012196 case X86::SEG_ALLOCA_32:
12197 return EmitLoweredSegAlloca(MI, BB, false);
12198 case X86::SEG_ALLOCA_64:
12199 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012200 case X86::TLSCall_32:
12201 case X86::TLSCall_64:
12202 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012203 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012204 case X86::CMOV_FR32:
12205 case X86::CMOV_FR64:
12206 case X86::CMOV_V4F32:
12207 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012208 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012209 case X86::CMOV_V8F32:
12210 case X86::CMOV_V4F64:
12211 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012212 case X86::CMOV_GR16:
12213 case X86::CMOV_GR32:
12214 case X86::CMOV_RFP32:
12215 case X86::CMOV_RFP64:
12216 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012217 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012218
Dale Johannesen849f2142007-07-03 00:53:03 +000012219 case X86::FP32_TO_INT16_IN_MEM:
12220 case X86::FP32_TO_INT32_IN_MEM:
12221 case X86::FP32_TO_INT64_IN_MEM:
12222 case X86::FP64_TO_INT16_IN_MEM:
12223 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012224 case X86::FP64_TO_INT64_IN_MEM:
12225 case X86::FP80_TO_INT16_IN_MEM:
12226 case X86::FP80_TO_INT32_IN_MEM:
12227 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012228 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12229 DebugLoc DL = MI->getDebugLoc();
12230
Evan Cheng60c07e12006-07-05 22:17:51 +000012231 // Change the floating point control register to use "round towards zero"
12232 // mode when truncating to an integer value.
12233 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012234 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012235 addFrameReference(BuildMI(*BB, MI, DL,
12236 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012237
12238 // Load the old value of the high byte of the control word...
12239 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012240 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012241 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012242 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012243
12244 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012245 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012246 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012247
12248 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012249 addFrameReference(BuildMI(*BB, MI, DL,
12250 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012251
12252 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012253 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012254 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012255
12256 // Get the X86 opcode to use.
12257 unsigned Opc;
12258 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012259 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012260 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12261 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12262 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12263 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12264 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12265 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012266 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12267 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12268 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012269 }
12270
12271 X86AddressMode AM;
12272 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012273 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012274 AM.BaseType = X86AddressMode::RegBase;
12275 AM.Base.Reg = Op.getReg();
12276 } else {
12277 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012278 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012279 }
12280 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012281 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012282 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012283 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012284 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012285 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012286 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012287 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012288 AM.GV = Op.getGlobal();
12289 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012290 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012291 }
Dan Gohman14152b42010-07-06 20:24:04 +000012292 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012293 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012294
12295 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012296 addFrameReference(BuildMI(*BB, MI, DL,
12297 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012298
Dan Gohman14152b42010-07-06 20:24:04 +000012299 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012300 return BB;
12301 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012302 // String/text processing lowering.
12303 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012304 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012305 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12306 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012307 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012308 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12309 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012310 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012311 return EmitPCMP(MI, BB, 5, false /* in mem */);
12312 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012313 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012314 return EmitPCMP(MI, BB, 5, true /* in mem */);
12315
Eric Christopher228232b2010-11-30 07:20:12 +000012316 // Thread synchronization.
12317 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012318 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012319 case X86::MWAIT:
12320 return EmitMwait(MI, BB);
12321
Eric Christopherb120ab42009-08-18 22:50:32 +000012322 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012323 case X86::ATOMAND32:
12324 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012325 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012326 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012327 X86::NOT32r, X86::EAX,
12328 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012329 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012330 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12331 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012332 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012333 X86::NOT32r, X86::EAX,
12334 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012335 case X86::ATOMXOR32:
12336 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012337 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012338 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012339 X86::NOT32r, X86::EAX,
12340 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012341 case X86::ATOMNAND32:
12342 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012343 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012344 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012345 X86::NOT32r, X86::EAX,
12346 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012347 case X86::ATOMMIN32:
12348 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12349 case X86::ATOMMAX32:
12350 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12351 case X86::ATOMUMIN32:
12352 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12353 case X86::ATOMUMAX32:
12354 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012355
12356 case X86::ATOMAND16:
12357 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12358 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012359 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012360 X86::NOT16r, X86::AX,
12361 X86::GR16RegisterClass);
12362 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012363 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012364 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012365 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012366 X86::NOT16r, X86::AX,
12367 X86::GR16RegisterClass);
12368 case X86::ATOMXOR16:
12369 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12370 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012371 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012372 X86::NOT16r, X86::AX,
12373 X86::GR16RegisterClass);
12374 case X86::ATOMNAND16:
12375 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12376 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012377 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012378 X86::NOT16r, X86::AX,
12379 X86::GR16RegisterClass, true);
12380 case X86::ATOMMIN16:
12381 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12382 case X86::ATOMMAX16:
12383 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12384 case X86::ATOMUMIN16:
12385 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12386 case X86::ATOMUMAX16:
12387 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12388
12389 case X86::ATOMAND8:
12390 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12391 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012392 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012393 X86::NOT8r, X86::AL,
12394 X86::GR8RegisterClass);
12395 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012396 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012397 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012398 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012399 X86::NOT8r, X86::AL,
12400 X86::GR8RegisterClass);
12401 case X86::ATOMXOR8:
12402 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12403 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012404 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012405 X86::NOT8r, X86::AL,
12406 X86::GR8RegisterClass);
12407 case X86::ATOMNAND8:
12408 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12409 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012410 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012411 X86::NOT8r, X86::AL,
12412 X86::GR8RegisterClass, true);
12413 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012414 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012415 case X86::ATOMAND64:
12416 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012417 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012418 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012419 X86::NOT64r, X86::RAX,
12420 X86::GR64RegisterClass);
12421 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012422 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12423 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012424 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012425 X86::NOT64r, X86::RAX,
12426 X86::GR64RegisterClass);
12427 case X86::ATOMXOR64:
12428 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012429 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012430 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012431 X86::NOT64r, X86::RAX,
12432 X86::GR64RegisterClass);
12433 case X86::ATOMNAND64:
12434 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12435 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012436 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012437 X86::NOT64r, X86::RAX,
12438 X86::GR64RegisterClass, true);
12439 case X86::ATOMMIN64:
12440 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12441 case X86::ATOMMAX64:
12442 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12443 case X86::ATOMUMIN64:
12444 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12445 case X86::ATOMUMAX64:
12446 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012447
12448 // This group does 64-bit operations on a 32-bit host.
12449 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012450 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012451 X86::AND32rr, X86::AND32rr,
12452 X86::AND32ri, X86::AND32ri,
12453 false);
12454 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012455 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012456 X86::OR32rr, X86::OR32rr,
12457 X86::OR32ri, X86::OR32ri,
12458 false);
12459 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012460 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012461 X86::XOR32rr, X86::XOR32rr,
12462 X86::XOR32ri, X86::XOR32ri,
12463 false);
12464 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012465 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012466 X86::AND32rr, X86::AND32rr,
12467 X86::AND32ri, X86::AND32ri,
12468 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012469 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012470 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012471 X86::ADD32rr, X86::ADC32rr,
12472 X86::ADD32ri, X86::ADC32ri,
12473 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012474 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012475 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012476 X86::SUB32rr, X86::SBB32rr,
12477 X86::SUB32ri, X86::SBB32ri,
12478 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012479 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012480 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012481 X86::MOV32rr, X86::MOV32rr,
12482 X86::MOV32ri, X86::MOV32ri,
12483 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012484 case X86::VASTART_SAVE_XMM_REGS:
12485 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012486
12487 case X86::VAARG_64:
12488 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012489 }
12490}
12491
12492//===----------------------------------------------------------------------===//
12493// X86 Optimization Hooks
12494//===----------------------------------------------------------------------===//
12495
Dan Gohman475871a2008-07-27 21:46:04 +000012496void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012497 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012498 APInt &KnownZero,
12499 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012500 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012501 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012502 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012503 assert((Opc >= ISD::BUILTIN_OP_END ||
12504 Opc == ISD::INTRINSIC_WO_CHAIN ||
12505 Opc == ISD::INTRINSIC_W_CHAIN ||
12506 Opc == ISD::INTRINSIC_VOID) &&
12507 "Should use MaskedValueIsZero if you don't know whether Op"
12508 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012509
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012510 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012511 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012512 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012513 case X86ISD::ADD:
12514 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012515 case X86ISD::ADC:
12516 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012517 case X86ISD::SMUL:
12518 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012519 case X86ISD::INC:
12520 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012521 case X86ISD::OR:
12522 case X86ISD::XOR:
12523 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012524 // These nodes' second result is a boolean.
12525 if (Op.getResNo() == 0)
12526 break;
12527 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012528 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012529 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12530 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012531 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012532 case ISD::INTRINSIC_WO_CHAIN: {
12533 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12534 unsigned NumLoBits = 0;
12535 switch (IntId) {
12536 default: break;
12537 case Intrinsic::x86_sse_movmsk_ps:
12538 case Intrinsic::x86_avx_movmsk_ps_256:
12539 case Intrinsic::x86_sse2_movmsk_pd:
12540 case Intrinsic::x86_avx_movmsk_pd_256:
12541 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000012542 case Intrinsic::x86_sse2_pmovmskb_128:
12543 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000012544 // High bits of movmskp{s|d}, pmovmskb are known zero.
12545 switch (IntId) {
12546 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12547 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12548 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12549 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12550 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12551 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000012552 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012553 }
12554 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12555 Mask.getBitWidth() - NumLoBits);
12556 break;
12557 }
12558 }
12559 break;
12560 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012561 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012562}
Chris Lattner259e97c2006-01-31 19:43:35 +000012563
Owen Andersonbc146b02010-09-21 20:42:50 +000012564unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12565 unsigned Depth) const {
12566 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12567 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12568 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012569
Owen Andersonbc146b02010-09-21 20:42:50 +000012570 // Fallback case.
12571 return 1;
12572}
12573
Evan Cheng206ee9d2006-07-07 08:33:52 +000012574/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012575/// node is a GlobalAddress + offset.
12576bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012577 const GlobalValue* &GA,
12578 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012579 if (N->getOpcode() == X86ISD::Wrapper) {
12580 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012581 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012582 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012583 return true;
12584 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012585 }
Evan Chengad4196b2008-05-12 19:56:52 +000012586 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012587}
12588
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012589/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12590/// same as extracting the high 128-bit part of 256-bit vector and then
12591/// inserting the result into the low part of a new 256-bit vector
12592static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12593 EVT VT = SVOp->getValueType(0);
12594 int NumElems = VT.getVectorNumElements();
12595
12596 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12597 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12598 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12599 SVOp->getMaskElt(j) >= 0)
12600 return false;
12601
12602 return true;
12603}
12604
12605/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12606/// same as extracting the low 128-bit part of 256-bit vector and then
12607/// inserting the result into the high part of a new 256-bit vector
12608static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12609 EVT VT = SVOp->getValueType(0);
12610 int NumElems = VT.getVectorNumElements();
12611
12612 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12613 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12614 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12615 SVOp->getMaskElt(j) >= 0)
12616 return false;
12617
12618 return true;
12619}
12620
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012621/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12622static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000012623 TargetLowering::DAGCombinerInfo &DCI,
12624 bool HasAVX2) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012625 DebugLoc dl = N->getDebugLoc();
12626 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12627 SDValue V1 = SVOp->getOperand(0);
12628 SDValue V2 = SVOp->getOperand(1);
12629 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012630 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012631
12632 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12633 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12634 //
12635 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012636 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012637 // V UNDEF BUILD_VECTOR UNDEF
12638 // \ / \ /
12639 // CONCAT_VECTOR CONCAT_VECTOR
12640 // \ /
12641 // \ /
12642 // RESULT: V + zero extended
12643 //
12644 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12645 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12646 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12647 return SDValue();
12648
12649 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12650 return SDValue();
12651
12652 // To match the shuffle mask, the first half of the mask should
12653 // be exactly the first vector, and all the rest a splat with the
12654 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012655 for (int i = 0; i < NumElems/2; ++i)
12656 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12657 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12658 return SDValue();
12659
Chad Rosier3d1161e2012-01-03 21:05:52 +000012660 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
12661 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
12662 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
12663 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
12664 SDValue ResNode =
12665 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
12666 Ld->getMemoryVT(),
12667 Ld->getPointerInfo(),
12668 Ld->getAlignment(),
12669 false/*isVolatile*/, true/*ReadMem*/,
12670 false/*WriteMem*/);
12671 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
12672 }
12673
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012674 // Emit a zeroed vector and insert the desired subvector on its
12675 // first half.
Craig Topper12216172012-01-13 08:12:35 +000012676 SDValue Zeros = getZeroVector(VT, true /* HasSSE2 */, HasAVX2, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012677 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12678 DAG.getConstant(0, MVT::i32), DAG, dl);
12679 return DCI.CombineTo(N, InsV);
12680 }
12681
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012682 //===--------------------------------------------------------------------===//
12683 // Combine some shuffles into subvector extracts and inserts:
12684 //
12685
12686 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12687 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12688 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12689 DAG, dl);
12690 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12691 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12692 return DCI.CombineTo(N, InsV);
12693 }
12694
12695 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12696 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12697 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12698 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12699 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12700 return DCI.CombineTo(N, InsV);
12701 }
12702
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012703 return SDValue();
12704}
12705
12706/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012707static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012708 TargetLowering::DAGCombinerInfo &DCI,
12709 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012710 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012711 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012712
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012713 // Don't create instructions with illegal types after legalize types has run.
12714 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12715 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12716 return SDValue();
12717
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012718 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12719 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12720 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Craig Topper12216172012-01-13 08:12:35 +000012721 return PerformShuffleCombine256(N, DAG, DCI, Subtarget->hasAVX2());
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012722
12723 // Only handle 128 wide vector from here on.
12724 if (VT.getSizeInBits() != 128)
12725 return SDValue();
12726
12727 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12728 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12729 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012730 SmallVector<SDValue, 16> Elts;
12731 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012732 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012733
Nate Begemanfdea31a2010-03-24 20:49:50 +000012734 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012735}
Evan Chengd880b972008-05-09 21:53:03 +000012736
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012737/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12738/// generation and convert it from being a bunch of shuffles and extracts
12739/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012740static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12741 const TargetLowering &TLI) {
12742 SDValue InputVector = N->getOperand(0);
12743
12744 // Only operate on vectors of 4 elements, where the alternative shuffling
12745 // gets to be more expensive.
12746 if (InputVector.getValueType() != MVT::v4i32)
12747 return SDValue();
12748
12749 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12750 // single use which is a sign-extend or zero-extend, and all elements are
12751 // used.
12752 SmallVector<SDNode *, 4> Uses;
12753 unsigned ExtractedElements = 0;
12754 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12755 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12756 if (UI.getUse().getResNo() != InputVector.getResNo())
12757 return SDValue();
12758
12759 SDNode *Extract = *UI;
12760 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12761 return SDValue();
12762
12763 if (Extract->getValueType(0) != MVT::i32)
12764 return SDValue();
12765 if (!Extract->hasOneUse())
12766 return SDValue();
12767 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12768 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12769 return SDValue();
12770 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12771 return SDValue();
12772
12773 // Record which element was extracted.
12774 ExtractedElements |=
12775 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12776
12777 Uses.push_back(Extract);
12778 }
12779
12780 // If not all the elements were used, this may not be worthwhile.
12781 if (ExtractedElements != 15)
12782 return SDValue();
12783
12784 // Ok, we've now decided to do the transformation.
12785 DebugLoc dl = InputVector.getDebugLoc();
12786
12787 // Store the value to a temporary stack slot.
12788 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012789 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12790 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012791
12792 // Replace each use (extract) with a load of the appropriate element.
12793 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12794 UE = Uses.end(); UI != UE; ++UI) {
12795 SDNode *Extract = *UI;
12796
Nadav Rotem86694292011-05-17 08:31:57 +000012797 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012798 SDValue Idx = Extract->getOperand(1);
12799 unsigned EltSize =
12800 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12801 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12802 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12803
Nadav Rotem86694292011-05-17 08:31:57 +000012804 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012805 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012806
12807 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012808 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012809 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000012810 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012811
12812 // Replace the exact with the load.
12813 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12814 }
12815
12816 // The replacement was made in place; don't return anything.
12817 return SDValue();
12818}
12819
Duncan Sands6bcd2192011-09-17 16:49:39 +000012820/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
12821/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012822static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000012823 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012824 const X86Subtarget *Subtarget) {
12825 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012826 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012827 // Get the LHS/RHS of the select.
12828 SDValue LHS = N->getOperand(1);
12829 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000012830 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000012831
Dan Gohman670e5392009-09-21 18:03:22 +000012832 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012833 // instructions match the semantics of the common C idiom x<y?x:y but not
12834 // x<=y?x:y, because of how they handle negative zero (which can be
12835 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000012836 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
12837 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000012838 (Subtarget->hasSSE2() ||
12839 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012840 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012841
Chris Lattner47b4ce82009-03-11 05:48:52 +000012842 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012843 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012844 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12845 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012846 switch (CC) {
12847 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012848 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012849 // Converting this to a min would handle NaNs incorrectly, and swapping
12850 // the operands would cause it to handle comparisons between positive
12851 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012852 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012853 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012854 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12855 break;
12856 std::swap(LHS, RHS);
12857 }
Dan Gohman670e5392009-09-21 18:03:22 +000012858 Opcode = X86ISD::FMIN;
12859 break;
12860 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012861 // Converting this to a min would handle comparisons between positive
12862 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012863 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012864 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12865 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012866 Opcode = X86ISD::FMIN;
12867 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012868 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012869 // Converting this to a min would handle both negative zeros and NaNs
12870 // incorrectly, but we can swap the operands to fix both.
12871 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012872 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012873 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012874 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012875 Opcode = X86ISD::FMIN;
12876 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012877
Dan Gohman670e5392009-09-21 18:03:22 +000012878 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012879 // Converting this to a max would handle comparisons between positive
12880 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012881 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012882 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012883 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012884 Opcode = X86ISD::FMAX;
12885 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012886 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012887 // Converting this to a max would handle NaNs incorrectly, and swapping
12888 // the operands would cause it to handle comparisons between positive
12889 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012890 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012891 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012892 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12893 break;
12894 std::swap(LHS, RHS);
12895 }
Dan Gohman670e5392009-09-21 18:03:22 +000012896 Opcode = X86ISD::FMAX;
12897 break;
12898 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012899 // Converting this to a max would handle both negative zeros and NaNs
12900 // incorrectly, but we can swap the operands to fix both.
12901 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012902 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012903 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012904 case ISD::SETGE:
12905 Opcode = X86ISD::FMAX;
12906 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012907 }
Dan Gohman670e5392009-09-21 18:03:22 +000012908 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012909 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12910 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012911 switch (CC) {
12912 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012913 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012914 // Converting this to a min would handle comparisons between positive
12915 // and negative zero incorrectly, and swapping the operands would
12916 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012917 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012918 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012919 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012920 break;
12921 std::swap(LHS, RHS);
12922 }
Dan Gohman670e5392009-09-21 18:03:22 +000012923 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012924 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012925 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012926 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012927 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012928 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12929 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012930 Opcode = X86ISD::FMIN;
12931 break;
12932 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012933 // Converting this to a min would handle both negative zeros and NaNs
12934 // incorrectly, but we can swap the operands to fix both.
12935 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012936 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012937 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012938 case ISD::SETGE:
12939 Opcode = X86ISD::FMIN;
12940 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012941
Dan Gohman670e5392009-09-21 18:03:22 +000012942 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012943 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012944 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012945 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012946 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012947 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012948 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012949 // Converting this to a max would handle comparisons between positive
12950 // and negative zero incorrectly, and swapping the operands would
12951 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012952 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012953 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012954 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012955 break;
12956 std::swap(LHS, RHS);
12957 }
Dan Gohman670e5392009-09-21 18:03:22 +000012958 Opcode = X86ISD::FMAX;
12959 break;
12960 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012961 // Converting this to a max would handle both negative zeros and NaNs
12962 // incorrectly, but we can swap the operands to fix both.
12963 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012964 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012965 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012966 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012967 Opcode = X86ISD::FMAX;
12968 break;
12969 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012970 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012971
Chris Lattner47b4ce82009-03-11 05:48:52 +000012972 if (Opcode)
12973 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012974 }
Eric Christopherfd179292009-08-27 18:07:15 +000012975
Chris Lattnerd1980a52009-03-12 06:52:53 +000012976 // If this is a select between two integer constants, try to do some
12977 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012978 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12979 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012980 // Don't do this for crazy integer types.
12981 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12982 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012983 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012984 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012985
Chris Lattnercee56e72009-03-13 05:53:31 +000012986 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012987 // Efficiently invertible.
12988 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12989 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12990 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12991 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012992 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012993 }
Eric Christopherfd179292009-08-27 18:07:15 +000012994
Chris Lattnerd1980a52009-03-12 06:52:53 +000012995 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012996 if (FalseC->getAPIntValue() == 0 &&
12997 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012998 if (NeedsCondInvert) // Invert the condition if needed.
12999 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13000 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013001
Chris Lattnerd1980a52009-03-12 06:52:53 +000013002 // Zero extend the condition if needed.
13003 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013004
Chris Lattnercee56e72009-03-13 05:53:31 +000013005 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013006 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013007 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013008 }
Eric Christopherfd179292009-08-27 18:07:15 +000013009
Chris Lattner97a29a52009-03-13 05:22:11 +000013010 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013011 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013012 if (NeedsCondInvert) // Invert the condition if needed.
13013 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13014 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013015
Chris Lattner97a29a52009-03-13 05:22:11 +000013016 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013017 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13018 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013019 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013020 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013021 }
Eric Christopherfd179292009-08-27 18:07:15 +000013022
Chris Lattnercee56e72009-03-13 05:53:31 +000013023 // Optimize cases that will turn into an LEA instruction. This requires
13024 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013025 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013026 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013027 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013028
Chris Lattnercee56e72009-03-13 05:53:31 +000013029 bool isFastMultiplier = false;
13030 if (Diff < 10) {
13031 switch ((unsigned char)Diff) {
13032 default: break;
13033 case 1: // result = add base, cond
13034 case 2: // result = lea base( , cond*2)
13035 case 3: // result = lea base(cond, cond*2)
13036 case 4: // result = lea base( , cond*4)
13037 case 5: // result = lea base(cond, cond*4)
13038 case 8: // result = lea base( , cond*8)
13039 case 9: // result = lea base(cond, cond*8)
13040 isFastMultiplier = true;
13041 break;
13042 }
13043 }
Eric Christopherfd179292009-08-27 18:07:15 +000013044
Chris Lattnercee56e72009-03-13 05:53:31 +000013045 if (isFastMultiplier) {
13046 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13047 if (NeedsCondInvert) // Invert the condition if needed.
13048 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13049 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013050
Chris Lattnercee56e72009-03-13 05:53:31 +000013051 // Zero extend the condition if needed.
13052 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13053 Cond);
13054 // Scale the condition by the difference.
13055 if (Diff != 1)
13056 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13057 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013058
Chris Lattnercee56e72009-03-13 05:53:31 +000013059 // Add the base if non-zero.
13060 if (FalseC->getAPIntValue() != 0)
13061 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13062 SDValue(FalseC, 0));
13063 return Cond;
13064 }
Eric Christopherfd179292009-08-27 18:07:15 +000013065 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013066 }
13067 }
Eric Christopherfd179292009-08-27 18:07:15 +000013068
Evan Cheng56f582d2012-01-04 01:41:39 +000013069 // Canonicalize max and min:
13070 // (x > y) ? x : y -> (x >= y) ? x : y
13071 // (x < y) ? x : y -> (x <= y) ? x : y
13072 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
13073 // the need for an extra compare
13074 // against zero. e.g.
13075 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
13076 // subl %esi, %edi
13077 // testl %edi, %edi
13078 // movl $0, %eax
13079 // cmovgl %edi, %eax
13080 // =>
13081 // xorl %eax, %eax
13082 // subl %esi, $edi
13083 // cmovsl %eax, %edi
13084 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
13085 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13086 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
13087 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
13088 switch (CC) {
13089 default: break;
13090 case ISD::SETLT:
13091 case ISD::SETGT: {
13092 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
13093 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
13094 Cond.getOperand(0), Cond.getOperand(1), NewCC);
13095 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
13096 }
13097 }
13098 }
13099
Nadav Rotemcc616562012-01-15 19:27:55 +000013100 // If we know that this node is legal then we know that it is going to be
13101 // matched by one of the SSE/AVX BLEND instructions. These instructions only
13102 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
13103 // to simplify previous instructions.
13104 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13105 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
13106 !DCI.isBeforeLegalize() &&
13107 TLI.isOperationLegal(ISD::VSELECT, VT)) {
13108 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
13109 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
13110 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
13111
13112 APInt KnownZero, KnownOne;
13113 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
13114 DCI.isBeforeLegalizeOps());
13115 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
13116 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
13117 DCI.CommitTargetLoweringOpt(TLO);
13118 }
13119
Dan Gohman475871a2008-07-27 21:46:04 +000013120 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013121}
13122
Chris Lattnerd1980a52009-03-12 06:52:53 +000013123/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13124static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13125 TargetLowering::DAGCombinerInfo &DCI) {
13126 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013127
Chris Lattnerd1980a52009-03-12 06:52:53 +000013128 // If the flag operand isn't dead, don't touch this CMOV.
13129 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13130 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013131
Evan Chengb5a55d92011-05-24 01:48:22 +000013132 SDValue FalseOp = N->getOperand(0);
13133 SDValue TrueOp = N->getOperand(1);
13134 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13135 SDValue Cond = N->getOperand(3);
13136 if (CC == X86::COND_E || CC == X86::COND_NE) {
13137 switch (Cond.getOpcode()) {
13138 default: break;
13139 case X86ISD::BSR:
13140 case X86ISD::BSF:
13141 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13142 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13143 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13144 }
13145 }
13146
Chris Lattnerd1980a52009-03-12 06:52:53 +000013147 // If this is a select between two integer constants, try to do some
13148 // optimizations. Note that the operands are ordered the opposite of SELECT
13149 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013150 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13151 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013152 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13153 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013154 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13155 CC = X86::GetOppositeBranchCondition(CC);
13156 std::swap(TrueC, FalseC);
13157 }
Eric Christopherfd179292009-08-27 18:07:15 +000013158
Chris Lattnerd1980a52009-03-12 06:52:53 +000013159 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013160 // This is efficient for any integer data type (including i8/i16) and
13161 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013162 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013163 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13164 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013165
Chris Lattnerd1980a52009-03-12 06:52:53 +000013166 // Zero extend the condition if needed.
13167 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013168
Chris Lattnerd1980a52009-03-12 06:52:53 +000013169 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13170 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013171 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013172 if (N->getNumValues() == 2) // Dead flag value?
13173 return DCI.CombineTo(N, Cond, SDValue());
13174 return Cond;
13175 }
Eric Christopherfd179292009-08-27 18:07:15 +000013176
Chris Lattnercee56e72009-03-13 05:53:31 +000013177 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13178 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013179 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013180 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13181 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013182
Chris Lattner97a29a52009-03-13 05:22:11 +000013183 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013184 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13185 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013186 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13187 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013188
Chris Lattner97a29a52009-03-13 05:22:11 +000013189 if (N->getNumValues() == 2) // Dead flag value?
13190 return DCI.CombineTo(N, Cond, SDValue());
13191 return Cond;
13192 }
Eric Christopherfd179292009-08-27 18:07:15 +000013193
Chris Lattnercee56e72009-03-13 05:53:31 +000013194 // Optimize cases that will turn into an LEA instruction. This requires
13195 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013196 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013197 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013198 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013199
Chris Lattnercee56e72009-03-13 05:53:31 +000013200 bool isFastMultiplier = false;
13201 if (Diff < 10) {
13202 switch ((unsigned char)Diff) {
13203 default: break;
13204 case 1: // result = add base, cond
13205 case 2: // result = lea base( , cond*2)
13206 case 3: // result = lea base(cond, cond*2)
13207 case 4: // result = lea base( , cond*4)
13208 case 5: // result = lea base(cond, cond*4)
13209 case 8: // result = lea base( , cond*8)
13210 case 9: // result = lea base(cond, cond*8)
13211 isFastMultiplier = true;
13212 break;
13213 }
13214 }
Eric Christopherfd179292009-08-27 18:07:15 +000013215
Chris Lattnercee56e72009-03-13 05:53:31 +000013216 if (isFastMultiplier) {
13217 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013218 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13219 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013220 // Zero extend the condition if needed.
13221 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13222 Cond);
13223 // Scale the condition by the difference.
13224 if (Diff != 1)
13225 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13226 DAG.getConstant(Diff, Cond.getValueType()));
13227
13228 // Add the base if non-zero.
13229 if (FalseC->getAPIntValue() != 0)
13230 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13231 SDValue(FalseC, 0));
13232 if (N->getNumValues() == 2) // Dead flag value?
13233 return DCI.CombineTo(N, Cond, SDValue());
13234 return Cond;
13235 }
Eric Christopherfd179292009-08-27 18:07:15 +000013236 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013237 }
13238 }
13239 return SDValue();
13240}
13241
13242
Evan Cheng0b0cd912009-03-28 05:57:29 +000013243/// PerformMulCombine - Optimize a single multiply with constant into two
13244/// in order to implement it with two cheaper instructions, e.g.
13245/// LEA + SHL, LEA + LEA.
13246static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13247 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013248 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13249 return SDValue();
13250
Owen Andersone50ed302009-08-10 22:56:29 +000013251 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013252 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013253 return SDValue();
13254
13255 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13256 if (!C)
13257 return SDValue();
13258 uint64_t MulAmt = C->getZExtValue();
13259 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13260 return SDValue();
13261
13262 uint64_t MulAmt1 = 0;
13263 uint64_t MulAmt2 = 0;
13264 if ((MulAmt % 9) == 0) {
13265 MulAmt1 = 9;
13266 MulAmt2 = MulAmt / 9;
13267 } else if ((MulAmt % 5) == 0) {
13268 MulAmt1 = 5;
13269 MulAmt2 = MulAmt / 5;
13270 } else if ((MulAmt % 3) == 0) {
13271 MulAmt1 = 3;
13272 MulAmt2 = MulAmt / 3;
13273 }
13274 if (MulAmt2 &&
13275 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13276 DebugLoc DL = N->getDebugLoc();
13277
13278 if (isPowerOf2_64(MulAmt2) &&
13279 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13280 // If second multiplifer is pow2, issue it first. We want the multiply by
13281 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13282 // is an add.
13283 std::swap(MulAmt1, MulAmt2);
13284
13285 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013286 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013287 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013288 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013289 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013290 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013291 DAG.getConstant(MulAmt1, VT));
13292
Eric Christopherfd179292009-08-27 18:07:15 +000013293 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013294 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013295 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013296 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013297 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013298 DAG.getConstant(MulAmt2, VT));
13299
13300 // Do not add new nodes to DAG combiner worklist.
13301 DCI.CombineTo(N, NewMul, false);
13302 }
13303 return SDValue();
13304}
13305
Evan Chengad9c0a32009-12-15 00:53:42 +000013306static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13307 SDValue N0 = N->getOperand(0);
13308 SDValue N1 = N->getOperand(1);
13309 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13310 EVT VT = N0.getValueType();
13311
13312 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13313 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013314 if (VT.isInteger() && !VT.isVector() &&
13315 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013316 N0.getOperand(1).getOpcode() == ISD::Constant) {
13317 SDValue N00 = N0.getOperand(0);
13318 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13319 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13320 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13321 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13322 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13323 APInt ShAmt = N1C->getAPIntValue();
13324 Mask = Mask.shl(ShAmt);
13325 if (Mask != 0)
13326 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13327 N00, DAG.getConstant(Mask, VT));
13328 }
13329 }
13330
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013331
13332 // Hardware support for vector shifts is sparse which makes us scalarize the
13333 // vector operations in many cases. Also, on sandybridge ADD is faster than
13334 // shl.
13335 // (shl V, 1) -> add V,V
13336 if (isSplatVector(N1.getNode())) {
13337 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13338 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13339 // We shift all of the values by one. In many cases we do not have
13340 // hardware support for this operation. This is better expressed as an ADD
13341 // of two values.
13342 if (N1C && (1 == N1C->getZExtValue())) {
13343 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13344 }
13345 }
13346
Evan Chengad9c0a32009-12-15 00:53:42 +000013347 return SDValue();
13348}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013349
Nate Begeman740ab032009-01-26 00:52:55 +000013350/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13351/// when possible.
13352static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13353 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013354 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013355 if (N->getOpcode() == ISD::SHL) {
13356 SDValue V = PerformSHLCombine(N, DAG);
13357 if (V.getNode()) return V;
13358 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013359
Nate Begeman740ab032009-01-26 00:52:55 +000013360 // On X86 with SSE2 support, we can transform this to a vector shift if
13361 // all elements are shifted by the same amount. We can't do this in legalize
13362 // because the a constant vector is typically transformed to a constant pool
13363 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000013364 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013365 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013366
Craig Topper7be5dfd2011-11-12 09:58:49 +000013367 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
13368 (!Subtarget->hasAVX2() ||
13369 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013370 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013371
Mon P Wang3becd092009-01-28 08:12:05 +000013372 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013373 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013374 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013375 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013376 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13377 unsigned NumElts = VT.getVectorNumElements();
13378 unsigned i = 0;
13379 for (; i != NumElts; ++i) {
13380 SDValue Arg = ShAmtOp.getOperand(i);
13381 if (Arg.getOpcode() == ISD::UNDEF) continue;
13382 BaseShAmt = Arg;
13383 break;
13384 }
Craig Topper37c26772012-01-17 04:44:50 +000013385 // Handle the case where the build_vector is all undef
13386 // FIXME: Should DAG allow this?
13387 if (i == NumElts)
13388 return SDValue();
13389
Mon P Wang3becd092009-01-28 08:12:05 +000013390 for (; i != NumElts; ++i) {
13391 SDValue Arg = ShAmtOp.getOperand(i);
13392 if (Arg.getOpcode() == ISD::UNDEF) continue;
13393 if (Arg != BaseShAmt) {
13394 return SDValue();
13395 }
13396 }
13397 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013398 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013399 SDValue InVec = ShAmtOp.getOperand(0);
13400 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13401 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13402 unsigned i = 0;
13403 for (; i != NumElts; ++i) {
13404 SDValue Arg = InVec.getOperand(i);
13405 if (Arg.getOpcode() == ISD::UNDEF) continue;
13406 BaseShAmt = Arg;
13407 break;
13408 }
13409 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13410 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013411 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013412 if (C->getZExtValue() == SplatIdx)
13413 BaseShAmt = InVec.getOperand(1);
13414 }
13415 }
13416 if (BaseShAmt.getNode() == 0)
13417 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13418 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013419 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013420 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013421
Mon P Wangefa42202009-09-03 19:56:25 +000013422 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013423 if (EltVT.bitsGT(MVT::i32))
13424 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13425 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013426 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013427
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013428 // The shift amount is identical so we can do a vector shift.
13429 SDValue ValOp = N->getOperand(0);
13430 switch (N->getOpcode()) {
13431 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013432 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013433 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000013434 switch (VT.getSimpleVT().SimpleTy) {
13435 default: return SDValue();
13436 case MVT::v2i64:
13437 case MVT::v4i32:
13438 case MVT::v8i16:
13439 case MVT::v4i64:
13440 case MVT::v8i32:
13441 case MVT::v16i16:
13442 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
13443 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013444 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000013445 switch (VT.getSimpleVT().SimpleTy) {
13446 default: return SDValue();
13447 case MVT::v4i32:
13448 case MVT::v8i16:
13449 case MVT::v8i32:
13450 case MVT::v16i16:
13451 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
13452 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013453 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000013454 switch (VT.getSimpleVT().SimpleTy) {
13455 default: return SDValue();
13456 case MVT::v2i64:
13457 case MVT::v4i32:
13458 case MVT::v8i16:
13459 case MVT::v4i64:
13460 case MVT::v8i32:
13461 case MVT::v16i16:
13462 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
13463 }
Nate Begeman740ab032009-01-26 00:52:55 +000013464 }
Nate Begeman740ab032009-01-26 00:52:55 +000013465}
13466
Nate Begemanb65c1752010-12-17 22:55:37 +000013467
Stuart Hastings865f0932011-06-03 23:53:54 +000013468// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13469// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13470// and friends. Likewise for OR -> CMPNEQSS.
13471static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13472 TargetLowering::DAGCombinerInfo &DCI,
13473 const X86Subtarget *Subtarget) {
13474 unsigned opcode;
13475
13476 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13477 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000013478 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013479 SDValue N0 = N->getOperand(0);
13480 SDValue N1 = N->getOperand(1);
13481 SDValue CMP0 = N0->getOperand(1);
13482 SDValue CMP1 = N1->getOperand(1);
13483 DebugLoc DL = N->getDebugLoc();
13484
13485 // The SETCCs should both refer to the same CMP.
13486 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13487 return SDValue();
13488
13489 SDValue CMP00 = CMP0->getOperand(0);
13490 SDValue CMP01 = CMP0->getOperand(1);
13491 EVT VT = CMP00.getValueType();
13492
13493 if (VT == MVT::f32 || VT == MVT::f64) {
13494 bool ExpectingFlags = false;
13495 // Check for any users that want flags:
13496 for (SDNode::use_iterator UI = N->use_begin(),
13497 UE = N->use_end();
13498 !ExpectingFlags && UI != UE; ++UI)
13499 switch (UI->getOpcode()) {
13500 default:
13501 case ISD::BR_CC:
13502 case ISD::BRCOND:
13503 case ISD::SELECT:
13504 ExpectingFlags = true;
13505 break;
13506 case ISD::CopyToReg:
13507 case ISD::SIGN_EXTEND:
13508 case ISD::ZERO_EXTEND:
13509 case ISD::ANY_EXTEND:
13510 break;
13511 }
13512
13513 if (!ExpectingFlags) {
13514 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13515 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13516
13517 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13518 X86::CondCode tmp = cc0;
13519 cc0 = cc1;
13520 cc1 = tmp;
13521 }
13522
13523 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13524 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13525 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13526 X86ISD::NodeType NTOperator = is64BitFP ?
13527 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13528 // FIXME: need symbolic constants for these magic numbers.
13529 // See X86ATTInstPrinter.cpp:printSSECC().
13530 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13531 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13532 DAG.getConstant(x86cc, MVT::i8));
13533 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13534 OnesOrZeroesF);
13535 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13536 DAG.getConstant(1, MVT::i32));
13537 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13538 return OneBitOfTruth;
13539 }
13540 }
13541 }
13542 }
13543 return SDValue();
13544}
13545
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013546/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13547/// so it can be folded inside ANDNP.
13548static bool CanFoldXORWithAllOnes(const SDNode *N) {
13549 EVT VT = N->getValueType(0);
13550
13551 // Match direct AllOnes for 128 and 256-bit vectors
13552 if (ISD::isBuildVectorAllOnes(N))
13553 return true;
13554
13555 // Look through a bit convert.
13556 if (N->getOpcode() == ISD::BITCAST)
13557 N = N->getOperand(0).getNode();
13558
13559 // Sometimes the operand may come from a insert_subvector building a 256-bit
13560 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013561 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013562 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13563 SDValue V1 = N->getOperand(0);
13564 SDValue V2 = N->getOperand(1);
13565
13566 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13567 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13568 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13569 ISD::isBuildVectorAllOnes(V2.getNode()))
13570 return true;
13571 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013572
13573 return false;
13574}
13575
Nate Begemanb65c1752010-12-17 22:55:37 +000013576static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13577 TargetLowering::DAGCombinerInfo &DCI,
13578 const X86Subtarget *Subtarget) {
13579 if (DCI.isBeforeLegalizeOps())
13580 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013581
Stuart Hastings865f0932011-06-03 23:53:54 +000013582 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13583 if (R.getNode())
13584 return R;
13585
Craig Topper54a11172011-10-14 07:06:56 +000013586 EVT VT = N->getValueType(0);
13587
Craig Topperb4c94572011-10-21 06:55:01 +000013588 // Create ANDN, BLSI, and BLSR instructions
13589 // BLSI is X & (-X)
13590 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013591 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13592 SDValue N0 = N->getOperand(0);
13593 SDValue N1 = N->getOperand(1);
13594 DebugLoc DL = N->getDebugLoc();
13595
13596 // Check LHS for not
13597 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13598 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13599 // Check RHS for not
13600 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13601 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13602
Craig Topperb4c94572011-10-21 06:55:01 +000013603 // Check LHS for neg
13604 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13605 isZero(N0.getOperand(0)))
13606 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13607
13608 // Check RHS for neg
13609 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13610 isZero(N1.getOperand(0)))
13611 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13612
13613 // Check LHS for X-1
13614 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13615 isAllOnes(N0.getOperand(1)))
13616 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13617
13618 // Check RHS for X-1
13619 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13620 isAllOnes(N1.getOperand(1)))
13621 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13622
Craig Topper54a11172011-10-14 07:06:56 +000013623 return SDValue();
13624 }
13625
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013626 // Want to form ANDNP nodes:
13627 // 1) In the hopes of then easily combining them with OR and AND nodes
13628 // to form PBLEND/PSIGN.
13629 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013630 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013631 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013632
Nate Begemanb65c1752010-12-17 22:55:37 +000013633 SDValue N0 = N->getOperand(0);
13634 SDValue N1 = N->getOperand(1);
13635 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013636
Nate Begemanb65c1752010-12-17 22:55:37 +000013637 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013638 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013639 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13640 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013641 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013642
13643 // Check RHS for vnot
13644 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013645 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13646 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013647 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013648
Nate Begemanb65c1752010-12-17 22:55:37 +000013649 return SDValue();
13650}
13651
Evan Cheng760d1942010-01-04 21:22:48 +000013652static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013653 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013654 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013655 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013656 return SDValue();
13657
Stuart Hastings865f0932011-06-03 23:53:54 +000013658 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13659 if (R.getNode())
13660 return R;
13661
Evan Cheng760d1942010-01-04 21:22:48 +000013662 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000013663
Evan Cheng760d1942010-01-04 21:22:48 +000013664 SDValue N0 = N->getOperand(0);
13665 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013666
Nate Begemanb65c1752010-12-17 22:55:37 +000013667 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000013668 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000013669 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000013670 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
13671 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013672
Craig Topper1666cb62011-11-19 07:07:26 +000013673 // Canonicalize pandn to RHS
13674 if (N0.getOpcode() == X86ISD::ANDNP)
13675 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000013676 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000013677 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
13678 SDValue Mask = N1.getOperand(0);
13679 SDValue X = N1.getOperand(1);
13680 SDValue Y;
13681 if (N0.getOperand(0) == Mask)
13682 Y = N0.getOperand(1);
13683 if (N0.getOperand(1) == Mask)
13684 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013685
Craig Topper1666cb62011-11-19 07:07:26 +000013686 // Check to see if the mask appeared in both the AND and ANDNP and
13687 if (!Y.getNode())
13688 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013689
Craig Topper1666cb62011-11-19 07:07:26 +000013690 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13691 if (Mask.getOpcode() != ISD::BITCAST ||
13692 X.getOpcode() != ISD::BITCAST ||
13693 Y.getOpcode() != ISD::BITCAST)
13694 return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013695
Craig Topper1666cb62011-11-19 07:07:26 +000013696 // Look through mask bitcast.
13697 Mask = Mask.getOperand(0);
13698 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013699
Craig Toppered2e13d2012-01-22 19:15:14 +000013700 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000013701 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13702 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000013703 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000013704 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000013705
13706 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000013707 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000013708 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13709 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13710 if ((SraAmt + 1) != EltBits)
13711 return SDValue();
13712
13713 DebugLoc DL = N->getDebugLoc();
13714
13715 // Now we know we at least have a plendvb with the mask val. See if
13716 // we can form a psignb/w/d.
13717 // psign = x.type == y.type == mask.type && y = sub(0, x);
13718 X = X.getOperand(0);
13719 Y = Y.getOperand(0);
13720 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13721 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000013722 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
13723 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
13724 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000013725 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000013726 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000013727 }
13728 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000013729 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000013730 return SDValue();
13731
13732 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
13733
13734 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
13735 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
13736 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000013737 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000013738 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000013739 }
13740 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013741
Craig Topper1666cb62011-11-19 07:07:26 +000013742 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
13743 return SDValue();
13744
Nate Begemanb65c1752010-12-17 22:55:37 +000013745 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013746 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13747 std::swap(N0, N1);
13748 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13749 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013750 if (!N0.hasOneUse() || !N1.hasOneUse())
13751 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013752
13753 SDValue ShAmt0 = N0.getOperand(1);
13754 if (ShAmt0.getValueType() != MVT::i8)
13755 return SDValue();
13756 SDValue ShAmt1 = N1.getOperand(1);
13757 if (ShAmt1.getValueType() != MVT::i8)
13758 return SDValue();
13759 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13760 ShAmt0 = ShAmt0.getOperand(0);
13761 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13762 ShAmt1 = ShAmt1.getOperand(0);
13763
13764 DebugLoc DL = N->getDebugLoc();
13765 unsigned Opc = X86ISD::SHLD;
13766 SDValue Op0 = N0.getOperand(0);
13767 SDValue Op1 = N1.getOperand(0);
13768 if (ShAmt0.getOpcode() == ISD::SUB) {
13769 Opc = X86ISD::SHRD;
13770 std::swap(Op0, Op1);
13771 std::swap(ShAmt0, ShAmt1);
13772 }
13773
Evan Cheng8b1190a2010-04-28 01:18:01 +000013774 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013775 if (ShAmt1.getOpcode() == ISD::SUB) {
13776 SDValue Sum = ShAmt1.getOperand(0);
13777 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013778 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13779 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13780 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13781 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013782 return DAG.getNode(Opc, DL, VT,
13783 Op0, Op1,
13784 DAG.getNode(ISD::TRUNCATE, DL,
13785 MVT::i8, ShAmt0));
13786 }
13787 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13788 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13789 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013790 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013791 return DAG.getNode(Opc, DL, VT,
13792 N0.getOperand(0), N1.getOperand(0),
13793 DAG.getNode(ISD::TRUNCATE, DL,
13794 MVT::i8, ShAmt0));
13795 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013796
Evan Cheng760d1942010-01-04 21:22:48 +000013797 return SDValue();
13798}
13799
Craig Topper3738ccd2011-12-27 06:27:23 +000013800// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000013801static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
13802 TargetLowering::DAGCombinerInfo &DCI,
13803 const X86Subtarget *Subtarget) {
13804 if (DCI.isBeforeLegalizeOps())
13805 return SDValue();
13806
13807 EVT VT = N->getValueType(0);
13808
13809 if (VT != MVT::i32 && VT != MVT::i64)
13810 return SDValue();
13811
Craig Topper3738ccd2011-12-27 06:27:23 +000013812 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
13813
Craig Topperb4c94572011-10-21 06:55:01 +000013814 // Create BLSMSK instructions by finding X ^ (X-1)
13815 SDValue N0 = N->getOperand(0);
13816 SDValue N1 = N->getOperand(1);
13817 DebugLoc DL = N->getDebugLoc();
13818
13819 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13820 isAllOnes(N0.getOperand(1)))
13821 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
13822
13823 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13824 isAllOnes(N1.getOperand(1)))
13825 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
13826
13827 return SDValue();
13828}
13829
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013830/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
13831static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
13832 const X86Subtarget *Subtarget) {
13833 LoadSDNode *Ld = cast<LoadSDNode>(N);
13834 EVT RegVT = Ld->getValueType(0);
13835 EVT MemVT = Ld->getMemoryVT();
13836 DebugLoc dl = Ld->getDebugLoc();
13837 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13838
13839 ISD::LoadExtType Ext = Ld->getExtensionType();
13840
Nadav Rotemca6f2962011-09-18 19:00:23 +000013841 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013842 // shuffle. We need SSE4 for the shuffles.
13843 // TODO: It is possible to support ZExt by zeroing the undef values
13844 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000013845 if (RegVT.isVector() && RegVT.isInteger() &&
13846 Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013847 assert(MemVT != RegVT && "Cannot extend to the same type");
13848 assert(MemVT.isVector() && "Must load a vector from memory");
13849
13850 unsigned NumElems = RegVT.getVectorNumElements();
13851 unsigned RegSz = RegVT.getSizeInBits();
13852 unsigned MemSz = MemVT.getSizeInBits();
13853 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000013854 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013855 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
13856
13857 // Attempt to load the original value using a single load op.
13858 // Find a scalar type which is equal to the loaded word size.
13859 MVT SclrLoadTy = MVT::i8;
13860 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13861 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13862 MVT Tp = (MVT::SimpleValueType)tp;
13863 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
13864 SclrLoadTy = Tp;
13865 break;
13866 }
13867 }
13868
13869 // Proceed if a load word is found.
13870 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
13871
13872 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
13873 RegSz/SclrLoadTy.getSizeInBits());
13874
13875 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
13876 RegSz/MemVT.getScalarType().getSizeInBits());
13877 // Can't shuffle using an illegal type.
13878 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13879
13880 // Perform a single load.
13881 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
13882 Ld->getBasePtr(),
13883 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013884 Ld->isNonTemporal(), Ld->isInvariant(),
13885 Ld->getAlignment());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013886
13887 // Insert the word loaded into a vector.
13888 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
13889 LoadUnitVecVT, ScalarLoad);
13890
13891 // Bitcast the loaded value to a vector of the original element type, in
13892 // the size of the target vector type.
Chad Rosierb90d2a92012-01-03 23:19:12 +000013893 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT,
13894 ScalarInVector);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013895 unsigned SizeRatio = RegSz/MemSz;
13896
13897 // Redistribute the loaded elements into the different locations.
13898 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13899 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
13900
13901 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
13902 DAG.getUNDEF(SlicedVec.getValueType()),
13903 ShuffleVec.data());
13904
13905 // Bitcast to the requested type.
13906 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
13907 // Replace the original load with the new sequence
13908 // and return the new chain.
13909 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
13910 return SDValue(ScalarLoad.getNode(), 1);
13911 }
13912
13913 return SDValue();
13914}
13915
Chris Lattner149a4e52008-02-22 02:09:43 +000013916/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013917static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013918 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013919 StoreSDNode *St = cast<StoreSDNode>(N);
13920 EVT VT = St->getValue().getValueType();
13921 EVT StVT = St->getMemoryVT();
13922 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013923 SDValue StoredVal = St->getOperand(1);
13924 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13925
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013926 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013927 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13928 // 128-bit ones. If in the future the cost becomes only one memory access the
13929 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013930 if (VT.getSizeInBits() == 256 &&
13931 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13932 StoredVal.getNumOperands() == 2) {
13933
13934 SDValue Value0 = StoredVal.getOperand(0);
13935 SDValue Value1 = StoredVal.getOperand(1);
13936
13937 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13938 SDValue Ptr0 = St->getBasePtr();
13939 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13940
13941 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13942 St->getPointerInfo(), St->isVolatile(),
13943 St->isNonTemporal(), St->getAlignment());
13944 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13945 St->getPointerInfo(), St->isVolatile(),
13946 St->isNonTemporal(), St->getAlignment());
13947 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13948 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013949
13950 // Optimize trunc store (of multiple scalars) to shuffle and store.
13951 // First, pack all of the elements in one place. Next, store to memory
13952 // in fewer chunks.
13953 if (St->isTruncatingStore() && VT.isVector()) {
13954 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13955 unsigned NumElems = VT.getVectorNumElements();
13956 assert(StVT != VT && "Cannot truncate to the same type");
13957 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13958 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13959
13960 // From, To sizes and ElemCount must be pow of two
13961 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013962 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000013963 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013964 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013965
Nadav Rotem614061b2011-08-10 19:30:14 +000013966 unsigned SizeRatio = FromSz / ToSz;
13967
13968 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13969
13970 // Create a type on which we perform the shuffle
13971 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13972 StVT.getScalarType(), NumElems*SizeRatio);
13973
13974 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13975
13976 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13977 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13978 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13979
13980 // Can't shuffle using an illegal type
13981 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13982
13983 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13984 DAG.getUNDEF(WideVec.getValueType()),
13985 ShuffleVec.data());
13986 // At this point all of the data is stored at the bottom of the
13987 // register. We now need to save it to mem.
13988
13989 // Find the largest store unit
13990 MVT StoreType = MVT::i8;
13991 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13992 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13993 MVT Tp = (MVT::SimpleValueType)tp;
13994 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13995 StoreType = Tp;
13996 }
13997
13998 // Bitcast the original vector into a vector of store-size units
13999 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
14000 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
14001 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
14002 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
14003 SmallVector<SDValue, 8> Chains;
14004 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
14005 TLI.getPointerTy());
14006 SDValue Ptr = St->getBasePtr();
14007
14008 // Perform one or more big stores into memory.
14009 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
14010 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14011 StoreType, ShuffWide,
14012 DAG.getIntPtrConstant(i));
14013 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14014 St->getPointerInfo(), St->isVolatile(),
14015 St->isNonTemporal(), St->getAlignment());
14016 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14017 Chains.push_back(Ch);
14018 }
14019
14020 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14021 Chains.size());
14022 }
14023
14024
Chris Lattner149a4e52008-02-22 02:09:43 +000014025 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14026 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014027 // A preferable solution to the general problem is to figure out the right
14028 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014029
14030 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014031 if (VT.getSizeInBits() != 64)
14032 return SDValue();
14033
Devang Patel578efa92009-06-05 21:57:13 +000014034 const Function *F = DAG.getMachineFunction().getFunction();
14035 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014036 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000014037 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000014038 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014039 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014040 isa<LoadSDNode>(St->getValue()) &&
14041 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14042 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014043 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014044 LoadSDNode *Ld = 0;
14045 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014046 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014047 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014048 // Must be a store of a load. We currently handle two cases: the load
14049 // is a direct child, and it's under an intervening TokenFactor. It is
14050 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014051 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014052 Ld = cast<LoadSDNode>(St->getChain());
14053 else if (St->getValue().hasOneUse() &&
14054 ChainVal->getOpcode() == ISD::TokenFactor) {
14055 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014056 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014057 TokenFactorIndex = i;
14058 Ld = cast<LoadSDNode>(St->getValue());
14059 } else
14060 Ops.push_back(ChainVal->getOperand(i));
14061 }
14062 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014063
Evan Cheng536e6672009-03-12 05:59:15 +000014064 if (!Ld || !ISD::isNormalLoad(Ld))
14065 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014066
Evan Cheng536e6672009-03-12 05:59:15 +000014067 // If this is not the MMX case, i.e. we are just turning i64 load/store
14068 // into f64 load/store, avoid the transformation if there are multiple
14069 // uses of the loaded value.
14070 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14071 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014072
Evan Cheng536e6672009-03-12 05:59:15 +000014073 DebugLoc LdDL = Ld->getDebugLoc();
14074 DebugLoc StDL = N->getDebugLoc();
14075 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14076 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14077 // pair instead.
14078 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014079 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014080 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14081 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014082 Ld->isNonTemporal(), Ld->isInvariant(),
14083 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014084 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014085 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014086 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014087 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014088 Ops.size());
14089 }
Evan Cheng536e6672009-03-12 05:59:15 +000014090 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014091 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014092 St->isVolatile(), St->isNonTemporal(),
14093 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014094 }
Evan Cheng536e6672009-03-12 05:59:15 +000014095
14096 // Otherwise, lower to two pairs of 32-bit loads / stores.
14097 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014098 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14099 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014100
Owen Anderson825b72b2009-08-11 20:47:22 +000014101 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014102 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014103 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014104 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014105 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014106 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014107 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014108 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014109 MinAlign(Ld->getAlignment(), 4));
14110
14111 SDValue NewChain = LoLd.getValue(1);
14112 if (TokenFactorIndex != -1) {
14113 Ops.push_back(LoLd);
14114 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014115 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014116 Ops.size());
14117 }
14118
14119 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014120 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14121 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014122
14123 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014124 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014125 St->isVolatile(), St->isNonTemporal(),
14126 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014127 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014128 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014129 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014130 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014131 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014132 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014133 }
Dan Gohman475871a2008-07-27 21:46:04 +000014134 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014135}
14136
Duncan Sands17470be2011-09-22 20:15:48 +000014137/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14138/// and return the operands for the horizontal operation in LHS and RHS. A
14139/// horizontal operation performs the binary operation on successive elements
14140/// of its first operand, then on successive elements of its second operand,
14141/// returning the resulting values in a vector. For example, if
14142/// A = < float a0, float a1, float a2, float a3 >
14143/// and
14144/// B = < float b0, float b1, float b2, float b3 >
14145/// then the result of doing a horizontal operation on A and B is
14146/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14147/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14148/// A horizontal-op B, for some already available A and B, and if so then LHS is
14149/// set to A, RHS to B, and the routine returns 'true'.
14150/// Note that the binary operation should have the property that if one of the
14151/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014152static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000014153 // Look for the following pattern: if
14154 // A = < float a0, float a1, float a2, float a3 >
14155 // B = < float b0, float b1, float b2, float b3 >
14156 // and
14157 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14158 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14159 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14160 // which is A horizontal-op B.
14161
14162 // At least one of the operands should be a vector shuffle.
14163 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14164 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14165 return false;
14166
14167 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000014168
14169 assert((VT.is128BitVector() || VT.is256BitVector()) &&
14170 "Unsupported vector type for horizontal add/sub");
14171
14172 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
14173 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000014174 unsigned NumElts = VT.getVectorNumElements();
14175 unsigned NumLanes = VT.getSizeInBits()/128;
14176 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000014177 assert((NumLaneElts % 2 == 0) &&
14178 "Vector type should have an even number of elements in each lane");
14179 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000014180
14181 // View LHS in the form
14182 // LHS = VECTOR_SHUFFLE A, B, LMask
14183 // If LHS is not a shuffle then pretend it is the shuffle
14184 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14185 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14186 // type VT.
14187 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000014188 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014189 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14190 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14191 A = LHS.getOperand(0);
14192 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14193 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000014194 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
14195 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000014196 } else {
14197 if (LHS.getOpcode() != ISD::UNDEF)
14198 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014199 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014200 LMask[i] = i;
14201 }
14202
14203 // Likewise, view RHS in the form
14204 // RHS = VECTOR_SHUFFLE C, D, RMask
14205 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000014206 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014207 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14208 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14209 C = RHS.getOperand(0);
14210 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14211 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000014212 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
14213 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000014214 } else {
14215 if (RHS.getOpcode() != ISD::UNDEF)
14216 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014217 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014218 RMask[i] = i;
14219 }
14220
14221 // Check that the shuffles are both shuffling the same vectors.
14222 if (!(A == C && B == D) && !(A == D && B == C))
14223 return false;
14224
14225 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14226 if (!A.getNode() && !B.getNode())
14227 return false;
14228
14229 // If A and B occur in reverse order in RHS, then "swap" them (which means
14230 // rewriting the mask).
14231 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000014232 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014233
14234 // At this point LHS and RHS are equivalent to
14235 // LHS = VECTOR_SHUFFLE A, B, LMask
14236 // RHS = VECTOR_SHUFFLE A, B, RMask
14237 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000014238 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000014239 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000014240
Craig Topperf8363302011-12-02 08:18:41 +000014241 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014242 if (LIdx < 0 || RIdx < 0 ||
14243 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
14244 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000014245 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000014246
Craig Topperf8363302011-12-02 08:18:41 +000014247 // Check that successive elements are being operated on. If not, this is
14248 // not a horizontal operation.
14249 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
14250 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000014251 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000014252 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000014253 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000014254 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000014255 }
14256
14257 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14258 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14259 return true;
14260}
14261
14262/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14263static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14264 const X86Subtarget *Subtarget) {
14265 EVT VT = N->getValueType(0);
14266 SDValue LHS = N->getOperand(0);
14267 SDValue RHS = N->getOperand(1);
14268
14269 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000014270 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000014271 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014272 isHorizontalBinOp(LHS, RHS, true))
14273 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14274 return SDValue();
14275}
14276
14277/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14278static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14279 const X86Subtarget *Subtarget) {
14280 EVT VT = N->getValueType(0);
14281 SDValue LHS = N->getOperand(0);
14282 SDValue RHS = N->getOperand(1);
14283
14284 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000014285 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000014286 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014287 isHorizontalBinOp(LHS, RHS, false))
14288 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14289 return SDValue();
14290}
14291
Chris Lattner6cf73262008-01-25 06:14:17 +000014292/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14293/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014294static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014295 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14296 // F[X]OR(0.0, x) -> x
14297 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014298 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14299 if (C->getValueAPF().isPosZero())
14300 return N->getOperand(1);
14301 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14302 if (C->getValueAPF().isPosZero())
14303 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014304 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014305}
14306
14307/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014308static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014309 // FAND(0.0, x) -> 0.0
14310 // FAND(x, 0.0) -> 0.0
14311 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14312 if (C->getValueAPF().isPosZero())
14313 return N->getOperand(0);
14314 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14315 if (C->getValueAPF().isPosZero())
14316 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014317 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014318}
14319
Dan Gohmane5af2d32009-01-29 01:59:02 +000014320static SDValue PerformBTCombine(SDNode *N,
14321 SelectionDAG &DAG,
14322 TargetLowering::DAGCombinerInfo &DCI) {
14323 // BT ignores high bits in the bit index operand.
14324 SDValue Op1 = N->getOperand(1);
14325 if (Op1.hasOneUse()) {
14326 unsigned BitWidth = Op1.getValueSizeInBits();
14327 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14328 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014329 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14330 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014331 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014332 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14333 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14334 DCI.CommitTargetLoweringOpt(TLO);
14335 }
14336 return SDValue();
14337}
Chris Lattner83e6c992006-10-04 06:57:07 +000014338
Eli Friedman7a5e5552009-06-07 06:52:44 +000014339static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14340 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014341 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014342 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014343 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014344 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014345 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014346 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014347 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014348 }
14349 return SDValue();
14350}
14351
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000014352static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
14353 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000014354 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14355 // (and (i32 x86isd::setcc_carry), 1)
14356 // This eliminates the zext. This transformation is necessary because
14357 // ISD::SETCC is always legalized to i8.
14358 DebugLoc dl = N->getDebugLoc();
14359 SDValue N0 = N->getOperand(0);
14360 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000014361 EVT OpVT = N0.getValueType();
14362
Evan Cheng2e489c42009-12-16 00:53:11 +000014363 if (N0.getOpcode() == ISD::AND &&
14364 N0.hasOneUse() &&
14365 N0.getOperand(0).hasOneUse()) {
14366 SDValue N00 = N0.getOperand(0);
14367 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14368 return SDValue();
14369 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14370 if (!C || C->getZExtValue() != 1)
14371 return SDValue();
14372 return DAG.getNode(ISD::AND, dl, VT,
14373 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14374 N00.getOperand(0), N00.getOperand(1)),
14375 DAG.getConstant(1, VT));
14376 }
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000014377 // Optimize vectors in AVX mode:
14378 //
14379 // v8i16 -> v8i32
14380 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
14381 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
14382 // Concat upper and lower parts.
14383 //
14384 // v4i32 -> v4i64
14385 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
14386 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
14387 // Concat upper and lower parts.
14388 //
14389 if (Subtarget->hasAVX()) {
14390
14391 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
14392 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
14393
14394 SDValue ZeroVec = getZeroVector(OpVT, Subtarget->hasSSE2(), Subtarget->hasAVX2(),
14395 DAG, dl);
14396 SDValue OpLo = getTargetShuffleNode(X86ISD::UNPCKL, dl, OpVT, N0, ZeroVec, DAG);
14397 SDValue OpHi = getTargetShuffleNode(X86ISD::UNPCKH, dl, OpVT, N0, ZeroVec, DAG);
14398
14399 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
14400 VT.getVectorNumElements()/2);
14401
14402 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
14403 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
14404
14405 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
14406 }
14407 }
14408
Evan Cheng2e489c42009-12-16 00:53:11 +000014409
14410 return SDValue();
14411}
14412
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014413// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14414static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14415 unsigned X86CC = N->getConstantOperandVal(0);
14416 SDValue EFLAG = N->getOperand(1);
14417 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014418
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014419 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14420 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14421 // cases.
14422 if (X86CC == X86::COND_B)
14423 return DAG.getNode(ISD::AND, DL, MVT::i8,
14424 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14425 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14426 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014427
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014428 return SDValue();
14429}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014430
Benjamin Kramer1396c402011-06-18 11:09:41 +000014431static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14432 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014433 SDValue Op0 = N->getOperand(0);
14434 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14435 // a 32-bit target where SSE doesn't support i64->FP operations.
14436 if (Op0.getOpcode() == ISD::LOAD) {
14437 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14438 EVT VT = Ld->getValueType(0);
14439 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14440 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14441 !XTLI->getSubtarget()->is64Bit() &&
14442 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014443 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14444 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014445 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14446 return FILDChain;
14447 }
14448 }
14449 return SDValue();
14450}
14451
Chris Lattner23a01992010-12-20 01:37:09 +000014452// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14453static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14454 X86TargetLowering::DAGCombinerInfo &DCI) {
14455 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14456 // the result is either zero or one (depending on the input carry bit).
14457 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14458 if (X86::isZeroNode(N->getOperand(0)) &&
14459 X86::isZeroNode(N->getOperand(1)) &&
14460 // We don't have a good way to replace an EFLAGS use, so only do this when
14461 // dead right now.
14462 SDValue(N, 1).use_empty()) {
14463 DebugLoc DL = N->getDebugLoc();
14464 EVT VT = N->getValueType(0);
14465 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14466 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14467 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14468 DAG.getConstant(X86::COND_B,MVT::i8),
14469 N->getOperand(2)),
14470 DAG.getConstant(1, VT));
14471 return DCI.CombineTo(N, Res1, CarryOut);
14472 }
14473
14474 return SDValue();
14475}
14476
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014477// fold (add Y, (sete X, 0)) -> adc 0, Y
14478// (add Y, (setne X, 0)) -> sbb -1, Y
14479// (sub (sete X, 0), Y) -> sbb 0, Y
14480// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014481static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014482 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014483
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014484 // Look through ZExts.
14485 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14486 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14487 return SDValue();
14488
14489 SDValue SetCC = Ext.getOperand(0);
14490 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14491 return SDValue();
14492
14493 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14494 if (CC != X86::COND_E && CC != X86::COND_NE)
14495 return SDValue();
14496
14497 SDValue Cmp = SetCC.getOperand(1);
14498 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014499 !X86::isZeroNode(Cmp.getOperand(1)) ||
14500 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014501 return SDValue();
14502
14503 SDValue CmpOp0 = Cmp.getOperand(0);
14504 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14505 DAG.getConstant(1, CmpOp0.getValueType()));
14506
14507 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14508 if (CC == X86::COND_NE)
14509 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14510 DL, OtherVal.getValueType(), OtherVal,
14511 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14512 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14513 DL, OtherVal.getValueType(), OtherVal,
14514 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14515}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014516
Craig Topper54f952a2011-11-19 09:02:40 +000014517/// PerformADDCombine - Do target-specific dag combines on integer adds.
14518static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
14519 const X86Subtarget *Subtarget) {
14520 EVT VT = N->getValueType(0);
14521 SDValue Op0 = N->getOperand(0);
14522 SDValue Op1 = N->getOperand(1);
14523
14524 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000014525 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000014526 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000014527 isHorizontalBinOp(Op0, Op1, true))
14528 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
14529
14530 return OptimizeConditionalInDecrement(N, DAG);
14531}
14532
14533static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
14534 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014535 SDValue Op0 = N->getOperand(0);
14536 SDValue Op1 = N->getOperand(1);
14537
14538 // X86 can't encode an immediate LHS of a sub. See if we can push the
14539 // negation into a preceding instruction.
14540 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014541 // If the RHS of the sub is a XOR with one use and a constant, invert the
14542 // immediate. Then add one to the LHS of the sub so we can turn
14543 // X-Y -> X+~Y+1, saving one register.
14544 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14545 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014546 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014547 EVT VT = Op0.getValueType();
14548 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14549 Op1.getOperand(0),
14550 DAG.getConstant(~XorC, VT));
14551 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014552 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014553 }
14554 }
14555
Craig Topper54f952a2011-11-19 09:02:40 +000014556 // Try to synthesize horizontal adds from adds of shuffles.
14557 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000014558 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000014559 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
14560 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000014561 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
14562
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014563 return OptimizeConditionalInDecrement(N, DAG);
14564}
14565
Dan Gohman475871a2008-07-27 21:46:04 +000014566SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014567 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014568 SelectionDAG &DAG = DCI.DAG;
14569 switch (N->getOpcode()) {
14570 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014571 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014572 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014573 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000014574 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014575 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Craig Topper54f952a2011-11-19 09:02:40 +000014576 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
14577 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000014578 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014579 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014580 case ISD::SHL:
14581 case ISD::SRA:
14582 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014583 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014584 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014585 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014586 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014587 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014588 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014589 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14590 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014591 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014592 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14593 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014594 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014595 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000014596 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, Subtarget);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014597 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Craig Topperb3982da2011-12-31 23:50:21 +000014598 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014599 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000014600 case X86ISD::UNPCKH:
14601 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014602 case X86ISD::MOVHLPS:
14603 case X86ISD::MOVLHPS:
14604 case X86ISD::PSHUFD:
14605 case X86ISD::PSHUFHW:
14606 case X86ISD::PSHUFLW:
14607 case X86ISD::MOVSS:
14608 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000014609 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000014610 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014611 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014612 }
14613
Dan Gohman475871a2008-07-27 21:46:04 +000014614 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014615}
14616
Evan Chenge5b51ac2010-04-17 06:13:15 +000014617/// isTypeDesirableForOp - Return true if the target has native support for
14618/// the specified value type and it is 'desirable' to use the type for the
14619/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14620/// instruction encodings are longer and some i16 instructions are slow.
14621bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14622 if (!isTypeLegal(VT))
14623 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014624 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014625 return true;
14626
14627 switch (Opc) {
14628 default:
14629 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014630 case ISD::LOAD:
14631 case ISD::SIGN_EXTEND:
14632 case ISD::ZERO_EXTEND:
14633 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014634 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014635 case ISD::SRL:
14636 case ISD::SUB:
14637 case ISD::ADD:
14638 case ISD::MUL:
14639 case ISD::AND:
14640 case ISD::OR:
14641 case ISD::XOR:
14642 return false;
14643 }
14644}
14645
14646/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014647/// beneficial for dag combiner to promote the specified node. If true, it
14648/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014649bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014650 EVT VT = Op.getValueType();
14651 if (VT != MVT::i16)
14652 return false;
14653
Evan Cheng4c26e932010-04-19 19:29:22 +000014654 bool Promote = false;
14655 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014656 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014657 default: break;
14658 case ISD::LOAD: {
14659 LoadSDNode *LD = cast<LoadSDNode>(Op);
14660 // If the non-extending load has a single use and it's not live out, then it
14661 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014662 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14663 Op.hasOneUse()*/) {
14664 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14665 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14666 // The only case where we'd want to promote LOAD (rather then it being
14667 // promoted as an operand is when it's only use is liveout.
14668 if (UI->getOpcode() != ISD::CopyToReg)
14669 return false;
14670 }
14671 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014672 Promote = true;
14673 break;
14674 }
14675 case ISD::SIGN_EXTEND:
14676 case ISD::ZERO_EXTEND:
14677 case ISD::ANY_EXTEND:
14678 Promote = true;
14679 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014680 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014681 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014682 SDValue N0 = Op.getOperand(0);
14683 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014684 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014685 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014686 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014687 break;
14688 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014689 case ISD::ADD:
14690 case ISD::MUL:
14691 case ISD::AND:
14692 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014693 case ISD::XOR:
14694 Commute = true;
14695 // fallthrough
14696 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014697 SDValue N0 = Op.getOperand(0);
14698 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014699 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014700 return false;
14701 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014702 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014703 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014704 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014705 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014706 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014707 }
14708 }
14709
14710 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014711 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014712}
14713
Evan Cheng60c07e12006-07-05 22:17:51 +000014714//===----------------------------------------------------------------------===//
14715// X86 Inline Assembly Support
14716//===----------------------------------------------------------------------===//
14717
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000014718namespace {
14719 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000014720 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000014721 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014722
Benjamin Kramer0581ed72011-12-18 20:51:31 +000014723 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000014724 StringRef piece(*args[i]);
14725 if (!s.startswith(piece)) // Check if the piece matches.
14726 return false;
14727
14728 s = s.substr(piece.size());
14729 StringRef::size_type pos = s.find_first_not_of(" \t");
14730 if (pos == 0) // We matched a prefix.
14731 return false;
14732
14733 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014734 }
14735
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000014736 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014737 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000014738 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014739}
14740
Chris Lattnerb8105652009-07-20 17:51:36 +000014741bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14742 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014743
14744 std::string AsmStr = IA->getAsmString();
14745
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014746 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
14747 if (!Ty || Ty->getBitWidth() % 16 != 0)
14748 return false;
14749
Chris Lattnerb8105652009-07-20 17:51:36 +000014750 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014751 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014752 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014753
14754 switch (AsmPieces.size()) {
14755 default: return false;
14756 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014757 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014758 // we will turn this bswap into something that will be lowered to logical
14759 // ops instead of emitting the bswap asm. For now, we don't support 486 or
14760 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014761 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000014762 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
14763 matchAsm(AsmPieces[0], "bswapl", "$0") ||
14764 matchAsm(AsmPieces[0], "bswapq", "$0") ||
14765 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
14766 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
14767 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000014768 // No need to check constraints, nothing other than the equivalent of
14769 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000014770 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014771 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014772
Chris Lattnerb8105652009-07-20 17:51:36 +000014773 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014774 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014775 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000014776 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
14777 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000014778 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014779 const std::string &ConstraintsStr = IA->getConstraintString();
14780 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014781 std::sort(AsmPieces.begin(), AsmPieces.end());
14782 if (AsmPieces.size() == 4 &&
14783 AsmPieces[0] == "~{cc}" &&
14784 AsmPieces[1] == "~{dirflag}" &&
14785 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014786 AsmPieces[3] == "~{fpsr}")
14787 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014788 }
14789 break;
14790 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014791 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014792 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000014793 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
14794 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
14795 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014796 AsmPieces.clear();
14797 const std::string &ConstraintsStr = IA->getConstraintString();
14798 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
14799 std::sort(AsmPieces.begin(), AsmPieces.end());
14800 if (AsmPieces.size() == 4 &&
14801 AsmPieces[0] == "~{cc}" &&
14802 AsmPieces[1] == "~{dirflag}" &&
14803 AsmPieces[2] == "~{flags}" &&
14804 AsmPieces[3] == "~{fpsr}")
14805 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014806 }
Evan Cheng55d42002011-01-08 01:24:27 +000014807
14808 if (CI->getType()->isIntegerTy(64)) {
14809 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14810 if (Constraints.size() >= 2 &&
14811 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14812 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14813 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000014814 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
14815 matchAsm(AsmPieces[1], "bswap", "%edx") &&
14816 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014817 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014818 }
14819 }
14820 break;
14821 }
14822 return false;
14823}
14824
14825
14826
Chris Lattnerf4dff842006-07-11 02:54:03 +000014827/// getConstraintType - Given a constraint letter, return the type of
14828/// constraint it is for this target.
14829X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014830X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14831 if (Constraint.size() == 1) {
14832 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014833 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014834 case 'q':
14835 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014836 case 'f':
14837 case 't':
14838 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014839 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014840 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014841 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014842 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014843 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014844 case 'a':
14845 case 'b':
14846 case 'c':
14847 case 'd':
14848 case 'S':
14849 case 'D':
14850 case 'A':
14851 return C_Register;
14852 case 'I':
14853 case 'J':
14854 case 'K':
14855 case 'L':
14856 case 'M':
14857 case 'N':
14858 case 'G':
14859 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014860 case 'e':
14861 case 'Z':
14862 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014863 default:
14864 break;
14865 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014866 }
Chris Lattner4234f572007-03-25 02:14:49 +000014867 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014868}
14869
John Thompson44ab89e2010-10-29 17:29:13 +000014870/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014871/// This object must already have been set up with the operand type
14872/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014873TargetLowering::ConstraintWeight
14874 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014875 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014876 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014877 Value *CallOperandVal = info.CallOperandVal;
14878 // If we don't have a value, we can't do a match,
14879 // but allow it at the lowest weight.
14880 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014881 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014882 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014883 // Look at the constraint type.
14884 switch (*constraint) {
14885 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014886 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14887 case 'R':
14888 case 'q':
14889 case 'Q':
14890 case 'a':
14891 case 'b':
14892 case 'c':
14893 case 'd':
14894 case 'S':
14895 case 'D':
14896 case 'A':
14897 if (CallOperandVal->getType()->isIntegerTy())
14898 weight = CW_SpecificReg;
14899 break;
14900 case 'f':
14901 case 't':
14902 case 'u':
14903 if (type->isFloatingPointTy())
14904 weight = CW_SpecificReg;
14905 break;
14906 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014907 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014908 weight = CW_SpecificReg;
14909 break;
14910 case 'x':
14911 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000014912 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000014913 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000014914 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014915 break;
14916 case 'I':
14917 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14918 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014919 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014920 }
14921 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014922 case 'J':
14923 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14924 if (C->getZExtValue() <= 63)
14925 weight = CW_Constant;
14926 }
14927 break;
14928 case 'K':
14929 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14930 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14931 weight = CW_Constant;
14932 }
14933 break;
14934 case 'L':
14935 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14936 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14937 weight = CW_Constant;
14938 }
14939 break;
14940 case 'M':
14941 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14942 if (C->getZExtValue() <= 3)
14943 weight = CW_Constant;
14944 }
14945 break;
14946 case 'N':
14947 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14948 if (C->getZExtValue() <= 0xff)
14949 weight = CW_Constant;
14950 }
14951 break;
14952 case 'G':
14953 case 'C':
14954 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14955 weight = CW_Constant;
14956 }
14957 break;
14958 case 'e':
14959 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14960 if ((C->getSExtValue() >= -0x80000000LL) &&
14961 (C->getSExtValue() <= 0x7fffffffLL))
14962 weight = CW_Constant;
14963 }
14964 break;
14965 case 'Z':
14966 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14967 if (C->getZExtValue() <= 0xffffffff)
14968 weight = CW_Constant;
14969 }
14970 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014971 }
14972 return weight;
14973}
14974
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014975/// LowerXConstraint - try to replace an X constraint, which matches anything,
14976/// with another that has more specific requirements based on the type of the
14977/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014978const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014979LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014980 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14981 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014982 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000014983 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000014984 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000014985 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000014986 return "x";
14987 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014988
Chris Lattner5e764232008-04-26 23:02:14 +000014989 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014990}
14991
Chris Lattner48884cd2007-08-25 00:47:38 +000014992/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14993/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014994void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014995 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014996 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014997 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014998 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014999
Eric Christopher100c8332011-06-02 23:16:42 +000015000 // Only support length 1 constraints for now.
15001 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000015002
Eric Christopher100c8332011-06-02 23:16:42 +000015003 char ConstraintLetter = Constraint[0];
15004 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015005 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000015006 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000015007 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015008 if (C->getZExtValue() <= 31) {
15009 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015010 break;
15011 }
Devang Patel84f7fd22007-03-17 00:13:28 +000015012 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015013 return;
Evan Cheng364091e2008-09-22 23:57:37 +000015014 case 'J':
15015 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015016 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000015017 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15018 break;
15019 }
15020 }
15021 return;
15022 case 'K':
15023 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015024 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000015025 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15026 break;
15027 }
15028 }
15029 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000015030 case 'N':
15031 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015032 if (C->getZExtValue() <= 255) {
15033 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015034 break;
15035 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000015036 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015037 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000015038 case 'e': {
15039 // 32-bit signed value
15040 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015041 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15042 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015043 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015044 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000015045 break;
15046 }
15047 // FIXME gcc accepts some relocatable values here too, but only in certain
15048 // memory models; it's complicated.
15049 }
15050 return;
15051 }
15052 case 'Z': {
15053 // 32-bit unsigned value
15054 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015055 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15056 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015057 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15058 break;
15059 }
15060 }
15061 // FIXME gcc accepts some relocatable values here too, but only in certain
15062 // memory models; it's complicated.
15063 return;
15064 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015065 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015066 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000015067 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015068 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015069 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000015070 break;
15071 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015072
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015073 // In any sort of PIC mode addresses need to be computed at runtime by
15074 // adding in a register or some sort of table lookup. These can't
15075 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000015076 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015077 return;
15078
Chris Lattnerdc43a882007-05-03 16:52:29 +000015079 // If we are in non-pic codegen mode, we allow the address of a global (with
15080 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000015081 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015082 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000015083
Chris Lattner49921962009-05-08 18:23:14 +000015084 // Match either (GA), (GA+C), (GA+C1+C2), etc.
15085 while (1) {
15086 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15087 Offset += GA->getOffset();
15088 break;
15089 } else if (Op.getOpcode() == ISD::ADD) {
15090 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15091 Offset += C->getZExtValue();
15092 Op = Op.getOperand(0);
15093 continue;
15094 }
15095 } else if (Op.getOpcode() == ISD::SUB) {
15096 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15097 Offset += -C->getZExtValue();
15098 Op = Op.getOperand(0);
15099 continue;
15100 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015101 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015102
Chris Lattner49921962009-05-08 18:23:14 +000015103 // Otherwise, this isn't something we can handle, reject it.
15104 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015105 }
Eric Christopherfd179292009-08-27 18:07:15 +000015106
Dan Gohman46510a72010-04-15 01:51:59 +000015107 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015108 // If we require an extra load to get this address, as in PIC mode, we
15109 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015110 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15111 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015112 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015113
Devang Patel0d881da2010-07-06 22:08:15 +000015114 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15115 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015116 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015117 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015118 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015119
Gabor Greifba36cb52008-08-28 21:40:38 +000015120 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015121 Ops.push_back(Result);
15122 return;
15123 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015124 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015125}
15126
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015127std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015128X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015129 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015130 // First, see if this is a constraint that directly corresponds to an LLVM
15131 // register class.
15132 if (Constraint.size() == 1) {
15133 // GCC Constraint Letters
15134 switch (Constraint[0]) {
15135 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015136 // TODO: Slight differences here in allocation order and leaving
15137 // RIP in the class. Do they matter any more here than they do
15138 // in the normal allocation?
15139 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15140 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015141 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015142 return std::make_pair(0U, X86::GR32RegisterClass);
15143 else if (VT == MVT::i16)
15144 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015145 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015146 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015147 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015148 return std::make_pair(0U, X86::GR64RegisterClass);
15149 break;
15150 }
15151 // 32-bit fallthrough
15152 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015153 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015154 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
15155 else if (VT == MVT::i16)
15156 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015157 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015158 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
15159 else if (VT == MVT::i64)
15160 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15161 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015162 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015163 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015164 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015165 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015166 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015167 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015168 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015169 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015170 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015171 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015172 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015173 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15174 if (VT == MVT::i16)
15175 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15176 if (VT == MVT::i32 || !Subtarget->is64Bit())
15177 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15178 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015179 case 'f': // FP Stack registers.
15180 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15181 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015182 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015183 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015184 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015185 return std::make_pair(0U, X86::RFP64RegisterClass);
15186 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015187 case 'y': // MMX_REGS if MMX allowed.
15188 if (!Subtarget->hasMMX()) break;
15189 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015190 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000015191 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015192 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000015193 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000015194 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015195
Owen Anderson825b72b2009-08-11 20:47:22 +000015196 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015197 default: break;
15198 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015199 case MVT::f32:
15200 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015201 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015202 case MVT::f64:
15203 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015204 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015205 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015206 case MVT::v16i8:
15207 case MVT::v8i16:
15208 case MVT::v4i32:
15209 case MVT::v2i64:
15210 case MVT::v4f32:
15211 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015212 return std::make_pair(0U, X86::VR128RegisterClass);
Eric Christopher55487552012-01-07 01:02:09 +000015213 // AVX types.
15214 case MVT::v32i8:
15215 case MVT::v16i16:
15216 case MVT::v8i32:
15217 case MVT::v4i64:
15218 case MVT::v8f32:
15219 case MVT::v4f64:
15220 return std::make_pair(0U, X86::VR256RegisterClass);
15221
Chris Lattner0f65cad2007-04-09 05:49:22 +000015222 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015223 break;
15224 }
15225 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015226
Chris Lattnerf76d1802006-07-31 23:26:50 +000015227 // Use the default implementation in TargetLowering to convert the register
15228 // constraint into a member of a register class.
15229 std::pair<unsigned, const TargetRegisterClass*> Res;
15230 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015231
15232 // Not found as a standard register?
15233 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015234 // Map st(0) -> st(7) -> ST0
15235 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15236 tolower(Constraint[1]) == 's' &&
15237 tolower(Constraint[2]) == 't' &&
15238 Constraint[3] == '(' &&
15239 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15240 Constraint[5] == ')' &&
15241 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015242
Chris Lattner56d77c72009-09-13 22:41:48 +000015243 Res.first = X86::ST0+Constraint[4]-'0';
15244 Res.second = X86::RFP80RegisterClass;
15245 return Res;
15246 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015247
Chris Lattner56d77c72009-09-13 22:41:48 +000015248 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015249 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015250 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015251 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015252 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015253 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015254
15255 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015256 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015257 Res.first = X86::EFLAGS;
15258 Res.second = X86::CCRRegisterClass;
15259 return Res;
15260 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015261
Dale Johannesen330169f2008-11-13 21:52:36 +000015262 // 'A' means EAX + EDX.
15263 if (Constraint == "A") {
15264 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015265 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015266 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015267 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015268 return Res;
15269 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015270
Chris Lattnerf76d1802006-07-31 23:26:50 +000015271 // Otherwise, check to see if this is a register class of the wrong value
15272 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15273 // turn into {ax},{dx}.
15274 if (Res.second->hasType(VT))
15275 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015276
Chris Lattnerf76d1802006-07-31 23:26:50 +000015277 // All of the single-register GCC register classes map their values onto
15278 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15279 // really want an 8-bit or 32-bit register, map to the appropriate register
15280 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015281 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015282 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015283 unsigned DestReg = 0;
15284 switch (Res.first) {
15285 default: break;
15286 case X86::AX: DestReg = X86::AL; break;
15287 case X86::DX: DestReg = X86::DL; break;
15288 case X86::CX: DestReg = X86::CL; break;
15289 case X86::BX: DestReg = X86::BL; break;
15290 }
15291 if (DestReg) {
15292 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015293 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015294 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015295 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015296 unsigned DestReg = 0;
15297 switch (Res.first) {
15298 default: break;
15299 case X86::AX: DestReg = X86::EAX; break;
15300 case X86::DX: DestReg = X86::EDX; break;
15301 case X86::CX: DestReg = X86::ECX; break;
15302 case X86::BX: DestReg = X86::EBX; break;
15303 case X86::SI: DestReg = X86::ESI; break;
15304 case X86::DI: DestReg = X86::EDI; break;
15305 case X86::BP: DestReg = X86::EBP; break;
15306 case X86::SP: DestReg = X86::ESP; break;
15307 }
15308 if (DestReg) {
15309 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015310 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015311 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015312 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015313 unsigned DestReg = 0;
15314 switch (Res.first) {
15315 default: break;
15316 case X86::AX: DestReg = X86::RAX; break;
15317 case X86::DX: DestReg = X86::RDX; break;
15318 case X86::CX: DestReg = X86::RCX; break;
15319 case X86::BX: DestReg = X86::RBX; break;
15320 case X86::SI: DestReg = X86::RSI; break;
15321 case X86::DI: DestReg = X86::RDI; break;
15322 case X86::BP: DestReg = X86::RBP; break;
15323 case X86::SP: DestReg = X86::RSP; break;
15324 }
15325 if (DestReg) {
15326 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015327 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015328 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015329 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015330 } else if (Res.second == X86::FR32RegisterClass ||
15331 Res.second == X86::FR64RegisterClass ||
15332 Res.second == X86::VR128RegisterClass) {
15333 // Handle references to XMM physical registers that got mapped into the
15334 // wrong class. This can happen with constraints like {xmm0} where the
15335 // target independent register mapper will just pick the first match it can
15336 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015337 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015338 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015339 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015340 Res.second = X86::FR64RegisterClass;
15341 else if (X86::VR128RegisterClass->hasType(VT))
15342 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015343 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015344
Chris Lattnerf76d1802006-07-31 23:26:50 +000015345 return Res;
15346}