blob: a5bfe1ac8fe9742c3827327eed69e58bac49ef51 [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"
Chris Lattnerc64daab2010-01-26 05:02:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000047#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000048#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000049#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000050#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000053#include "llvm/Target/TargetOptions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000055using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000056
Evan Chengb1712452010-01-27 06:25:16 +000057STATISTIC(NumTailCalls, "Number of tail calls");
58
Evan Cheng10e86422008-04-25 19:11:04 +000059// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000060static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000061 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000062
David Greenea5f26012011-02-07 19:36:54 +000063static SDValue Insert128BitVector(SDValue Result,
64 SDValue Vec,
65 SDValue Idx,
66 SelectionDAG &DAG,
67 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000068
David Greenea5f26012011-02-07 19:36:54 +000069static SDValue Extract128BitVector(SDValue Vec,
70 SDValue Idx,
71 SelectionDAG &DAG,
72 DebugLoc dl);
73
74/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
75/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000076/// simple subregister reference. Idx is an index in the 128 bits we
77/// want. It need not be aligned to a 128-bit bounday. That makes
78/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000079static SDValue Extract128BitVector(SDValue Vec,
80 SDValue Idx,
81 SelectionDAG &DAG,
82 DebugLoc dl) {
83 EVT VT = Vec.getValueType();
84 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000085 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000086 int Factor = VT.getSizeInBits()/128;
87 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
88 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000089
90 // Extract from UNDEF is UNDEF.
91 if (Vec.getOpcode() == ISD::UNDEF)
92 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
93
94 if (isa<ConstantSDNode>(Idx)) {
95 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
96
97 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
98 // we can match to VEXTRACTF128.
99 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
100
101 // This is the index of the first element of the 128-bit chunk
102 // we want.
103 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
104 * ElemsPerChunk);
105
106 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000107 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
108 VecIdx);
109
110 return Result;
111 }
112
113 return SDValue();
114}
115
116/// Generate a DAG to put 128-bits into a vector > 128 bits. This
117/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000118/// simple superregister reference. Idx is an index in the 128 bits
119/// we want. It need not be aligned to a 128-bit bounday. That makes
120/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000121static SDValue Insert128BitVector(SDValue Result,
122 SDValue Vec,
123 SDValue Idx,
124 SelectionDAG &DAG,
125 DebugLoc dl) {
126 if (isa<ConstantSDNode>(Idx)) {
127 EVT VT = Vec.getValueType();
128 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
129
130 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000131 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000132 EVT ResultVT = Result.getValueType();
133
134 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000135 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000136
137 // This is the index of the first element of the 128-bit chunk
138 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000139 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000140 * ElemsPerChunk);
141
142 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000143 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
144 VecIdx);
145 return Result;
146 }
147
148 return SDValue();
149}
150
Chris Lattnerf0144122009-07-28 03:13:23 +0000151static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000152 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
153 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000154
Evan Cheng2bffee22011-02-01 01:14:13 +0000155 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000156 if (is64Bit)
157 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000158 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000159 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000160
Evan Cheng203576a2011-07-20 19:50:42 +0000161 if (Subtarget->isTargetELF())
162 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000163 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000164 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000165 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000166}
167
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000168X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000169 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000170 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000171 X86ScalarSSEf64 = Subtarget->hasXMMInt();
172 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000173 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000174
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000175 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000176 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000177
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000178 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000179 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000180
181 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000182 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000183 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
184 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000185
Eric Christopherde5e1012011-03-11 01:05:58 +0000186 // For 64-bit since we have so many registers use the ILP scheduler, for
187 // 32-bit code use the register pressure specific scheduling.
188 if (Subtarget->is64Bit())
189 setSchedulingPreference(Sched::ILP);
190 else
191 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000192 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000193
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000194 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000195 // Setup Windows compiler runtime calls.
196 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000197 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000198 setLibcallName(RTLIB::SREM_I64, "_allrem");
199 setLibcallName(RTLIB::UREM_I64, "_aullrem");
200 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000201 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000202 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000203 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000204 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000205 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
206 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000208 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
209 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000210 }
211
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000212 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000213 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000214 setUseUnderscoreSetJmp(false);
215 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000216 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000217 // MS runtime is weird: it exports _setjmp, but longjmp!
218 setUseUnderscoreSetJmp(true);
219 setUseUnderscoreLongJmp(false);
220 } else {
221 setUseUnderscoreSetJmp(true);
222 setUseUnderscoreLongJmp(true);
223 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000224
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000227 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000231
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000233
Scott Michelfdc40a02009-02-17 22:15:04 +0000234 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000236 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000238 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
240 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000241
242 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
244 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
246 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000249
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000250 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
251 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
253 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000255
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
258 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000259 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000260 // We have an algorithm for SSE2->double, and we turn this into a
261 // 64-bit FILD followed by conditional FADD for other targets.
262 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000263 // We have an algorithm for SSE2, and we turn this into a 64-bit
264 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000265 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000266 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000267
268 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
269 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
271 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000272
Devang Patel6a784892009-06-05 18:48:29 +0000273 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000274 // SSE has no i16 to fp conversion, only i32
275 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000277 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000279 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
281 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000282 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000283 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
285 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000286 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000287
Dale Johannesen73328d12007-09-19 23:55:34 +0000288 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
289 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
291 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000292
Evan Cheng02568ff2006-01-30 22:13:22 +0000293 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
294 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
296 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000297
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000298 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000300 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000302 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
304 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000305 }
306
307 // Handle FP_TO_UINT by promoting the destination to a larger signed
308 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
310 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000312
Evan Cheng25ab6902006-09-08 06:48:29 +0000313 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
315 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000316 } else if (!UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000317 // Since AVX is a superset of SSE3, only check for SSE here.
318 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000319 // Expand FP_TO_UINT into a select.
320 // FIXME: We would like to use a Custom expander here eventually to do
321 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000323 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000324 // With SSE3 we can use fisttpll to convert to a signed i64; without
325 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000327 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000328
Chris Lattner399610a2006-12-05 18:22:22 +0000329 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000330 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000331 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
332 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000333 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000335 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000336 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000337 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000338 }
Chris Lattner21f66852005-12-23 05:15:23 +0000339
Dan Gohmanb00ee212008-02-18 19:34:53 +0000340 // Scalar integer divide and remainder are lowered to use operations that
341 // produce two results, to match the available instructions. This exposes
342 // the two-result form to trivial CSE, which is able to combine x/y and x%y
343 // into a single instruction.
344 //
345 // Scalar integer multiply-high is also lowered to use two-result
346 // operations, to match the available instructions. However, plain multiply
347 // (low) operations are left as Legal, as there are single-result
348 // instructions for this in x86. Using the two-result multiply instructions
349 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000350 for (unsigned i = 0, e = 4; i != e; ++i) {
351 MVT VT = IntVTs[i];
352 setOperationAction(ISD::MULHS, VT, Expand);
353 setOperationAction(ISD::MULHU, VT, Expand);
354 setOperationAction(ISD::SDIV, VT, Expand);
355 setOperationAction(ISD::UDIV, VT, Expand);
356 setOperationAction(ISD::SREM, VT, Expand);
357 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000358
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000359 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000360 setOperationAction(ISD::ADDC, VT, Custom);
361 setOperationAction(ISD::ADDE, VT, Custom);
362 setOperationAction(ISD::SUBC, VT, Custom);
363 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000364 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000365
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
367 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
368 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
369 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000370 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
375 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
376 setOperationAction(ISD::FREM , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f64 , Expand);
378 setOperationAction(ISD::FREM , MVT::f80 , Expand);
379 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000380
Craig Topper909652f2011-10-14 03:21:46 +0000381 if (Subtarget->hasBMI()) {
382 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
383 } else {
384 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
385 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
386 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
387 if (Subtarget->is64Bit())
388 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
389 }
Craig Topper37f21672011-10-11 06:44:02 +0000390
391 if (Subtarget->hasLZCNT()) {
392 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
393 } else {
394 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
395 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
396 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
397 if (Subtarget->is64Bit())
398 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000399 }
400
Benjamin Kramer1292c222010-12-04 20:32:23 +0000401 if (Subtarget->hasPOPCNT()) {
402 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
403 } else {
404 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
405 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
406 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
407 if (Subtarget->is64Bit())
408 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
409 }
410
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
412 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000413
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000414 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000415 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000416 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000417 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000418 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
420 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
421 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
422 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
423 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000424 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
426 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
427 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
428 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000429 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000431 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000432 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000434
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000435 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
437 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
438 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
439 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000440 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
442 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000443 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000444 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
446 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
447 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
448 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000449 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000450 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000451 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
453 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
454 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000455 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
457 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
458 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000459 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000460
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000461 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000463
Eric Christopher9a9d2752010-07-22 02:48:34 +0000464 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000465 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000466
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000467 // On X86 and X86-64, atomic operations are lowered to locked instructions.
468 // Locked instructions, in turn, have implicit fence semantics (all memory
469 // operations are flushed before issuing the locked instruction, and they
470 // are not buffered), so we can fold away the common pattern of
471 // fence-atomic-fence.
472 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000473
Mon P Wang63307c32008-05-05 19:05:59 +0000474 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000475 for (unsigned i = 0, e = 4; i != e; ++i) {
476 MVT VT = IntVTs[i];
477 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
478 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000479 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000480 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000481
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000482 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000483 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
485 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
486 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
487 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
488 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
489 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
490 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000491 }
492
Eli Friedman43f51ae2011-08-26 21:21:21 +0000493 if (Subtarget->hasCmpxchg16b()) {
494 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
495 }
496
Evan Cheng3c992d22006-03-07 02:02:57 +0000497 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000498 if (!Subtarget->isTargetDarwin() &&
499 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000500 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000502 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000503
Owen Anderson825b72b2009-08-11 20:47:22 +0000504 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
505 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
506 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
507 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000508 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000509 setExceptionPointerRegister(X86::RAX);
510 setExceptionSelectorRegister(X86::RDX);
511 } else {
512 setExceptionPointerRegister(X86::EAX);
513 setExceptionSelectorRegister(X86::EDX);
514 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
516 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000517
Duncan Sands4a544a72011-09-06 13:37:06 +0000518 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
519 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000520
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000522
Nate Begemanacc398c2006-01-25 18:21:52 +0000523 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000524 setOperationAction(ISD::VASTART , MVT::Other, Custom);
525 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000526 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 setOperationAction(ISD::VAARG , MVT::Other, Custom);
528 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000529 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::VAARG , MVT::Other, Expand);
531 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000532 }
Evan Chengae642192007-03-02 23:16:35 +0000533
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
535 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000536
537 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
538 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
539 MVT::i64 : MVT::i32, Custom);
540 else if (EnableSegmentedStacks)
541 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
542 MVT::i64 : MVT::i32, Custom);
543 else
544 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
545 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000546
Evan Chengc7ce29b2009-02-13 22:36:38 +0000547 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000548 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000549 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
551 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000552
Evan Cheng223547a2006-01-31 22:28:30 +0000553 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 setOperationAction(ISD::FABS , MVT::f64, Custom);
555 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000556
557 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 setOperationAction(ISD::FNEG , MVT::f64, Custom);
559 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000560
Evan Cheng68c47cb2007-01-05 07:55:56 +0000561 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
563 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000564
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000565 // Lower this to FGETSIGNx86 plus an AND.
566 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
567 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
568
Evan Chengd25e9e82006-02-02 00:28:23 +0000569 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setOperationAction(ISD::FSIN , MVT::f64, Expand);
571 setOperationAction(ISD::FCOS , MVT::f64, Expand);
572 setOperationAction(ISD::FSIN , MVT::f32, Expand);
573 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000574
Chris Lattnera54aa942006-01-29 06:26:08 +0000575 // Expand FP immediates into loads from the stack, except for the special
576 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000577 addLegalFPImmediate(APFloat(+0.0)); // xorpd
578 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000579 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000580 // Use SSE for f32, x87 for f64.
581 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
583 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000584
585 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000587
588 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000590
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
593 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
595 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000596
597 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::FSIN , MVT::f32, Expand);
599 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600
Nate Begemane1795842008-02-14 08:57:00 +0000601 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602 addLegalFPImmediate(APFloat(+0.0f)); // xorps
603 addLegalFPImmediate(APFloat(+0.0)); // FLD0
604 addLegalFPImmediate(APFloat(+1.0)); // FLD1
605 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
606 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
607
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000608 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
610 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000611 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000612 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000613 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
616 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000617
Owen Anderson825b72b2009-08-11 20:47:22 +0000618 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
619 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
620 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
621 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000622
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000623 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
625 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000627 addLegalFPImmediate(APFloat(+0.0)); // FLD0
628 addLegalFPImmediate(APFloat(+1.0)); // FLD1
629 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
630 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000631 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
632 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
633 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
634 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000635 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000636
Cameron Zwarich33390842011-07-08 21:39:21 +0000637 // We don't support FMA.
638 setOperationAction(ISD::FMA, MVT::f64, Expand);
639 setOperationAction(ISD::FMA, MVT::f32, Expand);
640
Dale Johannesen59a58732007-08-05 18:49:15 +0000641 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000642 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
644 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
645 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000646 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000647 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000648 addLegalFPImmediate(TmpFlt); // FLD0
649 TmpFlt.changeSign();
650 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000651
652 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000653 APFloat TmpFlt2(+1.0);
654 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
655 &ignored);
656 addLegalFPImmediate(TmpFlt2); // FLD1
657 TmpFlt2.changeSign();
658 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
659 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000660
Evan Chengc7ce29b2009-02-13 22:36:38 +0000661 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
663 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000664 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000665
666 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000667 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000668
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000669 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
671 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
672 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000673
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 setOperationAction(ISD::FLOG, MVT::f80, Expand);
675 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
676 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
677 setOperationAction(ISD::FEXP, MVT::f80, Expand);
678 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000679
Mon P Wangf007a8b2008-11-06 05:31:54 +0000680 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000681 // (for widening) or expand (for scalarization). Then we will selectively
682 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
684 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
685 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000701 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
702 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000724 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000725 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000734 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000735 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000739 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000740 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
741 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
742 setTruncStoreAction((MVT::SimpleValueType)VT,
743 (MVT::SimpleValueType)InnerVT, Expand);
744 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
745 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
746 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000747 }
748
Evan Chengc7ce29b2009-02-13 22:36:38 +0000749 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
750 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000751 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000752 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000753 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000754 }
755
Dale Johannesen0488fb62010-09-30 23:57:10 +0000756 // MMX-sized vectors (other than x86mmx) are expected to be expanded
757 // into smaller operations.
758 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
759 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
760 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
761 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
762 setOperationAction(ISD::AND, MVT::v8i8, Expand);
763 setOperationAction(ISD::AND, MVT::v4i16, Expand);
764 setOperationAction(ISD::AND, MVT::v2i32, Expand);
765 setOperationAction(ISD::AND, MVT::v1i64, Expand);
766 setOperationAction(ISD::OR, MVT::v8i8, Expand);
767 setOperationAction(ISD::OR, MVT::v4i16, Expand);
768 setOperationAction(ISD::OR, MVT::v2i32, Expand);
769 setOperationAction(ISD::OR, MVT::v1i64, Expand);
770 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
771 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
772 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
773 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
774 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
775 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
776 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
777 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
778 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
779 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
780 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
781 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
782 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000783 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
784 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
785 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
786 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000787
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000788 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000789 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000790
Owen Anderson825b72b2009-08-11 20:47:22 +0000791 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
792 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
793 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
794 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
795 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
796 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
797 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
798 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
799 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
800 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
801 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000802 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000803 }
804
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000805 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000807
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000808 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
809 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
811 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
812 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
813 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000814
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
816 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
817 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
818 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
819 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
820 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
821 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
822 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
823 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
824 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
825 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
826 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
827 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
828 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
829 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
830 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000831
Nadav Rotem354efd82011-09-18 14:57:03 +0000832 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000833 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
834 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
835 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000836
Owen Anderson825b72b2009-08-11 20:47:22 +0000837 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
838 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
839 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
840 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000842
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000843 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
844 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
845 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
846 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
847 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
848
Evan Cheng2c3ae372006-04-12 21:21:57 +0000849 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000850 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
851 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000852 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000853 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000854 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000855 // Do not attempt to custom lower non-128-bit vectors
856 if (!VT.is128BitVector())
857 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000858 setOperationAction(ISD::BUILD_VECTOR,
859 VT.getSimpleVT().SimpleTy, Custom);
860 setOperationAction(ISD::VECTOR_SHUFFLE,
861 VT.getSimpleVT().SimpleTy, Custom);
862 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
863 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000864 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000865
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
867 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
868 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
869 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
870 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
871 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000872
Nate Begemancdd1eec2008-02-12 22:51:28 +0000873 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
875 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000876 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000877
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000878 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
880 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000881 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000882
883 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000884 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000885 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000886
Owen Andersond6662ad2009-08-10 20:46:15 +0000887 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000889 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000890 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000891 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000893 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000895 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000896 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000897 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000898
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000900
Evan Cheng2c3ae372006-04-12 21:21:57 +0000901 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
903 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
904 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
905 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000906
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
908 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000909 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000910
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000911 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000912 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
913 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
914 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
915 setOperationAction(ISD::FRINT, MVT::f32, Legal);
916 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
917 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
918 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
919 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
920 setOperationAction(ISD::FRINT, MVT::f64, Legal);
921 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
922
Nate Begeman14d12ca2008-02-11 04:19:36 +0000923 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000924 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000925
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000926 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
927 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
928 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
929 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
930 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000931
Nate Begeman14d12ca2008-02-11 04:19:36 +0000932 // i8 and i16 vectors are custom , because the source register and source
933 // source memory operand types are not the same width. f32 vectors are
934 // custom since the immediate controlling the insert encodes additional
935 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
937 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
938 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
939 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000940
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
942 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
943 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
944 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000945
Pete Coopera77214a2011-11-14 19:38:42 +0000946 // FIXME: these should be Legal but thats only for the case where
947 // the index is constant. For now custom expand to deal with that
Nate Begeman14d12ca2008-02-11 04:19:36 +0000948 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000949 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
950 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000951 }
952 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000953
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000954 if (Subtarget->hasXMMInt()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000955 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000956 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000957
Nadav Rotem43012222011-05-11 08:12:09 +0000958 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000959 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000960
Nadav Rotem43012222011-05-11 08:12:09 +0000961 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000962 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000963
964 if (Subtarget->hasAVX2()) {
965 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
966 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
967
968 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
969 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
970
971 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
972 } else {
973 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
974 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
975
976 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
977 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
978
979 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
980 }
Nadav Rotem43012222011-05-11 08:12:09 +0000981 }
982
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000983 if (Subtarget->hasSSE42() || Subtarget->hasAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000984 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000985
David Greene9b9838d2009-06-29 16:47:10 +0000986 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000987 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
988 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
989 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
990 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
991 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
992 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000993
Owen Anderson825b72b2009-08-11 20:47:22 +0000994 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000995 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
996 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000997
Owen Anderson825b72b2009-08-11 20:47:22 +0000998 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
999 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1000 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1001 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1002 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
1003 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001004
Owen Anderson825b72b2009-08-11 20:47:22 +00001005 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1006 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1007 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1008 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1009 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1010 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001011
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001012 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1013 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001014 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001015
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00001016 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
1017 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
1018 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
1019 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
1020 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1021 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1022
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001023 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1024 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1025
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001026 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1027 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1028
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001029 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001030 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001031
Duncan Sands28b77e92011-09-06 19:07:46 +00001032 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1033 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1034 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1035 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001036
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001037 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1038 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1039 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1040
Craig Topperaaa643c2011-11-09 07:28:55 +00001041 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1042 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1043 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1044 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001045
Craig Topperaaa643c2011-11-09 07:28:55 +00001046 if (Subtarget->hasAVX2()) {
1047 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1048 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1049 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1050 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001051
Craig Topperaaa643c2011-11-09 07:28:55 +00001052 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1053 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1054 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1055 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001056
Craig Topperaaa643c2011-11-09 07:28:55 +00001057 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1058 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1059 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001060 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001061
1062 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001063
1064 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1065 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1066
1067 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1068 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1069
1070 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001071 } else {
1072 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1073 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1074 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1075 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1076
1077 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1078 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1079 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1080 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1081
1082 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1083 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1084 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1085 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001086
1087 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1088 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1089
1090 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1091 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1092
1093 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001094 }
Craig Topper13894fa2011-08-24 06:14:18 +00001095
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001096 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001097 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001098 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1099 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1100 EVT VT = SVT;
1101
1102 // Extract subvector is special because the value type
1103 // (result) is 128-bit but the source is 256-bit wide.
1104 if (VT.is128BitVector())
1105 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1106
1107 // Do not attempt to custom lower other non-256-bit vectors
1108 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001109 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001110
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001111 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1112 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1113 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1114 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001115 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001116 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001117 }
1118
David Greene54d8eba2011-01-27 22:38:56 +00001119 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001120 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1121 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1122 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001123
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001124 // Do not attempt to promote non-256-bit vectors
1125 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001126 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001127
1128 setOperationAction(ISD::AND, SVT, Promote);
1129 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1130 setOperationAction(ISD::OR, SVT, Promote);
1131 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1132 setOperationAction(ISD::XOR, SVT, Promote);
1133 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1134 setOperationAction(ISD::LOAD, SVT, Promote);
1135 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1136 setOperationAction(ISD::SELECT, SVT, Promote);
1137 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001138 }
David Greene9b9838d2009-06-29 16:47:10 +00001139 }
1140
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001141 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1142 // of this type with custom code.
1143 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1144 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1145 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1146 }
1147
Evan Cheng6be2c582006-04-05 23:38:46 +00001148 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001149 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001150
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001151
Eli Friedman962f5492010-06-02 19:35:46 +00001152 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1153 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001154 //
Eli Friedman962f5492010-06-02 19:35:46 +00001155 // FIXME: We really should do custom legalization for addition and
1156 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1157 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001158 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1159 // Add/Sub/Mul with overflow operations are custom lowered.
1160 MVT VT = IntVTs[i];
1161 setOperationAction(ISD::SADDO, VT, Custom);
1162 setOperationAction(ISD::UADDO, VT, Custom);
1163 setOperationAction(ISD::SSUBO, VT, Custom);
1164 setOperationAction(ISD::USUBO, VT, Custom);
1165 setOperationAction(ISD::SMULO, VT, Custom);
1166 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001167 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001168
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001169 // There are no 8-bit 3-address imul/mul instructions
1170 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1171 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001172
Evan Chengd54f2d52009-03-31 19:38:51 +00001173 if (!Subtarget->is64Bit()) {
1174 // These libcalls are not available in 32-bit.
1175 setLibcallName(RTLIB::SHL_I128, 0);
1176 setLibcallName(RTLIB::SRL_I128, 0);
1177 setLibcallName(RTLIB::SRA_I128, 0);
1178 }
1179
Evan Cheng206ee9d2006-07-07 08:33:52 +00001180 // We have target-specific dag combine patterns for the following nodes:
1181 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001182 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001183 setTargetDAGCombine(ISD::BUILD_VECTOR);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001184 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001185 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001186 setTargetDAGCombine(ISD::SHL);
1187 setTargetDAGCombine(ISD::SRA);
1188 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001189 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001190 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001191 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001192 setTargetDAGCombine(ISD::FADD);
1193 setTargetDAGCombine(ISD::FSUB);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001194 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001195 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001196 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001197 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001198 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001199 if (Subtarget->is64Bit())
1200 setTargetDAGCombine(ISD::MUL);
Craig Topperb4c94572011-10-21 06:55:01 +00001201 if (Subtarget->hasBMI())
1202 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001203
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001204 computeRegisterProperties();
1205
Evan Cheng05219282011-01-06 06:52:41 +00001206 // On Darwin, -Os means optimize for size without hurting performance,
1207 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001208 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001209 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001210 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001211 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1212 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1213 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001214 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001215 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001216
1217 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001218}
1219
Scott Michel5b8f82e2008-03-10 15:42:14 +00001220
Duncan Sands28b77e92011-09-06 19:07:46 +00001221EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1222 if (!VT.isVector()) return MVT::i8;
1223 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001224}
1225
1226
Evan Cheng29286502008-01-23 23:17:41 +00001227/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1228/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001229static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001230 if (MaxAlign == 16)
1231 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001232 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001233 if (VTy->getBitWidth() == 128)
1234 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001235 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001236 unsigned EltAlign = 0;
1237 getMaxByValAlign(ATy->getElementType(), EltAlign);
1238 if (EltAlign > MaxAlign)
1239 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001240 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001241 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1242 unsigned EltAlign = 0;
1243 getMaxByValAlign(STy->getElementType(i), EltAlign);
1244 if (EltAlign > MaxAlign)
1245 MaxAlign = EltAlign;
1246 if (MaxAlign == 16)
1247 break;
1248 }
1249 }
1250 return;
1251}
1252
1253/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1254/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001255/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1256/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001257unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001258 if (Subtarget->is64Bit()) {
1259 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001260 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001261 if (TyAlign > 8)
1262 return TyAlign;
1263 return 8;
1264 }
1265
Evan Cheng29286502008-01-23 23:17:41 +00001266 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001267 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001268 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001269 return Align;
1270}
Chris Lattner2b02a442007-02-25 08:29:00 +00001271
Evan Chengf0df0312008-05-15 08:39:06 +00001272/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001273/// and store operations as a result of memset, memcpy, and memmove
1274/// lowering. If DstAlign is zero that means it's safe to destination
1275/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1276/// means there isn't a need to check it against alignment requirement,
1277/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001278/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001279/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1280/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1281/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001282/// It returns EVT::Other if the type should be determined using generic
1283/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001284EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001285X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1286 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001287 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001288 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001289 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001290 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1291 // linux. This is because the stack realignment code can't handle certain
1292 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001293 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001294 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001295 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001296 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001297 (Subtarget->isUnalignedMemAccessFast() ||
1298 ((DstAlign == 0 || DstAlign >= 16) &&
1299 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001300 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001301 if (Subtarget->hasAVX() &&
1302 Subtarget->getStackAlignment() >= 32)
1303 return MVT::v8f32;
1304 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001305 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001306 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001307 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001308 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001309 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001310 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001311 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001312 // Do not use f64 to lower memcpy if source is string constant. It's
1313 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001314 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001315 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001316 }
Evan Chengf0df0312008-05-15 08:39:06 +00001317 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001318 return MVT::i64;
1319 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001320}
1321
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001322/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1323/// current function. The returned value is a member of the
1324/// MachineJumpTableInfo::JTEntryKind enum.
1325unsigned X86TargetLowering::getJumpTableEncoding() const {
1326 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1327 // symbol.
1328 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1329 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001330 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001331
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001332 // Otherwise, use the normal jump table encoding heuristics.
1333 return TargetLowering::getJumpTableEncoding();
1334}
1335
Chris Lattnerc64daab2010-01-26 05:02:42 +00001336const MCExpr *
1337X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1338 const MachineBasicBlock *MBB,
1339 unsigned uid,MCContext &Ctx) const{
1340 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1341 Subtarget->isPICStyleGOT());
1342 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1343 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001344 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1345 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001346}
1347
Evan Chengcc415862007-11-09 01:32:10 +00001348/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1349/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001350SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001351 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001352 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001353 // This doesn't have DebugLoc associated with it, but is not really the
1354 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001355 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001356 return Table;
1357}
1358
Chris Lattner589c6f62010-01-26 06:28:43 +00001359/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1360/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1361/// MCExpr.
1362const MCExpr *X86TargetLowering::
1363getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1364 MCContext &Ctx) const {
1365 // X86-64 uses RIP relative addressing based on the jump table label.
1366 if (Subtarget->isPICStyleRIPRel())
1367 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1368
1369 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001370 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001371}
1372
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001373// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001374std::pair<const TargetRegisterClass*, uint8_t>
1375X86TargetLowering::findRepresentativeClass(EVT VT) const{
1376 const TargetRegisterClass *RRC = 0;
1377 uint8_t Cost = 1;
1378 switch (VT.getSimpleVT().SimpleTy) {
1379 default:
1380 return TargetLowering::findRepresentativeClass(VT);
1381 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1382 RRC = (Subtarget->is64Bit()
1383 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1384 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001385 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001386 RRC = X86::VR64RegisterClass;
1387 break;
1388 case MVT::f32: case MVT::f64:
1389 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1390 case MVT::v4f32: case MVT::v2f64:
1391 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1392 case MVT::v4f64:
1393 RRC = X86::VR128RegisterClass;
1394 break;
1395 }
1396 return std::make_pair(RRC, Cost);
1397}
1398
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001399bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1400 unsigned &Offset) const {
1401 if (!Subtarget->isTargetLinux())
1402 return false;
1403
1404 if (Subtarget->is64Bit()) {
1405 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1406 Offset = 0x28;
1407 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1408 AddressSpace = 256;
1409 else
1410 AddressSpace = 257;
1411 } else {
1412 // %gs:0x14 on i386
1413 Offset = 0x14;
1414 AddressSpace = 256;
1415 }
1416 return true;
1417}
1418
1419
Chris Lattner2b02a442007-02-25 08:29:00 +00001420//===----------------------------------------------------------------------===//
1421// Return Value Calling Convention Implementation
1422//===----------------------------------------------------------------------===//
1423
Chris Lattner59ed56b2007-02-28 04:55:35 +00001424#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001425
Michael J. Spencerec38de22010-10-10 22:04:20 +00001426bool
Eric Christopher471e4222011-06-08 23:55:35 +00001427X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1428 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001429 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001430 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001431 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001432 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001433 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001434 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001435}
1436
Dan Gohman98ca4f22009-08-05 01:29:28 +00001437SDValue
1438X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001439 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001440 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001441 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001442 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001443 MachineFunction &MF = DAG.getMachineFunction();
1444 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001445
Chris Lattner9774c912007-02-27 05:28:59 +00001446 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001447 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001448 RVLocs, *DAG.getContext());
1449 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001450
Evan Chengdcea1632010-02-04 02:40:39 +00001451 // Add the regs to the liveout set for the function.
1452 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1453 for (unsigned i = 0; i != RVLocs.size(); ++i)
1454 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1455 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001456
Dan Gohman475871a2008-07-27 21:46:04 +00001457 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001458
Dan Gohman475871a2008-07-27 21:46:04 +00001459 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001460 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1461 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001462 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1463 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001464
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001465 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001466 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1467 CCValAssign &VA = RVLocs[i];
1468 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001469 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001470 EVT ValVT = ValToCopy.getValueType();
1471
Dale Johannesenc4510512010-09-24 19:05:48 +00001472 // If this is x86-64, and we disabled SSE, we can't return FP values,
1473 // or SSE or MMX vectors.
1474 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1475 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001476 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001477 report_fatal_error("SSE register return with SSE disabled");
1478 }
1479 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1480 // llvm-gcc has never done it right and no one has noticed, so this
1481 // should be OK for now.
1482 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001483 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001484 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001485
Chris Lattner447ff682008-03-11 03:23:40 +00001486 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1487 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001488 if (VA.getLocReg() == X86::ST0 ||
1489 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001490 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1491 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001492 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001493 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001494 RetOps.push_back(ValToCopy);
1495 // Don't emit a copytoreg.
1496 continue;
1497 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001498
Evan Cheng242b38b2009-02-23 09:03:22 +00001499 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1500 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001501 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001502 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001503 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001504 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001505 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1506 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001507 // If we don't have SSE2 available, convert to v4f32 so the generated
1508 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001509 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001510 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001511 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001512 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001513 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001514
Dale Johannesendd64c412009-02-04 00:33:20 +00001515 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001516 Flag = Chain.getValue(1);
1517 }
Dan Gohman61a92132008-04-21 23:59:07 +00001518
1519 // The x86-64 ABI for returning structs by value requires that we copy
1520 // the sret argument into %rax for the return. We saved the argument into
1521 // a virtual register in the entry block, so now we copy the value out
1522 // and into %rax.
1523 if (Subtarget->is64Bit() &&
1524 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1525 MachineFunction &MF = DAG.getMachineFunction();
1526 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1527 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001528 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001529 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001530 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001531
Dale Johannesendd64c412009-02-04 00:33:20 +00001532 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001533 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001534
1535 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001536 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001537 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001538
Chris Lattner447ff682008-03-11 03:23:40 +00001539 RetOps[0] = Chain; // Update chain.
1540
1541 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001542 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001543 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001544
1545 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001546 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001547}
1548
Evan Cheng3d2125c2010-11-30 23:55:39 +00001549bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1550 if (N->getNumValues() != 1)
1551 return false;
1552 if (!N->hasNUsesOfValue(1, 0))
1553 return false;
1554
1555 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001556 if (Copy->getOpcode() != ISD::CopyToReg &&
1557 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001558 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001559
1560 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001561 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001562 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001563 if (UI->getOpcode() != X86ISD::RET_FLAG)
1564 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001565 HasRet = true;
1566 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001567
Evan Cheng1bf891a2010-12-01 22:59:46 +00001568 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001569}
1570
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001571EVT
1572X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001573 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001574 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001575 // TODO: Is this also valid on 32-bit?
1576 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001577 ReturnMVT = MVT::i8;
1578 else
1579 ReturnMVT = MVT::i32;
1580
1581 EVT MinVT = getRegisterType(Context, ReturnMVT);
1582 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001583}
1584
Dan Gohman98ca4f22009-08-05 01:29:28 +00001585/// LowerCallResult - Lower the result values of a call into the
1586/// appropriate copies out of appropriate physical registers.
1587///
1588SDValue
1589X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001590 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001591 const SmallVectorImpl<ISD::InputArg> &Ins,
1592 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001593 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001594
Chris Lattnere32bbf62007-02-28 07:09:55 +00001595 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001596 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001597 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001598 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1599 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001600 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001601
Chris Lattner3085e152007-02-25 08:59:22 +00001602 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001603 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001604 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001605 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001606
Torok Edwin3f142c32009-02-01 18:15:56 +00001607 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001608 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001609 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001610 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001611 }
1612
Evan Cheng79fb3b42009-02-20 20:43:02 +00001613 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001614
1615 // If this is a call to a function that returns an fp value on the floating
1616 // point stack, we must guarantee the the value is popped from the stack, so
1617 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001618 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001619 // instead.
1620 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1621 // If we prefer to use the value in xmm registers, copy it out as f80 and
1622 // use a truncate to move it from fp stack reg to xmm reg.
1623 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001624 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001625 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1626 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001627 Val = Chain.getValue(0);
1628
1629 // Round the f80 to the right size, which also moves it to the appropriate
1630 // xmm register.
1631 if (CopyVT != VA.getValVT())
1632 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1633 // This truncation won't change the value.
1634 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001635 } else {
1636 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1637 CopyVT, InFlag).getValue(1);
1638 Val = Chain.getValue(0);
1639 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001640 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001641 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001642 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001643
Dan Gohman98ca4f22009-08-05 01:29:28 +00001644 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001645}
1646
1647
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001648//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001649// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001650//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001651// StdCall calling convention seems to be standard for many Windows' API
1652// routines and around. It differs from C calling convention just a little:
1653// callee should clean up the stack, not caller. Symbols should be also
1654// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001655// For info on fast calling convention see Fast Calling Convention (tail call)
1656// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001657
Dan Gohman98ca4f22009-08-05 01:29:28 +00001658/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001659/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001660static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1661 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001662 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001663
Dan Gohman98ca4f22009-08-05 01:29:28 +00001664 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001665}
1666
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001667/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001668/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001669static bool
1670ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1671 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001672 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001673
Dan Gohman98ca4f22009-08-05 01:29:28 +00001674 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001675}
1676
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001677/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1678/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001679/// the specific parameter attribute. The copy will be passed as a byval
1680/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001681static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001682CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001683 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1684 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001685 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001686
Dale Johannesendd64c412009-02-04 00:33:20 +00001687 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001688 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001689 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001690}
1691
Chris Lattner29689432010-03-11 00:22:57 +00001692/// IsTailCallConvention - Return true if the calling convention is one that
1693/// supports tail call optimization.
1694static bool IsTailCallConvention(CallingConv::ID CC) {
1695 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1696}
1697
Evan Cheng485fafc2011-03-21 01:19:09 +00001698bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1699 if (!CI->isTailCall())
1700 return false;
1701
1702 CallSite CS(CI);
1703 CallingConv::ID CalleeCC = CS.getCallingConv();
1704 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1705 return false;
1706
1707 return true;
1708}
1709
Evan Cheng0c439eb2010-01-27 00:07:07 +00001710/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1711/// a tailcall target by changing its ABI.
1712static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001713 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001714}
1715
Dan Gohman98ca4f22009-08-05 01:29:28 +00001716SDValue
1717X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001718 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001719 const SmallVectorImpl<ISD::InputArg> &Ins,
1720 DebugLoc dl, SelectionDAG &DAG,
1721 const CCValAssign &VA,
1722 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001723 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001724 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001725 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001726 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001727 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001728 EVT ValVT;
1729
1730 // If value is passed by pointer we have address passed instead of the value
1731 // itself.
1732 if (VA.getLocInfo() == CCValAssign::Indirect)
1733 ValVT = VA.getLocVT();
1734 else
1735 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001736
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001737 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001738 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001739 // In case of tail call optimization mark all arguments mutable. Since they
1740 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001741 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001742 unsigned Bytes = Flags.getByValSize();
1743 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1744 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001745 return DAG.getFrameIndex(FI, getPointerTy());
1746 } else {
1747 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001748 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001749 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1750 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001751 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001752 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001753 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001754}
1755
Dan Gohman475871a2008-07-27 21:46:04 +00001756SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001757X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001758 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001759 bool isVarArg,
1760 const SmallVectorImpl<ISD::InputArg> &Ins,
1761 DebugLoc dl,
1762 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001763 SmallVectorImpl<SDValue> &InVals)
1764 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001765 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001766 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001767
Gordon Henriksen86737662008-01-05 16:56:59 +00001768 const Function* Fn = MF.getFunction();
1769 if (Fn->hasExternalLinkage() &&
1770 Subtarget->isTargetCygMing() &&
1771 Fn->getName() == "main")
1772 FuncInfo->setForceFramePointer(true);
1773
Evan Cheng1bc78042006-04-26 01:20:17 +00001774 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001775 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001776 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001777
Chris Lattner29689432010-03-11 00:22:57 +00001778 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1779 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001780
Chris Lattner638402b2007-02-28 07:00:42 +00001781 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001782 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001783 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001784 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001785
1786 // Allocate shadow area for Win64
1787 if (IsWin64) {
1788 CCInfo.AllocateStack(32, 8);
1789 }
1790
Duncan Sands45907662010-10-31 13:21:44 +00001791 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001792
Chris Lattnerf39f7712007-02-28 05:46:49 +00001793 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001794 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001795 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1796 CCValAssign &VA = ArgLocs[i];
1797 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1798 // places.
1799 assert(VA.getValNo() != LastVal &&
1800 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001801 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001802 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001803
Chris Lattnerf39f7712007-02-28 05:46:49 +00001804 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001805 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001806 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001807 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001808 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001809 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001810 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001811 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001812 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001813 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001814 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001815 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1816 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001817 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001818 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001819 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001820 RC = X86::VR64RegisterClass;
1821 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001822 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001823
Devang Patel68e6bee2011-02-21 23:21:26 +00001824 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001825 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001826
Chris Lattnerf39f7712007-02-28 05:46:49 +00001827 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1828 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1829 // right size.
1830 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001831 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001832 DAG.getValueType(VA.getValVT()));
1833 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001834 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001835 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001836 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001837 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001838
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001839 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001840 // Handle MMX values passed in XMM regs.
1841 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001842 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1843 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001844 } else
1845 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001846 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001847 } else {
1848 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001849 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001850 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001851
1852 // If value is passed via pointer - do a load.
1853 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001854 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001855 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001856
Dan Gohman98ca4f22009-08-05 01:29:28 +00001857 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001858 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001859
Dan Gohman61a92132008-04-21 23:59:07 +00001860 // The x86-64 ABI for returning structs by value requires that we copy
1861 // the sret argument into %rax for the return. Save the argument into
1862 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001863 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001864 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1865 unsigned Reg = FuncInfo->getSRetReturnReg();
1866 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001867 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001868 FuncInfo->setSRetReturnReg(Reg);
1869 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001870 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001871 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001872 }
1873
Chris Lattnerf39f7712007-02-28 05:46:49 +00001874 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001875 // Align stack specially for tail calls.
1876 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001877 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001878
Evan Cheng1bc78042006-04-26 01:20:17 +00001879 // If the function takes variable number of arguments, make a frame index for
1880 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001881 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001882 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1883 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001884 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001885 }
1886 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001887 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1888
1889 // FIXME: We should really autogenerate these arrays
1890 static const unsigned GPR64ArgRegsWin64[] = {
1891 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001892 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001893 static const unsigned GPR64ArgRegs64Bit[] = {
1894 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1895 };
1896 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001897 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1898 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1899 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001900 const unsigned *GPR64ArgRegs;
1901 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001902
1903 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001904 // The XMM registers which might contain var arg parameters are shadowed
1905 // in their paired GPR. So we only need to save the GPR to their home
1906 // slots.
1907 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001908 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001909 } else {
1910 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1911 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001912
1913 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001914 }
1915 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1916 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001917
Devang Patel578efa92009-06-05 21:57:13 +00001918 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001919 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001920 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001921 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001922 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001923 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001924 // Kernel mode asks for SSE to be disabled, so don't push them
1925 // on the stack.
1926 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001927
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001928 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001929 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001930 // Get to the caller-allocated home save location. Add 8 to account
1931 // for the return address.
1932 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001933 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001934 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001935 // Fixup to set vararg frame on shadow area (4 x i64).
1936 if (NumIntRegs < 4)
1937 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001938 } else {
1939 // For X86-64, if there are vararg parameters that are passed via
1940 // registers, then we must store them to their spots on the stack so they
1941 // may be loaded by deferencing the result of va_next.
1942 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1943 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1944 FuncInfo->setRegSaveFrameIndex(
1945 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001946 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001947 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001948
Gordon Henriksen86737662008-01-05 16:56:59 +00001949 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001950 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001951 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1952 getPointerTy());
1953 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001954 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001955 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1956 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001957 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001958 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001959 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001960 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001961 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001962 MachinePointerInfo::getFixedStack(
1963 FuncInfo->getRegSaveFrameIndex(), Offset),
1964 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001965 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001966 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001967 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001968
Dan Gohmanface41a2009-08-16 21:24:25 +00001969 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1970 // Now store the XMM (fp + vector) parameter registers.
1971 SmallVector<SDValue, 11> SaveXMMOps;
1972 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001973
Devang Patel68e6bee2011-02-21 23:21:26 +00001974 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001975 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1976 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001977
Dan Gohman1e93df62010-04-17 14:41:14 +00001978 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1979 FuncInfo->getRegSaveFrameIndex()));
1980 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1981 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001982
Dan Gohmanface41a2009-08-16 21:24:25 +00001983 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001984 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001985 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001986 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1987 SaveXMMOps.push_back(Val);
1988 }
1989 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1990 MVT::Other,
1991 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001992 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001993
1994 if (!MemOps.empty())
1995 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1996 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001997 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001998 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001999
Gordon Henriksen86737662008-01-05 16:56:59 +00002000 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00002001 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002002 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002003 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002004 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002005 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00002006 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00002007 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002008 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002009
Gordon Henriksen86737662008-01-05 16:56:59 +00002010 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002011 // RegSaveFrameIndex is X86-64 only.
2012 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002013 if (CallConv == CallingConv::X86_FastCall ||
2014 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002015 // fastcc functions can't have varargs.
2016 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002017 }
Evan Cheng25caf632006-05-23 21:06:34 +00002018
Rafael Espindola76927d752011-08-30 19:39:58 +00002019 FuncInfo->setArgumentStackSize(StackSize);
2020
Dan Gohman98ca4f22009-08-05 01:29:28 +00002021 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002022}
2023
Dan Gohman475871a2008-07-27 21:46:04 +00002024SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002025X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2026 SDValue StackPtr, SDValue Arg,
2027 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002028 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002029 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002030 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002031 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002032 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002033 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002034 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002035
2036 return DAG.getStore(Chain, dl, Arg, PtrOff,
2037 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002038 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002039}
2040
Bill Wendling64e87322009-01-16 19:25:27 +00002041/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002042/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002043SDValue
2044X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002045 SDValue &OutRetAddr, SDValue Chain,
2046 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002047 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002048 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002049 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002050 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002051
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002052 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002053 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002054 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002055 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002056}
2057
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002058/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002059/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002060static SDValue
2061EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002062 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002063 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002064 // Store the return address to the appropriate stack slot.
2065 if (!FPDiff) return Chain;
2066 // Calculate the new stack slot for the return address.
2067 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002068 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002069 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002070 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002071 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002072 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002073 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002074 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002075 return Chain;
2076}
2077
Dan Gohman98ca4f22009-08-05 01:29:28 +00002078SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002079X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002080 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002081 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002082 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002083 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002084 const SmallVectorImpl<ISD::InputArg> &Ins,
2085 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002086 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002087 MachineFunction &MF = DAG.getMachineFunction();
2088 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002089 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002090 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002091 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002092
Evan Cheng5f941932010-02-05 02:21:12 +00002093 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002094 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002095 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2096 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002097 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002098
2099 // Sibcalls are automatically detected tailcalls which do not require
2100 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002101 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002102 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002103
2104 if (isTailCall)
2105 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002106 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002107
Chris Lattner29689432010-03-11 00:22:57 +00002108 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2109 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002110
Chris Lattner638402b2007-02-28 07:00:42 +00002111 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002112 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002113 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002114 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002115
2116 // Allocate shadow area for Win64
2117 if (IsWin64) {
2118 CCInfo.AllocateStack(32, 8);
2119 }
2120
Duncan Sands45907662010-10-31 13:21:44 +00002121 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002122
Chris Lattner423c5f42007-02-28 05:31:48 +00002123 // Get a count of how many bytes are to be pushed on the stack.
2124 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002125 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002126 // This is a sibcall. The memory operands are available in caller's
2127 // own caller's stack.
2128 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002129 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002130 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002131
Gordon Henriksen86737662008-01-05 16:56:59 +00002132 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002133 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002134 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002135 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002136 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2137 FPDiff = NumBytesCallerPushed - NumBytes;
2138
2139 // Set the delta of movement of the returnaddr stackslot.
2140 // But only set if delta is greater than previous delta.
2141 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2142 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2143 }
2144
Evan Chengf22f9b32010-02-06 03:28:46 +00002145 if (!IsSibcall)
2146 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002147
Dan Gohman475871a2008-07-27 21:46:04 +00002148 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002149 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002150 if (isTailCall && FPDiff)
2151 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2152 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002153
Dan Gohman475871a2008-07-27 21:46:04 +00002154 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2155 SmallVector<SDValue, 8> MemOpChains;
2156 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002157
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002158 // Walk the register/memloc assignments, inserting copies/loads. In the case
2159 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002160 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2161 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002162 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002163 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002164 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002165 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002166
Chris Lattner423c5f42007-02-28 05:31:48 +00002167 // Promote the value if needed.
2168 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002169 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002170 case CCValAssign::Full: break;
2171 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002172 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002173 break;
2174 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002175 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002176 break;
2177 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002178 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2179 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002180 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002181 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2182 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002183 } else
2184 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2185 break;
2186 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002187 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002188 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002189 case CCValAssign::Indirect: {
2190 // Store the argument.
2191 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002192 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002193 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002194 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002195 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002196 Arg = SpillSlot;
2197 break;
2198 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002199 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002200
Chris Lattner423c5f42007-02-28 05:31:48 +00002201 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002202 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2203 if (isVarArg && IsWin64) {
2204 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2205 // shadow reg if callee is a varargs function.
2206 unsigned ShadowReg = 0;
2207 switch (VA.getLocReg()) {
2208 case X86::XMM0: ShadowReg = X86::RCX; break;
2209 case X86::XMM1: ShadowReg = X86::RDX; break;
2210 case X86::XMM2: ShadowReg = X86::R8; break;
2211 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002212 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002213 if (ShadowReg)
2214 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002215 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002216 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002217 assert(VA.isMemLoc());
2218 if (StackPtr.getNode() == 0)
2219 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2220 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2221 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002222 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002223 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002224
Evan Cheng32fe1032006-05-25 00:59:30 +00002225 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002226 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002227 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002228
Evan Cheng347d5f72006-04-28 21:29:37 +00002229 // Build a sequence of copy-to-reg nodes chained together with token chain
2230 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002231 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002232 // Tail call byval lowering might overwrite argument registers so in case of
2233 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002234 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002235 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002236 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002237 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002238 InFlag = Chain.getValue(1);
2239 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002240
Chris Lattner88e1fd52009-07-09 04:24:46 +00002241 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002242 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2243 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002244 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002245 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2246 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002247 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002248 InFlag);
2249 InFlag = Chain.getValue(1);
2250 } else {
2251 // If we are tail calling and generating PIC/GOT style code load the
2252 // address of the callee into ECX. The value in ecx is used as target of
2253 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2254 // for tail calls on PIC/GOT architectures. Normally we would just put the
2255 // address of GOT into ebx and then call target@PLT. But for tail calls
2256 // ebx would be restored (since ebx is callee saved) before jumping to the
2257 // target@PLT.
2258
2259 // Note: The actual moving to ECX is done further down.
2260 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2261 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2262 !G->getGlobal()->hasProtectedVisibility())
2263 Callee = LowerGlobalAddress(Callee, DAG);
2264 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002265 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002266 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002267 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002268
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002269 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002270 // From AMD64 ABI document:
2271 // For calls that may call functions that use varargs or stdargs
2272 // (prototype-less calls or calls to functions containing ellipsis (...) in
2273 // the declaration) %al is used as hidden argument to specify the number
2274 // of SSE registers used. The contents of %al do not need to match exactly
2275 // the number of registers, but must be an ubound on the number of SSE
2276 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002277
Gordon Henriksen86737662008-01-05 16:56:59 +00002278 // Count the number of XMM registers allocated.
2279 static const unsigned XMMArgRegs[] = {
2280 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2281 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2282 };
2283 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002284 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002285 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002286
Dale Johannesendd64c412009-02-04 00:33:20 +00002287 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002288 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002289 InFlag = Chain.getValue(1);
2290 }
2291
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002292
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002293 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002294 if (isTailCall) {
2295 // Force all the incoming stack arguments to be loaded from the stack
2296 // before any new outgoing arguments are stored to the stack, because the
2297 // outgoing stack slots may alias the incoming argument stack slots, and
2298 // the alias isn't otherwise explicit. This is slightly more conservative
2299 // than necessary, because it means that each store effectively depends
2300 // on every argument instead of just those arguments it would clobber.
2301 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2302
Dan Gohman475871a2008-07-27 21:46:04 +00002303 SmallVector<SDValue, 8> MemOpChains2;
2304 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002305 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002306 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002307 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002308 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002309 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2310 CCValAssign &VA = ArgLocs[i];
2311 if (VA.isRegLoc())
2312 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002313 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002314 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002315 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002316 // Create frame index.
2317 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002318 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002319 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002320 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002321
Duncan Sands276dcbd2008-03-21 09:14:45 +00002322 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002323 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002324 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002325 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002326 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002327 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002328 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002329
Dan Gohman98ca4f22009-08-05 01:29:28 +00002330 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2331 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002332 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002333 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002334 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002335 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002336 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002337 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002338 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002339 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002340 }
2341 }
2342
2343 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002344 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002345 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002346
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002347 // Copy arguments to their registers.
2348 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002349 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002350 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002351 InFlag = Chain.getValue(1);
2352 }
Dan Gohman475871a2008-07-27 21:46:04 +00002353 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002354
Gordon Henriksen86737662008-01-05 16:56:59 +00002355 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002356 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002357 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002358 }
2359
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002360 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2361 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2362 // In the 64-bit large code model, we have to make all calls
2363 // through a register, since the call instruction's 32-bit
2364 // pc-relative offset may not be large enough to hold the whole
2365 // address.
2366 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002367 // If the callee is a GlobalAddress node (quite common, every direct call
2368 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2369 // it.
2370
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002371 // We should use extra load for direct calls to dllimported functions in
2372 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002373 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002374 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002375 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002376 bool ExtraLoad = false;
2377 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002378
Chris Lattner48a7d022009-07-09 05:02:21 +00002379 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2380 // external symbols most go through the PLT in PIC mode. If the symbol
2381 // has hidden or protected visibility, or if it is static or local, then
2382 // we don't need to use the PLT - we can directly call it.
2383 if (Subtarget->isTargetELF() &&
2384 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002385 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002386 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002387 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002388 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002389 (!Subtarget->getTargetTriple().isMacOSX() ||
2390 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002391 // PC-relative references to external symbols should go through $stub,
2392 // unless we're building with the leopard linker or later, which
2393 // automatically synthesizes these stubs.
2394 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002395 } else if (Subtarget->isPICStyleRIPRel() &&
2396 isa<Function>(GV) &&
2397 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2398 // If the function is marked as non-lazy, generate an indirect call
2399 // which loads from the GOT directly. This avoids runtime overhead
2400 // at the cost of eager binding (and one extra byte of encoding).
2401 OpFlags = X86II::MO_GOTPCREL;
2402 WrapperKind = X86ISD::WrapperRIP;
2403 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002404 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002405
Devang Patel0d881da2010-07-06 22:08:15 +00002406 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002407 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002408
2409 // Add a wrapper if needed.
2410 if (WrapperKind != ISD::DELETED_NODE)
2411 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2412 // Add extra indirection if needed.
2413 if (ExtraLoad)
2414 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2415 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002416 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002417 }
Bill Wendling056292f2008-09-16 21:48:12 +00002418 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002419 unsigned char OpFlags = 0;
2420
Evan Cheng1bf891a2010-12-01 22:59:46 +00002421 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2422 // external symbols should go through the PLT.
2423 if (Subtarget->isTargetELF() &&
2424 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2425 OpFlags = X86II::MO_PLT;
2426 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002427 (!Subtarget->getTargetTriple().isMacOSX() ||
2428 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002429 // PC-relative references to external symbols should go through $stub,
2430 // unless we're building with the leopard linker or later, which
2431 // automatically synthesizes these stubs.
2432 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002433 }
Eric Christopherfd179292009-08-27 18:07:15 +00002434
Chris Lattner48a7d022009-07-09 05:02:21 +00002435 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2436 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002437 }
2438
Chris Lattnerd96d0722007-02-25 06:40:16 +00002439 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002440 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002441 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002442
Evan Chengf22f9b32010-02-06 03:28:46 +00002443 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002444 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2445 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002446 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002447 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002448
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002449 Ops.push_back(Chain);
2450 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002451
Dan Gohman98ca4f22009-08-05 01:29:28 +00002452 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002453 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002454
Gordon Henriksen86737662008-01-05 16:56:59 +00002455 // Add argument registers to the end of the list so that they are known live
2456 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002457 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2458 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2459 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002460
Evan Cheng586ccac2008-03-18 23:36:35 +00002461 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002462 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002463 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2464
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002465 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002466 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002467 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002468
Gabor Greifba36cb52008-08-28 21:40:38 +00002469 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002470 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002471
Dan Gohman98ca4f22009-08-05 01:29:28 +00002472 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002473 // We used to do:
2474 //// If this is the first return lowered for this function, add the regs
2475 //// to the liveout set for the function.
2476 // This isn't right, although it's probably harmless on x86; liveouts
2477 // should be computed from returns not tail calls. Consider a void
2478 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002479 return DAG.getNode(X86ISD::TC_RETURN, dl,
2480 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002481 }
2482
Dale Johannesenace16102009-02-03 19:33:06 +00002483 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002484 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002485
Chris Lattner2d297092006-05-23 18:50:38 +00002486 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002487 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002488 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002489 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002490 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002491 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002492 // pops the hidden struct pointer, so we have to push it back.
2493 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002494 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002495 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002496 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002497
Gordon Henriksenae636f82008-01-03 16:47:34 +00002498 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002499 if (!IsSibcall) {
2500 Chain = DAG.getCALLSEQ_END(Chain,
2501 DAG.getIntPtrConstant(NumBytes, true),
2502 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2503 true),
2504 InFlag);
2505 InFlag = Chain.getValue(1);
2506 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002507
Chris Lattner3085e152007-02-25 08:59:22 +00002508 // Handle result values, copying them out of physregs into vregs that we
2509 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002510 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2511 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002512}
2513
Evan Cheng25ab6902006-09-08 06:48:29 +00002514
2515//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002516// Fast Calling Convention (tail call) implementation
2517//===----------------------------------------------------------------------===//
2518
2519// Like std call, callee cleans arguments, convention except that ECX is
2520// reserved for storing the tail called function address. Only 2 registers are
2521// free for argument passing (inreg). Tail call optimization is performed
2522// provided:
2523// * tailcallopt is enabled
2524// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002525// On X86_64 architecture with GOT-style position independent code only local
2526// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002527// To keep the stack aligned according to platform abi the function
2528// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2529// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002530// If a tail called function callee has more arguments than the caller the
2531// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002532// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002533// original REtADDR, but before the saved framepointer or the spilled registers
2534// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2535// stack layout:
2536// arg1
2537// arg2
2538// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002539// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002540// move area ]
2541// (possible EBP)
2542// ESI
2543// EDI
2544// local1 ..
2545
2546/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2547/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002548unsigned
2549X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2550 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002551 MachineFunction &MF = DAG.getMachineFunction();
2552 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002553 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002554 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002555 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002556 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002557 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002558 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2559 // Number smaller than 12 so just add the difference.
2560 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2561 } else {
2562 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002563 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002564 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002565 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002566 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002567}
2568
Evan Cheng5f941932010-02-05 02:21:12 +00002569/// MatchingStackOffset - Return true if the given stack call argument is
2570/// already available in the same position (relatively) of the caller's
2571/// incoming argument stack.
2572static
2573bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2574 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2575 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002576 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2577 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002578 if (Arg.getOpcode() == ISD::CopyFromReg) {
2579 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002580 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002581 return false;
2582 MachineInstr *Def = MRI->getVRegDef(VR);
2583 if (!Def)
2584 return false;
2585 if (!Flags.isByVal()) {
2586 if (!TII->isLoadFromStackSlot(Def, FI))
2587 return false;
2588 } else {
2589 unsigned Opcode = Def->getOpcode();
2590 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2591 Def->getOperand(1).isFI()) {
2592 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002593 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002594 } else
2595 return false;
2596 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002597 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2598 if (Flags.isByVal())
2599 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002600 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002601 // define @foo(%struct.X* %A) {
2602 // tail call @bar(%struct.X* byval %A)
2603 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002604 return false;
2605 SDValue Ptr = Ld->getBasePtr();
2606 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2607 if (!FINode)
2608 return false;
2609 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002610 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002611 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002612 FI = FINode->getIndex();
2613 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002614 } else
2615 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002616
Evan Cheng4cae1332010-03-05 08:38:04 +00002617 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002618 if (!MFI->isFixedObjectIndex(FI))
2619 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002620 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002621}
2622
Dan Gohman98ca4f22009-08-05 01:29:28 +00002623/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2624/// for tail call optimization. Targets which want to do tail call
2625/// optimization should implement this function.
2626bool
2627X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002628 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002629 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002630 bool isCalleeStructRet,
2631 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002632 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002633 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002634 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002635 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002636 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002637 CalleeCC != CallingConv::C)
2638 return false;
2639
Evan Cheng7096ae42010-01-29 06:45:59 +00002640 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002641 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002642 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002643 CallingConv::ID CallerCC = CallerF->getCallingConv();
2644 bool CCMatch = CallerCC == CalleeCC;
2645
Dan Gohman1797ed52010-02-08 20:27:50 +00002646 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002647 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002648 return true;
2649 return false;
2650 }
2651
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002652 // Look for obvious safe cases to perform tail call optimization that do not
2653 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002654
Evan Cheng2c12cb42010-03-26 16:26:03 +00002655 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2656 // emit a special epilogue.
2657 if (RegInfo->needsStackRealignment(MF))
2658 return false;
2659
Evan Chenga375d472010-03-15 18:54:48 +00002660 // Also avoid sibcall optimization if either caller or callee uses struct
2661 // return semantics.
2662 if (isCalleeStructRet || isCallerStructRet)
2663 return false;
2664
Chad Rosier2416da32011-06-24 21:15:36 +00002665 // An stdcall caller is expected to clean up its arguments; the callee
2666 // isn't going to do that.
2667 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2668 return false;
2669
Chad Rosier871f6642011-05-18 19:59:50 +00002670 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002671 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002672 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002673
2674 // Optimizing for varargs on Win64 is unlikely to be safe without
2675 // additional testing.
2676 if (Subtarget->isTargetWin64())
2677 return false;
2678
Chad Rosier871f6642011-05-18 19:59:50 +00002679 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002680 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2681 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002682
Chad Rosier871f6642011-05-18 19:59:50 +00002683 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2684 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2685 if (!ArgLocs[i].isRegLoc())
2686 return false;
2687 }
2688
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002689 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2690 // Therefore if it's not used by the call it is not safe to optimize this into
2691 // a sibcall.
2692 bool Unused = false;
2693 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2694 if (!Ins[i].Used) {
2695 Unused = true;
2696 break;
2697 }
2698 }
2699 if (Unused) {
2700 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002701 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2702 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002703 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002704 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002705 CCValAssign &VA = RVLocs[i];
2706 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2707 return false;
2708 }
2709 }
2710
Evan Cheng13617962010-04-30 01:12:32 +00002711 // If the calling conventions do not match, then we'd better make sure the
2712 // results are returned in the same way as what the caller expects.
2713 if (!CCMatch) {
2714 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002715 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2716 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002717 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2718
2719 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002720 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2721 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002722 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2723
2724 if (RVLocs1.size() != RVLocs2.size())
2725 return false;
2726 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2727 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2728 return false;
2729 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2730 return false;
2731 if (RVLocs1[i].isRegLoc()) {
2732 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2733 return false;
2734 } else {
2735 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2736 return false;
2737 }
2738 }
2739 }
2740
Evan Chenga6bff982010-01-30 01:22:00 +00002741 // If the callee takes no arguments then go on to check the results of the
2742 // call.
2743 if (!Outs.empty()) {
2744 // Check if stack adjustment is needed. For now, do not do this if any
2745 // argument is passed on the stack.
2746 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002747 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2748 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002749
2750 // Allocate shadow area for Win64
2751 if (Subtarget->isTargetWin64()) {
2752 CCInfo.AllocateStack(32, 8);
2753 }
2754
Duncan Sands45907662010-10-31 13:21:44 +00002755 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002756 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002757 MachineFunction &MF = DAG.getMachineFunction();
2758 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2759 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002760
2761 // Check if the arguments are already laid out in the right way as
2762 // the caller's fixed stack objects.
2763 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002764 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2765 const X86InstrInfo *TII =
2766 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002767 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2768 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002769 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002770 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002771 if (VA.getLocInfo() == CCValAssign::Indirect)
2772 return false;
2773 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002774 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2775 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002776 return false;
2777 }
2778 }
2779 }
Evan Cheng9c044672010-05-29 01:35:22 +00002780
2781 // If the tailcall address may be in a register, then make sure it's
2782 // possible to register allocate for it. In 32-bit, the call address can
2783 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002784 // callee-saved registers are restored. These happen to be the same
2785 // registers used to pass 'inreg' arguments so watch out for those.
2786 if (!Subtarget->is64Bit() &&
2787 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002788 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002789 unsigned NumInRegs = 0;
2790 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2791 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002792 if (!VA.isRegLoc())
2793 continue;
2794 unsigned Reg = VA.getLocReg();
2795 switch (Reg) {
2796 default: break;
2797 case X86::EAX: case X86::EDX: case X86::ECX:
2798 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002799 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002800 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002801 }
2802 }
2803 }
Evan Chenga6bff982010-01-30 01:22:00 +00002804 }
Evan Chengb1712452010-01-27 06:25:16 +00002805
Evan Cheng86809cc2010-02-03 03:28:02 +00002806 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002807}
2808
Dan Gohman3df24e62008-09-03 23:12:08 +00002809FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002810X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2811 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002812}
2813
2814
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002815//===----------------------------------------------------------------------===//
2816// Other Lowering Hooks
2817//===----------------------------------------------------------------------===//
2818
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002819static bool MayFoldLoad(SDValue Op) {
2820 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2821}
2822
2823static bool MayFoldIntoStore(SDValue Op) {
2824 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2825}
2826
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002827static bool isTargetShuffle(unsigned Opcode) {
2828 switch(Opcode) {
2829 default: return false;
2830 case X86ISD::PSHUFD:
2831 case X86ISD::PSHUFHW:
2832 case X86ISD::PSHUFLW:
2833 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002834 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002835 case X86ISD::SHUFPS:
2836 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002837 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002838 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002839 case X86ISD::MOVLPS:
2840 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002841 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002842 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002843 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002844 case X86ISD::MOVSS:
2845 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002846 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002847 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002848 case X86ISD::VUNPCKLPSY:
2849 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002850 case X86ISD::PUNPCKLWD:
2851 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002852 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002853 case X86ISD::PUNPCKLQDQ:
Craig Topper6347e862011-11-21 06:57:39 +00002854 case X86ISD::VPUNPCKLWDY:
Craig Topper6fa583d2011-11-21 08:26:50 +00002855 case X86ISD::VPUNPCKLBWY:
Craig Topper6347e862011-11-21 06:57:39 +00002856 case X86ISD::VPUNPCKLDQY:
2857 case X86ISD::VPUNPCKLQDQY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002858 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002859 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002860 case X86ISD::VUNPCKHPSY:
2861 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002862 case X86ISD::PUNPCKHWD:
2863 case X86ISD::PUNPCKHBW:
2864 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002865 case X86ISD::PUNPCKHQDQ:
Craig Topper6347e862011-11-21 06:57:39 +00002866 case X86ISD::VPUNPCKHWDY:
Craig Topper6fa583d2011-11-21 08:26:50 +00002867 case X86ISD::VPUNPCKHBWY:
Craig Topper6347e862011-11-21 06:57:39 +00002868 case X86ISD::VPUNPCKHDQY:
2869 case X86ISD::VPUNPCKHQDQY:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002870 case X86ISD::VPERMILPS:
2871 case X86ISD::VPERMILPSY:
2872 case X86ISD::VPERMILPD:
2873 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002874 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002875 return true;
2876 }
2877 return false;
2878}
2879
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002880static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002881 SDValue V1, SelectionDAG &DAG) {
2882 switch(Opc) {
2883 default: llvm_unreachable("Unknown x86 shuffle node");
2884 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002885 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002886 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002887 return DAG.getNode(Opc, dl, VT, V1);
2888 }
2889
2890 return SDValue();
2891}
2892
2893static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002894 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002895 switch(Opc) {
2896 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002897 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002898 case X86ISD::PSHUFHW:
2899 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002900 case X86ISD::VPERMILPS:
2901 case X86ISD::VPERMILPSY:
2902 case X86ISD::VPERMILPD:
2903 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002904 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2905 }
2906
2907 return SDValue();
2908}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002909
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002910static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2911 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2912 switch(Opc) {
2913 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002914 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002915 case X86ISD::SHUFPD:
2916 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002917 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002918 return DAG.getNode(Opc, dl, VT, V1, V2,
2919 DAG.getConstant(TargetMask, MVT::i8));
2920 }
2921 return SDValue();
2922}
2923
2924static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2925 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2926 switch(Opc) {
2927 default: llvm_unreachable("Unknown x86 shuffle node");
2928 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002929 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002930 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002931 case X86ISD::MOVLPS:
2932 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002933 case X86ISD::MOVSS:
2934 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002935 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002936 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002937 case X86ISD::VUNPCKLPSY:
2938 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002939 case X86ISD::PUNPCKLWD:
2940 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002941 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002942 case X86ISD::PUNPCKLQDQ:
Craig Topper6347e862011-11-21 06:57:39 +00002943 case X86ISD::VPUNPCKLWDY:
Craig Topper6fa583d2011-11-21 08:26:50 +00002944 case X86ISD::VPUNPCKLBWY:
Craig Topper6347e862011-11-21 06:57:39 +00002945 case X86ISD::VPUNPCKLDQY:
2946 case X86ISD::VPUNPCKLQDQY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002947 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002948 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002949 case X86ISD::VUNPCKHPSY:
2950 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002951 case X86ISD::PUNPCKHWD:
2952 case X86ISD::PUNPCKHBW:
2953 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002954 case X86ISD::PUNPCKHQDQ:
Craig Topper6347e862011-11-21 06:57:39 +00002955 case X86ISD::VPUNPCKHWDY:
Craig Topper6fa583d2011-11-21 08:26:50 +00002956 case X86ISD::VPUNPCKHBWY:
Craig Topper6347e862011-11-21 06:57:39 +00002957 case X86ISD::VPUNPCKHDQY:
2958 case X86ISD::VPUNPCKHQDQY:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002959 return DAG.getNode(Opc, dl, VT, V1, V2);
2960 }
2961 return SDValue();
2962}
2963
Dan Gohmand858e902010-04-17 15:26:15 +00002964SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002965 MachineFunction &MF = DAG.getMachineFunction();
2966 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2967 int ReturnAddrIndex = FuncInfo->getRAIndex();
2968
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002969 if (ReturnAddrIndex == 0) {
2970 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002971 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002972 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002973 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002974 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002975 }
2976
Evan Cheng25ab6902006-09-08 06:48:29 +00002977 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002978}
2979
2980
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002981bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2982 bool hasSymbolicDisplacement) {
2983 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002984 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002985 return false;
2986
2987 // If we don't have a symbolic displacement - we don't have any extra
2988 // restrictions.
2989 if (!hasSymbolicDisplacement)
2990 return true;
2991
2992 // FIXME: Some tweaks might be needed for medium code model.
2993 if (M != CodeModel::Small && M != CodeModel::Kernel)
2994 return false;
2995
2996 // For small code model we assume that latest object is 16MB before end of 31
2997 // bits boundary. We may also accept pretty large negative constants knowing
2998 // that all objects are in the positive half of address space.
2999 if (M == CodeModel::Small && Offset < 16*1024*1024)
3000 return true;
3001
3002 // For kernel code model we know that all object resist in the negative half
3003 // of 32bits address space. We may not accept negative offsets, since they may
3004 // be just off and we may accept pretty large positive ones.
3005 if (M == CodeModel::Kernel && Offset > 0)
3006 return true;
3007
3008 return false;
3009}
3010
Evan Chengef41ff62011-06-23 17:54:54 +00003011/// isCalleePop - Determines whether the callee is required to pop its
3012/// own arguments. Callee pop is necessary to support tail calls.
3013bool X86::isCalleePop(CallingConv::ID CallingConv,
3014 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3015 if (IsVarArg)
3016 return false;
3017
3018 switch (CallingConv) {
3019 default:
3020 return false;
3021 case CallingConv::X86_StdCall:
3022 return !is64Bit;
3023 case CallingConv::X86_FastCall:
3024 return !is64Bit;
3025 case CallingConv::X86_ThisCall:
3026 return !is64Bit;
3027 case CallingConv::Fast:
3028 return TailCallOpt;
3029 case CallingConv::GHC:
3030 return TailCallOpt;
3031 }
3032}
3033
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003034/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3035/// specific condition code, returning the condition code and the LHS/RHS of the
3036/// comparison to make.
3037static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3038 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003039 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003040 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3041 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3042 // X > -1 -> X == 0, jump !sign.
3043 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003044 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003045 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
3046 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003047 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00003048 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003049 // X < 1 -> X <= 0
3050 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003051 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003052 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003053 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003054
Evan Chengd9558e02006-01-06 00:43:03 +00003055 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003056 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003057 case ISD::SETEQ: return X86::COND_E;
3058 case ISD::SETGT: return X86::COND_G;
3059 case ISD::SETGE: return X86::COND_GE;
3060 case ISD::SETLT: return X86::COND_L;
3061 case ISD::SETLE: return X86::COND_LE;
3062 case ISD::SETNE: return X86::COND_NE;
3063 case ISD::SETULT: return X86::COND_B;
3064 case ISD::SETUGT: return X86::COND_A;
3065 case ISD::SETULE: return X86::COND_BE;
3066 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003067 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003068 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003069
Chris Lattner4c78e022008-12-23 23:42:27 +00003070 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003071
Chris Lattner4c78e022008-12-23 23:42:27 +00003072 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003073 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3074 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003075 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3076 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003077 }
3078
Chris Lattner4c78e022008-12-23 23:42:27 +00003079 switch (SetCCOpcode) {
3080 default: break;
3081 case ISD::SETOLT:
3082 case ISD::SETOLE:
3083 case ISD::SETUGT:
3084 case ISD::SETUGE:
3085 std::swap(LHS, RHS);
3086 break;
3087 }
3088
3089 // On a floating point condition, the flags are set as follows:
3090 // ZF PF CF op
3091 // 0 | 0 | 0 | X > Y
3092 // 0 | 0 | 1 | X < Y
3093 // 1 | 0 | 0 | X == Y
3094 // 1 | 1 | 1 | unordered
3095 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003096 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003097 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003098 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003099 case ISD::SETOLT: // flipped
3100 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003101 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003102 case ISD::SETOLE: // flipped
3103 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003104 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003105 case ISD::SETUGT: // flipped
3106 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003107 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003108 case ISD::SETUGE: // flipped
3109 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003110 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003111 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003112 case ISD::SETNE: return X86::COND_NE;
3113 case ISD::SETUO: return X86::COND_P;
3114 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003115 case ISD::SETOEQ:
3116 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003117 }
Evan Chengd9558e02006-01-06 00:43:03 +00003118}
3119
Evan Cheng4a460802006-01-11 00:33:36 +00003120/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3121/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003122/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003123static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003124 switch (X86CC) {
3125 default:
3126 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003127 case X86::COND_B:
3128 case X86::COND_BE:
3129 case X86::COND_E:
3130 case X86::COND_P:
3131 case X86::COND_A:
3132 case X86::COND_AE:
3133 case X86::COND_NE:
3134 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003135 return true;
3136 }
3137}
3138
Evan Chengeb2f9692009-10-27 19:56:55 +00003139/// isFPImmLegal - Returns true if the target can instruction select the
3140/// specified FP immediate natively. If false, the legalizer will
3141/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003142bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003143 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3144 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3145 return true;
3146 }
3147 return false;
3148}
3149
Nate Begeman9008ca62009-04-27 18:41:29 +00003150/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3151/// the specified range (L, H].
3152static bool isUndefOrInRange(int Val, int Low, int Hi) {
3153 return (Val < 0) || (Val >= Low && Val < Hi);
3154}
3155
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003156/// isUndefOrInRange - Return true if every element in Mask, begining
3157/// from position Pos and ending in Pos+Size, falls within the specified
3158/// range (L, L+Pos]. or is undef.
3159static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3160 int Pos, int Size, int Low, int Hi) {
3161 for (int i = Pos, e = Pos+Size; i != e; ++i)
3162 if (!isUndefOrInRange(Mask[i], Low, Hi))
3163 return false;
3164 return true;
3165}
3166
Nate Begeman9008ca62009-04-27 18:41:29 +00003167/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3168/// specified value.
3169static bool isUndefOrEqual(int Val, int CmpVal) {
3170 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003171 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003172 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003173}
3174
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003175/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3176/// from position Pos and ending in Pos+Size, falls within the specified
3177/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003178static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3179 int Pos, int Size, int Low) {
3180 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3181 if (!isUndefOrEqual(Mask[i], Low))
3182 return false;
3183 return true;
3184}
3185
Nate Begeman9008ca62009-04-27 18:41:29 +00003186/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3187/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3188/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003189static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003190 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003191 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003192 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003193 return (Mask[0] < 2 && Mask[1] < 2);
3194 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003195}
3196
Nate Begeman9008ca62009-04-27 18:41:29 +00003197bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003198 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003199 N->getMask(M);
3200 return ::isPSHUFDMask(M, N->getValueType(0));
3201}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003202
Nate Begeman9008ca62009-04-27 18:41:29 +00003203/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3204/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003205static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003206 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003207 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003208
Nate Begeman9008ca62009-04-27 18:41:29 +00003209 // Lower quadword copied in order or undef.
3210 for (int i = 0; i != 4; ++i)
3211 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003212 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003213
Evan Cheng506d3df2006-03-29 23:07:14 +00003214 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003215 for (int i = 4; i != 8; ++i)
3216 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003217 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003218
Evan Cheng506d3df2006-03-29 23:07:14 +00003219 return true;
3220}
3221
Nate Begeman9008ca62009-04-27 18:41:29 +00003222bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003223 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003224 N->getMask(M);
3225 return ::isPSHUFHWMask(M, N->getValueType(0));
3226}
Evan Cheng506d3df2006-03-29 23:07:14 +00003227
Nate Begeman9008ca62009-04-27 18:41:29 +00003228/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3229/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003230static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003231 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003232 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003233
Rafael Espindola15684b22009-04-24 12:40:33 +00003234 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003235 for (int i = 4; i != 8; ++i)
3236 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003237 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003238
Rafael Espindola15684b22009-04-24 12:40:33 +00003239 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003240 for (int i = 0; i != 4; ++i)
3241 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003242 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003243
Rafael Espindola15684b22009-04-24 12:40:33 +00003244 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003245}
3246
Nate Begeman9008ca62009-04-27 18:41:29 +00003247bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003248 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003249 N->getMask(M);
3250 return ::isPSHUFLWMask(M, N->getValueType(0));
3251}
3252
Nate Begemana09008b2009-10-19 02:17:23 +00003253/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3254/// is suitable for input to PALIGNR.
3255static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003256 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003257 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003258 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3259 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003260
Nate Begemana09008b2009-10-19 02:17:23 +00003261 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003262 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003263 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003264
Nate Begemana09008b2009-10-19 02:17:23 +00003265 for (i = 0; i != e; ++i)
3266 if (Mask[i] >= 0)
3267 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003268
Nate Begemana09008b2009-10-19 02:17:23 +00003269 // All undef, not a palignr.
3270 if (i == e)
3271 return false;
3272
Eli Friedman63f8dde2011-07-25 21:36:45 +00003273 // Make sure we're shifting in the right direction.
3274 if (Mask[i] <= i)
3275 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003276
3277 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003278
Nate Begemana09008b2009-10-19 02:17:23 +00003279 // Check the rest of the elements to see if they are consecutive.
3280 for (++i; i != e; ++i) {
3281 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003282 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003283 return false;
3284 }
3285 return true;
3286}
3287
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003288/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3289/// specifies a shuffle of elements that is suitable for input to 256-bit
3290/// VSHUFPSY.
3291static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3292 const X86Subtarget *Subtarget) {
3293 int NumElems = VT.getVectorNumElements();
3294
3295 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3296 return false;
3297
3298 if (NumElems != 8)
3299 return false;
3300
3301 // VSHUFPSY divides the resulting vector into 4 chunks.
3302 // The sources are also splitted into 4 chunks, and each destination
3303 // chunk must come from a different source chunk.
3304 //
3305 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3306 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3307 //
3308 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3309 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3310 //
3311 int QuarterSize = NumElems/4;
3312 int HalfSize = QuarterSize*2;
3313 for (int i = 0; i < QuarterSize; ++i)
3314 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3315 return false;
3316 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3317 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3318 return false;
3319
3320 // The mask of the second half must be the same as the first but with
3321 // the appropriate offsets. This works in the same way as VPERMILPS
3322 // works with masks.
3323 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3324 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3325 return false;
3326 int FstHalfIdx = i-HalfSize;
3327 if (Mask[FstHalfIdx] < 0)
3328 continue;
3329 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3330 return false;
3331 }
3332 for (int i = QuarterSize*3; i < NumElems; ++i) {
3333 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3334 return false;
3335 int FstHalfIdx = i-HalfSize;
3336 if (Mask[FstHalfIdx] < 0)
3337 continue;
3338 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3339 return false;
3340
3341 }
3342
3343 return true;
3344}
3345
3346/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3347/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3348static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3349 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3350 EVT VT = SVOp->getValueType(0);
3351 int NumElems = VT.getVectorNumElements();
3352
3353 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3354 "Only supports v8i32 and v8f32 types");
3355
3356 int HalfSize = NumElems/2;
3357 unsigned Mask = 0;
3358 for (int i = 0; i != NumElems ; ++i) {
3359 if (SVOp->getMaskElt(i) < 0)
3360 continue;
3361 // The mask of the first half must be equal to the second one.
3362 unsigned Shamt = (i%HalfSize)*2;
3363 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3364 Mask |= Elt << Shamt;
3365 }
3366
3367 return Mask;
3368}
3369
3370/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3371/// specifies a shuffle of elements that is suitable for input to 256-bit
3372/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3373/// version and the mask of the second half isn't binded with the first
3374/// one.
3375static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3376 const X86Subtarget *Subtarget) {
3377 int NumElems = VT.getVectorNumElements();
3378
3379 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3380 return false;
3381
3382 if (NumElems != 4)
3383 return false;
3384
3385 // VSHUFPSY divides the resulting vector into 4 chunks.
3386 // The sources are also splitted into 4 chunks, and each destination
3387 // chunk must come from a different source chunk.
3388 //
3389 // SRC1 => X3 X2 X1 X0
3390 // SRC2 => Y3 Y2 Y1 Y0
3391 //
3392 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3393 //
3394 int QuarterSize = NumElems/4;
3395 int HalfSize = QuarterSize*2;
3396 for (int i = 0; i < QuarterSize; ++i)
3397 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3398 return false;
3399 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3400 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3401 return false;
3402 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3403 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3404 return false;
3405 for (int i = QuarterSize*3; i < NumElems; ++i)
3406 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3407 return false;
3408
3409 return true;
3410}
3411
3412/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3413/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3414static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3415 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3416 EVT VT = SVOp->getValueType(0);
3417 int NumElems = VT.getVectorNumElements();
3418
3419 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3420 "Only supports v4i64 and v4f64 types");
3421
3422 int HalfSize = NumElems/2;
3423 unsigned Mask = 0;
3424 for (int i = 0; i != NumElems ; ++i) {
3425 if (SVOp->getMaskElt(i) < 0)
3426 continue;
3427 int Elt = SVOp->getMaskElt(i) % HalfSize;
3428 Mask |= Elt << i;
3429 }
3430
3431 return Mask;
3432}
3433
Evan Cheng14aed5e2006-03-24 01:18:28 +00003434/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003435/// specifies a shuffle of elements that is suitable for input to 128-bit
3436/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003437static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003438 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003439
3440 if (VT.getSizeInBits() != 128)
3441 return false;
3442
Nate Begeman9008ca62009-04-27 18:41:29 +00003443 if (NumElems != 2 && NumElems != 4)
3444 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003445
Nate Begeman9008ca62009-04-27 18:41:29 +00003446 int Half = NumElems / 2;
3447 for (int i = 0; i < Half; ++i)
3448 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003449 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003450 for (int i = Half; i < NumElems; ++i)
3451 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003452 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003453
Evan Cheng14aed5e2006-03-24 01:18:28 +00003454 return true;
3455}
3456
Nate Begeman9008ca62009-04-27 18:41:29 +00003457bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3458 SmallVector<int, 8> M;
3459 N->getMask(M);
3460 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003461}
3462
Evan Cheng213d2cf2007-05-17 18:45:50 +00003463/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003464/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3465/// half elements to come from vector 1 (which would equal the dest.) and
3466/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003467static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003468 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003469
3470 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003471 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003472
Nate Begeman9008ca62009-04-27 18:41:29 +00003473 int Half = NumElems / 2;
3474 for (int i = 0; i < Half; ++i)
3475 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003476 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003477 for (int i = Half; i < NumElems; ++i)
3478 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003479 return false;
3480 return true;
3481}
3482
Nate Begeman9008ca62009-04-27 18:41:29 +00003483static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3484 SmallVector<int, 8> M;
3485 N->getMask(M);
3486 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003487}
3488
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003489/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3490/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003491bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003492 EVT VT = N->getValueType(0);
3493 unsigned NumElems = VT.getVectorNumElements();
3494
3495 if (VT.getSizeInBits() != 128)
3496 return false;
3497
3498 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003499 return false;
3500
Evan Cheng2064a2b2006-03-28 06:50:32 +00003501 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003502 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3503 isUndefOrEqual(N->getMaskElt(1), 7) &&
3504 isUndefOrEqual(N->getMaskElt(2), 2) &&
3505 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003506}
3507
Nate Begeman0b10b912009-11-07 23:17:15 +00003508/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3509/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3510/// <2, 3, 2, 3>
3511bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003512 EVT VT = N->getValueType(0);
3513 unsigned NumElems = VT.getVectorNumElements();
3514
3515 if (VT.getSizeInBits() != 128)
3516 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003517
Nate Begeman0b10b912009-11-07 23:17:15 +00003518 if (NumElems != 4)
3519 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003520
Nate Begeman0b10b912009-11-07 23:17:15 +00003521 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003522 isUndefOrEqual(N->getMaskElt(1), 3) &&
3523 isUndefOrEqual(N->getMaskElt(2), 2) &&
3524 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003525}
3526
Evan Cheng5ced1d82006-04-06 23:23:56 +00003527/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3528/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003529bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3530 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003531
Evan Cheng5ced1d82006-04-06 23:23:56 +00003532 if (NumElems != 2 && NumElems != 4)
3533 return false;
3534
Evan Chengc5cdff22006-04-07 21:53:05 +00003535 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003536 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003537 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003538
Evan Chengc5cdff22006-04-07 21:53:05 +00003539 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003540 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003541 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003542
3543 return true;
3544}
3545
Nate Begeman0b10b912009-11-07 23:17:15 +00003546/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3547/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3548bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003549 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003550
David Greenea20244d2011-03-02 17:23:43 +00003551 if ((NumElems != 2 && NumElems != 4)
3552 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003553 return false;
3554
Evan Chengc5cdff22006-04-07 21:53:05 +00003555 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003556 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003557 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003558
Nate Begeman9008ca62009-04-27 18:41:29 +00003559 for (unsigned i = 0; i < NumElems/2; ++i)
3560 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003561 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003562
3563 return true;
3564}
3565
Evan Cheng0038e592006-03-28 00:39:58 +00003566/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3567/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003568static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003569 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003570 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003571
3572 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3573 "Unsupported vector type for unpckh");
3574
Craig Topper6347e862011-11-21 06:57:39 +00003575 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003576 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003577 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003578
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003579 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3580 // independently on 128-bit lanes.
3581 unsigned NumLanes = VT.getSizeInBits()/128;
3582 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003583
3584 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003585 unsigned End = NumLaneElts;
3586 for (unsigned s = 0; s < NumLanes; ++s) {
3587 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003588 i != End;
3589 i += 2, ++j) {
3590 int BitI = Mask[i];
3591 int BitI1 = Mask[i+1];
3592 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003593 return false;
David Greenea20244d2011-03-02 17:23:43 +00003594 if (V2IsSplat) {
3595 if (!isUndefOrEqual(BitI1, NumElts))
3596 return false;
3597 } else {
3598 if (!isUndefOrEqual(BitI1, j + NumElts))
3599 return false;
3600 }
Evan Cheng39623da2006-04-20 08:58:49 +00003601 }
David Greenea20244d2011-03-02 17:23:43 +00003602 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003603 Start += NumLaneElts;
3604 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003605 }
David Greenea20244d2011-03-02 17:23:43 +00003606
Evan Cheng0038e592006-03-28 00:39:58 +00003607 return true;
3608}
3609
Craig Topper6347e862011-11-21 06:57:39 +00003610bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003611 SmallVector<int, 8> M;
3612 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003613 return ::isUNPCKLMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003614}
3615
Evan Cheng4fcb9222006-03-28 02:43:26 +00003616/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3617/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003618static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003619 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003620 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003621
3622 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3623 "Unsupported vector type for unpckh");
3624
Craig Topper6347e862011-11-21 06:57:39 +00003625 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003626 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003627 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003628
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003629 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3630 // independently on 128-bit lanes.
3631 unsigned NumLanes = VT.getSizeInBits()/128;
3632 unsigned NumLaneElts = NumElts/NumLanes;
3633
3634 unsigned Start = 0;
3635 unsigned End = NumLaneElts;
3636 for (unsigned l = 0; l != NumLanes; ++l) {
3637 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3638 i != End; i += 2, ++j) {
3639 int BitI = Mask[i];
3640 int BitI1 = Mask[i+1];
3641 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003642 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003643 if (V2IsSplat) {
3644 if (isUndefOrEqual(BitI1, NumElts))
3645 return false;
3646 } else {
3647 if (!isUndefOrEqual(BitI1, j+NumElts))
3648 return false;
3649 }
Evan Cheng39623da2006-04-20 08:58:49 +00003650 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003651 // Process the next 128 bits.
3652 Start += NumLaneElts;
3653 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003654 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003655 return true;
3656}
3657
Craig Topper6347e862011-11-21 06:57:39 +00003658bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003659 SmallVector<int, 8> M;
3660 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003661 return ::isUNPCKHMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003662}
3663
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003664/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3665/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3666/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003667static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003668 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003669 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003670 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003671
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003672 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3673 // FIXME: Need a better way to get rid of this, there's no latency difference
3674 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3675 // the former later. We should also remove the "_undef" special mask.
3676 if (NumElems == 4 && VT.getSizeInBits() == 256)
3677 return false;
3678
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003679 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3680 // independently on 128-bit lanes.
3681 unsigned NumLanes = VT.getSizeInBits() / 128;
3682 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003683
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003684 for (unsigned s = 0; s < NumLanes; ++s) {
3685 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3686 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003687 i += 2, ++j) {
3688 int BitI = Mask[i];
3689 int BitI1 = Mask[i+1];
3690
3691 if (!isUndefOrEqual(BitI, j))
3692 return false;
3693 if (!isUndefOrEqual(BitI1, j))
3694 return false;
3695 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003696 }
David Greenea20244d2011-03-02 17:23:43 +00003697
Rafael Espindola15684b22009-04-24 12:40:33 +00003698 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003699}
3700
Nate Begeman9008ca62009-04-27 18:41:29 +00003701bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3702 SmallVector<int, 8> M;
3703 N->getMask(M);
3704 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3705}
3706
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003707/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3708/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3709/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003710static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003711 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003712 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3713 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003714
Nate Begeman9008ca62009-04-27 18:41:29 +00003715 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3716 int BitI = Mask[i];
3717 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003718 if (!isUndefOrEqual(BitI, j))
3719 return false;
3720 if (!isUndefOrEqual(BitI1, j))
3721 return false;
3722 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003723 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003724}
3725
Nate Begeman9008ca62009-04-27 18:41:29 +00003726bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3727 SmallVector<int, 8> M;
3728 N->getMask(M);
3729 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3730}
3731
Evan Cheng017dcc62006-04-21 01:05:10 +00003732/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3733/// specifies a shuffle of elements that is suitable for input to MOVSS,
3734/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003735static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003736 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003737 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003738
3739 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003740
Nate Begeman9008ca62009-04-27 18:41:29 +00003741 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003742 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003743
Nate Begeman9008ca62009-04-27 18:41:29 +00003744 for (int i = 1; i < NumElts; ++i)
3745 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003746 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003747
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003748 return true;
3749}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003750
Nate Begeman9008ca62009-04-27 18:41:29 +00003751bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3752 SmallVector<int, 8> M;
3753 N->getMask(M);
3754 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003755}
3756
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003757/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3758/// as permutations between 128-bit chunks or halves. As an example: this
3759/// shuffle bellow:
3760/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3761/// The first half comes from the second half of V1 and the second half from the
3762/// the second half of V2.
3763static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3764 const X86Subtarget *Subtarget) {
3765 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3766 return false;
3767
3768 // The shuffle result is divided into half A and half B. In total the two
3769 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3770 // B must come from C, D, E or F.
3771 int HalfSize = VT.getVectorNumElements()/2;
3772 bool MatchA = false, MatchB = false;
3773
3774 // Check if A comes from one of C, D, E, F.
3775 for (int Half = 0; Half < 4; ++Half) {
3776 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3777 MatchA = true;
3778 break;
3779 }
3780 }
3781
3782 // Check if B comes from one of C, D, E, F.
3783 for (int Half = 0; Half < 4; ++Half) {
3784 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3785 MatchB = true;
3786 break;
3787 }
3788 }
3789
3790 return MatchA && MatchB;
3791}
3792
3793/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3794/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3795static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3796 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3797 EVT VT = SVOp->getValueType(0);
3798
3799 int HalfSize = VT.getVectorNumElements()/2;
3800
3801 int FstHalf = 0, SndHalf = 0;
3802 for (int i = 0; i < HalfSize; ++i) {
3803 if (SVOp->getMaskElt(i) > 0) {
3804 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3805 break;
3806 }
3807 }
3808 for (int i = HalfSize; i < HalfSize*2; ++i) {
3809 if (SVOp->getMaskElt(i) > 0) {
3810 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3811 break;
3812 }
3813 }
3814
3815 return (FstHalf | (SndHalf << 4));
3816}
3817
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003818/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3819/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3820/// Note that VPERMIL mask matching is different depending whether theunderlying
3821/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3822/// to the same elements of the low, but to the higher half of the source.
3823/// In VPERMILPD the two lanes could be shuffled independently of each other
3824/// with the same restriction that lanes can't be crossed.
3825static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3826 const X86Subtarget *Subtarget) {
3827 int NumElts = VT.getVectorNumElements();
3828 int NumLanes = VT.getSizeInBits()/128;
3829
3830 if (!Subtarget->hasAVX())
3831 return false;
3832
Eli Friedmandca62d52011-10-10 22:28:47 +00003833 // Only match 256-bit with 64-bit types
3834 if (VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003835 return false;
3836
3837 // The mask on the high lane is independent of the low. Both can match
3838 // any element in inside its own lane, but can't cross.
3839 int LaneSize = NumElts/NumLanes;
3840 for (int l = 0; l < NumLanes; ++l)
3841 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3842 int LaneStart = l*LaneSize;
3843 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3844 return false;
3845 }
3846
3847 return true;
3848}
3849
3850/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3851/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3852/// Note that VPERMIL mask matching is different depending whether theunderlying
3853/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3854/// to the same elements of the low, but to the higher half of the source.
3855/// In VPERMILPD the two lanes could be shuffled independently of each other
3856/// with the same restriction that lanes can't be crossed.
3857static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3858 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003859 unsigned NumElts = VT.getVectorNumElements();
3860 unsigned NumLanes = VT.getSizeInBits()/128;
3861
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003862 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003863 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003864
Eli Friedmandca62d52011-10-10 22:28:47 +00003865 // Only match 256-bit with 32-bit types
3866 if (VT.getSizeInBits() != 256 || NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003867 return false;
3868
3869 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003870 // they can differ if any of the corresponding index in a lane is undef
3871 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003872 int LaneSize = NumElts/NumLanes;
3873 for (int i = 0; i < LaneSize; ++i) {
3874 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003875 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3876 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3877
3878 if (!HighValid || !LowValid)
3879 return false;
3880 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003881 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003882 if (Mask[HighElt]-Mask[i] != LaneSize)
3883 return false;
3884 }
3885
3886 return true;
3887}
3888
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003889/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3890/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3891static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003892 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3893 EVT VT = SVOp->getValueType(0);
3894
3895 int NumElts = VT.getVectorNumElements();
3896 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003897 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003898
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003899 // Although the mask is equal for both lanes do it twice to get the cases
3900 // where a mask will match because the same mask element is undef on the
3901 // first half but valid on the second. This would get pathological cases
3902 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003903 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003904 for (int l = 0; l < NumLanes; ++l) {
3905 for (int i = 0; i < LaneSize; ++i) {
3906 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3907 if (MaskElt < 0)
3908 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003909 if (MaskElt >= LaneSize)
3910 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003911 Mask |= MaskElt << (i*2);
3912 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003913 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003914
3915 return Mask;
3916}
3917
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003918/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3919/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3920static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3921 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3922 EVT VT = SVOp->getValueType(0);
3923
3924 int NumElts = VT.getVectorNumElements();
3925 int NumLanes = VT.getSizeInBits()/128;
3926
3927 unsigned Mask = 0;
3928 int LaneSize = NumElts/NumLanes;
3929 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003930 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3931 int MaskElt = SVOp->getMaskElt(i);
3932 if (MaskElt < 0)
3933 continue;
3934 Mask |= (MaskElt-l*LaneSize) << i;
3935 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003936
3937 return Mask;
3938}
3939
Evan Cheng017dcc62006-04-21 01:05:10 +00003940/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3941/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003942/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003943static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003944 bool V2IsSplat = false, bool V2IsUndef = false) {
3945 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003946 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003947 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003948
Nate Begeman9008ca62009-04-27 18:41:29 +00003949 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003950 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003951
Nate Begeman9008ca62009-04-27 18:41:29 +00003952 for (int i = 1; i < NumOps; ++i)
3953 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3954 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3955 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003956 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003957
Evan Cheng39623da2006-04-20 08:58:49 +00003958 return true;
3959}
3960
Nate Begeman9008ca62009-04-27 18:41:29 +00003961static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003962 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003963 SmallVector<int, 8> M;
3964 N->getMask(M);
3965 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003966}
3967
Evan Chengd9539472006-04-14 21:59:03 +00003968/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3969/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003970/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3971bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3972 const X86Subtarget *Subtarget) {
3973 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003974 return false;
3975
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003976 // The second vector must be undef
3977 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3978 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003979
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003980 EVT VT = N->getValueType(0);
3981 unsigned NumElems = VT.getVectorNumElements();
3982
3983 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3984 (VT.getSizeInBits() == 256 && NumElems != 8))
3985 return false;
3986
3987 // "i+1" is the value the indexed mask element must have
3988 for (unsigned i = 0; i < NumElems; i += 2)
3989 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3990 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003991 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003992
3993 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003994}
3995
3996/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3997/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003998/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3999bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
4000 const X86Subtarget *Subtarget) {
4001 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00004002 return false;
4003
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004004 // The second vector must be undef
4005 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
4006 return false;
4007
4008 EVT VT = N->getValueType(0);
4009 unsigned NumElems = VT.getVectorNumElements();
4010
4011 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
4012 (VT.getSizeInBits() == 256 && NumElems != 8))
4013 return false;
4014
4015 // "i" is the value the indexed mask element must have
4016 for (unsigned i = 0; i < NumElems; i += 2)
4017 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
4018 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00004019 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00004020
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004021 return true;
Evan Chengd9539472006-04-14 21:59:03 +00004022}
4023
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004024/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
4025/// specifies a shuffle of elements that is suitable for input to 256-bit
4026/// version of MOVDDUP.
4027static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
4028 const X86Subtarget *Subtarget) {
4029 EVT VT = N->getValueType(0);
4030 int NumElts = VT.getVectorNumElements();
4031 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
4032
4033 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
4034 !V2IsUndef || NumElts != 4)
4035 return false;
4036
4037 for (int i = 0; i != NumElts/2; ++i)
4038 if (!isUndefOrEqual(N->getMaskElt(i), 0))
4039 return false;
4040 for (int i = NumElts/2; i != NumElts; ++i)
4041 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
4042 return false;
4043 return true;
4044}
4045
Evan Cheng0b457f02008-09-25 20:50:48 +00004046/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004047/// specifies a shuffle of elements that is suitable for input to 128-bit
4048/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00004049bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004050 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004051
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004052 if (VT.getSizeInBits() != 128)
4053 return false;
4054
4055 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004056 for (int i = 0; i < e; ++i)
4057 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004058 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004059 for (int i = 0; i < e; ++i)
4060 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004061 return false;
4062 return true;
4063}
4064
David Greenec38a03e2011-02-03 15:50:00 +00004065/// isVEXTRACTF128Index - Return true if the specified
4066/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4067/// suitable for input to VEXTRACTF128.
4068bool X86::isVEXTRACTF128Index(SDNode *N) {
4069 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4070 return false;
4071
4072 // The index should be aligned on a 128-bit boundary.
4073 uint64_t Index =
4074 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4075
4076 unsigned VL = N->getValueType(0).getVectorNumElements();
4077 unsigned VBits = N->getValueType(0).getSizeInBits();
4078 unsigned ElSize = VBits / VL;
4079 bool Result = (Index * ElSize) % 128 == 0;
4080
4081 return Result;
4082}
4083
David Greeneccacdc12011-02-04 16:08:29 +00004084/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4085/// operand specifies a subvector insert that is suitable for input to
4086/// VINSERTF128.
4087bool X86::isVINSERTF128Index(SDNode *N) {
4088 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4089 return false;
4090
4091 // The index should be aligned on a 128-bit boundary.
4092 uint64_t Index =
4093 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4094
4095 unsigned VL = N->getValueType(0).getVectorNumElements();
4096 unsigned VBits = N->getValueType(0).getSizeInBits();
4097 unsigned ElSize = VBits / VL;
4098 bool Result = (Index * ElSize) % 128 == 0;
4099
4100 return Result;
4101}
4102
Evan Cheng63d33002006-03-22 08:01:21 +00004103/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004104/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004105unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004106 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4107 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4108
Evan Chengb9df0ca2006-03-22 02:53:00 +00004109 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4110 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004111 for (int i = 0; i < NumOperands; ++i) {
4112 int Val = SVOp->getMaskElt(NumOperands-i-1);
4113 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004114 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004115 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004116 if (i != NumOperands - 1)
4117 Mask <<= Shift;
4118 }
Evan Cheng63d33002006-03-22 08:01:21 +00004119 return Mask;
4120}
4121
Evan Cheng506d3df2006-03-29 23:07:14 +00004122/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004123/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004124unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004125 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004126 unsigned Mask = 0;
4127 // 8 nodes, but we only care about the last 4.
4128 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004129 int Val = SVOp->getMaskElt(i);
4130 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004131 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004132 if (i != 4)
4133 Mask <<= 2;
4134 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004135 return Mask;
4136}
4137
4138/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004139/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004140unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004141 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004142 unsigned Mask = 0;
4143 // 8 nodes, but we only care about the first 4.
4144 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004145 int Val = SVOp->getMaskElt(i);
4146 if (Val >= 0)
4147 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004148 if (i != 0)
4149 Mask <<= 2;
4150 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004151 return Mask;
4152}
4153
Nate Begemana09008b2009-10-19 02:17:23 +00004154/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4155/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4156unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4157 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4158 EVT VVT = N->getValueType(0);
4159 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4160 int Val = 0;
4161
4162 unsigned i, e;
4163 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4164 Val = SVOp->getMaskElt(i);
4165 if (Val >= 0)
4166 break;
4167 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004168 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004169 return (Val - i) * EltSize;
4170}
4171
David Greenec38a03e2011-02-03 15:50:00 +00004172/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4173/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4174/// instructions.
4175unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4176 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4177 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4178
4179 uint64_t Index =
4180 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4181
4182 EVT VecVT = N->getOperand(0).getValueType();
4183 EVT ElVT = VecVT.getVectorElementType();
4184
4185 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004186 return Index / NumElemsPerChunk;
4187}
4188
David Greeneccacdc12011-02-04 16:08:29 +00004189/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4190/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4191/// instructions.
4192unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4193 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4194 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4195
4196 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004197 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004198
4199 EVT VecVT = N->getValueType(0);
4200 EVT ElVT = VecVT.getVectorElementType();
4201
4202 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004203 return Index / NumElemsPerChunk;
4204}
4205
Evan Cheng37b73872009-07-30 08:33:02 +00004206/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4207/// constant +0.0.
4208bool X86::isZeroNode(SDValue Elt) {
4209 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004210 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004211 (isa<ConstantFPSDNode>(Elt) &&
4212 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4213}
4214
Nate Begeman9008ca62009-04-27 18:41:29 +00004215/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4216/// their permute mask.
4217static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4218 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004219 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004220 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004221 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004222
Nate Begeman5a5ca152009-04-29 05:20:52 +00004223 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004224 int idx = SVOp->getMaskElt(i);
4225 if (idx < 0)
4226 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004227 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004228 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004229 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004230 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004231 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004232 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4233 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004234}
4235
Evan Cheng779ccea2007-12-07 21:30:01 +00004236/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4237/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004238static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004239 unsigned NumElems = VT.getVectorNumElements();
4240 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004241 int idx = Mask[i];
4242 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004243 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004244 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004245 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004246 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004247 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004248 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004249}
4250
Evan Cheng533a0aa2006-04-19 20:35:22 +00004251/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4252/// match movhlps. The lower half elements should come from upper half of
4253/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004254/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004255static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004256 EVT VT = Op->getValueType(0);
4257 if (VT.getSizeInBits() != 128)
4258 return false;
4259 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004260 return false;
4261 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004262 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004263 return false;
4264 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004265 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004266 return false;
4267 return true;
4268}
4269
Evan Cheng5ced1d82006-04-06 23:23:56 +00004270/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004271/// is promoted to a vector. It also returns the LoadSDNode by reference if
4272/// required.
4273static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004274 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4275 return false;
4276 N = N->getOperand(0).getNode();
4277 if (!ISD::isNON_EXTLoad(N))
4278 return false;
4279 if (LD)
4280 *LD = cast<LoadSDNode>(N);
4281 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004282}
4283
Dan Gohman65fd6562011-11-03 21:49:52 +00004284// Test whether the given value is a vector value which will be legalized
4285// into a load.
4286static bool WillBeConstantPoolLoad(SDNode *N) {
4287 if (N->getOpcode() != ISD::BUILD_VECTOR)
4288 return false;
4289
4290 // Check for any non-constant elements.
4291 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4292 switch (N->getOperand(i).getNode()->getOpcode()) {
4293 case ISD::UNDEF:
4294 case ISD::ConstantFP:
4295 case ISD::Constant:
4296 break;
4297 default:
4298 return false;
4299 }
4300
4301 // Vectors of all-zeros and all-ones are materialized with special
4302 // instructions rather than being loaded.
4303 return !ISD::isBuildVectorAllZeros(N) &&
4304 !ISD::isBuildVectorAllOnes(N);
4305}
4306
Evan Cheng533a0aa2006-04-19 20:35:22 +00004307/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4308/// match movlp{s|d}. The lower half elements should come from lower half of
4309/// V1 (and in order), and the upper half elements should come from the upper
4310/// half of V2 (and in order). And since V1 will become the source of the
4311/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004312static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4313 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004314 EVT VT = Op->getValueType(0);
4315 if (VT.getSizeInBits() != 128)
4316 return false;
4317
Evan Cheng466685d2006-10-09 20:57:25 +00004318 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004319 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004320 // Is V2 is a vector load, don't do this transformation. We will try to use
4321 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004322 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004323 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004324
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004325 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004326
Evan Cheng533a0aa2006-04-19 20:35:22 +00004327 if (NumElems != 2 && NumElems != 4)
4328 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004329 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004330 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004331 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004332 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004333 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004334 return false;
4335 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004336}
4337
Evan Cheng39623da2006-04-20 08:58:49 +00004338/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4339/// all the same.
4340static bool isSplatVector(SDNode *N) {
4341 if (N->getOpcode() != ISD::BUILD_VECTOR)
4342 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004343
Dan Gohman475871a2008-07-27 21:46:04 +00004344 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004345 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4346 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004347 return false;
4348 return true;
4349}
4350
Evan Cheng213d2cf2007-05-17 18:45:50 +00004351/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004352/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004353/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004354static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004355 SDValue V1 = N->getOperand(0);
4356 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004357 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4358 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004359 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004360 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004361 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004362 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4363 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004364 if (Opc != ISD::BUILD_VECTOR ||
4365 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004366 return false;
4367 } else if (Idx >= 0) {
4368 unsigned Opc = V1.getOpcode();
4369 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4370 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004371 if (Opc != ISD::BUILD_VECTOR ||
4372 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004373 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004374 }
4375 }
4376 return true;
4377}
4378
4379/// getZeroVector - Returns a vector of specified type with all zero elements.
4380///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004381static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004382 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004383 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004384
Dale Johannesen0488fb62010-09-30 23:57:10 +00004385 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004386 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004387 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004388 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004389 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004390 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4391 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4392 } else { // SSE1
4393 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4394 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4395 }
4396 } else if (VT.getSizeInBits() == 256) { // AVX
4397 // 256-bit logic and arithmetic instructions in AVX are
4398 // all floating-point, no support for integer ops. Default
4399 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004400 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004401 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4402 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004403 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004404 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004405}
4406
Chris Lattner8a594482007-11-25 00:24:49 +00004407/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004408/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4409/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4410/// Then bitcast to their original type, ensuring they get CSE'd.
4411static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4412 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004413 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004414 assert((VT.is128BitVector() || VT.is256BitVector())
4415 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004416
Owen Anderson825b72b2009-08-11 20:47:22 +00004417 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004418 SDValue Vec;
4419 if (VT.getSizeInBits() == 256) {
4420 if (HasAVX2) { // AVX2
4421 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4422 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4423 } else { // AVX
4424 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4425 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4426 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4427 Vec = Insert128BitVector(InsV, Vec,
4428 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4429 }
4430 } else {
4431 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004432 }
4433
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004434 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004435}
4436
Evan Cheng39623da2006-04-20 08:58:49 +00004437/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4438/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004439static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004440 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004441 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004442
Evan Cheng39623da2006-04-20 08:58:49 +00004443 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004444 SmallVector<int, 8> MaskVec;
4445 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004446
Nate Begeman5a5ca152009-04-29 05:20:52 +00004447 for (unsigned i = 0; i != NumElems; ++i) {
4448 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004449 MaskVec[i] = NumElems;
4450 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004451 }
Evan Cheng39623da2006-04-20 08:58:49 +00004452 }
Evan Cheng39623da2006-04-20 08:58:49 +00004453 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004454 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4455 SVOp->getOperand(1), &MaskVec[0]);
4456 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004457}
4458
Evan Cheng017dcc62006-04-21 01:05:10 +00004459/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4460/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004461static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004462 SDValue V2) {
4463 unsigned NumElems = VT.getVectorNumElements();
4464 SmallVector<int, 8> Mask;
4465 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004466 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004467 Mask.push_back(i);
4468 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004469}
4470
Nate Begeman9008ca62009-04-27 18:41:29 +00004471/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004472static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004473 SDValue V2) {
4474 unsigned NumElems = VT.getVectorNumElements();
4475 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004476 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004477 Mask.push_back(i);
4478 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004479 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004480 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004481}
4482
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004483/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004484static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004485 SDValue V2) {
4486 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004487 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004488 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004489 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004490 Mask.push_back(i + Half);
4491 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004492 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004493 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004494}
4495
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004496// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004497// a generic shuffle instruction because the target has no such instructions.
4498// Generate shuffles which repeat i16 and i8 several times until they can be
4499// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004500static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004501 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004502 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004503 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004504
Nate Begeman9008ca62009-04-27 18:41:29 +00004505 while (NumElems > 4) {
4506 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004507 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004508 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004509 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004510 EltNo -= NumElems/2;
4511 }
4512 NumElems >>= 1;
4513 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004514 return V;
4515}
Eric Christopherfd179292009-08-27 18:07:15 +00004516
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004517/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4518static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4519 EVT VT = V.getValueType();
4520 DebugLoc dl = V.getDebugLoc();
4521 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4522 && "Vector size not supported");
4523
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004524 if (VT.getSizeInBits() == 128) {
4525 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004526 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004527 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4528 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004529 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004530 // To use VPERMILPS to splat scalars, the second half of indicies must
4531 // refer to the higher part, which is a duplication of the lower one,
4532 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004533 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4534 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004535
4536 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4537 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4538 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004539 }
4540
4541 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4542}
4543
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004544/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004545static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4546 EVT SrcVT = SV->getValueType(0);
4547 SDValue V1 = SV->getOperand(0);
4548 DebugLoc dl = SV->getDebugLoc();
4549
4550 int EltNo = SV->getSplatIndex();
4551 int NumElems = SrcVT.getVectorNumElements();
4552 unsigned Size = SrcVT.getSizeInBits();
4553
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004554 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4555 "Unknown how to promote splat for type");
4556
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004557 // Extract the 128-bit part containing the splat element and update
4558 // the splat element index when it refers to the higher register.
4559 if (Size == 256) {
4560 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4561 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4562 if (Idx > 0)
4563 EltNo -= NumElems/2;
4564 }
4565
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004566 // All i16 and i8 vector types can't be used directly by a generic shuffle
4567 // instruction because the target has no such instruction. Generate shuffles
4568 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004569 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004570 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004571 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004572 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004573
4574 // Recreate the 256-bit vector and place the same 128-bit vector
4575 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004576 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004577 if (Size == 256) {
4578 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4579 DAG.getConstant(0, MVT::i32), DAG, dl);
4580 V1 = Insert128BitVector(InsV, V1,
4581 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4582 }
4583
4584 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004585}
4586
Evan Chengba05f722006-04-21 23:03:30 +00004587/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004588/// vector of zero or undef vector. This produces a shuffle where the low
4589/// element of V2 is swizzled into the zero/undef vector, landing at element
4590/// 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 +00004591static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004592 bool isZero, bool HasXMMInt,
4593 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004594 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004595 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004596 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004597 unsigned NumElems = VT.getVectorNumElements();
4598 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004599 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004600 // If this is the insertion idx, put the low elt of V2 here.
4601 MaskVec.push_back(i == Idx ? NumElems : i);
4602 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004603}
4604
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004605/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4606/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004607static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4608 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004609 if (Depth == 6)
4610 return SDValue(); // Limit search depth.
4611
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004612 SDValue V = SDValue(N, 0);
4613 EVT VT = V.getValueType();
4614 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004615
4616 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4617 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4618 Index = SV->getMaskElt(Index);
4619
4620 if (Index < 0)
4621 return DAG.getUNDEF(VT.getVectorElementType());
4622
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004623 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004624 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004625 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004626 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004627
4628 // Recurse into target specific vector shuffles to find scalars.
4629 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004630 int NumElems = VT.getVectorNumElements();
4631 SmallVector<unsigned, 16> ShuffleMask;
4632 SDValue ImmN;
4633
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004634 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004635 case X86ISD::SHUFPS:
4636 case X86ISD::SHUFPD:
4637 ImmN = N->getOperand(N->getNumOperands()-1);
4638 DecodeSHUFPSMask(NumElems,
4639 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4640 ShuffleMask);
4641 break;
4642 case X86ISD::PUNPCKHBW:
4643 case X86ISD::PUNPCKHWD:
4644 case X86ISD::PUNPCKHDQ:
4645 case X86ISD::PUNPCKHQDQ:
Craig Topper6fa583d2011-11-21 08:26:50 +00004646 case X86ISD::VPUNPCKHBWY:
Craig Topper6347e862011-11-21 06:57:39 +00004647 case X86ISD::VPUNPCKHWDY:
4648 case X86ISD::VPUNPCKHDQY:
4649 case X86ISD::VPUNPCKHQDQY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004650 DecodePUNPCKHMask(NumElems, ShuffleMask);
4651 break;
4652 case X86ISD::UNPCKHPS:
4653 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004654 case X86ISD::VUNPCKHPSY:
4655 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004656 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4657 break;
4658 case X86ISD::PUNPCKLBW:
4659 case X86ISD::PUNPCKLWD:
4660 case X86ISD::PUNPCKLDQ:
4661 case X86ISD::PUNPCKLQDQ:
Craig Topper6fa583d2011-11-21 08:26:50 +00004662 case X86ISD::VPUNPCKLBWY:
Craig Topper6347e862011-11-21 06:57:39 +00004663 case X86ISD::VPUNPCKLWDY:
4664 case X86ISD::VPUNPCKLDQY:
4665 case X86ISD::VPUNPCKLQDQY:
David Greenec4db4e52011-02-28 19:06:56 +00004666 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004667 break;
4668 case X86ISD::UNPCKLPS:
4669 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004670 case X86ISD::VUNPCKLPSY:
4671 case X86ISD::VUNPCKLPDY:
4672 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004673 break;
4674 case X86ISD::MOVHLPS:
4675 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4676 break;
4677 case X86ISD::MOVLHPS:
4678 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4679 break;
4680 case X86ISD::PSHUFD:
4681 ImmN = N->getOperand(N->getNumOperands()-1);
4682 DecodePSHUFMask(NumElems,
4683 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4684 ShuffleMask);
4685 break;
4686 case X86ISD::PSHUFHW:
4687 ImmN = N->getOperand(N->getNumOperands()-1);
4688 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4689 ShuffleMask);
4690 break;
4691 case X86ISD::PSHUFLW:
4692 ImmN = N->getOperand(N->getNumOperands()-1);
4693 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4694 ShuffleMask);
4695 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004696 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004697 case X86ISD::MOVSD: {
4698 // The index 0 always comes from the first element of the second source,
4699 // this is why MOVSS and MOVSD are used in the first place. The other
4700 // elements come from the other positions of the first source vector.
4701 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004702 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4703 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004704 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004705 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004706 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004707 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004708 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004709 break;
4710 case X86ISD::VPERMILPSY:
4711 ImmN = N->getOperand(N->getNumOperands()-1);
4712 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4713 ShuffleMask);
4714 break;
4715 case X86ISD::VPERMILPD:
4716 ImmN = N->getOperand(N->getNumOperands()-1);
4717 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4718 ShuffleMask);
4719 break;
4720 case X86ISD::VPERMILPDY:
4721 ImmN = N->getOperand(N->getNumOperands()-1);
4722 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4723 ShuffleMask);
4724 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004725 case X86ISD::VPERM2F128:
4726 ImmN = N->getOperand(N->getNumOperands()-1);
4727 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4728 ShuffleMask);
4729 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004730 case X86ISD::MOVDDUP:
4731 case X86ISD::MOVLHPD:
4732 case X86ISD::MOVLPD:
4733 case X86ISD::MOVLPS:
4734 case X86ISD::MOVSHDUP:
4735 case X86ISD::MOVSLDUP:
4736 case X86ISD::PALIGN:
4737 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004738 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004739 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004740 return SDValue();
4741 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004742
4743 Index = ShuffleMask[Index];
4744 if (Index < 0)
4745 return DAG.getUNDEF(VT.getVectorElementType());
4746
4747 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4748 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4749 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004750 }
4751
4752 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004753 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004754 V = V.getOperand(0);
4755 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004756 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004757
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004758 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004759 return SDValue();
4760 }
4761
4762 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4763 return (Index == 0) ? V.getOperand(0)
4764 : DAG.getUNDEF(VT.getVectorElementType());
4765
4766 if (V.getOpcode() == ISD::BUILD_VECTOR)
4767 return V.getOperand(Index);
4768
4769 return SDValue();
4770}
4771
4772/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4773/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004774/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004775static
4776unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4777 bool ZerosFromLeft, SelectionDAG &DAG) {
4778 int i = 0;
4779
4780 while (i < NumElems) {
4781 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004782 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004783 if (!(Elt.getNode() &&
4784 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4785 break;
4786 ++i;
4787 }
4788
4789 return i;
4790}
4791
4792/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4793/// MaskE correspond consecutively to elements from one of the vector operands,
4794/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4795static
4796bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4797 int OpIdx, int NumElems, unsigned &OpNum) {
4798 bool SeenV1 = false;
4799 bool SeenV2 = false;
4800
4801 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4802 int Idx = SVOp->getMaskElt(i);
4803 // Ignore undef indicies
4804 if (Idx < 0)
4805 continue;
4806
4807 if (Idx < NumElems)
4808 SeenV1 = true;
4809 else
4810 SeenV2 = true;
4811
4812 // Only accept consecutive elements from the same vector
4813 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4814 return false;
4815 }
4816
4817 OpNum = SeenV1 ? 0 : 1;
4818 return true;
4819}
4820
4821/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4822/// logical left shift of a vector.
4823static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4824 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4825 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4826 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4827 false /* check zeros from right */, DAG);
4828 unsigned OpSrc;
4829
4830 if (!NumZeros)
4831 return false;
4832
4833 // Considering the elements in the mask that are not consecutive zeros,
4834 // check if they consecutively come from only one of the source vectors.
4835 //
4836 // V1 = {X, A, B, C} 0
4837 // \ \ \ /
4838 // vector_shuffle V1, V2 <1, 2, 3, X>
4839 //
4840 if (!isShuffleMaskConsecutive(SVOp,
4841 0, // Mask Start Index
4842 NumElems-NumZeros-1, // Mask End Index
4843 NumZeros, // Where to start looking in the src vector
4844 NumElems, // Number of elements in vector
4845 OpSrc)) // Which source operand ?
4846 return false;
4847
4848 isLeft = false;
4849 ShAmt = NumZeros;
4850 ShVal = SVOp->getOperand(OpSrc);
4851 return true;
4852}
4853
4854/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4855/// logical left shift of a vector.
4856static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4857 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4858 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4859 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4860 true /* check zeros from left */, DAG);
4861 unsigned OpSrc;
4862
4863 if (!NumZeros)
4864 return false;
4865
4866 // Considering the elements in the mask that are not consecutive zeros,
4867 // check if they consecutively come from only one of the source vectors.
4868 //
4869 // 0 { A, B, X, X } = V2
4870 // / \ / /
4871 // vector_shuffle V1, V2 <X, X, 4, 5>
4872 //
4873 if (!isShuffleMaskConsecutive(SVOp,
4874 NumZeros, // Mask Start Index
4875 NumElems-1, // Mask End Index
4876 0, // Where to start looking in the src vector
4877 NumElems, // Number of elements in vector
4878 OpSrc)) // Which source operand ?
4879 return false;
4880
4881 isLeft = true;
4882 ShAmt = NumZeros;
4883 ShVal = SVOp->getOperand(OpSrc);
4884 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004885}
4886
4887/// isVectorShift - Returns true if the shuffle can be implemented as a
4888/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004889static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004890 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004891 // Although the logic below support any bitwidth size, there are no
4892 // shift instructions which handle more than 128-bit vectors.
4893 if (SVOp->getValueType(0).getSizeInBits() > 128)
4894 return false;
4895
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004896 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4897 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4898 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004899
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004900 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004901}
4902
Evan Chengc78d3b42006-04-24 18:01:45 +00004903/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4904///
Dan Gohman475871a2008-07-27 21:46:04 +00004905static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004906 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004907 SelectionDAG &DAG,
4908 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004909 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004910 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004911
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004912 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004913 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004914 bool First = true;
4915 for (unsigned i = 0; i < 16; ++i) {
4916 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4917 if (ThisIsNonZero && First) {
4918 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004919 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004920 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004921 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004922 First = false;
4923 }
4924
4925 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004926 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004927 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4928 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004929 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004930 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004931 }
4932 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004933 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4934 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4935 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004936 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004937 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004938 } else
4939 ThisElt = LastElt;
4940
Gabor Greifba36cb52008-08-28 21:40:38 +00004941 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004942 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004943 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004944 }
4945 }
4946
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004947 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004948}
4949
Bill Wendlinga348c562007-03-22 18:42:45 +00004950/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004951///
Dan Gohman475871a2008-07-27 21:46:04 +00004952static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004953 unsigned NumNonZero, unsigned NumZero,
4954 SelectionDAG &DAG,
4955 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004956 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004957 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004958
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004959 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004960 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004961 bool First = true;
4962 for (unsigned i = 0; i < 8; ++i) {
4963 bool isNonZero = (NonZeros & (1 << i)) != 0;
4964 if (isNonZero) {
4965 if (First) {
4966 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004967 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004968 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004969 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004970 First = false;
4971 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004972 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004973 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004974 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004975 }
4976 }
4977
4978 return V;
4979}
4980
Evan Chengf26ffe92008-05-29 08:22:04 +00004981/// getVShift - Return a vector logical shift node.
4982///
Owen Andersone50ed302009-08-10 22:56:29 +00004983static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004984 unsigned NumBits, SelectionDAG &DAG,
4985 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004986 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004987 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004988 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004989 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4990 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004991 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004992 DAG.getConstant(NumBits,
4993 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004994}
4995
Dan Gohman475871a2008-07-27 21:46:04 +00004996SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004997X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004998 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004999
Evan Chengc3630942009-12-09 21:00:30 +00005000 // Check if the scalar load can be widened into a vector load. And if
5001 // the address is "base + cst" see if the cst can be "absorbed" into
5002 // the shuffle mask.
5003 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
5004 SDValue Ptr = LD->getBasePtr();
5005 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
5006 return SDValue();
5007 EVT PVT = LD->getValueType(0);
5008 if (PVT != MVT::i32 && PVT != MVT::f32)
5009 return SDValue();
5010
5011 int FI = -1;
5012 int64_t Offset = 0;
5013 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
5014 FI = FINode->getIndex();
5015 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00005016 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00005017 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
5018 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
5019 Offset = Ptr.getConstantOperandVal(1);
5020 Ptr = Ptr.getOperand(0);
5021 } else {
5022 return SDValue();
5023 }
5024
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005025 // FIXME: 256-bit vector instructions don't require a strict alignment,
5026 // improve this code to support it better.
5027 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00005028 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005029 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00005030 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005031 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00005032 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00005033 // Can't change the alignment. FIXME: It's possible to compute
5034 // the exact stack offset and reference FI + adjust offset instead.
5035 // If someone *really* cares about this. That's the way to implement it.
5036 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005037 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005038 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00005039 }
5040 }
5041
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005042 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00005043 // Ptr + (Offset & ~15).
5044 if (Offset < 0)
5045 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005046 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00005047 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005048 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00005049 if (StartOffset)
5050 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
5051 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
5052
5053 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005054 int NumElems = VT.getVectorNumElements();
5055
5056 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
5057 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5058 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00005059 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005060 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005061
5062 // Canonicalize it to a v4i32 or v8i32 shuffle.
5063 SmallVector<int, 8> Mask;
5064 for (int i = 0; i < NumElems; ++i)
5065 Mask.push_back(EltNo);
5066
5067 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
5068 return DAG.getNode(ISD::BITCAST, dl, NVT,
5069 DAG.getVectorShuffle(CanonVT, dl, V1,
5070 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00005071 }
5072
5073 return SDValue();
5074}
5075
Michael J. Spencerec38de22010-10-10 22:04:20 +00005076/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5077/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005078/// load which has the same value as a build_vector whose operands are 'elts'.
5079///
5080/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005081///
Nate Begeman1449f292010-03-24 22:19:06 +00005082/// FIXME: we'd also like to handle the case where the last elements are zero
5083/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5084/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005085static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005086 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005087 EVT EltVT = VT.getVectorElementType();
5088 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005089
Nate Begemanfdea31a2010-03-24 20:49:50 +00005090 LoadSDNode *LDBase = NULL;
5091 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005092
Nate Begeman1449f292010-03-24 22:19:06 +00005093 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005094 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005095 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005096 for (unsigned i = 0; i < NumElems; ++i) {
5097 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005098
Nate Begemanfdea31a2010-03-24 20:49:50 +00005099 if (!Elt.getNode() ||
5100 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5101 return SDValue();
5102 if (!LDBase) {
5103 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5104 return SDValue();
5105 LDBase = cast<LoadSDNode>(Elt.getNode());
5106 LastLoadedElt = i;
5107 continue;
5108 }
5109 if (Elt.getOpcode() == ISD::UNDEF)
5110 continue;
5111
5112 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5113 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5114 return SDValue();
5115 LastLoadedElt = i;
5116 }
Nate Begeman1449f292010-03-24 22:19:06 +00005117
5118 // If we have found an entire vector of loads and undefs, then return a large
5119 // load of the entire vector width starting at the base pointer. If we found
5120 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005121 if (LastLoadedElt == NumElems - 1) {
5122 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005123 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005124 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005125 LDBase->isVolatile(), LDBase->isNonTemporal(),
5126 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005127 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005128 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005129 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005130 LDBase->isInvariant(), LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005131 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5132 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005133 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5134 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005135 SDValue ResNode =
5136 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5137 LDBase->getPointerInfo(),
5138 LDBase->getAlignment(),
5139 false/*isVolatile*/, true/*ReadMem*/,
5140 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005141 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005142 }
5143 return SDValue();
5144}
5145
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005146/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
5147/// a vbroadcast node. We support two patterns:
5148/// 1. A splat BUILD_VECTOR which uses a single scalar load.
5149/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
5150/// a scalar load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005151/// The scalar load node is returned when a pattern is found,
5152/// or SDValue() otherwise.
5153static SDValue isVectorBroadcast(SDValue &Op, bool hasAVX2) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005154 EVT VT = Op.getValueType();
5155 SDValue V = Op;
5156
5157 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
5158 V = V.getOperand(0);
5159
5160 //A suspected load to be broadcasted.
5161 SDValue Ld;
5162
5163 switch (V.getOpcode()) {
5164 default:
5165 // Unknown pattern found.
5166 return SDValue();
5167
5168 case ISD::BUILD_VECTOR: {
5169 // The BUILD_VECTOR node must be a splat.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005170 if (!isSplatVector(V.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005171 return SDValue();
5172
5173 Ld = V.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005174
5175 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005176 // of its users are from the BUILD_VECTOR node.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005177 if (!Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005178 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005179 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005180 }
5181
5182 case ISD::VECTOR_SHUFFLE: {
5183 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5184
5185 // Shuffles must have a splat mask where the first element is
5186 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005187 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005188 return SDValue();
5189
5190 SDValue Sc = Op.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005191 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005192 return SDValue();
5193
5194 Ld = Sc.getOperand(0);
5195
5196 // The scalar_to_vector node and the suspected
5197 // load node must have exactly one user.
5198 if (!Sc.hasOneUse() || !Ld.hasOneUse())
5199 return SDValue();
5200 break;
5201 }
5202 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005203
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005204 // The scalar source must be a normal load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005205 if (!ISD::isNormalLoad(Ld.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005206 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005207
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005208 bool Is256 = VT.getSizeInBits() == 256;
5209 bool Is128 = VT.getSizeInBits() == 128;
5210 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5211
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005212 if (hasAVX2) {
5213 // VBroadcast to YMM
5214 if (Is256 && (ScalarSize == 8 || ScalarSize == 16 ||
5215 ScalarSize == 32 || ScalarSize == 64 ))
5216 return Ld;
5217
5218 // VBroadcast to XMM
5219 if (Is128 && (ScalarSize == 8 || ScalarSize == 32 ||
5220 ScalarSize == 16 || ScalarSize == 64 ))
5221 return Ld;
5222 }
5223
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005224 // VBroadcast to YMM
5225 if (Is256 && (ScalarSize == 32 || ScalarSize == 64))
5226 return Ld;
5227
5228 // VBroadcast to XMM
5229 if (Is128 && (ScalarSize == 32))
5230 return Ld;
5231
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005232
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005233 // Unsupported broadcast.
5234 return SDValue();
5235}
5236
Evan Chengc3630942009-12-09 21:00:30 +00005237SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005238X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005239 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005240
David Greenef125a292011-02-08 19:04:41 +00005241 EVT VT = Op.getValueType();
5242 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005243 unsigned NumElems = Op.getNumOperands();
5244
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005245 // Vectors containing all zeros can be matched by pxor and xorps later
5246 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5247 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5248 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005249 if (Op.getValueType() == MVT::v4i32 ||
5250 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005251 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005252
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005253 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005254 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005255
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005256 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005257 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5258 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005259 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper745a86b2011-11-19 22:34:59 +00005260 if (Op.getValueType() == MVT::v4i32 ||
5261 (Op.getValueType() == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005262 return Op;
5263
Craig Topper745a86b2011-11-19 22:34:59 +00005264 return getOnesVector(Op.getValueType(), Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005265 }
5266
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005267 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005268 if (Subtarget->hasAVX() && LD.getNode())
5269 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
5270
Owen Andersone50ed302009-08-10 22:56:29 +00005271 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005272
Evan Cheng0db9fe62006-04-25 20:13:52 +00005273 unsigned NumZero = 0;
5274 unsigned NumNonZero = 0;
5275 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005276 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005277 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005278 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005279 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005280 if (Elt.getOpcode() == ISD::UNDEF)
5281 continue;
5282 Values.insert(Elt);
5283 if (Elt.getOpcode() != ISD::Constant &&
5284 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005285 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005286 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005287 NumZero++;
5288 else {
5289 NonZeros |= (1 << i);
5290 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005291 }
5292 }
5293
Chris Lattner97a2a562010-08-26 05:24:29 +00005294 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5295 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005296 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005297
Chris Lattner67f453a2008-03-09 05:42:06 +00005298 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005299 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005300 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005301 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005302
Chris Lattner62098042008-03-09 01:05:04 +00005303 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5304 // the value are obviously zero, truncate the value to i32 and do the
5305 // insertion that way. Only do this if the value is non-constant or if the
5306 // value is a constant being inserted into element 0. It is cheaper to do
5307 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005308 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005309 (!IsAllConstants || Idx == 0)) {
5310 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005311 // Handle SSE only.
5312 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5313 EVT VecVT = MVT::v4i32;
5314 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005315
Chris Lattner62098042008-03-09 01:05:04 +00005316 // Truncate the value (which may itself be a constant) to i32, and
5317 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005318 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005319 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005320 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005321 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005322
Chris Lattner62098042008-03-09 01:05:04 +00005323 // Now we have our 32-bit value zero extended in the low element of
5324 // a vector. If Idx != 0, swizzle it into place.
5325 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005326 SmallVector<int, 4> Mask;
5327 Mask.push_back(Idx);
5328 for (unsigned i = 1; i != VecElts; ++i)
5329 Mask.push_back(i);
5330 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005331 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005332 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005333 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005334 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005335 }
5336 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005337
Chris Lattner19f79692008-03-08 22:59:52 +00005338 // If we have a constant or non-constant insertion into the low element of
5339 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5340 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005341 // depending on what the source datatype is.
5342 if (Idx == 0) {
5343 if (NumZero == 0) {
5344 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005345 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5346 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005347 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5348 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005349 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005350 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005351 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5352 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005353 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5354 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005355 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5356 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005357 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005358 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005359 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005360 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005361
5362 // Is it a vector logical left shift?
5363 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005364 X86::isZeroNode(Op.getOperand(0)) &&
5365 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005366 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005367 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005368 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005369 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005370 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005371 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005372
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005373 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005374 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005375
Chris Lattner19f79692008-03-08 22:59:52 +00005376 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5377 // is a non-constant being inserted into an element other than the low one,
5378 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5379 // movd/movss) to move this into the low element, then shuffle it into
5380 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005381 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005382 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005383
Evan Cheng0db9fe62006-04-25 20:13:52 +00005384 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005385 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005386 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005387 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005388 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005389 MaskVec.push_back(i == Idx ? 0 : 1);
5390 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005391 }
5392 }
5393
Chris Lattner67f453a2008-03-09 05:42:06 +00005394 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005395 if (Values.size() == 1) {
5396 if (EVTBits == 32) {
5397 // Instead of a shuffle like this:
5398 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5399 // Check if it's possible to issue this instead.
5400 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5401 unsigned Idx = CountTrailingZeros_32(NonZeros);
5402 SDValue Item = Op.getOperand(Idx);
5403 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5404 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5405 }
Dan Gohman475871a2008-07-27 21:46:04 +00005406 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005407 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005408
Dan Gohmana3941172007-07-24 22:55:08 +00005409 // A vector full of immediates; various special cases are already
5410 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005411 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005412 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005413
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005414 // For AVX-length vectors, build the individual 128-bit pieces and use
5415 // shuffles to put them in place.
5416 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5417 SmallVector<SDValue, 32> V;
5418 for (unsigned i = 0; i < NumElems; ++i)
5419 V.push_back(Op.getOperand(i));
5420
5421 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5422
5423 // Build both the lower and upper subvector.
5424 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5425 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5426 NumElems/2);
5427
5428 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005429 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5430 DAG.getConstant(0, MVT::i32), DAG, dl);
5431 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005432 DAG, dl);
5433 }
5434
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005435 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005436 if (EVTBits == 64) {
5437 if (NumNonZero == 1) {
5438 // One half is zero or undef.
5439 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005440 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005441 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005442 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005443 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005444 }
Dan Gohman475871a2008-07-27 21:46:04 +00005445 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005446 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005447
5448 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005449 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005450 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005451 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005452 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005453 }
5454
Bill Wendling826f36f2007-03-28 00:57:11 +00005455 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005456 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005457 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005458 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005459 }
5460
5461 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005462 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005463 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005464 if (NumElems == 4 && NumZero > 0) {
5465 for (unsigned i = 0; i < 4; ++i) {
5466 bool isZero = !(NonZeros & (1 << i));
5467 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005468 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005469 else
Dale Johannesenace16102009-02-03 19:33:06 +00005470 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005471 }
5472
5473 for (unsigned i = 0; i < 2; ++i) {
5474 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5475 default: break;
5476 case 0:
5477 V[i] = V[i*2]; // Must be a zero vector.
5478 break;
5479 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005480 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005481 break;
5482 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005483 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005484 break;
5485 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005486 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005487 break;
5488 }
5489 }
5490
Nate Begeman9008ca62009-04-27 18:41:29 +00005491 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005492 bool Reverse = (NonZeros & 0x3) == 2;
5493 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005494 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005495 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5496 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005497 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5498 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005499 }
5500
Nate Begemanfdea31a2010-03-24 20:49:50 +00005501 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5502 // Check for a build vector of consecutive loads.
5503 for (unsigned i = 0; i < NumElems; ++i)
5504 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005505
Nate Begemanfdea31a2010-03-24 20:49:50 +00005506 // Check for elements which are consecutive loads.
5507 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5508 if (LD.getNode())
5509 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005510
5511 // For SSE 4.1, use insertps to put the high elements into the low element.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005512 if (getSubtarget()->hasSSE41() || getSubtarget()->hasAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005513 SDValue Result;
5514 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5515 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5516 else
5517 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005518
Chris Lattner24faf612010-08-28 17:59:08 +00005519 for (unsigned i = 1; i < NumElems; ++i) {
5520 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5521 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005522 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005523 }
5524 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005525 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005526
Chris Lattner6e80e442010-08-28 17:15:43 +00005527 // Otherwise, expand into a number of unpckl*, start by extending each of
5528 // our (non-undef) elements to the full vector width with the element in the
5529 // bottom slot of the vector (which generates no code for SSE).
5530 for (unsigned i = 0; i < NumElems; ++i) {
5531 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5532 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5533 else
5534 V[i] = DAG.getUNDEF(VT);
5535 }
5536
5537 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005538 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5539 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5540 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005541 unsigned EltStride = NumElems >> 1;
5542 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005543 for (unsigned i = 0; i < EltStride; ++i) {
5544 // If V[i+EltStride] is undef and this is the first round of mixing,
5545 // then it is safe to just drop this shuffle: V[i] is already in the
5546 // right place, the one element (since it's the first round) being
5547 // inserted as undef can be dropped. This isn't safe for successive
5548 // rounds because they will permute elements within both vectors.
5549 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5550 EltStride == NumElems/2)
5551 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005552
Chris Lattner6e80e442010-08-28 17:15:43 +00005553 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005554 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005555 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005556 }
5557 return V[0];
5558 }
Dan Gohman475871a2008-07-27 21:46:04 +00005559 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005560}
5561
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005562// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5563// them in a MMX register. This is better than doing a stack convert.
5564static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005565 DebugLoc dl = Op.getDebugLoc();
5566 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005567
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005568 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5569 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5570 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005571 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005572 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5573 InVec = Op.getOperand(1);
5574 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5575 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005576 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005577 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5578 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5579 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005580 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005581 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5582 Mask[0] = 0; Mask[1] = 2;
5583 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5584 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005585 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005586}
5587
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005588// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5589// to create 256-bit vectors from two other 128-bit ones.
5590static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5591 DebugLoc dl = Op.getDebugLoc();
5592 EVT ResVT = Op.getValueType();
5593
5594 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5595
5596 SDValue V1 = Op.getOperand(0);
5597 SDValue V2 = Op.getOperand(1);
5598 unsigned NumElems = ResVT.getVectorNumElements();
5599
5600 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5601 DAG.getConstant(0, MVT::i32), DAG, dl);
5602 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5603 DAG, dl);
5604}
5605
5606SDValue
5607X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005608 EVT ResVT = Op.getValueType();
5609
5610 assert(Op.getNumOperands() == 2);
5611 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5612 "Unsupported CONCAT_VECTORS for value type");
5613
5614 // We support concatenate two MMX registers and place them in a MMX register.
5615 // This is better than doing a stack convert.
5616 if (ResVT.is128BitVector())
5617 return LowerMMXCONCAT_VECTORS(Op, DAG);
5618
5619 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5620 // from two other 128-bit ones.
5621 return LowerAVXCONCAT_VECTORS(Op, DAG);
5622}
5623
Nate Begemanb9a47b82009-02-23 08:49:38 +00005624// v8i16 shuffles - Prefer shuffles in the following order:
5625// 1. [all] pshuflw, pshufhw, optional move
5626// 2. [ssse3] 1 x pshufb
5627// 3. [ssse3] 2 x pshufb + 1 x por
5628// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005629SDValue
5630X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5631 SelectionDAG &DAG) const {
5632 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005633 SDValue V1 = SVOp->getOperand(0);
5634 SDValue V2 = SVOp->getOperand(1);
5635 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005636 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005637
Nate Begemanb9a47b82009-02-23 08:49:38 +00005638 // Determine if more than 1 of the words in each of the low and high quadwords
5639 // of the result come from the same quadword of one of the two inputs. Undef
5640 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005641 unsigned LoQuad[] = { 0, 0, 0, 0 };
5642 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005643 BitVector InputQuads(4);
5644 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005645 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005646 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005647 MaskVals.push_back(EltIdx);
5648 if (EltIdx < 0) {
5649 ++Quad[0];
5650 ++Quad[1];
5651 ++Quad[2];
5652 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005653 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005654 }
5655 ++Quad[EltIdx / 4];
5656 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005657 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005658
Nate Begemanb9a47b82009-02-23 08:49:38 +00005659 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005660 unsigned MaxQuad = 1;
5661 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005662 if (LoQuad[i] > MaxQuad) {
5663 BestLoQuad = i;
5664 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005665 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005666 }
5667
Nate Begemanb9a47b82009-02-23 08:49:38 +00005668 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005669 MaxQuad = 1;
5670 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005671 if (HiQuad[i] > MaxQuad) {
5672 BestHiQuad = i;
5673 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005674 }
5675 }
5676
Nate Begemanb9a47b82009-02-23 08:49:38 +00005677 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005678 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005679 // single pshufb instruction is necessary. If There are more than 2 input
5680 // quads, disable the next transformation since it does not help SSSE3.
5681 bool V1Used = InputQuads[0] || InputQuads[1];
5682 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005683 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005684 if (InputQuads.count() == 2 && V1Used && V2Used) {
5685 BestLoQuad = InputQuads.find_first();
5686 BestHiQuad = InputQuads.find_next(BestLoQuad);
5687 }
5688 if (InputQuads.count() > 2) {
5689 BestLoQuad = -1;
5690 BestHiQuad = -1;
5691 }
5692 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005693
Nate Begemanb9a47b82009-02-23 08:49:38 +00005694 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5695 // the shuffle mask. If a quad is scored as -1, that means that it contains
5696 // words from all 4 input quadwords.
5697 SDValue NewV;
5698 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005699 SmallVector<int, 8> MaskV;
5700 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5701 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005702 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005703 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5704 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5705 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005706
Nate Begemanb9a47b82009-02-23 08:49:38 +00005707 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5708 // source words for the shuffle, to aid later transformations.
5709 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005710 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005711 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005712 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005713 if (idx != (int)i)
5714 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005715 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005716 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005717 AllWordsInNewV = false;
5718 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005719 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005720
Nate Begemanb9a47b82009-02-23 08:49:38 +00005721 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5722 if (AllWordsInNewV) {
5723 for (int i = 0; i != 8; ++i) {
5724 int idx = MaskVals[i];
5725 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005726 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005727 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005728 if ((idx != i) && idx < 4)
5729 pshufhw = false;
5730 if ((idx != i) && idx > 3)
5731 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005732 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005733 V1 = NewV;
5734 V2Used = false;
5735 BestLoQuad = 0;
5736 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005737 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005738
Nate Begemanb9a47b82009-02-23 08:49:38 +00005739 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5740 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005741 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005742 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5743 unsigned TargetMask = 0;
5744 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005745 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005746 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5747 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5748 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005749 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005750 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005751 }
Eric Christopherfd179292009-08-27 18:07:15 +00005752
Nate Begemanb9a47b82009-02-23 08:49:38 +00005753 // If we have SSSE3, and all words of the result are from 1 input vector,
5754 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5755 // is present, fall back to case 4.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005756 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005757 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005758
Nate Begemanb9a47b82009-02-23 08:49:38 +00005759 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005760 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005761 // mask, and elements that come from V1 in the V2 mask, so that the two
5762 // results can be OR'd together.
5763 bool TwoInputs = V1Used && V2Used;
5764 for (unsigned i = 0; i != 8; ++i) {
5765 int EltIdx = MaskVals[i] * 2;
5766 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005767 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5768 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005769 continue;
5770 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005771 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5772 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005773 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005774 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005775 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005776 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005777 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005778 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005779 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005780
Nate Begemanb9a47b82009-02-23 08:49:38 +00005781 // Calculate the shuffle mask for the second input, shuffle it, and
5782 // OR it with the first shuffled input.
5783 pshufbMask.clear();
5784 for (unsigned i = 0; i != 8; ++i) {
5785 int EltIdx = MaskVals[i] * 2;
5786 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005787 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5788 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005789 continue;
5790 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005791 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5792 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005793 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005794 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005795 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005796 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005797 MVT::v16i8, &pshufbMask[0], 16));
5798 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005799 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005800 }
5801
5802 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5803 // and update MaskVals with new element order.
5804 BitVector InOrder(8);
5805 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005806 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005807 for (int i = 0; i != 4; ++i) {
5808 int idx = MaskVals[i];
5809 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005810 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005811 InOrder.set(i);
5812 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005813 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005814 InOrder.set(i);
5815 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005816 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005817 }
5818 }
5819 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005820 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005821 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005822 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005823
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005824 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5825 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005826 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5827 NewV.getOperand(0),
5828 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5829 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005830 }
Eric Christopherfd179292009-08-27 18:07:15 +00005831
Nate Begemanb9a47b82009-02-23 08:49:38 +00005832 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5833 // and update MaskVals with the new element order.
5834 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005835 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005836 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005837 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005838 for (unsigned i = 4; i != 8; ++i) {
5839 int idx = MaskVals[i];
5840 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005841 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005842 InOrder.set(i);
5843 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005844 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005845 InOrder.set(i);
5846 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005847 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005848 }
5849 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005850 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005851 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005852
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005853 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5854 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005855 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5856 NewV.getOperand(0),
5857 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5858 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005859 }
Eric Christopherfd179292009-08-27 18:07:15 +00005860
Nate Begemanb9a47b82009-02-23 08:49:38 +00005861 // In case BestHi & BestLo were both -1, which means each quadword has a word
5862 // from each of the four input quadwords, calculate the InOrder bitvector now
5863 // before falling through to the insert/extract cleanup.
5864 if (BestLoQuad == -1 && BestHiQuad == -1) {
5865 NewV = V1;
5866 for (int i = 0; i != 8; ++i)
5867 if (MaskVals[i] < 0 || MaskVals[i] == i)
5868 InOrder.set(i);
5869 }
Eric Christopherfd179292009-08-27 18:07:15 +00005870
Nate Begemanb9a47b82009-02-23 08:49:38 +00005871 // The other elements are put in the right place using pextrw and pinsrw.
5872 for (unsigned i = 0; i != 8; ++i) {
5873 if (InOrder[i])
5874 continue;
5875 int EltIdx = MaskVals[i];
5876 if (EltIdx < 0)
5877 continue;
5878 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005879 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005880 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005881 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005882 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005883 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005884 DAG.getIntPtrConstant(i));
5885 }
5886 return NewV;
5887}
5888
5889// v16i8 shuffles - Prefer shuffles in the following order:
5890// 1. [ssse3] 1 x pshufb
5891// 2. [ssse3] 2 x pshufb + 1 x por
5892// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5893static
Nate Begeman9008ca62009-04-27 18:41:29 +00005894SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005895 SelectionDAG &DAG,
5896 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005897 SDValue V1 = SVOp->getOperand(0);
5898 SDValue V2 = SVOp->getOperand(1);
5899 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005900 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005901 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005902
Nate Begemanb9a47b82009-02-23 08:49:38 +00005903 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005904 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005905 // present, fall back to case 3.
5906 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5907 bool V1Only = true;
5908 bool V2Only = true;
5909 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005910 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005911 if (EltIdx < 0)
5912 continue;
5913 if (EltIdx < 16)
5914 V2Only = false;
5915 else
5916 V1Only = false;
5917 }
Eric Christopherfd179292009-08-27 18:07:15 +00005918
Nate Begemanb9a47b82009-02-23 08:49:38 +00005919 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005920 if (TLI.getSubtarget()->hasSSSE3() || TLI.getSubtarget()->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005921 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005922
Nate Begemanb9a47b82009-02-23 08:49:38 +00005923 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005924 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005925 //
5926 // Otherwise, we have elements from both input vectors, and must zero out
5927 // elements that come from V2 in the first mask, and V1 in the second mask
5928 // so that we can OR them together.
5929 bool TwoInputs = !(V1Only || V2Only);
5930 for (unsigned i = 0; i != 16; ++i) {
5931 int EltIdx = MaskVals[i];
5932 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005933 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005934 continue;
5935 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005936 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005937 }
5938 // If all the elements are from V2, assign it to V1 and return after
5939 // building the first pshufb.
5940 if (V2Only)
5941 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005942 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005943 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005944 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005945 if (!TwoInputs)
5946 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005947
Nate Begemanb9a47b82009-02-23 08:49:38 +00005948 // Calculate the shuffle mask for the second input, shuffle it, and
5949 // OR it with the first shuffled input.
5950 pshufbMask.clear();
5951 for (unsigned i = 0; i != 16; ++i) {
5952 int EltIdx = MaskVals[i];
5953 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005954 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005955 continue;
5956 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005957 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005958 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005959 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005960 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005961 MVT::v16i8, &pshufbMask[0], 16));
5962 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005963 }
Eric Christopherfd179292009-08-27 18:07:15 +00005964
Nate Begemanb9a47b82009-02-23 08:49:38 +00005965 // No SSSE3 - Calculate in place words and then fix all out of place words
5966 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5967 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005968 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5969 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005970 SDValue NewV = V2Only ? V2 : V1;
5971 for (int i = 0; i != 8; ++i) {
5972 int Elt0 = MaskVals[i*2];
5973 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005974
Nate Begemanb9a47b82009-02-23 08:49:38 +00005975 // This word of the result is all undef, skip it.
5976 if (Elt0 < 0 && Elt1 < 0)
5977 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005978
Nate Begemanb9a47b82009-02-23 08:49:38 +00005979 // This word of the result is already in the correct place, skip it.
5980 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5981 continue;
5982 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5983 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005984
Nate Begemanb9a47b82009-02-23 08:49:38 +00005985 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5986 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5987 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005988
5989 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5990 // using a single extract together, load it and store it.
5991 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005992 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005993 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005994 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005995 DAG.getIntPtrConstant(i));
5996 continue;
5997 }
5998
Nate Begemanb9a47b82009-02-23 08:49:38 +00005999 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006000 // source byte is not also odd, shift the extracted word left 8 bits
6001 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006002 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006003 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006004 DAG.getIntPtrConstant(Elt1 / 2));
6005 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006006 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006007 DAG.getConstant(8,
6008 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006009 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006010 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6011 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006012 }
6013 // If Elt0 is defined, extract it from the appropriate source. If the
6014 // source byte is not also even, shift the extracted word right 8 bits. If
6015 // Elt1 was also defined, OR the extracted values together before
6016 // inserting them in the result.
6017 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006018 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006019 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6020 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006021 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006022 DAG.getConstant(8,
6023 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006024 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006025 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6026 DAG.getConstant(0x00FF, MVT::i16));
6027 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006028 : InsElt0;
6029 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006030 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006031 DAG.getIntPtrConstant(i));
6032 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006033 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006034}
6035
Evan Cheng7a831ce2007-12-15 03:00:47 +00006036/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006037/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006038/// done when every pair / quad of shuffle mask elements point to elements in
6039/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006040/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006041static
Nate Begeman9008ca62009-04-27 18:41:29 +00006042SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006043 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00006044 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00006045 SDValue V1 = SVOp->getOperand(0);
6046 SDValue V2 = SVOp->getOperand(1);
6047 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00006048 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006049 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00006050 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006051 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006052 case MVT::v4f32: NewVT = MVT::v2f64; break;
6053 case MVT::v4i32: NewVT = MVT::v2i64; break;
6054 case MVT::v8i16: NewVT = MVT::v4i32; break;
6055 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006056 }
6057
Nate Begeman9008ca62009-04-27 18:41:29 +00006058 int Scale = NumElems / NewWidth;
6059 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00006060 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006061 int StartIdx = -1;
6062 for (int j = 0; j < Scale; ++j) {
6063 int EltIdx = SVOp->getMaskElt(i+j);
6064 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006065 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00006066 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00006067 StartIdx = EltIdx - (EltIdx % Scale);
6068 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00006069 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006070 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006071 if (StartIdx == -1)
6072 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00006073 else
Nate Begeman9008ca62009-04-27 18:41:29 +00006074 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006075 }
6076
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006077 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
6078 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00006079 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006080}
6081
Evan Chengd880b972008-05-09 21:53:03 +00006082/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006083///
Owen Andersone50ed302009-08-10 22:56:29 +00006084static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006085 SDValue SrcOp, SelectionDAG &DAG,
6086 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006087 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006088 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006089 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006090 LD = dyn_cast<LoadSDNode>(SrcOp);
6091 if (!LD) {
6092 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6093 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006094 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006095 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006096 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006097 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006098 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006099 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006100 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006101 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006102 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6103 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6104 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006105 SrcOp.getOperand(0)
6106 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006107 }
6108 }
6109 }
6110
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006111 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006112 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006113 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006114 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006115}
6116
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006117/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
6118/// shuffle node referes to only one lane in the sources.
6119static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
6120 EVT VT = SVOp->getValueType(0);
6121 int NumElems = VT.getVectorNumElements();
6122 int HalfSize = NumElems/2;
6123 SmallVector<int, 16> M;
6124 SVOp->getMask(M);
6125 bool MatchA = false, MatchB = false;
6126
6127 for (int l = 0; l < NumElems*2; l += HalfSize) {
6128 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
6129 MatchA = true;
6130 break;
6131 }
6132 }
6133
6134 for (int l = 0; l < NumElems*2; l += HalfSize) {
6135 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
6136 MatchB = true;
6137 break;
6138 }
6139 }
6140
6141 return MatchA && MatchB;
6142}
6143
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006144/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6145/// which could not be matched by any known target speficic shuffle
6146static SDValue
6147LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006148 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
6149 // If each half of a vector shuffle node referes to only one lane in the
6150 // source vectors, extract each used 128-bit lane and shuffle them using
6151 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
6152 // the work to the legalizer.
6153 DebugLoc dl = SVOp->getDebugLoc();
6154 EVT VT = SVOp->getValueType(0);
6155 int NumElems = VT.getVectorNumElements();
6156 int HalfSize = NumElems/2;
6157
6158 // Extract the reference for each half
6159 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
6160 int FstVecOpNum = 0, SndVecOpNum = 0;
6161 for (int i = 0; i < HalfSize; ++i) {
6162 int Elt = SVOp->getMaskElt(i);
6163 if (SVOp->getMaskElt(i) < 0)
6164 continue;
6165 FstVecOpNum = Elt/NumElems;
6166 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6167 break;
6168 }
6169 for (int i = HalfSize; i < NumElems; ++i) {
6170 int Elt = SVOp->getMaskElt(i);
6171 if (SVOp->getMaskElt(i) < 0)
6172 continue;
6173 SndVecOpNum = Elt/NumElems;
6174 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6175 break;
6176 }
6177
6178 // Extract the subvectors
6179 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
6180 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
6181 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
6182 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
6183
6184 // Generate 128-bit shuffles
6185 SmallVector<int, 16> MaskV1, MaskV2;
6186 for (int i = 0; i < HalfSize; ++i) {
6187 int Elt = SVOp->getMaskElt(i);
6188 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6189 }
6190 for (int i = HalfSize; i < NumElems; ++i) {
6191 int Elt = SVOp->getMaskElt(i);
6192 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6193 }
6194
6195 EVT NVT = V1.getValueType();
6196 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
6197 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
6198
6199 // Concatenate the result back
6200 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
6201 DAG.getConstant(0, MVT::i32), DAG, dl);
6202 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
6203 DAG, dl);
6204 }
6205
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006206 return SDValue();
6207}
6208
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006209/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6210/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006211static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006212LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006213 SDValue V1 = SVOp->getOperand(0);
6214 SDValue V2 = SVOp->getOperand(1);
6215 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006216 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006217
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006218 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6219
Evan Chengace3c172008-07-22 21:13:36 +00006220 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006221 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006222 SmallVector<int, 8> Mask1(4U, -1);
6223 SmallVector<int, 8> PermMask;
6224 SVOp->getMask(PermMask);
6225
Evan Chengace3c172008-07-22 21:13:36 +00006226 unsigned NumHi = 0;
6227 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006228 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006229 int Idx = PermMask[i];
6230 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006231 Locs[i] = std::make_pair(-1, -1);
6232 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006233 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6234 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006235 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006236 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006237 NumLo++;
6238 } else {
6239 Locs[i] = std::make_pair(1, NumHi);
6240 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006241 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006242 NumHi++;
6243 }
6244 }
6245 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006246
Evan Chengace3c172008-07-22 21:13:36 +00006247 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006248 // If no more than two elements come from either vector. This can be
6249 // implemented with two shuffles. First shuffle gather the elements.
6250 // The second shuffle, which takes the first shuffle as both of its
6251 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006252 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006253
Nate Begeman9008ca62009-04-27 18:41:29 +00006254 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006255
Evan Chengace3c172008-07-22 21:13:36 +00006256 for (unsigned i = 0; i != 4; ++i) {
6257 if (Locs[i].first == -1)
6258 continue;
6259 else {
6260 unsigned Idx = (i < 2) ? 0 : 4;
6261 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006262 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006263 }
6264 }
6265
Nate Begeman9008ca62009-04-27 18:41:29 +00006266 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006267 } else if (NumLo == 3 || NumHi == 3) {
6268 // Otherwise, we must have three elements from one vector, call it X, and
6269 // one element from the other, call it Y. First, use a shufps to build an
6270 // intermediate vector with the one element from Y and the element from X
6271 // that will be in the same half in the final destination (the indexes don't
6272 // matter). Then, use a shufps to build the final vector, taking the half
6273 // containing the element from Y from the intermediate, and the other half
6274 // from X.
6275 if (NumHi == 3) {
6276 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006277 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006278 std::swap(V1, V2);
6279 }
6280
6281 // Find the element from V2.
6282 unsigned HiIndex;
6283 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006284 int Val = PermMask[HiIndex];
6285 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006286 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006287 if (Val >= 4)
6288 break;
6289 }
6290
Nate Begeman9008ca62009-04-27 18:41:29 +00006291 Mask1[0] = PermMask[HiIndex];
6292 Mask1[1] = -1;
6293 Mask1[2] = PermMask[HiIndex^1];
6294 Mask1[3] = -1;
6295 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006296
6297 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006298 Mask1[0] = PermMask[0];
6299 Mask1[1] = PermMask[1];
6300 Mask1[2] = HiIndex & 1 ? 6 : 4;
6301 Mask1[3] = HiIndex & 1 ? 4 : 6;
6302 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006303 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006304 Mask1[0] = HiIndex & 1 ? 2 : 0;
6305 Mask1[1] = HiIndex & 1 ? 0 : 2;
6306 Mask1[2] = PermMask[2];
6307 Mask1[3] = PermMask[3];
6308 if (Mask1[2] >= 0)
6309 Mask1[2] += 4;
6310 if (Mask1[3] >= 0)
6311 Mask1[3] += 4;
6312 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006313 }
Evan Chengace3c172008-07-22 21:13:36 +00006314 }
6315
6316 // Break it into (shuffle shuffle_hi, shuffle_lo).
6317 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006318 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006319 SmallVector<int,8> LoMask(4U, -1);
6320 SmallVector<int,8> HiMask(4U, -1);
6321
6322 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006323 unsigned MaskIdx = 0;
6324 unsigned LoIdx = 0;
6325 unsigned HiIdx = 2;
6326 for (unsigned i = 0; i != 4; ++i) {
6327 if (i == 2) {
6328 MaskPtr = &HiMask;
6329 MaskIdx = 1;
6330 LoIdx = 0;
6331 HiIdx = 2;
6332 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006333 int Idx = PermMask[i];
6334 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006335 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006336 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006337 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006338 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006339 LoIdx++;
6340 } else {
6341 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006342 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006343 HiIdx++;
6344 }
6345 }
6346
Nate Begeman9008ca62009-04-27 18:41:29 +00006347 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6348 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6349 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006350 for (unsigned i = 0; i != 4; ++i) {
6351 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006352 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006353 } else {
6354 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006355 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006356 }
6357 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006358 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006359}
6360
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006361static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006362 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006363 V = V.getOperand(0);
6364 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6365 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006366 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6367 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6368 // BUILD_VECTOR (load), undef
6369 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006370 if (MayFoldLoad(V))
6371 return true;
6372 return false;
6373}
6374
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006375// FIXME: the version above should always be used. Since there's
6376// a bug where several vector shuffles can't be folded because the
6377// DAG is not updated during lowering and a node claims to have two
6378// uses while it only has one, use this version, and let isel match
6379// another instruction if the load really happens to have more than
6380// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006381// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006382static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006383 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006384 V = V.getOperand(0);
6385 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6386 V = V.getOperand(0);
6387 if (ISD::isNormalLoad(V.getNode()))
6388 return true;
6389 return false;
6390}
6391
6392/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6393/// a vector extract, and if both can be later optimized into a single load.
6394/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6395/// here because otherwise a target specific shuffle node is going to be
6396/// emitted for this shuffle, and the optimization not done.
6397/// FIXME: This is probably not the best approach, but fix the problem
6398/// until the right path is decided.
6399static
6400bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6401 const TargetLowering &TLI) {
6402 EVT VT = V.getValueType();
6403 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6404
6405 // Be sure that the vector shuffle is present in a pattern like this:
6406 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6407 if (!V.hasOneUse())
6408 return false;
6409
6410 SDNode *N = *V.getNode()->use_begin();
6411 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6412 return false;
6413
6414 SDValue EltNo = N->getOperand(1);
6415 if (!isa<ConstantSDNode>(EltNo))
6416 return false;
6417
6418 // If the bit convert changed the number of elements, it is unsafe
6419 // to examine the mask.
6420 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006421 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006422 EVT SrcVT = V.getOperand(0).getValueType();
6423 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6424 return false;
6425 V = V.getOperand(0);
6426 HasShuffleIntoBitcast = true;
6427 }
6428
6429 // Select the input vector, guarding against out of range extract vector.
6430 unsigned NumElems = VT.getVectorNumElements();
6431 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6432 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6433 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6434
6435 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006436 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006437 V = V.getOperand(0);
6438
6439 if (ISD::isNormalLoad(V.getNode())) {
6440 // Is the original load suitable?
6441 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6442
6443 // FIXME: avoid the multi-use bug that is preventing lots of
6444 // of foldings to be detected, this is still wrong of course, but
6445 // give the temporary desired behavior, and if it happens that
6446 // the load has real more uses, during isel it will not fold, and
6447 // will generate poor code.
6448 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6449 return false;
6450
6451 if (!HasShuffleIntoBitcast)
6452 return true;
6453
6454 // If there's a bitcast before the shuffle, check if the load type and
6455 // alignment is valid.
6456 unsigned Align = LN0->getAlignment();
6457 unsigned NewAlign =
6458 TLI.getTargetData()->getABITypeAlignment(
6459 VT.getTypeForEVT(*DAG.getContext()));
6460
6461 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6462 return false;
6463 }
6464
6465 return true;
6466}
6467
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006468static
Evan Cheng835580f2010-10-07 20:50:20 +00006469SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6470 EVT VT = Op.getValueType();
6471
6472 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006473 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6474 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006475 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6476 V1, DAG));
6477}
6478
6479static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006480SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006481 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006482 SDValue V1 = Op.getOperand(0);
6483 SDValue V2 = Op.getOperand(1);
6484 EVT VT = Op.getValueType();
6485
6486 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6487
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006488 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006489 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6490
Evan Cheng0899f5c2011-08-31 02:05:24 +00006491 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6492 return DAG.getNode(ISD::BITCAST, dl, VT,
6493 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6494 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6495 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006496}
6497
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006498static
6499SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6500 SDValue V1 = Op.getOperand(0);
6501 SDValue V2 = Op.getOperand(1);
6502 EVT VT = Op.getValueType();
6503
6504 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6505 "unsupported shuffle type");
6506
6507 if (V2.getOpcode() == ISD::UNDEF)
6508 V2 = V1;
6509
6510 // v4i32 or v4f32
6511 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6512}
6513
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006514static inline unsigned getSHUFPOpcode(EVT VT) {
6515 switch(VT.getSimpleVT().SimpleTy) {
6516 case MVT::v8i32: // Use fp unit for int unpack.
6517 case MVT::v8f32:
6518 case MVT::v4i32: // Use fp unit for int unpack.
6519 case MVT::v4f32: return X86ISD::SHUFPS;
6520 case MVT::v4i64: // Use fp unit for int unpack.
6521 case MVT::v4f64:
6522 case MVT::v2i64: // Use fp unit for int unpack.
6523 case MVT::v2f64: return X86ISD::SHUFPD;
6524 default:
6525 llvm_unreachable("Unknown type for shufp*");
6526 }
6527 return 0;
6528}
6529
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006530static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006531SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006532 SDValue V1 = Op.getOperand(0);
6533 SDValue V2 = Op.getOperand(1);
6534 EVT VT = Op.getValueType();
6535 unsigned NumElems = VT.getVectorNumElements();
6536
6537 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6538 // operand of these instructions is only memory, so check if there's a
6539 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6540 // same masks.
6541 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006542
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006543 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006544 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006545 CanFoldLoad = true;
6546
6547 // When V1 is a load, it can be folded later into a store in isel, example:
6548 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6549 // turns into:
6550 // (MOVLPSmr addr:$src1, VR128:$src2)
6551 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006552 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006553 CanFoldLoad = true;
6554
Dan Gohman65fd6562011-11-03 21:49:52 +00006555 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006556 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006557 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006558 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6559
6560 if (NumElems == 4)
Dan Gohman65fd6562011-11-03 21:49:52 +00006561 // If we don't care about the second element, procede to use movss.
6562 if (SVOp->getMaskElt(1) != -1)
6563 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006564 }
6565
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006566 // movl and movlp will both match v2i64, but v2i64 is never matched by
6567 // movl earlier because we make it strict to avoid messing with the movlp load
6568 // folding logic (see the code above getMOVLP call). Match it here then,
6569 // this is horrible, but will stay like this until we move all shuffle
6570 // matching to x86 specific nodes. Note that for the 1st condition all
6571 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006572 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006573 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6574 // as to remove this logic from here, as much as possible
6575 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006576 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006577 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006578 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006579
6580 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6581
6582 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006583 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006584 X86::getShuffleSHUFImmediate(SVOp), DAG);
6585}
6586
Craig Topper6347e862011-11-21 06:57:39 +00006587static inline unsigned getUNPCKLOpcode(EVT VT, bool HasAVX2) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006588 switch(VT.getSimpleVT().SimpleTy) {
6589 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6590 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006591 case MVT::v4f32: return X86ISD::UNPCKLPS;
6592 case MVT::v2f64: return X86ISD::UNPCKLPD;
Craig Topper6347e862011-11-21 06:57:39 +00006593 case MVT::v8i32:
6594 if (HasAVX2) return X86ISD::VPUNPCKLDQY;
6595 // else use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006596 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Craig Topper6347e862011-11-21 06:57:39 +00006597 case MVT::v4i64:
6598 if (HasAVX2) return X86ISD::VPUNPCKLQDQY;
6599 // else use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006600 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006601 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6602 case MVT::v8i16: return X86ISD::PUNPCKLWD;
Craig Topper6347e862011-11-21 06:57:39 +00006603 case MVT::v16i16: return X86ISD::VPUNPCKLWDY;
Craig Topper6fa583d2011-11-21 08:26:50 +00006604 case MVT::v32i8: return X86ISD::VPUNPCKLBWY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006605 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006606 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006607 }
6608 return 0;
6609}
6610
Craig Topper6347e862011-11-21 06:57:39 +00006611static inline unsigned getUNPCKHOpcode(EVT VT, bool HasAVX2) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006612 switch(VT.getSimpleVT().SimpleTy) {
6613 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6614 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6615 case MVT::v4f32: return X86ISD::UNPCKHPS;
6616 case MVT::v2f64: return X86ISD::UNPCKHPD;
Craig Topper6347e862011-11-21 06:57:39 +00006617 case MVT::v8i32:
6618 if (HasAVX2) return X86ISD::VPUNPCKHDQY;
6619 // else use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006620 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Craig Topper6347e862011-11-21 06:57:39 +00006621 case MVT::v4i64:
6622 if (HasAVX2) return X86ISD::VPUNPCKHQDQY;
6623 // else use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006624 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006625 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6626 case MVT::v8i16: return X86ISD::PUNPCKHWD;
Craig Topper6347e862011-11-21 06:57:39 +00006627 case MVT::v16i16: return X86ISD::VPUNPCKHWDY;
Craig Topper6fa583d2011-11-21 08:26:50 +00006628 case MVT::v32i8: return X86ISD::VPUNPCKHBWY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006629 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006630 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006631 }
6632 return 0;
6633}
6634
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006635static inline unsigned getVPERMILOpcode(EVT VT) {
6636 switch(VT.getSimpleVT().SimpleTy) {
6637 case MVT::v4i32:
6638 case MVT::v4f32: return X86ISD::VPERMILPS;
6639 case MVT::v2i64:
6640 case MVT::v2f64: return X86ISD::VPERMILPD;
6641 case MVT::v8i32:
6642 case MVT::v8f32: return X86ISD::VPERMILPSY;
6643 case MVT::v4i64:
6644 case MVT::v4f64: return X86ISD::VPERMILPDY;
6645 default:
6646 llvm_unreachable("Unknown type for vpermil");
6647 }
6648 return 0;
6649}
6650
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006651static
6652SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006653 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006654 const X86Subtarget *Subtarget) {
6655 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6656 EVT VT = Op.getValueType();
6657 DebugLoc dl = Op.getDebugLoc();
6658 SDValue V1 = Op.getOperand(0);
6659 SDValue V2 = Op.getOperand(1);
6660
6661 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006662 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006663
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006664 // Handle splat operations
6665 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006666 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006667 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006668 // Special case, this is the only place now where it's allowed to return
6669 // a vector_shuffle operation without using a target specific node, because
6670 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6671 // this be moved to DAGCombine instead?
6672 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006673 return Op;
6674
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006675 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00006676 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00006677 if (Subtarget->hasAVX() && LD.getNode())
6678 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006679
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006680 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006681 if ((Size == 128 && NumElem <= 4) ||
6682 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006683 return SDValue();
6684
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006685 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006686 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006687 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006688
6689 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6690 // do it!
6691 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6692 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6693 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006694 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006695 } else if ((VT == MVT::v4i32 ||
6696 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006697 // FIXME: Figure out a cleaner way to do this.
6698 // Try to make use of movq to zero out the top part.
6699 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6700 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6701 if (NewOp.getNode()) {
6702 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6703 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6704 DAG, Subtarget, dl);
6705 }
6706 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6707 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6708 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6709 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6710 DAG, Subtarget, dl);
6711 }
6712 }
6713 return SDValue();
6714}
6715
Dan Gohman475871a2008-07-27 21:46:04 +00006716SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006717X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006718 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006719 SDValue V1 = Op.getOperand(0);
6720 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006721 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006722 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006723 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006724 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6725 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006726 bool V1IsSplat = false;
6727 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006728 bool HasXMMInt = Subtarget->hasXMMInt();
Craig Topper6347e862011-11-21 06:57:39 +00006729 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006730 MachineFunction &MF = DAG.getMachineFunction();
6731 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006732
Craig Topper3426a3e2011-11-14 06:46:21 +00006733 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006734
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006735 // Vector shuffle lowering takes 3 steps:
6736 //
6737 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6738 // narrowing and commutation of operands should be handled.
6739 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6740 // shuffle nodes.
6741 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6742 // so the shuffle can be broken into other shuffles and the legalizer can
6743 // try the lowering again.
6744 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006745 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006746 // be matched during isel, all of them must be converted to a target specific
6747 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006748
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006749 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6750 // narrowing and commutation of operands should be handled. The actual code
6751 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006752 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006753 if (NewOp.getNode())
6754 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006755
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006756 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6757 // unpckh_undef). Only use pshufd if speed is more important than size.
6758 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006759 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V1,
6760 DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006761 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006762 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6763 DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006764
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006765 if (X86::isMOVDDUPMask(SVOp) &&
6766 (Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
6767 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006768 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006769
Dale Johannesen0488fb62010-09-30 23:57:10 +00006770 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006771 return getMOVHighToLow(Op, dl, DAG);
6772
6773 // Use to match splats
Craig Topper6347e862011-11-21 06:57:39 +00006774 if (HasXMMInt && X86::isUNPCKHMask(SVOp, Subtarget->hasAVX2()) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006775 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper6347e862011-11-21 06:57:39 +00006776 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6777 DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006778
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006779 if (X86::isPSHUFDMask(SVOp)) {
6780 // The actual implementation will match the mask in the if above and then
6781 // during isel it can match several different instructions, not only pshufd
6782 // as its name says, sad but true, emulate the behavior for now...
6783 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6784 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6785
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006786 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6787
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006788 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006789 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6790
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006791 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6792 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006793 }
Eric Christopherfd179292009-08-27 18:07:15 +00006794
Evan Chengf26ffe92008-05-29 08:22:04 +00006795 // Check if this can be converted into a logical shift.
6796 bool isLeft = false;
6797 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006798 SDValue ShVal;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006799 bool isShift = getSubtarget()->hasXMMInt() &&
6800 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006801 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006802 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006803 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006804 EVT EltVT = VT.getVectorElementType();
6805 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006806 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006807 }
Eric Christopherfd179292009-08-27 18:07:15 +00006808
Nate Begeman9008ca62009-04-27 18:41:29 +00006809 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006810 if (V1IsUndef)
6811 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006812 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006813 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006814 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006815 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006816 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6817
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006818 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006819 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6820 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006821 }
Eric Christopherfd179292009-08-27 18:07:15 +00006822
Nate Begeman9008ca62009-04-27 18:41:29 +00006823 // FIXME: fold these into legal mask.
Craig Topper6347e862011-11-21 06:57:39 +00006824 if (X86::isMOVLHPSMask(SVOp) &&
6825 !X86::isUNPCKLMask(SVOp, Subtarget->hasAVX2()))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006826 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006827
Dale Johannesen0488fb62010-09-30 23:57:10 +00006828 if (X86::isMOVHLPSMask(SVOp))
6829 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006830
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006831 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006832 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006833
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006834 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006835 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006836
Dale Johannesen0488fb62010-09-30 23:57:10 +00006837 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006838 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006839
Nate Begeman9008ca62009-04-27 18:41:29 +00006840 if (ShouldXformToMOVHLPS(SVOp) ||
6841 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6842 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006843
Evan Chengf26ffe92008-05-29 08:22:04 +00006844 if (isShift) {
6845 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006846 EVT EltVT = VT.getVectorElementType();
6847 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006848 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006849 }
Eric Christopherfd179292009-08-27 18:07:15 +00006850
Evan Cheng9eca5e82006-10-25 21:49:50 +00006851 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006852 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6853 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006854 V1IsSplat = isSplatVector(V1.getNode());
6855 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006856
Chris Lattner8a594482007-11-25 00:24:49 +00006857 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006858 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006859 Op = CommuteVectorShuffle(SVOp, DAG);
6860 SVOp = cast<ShuffleVectorSDNode>(Op);
6861 V1 = SVOp->getOperand(0);
6862 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006863 std::swap(V1IsSplat, V2IsSplat);
6864 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006865 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006866 }
6867
Nate Begeman9008ca62009-04-27 18:41:29 +00006868 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6869 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006870 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006871 return V1;
6872 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6873 // the instruction selector will not match, so get a canonical MOVL with
6874 // swapped operands to undo the commute.
6875 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006876 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006877
Craig Topper6347e862011-11-21 06:57:39 +00006878 if (X86::isUNPCKLMask(SVOp, Subtarget->hasAVX2()))
6879 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V2,
6880 DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006881
Craig Topper6347e862011-11-21 06:57:39 +00006882 if (X86::isUNPCKHMask(SVOp, Subtarget->hasAVX2()))
6883 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V2,
6884 DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006885
Evan Cheng9bbbb982006-10-25 20:48:19 +00006886 if (V2IsSplat) {
6887 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006888 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006889 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006890 SDValue NewMask = NormalizeMask(SVOp, DAG);
6891 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6892 if (NSVOp != SVOp) {
Craig Topper6347e862011-11-21 06:57:39 +00006893 if (X86::isUNPCKLMask(NSVOp, Subtarget->hasAVX2(), true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006894 return NewMask;
Craig Topper6347e862011-11-21 06:57:39 +00006895 } else if (X86::isUNPCKHMask(NSVOp, Subtarget->hasAVX2(), true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006896 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006897 }
6898 }
6899 }
6900
Evan Cheng9eca5e82006-10-25 21:49:50 +00006901 if (Commuted) {
6902 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006903 // FIXME: this seems wrong.
6904 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6905 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006906
Craig Topper6347e862011-11-21 06:57:39 +00006907 if (X86::isUNPCKLMask(NewSVOp, Subtarget->hasAVX2()))
6908 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V2, V1,
6909 DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006910
Craig Topper6347e862011-11-21 06:57:39 +00006911 if (X86::isUNPCKHMask(NewSVOp, Subtarget->hasAVX2()))
6912 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V2, V1,
6913 DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006914 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006915
Nate Begeman9008ca62009-04-27 18:41:29 +00006916 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006917 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006918 return CommuteVectorShuffle(SVOp, DAG);
6919
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006920 // The checks below are all present in isShuffleMaskLegal, but they are
6921 // inlined here right now to enable us to directly emit target specific
6922 // nodes, and remove one by one until they don't return Op anymore.
6923 SmallVector<int, 16> M;
6924 SVOp->getMask(M);
6925
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006926 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006927 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6928 X86::getShufflePALIGNRImmediate(SVOp),
6929 DAG);
6930
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006931 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6932 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006933 if (VT == MVT::v2f64)
6934 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006935 if (VT == MVT::v2i64)
6936 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6937 }
6938
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006939 if (isPSHUFHWMask(M, VT))
6940 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6941 X86::getShufflePSHUFHWImmediate(SVOp),
6942 DAG);
6943
6944 if (isPSHUFLWMask(M, VT))
6945 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6946 X86::getShufflePSHUFLWImmediate(SVOp),
6947 DAG);
6948
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006949 if (isSHUFPMask(M, VT))
6950 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6951 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006952
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006953 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006954 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V1,
6955 DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006956 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006957 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6958 DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006959
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006960 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006961 // Generate target specific nodes for 128 or 256-bit shuffles only
6962 // supported in the AVX instruction set.
6963 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006964
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006965 // Handle VMOVDDUPY permutations
6966 if (isMOVDDUPYMask(SVOp, Subtarget))
6967 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6968
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006969 // Handle VPERMILPS* permutations
6970 if (isVPERMILPSMask(M, VT, Subtarget))
6971 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6972 getShuffleVPERMILPSImmediate(SVOp), DAG);
6973
6974 // Handle VPERMILPD* permutations
6975 if (isVPERMILPDMask(M, VT, Subtarget))
6976 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6977 getShuffleVPERMILPDImmediate(SVOp), DAG);
6978
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006979 // Handle VPERM2F128 permutations
6980 if (isVPERM2F128Mask(M, VT, Subtarget))
6981 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6982 getShuffleVPERM2F128Immediate(SVOp), DAG);
6983
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006984 // Handle VSHUFPSY permutations
6985 if (isVSHUFPSYMask(M, VT, Subtarget))
6986 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6987 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6988
6989 // Handle VSHUFPDY permutations
6990 if (isVSHUFPDYMask(M, VT, Subtarget))
6991 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6992 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6993
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006994 //===--------------------------------------------------------------------===//
6995 // Since no target specific shuffle was selected for this generic one,
6996 // lower it into other known shuffles. FIXME: this isn't true yet, but
6997 // this is the plan.
6998 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006999
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007000 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7001 if (VT == MVT::v8i16) {
7002 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
7003 if (NewOp.getNode())
7004 return NewOp;
7005 }
7006
7007 if (VT == MVT::v16i8) {
7008 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7009 if (NewOp.getNode())
7010 return NewOp;
7011 }
7012
7013 // Handle all 128-bit wide vectors with 4 elements, and match them with
7014 // several different shuffle types.
7015 if (NumElems == 4 && VT.getSizeInBits() == 128)
7016 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7017
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007018 // Handle general 256-bit shuffles
7019 if (VT.is256BitVector())
7020 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7021
Dan Gohman475871a2008-07-27 21:46:04 +00007022 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007023}
7024
Dan Gohman475871a2008-07-27 21:46:04 +00007025SDValue
7026X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007027 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007028 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007029 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007030
7031 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
7032 return SDValue();
7033
Duncan Sands83ec4b62008-06-06 12:08:01 +00007034 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007035 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00007036 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007037 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00007038 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007039 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007040 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007041 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7042 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7043 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007044 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7045 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007046 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007047 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007048 Op.getOperand(0)),
7049 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007050 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00007051 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007052 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00007053 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007054 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00007055 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007056 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7057 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007058 // result has a single use which is a store or a bitcast to i32. And in
7059 // the case of a store, it's not worth it if the index is a constant 0,
7060 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007061 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007062 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007063 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007064 if ((User->getOpcode() != ISD::STORE ||
7065 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7066 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007067 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007068 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007069 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007070 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007071 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007072 Op.getOperand(0)),
7073 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007074 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Pete Coopera77214a2011-11-14 19:38:42 +00007075 } else if (VT == MVT::i32 || VT == MVT::i64) {
7076 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007077 if (isa<ConstantSDNode>(Op.getOperand(1)))
7078 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007079 }
Dan Gohman475871a2008-07-27 21:46:04 +00007080 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007081}
7082
7083
Dan Gohman475871a2008-07-27 21:46:04 +00007084SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007085X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7086 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007087 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007088 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007089
David Greene74a579d2011-02-10 16:57:36 +00007090 SDValue Vec = Op.getOperand(0);
7091 EVT VecVT = Vec.getValueType();
7092
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007093 // If this is a 256-bit vector result, first extract the 128-bit vector and
7094 // then extract the element from the 128-bit vector.
7095 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00007096 DebugLoc dl = Op.getNode()->getDebugLoc();
7097 unsigned NumElems = VecVT.getVectorNumElements();
7098 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007099 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7100
7101 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007102 bool Upper = IdxVal >= NumElems/2;
7103 Vec = Extract128BitVector(Vec,
7104 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007105
David Greene74a579d2011-02-10 16:57:36 +00007106 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007107 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00007108 }
7109
7110 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
7111
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007112 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007113 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007114 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007115 return Res;
7116 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007117
Owen Andersone50ed302009-08-10 22:56:29 +00007118 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007119 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007120 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007121 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007122 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007123 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007124 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007125 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7126 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007127 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007128 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007129 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007130 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007131 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007132 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007133 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007134 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007135 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007136 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007137 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007138 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007139 if (Idx == 0)
7140 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007141
Evan Cheng0db9fe62006-04-25 20:13:52 +00007142 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007143 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007144 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007145 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007146 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007147 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007148 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00007149 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007150 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7151 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7152 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007153 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007154 if (Idx == 0)
7155 return Op;
7156
7157 // UNPCKHPD the element to the lowest double word, then movsd.
7158 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7159 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007160 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007161 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007162 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007163 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007164 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007165 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007166 }
7167
Dan Gohman475871a2008-07-27 21:46:04 +00007168 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007169}
7170
Dan Gohman475871a2008-07-27 21:46:04 +00007171SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007172X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7173 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007174 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007175 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007176 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007177
Dan Gohman475871a2008-07-27 21:46:04 +00007178 SDValue N0 = Op.getOperand(0);
7179 SDValue N1 = Op.getOperand(1);
7180 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007181
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007182 if (VT.getSizeInBits() == 256)
7183 return SDValue();
7184
Dan Gohman8a55ce42009-09-23 21:02:20 +00007185 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007186 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007187 unsigned Opc;
7188 if (VT == MVT::v8i16)
7189 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007190 else if (VT == MVT::v16i8)
7191 Opc = X86ISD::PINSRB;
7192 else
7193 Opc = X86ISD::PINSRB;
7194
Nate Begeman14d12ca2008-02-11 04:19:36 +00007195 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7196 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007197 if (N1.getValueType() != MVT::i32)
7198 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7199 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007200 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007201 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007202 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007203 // Bits [7:6] of the constant are the source select. This will always be
7204 // zero here. The DAG Combiner may combine an extract_elt index into these
7205 // bits. For example (insert (extract, 3), 2) could be matched by putting
7206 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007207 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007208 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007209 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007210 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007211 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007212 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007213 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007214 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Pete Coopera77214a2011-11-14 19:38:42 +00007215 } else if ((EltVT == MVT::i32 || EltVT == MVT::i64) &&
7216 isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007217 // PINSR* works with constant index.
7218 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007219 }
Dan Gohman475871a2008-07-27 21:46:04 +00007220 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007221}
7222
Dan Gohman475871a2008-07-27 21:46:04 +00007223SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007224X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007225 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007226 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007227
David Greene6b381262011-02-09 15:32:06 +00007228 DebugLoc dl = Op.getDebugLoc();
7229 SDValue N0 = Op.getOperand(0);
7230 SDValue N1 = Op.getOperand(1);
7231 SDValue N2 = Op.getOperand(2);
7232
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007233 // If this is a 256-bit vector result, first extract the 128-bit vector,
7234 // insert the element into the extracted half and then place it back.
7235 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007236 if (!isa<ConstantSDNode>(N2))
7237 return SDValue();
7238
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007239 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007240 unsigned NumElems = VT.getVectorNumElements();
7241 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007242 bool Upper = IdxVal >= NumElems/2;
7243 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7244 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007245
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007246 // Insert the element into the desired half.
7247 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7248 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007249
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007250 // Insert the changed part back to the 256-bit vector
7251 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007252 }
7253
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007254 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007255 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7256
Dan Gohman8a55ce42009-09-23 21:02:20 +00007257 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007258 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007259
Dan Gohman8a55ce42009-09-23 21:02:20 +00007260 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007261 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7262 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007263 if (N1.getValueType() != MVT::i32)
7264 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7265 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007266 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007267 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007268 }
Dan Gohman475871a2008-07-27 21:46:04 +00007269 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007270}
7271
Dan Gohman475871a2008-07-27 21:46:04 +00007272SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007273X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007274 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007275 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007276 EVT OpVT = Op.getValueType();
7277
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007278 // If this is a 256-bit vector result, first insert into a 128-bit
7279 // vector and then insert into the 256-bit vector.
7280 if (OpVT.getSizeInBits() > 128) {
7281 // Insert into a 128-bit vector.
7282 EVT VT128 = EVT::getVectorVT(*Context,
7283 OpVT.getVectorElementType(),
7284 OpVT.getVectorNumElements() / 2);
7285
7286 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7287
7288 // Insert the 128-bit vector.
7289 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7290 DAG.getConstant(0, MVT::i32),
7291 DAG, dl);
7292 }
7293
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007294 if (Op.getValueType() == MVT::v1i64 &&
7295 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007296 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007297
Owen Anderson825b72b2009-08-11 20:47:22 +00007298 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007299 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7300 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007301 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007302 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007303}
7304
David Greene91585092011-01-26 15:38:49 +00007305// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7306// a simple subregister reference or explicit instructions to grab
7307// upper bits of a vector.
7308SDValue
7309X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7310 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007311 DebugLoc dl = Op.getNode()->getDebugLoc();
7312 SDValue Vec = Op.getNode()->getOperand(0);
7313 SDValue Idx = Op.getNode()->getOperand(1);
7314
7315 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7316 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7317 return Extract128BitVector(Vec, Idx, DAG, dl);
7318 }
David Greene91585092011-01-26 15:38:49 +00007319 }
7320 return SDValue();
7321}
7322
David Greenecfe33c42011-01-26 19:13:22 +00007323// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7324// simple superregister reference or explicit instructions to insert
7325// the upper bits of a vector.
7326SDValue
7327X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7328 if (Subtarget->hasAVX()) {
7329 DebugLoc dl = Op.getNode()->getDebugLoc();
7330 SDValue Vec = Op.getNode()->getOperand(0);
7331 SDValue SubVec = Op.getNode()->getOperand(1);
7332 SDValue Idx = Op.getNode()->getOperand(2);
7333
7334 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7335 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007336 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007337 }
7338 }
7339 return SDValue();
7340}
7341
Bill Wendling056292f2008-09-16 21:48:12 +00007342// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7343// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7344// one of the above mentioned nodes. It has to be wrapped because otherwise
7345// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7346// be used to form addressing mode. These wrapped nodes will be selected
7347// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007348SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007349X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007350 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007351
Chris Lattner41621a22009-06-26 19:22:52 +00007352 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7353 // global base reg.
7354 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007355 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007356 CodeModel::Model M = getTargetMachine().getCodeModel();
7357
Chris Lattner4f066492009-07-11 20:29:19 +00007358 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007359 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007360 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007361 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007362 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007363 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007364 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007365
Evan Cheng1606e8e2009-03-13 07:51:59 +00007366 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007367 CP->getAlignment(),
7368 CP->getOffset(), OpFlag);
7369 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007370 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007371 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007372 if (OpFlag) {
7373 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007374 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007375 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007376 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007377 }
7378
7379 return Result;
7380}
7381
Dan Gohmand858e902010-04-17 15:26:15 +00007382SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007383 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007384
Chris Lattner18c59872009-06-27 04:16:01 +00007385 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7386 // global base reg.
7387 unsigned char OpFlag = 0;
7388 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007389 CodeModel::Model M = getTargetMachine().getCodeModel();
7390
Chris Lattner4f066492009-07-11 20:29:19 +00007391 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007392 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007393 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007394 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007395 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007396 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007397 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007398
Chris Lattner18c59872009-06-27 04:16:01 +00007399 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7400 OpFlag);
7401 DebugLoc DL = JT->getDebugLoc();
7402 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007403
Chris Lattner18c59872009-06-27 04:16:01 +00007404 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007405 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007406 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7407 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007408 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007409 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007410
Chris Lattner18c59872009-06-27 04:16:01 +00007411 return Result;
7412}
7413
7414SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007415X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007416 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007417
Chris Lattner18c59872009-06-27 04:16:01 +00007418 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7419 // global base reg.
7420 unsigned char OpFlag = 0;
7421 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007422 CodeModel::Model M = getTargetMachine().getCodeModel();
7423
Chris Lattner4f066492009-07-11 20:29:19 +00007424 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007425 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7426 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7427 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007428 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007429 } else if (Subtarget->isPICStyleGOT()) {
7430 OpFlag = X86II::MO_GOT;
7431 } else if (Subtarget->isPICStyleStubPIC()) {
7432 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7433 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7434 OpFlag = X86II::MO_DARWIN_NONLAZY;
7435 }
Eric Christopherfd179292009-08-27 18:07:15 +00007436
Chris Lattner18c59872009-06-27 04:16:01 +00007437 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007438
Chris Lattner18c59872009-06-27 04:16:01 +00007439 DebugLoc DL = Op.getDebugLoc();
7440 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007441
7442
Chris Lattner18c59872009-06-27 04:16:01 +00007443 // With PIC, the address is actually $g + Offset.
7444 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007445 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007446 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7447 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007448 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007449 Result);
7450 }
Eric Christopherfd179292009-08-27 18:07:15 +00007451
Eli Friedman586272d2011-08-11 01:48:05 +00007452 // For symbols that require a load from a stub to get the address, emit the
7453 // load.
7454 if (isGlobalStubReference(OpFlag))
7455 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007456 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007457
Chris Lattner18c59872009-06-27 04:16:01 +00007458 return Result;
7459}
7460
Dan Gohman475871a2008-07-27 21:46:04 +00007461SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007462X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007463 // Create the TargetBlockAddressAddress node.
7464 unsigned char OpFlags =
7465 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007466 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007467 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007468 DebugLoc dl = Op.getDebugLoc();
7469 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7470 /*isTarget=*/true, OpFlags);
7471
Dan Gohmanf705adb2009-10-30 01:28:02 +00007472 if (Subtarget->isPICStyleRIPRel() &&
7473 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007474 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7475 else
7476 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007477
Dan Gohman29cbade2009-11-20 23:18:13 +00007478 // With PIC, the address is actually $g + Offset.
7479 if (isGlobalRelativeToPICBase(OpFlags)) {
7480 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7481 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7482 Result);
7483 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007484
7485 return Result;
7486}
7487
7488SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007489X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007490 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007491 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007492 // Create the TargetGlobalAddress node, folding in the constant
7493 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007494 unsigned char OpFlags =
7495 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007496 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007497 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007498 if (OpFlags == X86II::MO_NO_FLAG &&
7499 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007500 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007501 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007502 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007503 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007504 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007505 }
Eric Christopherfd179292009-08-27 18:07:15 +00007506
Chris Lattner4f066492009-07-11 20:29:19 +00007507 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007508 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007509 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7510 else
7511 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007512
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007513 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007514 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007515 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7516 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007517 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007518 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007519
Chris Lattner36c25012009-07-10 07:34:39 +00007520 // For globals that require a load from a stub to get the address, emit the
7521 // load.
7522 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007523 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007524 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007525
Dan Gohman6520e202008-10-18 02:06:02 +00007526 // If there was a non-zero offset that we didn't fold, create an explicit
7527 // addition for it.
7528 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007529 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007530 DAG.getConstant(Offset, getPointerTy()));
7531
Evan Cheng0db9fe62006-04-25 20:13:52 +00007532 return Result;
7533}
7534
Evan Chengda43bcf2008-09-24 00:05:32 +00007535SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007536X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007537 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007538 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007539 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007540}
7541
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007542static SDValue
7543GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007544 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007545 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007546 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007547 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007548 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007549 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007550 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007551 GA->getOffset(),
7552 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007553 if (InFlag) {
7554 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007555 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007556 } else {
7557 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007558 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007559 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007560
7561 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007562 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007563
Rafael Espindola15f1b662009-04-24 12:59:40 +00007564 SDValue Flag = Chain.getValue(1);
7565 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007566}
7567
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007568// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007569static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007570LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007571 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007572 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007573 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7574 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007575 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007576 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007577 InFlag = Chain.getValue(1);
7578
Chris Lattnerb903bed2009-06-26 21:20:29 +00007579 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007580}
7581
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007582// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007583static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007584LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007585 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007586 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7587 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007588}
7589
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007590// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7591// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007592static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007593 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007594 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007595 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007596
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007597 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7598 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7599 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007600
Michael J. Spencerec38de22010-10-10 22:04:20 +00007601 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007602 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007603 MachinePointerInfo(Ptr),
7604 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007605
Chris Lattnerb903bed2009-06-26 21:20:29 +00007606 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007607 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7608 // initialexec.
7609 unsigned WrapperKind = X86ISD::Wrapper;
7610 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007611 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007612 } else if (is64Bit) {
7613 assert(model == TLSModel::InitialExec);
7614 OperandFlags = X86II::MO_GOTTPOFF;
7615 WrapperKind = X86ISD::WrapperRIP;
7616 } else {
7617 assert(model == TLSModel::InitialExec);
7618 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007619 }
Eric Christopherfd179292009-08-27 18:07:15 +00007620
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007621 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7622 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007623 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007624 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007625 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007626 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007627
Rafael Espindola9a580232009-02-27 13:37:18 +00007628 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007629 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007630 MachinePointerInfo::getGOT(), false, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007631
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007632 // The address of the thread local variable is the add of the thread
7633 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007634 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007635}
7636
Dan Gohman475871a2008-07-27 21:46:04 +00007637SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007638X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007639
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007640 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007641 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007642
Eric Christopher30ef0e52010-06-03 04:07:48 +00007643 if (Subtarget->isTargetELF()) {
7644 // TODO: implement the "local dynamic" model
7645 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007646
Eric Christopher30ef0e52010-06-03 04:07:48 +00007647 // If GV is an alias then use the aliasee for determining
7648 // thread-localness.
7649 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7650 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007651
7652 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007653 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007654
Eric Christopher30ef0e52010-06-03 04:07:48 +00007655 switch (model) {
7656 case TLSModel::GeneralDynamic:
7657 case TLSModel::LocalDynamic: // not implemented
7658 if (Subtarget->is64Bit())
7659 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7660 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007661
Eric Christopher30ef0e52010-06-03 04:07:48 +00007662 case TLSModel::InitialExec:
7663 case TLSModel::LocalExec:
7664 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7665 Subtarget->is64Bit());
7666 }
7667 } else if (Subtarget->isTargetDarwin()) {
7668 // Darwin only has one model of TLS. Lower to that.
7669 unsigned char OpFlag = 0;
7670 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7671 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007672
Eric Christopher30ef0e52010-06-03 04:07:48 +00007673 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7674 // global base reg.
7675 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7676 !Subtarget->is64Bit();
7677 if (PIC32)
7678 OpFlag = X86II::MO_TLVP_PIC_BASE;
7679 else
7680 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007681 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007682 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007683 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007684 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007685 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007686
Eric Christopher30ef0e52010-06-03 04:07:48 +00007687 // With PIC32, the address is actually $g + Offset.
7688 if (PIC32)
7689 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7690 DAG.getNode(X86ISD::GlobalBaseReg,
7691 DebugLoc(), getPointerTy()),
7692 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007693
Eric Christopher30ef0e52010-06-03 04:07:48 +00007694 // Lowering the machine isd will make sure everything is in the right
7695 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007696 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007697 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007698 SDValue Args[] = { Chain, Offset };
7699 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007700
Eric Christopher30ef0e52010-06-03 04:07:48 +00007701 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7702 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7703 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007704
Eric Christopher30ef0e52010-06-03 04:07:48 +00007705 // And our return value (tls address) is in the standard call return value
7706 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007707 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007708 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7709 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007710 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007711
Eric Christopher30ef0e52010-06-03 04:07:48 +00007712 assert(false &&
7713 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007714
Torok Edwinc23197a2009-07-14 16:55:14 +00007715 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007716 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007717}
7718
Evan Cheng0db9fe62006-04-25 20:13:52 +00007719
Nadav Rotem43012222011-05-11 08:12:09 +00007720/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007721/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007722SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007723 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007724 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007725 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007726 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007727 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007728 SDValue ShOpLo = Op.getOperand(0);
7729 SDValue ShOpHi = Op.getOperand(1);
7730 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007731 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007732 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007733 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007734
Dan Gohman475871a2008-07-27 21:46:04 +00007735 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007736 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007737 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7738 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007739 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007740 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7741 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007742 }
Evan Chenge3413162006-01-09 18:33:28 +00007743
Owen Anderson825b72b2009-08-11 20:47:22 +00007744 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7745 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007746 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007747 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007748
Dan Gohman475871a2008-07-27 21:46:04 +00007749 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007750 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007751 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7752 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007753
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007754 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007755 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7756 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007757 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007758 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7759 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007760 }
7761
Dan Gohman475871a2008-07-27 21:46:04 +00007762 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007763 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007764}
Evan Chenga3195e82006-01-12 22:54:21 +00007765
Dan Gohmand858e902010-04-17 15:26:15 +00007766SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7767 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007768 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007769
Dale Johannesen0488fb62010-09-30 23:57:10 +00007770 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007771 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007772
Owen Anderson825b72b2009-08-11 20:47:22 +00007773 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007774 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007775
Eli Friedman36df4992009-05-27 00:47:34 +00007776 // These are really Legal; return the operand so the caller accepts it as
7777 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007778 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007779 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007780 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007781 Subtarget->is64Bit()) {
7782 return Op;
7783 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007784
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007785 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007786 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007787 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007788 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007789 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007790 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007791 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007792 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007793 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007794 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7795}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007796
Owen Andersone50ed302009-08-10 22:56:29 +00007797SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007798 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007799 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007800 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007801 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007802 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007803 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007804 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007805 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007806 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007807 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007808
Chris Lattner492a43e2010-09-22 01:28:21 +00007809 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007810
Stuart Hastings84be9582011-06-02 15:57:11 +00007811 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7812 MachineMemOperand *MMO;
7813 if (FI) {
7814 int SSFI = FI->getIndex();
7815 MMO =
7816 DAG.getMachineFunction()
7817 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7818 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7819 } else {
7820 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7821 StackSlot = StackSlot.getOperand(1);
7822 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007823 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007824 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7825 X86ISD::FILD, DL,
7826 Tys, Ops, array_lengthof(Ops),
7827 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007828
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007829 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007830 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007831 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007832
7833 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7834 // shouldn't be necessary except that RFP cannot be live across
7835 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007836 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007837 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7838 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007839 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007840 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007841 SDValue Ops[] = {
7842 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7843 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007844 MachineMemOperand *MMO =
7845 DAG.getMachineFunction()
7846 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007847 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007848
Chris Lattner492a43e2010-09-22 01:28:21 +00007849 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7850 Ops, array_lengthof(Ops),
7851 Op.getValueType(), MMO);
7852 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007853 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007854 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007855 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007856
Evan Cheng0db9fe62006-04-25 20:13:52 +00007857 return Result;
7858}
7859
Bill Wendling8b8a6362009-01-17 03:56:04 +00007860// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007861SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7862 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007863 // This algorithm is not obvious. Here it is in C code, more or less:
7864 /*
7865 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7866 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7867 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007868
Bill Wendling8b8a6362009-01-17 03:56:04 +00007869 // Copy ints to xmm registers.
7870 __m128i xh = _mm_cvtsi32_si128( hi );
7871 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007872
Bill Wendling8b8a6362009-01-17 03:56:04 +00007873 // Combine into low half of a single xmm register.
7874 __m128i x = _mm_unpacklo_epi32( xh, xl );
7875 __m128d d;
7876 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007877
Bill Wendling8b8a6362009-01-17 03:56:04 +00007878 // Merge in appropriate exponents to give the integer bits the right
7879 // magnitude.
7880 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007881
Bill Wendling8b8a6362009-01-17 03:56:04 +00007882 // Subtract away the biases to deal with the IEEE-754 double precision
7883 // implicit 1.
7884 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007885
Bill Wendling8b8a6362009-01-17 03:56:04 +00007886 // All conversions up to here are exact. The correctly rounded result is
7887 // calculated using the current rounding mode using the following
7888 // horizontal add.
7889 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7890 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7891 // store doesn't really need to be here (except
7892 // maybe to zero the other double)
7893 return sd;
7894 }
7895 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007896
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007897 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007898 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007899
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007900 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007901 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007902 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7903 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7904 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7905 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007906 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007907 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007908
Bill Wendling8b8a6362009-01-17 03:56:04 +00007909 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007910 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007911 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007912 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007913 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007914 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007915 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007916
Owen Anderson825b72b2009-08-11 20:47:22 +00007917 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7918 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007919 Op.getOperand(0),
7920 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007921 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7922 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007923 Op.getOperand(0),
7924 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007925 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7926 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007927 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007928 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007929 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007930 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007931 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007932 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007933 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007934 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007935
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007936 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007937 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007938 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7939 DAG.getUNDEF(MVT::v2f64), ShufMask);
7940 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7941 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007942 DAG.getIntPtrConstant(0));
7943}
7944
Bill Wendling8b8a6362009-01-17 03:56:04 +00007945// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007946SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7947 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007948 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007949 // FP constant to bias correct the final result.
7950 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007951 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007952
7953 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007954 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007955 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007956
Eli Friedmanf3704762011-08-29 21:15:46 +00007957 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007958 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7959 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007960
Owen Anderson825b72b2009-08-11 20:47:22 +00007961 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007962 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007963 DAG.getIntPtrConstant(0));
7964
7965 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007966 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007967 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007968 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007969 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007970 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007971 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007972 MVT::v2f64, Bias)));
7973 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007974 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007975 DAG.getIntPtrConstant(0));
7976
7977 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007978 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007979
7980 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007981 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007982
Owen Anderson825b72b2009-08-11 20:47:22 +00007983 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007984 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007985 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007986 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007987 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007988 }
7989
7990 // Handle final rounding.
7991 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007992}
7993
Dan Gohmand858e902010-04-17 15:26:15 +00007994SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7995 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007996 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007997 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007998
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007999 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008000 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8001 // the optimization here.
8002 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008003 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008004
Owen Andersone50ed302009-08-10 22:56:29 +00008005 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008006 EVT DstVT = Op.getValueType();
8007 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008008 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008009 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008010 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008011
8012 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008013 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008014 if (SrcVT == MVT::i32) {
8015 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8016 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8017 getPointerTy(), StackSlot, WordOff);
8018 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008019 StackSlot, MachinePointerInfo(),
8020 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008021 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008022 OffsetSlot, MachinePointerInfo(),
8023 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008024 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8025 return Fild;
8026 }
8027
8028 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8029 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008030 StackSlot, MachinePointerInfo(),
8031 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008032 // For i64 source, we need to add the appropriate power of 2 if the input
8033 // was negative. This is the same as the optimization in
8034 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8035 // we must be careful to do the computation in x87 extended precision, not
8036 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008037 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8038 MachineMemOperand *MMO =
8039 DAG.getMachineFunction()
8040 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8041 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008042
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008043 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8044 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008045 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8046 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008047
8048 APInt FF(32, 0x5F800000ULL);
8049
8050 // Check whether the sign bit is set.
8051 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8052 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8053 ISD::SETLT);
8054
8055 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8056 SDValue FudgePtr = DAG.getConstantPool(
8057 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8058 getPointerTy());
8059
8060 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8061 SDValue Zero = DAG.getIntPtrConstant(0);
8062 SDValue Four = DAG.getIntPtrConstant(4);
8063 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8064 Zero, Four);
8065 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8066
8067 // Load the value out, extending it from f32 to f80.
8068 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008069 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008070 FudgePtr, MachinePointerInfo::getConstantPool(),
8071 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008072 // Extend everything to 80 bits to force it to be done on x87.
8073 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8074 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008075}
8076
Dan Gohman475871a2008-07-27 21:46:04 +00008077std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00008078FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00008079 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008080
Owen Andersone50ed302009-08-10 22:56:29 +00008081 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008082
8083 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008084 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8085 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008086 }
8087
Owen Anderson825b72b2009-08-11 20:47:22 +00008088 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8089 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00008090 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008091
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008092 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008093 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008094 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008095 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008096 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008097 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008098 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008099 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008100
Evan Cheng87c89352007-10-15 20:11:21 +00008101 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
8102 // stack slot.
8103 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008104 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008105 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008106 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008107
Michael J. Spencerec38de22010-10-10 22:04:20 +00008108
8109
Evan Cheng0db9fe62006-04-25 20:13:52 +00008110 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00008111 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008112 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008113 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8114 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8115 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00008116 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008117
Dan Gohman475871a2008-07-27 21:46:04 +00008118 SDValue Chain = DAG.getEntryNode();
8119 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008120 EVT TheVT = Op.getOperand(0).getValueType();
8121 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008122 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008123 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008124 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008125 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008126 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008127 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008128 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008129 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008130
Chris Lattner492a43e2010-09-22 01:28:21 +00008131 MachineMemOperand *MMO =
8132 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8133 MachineMemOperand::MOLoad, MemSize, MemSize);
8134 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8135 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008136 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008137 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008138 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8139 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008140
Chris Lattner07290932010-09-22 01:05:16 +00008141 MachineMemOperand *MMO =
8142 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8143 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008144
Evan Cheng0db9fe62006-04-25 20:13:52 +00008145 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00008146 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00008147 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8148 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00008149
Chris Lattner27a6c732007-11-24 07:07:01 +00008150 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008151}
8152
Dan Gohmand858e902010-04-17 15:26:15 +00008153SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8154 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008155 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008156 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008157
Eli Friedman948e95a2009-05-23 09:59:16 +00008158 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00008159 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008160 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8161 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008162
Chris Lattner27a6c732007-11-24 07:07:01 +00008163 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008164 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008165 FIST, StackSlot, MachinePointerInfo(),
8166 false, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00008167}
8168
Dan Gohmand858e902010-04-17 15:26:15 +00008169SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8170 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00008171 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
8172 SDValue FIST = Vals.first, StackSlot = Vals.second;
8173 assert(FIST.getNode() && "Unexpected failure");
8174
8175 // Load the result.
8176 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008177 FIST, StackSlot, MachinePointerInfo(),
8178 false, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00008179}
8180
Dan Gohmand858e902010-04-17 15:26:15 +00008181SDValue X86TargetLowering::LowerFABS(SDValue Op,
8182 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008183 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008184 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008185 EVT VT = Op.getValueType();
8186 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008187 if (VT.isVector())
8188 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008189 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008190 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008191 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00008192 CV.push_back(C);
8193 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008194 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008195 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00008196 CV.push_back(C);
8197 CV.push_back(C);
8198 CV.push_back(C);
8199 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008200 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008201 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008202 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008203 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008204 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008205 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008206 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008207}
8208
Dan Gohmand858e902010-04-17 15:26:15 +00008209SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008210 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008211 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008212 EVT VT = Op.getValueType();
8213 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008214 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008215 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008216 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008217 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008218 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008219 CV.push_back(C);
8220 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008221 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008222 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008223 CV.push_back(C);
8224 CV.push_back(C);
8225 CV.push_back(C);
8226 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008227 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008228 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008229 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008230 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008231 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008232 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008233 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008234 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008235 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008236 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008237 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008238 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008239 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008240 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008241 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008242}
8243
Dan Gohmand858e902010-04-17 15:26:15 +00008244SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008245 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008246 SDValue Op0 = Op.getOperand(0);
8247 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008248 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008249 EVT VT = Op.getValueType();
8250 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008251
8252 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008253 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008254 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008255 SrcVT = VT;
8256 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008257 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008258 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008259 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008260 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008261 }
8262
8263 // At this point the operands and the result should have the same
8264 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008265
Evan Cheng68c47cb2007-01-05 07:55:56 +00008266 // First get the sign bit of second operand.
8267 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008268 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008269 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8270 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008271 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008272 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8273 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8274 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8275 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008276 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008277 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008278 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008279 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008280 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008281 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008282 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008283
8284 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008285 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008286 // Op0 is MVT::f32, Op1 is MVT::f64.
8287 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8288 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8289 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008290 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008291 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008292 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008293 }
8294
Evan Cheng73d6cf12007-01-05 21:37:56 +00008295 // Clear first operand sign bit.
8296 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008297 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008298 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8299 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008300 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008301 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8302 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8303 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8304 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008305 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008306 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008307 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008308 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008309 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008310 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008311 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008312
8313 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008314 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008315}
8316
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008317SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8318 SDValue N0 = Op.getOperand(0);
8319 DebugLoc dl = Op.getDebugLoc();
8320 EVT VT = Op.getValueType();
8321
8322 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8323 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8324 DAG.getConstant(1, VT));
8325 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8326}
8327
Dan Gohman076aee32009-03-04 19:44:21 +00008328/// Emit nodes that will be selected as "test Op0,Op0", or something
8329/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008330SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008331 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008332 DebugLoc dl = Op.getDebugLoc();
8333
Dan Gohman31125812009-03-07 01:58:32 +00008334 // CF and OF aren't always set the way we want. Determine which
8335 // of these we need.
8336 bool NeedCF = false;
8337 bool NeedOF = false;
8338 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008339 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008340 case X86::COND_A: case X86::COND_AE:
8341 case X86::COND_B: case X86::COND_BE:
8342 NeedCF = true;
8343 break;
8344 case X86::COND_G: case X86::COND_GE:
8345 case X86::COND_L: case X86::COND_LE:
8346 case X86::COND_O: case X86::COND_NO:
8347 NeedOF = true;
8348 break;
Dan Gohman31125812009-03-07 01:58:32 +00008349 }
8350
Dan Gohman076aee32009-03-04 19:44:21 +00008351 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008352 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8353 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008354 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8355 // Emit a CMP with 0, which is the TEST pattern.
8356 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8357 DAG.getConstant(0, Op.getValueType()));
8358
8359 unsigned Opcode = 0;
8360 unsigned NumOperands = 0;
8361 switch (Op.getNode()->getOpcode()) {
8362 case ISD::ADD:
8363 // Due to an isel shortcoming, be conservative if this add is likely to be
8364 // selected as part of a load-modify-store instruction. When the root node
8365 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8366 // uses of other nodes in the match, such as the ADD in this case. This
8367 // leads to the ADD being left around and reselected, with the result being
8368 // two adds in the output. Alas, even if none our users are stores, that
8369 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8370 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8371 // climbing the DAG back to the root, and it doesn't seem to be worth the
8372 // effort.
8373 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008374 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8375 if (UI->getOpcode() != ISD::CopyToReg &&
8376 UI->getOpcode() != ISD::SETCC &&
8377 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008378 goto default_case;
8379
8380 if (ConstantSDNode *C =
8381 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8382 // An add of one will be selected as an INC.
8383 if (C->getAPIntValue() == 1) {
8384 Opcode = X86ISD::INC;
8385 NumOperands = 1;
8386 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008387 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008388
8389 // An add of negative one (subtract of one) will be selected as a DEC.
8390 if (C->getAPIntValue().isAllOnesValue()) {
8391 Opcode = X86ISD::DEC;
8392 NumOperands = 1;
8393 break;
8394 }
Dan Gohman076aee32009-03-04 19:44:21 +00008395 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008396
8397 // Otherwise use a regular EFLAGS-setting add.
8398 Opcode = X86ISD::ADD;
8399 NumOperands = 2;
8400 break;
8401 case ISD::AND: {
8402 // If the primary and result isn't used, don't bother using X86ISD::AND,
8403 // because a TEST instruction will be better.
8404 bool NonFlagUse = false;
8405 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8406 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8407 SDNode *User = *UI;
8408 unsigned UOpNo = UI.getOperandNo();
8409 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8410 // Look pass truncate.
8411 UOpNo = User->use_begin().getOperandNo();
8412 User = *User->use_begin();
8413 }
8414
8415 if (User->getOpcode() != ISD::BRCOND &&
8416 User->getOpcode() != ISD::SETCC &&
8417 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8418 NonFlagUse = true;
8419 break;
8420 }
Dan Gohman076aee32009-03-04 19:44:21 +00008421 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008422
8423 if (!NonFlagUse)
8424 break;
8425 }
8426 // FALL THROUGH
8427 case ISD::SUB:
8428 case ISD::OR:
8429 case ISD::XOR:
8430 // Due to the ISEL shortcoming noted above, be conservative if this op is
8431 // likely to be selected as part of a load-modify-store instruction.
8432 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8433 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8434 if (UI->getOpcode() == ISD::STORE)
8435 goto default_case;
8436
8437 // Otherwise use a regular EFLAGS-setting instruction.
8438 switch (Op.getNode()->getOpcode()) {
8439 default: llvm_unreachable("unexpected operator!");
8440 case ISD::SUB: Opcode = X86ISD::SUB; break;
8441 case ISD::OR: Opcode = X86ISD::OR; break;
8442 case ISD::XOR: Opcode = X86ISD::XOR; break;
8443 case ISD::AND: Opcode = X86ISD::AND; break;
8444 }
8445
8446 NumOperands = 2;
8447 break;
8448 case X86ISD::ADD:
8449 case X86ISD::SUB:
8450 case X86ISD::INC:
8451 case X86ISD::DEC:
8452 case X86ISD::OR:
8453 case X86ISD::XOR:
8454 case X86ISD::AND:
8455 return SDValue(Op.getNode(), 1);
8456 default:
8457 default_case:
8458 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008459 }
8460
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008461 if (Opcode == 0)
8462 // Emit a CMP with 0, which is the TEST pattern.
8463 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8464 DAG.getConstant(0, Op.getValueType()));
8465
8466 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8467 SmallVector<SDValue, 4> Ops;
8468 for (unsigned i = 0; i != NumOperands; ++i)
8469 Ops.push_back(Op.getOperand(i));
8470
8471 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8472 DAG.ReplaceAllUsesWith(Op, New);
8473 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008474}
8475
8476/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8477/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008478SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008479 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008480 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8481 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008482 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008483
8484 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008485 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008486}
8487
Evan Chengd40d03e2010-01-06 19:38:29 +00008488/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8489/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008490SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8491 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008492 SDValue Op0 = And.getOperand(0);
8493 SDValue Op1 = And.getOperand(1);
8494 if (Op0.getOpcode() == ISD::TRUNCATE)
8495 Op0 = Op0.getOperand(0);
8496 if (Op1.getOpcode() == ISD::TRUNCATE)
8497 Op1 = Op1.getOperand(0);
8498
Evan Chengd40d03e2010-01-06 19:38:29 +00008499 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008500 if (Op1.getOpcode() == ISD::SHL)
8501 std::swap(Op0, Op1);
8502 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008503 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8504 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008505 // If we looked past a truncate, check that it's only truncating away
8506 // known zeros.
8507 unsigned BitWidth = Op0.getValueSizeInBits();
8508 unsigned AndBitWidth = And.getValueSizeInBits();
8509 if (BitWidth > AndBitWidth) {
8510 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8511 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8512 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8513 return SDValue();
8514 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008515 LHS = Op1;
8516 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008517 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008518 } else if (Op1.getOpcode() == ISD::Constant) {
8519 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8520 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008521 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8522 LHS = AndLHS.getOperand(0);
8523 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008524 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008525 }
Evan Cheng0488db92007-09-25 01:57:46 +00008526
Evan Chengd40d03e2010-01-06 19:38:29 +00008527 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008528 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008529 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008530 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008531 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008532 // Also promote i16 to i32 for performance / code size reason.
8533 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008534 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008535 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008536
Evan Chengd40d03e2010-01-06 19:38:29 +00008537 // If the operand types disagree, extend the shift amount to match. Since
8538 // BT ignores high bits (like shifts) we can use anyextend.
8539 if (LHS.getValueType() != RHS.getValueType())
8540 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008541
Evan Chengd40d03e2010-01-06 19:38:29 +00008542 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8543 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8544 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8545 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008546 }
8547
Evan Cheng54de3ea2010-01-05 06:52:31 +00008548 return SDValue();
8549}
8550
Dan Gohmand858e902010-04-17 15:26:15 +00008551SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008552
8553 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8554
Evan Cheng54de3ea2010-01-05 06:52:31 +00008555 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8556 SDValue Op0 = Op.getOperand(0);
8557 SDValue Op1 = Op.getOperand(1);
8558 DebugLoc dl = Op.getDebugLoc();
8559 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8560
8561 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008562 // Lower (X & (1 << N)) == 0 to BT(X, N).
8563 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8564 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008565 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008566 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008567 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008568 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8569 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8570 if (NewSetCC.getNode())
8571 return NewSetCC;
8572 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008573
Chris Lattner481eebc2010-12-19 21:23:48 +00008574 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8575 // these.
8576 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008577 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008578 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8579 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008580
Chris Lattner481eebc2010-12-19 21:23:48 +00008581 // If the input is a setcc, then reuse the input setcc or use a new one with
8582 // the inverted condition.
8583 if (Op0.getOpcode() == X86ISD::SETCC) {
8584 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8585 bool Invert = (CC == ISD::SETNE) ^
8586 cast<ConstantSDNode>(Op1)->isNullValue();
8587 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008588
Evan Cheng2c755ba2010-02-27 07:36:59 +00008589 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008590 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8591 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8592 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008593 }
8594
Evan Chenge5b51ac2010-04-17 06:13:15 +00008595 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008596 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008597 if (X86CC == X86::COND_INVALID)
8598 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008599
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008600 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008601 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008602 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008603}
8604
Craig Topper89af15e2011-09-18 08:03:58 +00008605// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008606// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008607static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008608 EVT VT = Op.getValueType();
8609
Duncan Sands28b77e92011-09-06 19:07:46 +00008610 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008611 "Unsupported value type for operation");
8612
8613 int NumElems = VT.getVectorNumElements();
8614 DebugLoc dl = Op.getDebugLoc();
8615 SDValue CC = Op.getOperand(2);
8616 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8617 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8618
8619 // Extract the LHS vectors
8620 SDValue LHS = Op.getOperand(0);
8621 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8622 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8623
8624 // Extract the RHS vectors
8625 SDValue RHS = Op.getOperand(1);
8626 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8627 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8628
8629 // Issue the operation on the smaller types and concatenate the result back
8630 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8631 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8632 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8633 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8634 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8635}
8636
8637
Dan Gohmand858e902010-04-17 15:26:15 +00008638SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008639 SDValue Cond;
8640 SDValue Op0 = Op.getOperand(0);
8641 SDValue Op1 = Op.getOperand(1);
8642 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008643 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008644 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8645 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008646 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008647
8648 if (isFP) {
8649 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008650 EVT EltVT = Op0.getValueType().getVectorElementType();
8651 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8652
8653 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008654 bool Swap = false;
8655
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008656 // SSE Condition code mapping:
8657 // 0 - EQ
8658 // 1 - LT
8659 // 2 - LE
8660 // 3 - UNORD
8661 // 4 - NEQ
8662 // 5 - NLT
8663 // 6 - NLE
8664 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008665 switch (SetCCOpcode) {
8666 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008667 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008668 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008669 case ISD::SETOGT:
8670 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008671 case ISD::SETLT:
8672 case ISD::SETOLT: SSECC = 1; break;
8673 case ISD::SETOGE:
8674 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008675 case ISD::SETLE:
8676 case ISD::SETOLE: SSECC = 2; break;
8677 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008678 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008679 case ISD::SETNE: SSECC = 4; break;
8680 case ISD::SETULE: Swap = true;
8681 case ISD::SETUGE: SSECC = 5; break;
8682 case ISD::SETULT: Swap = true;
8683 case ISD::SETUGT: SSECC = 6; break;
8684 case ISD::SETO: SSECC = 7; break;
8685 }
8686 if (Swap)
8687 std::swap(Op0, Op1);
8688
Nate Begemanfb8ead02008-07-25 19:05:58 +00008689 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008690 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008691 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008692 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008693 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8694 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008695 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Craig Topper0a150352011-11-09 08:06:13 +00008696 } else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008697 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008698 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8699 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008700 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008701 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008702 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008703 }
8704 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008705 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008706 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008707
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008708 // Break 256-bit integer vector compare into smaller ones.
Craig Topper0a150352011-11-09 08:06:13 +00008709 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008710 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008711
Nate Begeman30a0de92008-07-17 16:51:19 +00008712 // We are handling one of the integer comparisons here. Since SSE only has
8713 // GT and EQ comparisons for integer, swapping operands and multiple
8714 // operations may be required for some comparisons.
8715 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8716 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008717
Craig Topper0a150352011-11-09 08:06:13 +00008718 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008719 default: break;
Craig Topper0a150352011-11-09 08:06:13 +00008720 case MVT::i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
8721 case MVT::i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
8722 case MVT::i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8723 case MVT::i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008724 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008725
Nate Begeman30a0de92008-07-17 16:51:19 +00008726 switch (SetCCOpcode) {
8727 default: break;
8728 case ISD::SETNE: Invert = true;
8729 case ISD::SETEQ: Opc = EQOpc; break;
8730 case ISD::SETLT: Swap = true;
8731 case ISD::SETGT: Opc = GTOpc; break;
8732 case ISD::SETGE: Swap = true;
8733 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8734 case ISD::SETULT: Swap = true;
8735 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8736 case ISD::SETUGE: Swap = true;
8737 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8738 }
8739 if (Swap)
8740 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008741
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008742 // Check that the operation in question is available (most are plain SSE2,
8743 // but PCMPGTQ and PCMPEQQ have different requirements).
8744 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42() && !Subtarget->hasAVX())
8745 return SDValue();
8746 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41() && !Subtarget->hasAVX())
8747 return SDValue();
8748
Nate Begeman30a0de92008-07-17 16:51:19 +00008749 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8750 // bits of the inputs before performing those operations.
8751 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008752 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008753 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8754 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008755 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008756 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8757 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008758 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8759 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008760 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008761
Dale Johannesenace16102009-02-03 19:33:06 +00008762 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008763
8764 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008765 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008766 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008767
Nate Begeman30a0de92008-07-17 16:51:19 +00008768 return Result;
8769}
Evan Cheng0488db92007-09-25 01:57:46 +00008770
Evan Cheng370e5342008-12-03 08:38:43 +00008771// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008772static bool isX86LogicalCmp(SDValue Op) {
8773 unsigned Opc = Op.getNode()->getOpcode();
8774 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8775 return true;
8776 if (Op.getResNo() == 1 &&
8777 (Opc == X86ISD::ADD ||
8778 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008779 Opc == X86ISD::ADC ||
8780 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008781 Opc == X86ISD::SMUL ||
8782 Opc == X86ISD::UMUL ||
8783 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008784 Opc == X86ISD::DEC ||
8785 Opc == X86ISD::OR ||
8786 Opc == X86ISD::XOR ||
8787 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008788 return true;
8789
Chris Lattner9637d5b2010-12-05 07:49:54 +00008790 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8791 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008792
Dan Gohman076aee32009-03-04 19:44:21 +00008793 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008794}
8795
Chris Lattnera2b56002010-12-05 01:23:24 +00008796static bool isZero(SDValue V) {
8797 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8798 return C && C->isNullValue();
8799}
8800
Chris Lattner96908b12010-12-05 02:00:51 +00008801static bool isAllOnes(SDValue V) {
8802 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8803 return C && C->isAllOnesValue();
8804}
8805
Dan Gohmand858e902010-04-17 15:26:15 +00008806SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008807 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008808 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008809 SDValue Op1 = Op.getOperand(1);
8810 SDValue Op2 = Op.getOperand(2);
8811 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008812 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008813
Dan Gohman1a492952009-10-20 16:22:37 +00008814 if (Cond.getOpcode() == ISD::SETCC) {
8815 SDValue NewCond = LowerSETCC(Cond, DAG);
8816 if (NewCond.getNode())
8817 Cond = NewCond;
8818 }
Evan Cheng734503b2006-09-11 02:19:56 +00008819
Chris Lattnera2b56002010-12-05 01:23:24 +00008820 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008821 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008822 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008823 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008824 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008825 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8826 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008827 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008828
Chris Lattnera2b56002010-12-05 01:23:24 +00008829 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008830
8831 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008832 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8833 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008834
8835 SDValue CmpOp0 = Cmp.getOperand(0);
8836 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8837 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008838
Chris Lattner96908b12010-12-05 02:00:51 +00008839 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008840 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8841 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008842
Chris Lattner96908b12010-12-05 02:00:51 +00008843 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8844 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008845
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008846 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008847 if (N2C == 0 || !N2C->isNullValue())
8848 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8849 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008850 }
8851 }
8852
Chris Lattnera2b56002010-12-05 01:23:24 +00008853 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008854 if (Cond.getOpcode() == ISD::AND &&
8855 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8856 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008857 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008858 Cond = Cond.getOperand(0);
8859 }
8860
Evan Cheng3f41d662007-10-08 22:16:29 +00008861 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8862 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008863 unsigned CondOpcode = Cond.getOpcode();
8864 if (CondOpcode == X86ISD::SETCC ||
8865 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008866 CC = Cond.getOperand(0);
8867
Dan Gohman475871a2008-07-27 21:46:04 +00008868 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008869 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008870 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008871
Evan Cheng3f41d662007-10-08 22:16:29 +00008872 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008873 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008874 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008875 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008876
Chris Lattnerd1980a52009-03-12 06:52:53 +00008877 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8878 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008879 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008880 addTest = false;
8881 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008882 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8883 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8884 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8885 Cond.getOperand(0).getValueType() != MVT::i8)) {
8886 SDValue LHS = Cond.getOperand(0);
8887 SDValue RHS = Cond.getOperand(1);
8888 unsigned X86Opcode;
8889 unsigned X86Cond;
8890 SDVTList VTs;
8891 switch (CondOpcode) {
8892 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8893 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8894 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8895 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8896 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8897 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8898 default: llvm_unreachable("unexpected overflowing operator");
8899 }
8900 if (CondOpcode == ISD::UMULO)
8901 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8902 MVT::i32);
8903 else
8904 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8905
8906 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8907
8908 if (CondOpcode == ISD::UMULO)
8909 Cond = X86Op.getValue(2);
8910 else
8911 Cond = X86Op.getValue(1);
8912
8913 CC = DAG.getConstant(X86Cond, MVT::i8);
8914 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008915 }
8916
8917 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008918 // Look pass the truncate.
8919 if (Cond.getOpcode() == ISD::TRUNCATE)
8920 Cond = Cond.getOperand(0);
8921
8922 // We know the result of AND is compared against zero. Try to match
8923 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008924 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008925 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008926 if (NewSetCC.getNode()) {
8927 CC = NewSetCC.getOperand(0);
8928 Cond = NewSetCC.getOperand(1);
8929 addTest = false;
8930 }
8931 }
8932 }
8933
8934 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008935 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008936 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008937 }
8938
Benjamin Kramere915ff32010-12-22 23:09:28 +00008939 // a < b ? -1 : 0 -> RES = ~setcc_carry
8940 // a < b ? 0 : -1 -> RES = setcc_carry
8941 // a >= b ? -1 : 0 -> RES = setcc_carry
8942 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8943 if (Cond.getOpcode() == X86ISD::CMP) {
8944 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8945
8946 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8947 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8948 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8949 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8950 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8951 return DAG.getNOT(DL, Res, Res.getValueType());
8952 return Res;
8953 }
8954 }
8955
Evan Cheng0488db92007-09-25 01:57:46 +00008956 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8957 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008958 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008959 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008960 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008961}
8962
Evan Cheng370e5342008-12-03 08:38:43 +00008963// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8964// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8965// from the AND / OR.
8966static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8967 Opc = Op.getOpcode();
8968 if (Opc != ISD::OR && Opc != ISD::AND)
8969 return false;
8970 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8971 Op.getOperand(0).hasOneUse() &&
8972 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8973 Op.getOperand(1).hasOneUse());
8974}
8975
Evan Cheng961d6d42009-02-02 08:19:07 +00008976// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8977// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008978static bool isXor1OfSetCC(SDValue Op) {
8979 if (Op.getOpcode() != ISD::XOR)
8980 return false;
8981 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8982 if (N1C && N1C->getAPIntValue() == 1) {
8983 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8984 Op.getOperand(0).hasOneUse();
8985 }
8986 return false;
8987}
8988
Dan Gohmand858e902010-04-17 15:26:15 +00008989SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008990 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008991 SDValue Chain = Op.getOperand(0);
8992 SDValue Cond = Op.getOperand(1);
8993 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008994 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008995 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008996 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008997
Dan Gohman1a492952009-10-20 16:22:37 +00008998 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008999 // Check for setcc([su]{add,sub,mul}o == 0).
9000 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9001 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9002 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9003 Cond.getOperand(0).getResNo() == 1 &&
9004 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9005 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9006 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9007 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9008 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9009 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9010 Inverted = true;
9011 Cond = Cond.getOperand(0);
9012 } else {
9013 SDValue NewCond = LowerSETCC(Cond, DAG);
9014 if (NewCond.getNode())
9015 Cond = NewCond;
9016 }
Dan Gohman1a492952009-10-20 16:22:37 +00009017 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009018#if 0
9019 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009020 else if (Cond.getOpcode() == X86ISD::ADD ||
9021 Cond.getOpcode() == X86ISD::SUB ||
9022 Cond.getOpcode() == X86ISD::SMUL ||
9023 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009024 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009025#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009026
Evan Chengad9c0a32009-12-15 00:53:42 +00009027 // Look pass (and (setcc_carry (cmp ...)), 1).
9028 if (Cond.getOpcode() == ISD::AND &&
9029 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9030 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009031 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009032 Cond = Cond.getOperand(0);
9033 }
9034
Evan Cheng3f41d662007-10-08 22:16:29 +00009035 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9036 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009037 unsigned CondOpcode = Cond.getOpcode();
9038 if (CondOpcode == X86ISD::SETCC ||
9039 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009040 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009041
Dan Gohman475871a2008-07-27 21:46:04 +00009042 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009043 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009044 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009045 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009046 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009047 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009048 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009049 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009050 default: break;
9051 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009052 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009053 // These can only come from an arithmetic instruction with overflow,
9054 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009055 Cond = Cond.getNode()->getOperand(1);
9056 addTest = false;
9057 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009058 }
Evan Cheng0488db92007-09-25 01:57:46 +00009059 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009060 }
9061 CondOpcode = Cond.getOpcode();
9062 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9063 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9064 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9065 Cond.getOperand(0).getValueType() != MVT::i8)) {
9066 SDValue LHS = Cond.getOperand(0);
9067 SDValue RHS = Cond.getOperand(1);
9068 unsigned X86Opcode;
9069 unsigned X86Cond;
9070 SDVTList VTs;
9071 switch (CondOpcode) {
9072 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9073 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9074 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9075 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9076 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9077 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9078 default: llvm_unreachable("unexpected overflowing operator");
9079 }
9080 if (Inverted)
9081 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9082 if (CondOpcode == ISD::UMULO)
9083 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9084 MVT::i32);
9085 else
9086 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9087
9088 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9089
9090 if (CondOpcode == ISD::UMULO)
9091 Cond = X86Op.getValue(2);
9092 else
9093 Cond = X86Op.getValue(1);
9094
9095 CC = DAG.getConstant(X86Cond, MVT::i8);
9096 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009097 } else {
9098 unsigned CondOpc;
9099 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9100 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009101 if (CondOpc == ISD::OR) {
9102 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9103 // two branches instead of an explicit OR instruction with a
9104 // separate test.
9105 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009106 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009107 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009108 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009109 Chain, Dest, CC, Cmp);
9110 CC = Cond.getOperand(1).getOperand(0);
9111 Cond = Cmp;
9112 addTest = false;
9113 }
9114 } else { // ISD::AND
9115 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9116 // two branches instead of an explicit AND instruction with a
9117 // separate test. However, we only do this if this block doesn't
9118 // have a fall-through edge, because this requires an explicit
9119 // jmp when the condition is false.
9120 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009121 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009122 Op.getNode()->hasOneUse()) {
9123 X86::CondCode CCode =
9124 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9125 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009126 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009127 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009128 // Look for an unconditional branch following this conditional branch.
9129 // We need this because we need to reverse the successors in order
9130 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009131 if (User->getOpcode() == ISD::BR) {
9132 SDValue FalseBB = User->getOperand(1);
9133 SDNode *NewBR =
9134 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009135 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009136 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009137 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009138
Dale Johannesene4d209d2009-02-03 20:21:25 +00009139 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009140 Chain, Dest, CC, Cmp);
9141 X86::CondCode CCode =
9142 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9143 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009144 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009145 Cond = Cmp;
9146 addTest = false;
9147 }
9148 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009149 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009150 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9151 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9152 // It should be transformed during dag combiner except when the condition
9153 // is set by a arithmetics with overflow node.
9154 X86::CondCode CCode =
9155 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9156 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009157 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009158 Cond = Cond.getOperand(0).getOperand(1);
9159 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009160 } else if (Cond.getOpcode() == ISD::SETCC &&
9161 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9162 // For FCMP_OEQ, we can emit
9163 // two branches instead of an explicit AND instruction with a
9164 // separate test. However, we only do this if this block doesn't
9165 // have a fall-through edge, because this requires an explicit
9166 // jmp when the condition is false.
9167 if (Op.getNode()->hasOneUse()) {
9168 SDNode *User = *Op.getNode()->use_begin();
9169 // Look for an unconditional branch following this conditional branch.
9170 // We need this because we need to reverse the successors in order
9171 // to implement FCMP_OEQ.
9172 if (User->getOpcode() == ISD::BR) {
9173 SDValue FalseBB = User->getOperand(1);
9174 SDNode *NewBR =
9175 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9176 assert(NewBR == User);
9177 (void)NewBR;
9178 Dest = FalseBB;
9179
9180 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9181 Cond.getOperand(0), Cond.getOperand(1));
9182 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9183 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9184 Chain, Dest, CC, Cmp);
9185 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9186 Cond = Cmp;
9187 addTest = false;
9188 }
9189 }
9190 } else if (Cond.getOpcode() == ISD::SETCC &&
9191 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9192 // For FCMP_UNE, we can emit
9193 // two branches instead of an explicit AND instruction with a
9194 // separate test. However, we only do this if this block doesn't
9195 // have a fall-through edge, because this requires an explicit
9196 // jmp when the condition is false.
9197 if (Op.getNode()->hasOneUse()) {
9198 SDNode *User = *Op.getNode()->use_begin();
9199 // Look for an unconditional branch following this conditional branch.
9200 // We need this because we need to reverse the successors in order
9201 // to implement FCMP_UNE.
9202 if (User->getOpcode() == ISD::BR) {
9203 SDValue FalseBB = User->getOperand(1);
9204 SDNode *NewBR =
9205 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9206 assert(NewBR == User);
9207 (void)NewBR;
9208
9209 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9210 Cond.getOperand(0), Cond.getOperand(1));
9211 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9212 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9213 Chain, Dest, CC, Cmp);
9214 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9215 Cond = Cmp;
9216 addTest = false;
9217 Dest = FalseBB;
9218 }
9219 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009220 }
Evan Cheng0488db92007-09-25 01:57:46 +00009221 }
9222
9223 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009224 // Look pass the truncate.
9225 if (Cond.getOpcode() == ISD::TRUNCATE)
9226 Cond = Cond.getOperand(0);
9227
9228 // We know the result of AND is compared against zero. Try to match
9229 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009230 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009231 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9232 if (NewSetCC.getNode()) {
9233 CC = NewSetCC.getOperand(0);
9234 Cond = NewSetCC.getOperand(1);
9235 addTest = false;
9236 }
9237 }
9238 }
9239
9240 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009241 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009242 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009243 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00009244 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009245 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009246}
9247
Anton Korobeynikove060b532007-04-17 19:34:00 +00009248
9249// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9250// Calls to _alloca is needed to probe the stack when allocating more than 4k
9251// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9252// that the guard pages used by the OS virtual memory manager are allocated in
9253// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009254SDValue
9255X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009256 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009257 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
9258 EnableSegmentedStacks) &&
9259 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009260 "are being used");
9261 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009262 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009263
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009264 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009265 SDValue Chain = Op.getOperand(0);
9266 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009267 // FIXME: Ensure alignment here
9268
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009269 bool Is64Bit = Subtarget->is64Bit();
9270 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009271
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009272 if (EnableSegmentedStacks) {
9273 MachineFunction &MF = DAG.getMachineFunction();
9274 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009275
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009276 if (Is64Bit) {
9277 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009278 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009279 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009280
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009281 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
9282 I != E; I++)
9283 if (I->hasNestAttr())
9284 report_fatal_error("Cannot use segmented stacks with functions that "
9285 "have nested arguments.");
9286 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009287
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009288 const TargetRegisterClass *AddrRegClass =
9289 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9290 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9291 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9292 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9293 DAG.getRegister(Vreg, SPTy));
9294 SDValue Ops1[2] = { Value, Chain };
9295 return DAG.getMergeValues(Ops1, 2, dl);
9296 } else {
9297 SDValue Flag;
9298 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009299
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009300 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9301 Flag = Chain.getValue(1);
9302 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009303
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009304 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9305 Flag = Chain.getValue(1);
9306
9307 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9308
9309 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9310 return DAG.getMergeValues(Ops1, 2, dl);
9311 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009312}
9313
Dan Gohmand858e902010-04-17 15:26:15 +00009314SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009315 MachineFunction &MF = DAG.getMachineFunction();
9316 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9317
Dan Gohman69de1932008-02-06 22:27:42 +00009318 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009319 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009320
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009321 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009322 // vastart just stores the address of the VarArgsFrameIndex slot into the
9323 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009324 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9325 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009326 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9327 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009328 }
9329
9330 // __va_list_tag:
9331 // gp_offset (0 - 6 * 8)
9332 // fp_offset (48 - 48 + 8 * 16)
9333 // overflow_arg_area (point to parameters coming in memory).
9334 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009335 SmallVector<SDValue, 8> MemOps;
9336 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009337 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009338 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009339 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9340 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009341 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009342 MemOps.push_back(Store);
9343
9344 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009345 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009346 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009347 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009348 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9349 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009350 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009351 MemOps.push_back(Store);
9352
9353 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009354 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009355 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009356 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9357 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009358 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9359 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009360 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009361 MemOps.push_back(Store);
9362
9363 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009364 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009365 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009366 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9367 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009368 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9369 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009370 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009371 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009372 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009373}
9374
Dan Gohmand858e902010-04-17 15:26:15 +00009375SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009376 assert(Subtarget->is64Bit() &&
9377 "LowerVAARG only handles 64-bit va_arg!");
9378 assert((Subtarget->isTargetLinux() ||
9379 Subtarget->isTargetDarwin()) &&
9380 "Unhandled target in LowerVAARG");
9381 assert(Op.getNode()->getNumOperands() == 4);
9382 SDValue Chain = Op.getOperand(0);
9383 SDValue SrcPtr = Op.getOperand(1);
9384 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9385 unsigned Align = Op.getConstantOperandVal(3);
9386 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009387
Dan Gohman320afb82010-10-12 18:00:49 +00009388 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009389 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009390 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9391 uint8_t ArgMode;
9392
9393 // Decide which area this value should be read from.
9394 // TODO: Implement the AMD64 ABI in its entirety. This simple
9395 // selection mechanism works only for the basic types.
9396 if (ArgVT == MVT::f80) {
9397 llvm_unreachable("va_arg for f80 not yet implemented");
9398 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9399 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9400 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9401 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9402 } else {
9403 llvm_unreachable("Unhandled argument type in LowerVAARG");
9404 }
9405
9406 if (ArgMode == 2) {
9407 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009408 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009409 !(DAG.getMachineFunction()
9410 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009411 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009412 }
9413
9414 // Insert VAARG_64 node into the DAG
9415 // VAARG_64 returns two values: Variable Argument Address, Chain
9416 SmallVector<SDValue, 11> InstOps;
9417 InstOps.push_back(Chain);
9418 InstOps.push_back(SrcPtr);
9419 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9420 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9421 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9422 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9423 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9424 VTs, &InstOps[0], InstOps.size(),
9425 MVT::i64,
9426 MachinePointerInfo(SV),
9427 /*Align=*/0,
9428 /*Volatile=*/false,
9429 /*ReadMem=*/true,
9430 /*WriteMem=*/true);
9431 Chain = VAARG.getValue(1);
9432
9433 // Load the next argument and return it
9434 return DAG.getLoad(ArgVT, dl,
9435 Chain,
9436 VAARG,
9437 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009438 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009439}
9440
Dan Gohmand858e902010-04-17 15:26:15 +00009441SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009442 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009443 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009444 SDValue Chain = Op.getOperand(0);
9445 SDValue DstPtr = Op.getOperand(1);
9446 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009447 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9448 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009449 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009450
Chris Lattnere72f2022010-09-21 05:40:29 +00009451 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009452 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009453 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009454 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009455}
9456
Dan Gohman475871a2008-07-27 21:46:04 +00009457SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009458X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009459 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009460 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009461 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009462 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009463 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009464 case Intrinsic::x86_sse_comieq_ss:
9465 case Intrinsic::x86_sse_comilt_ss:
9466 case Intrinsic::x86_sse_comile_ss:
9467 case Intrinsic::x86_sse_comigt_ss:
9468 case Intrinsic::x86_sse_comige_ss:
9469 case Intrinsic::x86_sse_comineq_ss:
9470 case Intrinsic::x86_sse_ucomieq_ss:
9471 case Intrinsic::x86_sse_ucomilt_ss:
9472 case Intrinsic::x86_sse_ucomile_ss:
9473 case Intrinsic::x86_sse_ucomigt_ss:
9474 case Intrinsic::x86_sse_ucomige_ss:
9475 case Intrinsic::x86_sse_ucomineq_ss:
9476 case Intrinsic::x86_sse2_comieq_sd:
9477 case Intrinsic::x86_sse2_comilt_sd:
9478 case Intrinsic::x86_sse2_comile_sd:
9479 case Intrinsic::x86_sse2_comigt_sd:
9480 case Intrinsic::x86_sse2_comige_sd:
9481 case Intrinsic::x86_sse2_comineq_sd:
9482 case Intrinsic::x86_sse2_ucomieq_sd:
9483 case Intrinsic::x86_sse2_ucomilt_sd:
9484 case Intrinsic::x86_sse2_ucomile_sd:
9485 case Intrinsic::x86_sse2_ucomigt_sd:
9486 case Intrinsic::x86_sse2_ucomige_sd:
9487 case Intrinsic::x86_sse2_ucomineq_sd: {
9488 unsigned Opc = 0;
9489 ISD::CondCode CC = ISD::SETCC_INVALID;
9490 switch (IntNo) {
9491 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009492 case Intrinsic::x86_sse_comieq_ss:
9493 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009494 Opc = X86ISD::COMI;
9495 CC = ISD::SETEQ;
9496 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009497 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009498 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009499 Opc = X86ISD::COMI;
9500 CC = ISD::SETLT;
9501 break;
9502 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009503 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009504 Opc = X86ISD::COMI;
9505 CC = ISD::SETLE;
9506 break;
9507 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009508 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009509 Opc = X86ISD::COMI;
9510 CC = ISD::SETGT;
9511 break;
9512 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009513 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009514 Opc = X86ISD::COMI;
9515 CC = ISD::SETGE;
9516 break;
9517 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009518 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009519 Opc = X86ISD::COMI;
9520 CC = ISD::SETNE;
9521 break;
9522 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009523 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009524 Opc = X86ISD::UCOMI;
9525 CC = ISD::SETEQ;
9526 break;
9527 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009528 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009529 Opc = X86ISD::UCOMI;
9530 CC = ISD::SETLT;
9531 break;
9532 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009533 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009534 Opc = X86ISD::UCOMI;
9535 CC = ISD::SETLE;
9536 break;
9537 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009538 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009539 Opc = X86ISD::UCOMI;
9540 CC = ISD::SETGT;
9541 break;
9542 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009543 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009544 Opc = X86ISD::UCOMI;
9545 CC = ISD::SETGE;
9546 break;
9547 case Intrinsic::x86_sse_ucomineq_ss:
9548 case Intrinsic::x86_sse2_ucomineq_sd:
9549 Opc = X86ISD::UCOMI;
9550 CC = ISD::SETNE;
9551 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009552 }
Evan Cheng734503b2006-09-11 02:19:56 +00009553
Dan Gohman475871a2008-07-27 21:46:04 +00009554 SDValue LHS = Op.getOperand(1);
9555 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009556 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009557 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009558 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9559 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9560 DAG.getConstant(X86CC, MVT::i8), Cond);
9561 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009562 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009563 // Arithmetic intrinsics.
9564 case Intrinsic::x86_sse3_hadd_ps:
9565 case Intrinsic::x86_sse3_hadd_pd:
9566 case Intrinsic::x86_avx_hadd_ps_256:
9567 case Intrinsic::x86_avx_hadd_pd_256:
9568 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9569 Op.getOperand(1), Op.getOperand(2));
9570 case Intrinsic::x86_sse3_hsub_ps:
9571 case Intrinsic::x86_sse3_hsub_pd:
9572 case Intrinsic::x86_avx_hsub_ps_256:
9573 case Intrinsic::x86_avx_hsub_pd_256:
9574 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9575 Op.getOperand(1), Op.getOperand(2));
Craig Topper98fc7292011-11-19 17:46:46 +00009576 case Intrinsic::x86_avx2_psllv_d:
9577 case Intrinsic::x86_avx2_psllv_q:
9578 case Intrinsic::x86_avx2_psllv_d_256:
9579 case Intrinsic::x86_avx2_psllv_q_256:
9580 return DAG.getNode(ISD::SHL, dl, Op.getValueType(),
9581 Op.getOperand(1), Op.getOperand(2));
9582 case Intrinsic::x86_avx2_psrlv_d:
9583 case Intrinsic::x86_avx2_psrlv_q:
9584 case Intrinsic::x86_avx2_psrlv_d_256:
9585 case Intrinsic::x86_avx2_psrlv_q_256:
9586 return DAG.getNode(ISD::SRL, dl, Op.getValueType(),
9587 Op.getOperand(1), Op.getOperand(2));
9588 case Intrinsic::x86_avx2_psrav_d:
9589 case Intrinsic::x86_avx2_psrav_d_256:
9590 return DAG.getNode(ISD::SRA, dl, Op.getValueType(),
9591 Op.getOperand(1), Op.getOperand(2));
9592
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009593 // ptest and testp intrinsics. The intrinsic these come from are designed to
9594 // return an integer value, not just an instruction so lower it to the ptest
9595 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009596 case Intrinsic::x86_sse41_ptestz:
9597 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009598 case Intrinsic::x86_sse41_ptestnzc:
9599 case Intrinsic::x86_avx_ptestz_256:
9600 case Intrinsic::x86_avx_ptestc_256:
9601 case Intrinsic::x86_avx_ptestnzc_256:
9602 case Intrinsic::x86_avx_vtestz_ps:
9603 case Intrinsic::x86_avx_vtestc_ps:
9604 case Intrinsic::x86_avx_vtestnzc_ps:
9605 case Intrinsic::x86_avx_vtestz_pd:
9606 case Intrinsic::x86_avx_vtestc_pd:
9607 case Intrinsic::x86_avx_vtestnzc_pd:
9608 case Intrinsic::x86_avx_vtestz_ps_256:
9609 case Intrinsic::x86_avx_vtestc_ps_256:
9610 case Intrinsic::x86_avx_vtestnzc_ps_256:
9611 case Intrinsic::x86_avx_vtestz_pd_256:
9612 case Intrinsic::x86_avx_vtestc_pd_256:
9613 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9614 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009615 unsigned X86CC = 0;
9616 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009617 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009618 case Intrinsic::x86_avx_vtestz_ps:
9619 case Intrinsic::x86_avx_vtestz_pd:
9620 case Intrinsic::x86_avx_vtestz_ps_256:
9621 case Intrinsic::x86_avx_vtestz_pd_256:
9622 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009623 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009624 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009625 // ZF = 1
9626 X86CC = X86::COND_E;
9627 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009628 case Intrinsic::x86_avx_vtestc_ps:
9629 case Intrinsic::x86_avx_vtestc_pd:
9630 case Intrinsic::x86_avx_vtestc_ps_256:
9631 case Intrinsic::x86_avx_vtestc_pd_256:
9632 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009633 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009634 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009635 // CF = 1
9636 X86CC = X86::COND_B;
9637 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009638 case Intrinsic::x86_avx_vtestnzc_ps:
9639 case Intrinsic::x86_avx_vtestnzc_pd:
9640 case Intrinsic::x86_avx_vtestnzc_ps_256:
9641 case Intrinsic::x86_avx_vtestnzc_pd_256:
9642 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009643 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009644 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009645 // ZF and CF = 0
9646 X86CC = X86::COND_A;
9647 break;
9648 }
Eric Christopherfd179292009-08-27 18:07:15 +00009649
Eric Christopher71c67532009-07-29 00:28:05 +00009650 SDValue LHS = Op.getOperand(1);
9651 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009652 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9653 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009654 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9655 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9656 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009657 }
Evan Cheng5759f972008-05-04 09:15:50 +00009658
9659 // Fix vector shift instructions where the last operand is a non-immediate
9660 // i32 value.
Craig Topper7be5dfd2011-11-12 09:58:49 +00009661 case Intrinsic::x86_avx2_pslli_w:
9662 case Intrinsic::x86_avx2_pslli_d:
9663 case Intrinsic::x86_avx2_pslli_q:
9664 case Intrinsic::x86_avx2_psrli_w:
9665 case Intrinsic::x86_avx2_psrli_d:
9666 case Intrinsic::x86_avx2_psrli_q:
9667 case Intrinsic::x86_avx2_psrai_w:
9668 case Intrinsic::x86_avx2_psrai_d:
Evan Cheng5759f972008-05-04 09:15:50 +00009669 case Intrinsic::x86_sse2_pslli_w:
9670 case Intrinsic::x86_sse2_pslli_d:
9671 case Intrinsic::x86_sse2_pslli_q:
9672 case Intrinsic::x86_sse2_psrli_w:
9673 case Intrinsic::x86_sse2_psrli_d:
9674 case Intrinsic::x86_sse2_psrli_q:
9675 case Intrinsic::x86_sse2_psrai_w:
9676 case Intrinsic::x86_sse2_psrai_d:
9677 case Intrinsic::x86_mmx_pslli_w:
9678 case Intrinsic::x86_mmx_pslli_d:
9679 case Intrinsic::x86_mmx_pslli_q:
9680 case Intrinsic::x86_mmx_psrli_w:
9681 case Intrinsic::x86_mmx_psrli_d:
9682 case Intrinsic::x86_mmx_psrli_q:
9683 case Intrinsic::x86_mmx_psrai_w:
9684 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009685 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009686 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009687 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009688
9689 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009690 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009691 switch (IntNo) {
9692 case Intrinsic::x86_sse2_pslli_w:
9693 NewIntNo = Intrinsic::x86_sse2_psll_w;
9694 break;
9695 case Intrinsic::x86_sse2_pslli_d:
9696 NewIntNo = Intrinsic::x86_sse2_psll_d;
9697 break;
9698 case Intrinsic::x86_sse2_pslli_q:
9699 NewIntNo = Intrinsic::x86_sse2_psll_q;
9700 break;
9701 case Intrinsic::x86_sse2_psrli_w:
9702 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9703 break;
9704 case Intrinsic::x86_sse2_psrli_d:
9705 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9706 break;
9707 case Intrinsic::x86_sse2_psrli_q:
9708 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9709 break;
9710 case Intrinsic::x86_sse2_psrai_w:
9711 NewIntNo = Intrinsic::x86_sse2_psra_w;
9712 break;
9713 case Intrinsic::x86_sse2_psrai_d:
9714 NewIntNo = Intrinsic::x86_sse2_psra_d;
9715 break;
Craig Topper7be5dfd2011-11-12 09:58:49 +00009716 case Intrinsic::x86_avx2_pslli_w:
9717 NewIntNo = Intrinsic::x86_avx2_psll_w;
9718 break;
9719 case Intrinsic::x86_avx2_pslli_d:
9720 NewIntNo = Intrinsic::x86_avx2_psll_d;
9721 break;
9722 case Intrinsic::x86_avx2_pslli_q:
9723 NewIntNo = Intrinsic::x86_avx2_psll_q;
9724 break;
9725 case Intrinsic::x86_avx2_psrli_w:
9726 NewIntNo = Intrinsic::x86_avx2_psrl_w;
9727 break;
9728 case Intrinsic::x86_avx2_psrli_d:
9729 NewIntNo = Intrinsic::x86_avx2_psrl_d;
9730 break;
9731 case Intrinsic::x86_avx2_psrli_q:
9732 NewIntNo = Intrinsic::x86_avx2_psrl_q;
9733 break;
9734 case Intrinsic::x86_avx2_psrai_w:
9735 NewIntNo = Intrinsic::x86_avx2_psra_w;
9736 break;
9737 case Intrinsic::x86_avx2_psrai_d:
9738 NewIntNo = Intrinsic::x86_avx2_psra_d;
9739 break;
Evan Cheng5759f972008-05-04 09:15:50 +00009740 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009741 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009742 switch (IntNo) {
9743 case Intrinsic::x86_mmx_pslli_w:
9744 NewIntNo = Intrinsic::x86_mmx_psll_w;
9745 break;
9746 case Intrinsic::x86_mmx_pslli_d:
9747 NewIntNo = Intrinsic::x86_mmx_psll_d;
9748 break;
9749 case Intrinsic::x86_mmx_pslli_q:
9750 NewIntNo = Intrinsic::x86_mmx_psll_q;
9751 break;
9752 case Intrinsic::x86_mmx_psrli_w:
9753 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9754 break;
9755 case Intrinsic::x86_mmx_psrli_d:
9756 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9757 break;
9758 case Intrinsic::x86_mmx_psrli_q:
9759 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9760 break;
9761 case Intrinsic::x86_mmx_psrai_w:
9762 NewIntNo = Intrinsic::x86_mmx_psra_w;
9763 break;
9764 case Intrinsic::x86_mmx_psrai_d:
9765 NewIntNo = Intrinsic::x86_mmx_psra_d;
9766 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009767 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009768 }
9769 break;
9770 }
9771 }
Mon P Wangefa42202009-09-03 19:56:25 +00009772
9773 // The vector shift intrinsics with scalars uses 32b shift amounts but
9774 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9775 // to be zero.
9776 SDValue ShOps[4];
9777 ShOps[0] = ShAmt;
9778 ShOps[1] = DAG.getConstant(0, MVT::i32);
9779 if (ShAmtVT == MVT::v4i32) {
9780 ShOps[2] = DAG.getUNDEF(MVT::i32);
9781 ShOps[3] = DAG.getUNDEF(MVT::i32);
9782 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9783 } else {
9784 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009785// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009786 }
9787
Owen Andersone50ed302009-08-10 22:56:29 +00009788 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009789 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009790 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009791 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009792 Op.getOperand(1), ShAmt);
9793 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009794 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009795}
Evan Cheng72261582005-12-20 06:22:03 +00009796
Dan Gohmand858e902010-04-17 15:26:15 +00009797SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9798 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009799 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9800 MFI->setReturnAddressIsTaken(true);
9801
Bill Wendling64e87322009-01-16 19:25:27 +00009802 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009803 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009804
9805 if (Depth > 0) {
9806 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9807 SDValue Offset =
9808 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009809 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009810 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009811 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009812 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009813 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009814 }
9815
9816 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009817 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009818 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009819 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009820}
9821
Dan Gohmand858e902010-04-17 15:26:15 +00009822SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009823 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9824 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009825
Owen Andersone50ed302009-08-10 22:56:29 +00009826 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009827 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009828 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9829 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009830 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009831 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009832 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9833 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009834 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009835 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009836}
9837
Dan Gohman475871a2008-07-27 21:46:04 +00009838SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009839 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009840 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009841}
9842
Dan Gohmand858e902010-04-17 15:26:15 +00009843SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009844 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009845 SDValue Chain = Op.getOperand(0);
9846 SDValue Offset = Op.getOperand(1);
9847 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009848 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009849
Dan Gohmand8816272010-08-11 18:14:00 +00009850 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9851 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9852 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009853 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009854
Dan Gohmand8816272010-08-11 18:14:00 +00009855 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9856 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009857 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009858 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9859 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009860 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009861 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009862
Dale Johannesene4d209d2009-02-03 20:21:25 +00009863 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009864 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009865 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009866}
9867
Duncan Sands4a544a72011-09-06 13:37:06 +00009868SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9869 SelectionDAG &DAG) const {
9870 return Op.getOperand(0);
9871}
9872
9873SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9874 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009875 SDValue Root = Op.getOperand(0);
9876 SDValue Trmp = Op.getOperand(1); // trampoline
9877 SDValue FPtr = Op.getOperand(2); // nested function
9878 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009879 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009880
Dan Gohman69de1932008-02-06 22:27:42 +00009881 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009882
9883 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009884 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009885
9886 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009887 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9888 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009889
Evan Cheng0e6a0522011-07-18 20:57:22 +00009890 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9891 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009892
9893 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9894
9895 // Load the pointer to the nested function into R11.
9896 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009897 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009898 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009899 Addr, MachinePointerInfo(TrmpAddr),
9900 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009901
Owen Anderson825b72b2009-08-11 20:47:22 +00009902 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9903 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009904 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9905 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009906 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009907
9908 // Load the 'nest' parameter value into R10.
9909 // R10 is specified in X86CallingConv.td
9910 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009911 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9912 DAG.getConstant(10, MVT::i64));
9913 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009914 Addr, MachinePointerInfo(TrmpAddr, 10),
9915 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009916
Owen Anderson825b72b2009-08-11 20:47:22 +00009917 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9918 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009919 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9920 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009921 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009922
9923 // Jump to the nested function.
9924 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009925 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9926 DAG.getConstant(20, MVT::i64));
9927 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009928 Addr, MachinePointerInfo(TrmpAddr, 20),
9929 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009930
9931 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009932 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9933 DAG.getConstant(22, MVT::i64));
9934 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009935 MachinePointerInfo(TrmpAddr, 22),
9936 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009937
Duncan Sands4a544a72011-09-06 13:37:06 +00009938 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009939 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009940 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009941 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009942 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009943 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009944
9945 switch (CC) {
9946 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009947 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009948 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009949 case CallingConv::X86_StdCall: {
9950 // Pass 'nest' parameter in ECX.
9951 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009952 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009953
9954 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009955 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009956 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009957
Chris Lattner58d74912008-03-12 17:45:29 +00009958 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009959 unsigned InRegCount = 0;
9960 unsigned Idx = 1;
9961
9962 for (FunctionType::param_iterator I = FTy->param_begin(),
9963 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009964 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009965 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009966 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009967
9968 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009969 report_fatal_error("Nest register in use - reduce number of inreg"
9970 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009971 }
9972 }
9973 break;
9974 }
9975 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009976 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009977 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009978 // Pass 'nest' parameter in EAX.
9979 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009980 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009981 break;
9982 }
9983
Dan Gohman475871a2008-07-27 21:46:04 +00009984 SDValue OutChains[4];
9985 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009986
Owen Anderson825b72b2009-08-11 20:47:22 +00009987 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9988 DAG.getConstant(10, MVT::i32));
9989 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009990
Chris Lattnera62fe662010-02-05 19:20:30 +00009991 // This is storing the opcode for MOV32ri.
9992 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009993 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009994 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009995 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009996 Trmp, MachinePointerInfo(TrmpAddr),
9997 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009998
Owen Anderson825b72b2009-08-11 20:47:22 +00009999 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10000 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010001 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10002 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010003 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010004
Chris Lattnera62fe662010-02-05 19:20:30 +000010005 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010006 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10007 DAG.getConstant(5, MVT::i32));
10008 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010009 MachinePointerInfo(TrmpAddr, 5),
10010 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010011
Owen Anderson825b72b2009-08-11 20:47:22 +000010012 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10013 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010014 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10015 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010016 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010017
Duncan Sands4a544a72011-09-06 13:37:06 +000010018 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010019 }
10020}
10021
Dan Gohmand858e902010-04-17 15:26:15 +000010022SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10023 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010024 /*
10025 The rounding mode is in bits 11:10 of FPSR, and has the following
10026 settings:
10027 00 Round to nearest
10028 01 Round to -inf
10029 10 Round to +inf
10030 11 Round to 0
10031
10032 FLT_ROUNDS, on the other hand, expects the following:
10033 -1 Undefined
10034 0 Round to 0
10035 1 Round to nearest
10036 2 Round to +inf
10037 3 Round to -inf
10038
10039 To perform the conversion, we do:
10040 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10041 */
10042
10043 MachineFunction &MF = DAG.getMachineFunction();
10044 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010045 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010046 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010047 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010048 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010049
10050 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010051 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010052 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010053
Michael J. Spencerec38de22010-10-10 22:04:20 +000010054
Chris Lattner2156b792010-09-22 01:11:26 +000010055 MachineMemOperand *MMO =
10056 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10057 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010058
Chris Lattner2156b792010-09-22 01:11:26 +000010059 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10060 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10061 DAG.getVTList(MVT::Other),
10062 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010063
10064 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010065 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010066 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010067
10068 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010069 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010070 DAG.getNode(ISD::SRL, DL, MVT::i16,
10071 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010072 CWD, DAG.getConstant(0x800, MVT::i16)),
10073 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010074 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010075 DAG.getNode(ISD::SRL, DL, MVT::i16,
10076 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010077 CWD, DAG.getConstant(0x400, MVT::i16)),
10078 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010079
Dan Gohman475871a2008-07-27 21:46:04 +000010080 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010081 DAG.getNode(ISD::AND, DL, MVT::i16,
10082 DAG.getNode(ISD::ADD, DL, MVT::i16,
10083 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010084 DAG.getConstant(1, MVT::i16)),
10085 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010086
10087
Duncan Sands83ec4b62008-06-06 12:08:01 +000010088 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010089 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010090}
10091
Dan Gohmand858e902010-04-17 15:26:15 +000010092SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010093 EVT VT = Op.getValueType();
10094 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010095 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010096 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010097
10098 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010099 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010100 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010101 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010102 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010103 }
Evan Cheng18efe262007-12-14 02:13:44 +000010104
Evan Cheng152804e2007-12-14 08:30:15 +000010105 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010106 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010107 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010108
10109 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010110 SDValue Ops[] = {
10111 Op,
10112 DAG.getConstant(NumBits+NumBits-1, OpVT),
10113 DAG.getConstant(X86::COND_E, MVT::i8),
10114 Op.getValue(1)
10115 };
10116 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010117
10118 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010119 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010120
Owen Anderson825b72b2009-08-11 20:47:22 +000010121 if (VT == MVT::i8)
10122 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010123 return Op;
10124}
10125
Dan Gohmand858e902010-04-17 15:26:15 +000010126SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010127 EVT VT = Op.getValueType();
10128 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010129 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010130 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010131
10132 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010133 if (VT == MVT::i8) {
10134 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010135 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010136 }
Evan Cheng152804e2007-12-14 08:30:15 +000010137
10138 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010139 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010140 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010141
10142 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010143 SDValue Ops[] = {
10144 Op,
10145 DAG.getConstant(NumBits, OpVT),
10146 DAG.getConstant(X86::COND_E, MVT::i8),
10147 Op.getValue(1)
10148 };
10149 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010150
Owen Anderson825b72b2009-08-11 20:47:22 +000010151 if (VT == MVT::i8)
10152 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010153 return Op;
10154}
10155
Craig Topper13894fa2011-08-24 06:14:18 +000010156// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10157// ones, and then concatenate the result back.
10158static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010159 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010160
10161 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
10162 "Unsupported value type for operation");
10163
10164 int NumElems = VT.getVectorNumElements();
10165 DebugLoc dl = Op.getDebugLoc();
10166 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10167 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10168
10169 // Extract the LHS vectors
10170 SDValue LHS = Op.getOperand(0);
10171 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10172 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10173
10174 // Extract the RHS vectors
10175 SDValue RHS = Op.getOperand(1);
10176 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
10177 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
10178
10179 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10180 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10181
10182 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10183 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10184 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10185}
10186
10187SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
10188 assert(Op.getValueType().getSizeInBits() == 256 &&
10189 Op.getValueType().isInteger() &&
10190 "Only handle AVX 256-bit vector integer operation");
10191 return Lower256IntArith(Op, DAG);
10192}
10193
10194SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
10195 assert(Op.getValueType().getSizeInBits() == 256 &&
10196 Op.getValueType().isInteger() &&
10197 "Only handle AVX 256-bit vector integer operation");
10198 return Lower256IntArith(Op, DAG);
10199}
10200
10201SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10202 EVT VT = Op.getValueType();
10203
10204 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +000010205 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010206 return Lower256IntArith(Op, DAG);
10207
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010208 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010209
Craig Topperaaa643c2011-11-09 07:28:55 +000010210 SDValue A = Op.getOperand(0);
10211 SDValue B = Op.getOperand(1);
10212
10213 if (VT == MVT::v4i64) {
10214 assert(Subtarget->hasAVX2() && "Lowering v4i64 multiply requires AVX2");
10215
10216 // ulong2 Ahi = __builtin_ia32_psrlqi256( a, 32);
10217 // ulong2 Bhi = __builtin_ia32_psrlqi256( b, 32);
10218 // ulong2 AloBlo = __builtin_ia32_pmuludq256( a, b );
10219 // ulong2 AloBhi = __builtin_ia32_pmuludq256( a, Bhi );
10220 // ulong2 AhiBlo = __builtin_ia32_pmuludq256( Ahi, b );
10221 //
10222 // AloBhi = __builtin_ia32_psllqi256( AloBhi, 32 );
10223 // AhiBlo = __builtin_ia32_psllqi256( AhiBlo, 32 );
10224 // return AloBlo + AloBhi + AhiBlo;
10225
10226 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10227 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10228 A, DAG.getConstant(32, MVT::i32));
10229 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10230 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10231 B, DAG.getConstant(32, MVT::i32));
10232 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10233 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10234 A, B);
10235 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10236 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10237 A, Bhi);
10238 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10239 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10240 Ahi, B);
10241 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10242 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10243 AloBhi, DAG.getConstant(32, MVT::i32));
10244 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10245 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10246 AhiBlo, DAG.getConstant(32, MVT::i32));
10247 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10248 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
10249 return Res;
10250 }
10251
10252 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
10253
Mon P Wangaf9b9522008-12-18 21:42:19 +000010254 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
10255 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
10256 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
10257 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
10258 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
10259 //
10260 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
10261 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
10262 // return AloBlo + AloBhi + AhiBlo;
10263
Dale Johannesene4d209d2009-02-03 20:21:25 +000010264 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010265 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10266 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010267 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010268 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10269 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010270 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010271 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010272 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010273 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010274 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010275 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010276 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010277 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010278 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010279 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010280 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10281 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010282 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010283 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10284 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010285 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10286 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010287 return Res;
10288}
10289
Nadav Rotem43012222011-05-11 08:12:09 +000010290SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10291
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010292 EVT VT = Op.getValueType();
10293 DebugLoc dl = Op.getDebugLoc();
10294 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010295 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010296 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010297
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010298 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010299 return SDValue();
10300
Nadav Rotem43012222011-05-11 08:12:09 +000010301 // Optimize shl/srl/sra with constant shift amount.
10302 if (isSplatVector(Amt.getNode())) {
10303 SDValue SclrAmt = Amt->getOperand(0);
10304 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10305 uint64_t ShiftAmt = C->getZExtValue();
10306
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010307 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SHL) {
10308 // Make a large shift.
10309 SDValue SHL =
10310 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10311 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10312 R, DAG.getConstant(ShiftAmt, MVT::i32));
10313 // Zero out the rightmost bits.
10314 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10315 MVT::i8));
10316 return DAG.getNode(ISD::AND, dl, VT, SHL,
10317 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10318 }
10319
Nadav Rotem43012222011-05-11 08:12:09 +000010320 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
10321 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10322 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10323 R, DAG.getConstant(ShiftAmt, MVT::i32));
10324
10325 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
10326 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10327 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10328 R, DAG.getConstant(ShiftAmt, MVT::i32));
10329
10330 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
10331 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10332 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10333 R, DAG.getConstant(ShiftAmt, MVT::i32));
10334
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010335 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRL) {
10336 // Make a large shift.
10337 SDValue SRL =
10338 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10339 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10340 R, DAG.getConstant(ShiftAmt, MVT::i32));
10341 // Zero out the leftmost bits.
10342 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10343 MVT::i8));
10344 return DAG.getNode(ISD::AND, dl, VT, SRL,
10345 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10346 }
10347
Nadav Rotem43012222011-05-11 08:12:09 +000010348 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
10349 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10350 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10351 R, DAG.getConstant(ShiftAmt, MVT::i32));
10352
10353 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
10354 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10355 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
10356 R, DAG.getConstant(ShiftAmt, MVT::i32));
10357
10358 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
10359 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10360 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10361 R, DAG.getConstant(ShiftAmt, MVT::i32));
10362
10363 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
10364 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10365 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
10366 R, DAG.getConstant(ShiftAmt, MVT::i32));
10367
10368 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
10369 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10370 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
10371 R, DAG.getConstant(ShiftAmt, MVT::i32));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010372
10373 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRA) {
10374 if (ShiftAmt == 7) {
10375 // R s>> 7 === R s< 0
10376 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10377 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10378 }
10379
10380 // R s>> a === ((R u>> a) ^ m) - m
10381 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10382 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10383 MVT::i8));
10384 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10385 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10386 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10387 return Res;
10388 }
Craig Topper46154eb2011-11-11 07:39:23 +000010389
Craig Topper0d86d462011-11-20 00:12:05 +000010390 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10391 if (Op.getOpcode() == ISD::SHL) {
10392 // Make a large shift.
10393 SDValue SHL =
10394 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10395 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
10396 R, DAG.getConstant(ShiftAmt, MVT::i32));
10397 // Zero out the rightmost bits.
10398 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10399 MVT::i8));
10400 return DAG.getNode(ISD::AND, dl, VT, SHL,
10401 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010402 }
Craig Topper0d86d462011-11-20 00:12:05 +000010403 if (Op.getOpcode() == ISD::SRL) {
10404 // Make a large shift.
10405 SDValue SRL =
10406 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10407 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
10408 R, DAG.getConstant(ShiftAmt, MVT::i32));
10409 // Zero out the leftmost bits.
10410 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10411 MVT::i8));
10412 return DAG.getNode(ISD::AND, dl, VT, SRL,
10413 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10414 }
10415 if (Op.getOpcode() == ISD::SRA) {
10416 if (ShiftAmt == 7) {
10417 // R s>> 7 === R s< 0
10418 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10419 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10420 }
10421
10422 // R s>> a === ((R u>> a) ^ m) - m
10423 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10424 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10425 MVT::i8));
10426 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10427 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10428 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10429 return Res;
10430 }
10431 }
Nadav Rotem43012222011-05-11 08:12:09 +000010432 }
10433 }
10434
10435 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010436 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010437 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10438 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10439 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
10440
10441 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010442
Nate Begeman51409212010-07-28 00:21:48 +000010443 std::vector<Constant*> CV(4, CI);
10444 Constant *C = ConstantVector::get(CV);
10445 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10446 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010447 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010448 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010449
10450 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010451 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010452 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10453 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10454 }
Nadav Rotem43012222011-05-11 08:12:09 +000010455 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010456 // a = a << 5;
10457 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10458 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10459 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
10460
10461 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
10462 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
10463
10464 std::vector<Constant*> CVM1(16, CM1);
10465 std::vector<Constant*> CVM2(16, CM2);
10466 Constant *C = ConstantVector::get(CVM1);
10467 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10468 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010469 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010470 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010471
10472 // r = pblendv(r, psllw(r & (char16)15, 4), a);
10473 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10474 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10475 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10476 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010477 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010478 // a += a
10479 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010480
Nate Begeman51409212010-07-28 00:21:48 +000010481 C = ConstantVector::get(CVM2);
10482 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10483 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010484 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010485 false, false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010486
Nate Begeman51409212010-07-28 00:21:48 +000010487 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10488 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10489 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10490 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10491 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010492 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010493 // a += a
10494 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010495
Nate Begeman51409212010-07-28 00:21:48 +000010496 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010497 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10498 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010499 return R;
10500 }
Craig Topper46154eb2011-11-11 07:39:23 +000010501
10502 // Decompose 256-bit shifts into smaller 128-bit shifts.
10503 if (VT.getSizeInBits() == 256) {
10504 int NumElems = VT.getVectorNumElements();
10505 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10506 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10507
10508 // Extract the two vectors
10509 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10510 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10511 DAG, dl);
10512
10513 // Recreate the shift amount vectors
10514 SDValue Amt1, Amt2;
10515 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10516 // Constant shift amount
10517 SmallVector<SDValue, 4> Amt1Csts;
10518 SmallVector<SDValue, 4> Amt2Csts;
10519 for (int i = 0; i < NumElems/2; ++i)
10520 Amt1Csts.push_back(Amt->getOperand(i));
10521 for (int i = NumElems/2; i < NumElems; ++i)
10522 Amt2Csts.push_back(Amt->getOperand(i));
10523
10524 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10525 &Amt1Csts[0], NumElems/2);
10526 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10527 &Amt2Csts[0], NumElems/2);
10528 } else {
10529 // Variable shift amount
10530 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10531 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10532 DAG, dl);
10533 }
10534
10535 // Issue new vector shifts for the smaller types
10536 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10537 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10538
10539 // Concatenate the result back
10540 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10541 }
10542
Nate Begeman51409212010-07-28 00:21:48 +000010543 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010544}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010545
Dan Gohmand858e902010-04-17 15:26:15 +000010546SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010547 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10548 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010549 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10550 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010551 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010552 SDValue LHS = N->getOperand(0);
10553 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010554 unsigned BaseOp = 0;
10555 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010556 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010557 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010558 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010559 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010560 // A subtract of one will be selected as a INC. Note that INC doesn't
10561 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010562 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10563 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010564 BaseOp = X86ISD::INC;
10565 Cond = X86::COND_O;
10566 break;
10567 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010568 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010569 Cond = X86::COND_O;
10570 break;
10571 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010572 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010573 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010574 break;
10575 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010576 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10577 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010578 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10579 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010580 BaseOp = X86ISD::DEC;
10581 Cond = X86::COND_O;
10582 break;
10583 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010584 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010585 Cond = X86::COND_O;
10586 break;
10587 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010588 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010589 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010590 break;
10591 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010592 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010593 Cond = X86::COND_O;
10594 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010595 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10596 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10597 MVT::i32);
10598 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010599
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010600 SDValue SetCC =
10601 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10602 DAG.getConstant(X86::COND_O, MVT::i32),
10603 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010604
Dan Gohman6e5fda22011-07-22 18:45:15 +000010605 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010606 }
Bill Wendling74c37652008-12-09 22:08:41 +000010607 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010608
Bill Wendling61edeb52008-12-02 01:06:39 +000010609 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010610 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010611 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010612
Bill Wendling61edeb52008-12-02 01:06:39 +000010613 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010614 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10615 DAG.getConstant(Cond, MVT::i32),
10616 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010617
Dan Gohman6e5fda22011-07-22 18:45:15 +000010618 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010619}
10620
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010621SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10622 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010623 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10624 EVT VT = Op.getValueType();
10625
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010626 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010627 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10628 ExtraVT.getScalarType().getSizeInBits();
10629 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10630
10631 unsigned SHLIntrinsicsID = 0;
10632 unsigned SRAIntrinsicsID = 0;
10633 switch (VT.getSimpleVT().SimpleTy) {
10634 default:
10635 return SDValue();
Craig Toppera124f942011-11-21 01:12:36 +000010636 case MVT::v4i32:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010637 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10638 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10639 break;
Craig Toppera124f942011-11-21 01:12:36 +000010640 case MVT::v8i16:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010641 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10642 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10643 break;
Craig Toppera124f942011-11-21 01:12:36 +000010644 case MVT::v8i32:
10645 case MVT::v16i16:
10646 if (!Subtarget->hasAVX())
10647 return SDValue();
10648 if (!Subtarget->hasAVX2()) {
10649 // needs to be split
10650 int NumElems = VT.getVectorNumElements();
10651 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10652 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10653
10654 // Extract the LHS vectors
10655 SDValue LHS = Op.getOperand(0);
10656 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10657 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10658
10659 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10660 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10661
10662 EVT ExtraEltVT = ExtraVT.getVectorElementType();
10663 int ExtraNumElems = ExtraVT.getVectorNumElements();
10664 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10665 ExtraNumElems/2);
10666 SDValue Extra = DAG.getValueType(ExtraVT);
10667
10668 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10669 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
10670
10671 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10672 }
10673 if (VT == MVT::v8i32) {
10674 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_d;
10675 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_d;
10676 } else {
10677 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_w;
10678 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_w;
10679 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010680 }
10681
10682 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10683 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
Craig Toppera124f942011-11-21 01:12:36 +000010684 Op.getOperand(0), ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010685
Nadav Rotema7934dd2011-10-10 19:31:45 +000010686 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10687 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10688 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010689 }
10690
10691 return SDValue();
10692}
10693
10694
Eric Christopher9a9d2752010-07-22 02:48:34 +000010695SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10696 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010697
Eric Christopher77ed1352011-07-08 00:04:56 +000010698 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10699 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010700 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010701 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010702 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010703 SDValue Ops[] = {
10704 DAG.getRegister(X86::ESP, MVT::i32), // Base
10705 DAG.getTargetConstant(1, MVT::i8), // Scale
10706 DAG.getRegister(0, MVT::i32), // Index
10707 DAG.getTargetConstant(0, MVT::i32), // Disp
10708 DAG.getRegister(0, MVT::i32), // Segment.
10709 Zero,
10710 Chain
10711 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010712 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010713 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10714 array_lengthof(Ops));
10715 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010716 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010717
Eric Christopher9a9d2752010-07-22 02:48:34 +000010718 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010719 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010720 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010721
Chris Lattner132929a2010-08-14 17:26:09 +000010722 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10723 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10724 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10725 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010726
Chris Lattner132929a2010-08-14 17:26:09 +000010727 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10728 if (!Op1 && !Op2 && !Op3 && Op4)
10729 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010730
Chris Lattner132929a2010-08-14 17:26:09 +000010731 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10732 if (Op1 && !Op2 && !Op3 && !Op4)
10733 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010734
10735 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010736 // (MFENCE)>;
10737 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010738}
10739
Eli Friedman14648462011-07-27 22:21:52 +000010740SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10741 SelectionDAG &DAG) const {
10742 DebugLoc dl = Op.getDebugLoc();
10743 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10744 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10745 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10746 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10747
10748 // The only fence that needs an instruction is a sequentially-consistent
10749 // cross-thread fence.
10750 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10751 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10752 // no-sse2). There isn't any reason to disable it if the target processor
10753 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010754 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010755 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10756
10757 SDValue Chain = Op.getOperand(0);
10758 SDValue Zero = DAG.getConstant(0, MVT::i32);
10759 SDValue Ops[] = {
10760 DAG.getRegister(X86::ESP, MVT::i32), // Base
10761 DAG.getTargetConstant(1, MVT::i8), // Scale
10762 DAG.getRegister(0, MVT::i32), // Index
10763 DAG.getTargetConstant(0, MVT::i32), // Disp
10764 DAG.getRegister(0, MVT::i32), // Segment.
10765 Zero,
10766 Chain
10767 };
10768 SDNode *Res =
10769 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10770 array_lengthof(Ops));
10771 return SDValue(Res, 0);
10772 }
10773
10774 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10775 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10776}
10777
10778
Dan Gohmand858e902010-04-17 15:26:15 +000010779SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010780 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010781 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010782 unsigned Reg = 0;
10783 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010784 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010785 default:
10786 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010787 case MVT::i8: Reg = X86::AL; size = 1; break;
10788 case MVT::i16: Reg = X86::AX; size = 2; break;
10789 case MVT::i32: Reg = X86::EAX; size = 4; break;
10790 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010791 assert(Subtarget->is64Bit() && "Node not type legal!");
10792 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010793 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010794 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010795 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010796 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010797 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010798 Op.getOperand(1),
10799 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010800 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010801 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010802 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010803 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10804 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10805 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010806 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010807 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010808 return cpOut;
10809}
10810
Duncan Sands1607f052008-12-01 11:39:25 +000010811SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010812 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010813 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010814 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010815 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010816 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010817 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010818 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10819 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010820 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010821 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10822 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010823 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010824 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010825 rdx.getValue(1)
10826 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010827 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010828}
10829
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010830SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010831 SelectionDAG &DAG) const {
10832 EVT SrcVT = Op.getOperand(0).getValueType();
10833 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010834 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010835 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010836 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010837 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010838 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010839 // i64 <=> MMX conversions are Legal.
10840 if (SrcVT==MVT::i64 && DstVT.isVector())
10841 return Op;
10842 if (DstVT==MVT::i64 && SrcVT.isVector())
10843 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010844 // MMX <=> MMX conversions are Legal.
10845 if (SrcVT.isVector() && DstVT.isVector())
10846 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010847 // All other conversions need to be expanded.
10848 return SDValue();
10849}
Chris Lattner5b856542010-12-20 00:59:46 +000010850
Dan Gohmand858e902010-04-17 15:26:15 +000010851SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010852 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010853 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010854 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010855 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010856 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010857 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010858 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010859 Node->getOperand(0),
10860 Node->getOperand(1), negOp,
10861 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010862 cast<AtomicSDNode>(Node)->getAlignment(),
10863 cast<AtomicSDNode>(Node)->getOrdering(),
10864 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010865}
10866
Eli Friedman327236c2011-08-24 20:50:09 +000010867static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10868 SDNode *Node = Op.getNode();
10869 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010870 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010871
10872 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010873 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10874 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10875 // (The only way to get a 16-byte store is cmpxchg16b)
10876 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10877 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10878 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010879 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10880 cast<AtomicSDNode>(Node)->getMemoryVT(),
10881 Node->getOperand(0),
10882 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010883 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010884 cast<AtomicSDNode>(Node)->getOrdering(),
10885 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010886 return Swap.getValue(1);
10887 }
10888 // Other atomic stores have a simple pattern.
10889 return Op;
10890}
10891
Chris Lattner5b856542010-12-20 00:59:46 +000010892static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10893 EVT VT = Op.getNode()->getValueType(0);
10894
10895 // Let legalize expand this if it isn't a legal type yet.
10896 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10897 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010898
Chris Lattner5b856542010-12-20 00:59:46 +000010899 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010900
Chris Lattner5b856542010-12-20 00:59:46 +000010901 unsigned Opc;
10902 bool ExtraOp = false;
10903 switch (Op.getOpcode()) {
10904 default: assert(0 && "Invalid code");
10905 case ISD::ADDC: Opc = X86ISD::ADD; break;
10906 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10907 case ISD::SUBC: Opc = X86ISD::SUB; break;
10908 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10909 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010910
Chris Lattner5b856542010-12-20 00:59:46 +000010911 if (!ExtraOp)
10912 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10913 Op.getOperand(1));
10914 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10915 Op.getOperand(1), Op.getOperand(2));
10916}
10917
Evan Cheng0db9fe62006-04-25 20:13:52 +000010918/// LowerOperation - Provide custom lowering hooks for some operations.
10919///
Dan Gohmand858e902010-04-17 15:26:15 +000010920SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010921 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010922 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010923 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010924 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010925 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010926 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10927 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010928 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010929 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010930 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010931 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10932 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10933 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010934 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010935 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010936 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10937 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10938 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010939 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010940 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010941 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010942 case ISD::SHL_PARTS:
10943 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010944 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010945 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010946 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010947 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010948 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010949 case ISD::FABS: return LowerFABS(Op, DAG);
10950 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010951 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010952 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010953 case ISD::SETCC: return LowerSETCC(Op, DAG);
10954 case ISD::SELECT: return LowerSELECT(Op, DAG);
10955 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010956 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010957 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010958 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010959 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010960 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010961 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10962 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010963 case ISD::FRAME_TO_ARGS_OFFSET:
10964 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010965 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010966 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010967 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10968 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010969 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010970 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10971 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010972 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010973 case ISD::SRA:
10974 case ISD::SRL:
10975 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010976 case ISD::SADDO:
10977 case ISD::UADDO:
10978 case ISD::SSUBO:
10979 case ISD::USUBO:
10980 case ISD::SMULO:
10981 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010982 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010983 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010984 case ISD::ADDC:
10985 case ISD::ADDE:
10986 case ISD::SUBC:
10987 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010988 case ISD::ADD: return LowerADD(Op, DAG);
10989 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010990 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010991}
10992
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010993static void ReplaceATOMIC_LOAD(SDNode *Node,
10994 SmallVectorImpl<SDValue> &Results,
10995 SelectionDAG &DAG) {
10996 DebugLoc dl = Node->getDebugLoc();
10997 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10998
10999 // Convert wide load -> cmpxchg8b/cmpxchg16b
11000 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11001 // (The only way to get a 16-byte load is cmpxchg16b)
11002 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011003 SDValue Zero = DAG.getConstant(0, VT);
11004 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011005 Node->getOperand(0),
11006 Node->getOperand(1), Zero, Zero,
11007 cast<AtomicSDNode>(Node)->getMemOperand(),
11008 cast<AtomicSDNode>(Node)->getOrdering(),
11009 cast<AtomicSDNode>(Node)->getSynchScope());
11010 Results.push_back(Swap.getValue(0));
11011 Results.push_back(Swap.getValue(1));
11012}
11013
Duncan Sands1607f052008-12-01 11:39:25 +000011014void X86TargetLowering::
11015ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011016 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011017 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011018 assert (Node->getValueType(0) == MVT::i64 &&
11019 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011020
11021 SDValue Chain = Node->getOperand(0);
11022 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011023 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011024 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011025 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011026 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011027 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011028 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011029 SDValue Result =
11030 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11031 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011032 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011033 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011034 Results.push_back(Result.getValue(2));
11035}
11036
Duncan Sands126d9072008-07-04 11:47:58 +000011037/// ReplaceNodeResults - Replace a node with an illegal result type
11038/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011039void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11040 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011041 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011042 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011043 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011044 default:
Duncan Sands1607f052008-12-01 11:39:25 +000011045 assert(false && "Do not know how to custom type legalize this operation!");
11046 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011047 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011048 case ISD::ADDC:
11049 case ISD::ADDE:
11050 case ISD::SUBC:
11051 case ISD::SUBE:
11052 // We don't want to expand or promote these.
11053 return;
Duncan Sands1607f052008-12-01 11:39:25 +000011054 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000011055 std::pair<SDValue,SDValue> Vals =
11056 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000011057 SDValue FIST = Vals.first, StackSlot = Vals.second;
11058 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011059 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011060 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000011061 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011062 MachinePointerInfo(),
11063 false, false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000011064 }
11065 return;
11066 }
11067 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011068 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011069 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011070 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011071 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011072 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011073 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011074 eax.getValue(2));
11075 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11076 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011077 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011078 Results.push_back(edx.getValue(1));
11079 return;
11080 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011081 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011082 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011083 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011084 bool Regs64bit = T == MVT::i128;
11085 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011086 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011087 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11088 DAG.getConstant(0, HalfT));
11089 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11090 DAG.getConstant(1, HalfT));
11091 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11092 Regs64bit ? X86::RAX : X86::EAX,
11093 cpInL, SDValue());
11094 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11095 Regs64bit ? X86::RDX : X86::EDX,
11096 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011097 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011098 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11099 DAG.getConstant(0, HalfT));
11100 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11101 DAG.getConstant(1, HalfT));
11102 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11103 Regs64bit ? X86::RBX : X86::EBX,
11104 swapInL, cpInH.getValue(1));
11105 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
11106 Regs64bit ? X86::RCX : X86::ECX,
11107 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011108 SDValue Ops[] = { swapInH.getValue(0),
11109 N->getOperand(1),
11110 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011111 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011112 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011113 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11114 X86ISD::LCMPXCHG8_DAG;
11115 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011116 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011117 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11118 Regs64bit ? X86::RAX : X86::EAX,
11119 HalfT, Result.getValue(1));
11120 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11121 Regs64bit ? X86::RDX : X86::EDX,
11122 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011123 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011124 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011125 Results.push_back(cpOutH.getValue(1));
11126 return;
11127 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011128 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000011129 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
11130 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011131 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000011132 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
11133 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011134 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000011135 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
11136 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011137 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000011138 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
11139 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011140 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000011141 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
11142 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011143 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000011144 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
11145 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011146 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000011147 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
11148 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011149 case ISD::ATOMIC_LOAD:
11150 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011151 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011152}
11153
Evan Cheng72261582005-12-20 06:22:03 +000011154const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11155 switch (Opcode) {
11156 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011157 case X86ISD::BSF: return "X86ISD::BSF";
11158 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011159 case X86ISD::SHLD: return "X86ISD::SHLD";
11160 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011161 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011162 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011163 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011164 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011165 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011166 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011167 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11168 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11169 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011170 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011171 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011172 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011173 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011174 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011175 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011176 case X86ISD::COMI: return "X86ISD::COMI";
11177 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011178 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011179 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011180 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11181 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011182 case X86ISD::CMOV: return "X86ISD::CMOV";
11183 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011184 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011185 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11186 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011187 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011188 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011189 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011190 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011191 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011192 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11193 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011194 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011195 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011196 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011197 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011198 case X86ISD::BLENDV: return "X86ISD::BLENDV";
11199 case X86ISD::FHADD: return "X86ISD::FHADD";
11200 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011201 case X86ISD::FMAX: return "X86ISD::FMAX";
11202 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000011203 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11204 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011205 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011206 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011207 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011208 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011209 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011210 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11211 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011212 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11213 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11214 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11215 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11216 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11217 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011218 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
11219 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000011220 case X86ISD::VSHL: return "X86ISD::VSHL";
11221 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000011222 case X86ISD::CMPPD: return "X86ISD::CMPPD";
11223 case X86ISD::CMPPS: return "X86ISD::CMPPS";
11224 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
11225 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
11226 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
11227 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
11228 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
11229 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
11230 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
11231 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011232 case X86ISD::ADD: return "X86ISD::ADD";
11233 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011234 case X86ISD::ADC: return "X86ISD::ADC";
11235 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011236 case X86ISD::SMUL: return "X86ISD::SMUL";
11237 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011238 case X86ISD::INC: return "X86ISD::INC";
11239 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011240 case X86ISD::OR: return "X86ISD::OR";
11241 case X86ISD::XOR: return "X86ISD::XOR";
11242 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011243 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011244 case X86ISD::BLSI: return "X86ISD::BLSI";
11245 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11246 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011247 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011248 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011249 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011250 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11251 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11252 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
11253 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
11254 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
11255 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
11256 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
11257 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
11258 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011259 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011260 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011261 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011262 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11263 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011264 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11265 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11266 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
11267 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
11268 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
11269 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11270 case X86ISD::MOVSS: return "X86ISD::MOVSS";
11271 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
11272 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
Craig Topper6347e862011-11-21 06:57:39 +000011273 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
David Greenefbf05d32011-02-22 23:31:46 +000011274 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011275 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
11276 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
11277 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
11278 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
11279 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
11280 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
Craig Topper6fa583d2011-11-21 08:26:50 +000011281 case X86ISD::VPUNPCKLBWY: return "X86ISD::VPUNPCKLBWY";
Craig Topper6347e862011-11-21 06:57:39 +000011282 case X86ISD::VPUNPCKLWDY: return "X86ISD::VPUNPCKLWDY";
11283 case X86ISD::VPUNPCKLDQY: return "X86ISD::VPUNPCKLDQY";
11284 case X86ISD::VPUNPCKLQDQY: return "X86ISD::VPUNPCKLQDQY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011285 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
11286 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
11287 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
11288 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Craig Topper6fa583d2011-11-21 08:26:50 +000011289 case X86ISD::VPUNPCKHBWY: return "X86ISD::VPUNPCKHBWY";
Craig Topper6347e862011-11-21 06:57:39 +000011290 case X86ISD::VPUNPCKHWDY: return "X86ISD::VPUNPCKHWDY";
11291 case X86ISD::VPUNPCKHDQY: return "X86ISD::VPUNPCKHDQY";
11292 case X86ISD::VPUNPCKHQDQY: return "X86ISD::VPUNPCKHQDQY";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011293 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000011294 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
11295 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
11296 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
11297 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000011298 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011299 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011300 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011301 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011302 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011303 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000011304 }
11305}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011306
Chris Lattnerc9addb72007-03-30 23:15:24 +000011307// isLegalAddressingMode - Return true if the addressing mode represented
11308// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011309bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011310 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011311 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011312 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011313 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011314
Chris Lattnerc9addb72007-03-30 23:15:24 +000011315 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011316 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011317 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011318
Chris Lattnerc9addb72007-03-30 23:15:24 +000011319 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011320 unsigned GVFlags =
11321 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011322
Chris Lattnerdfed4132009-07-10 07:38:24 +000011323 // If a reference to this global requires an extra load, we can't fold it.
11324 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011325 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011326
Chris Lattnerdfed4132009-07-10 07:38:24 +000011327 // If BaseGV requires a register for the PIC base, we cannot also have a
11328 // BaseReg specified.
11329 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011330 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011331
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011332 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011333 if ((M != CodeModel::Small || R != Reloc::Static) &&
11334 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011335 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011336 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011337
Chris Lattnerc9addb72007-03-30 23:15:24 +000011338 switch (AM.Scale) {
11339 case 0:
11340 case 1:
11341 case 2:
11342 case 4:
11343 case 8:
11344 // These scales always work.
11345 break;
11346 case 3:
11347 case 5:
11348 case 9:
11349 // These scales are formed with basereg+scalereg. Only accept if there is
11350 // no basereg yet.
11351 if (AM.HasBaseReg)
11352 return false;
11353 break;
11354 default: // Other stuff never works.
11355 return false;
11356 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011357
Chris Lattnerc9addb72007-03-30 23:15:24 +000011358 return true;
11359}
11360
11361
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011362bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011363 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011364 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011365 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11366 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011367 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011368 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011369 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011370}
11371
Owen Andersone50ed302009-08-10 22:56:29 +000011372bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011373 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011374 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011375 unsigned NumBits1 = VT1.getSizeInBits();
11376 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011377 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011378 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011379 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011380}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011381
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011382bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011383 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011384 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011385}
11386
Owen Andersone50ed302009-08-10 22:56:29 +000011387bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011388 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011389 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011390}
11391
Owen Andersone50ed302009-08-10 22:56:29 +000011392bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011393 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011394 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011395}
11396
Evan Cheng60c07e12006-07-05 22:17:51 +000011397/// isShuffleMaskLegal - Targets can use this to indicate that they only
11398/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11399/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11400/// are assumed to be legal.
11401bool
Eric Christopherfd179292009-08-27 18:07:15 +000011402X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011403 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011404 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011405 if (VT.getSizeInBits() == 64)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000011406 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX());
Nate Begeman9008ca62009-04-27 18:41:29 +000011407
Nate Begemana09008b2009-10-19 02:17:23 +000011408 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011409 return (VT.getVectorNumElements() == 2 ||
11410 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11411 isMOVLMask(M, VT) ||
11412 isSHUFPMask(M, VT) ||
11413 isPSHUFDMask(M, VT) ||
11414 isPSHUFHWMask(M, VT) ||
11415 isPSHUFLWMask(M, VT) ||
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000011416 isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()) ||
Craig Topper6347e862011-11-21 06:57:39 +000011417 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11418 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011419 isUNPCKL_v_undef_Mask(M, VT) ||
11420 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011421}
11422
Dan Gohman7d8143f2008-04-09 20:09:42 +000011423bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011424X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011425 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011426 unsigned NumElts = VT.getVectorNumElements();
11427 // FIXME: This collection of masks seems suspect.
11428 if (NumElts == 2)
11429 return true;
11430 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11431 return (isMOVLMask(Mask, VT) ||
11432 isCommutedMOVLMask(Mask, VT, true) ||
11433 isSHUFPMask(Mask, VT) ||
11434 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011435 }
11436 return false;
11437}
11438
11439//===----------------------------------------------------------------------===//
11440// X86 Scheduler Hooks
11441//===----------------------------------------------------------------------===//
11442
Mon P Wang63307c32008-05-05 19:05:59 +000011443// private utility function
11444MachineBasicBlock *
11445X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11446 MachineBasicBlock *MBB,
11447 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011448 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011449 unsigned LoadOpc,
11450 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011451 unsigned notOpc,
11452 unsigned EAXreg,
11453 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011454 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011455 // For the atomic bitwise operator, we generate
11456 // thisMBB:
11457 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011458 // ld t1 = [bitinstr.addr]
11459 // op t2 = t1, [bitinstr.val]
11460 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011461 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11462 // bz newMBB
11463 // fallthrough -->nextMBB
11464 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11465 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011466 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011467 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011468
Mon P Wang63307c32008-05-05 19:05:59 +000011469 /// First build the CFG
11470 MachineFunction *F = MBB->getParent();
11471 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011472 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11473 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11474 F->insert(MBBIter, newMBB);
11475 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011476
Dan Gohman14152b42010-07-06 20:24:04 +000011477 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11478 nextMBB->splice(nextMBB->begin(), thisMBB,
11479 llvm::next(MachineBasicBlock::iterator(bInstr)),
11480 thisMBB->end());
11481 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011482
Mon P Wang63307c32008-05-05 19:05:59 +000011483 // Update thisMBB to fall through to newMBB
11484 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011485
Mon P Wang63307c32008-05-05 19:05:59 +000011486 // newMBB jumps to itself and fall through to nextMBB
11487 newMBB->addSuccessor(nextMBB);
11488 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011489
Mon P Wang63307c32008-05-05 19:05:59 +000011490 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011491 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011492 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011493 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011494 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011495 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011496 int numArgs = bInstr->getNumOperands() - 1;
11497 for (int i=0; i < numArgs; ++i)
11498 argOpers[i] = &bInstr->getOperand(i+1);
11499
11500 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011501 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011502 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011503
Dale Johannesen140be2d2008-08-19 18:47:28 +000011504 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011505 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011506 for (int i=0; i <= lastAddrIndx; ++i)
11507 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011508
Dale Johannesen140be2d2008-08-19 18:47:28 +000011509 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011510 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011511 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011512 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011513 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011514 tt = t1;
11515
Dale Johannesen140be2d2008-08-19 18:47:28 +000011516 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011517 assert((argOpers[valArgIndx]->isReg() ||
11518 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011519 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011520 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011521 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011522 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011523 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011524 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011525 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011526
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011527 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011528 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011529
Dale Johannesene4d209d2009-02-03 20:21:25 +000011530 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011531 for (int i=0; i <= lastAddrIndx; ++i)
11532 (*MIB).addOperand(*argOpers[i]);
11533 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011534 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011535 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11536 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011537
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011538 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011539 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011540
Mon P Wang63307c32008-05-05 19:05:59 +000011541 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011542 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011543
Dan Gohman14152b42010-07-06 20:24:04 +000011544 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011545 return nextMBB;
11546}
11547
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011548// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011549MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011550X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11551 MachineBasicBlock *MBB,
11552 unsigned regOpcL,
11553 unsigned regOpcH,
11554 unsigned immOpcL,
11555 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011556 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011557 // For the atomic bitwise operator, we generate
11558 // thisMBB (instructions are in pairs, except cmpxchg8b)
11559 // ld t1,t2 = [bitinstr.addr]
11560 // newMBB:
11561 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11562 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011563 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011564 // mov ECX, EBX <- t5, t6
11565 // mov EAX, EDX <- t1, t2
11566 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11567 // mov t3, t4 <- EAX, EDX
11568 // bz newMBB
11569 // result in out1, out2
11570 // fallthrough -->nextMBB
11571
11572 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11573 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011574 const unsigned NotOpc = X86::NOT32r;
11575 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11576 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11577 MachineFunction::iterator MBBIter = MBB;
11578 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011579
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011580 /// First build the CFG
11581 MachineFunction *F = MBB->getParent();
11582 MachineBasicBlock *thisMBB = MBB;
11583 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11584 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11585 F->insert(MBBIter, newMBB);
11586 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011587
Dan Gohman14152b42010-07-06 20:24:04 +000011588 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11589 nextMBB->splice(nextMBB->begin(), thisMBB,
11590 llvm::next(MachineBasicBlock::iterator(bInstr)),
11591 thisMBB->end());
11592 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011593
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011594 // Update thisMBB to fall through to newMBB
11595 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011596
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011597 // newMBB jumps to itself and fall through to nextMBB
11598 newMBB->addSuccessor(nextMBB);
11599 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011600
Dale Johannesene4d209d2009-02-03 20:21:25 +000011601 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011602 // Insert instructions into newMBB based on incoming instruction
11603 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011604 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011605 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011606 MachineOperand& dest1Oper = bInstr->getOperand(0);
11607 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011608 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11609 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011610 argOpers[i] = &bInstr->getOperand(i+2);
11611
Dan Gohman71ea4e52010-05-14 21:01:44 +000011612 // We use some of the operands multiple times, so conservatively just
11613 // clear any kill flags that might be present.
11614 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11615 argOpers[i]->setIsKill(false);
11616 }
11617
Evan Chengad5b52f2010-01-08 19:14:57 +000011618 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011619 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011620
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011621 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011622 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011623 for (int i=0; i <= lastAddrIndx; ++i)
11624 (*MIB).addOperand(*argOpers[i]);
11625 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011626 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011627 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011628 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011629 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011630 MachineOperand newOp3 = *(argOpers[3]);
11631 if (newOp3.isImm())
11632 newOp3.setImm(newOp3.getImm()+4);
11633 else
11634 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011635 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011636 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011637
11638 // t3/4 are defined later, at the bottom of the loop
11639 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11640 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011641 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011642 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011643 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011644 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11645
Evan Cheng306b4ca2010-01-08 23:41:50 +000011646 // The subsequent operations should be using the destination registers of
11647 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011648 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011649 t1 = F->getRegInfo().createVirtualRegister(RC);
11650 t2 = F->getRegInfo().createVirtualRegister(RC);
11651 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11652 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011653 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011654 t1 = dest1Oper.getReg();
11655 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011656 }
11657
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011658 int valArgIndx = lastAddrIndx + 1;
11659 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011660 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011661 "invalid operand");
11662 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11663 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011664 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011665 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011666 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011667 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011668 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011669 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011670 (*MIB).addOperand(*argOpers[valArgIndx]);
11671 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011672 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011673 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011674 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011675 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011676 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011677 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011678 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011679 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011680 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011681 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011682
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011683 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011684 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011685 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011686 MIB.addReg(t2);
11687
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011688 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011689 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011690 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011691 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011692
Dale Johannesene4d209d2009-02-03 20:21:25 +000011693 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011694 for (int i=0; i <= lastAddrIndx; ++i)
11695 (*MIB).addOperand(*argOpers[i]);
11696
11697 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011698 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11699 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011700
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011701 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011702 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011703 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011704 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011705
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011706 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011707 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011708
Dan Gohman14152b42010-07-06 20:24:04 +000011709 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011710 return nextMBB;
11711}
11712
11713// private utility function
11714MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011715X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11716 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011717 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011718 // For the atomic min/max operator, we generate
11719 // thisMBB:
11720 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011721 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011722 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011723 // cmp t1, t2
11724 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011725 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011726 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11727 // bz newMBB
11728 // fallthrough -->nextMBB
11729 //
11730 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11731 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011732 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011733 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011734
Mon P Wang63307c32008-05-05 19:05:59 +000011735 /// First build the CFG
11736 MachineFunction *F = MBB->getParent();
11737 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011738 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11739 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11740 F->insert(MBBIter, newMBB);
11741 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011742
Dan Gohman14152b42010-07-06 20:24:04 +000011743 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11744 nextMBB->splice(nextMBB->begin(), thisMBB,
11745 llvm::next(MachineBasicBlock::iterator(mInstr)),
11746 thisMBB->end());
11747 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011748
Mon P Wang63307c32008-05-05 19:05:59 +000011749 // Update thisMBB to fall through to newMBB
11750 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011751
Mon P Wang63307c32008-05-05 19:05:59 +000011752 // newMBB jumps to newMBB and fall through to nextMBB
11753 newMBB->addSuccessor(nextMBB);
11754 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011755
Dale Johannesene4d209d2009-02-03 20:21:25 +000011756 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011757 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011758 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011759 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011760 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011761 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011762 int numArgs = mInstr->getNumOperands() - 1;
11763 for (int i=0; i < numArgs; ++i)
11764 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011765
Mon P Wang63307c32008-05-05 19:05:59 +000011766 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011767 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011768 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011769
Mon P Wangab3e7472008-05-05 22:56:23 +000011770 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011771 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011772 for (int i=0; i <= lastAddrIndx; ++i)
11773 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011774
Mon P Wang63307c32008-05-05 19:05:59 +000011775 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011776 assert((argOpers[valArgIndx]->isReg() ||
11777 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011778 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011779
11780 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011781 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011782 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011783 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011784 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011785 (*MIB).addOperand(*argOpers[valArgIndx]);
11786
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011787 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011788 MIB.addReg(t1);
11789
Dale Johannesene4d209d2009-02-03 20:21:25 +000011790 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011791 MIB.addReg(t1);
11792 MIB.addReg(t2);
11793
11794 // Generate movc
11795 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011796 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011797 MIB.addReg(t2);
11798 MIB.addReg(t1);
11799
11800 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011801 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011802 for (int i=0; i <= lastAddrIndx; ++i)
11803 (*MIB).addOperand(*argOpers[i]);
11804 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011805 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011806 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11807 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011808
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011809 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011810 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011811
Mon P Wang63307c32008-05-05 19:05:59 +000011812 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011813 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011814
Dan Gohman14152b42010-07-06 20:24:04 +000011815 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011816 return nextMBB;
11817}
11818
Eric Christopherf83a5de2009-08-27 18:08:16 +000011819// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011820// or XMM0_V32I8 in AVX all of this code can be replaced with that
11821// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011822MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011823X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011824 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011825 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11826 "Target must have SSE4.2 or AVX features enabled");
11827
Eric Christopherb120ab42009-08-18 22:50:32 +000011828 DebugLoc dl = MI->getDebugLoc();
11829 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011830 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011831 if (!Subtarget->hasAVX()) {
11832 if (memArg)
11833 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11834 else
11835 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11836 } else {
11837 if (memArg)
11838 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11839 else
11840 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11841 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011842
Eric Christopher41c902f2010-11-30 08:20:21 +000011843 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011844 for (unsigned i = 0; i < numArgs; ++i) {
11845 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011846 if (!(Op.isReg() && Op.isImplicit()))
11847 MIB.addOperand(Op);
11848 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011849 BuildMI(*BB, MI, dl,
11850 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11851 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011852 .addReg(X86::XMM0);
11853
Dan Gohman14152b42010-07-06 20:24:04 +000011854 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011855 return BB;
11856}
11857
11858MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011859X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011860 DebugLoc dl = MI->getDebugLoc();
11861 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011862
Eric Christopher228232b2010-11-30 07:20:12 +000011863 // Address into RAX/EAX, other two args into ECX, EDX.
11864 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11865 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11866 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11867 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011868 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011869
Eric Christopher228232b2010-11-30 07:20:12 +000011870 unsigned ValOps = X86::AddrNumOperands;
11871 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11872 .addReg(MI->getOperand(ValOps).getReg());
11873 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11874 .addReg(MI->getOperand(ValOps+1).getReg());
11875
11876 // The instruction doesn't actually take any operands though.
11877 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011878
Eric Christopher228232b2010-11-30 07:20:12 +000011879 MI->eraseFromParent(); // The pseudo is gone now.
11880 return BB;
11881}
11882
11883MachineBasicBlock *
11884X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011885 DebugLoc dl = MI->getDebugLoc();
11886 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011887
Eric Christopher228232b2010-11-30 07:20:12 +000011888 // First arg in ECX, the second in EAX.
11889 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11890 .addReg(MI->getOperand(0).getReg());
11891 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11892 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011893
Eric Christopher228232b2010-11-30 07:20:12 +000011894 // The instruction doesn't actually take any operands though.
11895 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011896
Eric Christopher228232b2010-11-30 07:20:12 +000011897 MI->eraseFromParent(); // The pseudo is gone now.
11898 return BB;
11899}
11900
11901MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011902X86TargetLowering::EmitVAARG64WithCustomInserter(
11903 MachineInstr *MI,
11904 MachineBasicBlock *MBB) const {
11905 // Emit va_arg instruction on X86-64.
11906
11907 // Operands to this pseudo-instruction:
11908 // 0 ) Output : destination address (reg)
11909 // 1-5) Input : va_list address (addr, i64mem)
11910 // 6 ) ArgSize : Size (in bytes) of vararg type
11911 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11912 // 8 ) Align : Alignment of type
11913 // 9 ) EFLAGS (implicit-def)
11914
11915 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11916 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11917
11918 unsigned DestReg = MI->getOperand(0).getReg();
11919 MachineOperand &Base = MI->getOperand(1);
11920 MachineOperand &Scale = MI->getOperand(2);
11921 MachineOperand &Index = MI->getOperand(3);
11922 MachineOperand &Disp = MI->getOperand(4);
11923 MachineOperand &Segment = MI->getOperand(5);
11924 unsigned ArgSize = MI->getOperand(6).getImm();
11925 unsigned ArgMode = MI->getOperand(7).getImm();
11926 unsigned Align = MI->getOperand(8).getImm();
11927
11928 // Memory Reference
11929 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11930 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11931 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11932
11933 // Machine Information
11934 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11935 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11936 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11937 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11938 DebugLoc DL = MI->getDebugLoc();
11939
11940 // struct va_list {
11941 // i32 gp_offset
11942 // i32 fp_offset
11943 // i64 overflow_area (address)
11944 // i64 reg_save_area (address)
11945 // }
11946 // sizeof(va_list) = 24
11947 // alignment(va_list) = 8
11948
11949 unsigned TotalNumIntRegs = 6;
11950 unsigned TotalNumXMMRegs = 8;
11951 bool UseGPOffset = (ArgMode == 1);
11952 bool UseFPOffset = (ArgMode == 2);
11953 unsigned MaxOffset = TotalNumIntRegs * 8 +
11954 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11955
11956 /* Align ArgSize to a multiple of 8 */
11957 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11958 bool NeedsAlign = (Align > 8);
11959
11960 MachineBasicBlock *thisMBB = MBB;
11961 MachineBasicBlock *overflowMBB;
11962 MachineBasicBlock *offsetMBB;
11963 MachineBasicBlock *endMBB;
11964
11965 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11966 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11967 unsigned OffsetReg = 0;
11968
11969 if (!UseGPOffset && !UseFPOffset) {
11970 // If we only pull from the overflow region, we don't create a branch.
11971 // We don't need to alter control flow.
11972 OffsetDestReg = 0; // unused
11973 OverflowDestReg = DestReg;
11974
11975 offsetMBB = NULL;
11976 overflowMBB = thisMBB;
11977 endMBB = thisMBB;
11978 } else {
11979 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11980 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11981 // If not, pull from overflow_area. (branch to overflowMBB)
11982 //
11983 // thisMBB
11984 // | .
11985 // | .
11986 // offsetMBB overflowMBB
11987 // | .
11988 // | .
11989 // endMBB
11990
11991 // Registers for the PHI in endMBB
11992 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11993 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11994
11995 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11996 MachineFunction *MF = MBB->getParent();
11997 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11998 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11999 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12000
12001 MachineFunction::iterator MBBIter = MBB;
12002 ++MBBIter;
12003
12004 // Insert the new basic blocks
12005 MF->insert(MBBIter, offsetMBB);
12006 MF->insert(MBBIter, overflowMBB);
12007 MF->insert(MBBIter, endMBB);
12008
12009 // Transfer the remainder of MBB and its successor edges to endMBB.
12010 endMBB->splice(endMBB->begin(), thisMBB,
12011 llvm::next(MachineBasicBlock::iterator(MI)),
12012 thisMBB->end());
12013 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12014
12015 // Make offsetMBB and overflowMBB successors of thisMBB
12016 thisMBB->addSuccessor(offsetMBB);
12017 thisMBB->addSuccessor(overflowMBB);
12018
12019 // endMBB is a successor of both offsetMBB and overflowMBB
12020 offsetMBB->addSuccessor(endMBB);
12021 overflowMBB->addSuccessor(endMBB);
12022
12023 // Load the offset value into a register
12024 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12025 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12026 .addOperand(Base)
12027 .addOperand(Scale)
12028 .addOperand(Index)
12029 .addDisp(Disp, UseFPOffset ? 4 : 0)
12030 .addOperand(Segment)
12031 .setMemRefs(MMOBegin, MMOEnd);
12032
12033 // Check if there is enough room left to pull this argument.
12034 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12035 .addReg(OffsetReg)
12036 .addImm(MaxOffset + 8 - ArgSizeA8);
12037
12038 // Branch to "overflowMBB" if offset >= max
12039 // Fall through to "offsetMBB" otherwise
12040 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12041 .addMBB(overflowMBB);
12042 }
12043
12044 // In offsetMBB, emit code to use the reg_save_area.
12045 if (offsetMBB) {
12046 assert(OffsetReg != 0);
12047
12048 // Read the reg_save_area address.
12049 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12050 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12051 .addOperand(Base)
12052 .addOperand(Scale)
12053 .addOperand(Index)
12054 .addDisp(Disp, 16)
12055 .addOperand(Segment)
12056 .setMemRefs(MMOBegin, MMOEnd);
12057
12058 // Zero-extend the offset
12059 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12060 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12061 .addImm(0)
12062 .addReg(OffsetReg)
12063 .addImm(X86::sub_32bit);
12064
12065 // Add the offset to the reg_save_area to get the final address.
12066 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12067 .addReg(OffsetReg64)
12068 .addReg(RegSaveReg);
12069
12070 // Compute the offset for the next argument
12071 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12072 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12073 .addReg(OffsetReg)
12074 .addImm(UseFPOffset ? 16 : 8);
12075
12076 // Store it back into the va_list.
12077 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12078 .addOperand(Base)
12079 .addOperand(Scale)
12080 .addOperand(Index)
12081 .addDisp(Disp, UseFPOffset ? 4 : 0)
12082 .addOperand(Segment)
12083 .addReg(NextOffsetReg)
12084 .setMemRefs(MMOBegin, MMOEnd);
12085
12086 // Jump to endMBB
12087 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12088 .addMBB(endMBB);
12089 }
12090
12091 //
12092 // Emit code to use overflow area
12093 //
12094
12095 // Load the overflow_area address into a register.
12096 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12097 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12098 .addOperand(Base)
12099 .addOperand(Scale)
12100 .addOperand(Index)
12101 .addDisp(Disp, 8)
12102 .addOperand(Segment)
12103 .setMemRefs(MMOBegin, MMOEnd);
12104
12105 // If we need to align it, do so. Otherwise, just copy the address
12106 // to OverflowDestReg.
12107 if (NeedsAlign) {
12108 // Align the overflow address
12109 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12110 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12111
12112 // aligned_addr = (addr + (align-1)) & ~(align-1)
12113 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12114 .addReg(OverflowAddrReg)
12115 .addImm(Align-1);
12116
12117 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12118 .addReg(TmpReg)
12119 .addImm(~(uint64_t)(Align-1));
12120 } else {
12121 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12122 .addReg(OverflowAddrReg);
12123 }
12124
12125 // Compute the next overflow address after this argument.
12126 // (the overflow address should be kept 8-byte aligned)
12127 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12128 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12129 .addReg(OverflowDestReg)
12130 .addImm(ArgSizeA8);
12131
12132 // Store the new overflow address.
12133 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12134 .addOperand(Base)
12135 .addOperand(Scale)
12136 .addOperand(Index)
12137 .addDisp(Disp, 8)
12138 .addOperand(Segment)
12139 .addReg(NextAddrReg)
12140 .setMemRefs(MMOBegin, MMOEnd);
12141
12142 // If we branched, emit the PHI to the front of endMBB.
12143 if (offsetMBB) {
12144 BuildMI(*endMBB, endMBB->begin(), DL,
12145 TII->get(X86::PHI), DestReg)
12146 .addReg(OffsetDestReg).addMBB(offsetMBB)
12147 .addReg(OverflowDestReg).addMBB(overflowMBB);
12148 }
12149
12150 // Erase the pseudo instruction
12151 MI->eraseFromParent();
12152
12153 return endMBB;
12154}
12155
12156MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012157X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12158 MachineInstr *MI,
12159 MachineBasicBlock *MBB) const {
12160 // Emit code to save XMM registers to the stack. The ABI says that the
12161 // number of registers to save is given in %al, so it's theoretically
12162 // possible to do an indirect jump trick to avoid saving all of them,
12163 // however this code takes a simpler approach and just executes all
12164 // of the stores if %al is non-zero. It's less code, and it's probably
12165 // easier on the hardware branch predictor, and stores aren't all that
12166 // expensive anyway.
12167
12168 // Create the new basic blocks. One block contains all the XMM stores,
12169 // and one block is the final destination regardless of whether any
12170 // stores were performed.
12171 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12172 MachineFunction *F = MBB->getParent();
12173 MachineFunction::iterator MBBIter = MBB;
12174 ++MBBIter;
12175 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12176 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12177 F->insert(MBBIter, XMMSaveMBB);
12178 F->insert(MBBIter, EndMBB);
12179
Dan Gohman14152b42010-07-06 20:24:04 +000012180 // Transfer the remainder of MBB and its successor edges to EndMBB.
12181 EndMBB->splice(EndMBB->begin(), MBB,
12182 llvm::next(MachineBasicBlock::iterator(MI)),
12183 MBB->end());
12184 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12185
Dan Gohmand6708ea2009-08-15 01:38:56 +000012186 // The original block will now fall through to the XMM save block.
12187 MBB->addSuccessor(XMMSaveMBB);
12188 // The XMMSaveMBB will fall through to the end block.
12189 XMMSaveMBB->addSuccessor(EndMBB);
12190
12191 // Now add the instructions.
12192 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12193 DebugLoc DL = MI->getDebugLoc();
12194
12195 unsigned CountReg = MI->getOperand(0).getReg();
12196 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12197 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12198
12199 if (!Subtarget->isTargetWin64()) {
12200 // If %al is 0, branch around the XMM save block.
12201 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012202 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012203 MBB->addSuccessor(EndMBB);
12204 }
12205
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012206 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012207 // In the XMM save block, save all the XMM argument registers.
12208 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12209 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012210 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012211 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012212 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012213 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012214 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012215 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012216 .addFrameIndex(RegSaveFrameIndex)
12217 .addImm(/*Scale=*/1)
12218 .addReg(/*IndexReg=*/0)
12219 .addImm(/*Disp=*/Offset)
12220 .addReg(/*Segment=*/0)
12221 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012222 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012223 }
12224
Dan Gohman14152b42010-07-06 20:24:04 +000012225 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012226
12227 return EndMBB;
12228}
Mon P Wang63307c32008-05-05 19:05:59 +000012229
Evan Cheng60c07e12006-07-05 22:17:51 +000012230MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012231X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012232 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012233 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12234 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012235
Chris Lattner52600972009-09-02 05:57:00 +000012236 // To "insert" a SELECT_CC instruction, we actually have to insert the
12237 // diamond control-flow pattern. The incoming instruction knows the
12238 // destination vreg to set, the condition code register to branch on, the
12239 // true/false values to select between, and a branch opcode to use.
12240 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12241 MachineFunction::iterator It = BB;
12242 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012243
Chris Lattner52600972009-09-02 05:57:00 +000012244 // thisMBB:
12245 // ...
12246 // TrueVal = ...
12247 // cmpTY ccX, r1, r2
12248 // bCC copy1MBB
12249 // fallthrough --> copy0MBB
12250 MachineBasicBlock *thisMBB = BB;
12251 MachineFunction *F = BB->getParent();
12252 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12253 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012254 F->insert(It, copy0MBB);
12255 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012256
Bill Wendling730c07e2010-06-25 20:48:10 +000012257 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12258 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000012259 if (!MI->killsRegister(X86::EFLAGS)) {
12260 copy0MBB->addLiveIn(X86::EFLAGS);
12261 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012262 }
12263
Dan Gohman14152b42010-07-06 20:24:04 +000012264 // Transfer the remainder of BB and its successor edges to sinkMBB.
12265 sinkMBB->splice(sinkMBB->begin(), BB,
12266 llvm::next(MachineBasicBlock::iterator(MI)),
12267 BB->end());
12268 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12269
12270 // Add the true and fallthrough blocks as its successors.
12271 BB->addSuccessor(copy0MBB);
12272 BB->addSuccessor(sinkMBB);
12273
12274 // Create the conditional branch instruction.
12275 unsigned Opc =
12276 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12277 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12278
Chris Lattner52600972009-09-02 05:57:00 +000012279 // copy0MBB:
12280 // %FalseValue = ...
12281 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012282 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012283
Chris Lattner52600972009-09-02 05:57:00 +000012284 // sinkMBB:
12285 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12286 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012287 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12288 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012289 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12290 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12291
Dan Gohman14152b42010-07-06 20:24:04 +000012292 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012293 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012294}
12295
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012296MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012297X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12298 bool Is64Bit) const {
12299 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12300 DebugLoc DL = MI->getDebugLoc();
12301 MachineFunction *MF = BB->getParent();
12302 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12303
12304 assert(EnableSegmentedStacks);
12305
12306 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12307 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12308
12309 // BB:
12310 // ... [Till the alloca]
12311 // If stacklet is not large enough, jump to mallocMBB
12312 //
12313 // bumpMBB:
12314 // Allocate by subtracting from RSP
12315 // Jump to continueMBB
12316 //
12317 // mallocMBB:
12318 // Allocate by call to runtime
12319 //
12320 // continueMBB:
12321 // ...
12322 // [rest of original BB]
12323 //
12324
12325 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12326 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12327 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12328
12329 MachineRegisterInfo &MRI = MF->getRegInfo();
12330 const TargetRegisterClass *AddrRegClass =
12331 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12332
12333 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12334 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12335 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012336 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012337 sizeVReg = MI->getOperand(1).getReg(),
12338 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12339
12340 MachineFunction::iterator MBBIter = BB;
12341 ++MBBIter;
12342
12343 MF->insert(MBBIter, bumpMBB);
12344 MF->insert(MBBIter, mallocMBB);
12345 MF->insert(MBBIter, continueMBB);
12346
12347 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12348 (MachineBasicBlock::iterator(MI)), BB->end());
12349 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12350
12351 // Add code to the main basic block to check if the stack limit has been hit,
12352 // and if so, jump to mallocMBB otherwise to bumpMBB.
12353 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012354 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012355 .addReg(tmpSPVReg).addReg(sizeVReg);
12356 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
12357 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012358 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012359 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12360
12361 // bumpMBB simply decreases the stack pointer, since we know the current
12362 // stacklet has enough space.
12363 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012364 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012365 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012366 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012367 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12368
12369 // Calls into a routine in libgcc to allocate more space from the heap.
12370 if (Is64Bit) {
12371 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12372 .addReg(sizeVReg);
12373 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12374 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12375 } else {
12376 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12377 .addImm(12);
12378 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12379 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12380 .addExternalSymbol("__morestack_allocate_stack_space");
12381 }
12382
12383 if (!Is64Bit)
12384 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12385 .addImm(16);
12386
12387 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12388 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12389 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12390
12391 // Set up the CFG correctly.
12392 BB->addSuccessor(bumpMBB);
12393 BB->addSuccessor(mallocMBB);
12394 mallocMBB->addSuccessor(continueMBB);
12395 bumpMBB->addSuccessor(continueMBB);
12396
12397 // Take care of the PHI nodes.
12398 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12399 MI->getOperand(0).getReg())
12400 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12401 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12402
12403 // Delete the original pseudo instruction.
12404 MI->eraseFromParent();
12405
12406 // And we're done.
12407 return continueMBB;
12408}
12409
12410MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012411X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012412 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012413 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12414 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012415
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012416 assert(!Subtarget->isTargetEnvMacho());
12417
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012418 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12419 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012420
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012421 if (Subtarget->isTargetWin64()) {
12422 if (Subtarget->isTargetCygMing()) {
12423 // ___chkstk(Mingw64):
12424 // Clobbers R10, R11, RAX and EFLAGS.
12425 // Updates RSP.
12426 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12427 .addExternalSymbol("___chkstk")
12428 .addReg(X86::RAX, RegState::Implicit)
12429 .addReg(X86::RSP, RegState::Implicit)
12430 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12431 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12432 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12433 } else {
12434 // __chkstk(MSVCRT): does not update stack pointer.
12435 // Clobbers R10, R11 and EFLAGS.
12436 // FIXME: RAX(allocated size) might be reused and not killed.
12437 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12438 .addExternalSymbol("__chkstk")
12439 .addReg(X86::RAX, RegState::Implicit)
12440 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12441 // RAX has the offset to subtracted from RSP.
12442 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12443 .addReg(X86::RSP)
12444 .addReg(X86::RAX);
12445 }
12446 } else {
12447 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012448 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12449
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012450 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12451 .addExternalSymbol(StackProbeSymbol)
12452 .addReg(X86::EAX, RegState::Implicit)
12453 .addReg(X86::ESP, RegState::Implicit)
12454 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12455 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12456 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12457 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012458
Dan Gohman14152b42010-07-06 20:24:04 +000012459 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012460 return BB;
12461}
Chris Lattner52600972009-09-02 05:57:00 +000012462
12463MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012464X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12465 MachineBasicBlock *BB) const {
12466 // This is pretty easy. We're taking the value that we received from
12467 // our load from the relocation, sticking it in either RDI (x86-64)
12468 // or EAX and doing an indirect call. The return value will then
12469 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012470 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012471 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012472 DebugLoc DL = MI->getDebugLoc();
12473 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012474
12475 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012476 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012477
Eric Christopher30ef0e52010-06-03 04:07:48 +000012478 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012479 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12480 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012481 .addReg(X86::RIP)
12482 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012483 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012484 MI->getOperand(3).getTargetFlags())
12485 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012486 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012487 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012488 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012489 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12490 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012491 .addReg(0)
12492 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012493 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012494 MI->getOperand(3).getTargetFlags())
12495 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012496 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012497 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012498 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012499 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12500 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012501 .addReg(TII->getGlobalBaseReg(F))
12502 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012503 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012504 MI->getOperand(3).getTargetFlags())
12505 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012506 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012507 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012508 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012509
Dan Gohman14152b42010-07-06 20:24:04 +000012510 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012511 return BB;
12512}
12513
12514MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012515X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012516 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012517 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000012518 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012519 case X86::TAILJMPd64:
12520 case X86::TAILJMPr64:
12521 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000012522 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012523 case X86::TCRETURNdi64:
12524 case X86::TCRETURNri64:
12525 case X86::TCRETURNmi64:
12526 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12527 // On AMD64, additional defs should be added before register allocation.
12528 if (!Subtarget->isTargetWin64()) {
12529 MI->addRegisterDefined(X86::RSI);
12530 MI->addRegisterDefined(X86::RDI);
12531 MI->addRegisterDefined(X86::XMM6);
12532 MI->addRegisterDefined(X86::XMM7);
12533 MI->addRegisterDefined(X86::XMM8);
12534 MI->addRegisterDefined(X86::XMM9);
12535 MI->addRegisterDefined(X86::XMM10);
12536 MI->addRegisterDefined(X86::XMM11);
12537 MI->addRegisterDefined(X86::XMM12);
12538 MI->addRegisterDefined(X86::XMM13);
12539 MI->addRegisterDefined(X86::XMM14);
12540 MI->addRegisterDefined(X86::XMM15);
12541 }
12542 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012543 case X86::WIN_ALLOCA:
12544 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012545 case X86::SEG_ALLOCA_32:
12546 return EmitLoweredSegAlloca(MI, BB, false);
12547 case X86::SEG_ALLOCA_64:
12548 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012549 case X86::TLSCall_32:
12550 case X86::TLSCall_64:
12551 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012552 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012553 case X86::CMOV_FR32:
12554 case X86::CMOV_FR64:
12555 case X86::CMOV_V4F32:
12556 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012557 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012558 case X86::CMOV_V8F32:
12559 case X86::CMOV_V4F64:
12560 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012561 case X86::CMOV_GR16:
12562 case X86::CMOV_GR32:
12563 case X86::CMOV_RFP32:
12564 case X86::CMOV_RFP64:
12565 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012566 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012567
Dale Johannesen849f2142007-07-03 00:53:03 +000012568 case X86::FP32_TO_INT16_IN_MEM:
12569 case X86::FP32_TO_INT32_IN_MEM:
12570 case X86::FP32_TO_INT64_IN_MEM:
12571 case X86::FP64_TO_INT16_IN_MEM:
12572 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012573 case X86::FP64_TO_INT64_IN_MEM:
12574 case X86::FP80_TO_INT16_IN_MEM:
12575 case X86::FP80_TO_INT32_IN_MEM:
12576 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012577 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12578 DebugLoc DL = MI->getDebugLoc();
12579
Evan Cheng60c07e12006-07-05 22:17:51 +000012580 // Change the floating point control register to use "round towards zero"
12581 // mode when truncating to an integer value.
12582 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012583 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012584 addFrameReference(BuildMI(*BB, MI, DL,
12585 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012586
12587 // Load the old value of the high byte of the control word...
12588 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012589 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012590 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012591 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012592
12593 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012594 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012595 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012596
12597 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012598 addFrameReference(BuildMI(*BB, MI, DL,
12599 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012600
12601 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012602 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012603 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012604
12605 // Get the X86 opcode to use.
12606 unsigned Opc;
12607 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012608 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012609 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12610 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12611 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12612 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12613 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12614 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012615 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12616 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12617 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012618 }
12619
12620 X86AddressMode AM;
12621 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012622 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012623 AM.BaseType = X86AddressMode::RegBase;
12624 AM.Base.Reg = Op.getReg();
12625 } else {
12626 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012627 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012628 }
12629 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012630 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012631 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012632 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012633 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012634 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012635 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012636 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012637 AM.GV = Op.getGlobal();
12638 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012639 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012640 }
Dan Gohman14152b42010-07-06 20:24:04 +000012641 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012642 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012643
12644 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012645 addFrameReference(BuildMI(*BB, MI, DL,
12646 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012647
Dan Gohman14152b42010-07-06 20:24:04 +000012648 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012649 return BB;
12650 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012651 // String/text processing lowering.
12652 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012653 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012654 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12655 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012656 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012657 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12658 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012659 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012660 return EmitPCMP(MI, BB, 5, false /* in mem */);
12661 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012662 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012663 return EmitPCMP(MI, BB, 5, true /* in mem */);
12664
Eric Christopher228232b2010-11-30 07:20:12 +000012665 // Thread synchronization.
12666 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012667 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012668 case X86::MWAIT:
12669 return EmitMwait(MI, BB);
12670
Eric Christopherb120ab42009-08-18 22:50:32 +000012671 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012672 case X86::ATOMAND32:
12673 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012674 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012675 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012676 X86::NOT32r, X86::EAX,
12677 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012678 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012679 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12680 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012681 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012682 X86::NOT32r, X86::EAX,
12683 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012684 case X86::ATOMXOR32:
12685 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012686 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012687 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012688 X86::NOT32r, X86::EAX,
12689 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012690 case X86::ATOMNAND32:
12691 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012692 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012693 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012694 X86::NOT32r, X86::EAX,
12695 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012696 case X86::ATOMMIN32:
12697 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12698 case X86::ATOMMAX32:
12699 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12700 case X86::ATOMUMIN32:
12701 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12702 case X86::ATOMUMAX32:
12703 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012704
12705 case X86::ATOMAND16:
12706 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12707 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012708 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012709 X86::NOT16r, X86::AX,
12710 X86::GR16RegisterClass);
12711 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012712 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012713 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012714 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012715 X86::NOT16r, X86::AX,
12716 X86::GR16RegisterClass);
12717 case X86::ATOMXOR16:
12718 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12719 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012720 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012721 X86::NOT16r, X86::AX,
12722 X86::GR16RegisterClass);
12723 case X86::ATOMNAND16:
12724 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12725 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012726 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012727 X86::NOT16r, X86::AX,
12728 X86::GR16RegisterClass, true);
12729 case X86::ATOMMIN16:
12730 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12731 case X86::ATOMMAX16:
12732 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12733 case X86::ATOMUMIN16:
12734 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12735 case X86::ATOMUMAX16:
12736 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12737
12738 case X86::ATOMAND8:
12739 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12740 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012741 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012742 X86::NOT8r, X86::AL,
12743 X86::GR8RegisterClass);
12744 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012745 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012746 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012747 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012748 X86::NOT8r, X86::AL,
12749 X86::GR8RegisterClass);
12750 case X86::ATOMXOR8:
12751 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12752 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012753 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012754 X86::NOT8r, X86::AL,
12755 X86::GR8RegisterClass);
12756 case X86::ATOMNAND8:
12757 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12758 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012759 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012760 X86::NOT8r, X86::AL,
12761 X86::GR8RegisterClass, true);
12762 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012763 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012764 case X86::ATOMAND64:
12765 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012766 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012767 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012768 X86::NOT64r, X86::RAX,
12769 X86::GR64RegisterClass);
12770 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012771 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12772 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012773 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012774 X86::NOT64r, X86::RAX,
12775 X86::GR64RegisterClass);
12776 case X86::ATOMXOR64:
12777 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012778 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012779 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012780 X86::NOT64r, X86::RAX,
12781 X86::GR64RegisterClass);
12782 case X86::ATOMNAND64:
12783 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12784 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012785 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012786 X86::NOT64r, X86::RAX,
12787 X86::GR64RegisterClass, true);
12788 case X86::ATOMMIN64:
12789 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12790 case X86::ATOMMAX64:
12791 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12792 case X86::ATOMUMIN64:
12793 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12794 case X86::ATOMUMAX64:
12795 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012796
12797 // This group does 64-bit operations on a 32-bit host.
12798 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012799 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012800 X86::AND32rr, X86::AND32rr,
12801 X86::AND32ri, X86::AND32ri,
12802 false);
12803 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012804 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012805 X86::OR32rr, X86::OR32rr,
12806 X86::OR32ri, X86::OR32ri,
12807 false);
12808 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012809 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012810 X86::XOR32rr, X86::XOR32rr,
12811 X86::XOR32ri, X86::XOR32ri,
12812 false);
12813 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012814 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012815 X86::AND32rr, X86::AND32rr,
12816 X86::AND32ri, X86::AND32ri,
12817 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012818 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012819 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012820 X86::ADD32rr, X86::ADC32rr,
12821 X86::ADD32ri, X86::ADC32ri,
12822 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012823 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012824 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012825 X86::SUB32rr, X86::SBB32rr,
12826 X86::SUB32ri, X86::SBB32ri,
12827 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012828 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012829 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012830 X86::MOV32rr, X86::MOV32rr,
12831 X86::MOV32ri, X86::MOV32ri,
12832 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012833 case X86::VASTART_SAVE_XMM_REGS:
12834 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012835
12836 case X86::VAARG_64:
12837 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012838 }
12839}
12840
12841//===----------------------------------------------------------------------===//
12842// X86 Optimization Hooks
12843//===----------------------------------------------------------------------===//
12844
Dan Gohman475871a2008-07-27 21:46:04 +000012845void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012846 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012847 APInt &KnownZero,
12848 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012849 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012850 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012851 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012852 assert((Opc >= ISD::BUILTIN_OP_END ||
12853 Opc == ISD::INTRINSIC_WO_CHAIN ||
12854 Opc == ISD::INTRINSIC_W_CHAIN ||
12855 Opc == ISD::INTRINSIC_VOID) &&
12856 "Should use MaskedValueIsZero if you don't know whether Op"
12857 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012858
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012859 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012860 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012861 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012862 case X86ISD::ADD:
12863 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012864 case X86ISD::ADC:
12865 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012866 case X86ISD::SMUL:
12867 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012868 case X86ISD::INC:
12869 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012870 case X86ISD::OR:
12871 case X86ISD::XOR:
12872 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012873 // These nodes' second result is a boolean.
12874 if (Op.getResNo() == 0)
12875 break;
12876 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012877 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012878 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12879 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012880 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012881 case ISD::INTRINSIC_WO_CHAIN: {
12882 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12883 unsigned NumLoBits = 0;
12884 switch (IntId) {
12885 default: break;
12886 case Intrinsic::x86_sse_movmsk_ps:
12887 case Intrinsic::x86_avx_movmsk_ps_256:
12888 case Intrinsic::x86_sse2_movmsk_pd:
12889 case Intrinsic::x86_avx_movmsk_pd_256:
12890 case Intrinsic::x86_mmx_pmovmskb:
12891 case Intrinsic::x86_sse2_pmovmskb_128: {
12892 // High bits of movmskp{s|d}, pmovmskb are known zero.
12893 switch (IntId) {
12894 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12895 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12896 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12897 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12898 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12899 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
12900 }
12901 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12902 Mask.getBitWidth() - NumLoBits);
12903 break;
12904 }
12905 }
12906 break;
12907 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012908 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012909}
Chris Lattner259e97c2006-01-31 19:43:35 +000012910
Owen Andersonbc146b02010-09-21 20:42:50 +000012911unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12912 unsigned Depth) const {
12913 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12914 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12915 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012916
Owen Andersonbc146b02010-09-21 20:42:50 +000012917 // Fallback case.
12918 return 1;
12919}
12920
Evan Cheng206ee9d2006-07-07 08:33:52 +000012921/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012922/// node is a GlobalAddress + offset.
12923bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012924 const GlobalValue* &GA,
12925 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012926 if (N->getOpcode() == X86ISD::Wrapper) {
12927 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012928 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012929 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012930 return true;
12931 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012932 }
Evan Chengad4196b2008-05-12 19:56:52 +000012933 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012934}
12935
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012936/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12937/// same as extracting the high 128-bit part of 256-bit vector and then
12938/// inserting the result into the low part of a new 256-bit vector
12939static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12940 EVT VT = SVOp->getValueType(0);
12941 int NumElems = VT.getVectorNumElements();
12942
12943 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12944 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12945 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12946 SVOp->getMaskElt(j) >= 0)
12947 return false;
12948
12949 return true;
12950}
12951
12952/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12953/// same as extracting the low 128-bit part of 256-bit vector and then
12954/// inserting the result into the high part of a new 256-bit vector
12955static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12956 EVT VT = SVOp->getValueType(0);
12957 int NumElems = VT.getVectorNumElements();
12958
12959 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12960 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12961 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12962 SVOp->getMaskElt(j) >= 0)
12963 return false;
12964
12965 return true;
12966}
12967
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012968/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12969static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12970 TargetLowering::DAGCombinerInfo &DCI) {
12971 DebugLoc dl = N->getDebugLoc();
12972 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12973 SDValue V1 = SVOp->getOperand(0);
12974 SDValue V2 = SVOp->getOperand(1);
12975 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012976 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012977
12978 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12979 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12980 //
12981 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012982 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012983 // V UNDEF BUILD_VECTOR UNDEF
12984 // \ / \ /
12985 // CONCAT_VECTOR CONCAT_VECTOR
12986 // \ /
12987 // \ /
12988 // RESULT: V + zero extended
12989 //
12990 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12991 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12992 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12993 return SDValue();
12994
12995 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12996 return SDValue();
12997
12998 // To match the shuffle mask, the first half of the mask should
12999 // be exactly the first vector, and all the rest a splat with the
13000 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013001 for (int i = 0; i < NumElems/2; ++i)
13002 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
13003 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
13004 return SDValue();
13005
13006 // Emit a zeroed vector and insert the desired subvector on its
13007 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013008 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013009 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
13010 DAG.getConstant(0, MVT::i32), DAG, dl);
13011 return DCI.CombineTo(N, InsV);
13012 }
13013
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013014 //===--------------------------------------------------------------------===//
13015 // Combine some shuffles into subvector extracts and inserts:
13016 //
13017
13018 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13019 if (isShuffleHigh128VectorInsertLow(SVOp)) {
13020 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
13021 DAG, dl);
13022 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
13023 V, DAG.getConstant(0, MVT::i32), DAG, dl);
13024 return DCI.CombineTo(N, InsV);
13025 }
13026
13027 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13028 if (isShuffleLow128VectorInsertHigh(SVOp)) {
13029 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
13030 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
13031 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
13032 return DCI.CombineTo(N, InsV);
13033 }
13034
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013035 return SDValue();
13036}
13037
13038/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013039static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013040 TargetLowering::DAGCombinerInfo &DCI,
13041 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013042 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013043 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013044
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013045 // Don't create instructions with illegal types after legalize types has run.
13046 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13047 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
13048 return SDValue();
13049
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013050 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
13051 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
13052 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013053 return PerformShuffleCombine256(N, DAG, DCI);
13054
13055 // Only handle 128 wide vector from here on.
13056 if (VT.getSizeInBits() != 128)
13057 return SDValue();
13058
13059 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
13060 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
13061 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000013062 SmallVector<SDValue, 16> Elts;
13063 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013064 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000013065
Nate Begemanfdea31a2010-03-24 20:49:50 +000013066 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000013067}
Evan Chengd880b972008-05-09 21:53:03 +000013068
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000013069/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
13070/// generation and convert it from being a bunch of shuffles and extracts
13071/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013072static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
13073 const TargetLowering &TLI) {
13074 SDValue InputVector = N->getOperand(0);
13075
13076 // Only operate on vectors of 4 elements, where the alternative shuffling
13077 // gets to be more expensive.
13078 if (InputVector.getValueType() != MVT::v4i32)
13079 return SDValue();
13080
13081 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
13082 // single use which is a sign-extend or zero-extend, and all elements are
13083 // used.
13084 SmallVector<SDNode *, 4> Uses;
13085 unsigned ExtractedElements = 0;
13086 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
13087 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
13088 if (UI.getUse().getResNo() != InputVector.getResNo())
13089 return SDValue();
13090
13091 SDNode *Extract = *UI;
13092 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13093 return SDValue();
13094
13095 if (Extract->getValueType(0) != MVT::i32)
13096 return SDValue();
13097 if (!Extract->hasOneUse())
13098 return SDValue();
13099 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
13100 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
13101 return SDValue();
13102 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
13103 return SDValue();
13104
13105 // Record which element was extracted.
13106 ExtractedElements |=
13107 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
13108
13109 Uses.push_back(Extract);
13110 }
13111
13112 // If not all the elements were used, this may not be worthwhile.
13113 if (ExtractedElements != 15)
13114 return SDValue();
13115
13116 // Ok, we've now decided to do the transformation.
13117 DebugLoc dl = InputVector.getDebugLoc();
13118
13119 // Store the value to a temporary stack slot.
13120 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013121 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13122 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013123
13124 // Replace each use (extract) with a load of the appropriate element.
13125 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13126 UE = Uses.end(); UI != UE; ++UI) {
13127 SDNode *Extract = *UI;
13128
Nadav Rotem86694292011-05-17 08:31:57 +000013129 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013130 SDValue Idx = Extract->getOperand(1);
13131 unsigned EltSize =
13132 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
13133 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
13134 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
13135
Nadav Rotem86694292011-05-17 08:31:57 +000013136 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013137 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013138
13139 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013140 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013141 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013142 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013143
13144 // Replace the exact with the load.
13145 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13146 }
13147
13148 // The replacement was made in place; don't return anything.
13149 return SDValue();
13150}
13151
Duncan Sands6bcd2192011-09-17 16:49:39 +000013152/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13153/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013154static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013155 const X86Subtarget *Subtarget) {
13156 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013157 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013158 // Get the LHS/RHS of the select.
13159 SDValue LHS = N->getOperand(1);
13160 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013161 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013162
Dan Gohman670e5392009-09-21 18:03:22 +000013163 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013164 // instructions match the semantics of the common C idiom x<y?x:y but not
13165 // x<=y?x:y, because of how they handle negative zero (which can be
13166 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013167 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13168 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
13169 (Subtarget->hasXMMInt() ||
13170 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013171 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013172
Chris Lattner47b4ce82009-03-11 05:48:52 +000013173 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013174 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013175 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13176 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013177 switch (CC) {
13178 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013179 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013180 // Converting this to a min would handle NaNs incorrectly, and swapping
13181 // the operands would cause it to handle comparisons between positive
13182 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013183 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000013184 if (!UnsafeFPMath &&
13185 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13186 break;
13187 std::swap(LHS, RHS);
13188 }
Dan Gohman670e5392009-09-21 18:03:22 +000013189 Opcode = X86ISD::FMIN;
13190 break;
13191 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013192 // Converting this to a min would handle comparisons between positive
13193 // and negative zero incorrectly.
13194 if (!UnsafeFPMath &&
13195 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13196 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013197 Opcode = X86ISD::FMIN;
13198 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013199 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013200 // Converting this to a min would handle both negative zeros and NaNs
13201 // incorrectly, but we can swap the operands to fix both.
13202 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013203 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013204 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013205 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013206 Opcode = X86ISD::FMIN;
13207 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013208
Dan Gohman670e5392009-09-21 18:03:22 +000013209 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013210 // Converting this to a max would handle comparisons between positive
13211 // and negative zero incorrectly.
13212 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013213 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013214 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013215 Opcode = X86ISD::FMAX;
13216 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013217 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013218 // Converting this to a max would handle NaNs incorrectly, and swapping
13219 // the operands would cause it to handle comparisons between positive
13220 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013221 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000013222 if (!UnsafeFPMath &&
13223 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13224 break;
13225 std::swap(LHS, RHS);
13226 }
Dan Gohman670e5392009-09-21 18:03:22 +000013227 Opcode = X86ISD::FMAX;
13228 break;
13229 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013230 // Converting this to a max would handle both negative zeros and NaNs
13231 // incorrectly, but we can swap the operands to fix both.
13232 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013233 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013234 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013235 case ISD::SETGE:
13236 Opcode = X86ISD::FMAX;
13237 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013238 }
Dan Gohman670e5392009-09-21 18:03:22 +000013239 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013240 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13241 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013242 switch (CC) {
13243 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013244 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013245 // Converting this to a min would handle comparisons between positive
13246 // and negative zero incorrectly, and swapping the operands would
13247 // cause it to handle NaNs incorrectly.
13248 if (!UnsafeFPMath &&
13249 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013250 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013251 break;
13252 std::swap(LHS, RHS);
13253 }
Dan Gohman670e5392009-09-21 18:03:22 +000013254 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013255 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013256 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013257 // Converting this to a min would handle NaNs incorrectly.
13258 if (!UnsafeFPMath &&
13259 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13260 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013261 Opcode = X86ISD::FMIN;
13262 break;
13263 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013264 // Converting this to a min would handle both negative zeros and NaNs
13265 // incorrectly, but we can swap the operands to fix both.
13266 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013267 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013268 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013269 case ISD::SETGE:
13270 Opcode = X86ISD::FMIN;
13271 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013272
Dan Gohman670e5392009-09-21 18:03:22 +000013273 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013274 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013275 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013276 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013277 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013278 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013279 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013280 // Converting this to a max would handle comparisons between positive
13281 // and negative zero incorrectly, and swapping the operands would
13282 // cause it to handle NaNs incorrectly.
13283 if (!UnsafeFPMath &&
13284 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013285 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013286 break;
13287 std::swap(LHS, RHS);
13288 }
Dan Gohman670e5392009-09-21 18:03:22 +000013289 Opcode = X86ISD::FMAX;
13290 break;
13291 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013292 // Converting this to a max would handle both negative zeros and NaNs
13293 // incorrectly, but we can swap the operands to fix both.
13294 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013295 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013296 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013297 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013298 Opcode = X86ISD::FMAX;
13299 break;
13300 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013301 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013302
Chris Lattner47b4ce82009-03-11 05:48:52 +000013303 if (Opcode)
13304 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013305 }
Eric Christopherfd179292009-08-27 18:07:15 +000013306
Chris Lattnerd1980a52009-03-12 06:52:53 +000013307 // If this is a select between two integer constants, try to do some
13308 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013309 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13310 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013311 // Don't do this for crazy integer types.
13312 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13313 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013314 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013315 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013316
Chris Lattnercee56e72009-03-13 05:53:31 +000013317 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013318 // Efficiently invertible.
13319 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13320 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13321 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13322 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013323 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013324 }
Eric Christopherfd179292009-08-27 18:07:15 +000013325
Chris Lattnerd1980a52009-03-12 06:52:53 +000013326 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013327 if (FalseC->getAPIntValue() == 0 &&
13328 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013329 if (NeedsCondInvert) // Invert the condition if needed.
13330 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13331 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013332
Chris Lattnerd1980a52009-03-12 06:52:53 +000013333 // Zero extend the condition if needed.
13334 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013335
Chris Lattnercee56e72009-03-13 05:53:31 +000013336 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013337 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013338 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013339 }
Eric Christopherfd179292009-08-27 18:07:15 +000013340
Chris Lattner97a29a52009-03-13 05:22:11 +000013341 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013342 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013343 if (NeedsCondInvert) // Invert the condition if needed.
13344 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13345 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013346
Chris Lattner97a29a52009-03-13 05:22:11 +000013347 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013348 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13349 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013350 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013351 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013352 }
Eric Christopherfd179292009-08-27 18:07:15 +000013353
Chris Lattnercee56e72009-03-13 05:53:31 +000013354 // Optimize cases that will turn into an LEA instruction. This requires
13355 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013356 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013357 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013358 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013359
Chris Lattnercee56e72009-03-13 05:53:31 +000013360 bool isFastMultiplier = false;
13361 if (Diff < 10) {
13362 switch ((unsigned char)Diff) {
13363 default: break;
13364 case 1: // result = add base, cond
13365 case 2: // result = lea base( , cond*2)
13366 case 3: // result = lea base(cond, cond*2)
13367 case 4: // result = lea base( , cond*4)
13368 case 5: // result = lea base(cond, cond*4)
13369 case 8: // result = lea base( , cond*8)
13370 case 9: // result = lea base(cond, cond*8)
13371 isFastMultiplier = true;
13372 break;
13373 }
13374 }
Eric Christopherfd179292009-08-27 18:07:15 +000013375
Chris Lattnercee56e72009-03-13 05:53:31 +000013376 if (isFastMultiplier) {
13377 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13378 if (NeedsCondInvert) // Invert the condition if needed.
13379 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13380 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013381
Chris Lattnercee56e72009-03-13 05:53:31 +000013382 // Zero extend the condition if needed.
13383 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13384 Cond);
13385 // Scale the condition by the difference.
13386 if (Diff != 1)
13387 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13388 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013389
Chris Lattnercee56e72009-03-13 05:53:31 +000013390 // Add the base if non-zero.
13391 if (FalseC->getAPIntValue() != 0)
13392 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13393 SDValue(FalseC, 0));
13394 return Cond;
13395 }
Eric Christopherfd179292009-08-27 18:07:15 +000013396 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013397 }
13398 }
Eric Christopherfd179292009-08-27 18:07:15 +000013399
Dan Gohman475871a2008-07-27 21:46:04 +000013400 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013401}
13402
Chris Lattnerd1980a52009-03-12 06:52:53 +000013403/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13404static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13405 TargetLowering::DAGCombinerInfo &DCI) {
13406 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013407
Chris Lattnerd1980a52009-03-12 06:52:53 +000013408 // If the flag operand isn't dead, don't touch this CMOV.
13409 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13410 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013411
Evan Chengb5a55d92011-05-24 01:48:22 +000013412 SDValue FalseOp = N->getOperand(0);
13413 SDValue TrueOp = N->getOperand(1);
13414 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13415 SDValue Cond = N->getOperand(3);
13416 if (CC == X86::COND_E || CC == X86::COND_NE) {
13417 switch (Cond.getOpcode()) {
13418 default: break;
13419 case X86ISD::BSR:
13420 case X86ISD::BSF:
13421 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13422 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13423 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13424 }
13425 }
13426
Chris Lattnerd1980a52009-03-12 06:52:53 +000013427 // If this is a select between two integer constants, try to do some
13428 // optimizations. Note that the operands are ordered the opposite of SELECT
13429 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013430 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13431 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013432 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13433 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013434 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13435 CC = X86::GetOppositeBranchCondition(CC);
13436 std::swap(TrueC, FalseC);
13437 }
Eric Christopherfd179292009-08-27 18:07:15 +000013438
Chris Lattnerd1980a52009-03-12 06:52:53 +000013439 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013440 // This is efficient for any integer data type (including i8/i16) and
13441 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013442 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013443 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13444 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013445
Chris Lattnerd1980a52009-03-12 06:52:53 +000013446 // Zero extend the condition if needed.
13447 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013448
Chris Lattnerd1980a52009-03-12 06:52:53 +000013449 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13450 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013451 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013452 if (N->getNumValues() == 2) // Dead flag value?
13453 return DCI.CombineTo(N, Cond, SDValue());
13454 return Cond;
13455 }
Eric Christopherfd179292009-08-27 18:07:15 +000013456
Chris Lattnercee56e72009-03-13 05:53:31 +000013457 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13458 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013459 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013460 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13461 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013462
Chris Lattner97a29a52009-03-13 05:22:11 +000013463 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013464 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13465 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013466 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13467 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013468
Chris Lattner97a29a52009-03-13 05:22:11 +000013469 if (N->getNumValues() == 2) // Dead flag value?
13470 return DCI.CombineTo(N, Cond, SDValue());
13471 return Cond;
13472 }
Eric Christopherfd179292009-08-27 18:07:15 +000013473
Chris Lattnercee56e72009-03-13 05:53:31 +000013474 // Optimize cases that will turn into an LEA instruction. This requires
13475 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013476 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013477 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013478 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013479
Chris Lattnercee56e72009-03-13 05:53:31 +000013480 bool isFastMultiplier = false;
13481 if (Diff < 10) {
13482 switch ((unsigned char)Diff) {
13483 default: break;
13484 case 1: // result = add base, cond
13485 case 2: // result = lea base( , cond*2)
13486 case 3: // result = lea base(cond, cond*2)
13487 case 4: // result = lea base( , cond*4)
13488 case 5: // result = lea base(cond, cond*4)
13489 case 8: // result = lea base( , cond*8)
13490 case 9: // result = lea base(cond, cond*8)
13491 isFastMultiplier = true;
13492 break;
13493 }
13494 }
Eric Christopherfd179292009-08-27 18:07:15 +000013495
Chris Lattnercee56e72009-03-13 05:53:31 +000013496 if (isFastMultiplier) {
13497 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013498 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13499 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013500 // Zero extend the condition if needed.
13501 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13502 Cond);
13503 // Scale the condition by the difference.
13504 if (Diff != 1)
13505 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13506 DAG.getConstant(Diff, Cond.getValueType()));
13507
13508 // Add the base if non-zero.
13509 if (FalseC->getAPIntValue() != 0)
13510 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13511 SDValue(FalseC, 0));
13512 if (N->getNumValues() == 2) // Dead flag value?
13513 return DCI.CombineTo(N, Cond, SDValue());
13514 return Cond;
13515 }
Eric Christopherfd179292009-08-27 18:07:15 +000013516 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013517 }
13518 }
13519 return SDValue();
13520}
13521
13522
Evan Cheng0b0cd912009-03-28 05:57:29 +000013523/// PerformMulCombine - Optimize a single multiply with constant into two
13524/// in order to implement it with two cheaper instructions, e.g.
13525/// LEA + SHL, LEA + LEA.
13526static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13527 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013528 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13529 return SDValue();
13530
Owen Andersone50ed302009-08-10 22:56:29 +000013531 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013532 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013533 return SDValue();
13534
13535 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13536 if (!C)
13537 return SDValue();
13538 uint64_t MulAmt = C->getZExtValue();
13539 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13540 return SDValue();
13541
13542 uint64_t MulAmt1 = 0;
13543 uint64_t MulAmt2 = 0;
13544 if ((MulAmt % 9) == 0) {
13545 MulAmt1 = 9;
13546 MulAmt2 = MulAmt / 9;
13547 } else if ((MulAmt % 5) == 0) {
13548 MulAmt1 = 5;
13549 MulAmt2 = MulAmt / 5;
13550 } else if ((MulAmt % 3) == 0) {
13551 MulAmt1 = 3;
13552 MulAmt2 = MulAmt / 3;
13553 }
13554 if (MulAmt2 &&
13555 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13556 DebugLoc DL = N->getDebugLoc();
13557
13558 if (isPowerOf2_64(MulAmt2) &&
13559 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13560 // If second multiplifer is pow2, issue it first. We want the multiply by
13561 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13562 // is an add.
13563 std::swap(MulAmt1, MulAmt2);
13564
13565 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013566 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013567 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013568 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013569 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013570 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013571 DAG.getConstant(MulAmt1, VT));
13572
Eric Christopherfd179292009-08-27 18:07:15 +000013573 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013574 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013575 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013576 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013577 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013578 DAG.getConstant(MulAmt2, VT));
13579
13580 // Do not add new nodes to DAG combiner worklist.
13581 DCI.CombineTo(N, NewMul, false);
13582 }
13583 return SDValue();
13584}
13585
Evan Chengad9c0a32009-12-15 00:53:42 +000013586static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13587 SDValue N0 = N->getOperand(0);
13588 SDValue N1 = N->getOperand(1);
13589 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13590 EVT VT = N0.getValueType();
13591
13592 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13593 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013594 if (VT.isInteger() && !VT.isVector() &&
13595 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013596 N0.getOperand(1).getOpcode() == ISD::Constant) {
13597 SDValue N00 = N0.getOperand(0);
13598 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13599 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13600 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13601 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13602 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13603 APInt ShAmt = N1C->getAPIntValue();
13604 Mask = Mask.shl(ShAmt);
13605 if (Mask != 0)
13606 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13607 N00, DAG.getConstant(Mask, VT));
13608 }
13609 }
13610
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013611
13612 // Hardware support for vector shifts is sparse which makes us scalarize the
13613 // vector operations in many cases. Also, on sandybridge ADD is faster than
13614 // shl.
13615 // (shl V, 1) -> add V,V
13616 if (isSplatVector(N1.getNode())) {
13617 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13618 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13619 // We shift all of the values by one. In many cases we do not have
13620 // hardware support for this operation. This is better expressed as an ADD
13621 // of two values.
13622 if (N1C && (1 == N1C->getZExtValue())) {
13623 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13624 }
13625 }
13626
Evan Chengad9c0a32009-12-15 00:53:42 +000013627 return SDValue();
13628}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013629
Nate Begeman740ab032009-01-26 00:52:55 +000013630/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13631/// when possible.
13632static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13633 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013634 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013635 if (N->getOpcode() == ISD::SHL) {
13636 SDValue V = PerformSHLCombine(N, DAG);
13637 if (V.getNode()) return V;
13638 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013639
Nate Begeman740ab032009-01-26 00:52:55 +000013640 // On X86 with SSE2 support, we can transform this to a vector shift if
13641 // all elements are shifted by the same amount. We can't do this in legalize
13642 // because the a constant vector is typically transformed to a constant pool
13643 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013644 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013645 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013646
Craig Topper7be5dfd2011-11-12 09:58:49 +000013647 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
13648 (!Subtarget->hasAVX2() ||
13649 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013650 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013651
Mon P Wang3becd092009-01-28 08:12:05 +000013652 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013653 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013654 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013655 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013656 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13657 unsigned NumElts = VT.getVectorNumElements();
13658 unsigned i = 0;
13659 for (; i != NumElts; ++i) {
13660 SDValue Arg = ShAmtOp.getOperand(i);
13661 if (Arg.getOpcode() == ISD::UNDEF) continue;
13662 BaseShAmt = Arg;
13663 break;
13664 }
13665 for (; i != NumElts; ++i) {
13666 SDValue Arg = ShAmtOp.getOperand(i);
13667 if (Arg.getOpcode() == ISD::UNDEF) continue;
13668 if (Arg != BaseShAmt) {
13669 return SDValue();
13670 }
13671 }
13672 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013673 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013674 SDValue InVec = ShAmtOp.getOperand(0);
13675 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13676 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13677 unsigned i = 0;
13678 for (; i != NumElts; ++i) {
13679 SDValue Arg = InVec.getOperand(i);
13680 if (Arg.getOpcode() == ISD::UNDEF) continue;
13681 BaseShAmt = Arg;
13682 break;
13683 }
13684 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13685 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013686 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013687 if (C->getZExtValue() == SplatIdx)
13688 BaseShAmt = InVec.getOperand(1);
13689 }
13690 }
13691 if (BaseShAmt.getNode() == 0)
13692 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13693 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013694 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013695 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013696
Mon P Wangefa42202009-09-03 19:56:25 +000013697 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013698 if (EltVT.bitsGT(MVT::i32))
13699 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13700 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013701 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013702
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013703 // The shift amount is identical so we can do a vector shift.
13704 SDValue ValOp = N->getOperand(0);
13705 switch (N->getOpcode()) {
13706 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013707 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013708 break;
13709 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013710 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013711 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013712 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013713 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013714 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013715 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013716 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013717 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013718 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013719 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013720 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013721 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013722 if (VT == MVT::v4i64)
13723 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13724 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
13725 ValOp, BaseShAmt);
13726 if (VT == MVT::v8i32)
13727 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13728 DAG.getConstant(Intrinsic::x86_avx2_pslli_d, MVT::i32),
13729 ValOp, BaseShAmt);
13730 if (VT == MVT::v16i16)
13731 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13732 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
13733 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013734 break;
13735 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013736 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013737 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013738 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013739 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013740 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013741 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013742 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013743 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013744 if (VT == MVT::v8i32)
13745 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13746 DAG.getConstant(Intrinsic::x86_avx2_psrai_d, MVT::i32),
13747 ValOp, BaseShAmt);
13748 if (VT == MVT::v16i16)
13749 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13750 DAG.getConstant(Intrinsic::x86_avx2_psrai_w, MVT::i32),
13751 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013752 break;
13753 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013754 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013755 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013756 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013757 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013758 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013759 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013760 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013761 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013762 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013763 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013764 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013765 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013766 if (VT == MVT::v4i64)
13767 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13768 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
13769 ValOp, BaseShAmt);
13770 if (VT == MVT::v8i32)
13771 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13772 DAG.getConstant(Intrinsic::x86_avx2_psrli_d, MVT::i32),
13773 ValOp, BaseShAmt);
13774 if (VT == MVT::v16i16)
13775 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13776 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
13777 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013778 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013779 }
13780 return SDValue();
13781}
13782
Nate Begemanb65c1752010-12-17 22:55:37 +000013783
Stuart Hastings865f0932011-06-03 23:53:54 +000013784// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13785// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13786// and friends. Likewise for OR -> CMPNEQSS.
13787static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13788 TargetLowering::DAGCombinerInfo &DCI,
13789 const X86Subtarget *Subtarget) {
13790 unsigned opcode;
13791
13792 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13793 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013794 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013795 SDValue N0 = N->getOperand(0);
13796 SDValue N1 = N->getOperand(1);
13797 SDValue CMP0 = N0->getOperand(1);
13798 SDValue CMP1 = N1->getOperand(1);
13799 DebugLoc DL = N->getDebugLoc();
13800
13801 // The SETCCs should both refer to the same CMP.
13802 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13803 return SDValue();
13804
13805 SDValue CMP00 = CMP0->getOperand(0);
13806 SDValue CMP01 = CMP0->getOperand(1);
13807 EVT VT = CMP00.getValueType();
13808
13809 if (VT == MVT::f32 || VT == MVT::f64) {
13810 bool ExpectingFlags = false;
13811 // Check for any users that want flags:
13812 for (SDNode::use_iterator UI = N->use_begin(),
13813 UE = N->use_end();
13814 !ExpectingFlags && UI != UE; ++UI)
13815 switch (UI->getOpcode()) {
13816 default:
13817 case ISD::BR_CC:
13818 case ISD::BRCOND:
13819 case ISD::SELECT:
13820 ExpectingFlags = true;
13821 break;
13822 case ISD::CopyToReg:
13823 case ISD::SIGN_EXTEND:
13824 case ISD::ZERO_EXTEND:
13825 case ISD::ANY_EXTEND:
13826 break;
13827 }
13828
13829 if (!ExpectingFlags) {
13830 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13831 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13832
13833 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13834 X86::CondCode tmp = cc0;
13835 cc0 = cc1;
13836 cc1 = tmp;
13837 }
13838
13839 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13840 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13841 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13842 X86ISD::NodeType NTOperator = is64BitFP ?
13843 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13844 // FIXME: need symbolic constants for these magic numbers.
13845 // See X86ATTInstPrinter.cpp:printSSECC().
13846 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13847 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13848 DAG.getConstant(x86cc, MVT::i8));
13849 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13850 OnesOrZeroesF);
13851 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13852 DAG.getConstant(1, MVT::i32));
13853 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13854 return OneBitOfTruth;
13855 }
13856 }
13857 }
13858 }
13859 return SDValue();
13860}
13861
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013862/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13863/// so it can be folded inside ANDNP.
13864static bool CanFoldXORWithAllOnes(const SDNode *N) {
13865 EVT VT = N->getValueType(0);
13866
13867 // Match direct AllOnes for 128 and 256-bit vectors
13868 if (ISD::isBuildVectorAllOnes(N))
13869 return true;
13870
13871 // Look through a bit convert.
13872 if (N->getOpcode() == ISD::BITCAST)
13873 N = N->getOperand(0).getNode();
13874
13875 // Sometimes the operand may come from a insert_subvector building a 256-bit
13876 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013877 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013878 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13879 SDValue V1 = N->getOperand(0);
13880 SDValue V2 = N->getOperand(1);
13881
13882 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13883 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13884 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13885 ISD::isBuildVectorAllOnes(V2.getNode()))
13886 return true;
13887 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013888
13889 return false;
13890}
13891
Nate Begemanb65c1752010-12-17 22:55:37 +000013892static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13893 TargetLowering::DAGCombinerInfo &DCI,
13894 const X86Subtarget *Subtarget) {
13895 if (DCI.isBeforeLegalizeOps())
13896 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013897
Stuart Hastings865f0932011-06-03 23:53:54 +000013898 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13899 if (R.getNode())
13900 return R;
13901
Craig Topper54a11172011-10-14 07:06:56 +000013902 EVT VT = N->getValueType(0);
13903
Craig Topperb4c94572011-10-21 06:55:01 +000013904 // Create ANDN, BLSI, and BLSR instructions
13905 // BLSI is X & (-X)
13906 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013907 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13908 SDValue N0 = N->getOperand(0);
13909 SDValue N1 = N->getOperand(1);
13910 DebugLoc DL = N->getDebugLoc();
13911
13912 // Check LHS for not
13913 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13914 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13915 // Check RHS for not
13916 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13917 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13918
Craig Topperb4c94572011-10-21 06:55:01 +000013919 // Check LHS for neg
13920 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13921 isZero(N0.getOperand(0)))
13922 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13923
13924 // Check RHS for neg
13925 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13926 isZero(N1.getOperand(0)))
13927 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13928
13929 // Check LHS for X-1
13930 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13931 isAllOnes(N0.getOperand(1)))
13932 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13933
13934 // Check RHS for X-1
13935 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13936 isAllOnes(N1.getOperand(1)))
13937 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13938
Craig Topper54a11172011-10-14 07:06:56 +000013939 return SDValue();
13940 }
13941
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013942 // Want to form ANDNP nodes:
13943 // 1) In the hopes of then easily combining them with OR and AND nodes
13944 // to form PBLEND/PSIGN.
13945 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013946 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013947 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013948
Nate Begemanb65c1752010-12-17 22:55:37 +000013949 SDValue N0 = N->getOperand(0);
13950 SDValue N1 = N->getOperand(1);
13951 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013952
Nate Begemanb65c1752010-12-17 22:55:37 +000013953 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013954 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013955 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13956 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013957 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013958
13959 // Check RHS for vnot
13960 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013961 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13962 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013963 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013964
Nate Begemanb65c1752010-12-17 22:55:37 +000013965 return SDValue();
13966}
13967
Evan Cheng760d1942010-01-04 21:22:48 +000013968static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013969 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013970 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013971 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013972 return SDValue();
13973
Stuart Hastings865f0932011-06-03 23:53:54 +000013974 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13975 if (R.getNode())
13976 return R;
13977
Evan Cheng760d1942010-01-04 21:22:48 +000013978 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000013979
Evan Cheng760d1942010-01-04 21:22:48 +000013980 SDValue N0 = N->getOperand(0);
13981 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013982
Nate Begemanb65c1752010-12-17 22:55:37 +000013983 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000013984 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
13985 if (!(Subtarget->hasSSSE3() || Subtarget->hasAVX()) ||
13986 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
13987 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013988
Craig Topper1666cb62011-11-19 07:07:26 +000013989 // Canonicalize pandn to RHS
13990 if (N0.getOpcode() == X86ISD::ANDNP)
13991 std::swap(N0, N1);
13992 // or (and (m, x), (pandn m, y))
13993 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
13994 SDValue Mask = N1.getOperand(0);
13995 SDValue X = N1.getOperand(1);
13996 SDValue Y;
13997 if (N0.getOperand(0) == Mask)
13998 Y = N0.getOperand(1);
13999 if (N0.getOperand(1) == Mask)
14000 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014001
Craig Topper1666cb62011-11-19 07:07:26 +000014002 // Check to see if the mask appeared in both the AND and ANDNP and
14003 if (!Y.getNode())
14004 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014005
Craig Topper1666cb62011-11-19 07:07:26 +000014006 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
14007 if (Mask.getOpcode() != ISD::BITCAST ||
14008 X.getOpcode() != ISD::BITCAST ||
14009 Y.getOpcode() != ISD::BITCAST)
14010 return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000014011
Craig Topper1666cb62011-11-19 07:07:26 +000014012 // Look through mask bitcast.
14013 Mask = Mask.getOperand(0);
14014 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014015
Craig Topper1666cb62011-11-19 07:07:26 +000014016 // Validate that the Mask operand is a vector sra node. The sra node
14017 // will be an intrinsic.
14018 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
14019 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014020
Craig Topper1666cb62011-11-19 07:07:26 +000014021 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
14022 // there is no psrai.b
14023 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
14024 case Intrinsic::x86_sse2_psrai_w:
14025 case Intrinsic::x86_sse2_psrai_d:
14026 case Intrinsic::x86_avx2_psrai_w:
14027 case Intrinsic::x86_avx2_psrai_d:
14028 break;
14029 default: return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000014030 }
Craig Topper1666cb62011-11-19 07:07:26 +000014031
14032 // Check that the SRA is all signbits.
14033 SDValue SraC = Mask.getOperand(2);
14034 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
14035 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
14036 if ((SraAmt + 1) != EltBits)
14037 return SDValue();
14038
14039 DebugLoc DL = N->getDebugLoc();
14040
14041 // Now we know we at least have a plendvb with the mask val. See if
14042 // we can form a psignb/w/d.
14043 // psign = x.type == y.type == mask.type && y = sub(0, x);
14044 X = X.getOperand(0);
14045 Y = Y.getOperand(0);
14046 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
14047 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Topper31133842011-11-19 07:33:10 +000014048 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType() &&
14049 (EltBits == 8 || EltBits == 16 || EltBits == 32)) {
14050 SDValue Sign = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X,
14051 Mask.getOperand(1));
14052 return DAG.getNode(ISD::BITCAST, DL, VT, Sign);
Craig Topper1666cb62011-11-19 07:07:26 +000014053 }
14054 // PBLENDVB only available on SSE 4.1
14055 if (!(Subtarget->hasSSE41() || Subtarget->hasAVX()))
14056 return SDValue();
14057
14058 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
14059
14060 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
14061 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
14062 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
14063 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, X, Y);
14064 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000014065 }
14066 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014067
Craig Topper1666cb62011-11-19 07:07:26 +000014068 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
14069 return SDValue();
14070
Nate Begemanb65c1752010-12-17 22:55:37 +000014071 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000014072 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
14073 std::swap(N0, N1);
14074 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
14075 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000014076 if (!N0.hasOneUse() || !N1.hasOneUse())
14077 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000014078
14079 SDValue ShAmt0 = N0.getOperand(1);
14080 if (ShAmt0.getValueType() != MVT::i8)
14081 return SDValue();
14082 SDValue ShAmt1 = N1.getOperand(1);
14083 if (ShAmt1.getValueType() != MVT::i8)
14084 return SDValue();
14085 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
14086 ShAmt0 = ShAmt0.getOperand(0);
14087 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
14088 ShAmt1 = ShAmt1.getOperand(0);
14089
14090 DebugLoc DL = N->getDebugLoc();
14091 unsigned Opc = X86ISD::SHLD;
14092 SDValue Op0 = N0.getOperand(0);
14093 SDValue Op1 = N1.getOperand(0);
14094 if (ShAmt0.getOpcode() == ISD::SUB) {
14095 Opc = X86ISD::SHRD;
14096 std::swap(Op0, Op1);
14097 std::swap(ShAmt0, ShAmt1);
14098 }
14099
Evan Cheng8b1190a2010-04-28 01:18:01 +000014100 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000014101 if (ShAmt1.getOpcode() == ISD::SUB) {
14102 SDValue Sum = ShAmt1.getOperand(0);
14103 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000014104 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
14105 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
14106 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
14107 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000014108 return DAG.getNode(Opc, DL, VT,
14109 Op0, Op1,
14110 DAG.getNode(ISD::TRUNCATE, DL,
14111 MVT::i8, ShAmt0));
14112 }
14113 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
14114 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
14115 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000014116 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000014117 return DAG.getNode(Opc, DL, VT,
14118 N0.getOperand(0), N1.getOperand(0),
14119 DAG.getNode(ISD::TRUNCATE, DL,
14120 MVT::i8, ShAmt0));
14121 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014122
Evan Cheng760d1942010-01-04 21:22:48 +000014123 return SDValue();
14124}
14125
Craig Topperb4c94572011-10-21 06:55:01 +000014126static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
14127 TargetLowering::DAGCombinerInfo &DCI,
14128 const X86Subtarget *Subtarget) {
14129 if (DCI.isBeforeLegalizeOps())
14130 return SDValue();
14131
14132 EVT VT = N->getValueType(0);
14133
14134 if (VT != MVT::i32 && VT != MVT::i64)
14135 return SDValue();
14136
14137 // Create BLSMSK instructions by finding X ^ (X-1)
14138 SDValue N0 = N->getOperand(0);
14139 SDValue N1 = N->getOperand(1);
14140 DebugLoc DL = N->getDebugLoc();
14141
14142 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14143 isAllOnes(N0.getOperand(1)))
14144 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
14145
14146 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14147 isAllOnes(N1.getOperand(1)))
14148 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
14149
14150 return SDValue();
14151}
14152
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014153/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
14154static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
14155 const X86Subtarget *Subtarget) {
14156 LoadSDNode *Ld = cast<LoadSDNode>(N);
14157 EVT RegVT = Ld->getValueType(0);
14158 EVT MemVT = Ld->getMemoryVT();
14159 DebugLoc dl = Ld->getDebugLoc();
14160 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14161
14162 ISD::LoadExtType Ext = Ld->getExtensionType();
14163
Nadav Rotemca6f2962011-09-18 19:00:23 +000014164 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014165 // shuffle. We need SSE4 for the shuffles.
14166 // TODO: It is possible to support ZExt by zeroing the undef values
14167 // during the shuffle phase or after the shuffle.
14168 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
14169 assert(MemVT != RegVT && "Cannot extend to the same type");
14170 assert(MemVT.isVector() && "Must load a vector from memory");
14171
14172 unsigned NumElems = RegVT.getVectorNumElements();
14173 unsigned RegSz = RegVT.getSizeInBits();
14174 unsigned MemSz = MemVT.getSizeInBits();
14175 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000014176 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014177 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
14178
14179 // Attempt to load the original value using a single load op.
14180 // Find a scalar type which is equal to the loaded word size.
14181 MVT SclrLoadTy = MVT::i8;
14182 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14183 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14184 MVT Tp = (MVT::SimpleValueType)tp;
14185 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
14186 SclrLoadTy = Tp;
14187 break;
14188 }
14189 }
14190
14191 // Proceed if a load word is found.
14192 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
14193
14194 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
14195 RegSz/SclrLoadTy.getSizeInBits());
14196
14197 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
14198 RegSz/MemVT.getScalarType().getSizeInBits());
14199 // Can't shuffle using an illegal type.
14200 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14201
14202 // Perform a single load.
14203 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
14204 Ld->getBasePtr(),
14205 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014206 Ld->isNonTemporal(), Ld->isInvariant(),
14207 Ld->getAlignment());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014208
14209 // Insert the word loaded into a vector.
14210 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
14211 LoadUnitVecVT, ScalarLoad);
14212
14213 // Bitcast the loaded value to a vector of the original element type, in
14214 // the size of the target vector type.
14215 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
14216 unsigned SizeRatio = RegSz/MemSz;
14217
14218 // Redistribute the loaded elements into the different locations.
14219 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14220 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
14221
14222 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
14223 DAG.getUNDEF(SlicedVec.getValueType()),
14224 ShuffleVec.data());
14225
14226 // Bitcast to the requested type.
14227 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
14228 // Replace the original load with the new sequence
14229 // and return the new chain.
14230 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
14231 return SDValue(ScalarLoad.getNode(), 1);
14232 }
14233
14234 return SDValue();
14235}
14236
Chris Lattner149a4e52008-02-22 02:09:43 +000014237/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014238static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000014239 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000014240 StoreSDNode *St = cast<StoreSDNode>(N);
14241 EVT VT = St->getValue().getValueType();
14242 EVT StVT = St->getMemoryVT();
14243 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000014244 SDValue StoredVal = St->getOperand(1);
14245 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14246
14247 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000014248 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
14249 // 128-bit ones. If in the future the cost becomes only one memory access the
14250 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000014251 if (VT.getSizeInBits() == 256 &&
14252 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
14253 StoredVal.getNumOperands() == 2) {
14254
14255 SDValue Value0 = StoredVal.getOperand(0);
14256 SDValue Value1 = StoredVal.getOperand(1);
14257
14258 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
14259 SDValue Ptr0 = St->getBasePtr();
14260 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
14261
14262 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
14263 St->getPointerInfo(), St->isVolatile(),
14264 St->isNonTemporal(), St->getAlignment());
14265 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
14266 St->getPointerInfo(), St->isVolatile(),
14267 St->isNonTemporal(), St->getAlignment());
14268 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
14269 }
Nadav Rotem614061b2011-08-10 19:30:14 +000014270
14271 // Optimize trunc store (of multiple scalars) to shuffle and store.
14272 // First, pack all of the elements in one place. Next, store to memory
14273 // in fewer chunks.
14274 if (St->isTruncatingStore() && VT.isVector()) {
14275 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14276 unsigned NumElems = VT.getVectorNumElements();
14277 assert(StVT != VT && "Cannot truncate to the same type");
14278 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
14279 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
14280
14281 // From, To sizes and ElemCount must be pow of two
14282 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014283 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000014284 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014285 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014286
Nadav Rotem614061b2011-08-10 19:30:14 +000014287 unsigned SizeRatio = FromSz / ToSz;
14288
14289 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
14290
14291 // Create a type on which we perform the shuffle
14292 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
14293 StVT.getScalarType(), NumElems*SizeRatio);
14294
14295 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
14296
14297 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
14298 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14299 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
14300
14301 // Can't shuffle using an illegal type
14302 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14303
14304 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
14305 DAG.getUNDEF(WideVec.getValueType()),
14306 ShuffleVec.data());
14307 // At this point all of the data is stored at the bottom of the
14308 // register. We now need to save it to mem.
14309
14310 // Find the largest store unit
14311 MVT StoreType = MVT::i8;
14312 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14313 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14314 MVT Tp = (MVT::SimpleValueType)tp;
14315 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
14316 StoreType = Tp;
14317 }
14318
14319 // Bitcast the original vector into a vector of store-size units
14320 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
14321 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
14322 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
14323 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
14324 SmallVector<SDValue, 8> Chains;
14325 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
14326 TLI.getPointerTy());
14327 SDValue Ptr = St->getBasePtr();
14328
14329 // Perform one or more big stores into memory.
14330 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
14331 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14332 StoreType, ShuffWide,
14333 DAG.getIntPtrConstant(i));
14334 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14335 St->getPointerInfo(), St->isVolatile(),
14336 St->isNonTemporal(), St->getAlignment());
14337 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14338 Chains.push_back(Ch);
14339 }
14340
14341 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14342 Chains.size());
14343 }
14344
14345
Chris Lattner149a4e52008-02-22 02:09:43 +000014346 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14347 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014348 // A preferable solution to the general problem is to figure out the right
14349 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014350
14351 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014352 if (VT.getSizeInBits() != 64)
14353 return SDValue();
14354
Devang Patel578efa92009-06-05 21:57:13 +000014355 const Function *F = DAG.getMachineFunction().getFunction();
14356 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000014357 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000014358 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000014359 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014360 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014361 isa<LoadSDNode>(St->getValue()) &&
14362 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14363 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014364 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014365 LoadSDNode *Ld = 0;
14366 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014367 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014368 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014369 // Must be a store of a load. We currently handle two cases: the load
14370 // is a direct child, and it's under an intervening TokenFactor. It is
14371 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014372 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014373 Ld = cast<LoadSDNode>(St->getChain());
14374 else if (St->getValue().hasOneUse() &&
14375 ChainVal->getOpcode() == ISD::TokenFactor) {
14376 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014377 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014378 TokenFactorIndex = i;
14379 Ld = cast<LoadSDNode>(St->getValue());
14380 } else
14381 Ops.push_back(ChainVal->getOperand(i));
14382 }
14383 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014384
Evan Cheng536e6672009-03-12 05:59:15 +000014385 if (!Ld || !ISD::isNormalLoad(Ld))
14386 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014387
Evan Cheng536e6672009-03-12 05:59:15 +000014388 // If this is not the MMX case, i.e. we are just turning i64 load/store
14389 // into f64 load/store, avoid the transformation if there are multiple
14390 // uses of the loaded value.
14391 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14392 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014393
Evan Cheng536e6672009-03-12 05:59:15 +000014394 DebugLoc LdDL = Ld->getDebugLoc();
14395 DebugLoc StDL = N->getDebugLoc();
14396 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14397 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14398 // pair instead.
14399 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014400 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014401 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14402 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014403 Ld->isNonTemporal(), Ld->isInvariant(),
14404 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014405 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014406 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014407 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014408 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014409 Ops.size());
14410 }
Evan Cheng536e6672009-03-12 05:59:15 +000014411 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014412 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014413 St->isVolatile(), St->isNonTemporal(),
14414 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014415 }
Evan Cheng536e6672009-03-12 05:59:15 +000014416
14417 // Otherwise, lower to two pairs of 32-bit loads / stores.
14418 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014419 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14420 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014421
Owen Anderson825b72b2009-08-11 20:47:22 +000014422 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014423 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014424 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014425 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014426 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014427 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014428 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014429 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014430 MinAlign(Ld->getAlignment(), 4));
14431
14432 SDValue NewChain = LoLd.getValue(1);
14433 if (TokenFactorIndex != -1) {
14434 Ops.push_back(LoLd);
14435 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014436 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014437 Ops.size());
14438 }
14439
14440 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014441 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14442 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014443
14444 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014445 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014446 St->isVolatile(), St->isNonTemporal(),
14447 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014448 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014449 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014450 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014451 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014452 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014453 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014454 }
Dan Gohman475871a2008-07-27 21:46:04 +000014455 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014456}
14457
Duncan Sands17470be2011-09-22 20:15:48 +000014458/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14459/// and return the operands for the horizontal operation in LHS and RHS. A
14460/// horizontal operation performs the binary operation on successive elements
14461/// of its first operand, then on successive elements of its second operand,
14462/// returning the resulting values in a vector. For example, if
14463/// A = < float a0, float a1, float a2, float a3 >
14464/// and
14465/// B = < float b0, float b1, float b2, float b3 >
14466/// then the result of doing a horizontal operation on A and B is
14467/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14468/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14469/// A horizontal-op B, for some already available A and B, and if so then LHS is
14470/// set to A, RHS to B, and the routine returns 'true'.
14471/// Note that the binary operation should have the property that if one of the
14472/// operands is UNDEF then the result is UNDEF.
14473static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool isCommutative) {
14474 // Look for the following pattern: if
14475 // A = < float a0, float a1, float a2, float a3 >
14476 // B = < float b0, float b1, float b2, float b3 >
14477 // and
14478 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14479 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14480 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14481 // which is A horizontal-op B.
14482
14483 // At least one of the operands should be a vector shuffle.
14484 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14485 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14486 return false;
14487
14488 EVT VT = LHS.getValueType();
14489 unsigned N = VT.getVectorNumElements();
14490
14491 // View LHS in the form
14492 // LHS = VECTOR_SHUFFLE A, B, LMask
14493 // If LHS is not a shuffle then pretend it is the shuffle
14494 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14495 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14496 // type VT.
14497 SDValue A, B;
14498 SmallVector<int, 8> LMask(N);
14499 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14500 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14501 A = LHS.getOperand(0);
14502 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14503 B = LHS.getOperand(1);
14504 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
14505 } else {
14506 if (LHS.getOpcode() != ISD::UNDEF)
14507 A = LHS;
14508 for (unsigned i = 0; i != N; ++i)
14509 LMask[i] = i;
14510 }
14511
14512 // Likewise, view RHS in the form
14513 // RHS = VECTOR_SHUFFLE C, D, RMask
14514 SDValue C, D;
14515 SmallVector<int, 8> RMask(N);
14516 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14517 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14518 C = RHS.getOperand(0);
14519 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14520 D = RHS.getOperand(1);
14521 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
14522 } else {
14523 if (RHS.getOpcode() != ISD::UNDEF)
14524 C = RHS;
14525 for (unsigned i = 0; i != N; ++i)
14526 RMask[i] = i;
14527 }
14528
14529 // Check that the shuffles are both shuffling the same vectors.
14530 if (!(A == C && B == D) && !(A == D && B == C))
14531 return false;
14532
14533 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14534 if (!A.getNode() && !B.getNode())
14535 return false;
14536
14537 // If A and B occur in reverse order in RHS, then "swap" them (which means
14538 // rewriting the mask).
14539 if (A != C)
14540 for (unsigned i = 0; i != N; ++i) {
14541 unsigned Idx = RMask[i];
14542 if (Idx < N)
14543 RMask[i] += N;
14544 else if (Idx < 2*N)
14545 RMask[i] -= N;
14546 }
14547
14548 // At this point LHS and RHS are equivalent to
14549 // LHS = VECTOR_SHUFFLE A, B, LMask
14550 // RHS = VECTOR_SHUFFLE A, B, RMask
14551 // Check that the masks correspond to performing a horizontal operation.
14552 for (unsigned i = 0; i != N; ++i) {
14553 unsigned LIdx = LMask[i], RIdx = RMask[i];
14554
14555 // Ignore any UNDEF components.
14556 if (LIdx >= 2*N || RIdx >= 2*N || (!A.getNode() && (LIdx < N || RIdx < N))
14557 || (!B.getNode() && (LIdx >= N || RIdx >= N)))
14558 continue;
14559
14560 // Check that successive elements are being operated on. If not, this is
14561 // not a horizontal operation.
14562 if (!(LIdx == 2*i && RIdx == 2*i + 1) &&
14563 !(isCommutative && LIdx == 2*i + 1 && RIdx == 2*i))
14564 return false;
14565 }
14566
14567 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14568 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14569 return true;
14570}
14571
14572/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14573static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14574 const X86Subtarget *Subtarget) {
14575 EVT VT = N->getValueType(0);
14576 SDValue LHS = N->getOperand(0);
14577 SDValue RHS = N->getOperand(1);
14578
14579 // Try to synthesize horizontal adds from adds of shuffles.
14580 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
14581 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
14582 isHorizontalBinOp(LHS, RHS, true))
14583 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14584 return SDValue();
14585}
14586
14587/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14588static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14589 const X86Subtarget *Subtarget) {
14590 EVT VT = N->getValueType(0);
14591 SDValue LHS = N->getOperand(0);
14592 SDValue RHS = N->getOperand(1);
14593
14594 // Try to synthesize horizontal subs from subs of shuffles.
14595 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
14596 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
14597 isHorizontalBinOp(LHS, RHS, false))
14598 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14599 return SDValue();
14600}
14601
Chris Lattner6cf73262008-01-25 06:14:17 +000014602/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14603/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014604static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014605 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14606 // F[X]OR(0.0, x) -> x
14607 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014608 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14609 if (C->getValueAPF().isPosZero())
14610 return N->getOperand(1);
14611 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14612 if (C->getValueAPF().isPosZero())
14613 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014614 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014615}
14616
14617/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014618static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014619 // FAND(0.0, x) -> 0.0
14620 // FAND(x, 0.0) -> 0.0
14621 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14622 if (C->getValueAPF().isPosZero())
14623 return N->getOperand(0);
14624 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14625 if (C->getValueAPF().isPosZero())
14626 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014627 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014628}
14629
Dan Gohmane5af2d32009-01-29 01:59:02 +000014630static SDValue PerformBTCombine(SDNode *N,
14631 SelectionDAG &DAG,
14632 TargetLowering::DAGCombinerInfo &DCI) {
14633 // BT ignores high bits in the bit index operand.
14634 SDValue Op1 = N->getOperand(1);
14635 if (Op1.hasOneUse()) {
14636 unsigned BitWidth = Op1.getValueSizeInBits();
14637 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14638 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014639 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14640 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014641 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014642 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14643 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14644 DCI.CommitTargetLoweringOpt(TLO);
14645 }
14646 return SDValue();
14647}
Chris Lattner83e6c992006-10-04 06:57:07 +000014648
Eli Friedman7a5e5552009-06-07 06:52:44 +000014649static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14650 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014651 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014652 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014653 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014654 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014655 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014656 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014657 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014658 }
14659 return SDValue();
14660}
14661
Evan Cheng2e489c42009-12-16 00:53:11 +000014662static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14663 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14664 // (and (i32 x86isd::setcc_carry), 1)
14665 // This eliminates the zext. This transformation is necessary because
14666 // ISD::SETCC is always legalized to i8.
14667 DebugLoc dl = N->getDebugLoc();
14668 SDValue N0 = N->getOperand(0);
14669 EVT VT = N->getValueType(0);
14670 if (N0.getOpcode() == ISD::AND &&
14671 N0.hasOneUse() &&
14672 N0.getOperand(0).hasOneUse()) {
14673 SDValue N00 = N0.getOperand(0);
14674 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14675 return SDValue();
14676 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14677 if (!C || C->getZExtValue() != 1)
14678 return SDValue();
14679 return DAG.getNode(ISD::AND, dl, VT,
14680 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14681 N00.getOperand(0), N00.getOperand(1)),
14682 DAG.getConstant(1, VT));
14683 }
14684
14685 return SDValue();
14686}
14687
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014688// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14689static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14690 unsigned X86CC = N->getConstantOperandVal(0);
14691 SDValue EFLAG = N->getOperand(1);
14692 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014693
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014694 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14695 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14696 // cases.
14697 if (X86CC == X86::COND_B)
14698 return DAG.getNode(ISD::AND, DL, MVT::i8,
14699 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14700 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14701 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014702
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014703 return SDValue();
14704}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014705
Benjamin Kramer1396c402011-06-18 11:09:41 +000014706static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14707 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014708 SDValue Op0 = N->getOperand(0);
14709 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14710 // a 32-bit target where SSE doesn't support i64->FP operations.
14711 if (Op0.getOpcode() == ISD::LOAD) {
14712 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14713 EVT VT = Ld->getValueType(0);
14714 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14715 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14716 !XTLI->getSubtarget()->is64Bit() &&
14717 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014718 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14719 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014720 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14721 return FILDChain;
14722 }
14723 }
14724 return SDValue();
14725}
14726
Chris Lattner23a01992010-12-20 01:37:09 +000014727// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14728static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14729 X86TargetLowering::DAGCombinerInfo &DCI) {
14730 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14731 // the result is either zero or one (depending on the input carry bit).
14732 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14733 if (X86::isZeroNode(N->getOperand(0)) &&
14734 X86::isZeroNode(N->getOperand(1)) &&
14735 // We don't have a good way to replace an EFLAGS use, so only do this when
14736 // dead right now.
14737 SDValue(N, 1).use_empty()) {
14738 DebugLoc DL = N->getDebugLoc();
14739 EVT VT = N->getValueType(0);
14740 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14741 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14742 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14743 DAG.getConstant(X86::COND_B,MVT::i8),
14744 N->getOperand(2)),
14745 DAG.getConstant(1, VT));
14746 return DCI.CombineTo(N, Res1, CarryOut);
14747 }
14748
14749 return SDValue();
14750}
14751
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014752// fold (add Y, (sete X, 0)) -> adc 0, Y
14753// (add Y, (setne X, 0)) -> sbb -1, Y
14754// (sub (sete X, 0), Y) -> sbb 0, Y
14755// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014756static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014757 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014758
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014759 // Look through ZExts.
14760 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14761 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14762 return SDValue();
14763
14764 SDValue SetCC = Ext.getOperand(0);
14765 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14766 return SDValue();
14767
14768 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14769 if (CC != X86::COND_E && CC != X86::COND_NE)
14770 return SDValue();
14771
14772 SDValue Cmp = SetCC.getOperand(1);
14773 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014774 !X86::isZeroNode(Cmp.getOperand(1)) ||
14775 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014776 return SDValue();
14777
14778 SDValue CmpOp0 = Cmp.getOperand(0);
14779 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14780 DAG.getConstant(1, CmpOp0.getValueType()));
14781
14782 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14783 if (CC == X86::COND_NE)
14784 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14785 DL, OtherVal.getValueType(), OtherVal,
14786 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14787 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14788 DL, OtherVal.getValueType(), OtherVal,
14789 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14790}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014791
Craig Topper54f952a2011-11-19 09:02:40 +000014792/// PerformADDCombine - Do target-specific dag combines on integer adds.
14793static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
14794 const X86Subtarget *Subtarget) {
14795 EVT VT = N->getValueType(0);
14796 SDValue Op0 = N->getOperand(0);
14797 SDValue Op1 = N->getOperand(1);
14798
14799 // Try to synthesize horizontal adds from adds of shuffles.
14800 if ((Subtarget->hasSSSE3() || Subtarget->hasAVX()) &&
14801 (VT == MVT::v8i16 || VT == MVT::v4i32) &&
14802 isHorizontalBinOp(Op0, Op1, true))
14803 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
14804
14805 return OptimizeConditionalInDecrement(N, DAG);
14806}
14807
14808static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
14809 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014810 SDValue Op0 = N->getOperand(0);
14811 SDValue Op1 = N->getOperand(1);
14812
14813 // X86 can't encode an immediate LHS of a sub. See if we can push the
14814 // negation into a preceding instruction.
14815 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014816 // If the RHS of the sub is a XOR with one use and a constant, invert the
14817 // immediate. Then add one to the LHS of the sub so we can turn
14818 // X-Y -> X+~Y+1, saving one register.
14819 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14820 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014821 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014822 EVT VT = Op0.getValueType();
14823 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14824 Op1.getOperand(0),
14825 DAG.getConstant(~XorC, VT));
14826 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014827 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014828 }
14829 }
14830
Craig Topper54f952a2011-11-19 09:02:40 +000014831 // Try to synthesize horizontal adds from adds of shuffles.
14832 EVT VT = N->getValueType(0);
14833 if ((Subtarget->hasSSSE3() || Subtarget->hasAVX()) &&
14834 (VT == MVT::v8i16 || VT == MVT::v4i32) &&
14835 isHorizontalBinOp(Op0, Op1, false))
14836 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
14837
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014838 return OptimizeConditionalInDecrement(N, DAG);
14839}
14840
Dan Gohman475871a2008-07-27 21:46:04 +000014841SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014842 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014843 SelectionDAG &DAG = DCI.DAG;
14844 switch (N->getOpcode()) {
14845 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014846 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014847 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014848 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014849 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014850 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Craig Topper54f952a2011-11-19 09:02:40 +000014851 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
14852 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000014853 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014854 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014855 case ISD::SHL:
14856 case ISD::SRA:
14857 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014858 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014859 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014860 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014861 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014862 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014863 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014864 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14865 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014866 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014867 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14868 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014869 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014870 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014871 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014872 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014873 case X86ISD::SHUFPS: // Handle all target specific shuffles
14874 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014875 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014876 case X86ISD::PUNPCKHBW:
14877 case X86ISD::PUNPCKHWD:
14878 case X86ISD::PUNPCKHDQ:
14879 case X86ISD::PUNPCKHQDQ:
Craig Topper6fa583d2011-11-21 08:26:50 +000014880 case X86ISD::VPUNPCKHBWY:
Craig Topper6347e862011-11-21 06:57:39 +000014881 case X86ISD::VPUNPCKHWDY:
14882 case X86ISD::VPUNPCKHDQY:
14883 case X86ISD::VPUNPCKHQDQY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014884 case X86ISD::UNPCKHPS:
14885 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000014886 case X86ISD::VUNPCKHPSY:
14887 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014888 case X86ISD::PUNPCKLBW:
14889 case X86ISD::PUNPCKLWD:
14890 case X86ISD::PUNPCKLDQ:
14891 case X86ISD::PUNPCKLQDQ:
Craig Topper6fa583d2011-11-21 08:26:50 +000014892 case X86ISD::VPUNPCKLBWY:
Craig Topper6347e862011-11-21 06:57:39 +000014893 case X86ISD::VPUNPCKLWDY:
14894 case X86ISD::VPUNPCKLDQY:
14895 case X86ISD::VPUNPCKLQDQY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014896 case X86ISD::UNPCKLPS:
14897 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000014898 case X86ISD::VUNPCKLPSY:
14899 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014900 case X86ISD::MOVHLPS:
14901 case X86ISD::MOVLHPS:
14902 case X86ISD::PSHUFD:
14903 case X86ISD::PSHUFHW:
14904 case X86ISD::PSHUFLW:
14905 case X86ISD::MOVSS:
14906 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000014907 case X86ISD::VPERMILPS:
14908 case X86ISD::VPERMILPSY:
14909 case X86ISD::VPERMILPD:
14910 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000014911 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014912 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014913 }
14914
Dan Gohman475871a2008-07-27 21:46:04 +000014915 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014916}
14917
Evan Chenge5b51ac2010-04-17 06:13:15 +000014918/// isTypeDesirableForOp - Return true if the target has native support for
14919/// the specified value type and it is 'desirable' to use the type for the
14920/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14921/// instruction encodings are longer and some i16 instructions are slow.
14922bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14923 if (!isTypeLegal(VT))
14924 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014925 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014926 return true;
14927
14928 switch (Opc) {
14929 default:
14930 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014931 case ISD::LOAD:
14932 case ISD::SIGN_EXTEND:
14933 case ISD::ZERO_EXTEND:
14934 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014935 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014936 case ISD::SRL:
14937 case ISD::SUB:
14938 case ISD::ADD:
14939 case ISD::MUL:
14940 case ISD::AND:
14941 case ISD::OR:
14942 case ISD::XOR:
14943 return false;
14944 }
14945}
14946
14947/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014948/// beneficial for dag combiner to promote the specified node. If true, it
14949/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014950bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014951 EVT VT = Op.getValueType();
14952 if (VT != MVT::i16)
14953 return false;
14954
Evan Cheng4c26e932010-04-19 19:29:22 +000014955 bool Promote = false;
14956 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014957 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014958 default: break;
14959 case ISD::LOAD: {
14960 LoadSDNode *LD = cast<LoadSDNode>(Op);
14961 // If the non-extending load has a single use and it's not live out, then it
14962 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014963 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14964 Op.hasOneUse()*/) {
14965 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14966 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14967 // The only case where we'd want to promote LOAD (rather then it being
14968 // promoted as an operand is when it's only use is liveout.
14969 if (UI->getOpcode() != ISD::CopyToReg)
14970 return false;
14971 }
14972 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014973 Promote = true;
14974 break;
14975 }
14976 case ISD::SIGN_EXTEND:
14977 case ISD::ZERO_EXTEND:
14978 case ISD::ANY_EXTEND:
14979 Promote = true;
14980 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014981 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014982 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014983 SDValue N0 = Op.getOperand(0);
14984 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014985 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014986 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014987 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014988 break;
14989 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014990 case ISD::ADD:
14991 case ISD::MUL:
14992 case ISD::AND:
14993 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014994 case ISD::XOR:
14995 Commute = true;
14996 // fallthrough
14997 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014998 SDValue N0 = Op.getOperand(0);
14999 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000015000 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000015001 return false;
15002 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000015003 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000015004 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000015005 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000015006 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000015007 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000015008 }
15009 }
15010
15011 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000015012 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000015013}
15014
Evan Cheng60c07e12006-07-05 22:17:51 +000015015//===----------------------------------------------------------------------===//
15016// X86 Inline Assembly Support
15017//===----------------------------------------------------------------------===//
15018
Chris Lattnerb8105652009-07-20 17:51:36 +000015019bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
15020 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000015021
15022 std::string AsmStr = IA->getAsmString();
15023
15024 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000015025 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000015026 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000015027
15028 switch (AsmPieces.size()) {
15029 default: return false;
15030 case 1:
15031 AsmStr = AsmPieces[0];
15032 AsmPieces.clear();
15033 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
15034
Chris Lattner7a2bdde2011-04-15 05:18:47 +000015035 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000015036 // we will turn this bswap into something that will be lowered to logical ops
15037 // instead of emitting the bswap asm. For now, we don't support 486 or lower
15038 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000015039 // bswap $0
15040 if (AsmPieces.size() == 2 &&
15041 (AsmPieces[0] == "bswap" ||
15042 AsmPieces[0] == "bswapq" ||
15043 AsmPieces[0] == "bswapl") &&
15044 (AsmPieces[1] == "$0" ||
15045 AsmPieces[1] == "${0:q}")) {
15046 // No need to check constraints, nothing other than the equivalent of
15047 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015048 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000015049 if (!Ty || Ty->getBitWidth() % 16 != 0)
15050 return false;
15051 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000015052 }
15053 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000015054 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000015055 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000015056 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000015057 AsmPieces[1] == "$$8," &&
15058 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000015059 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
15060 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000015061 const std::string &ConstraintsStr = IA->getConstraintString();
15062 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000015063 std::sort(AsmPieces.begin(), AsmPieces.end());
15064 if (AsmPieces.size() == 4 &&
15065 AsmPieces[0] == "~{cc}" &&
15066 AsmPieces[1] == "~{dirflag}" &&
15067 AsmPieces[2] == "~{flags}" &&
15068 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015069 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000015070 if (!Ty || Ty->getBitWidth() % 16 != 0)
15071 return false;
15072 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000015073 }
Chris Lattnerb8105652009-07-20 17:51:36 +000015074 }
15075 break;
15076 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000015077 if (CI->getType()->isIntegerTy(32) &&
15078 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
15079 SmallVector<StringRef, 4> Words;
15080 SplitString(AsmPieces[0], Words, " \t,");
15081 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
15082 Words[2] == "${0:w}") {
15083 Words.clear();
15084 SplitString(AsmPieces[1], Words, " \t,");
15085 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
15086 Words[2] == "$0") {
15087 Words.clear();
15088 SplitString(AsmPieces[2], Words, " \t,");
15089 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
15090 Words[2] == "${0:w}") {
15091 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000015092 const std::string &ConstraintsStr = IA->getConstraintString();
15093 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000015094 std::sort(AsmPieces.begin(), AsmPieces.end());
15095 if (AsmPieces.size() == 4 &&
15096 AsmPieces[0] == "~{cc}" &&
15097 AsmPieces[1] == "~{dirflag}" &&
15098 AsmPieces[2] == "~{flags}" &&
15099 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015100 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000015101 if (!Ty || Ty->getBitWidth() % 16 != 0)
15102 return false;
15103 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000015104 }
15105 }
15106 }
15107 }
15108 }
Evan Cheng55d42002011-01-08 01:24:27 +000015109
15110 if (CI->getType()->isIntegerTy(64)) {
15111 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
15112 if (Constraints.size() >= 2 &&
15113 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
15114 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
15115 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
15116 SmallVector<StringRef, 4> Words;
15117 SplitString(AsmPieces[0], Words, " \t");
15118 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000015119 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000015120 SplitString(AsmPieces[1], Words, " \t");
15121 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
15122 Words.clear();
15123 SplitString(AsmPieces[2], Words, " \t,");
15124 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
15125 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015126 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000015127 if (!Ty || Ty->getBitWidth() % 16 != 0)
15128 return false;
15129 return IntrinsicLowering::LowerToByteSwap(CI);
15130 }
Chris Lattnerb8105652009-07-20 17:51:36 +000015131 }
15132 }
15133 }
15134 }
15135 break;
15136 }
15137 return false;
15138}
15139
15140
15141
Chris Lattnerf4dff842006-07-11 02:54:03 +000015142/// getConstraintType - Given a constraint letter, return the type of
15143/// constraint it is for this target.
15144X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000015145X86TargetLowering::getConstraintType(const std::string &Constraint) const {
15146 if (Constraint.size() == 1) {
15147 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000015148 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000015149 case 'q':
15150 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000015151 case 'f':
15152 case 't':
15153 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000015154 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000015155 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000015156 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000015157 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000015158 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000015159 case 'a':
15160 case 'b':
15161 case 'c':
15162 case 'd':
15163 case 'S':
15164 case 'D':
15165 case 'A':
15166 return C_Register;
15167 case 'I':
15168 case 'J':
15169 case 'K':
15170 case 'L':
15171 case 'M':
15172 case 'N':
15173 case 'G':
15174 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000015175 case 'e':
15176 case 'Z':
15177 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000015178 default:
15179 break;
15180 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000015181 }
Chris Lattner4234f572007-03-25 02:14:49 +000015182 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000015183}
15184
John Thompson44ab89e2010-10-29 17:29:13 +000015185/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000015186/// This object must already have been set up with the operand type
15187/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000015188TargetLowering::ConstraintWeight
15189 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000015190 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000015191 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015192 Value *CallOperandVal = info.CallOperandVal;
15193 // If we don't have a value, we can't do a match,
15194 // but allow it at the lowest weight.
15195 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000015196 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015197 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000015198 // Look at the constraint type.
15199 switch (*constraint) {
15200 default:
John Thompson44ab89e2010-10-29 17:29:13 +000015201 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
15202 case 'R':
15203 case 'q':
15204 case 'Q':
15205 case 'a':
15206 case 'b':
15207 case 'c':
15208 case 'd':
15209 case 'S':
15210 case 'D':
15211 case 'A':
15212 if (CallOperandVal->getType()->isIntegerTy())
15213 weight = CW_SpecificReg;
15214 break;
15215 case 'f':
15216 case 't':
15217 case 'u':
15218 if (type->isFloatingPointTy())
15219 weight = CW_SpecificReg;
15220 break;
15221 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000015222 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000015223 weight = CW_SpecificReg;
15224 break;
15225 case 'x':
15226 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015227 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000015228 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015229 break;
15230 case 'I':
15231 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
15232 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000015233 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015234 }
15235 break;
John Thompson44ab89e2010-10-29 17:29:13 +000015236 case 'J':
15237 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15238 if (C->getZExtValue() <= 63)
15239 weight = CW_Constant;
15240 }
15241 break;
15242 case 'K':
15243 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15244 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
15245 weight = CW_Constant;
15246 }
15247 break;
15248 case 'L':
15249 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15250 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
15251 weight = CW_Constant;
15252 }
15253 break;
15254 case 'M':
15255 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15256 if (C->getZExtValue() <= 3)
15257 weight = CW_Constant;
15258 }
15259 break;
15260 case 'N':
15261 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15262 if (C->getZExtValue() <= 0xff)
15263 weight = CW_Constant;
15264 }
15265 break;
15266 case 'G':
15267 case 'C':
15268 if (dyn_cast<ConstantFP>(CallOperandVal)) {
15269 weight = CW_Constant;
15270 }
15271 break;
15272 case 'e':
15273 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15274 if ((C->getSExtValue() >= -0x80000000LL) &&
15275 (C->getSExtValue() <= 0x7fffffffLL))
15276 weight = CW_Constant;
15277 }
15278 break;
15279 case 'Z':
15280 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15281 if (C->getZExtValue() <= 0xffffffff)
15282 weight = CW_Constant;
15283 }
15284 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015285 }
15286 return weight;
15287}
15288
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015289/// LowerXConstraint - try to replace an X constraint, which matches anything,
15290/// with another that has more specific requirements based on the type of the
15291/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000015292const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000015293LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000015294 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
15295 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000015296 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015297 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000015298 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015299 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000015300 return "x";
15301 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015302
Chris Lattner5e764232008-04-26 23:02:14 +000015303 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015304}
15305
Chris Lattner48884cd2007-08-25 00:47:38 +000015306/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
15307/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000015308void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000015309 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000015310 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000015311 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000015312 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000015313
Eric Christopher100c8332011-06-02 23:16:42 +000015314 // Only support length 1 constraints for now.
15315 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000015316
Eric Christopher100c8332011-06-02 23:16:42 +000015317 char ConstraintLetter = Constraint[0];
15318 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015319 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000015320 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000015321 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015322 if (C->getZExtValue() <= 31) {
15323 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015324 break;
15325 }
Devang Patel84f7fd22007-03-17 00:13:28 +000015326 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015327 return;
Evan Cheng364091e2008-09-22 23:57:37 +000015328 case 'J':
15329 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015330 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000015331 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15332 break;
15333 }
15334 }
15335 return;
15336 case 'K':
15337 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015338 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000015339 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15340 break;
15341 }
15342 }
15343 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000015344 case 'N':
15345 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015346 if (C->getZExtValue() <= 255) {
15347 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015348 break;
15349 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000015350 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015351 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000015352 case 'e': {
15353 // 32-bit signed value
15354 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015355 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15356 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015357 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015358 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000015359 break;
15360 }
15361 // FIXME gcc accepts some relocatable values here too, but only in certain
15362 // memory models; it's complicated.
15363 }
15364 return;
15365 }
15366 case 'Z': {
15367 // 32-bit unsigned value
15368 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015369 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15370 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015371 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15372 break;
15373 }
15374 }
15375 // FIXME gcc accepts some relocatable values here too, but only in certain
15376 // memory models; it's complicated.
15377 return;
15378 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015379 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015380 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000015381 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015382 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015383 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000015384 break;
15385 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015386
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015387 // In any sort of PIC mode addresses need to be computed at runtime by
15388 // adding in a register or some sort of table lookup. These can't
15389 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000015390 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015391 return;
15392
Chris Lattnerdc43a882007-05-03 16:52:29 +000015393 // If we are in non-pic codegen mode, we allow the address of a global (with
15394 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000015395 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015396 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000015397
Chris Lattner49921962009-05-08 18:23:14 +000015398 // Match either (GA), (GA+C), (GA+C1+C2), etc.
15399 while (1) {
15400 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15401 Offset += GA->getOffset();
15402 break;
15403 } else if (Op.getOpcode() == ISD::ADD) {
15404 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15405 Offset += C->getZExtValue();
15406 Op = Op.getOperand(0);
15407 continue;
15408 }
15409 } else if (Op.getOpcode() == ISD::SUB) {
15410 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15411 Offset += -C->getZExtValue();
15412 Op = Op.getOperand(0);
15413 continue;
15414 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015415 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015416
Chris Lattner49921962009-05-08 18:23:14 +000015417 // Otherwise, this isn't something we can handle, reject it.
15418 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015419 }
Eric Christopherfd179292009-08-27 18:07:15 +000015420
Dan Gohman46510a72010-04-15 01:51:59 +000015421 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015422 // If we require an extra load to get this address, as in PIC mode, we
15423 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015424 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15425 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015426 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015427
Devang Patel0d881da2010-07-06 22:08:15 +000015428 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15429 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015430 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015431 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015432 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015433
Gabor Greifba36cb52008-08-28 21:40:38 +000015434 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015435 Ops.push_back(Result);
15436 return;
15437 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015438 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015439}
15440
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015441std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015442X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015443 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015444 // First, see if this is a constraint that directly corresponds to an LLVM
15445 // register class.
15446 if (Constraint.size() == 1) {
15447 // GCC Constraint Letters
15448 switch (Constraint[0]) {
15449 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015450 // TODO: Slight differences here in allocation order and leaving
15451 // RIP in the class. Do they matter any more here than they do
15452 // in the normal allocation?
15453 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15454 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015455 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015456 return std::make_pair(0U, X86::GR32RegisterClass);
15457 else if (VT == MVT::i16)
15458 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015459 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015460 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015461 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015462 return std::make_pair(0U, X86::GR64RegisterClass);
15463 break;
15464 }
15465 // 32-bit fallthrough
15466 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015467 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015468 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
15469 else if (VT == MVT::i16)
15470 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015471 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015472 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
15473 else if (VT == MVT::i64)
15474 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15475 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015476 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015477 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015478 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015479 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015480 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015481 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015482 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015483 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015484 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015485 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015486 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015487 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15488 if (VT == MVT::i16)
15489 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15490 if (VT == MVT::i32 || !Subtarget->is64Bit())
15491 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15492 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015493 case 'f': // FP Stack registers.
15494 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15495 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015496 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015497 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015498 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015499 return std::make_pair(0U, X86::RFP64RegisterClass);
15500 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015501 case 'y': // MMX_REGS if MMX allowed.
15502 if (!Subtarget->hasMMX()) break;
15503 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015504 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015505 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015506 // FALL THROUGH.
15507 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015508 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015509
Owen Anderson825b72b2009-08-11 20:47:22 +000015510 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015511 default: break;
15512 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015513 case MVT::f32:
15514 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015515 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015516 case MVT::f64:
15517 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015518 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015519 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015520 case MVT::v16i8:
15521 case MVT::v8i16:
15522 case MVT::v4i32:
15523 case MVT::v2i64:
15524 case MVT::v4f32:
15525 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015526 return std::make_pair(0U, X86::VR128RegisterClass);
15527 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015528 break;
15529 }
15530 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015531
Chris Lattnerf76d1802006-07-31 23:26:50 +000015532 // Use the default implementation in TargetLowering to convert the register
15533 // constraint into a member of a register class.
15534 std::pair<unsigned, const TargetRegisterClass*> Res;
15535 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015536
15537 // Not found as a standard register?
15538 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015539 // Map st(0) -> st(7) -> ST0
15540 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15541 tolower(Constraint[1]) == 's' &&
15542 tolower(Constraint[2]) == 't' &&
15543 Constraint[3] == '(' &&
15544 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15545 Constraint[5] == ')' &&
15546 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015547
Chris Lattner56d77c72009-09-13 22:41:48 +000015548 Res.first = X86::ST0+Constraint[4]-'0';
15549 Res.second = X86::RFP80RegisterClass;
15550 return Res;
15551 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015552
Chris Lattner56d77c72009-09-13 22:41:48 +000015553 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015554 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015555 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015556 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015557 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015558 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015559
15560 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015561 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015562 Res.first = X86::EFLAGS;
15563 Res.second = X86::CCRRegisterClass;
15564 return Res;
15565 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015566
Dale Johannesen330169f2008-11-13 21:52:36 +000015567 // 'A' means EAX + EDX.
15568 if (Constraint == "A") {
15569 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015570 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015571 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015572 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015573 return Res;
15574 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015575
Chris Lattnerf76d1802006-07-31 23:26:50 +000015576 // Otherwise, check to see if this is a register class of the wrong value
15577 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15578 // turn into {ax},{dx}.
15579 if (Res.second->hasType(VT))
15580 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015581
Chris Lattnerf76d1802006-07-31 23:26:50 +000015582 // All of the single-register GCC register classes map their values onto
15583 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15584 // really want an 8-bit or 32-bit register, map to the appropriate register
15585 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015586 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015587 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015588 unsigned DestReg = 0;
15589 switch (Res.first) {
15590 default: break;
15591 case X86::AX: DestReg = X86::AL; break;
15592 case X86::DX: DestReg = X86::DL; break;
15593 case X86::CX: DestReg = X86::CL; break;
15594 case X86::BX: DestReg = X86::BL; break;
15595 }
15596 if (DestReg) {
15597 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015598 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015599 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015600 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015601 unsigned DestReg = 0;
15602 switch (Res.first) {
15603 default: break;
15604 case X86::AX: DestReg = X86::EAX; break;
15605 case X86::DX: DestReg = X86::EDX; break;
15606 case X86::CX: DestReg = X86::ECX; break;
15607 case X86::BX: DestReg = X86::EBX; break;
15608 case X86::SI: DestReg = X86::ESI; break;
15609 case X86::DI: DestReg = X86::EDI; break;
15610 case X86::BP: DestReg = X86::EBP; break;
15611 case X86::SP: DestReg = X86::ESP; break;
15612 }
15613 if (DestReg) {
15614 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015615 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015616 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015617 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015618 unsigned DestReg = 0;
15619 switch (Res.first) {
15620 default: break;
15621 case X86::AX: DestReg = X86::RAX; break;
15622 case X86::DX: DestReg = X86::RDX; break;
15623 case X86::CX: DestReg = X86::RCX; break;
15624 case X86::BX: DestReg = X86::RBX; break;
15625 case X86::SI: DestReg = X86::RSI; break;
15626 case X86::DI: DestReg = X86::RDI; break;
15627 case X86::BP: DestReg = X86::RBP; break;
15628 case X86::SP: DestReg = X86::RSP; break;
15629 }
15630 if (DestReg) {
15631 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015632 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015633 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015634 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015635 } else if (Res.second == X86::FR32RegisterClass ||
15636 Res.second == X86::FR64RegisterClass ||
15637 Res.second == X86::VR128RegisterClass) {
15638 // Handle references to XMM physical registers that got mapped into the
15639 // wrong class. This can happen with constraints like {xmm0} where the
15640 // target independent register mapper will just pick the first match it can
15641 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015642 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015643 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015644 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015645 Res.second = X86::FR64RegisterClass;
15646 else if (X86::VR128RegisterClass->hasType(VT))
15647 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015648 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015649
Chris Lattnerf76d1802006-07-31 23:26:50 +000015650 return Res;
15651}