blob: 4f7bf15a79c58015026cc674d12db1457fe02f1a [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:
2855 case X86ISD::VPUNPCKLDQY:
2856 case X86ISD::VPUNPCKLQDQY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002857 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002858 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002859 case X86ISD::VUNPCKHPSY:
2860 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002861 case X86ISD::PUNPCKHWD:
2862 case X86ISD::PUNPCKHBW:
2863 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002864 case X86ISD::PUNPCKHQDQ:
Craig Topper6347e862011-11-21 06:57:39 +00002865 case X86ISD::VPUNPCKHWDY:
2866 case X86ISD::VPUNPCKHDQY:
2867 case X86ISD::VPUNPCKHQDQY:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002868 case X86ISD::VPERMILPS:
2869 case X86ISD::VPERMILPSY:
2870 case X86ISD::VPERMILPD:
2871 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002872 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002873 return true;
2874 }
2875 return false;
2876}
2877
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002878static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002879 SDValue V1, SelectionDAG &DAG) {
2880 switch(Opc) {
2881 default: llvm_unreachable("Unknown x86 shuffle node");
2882 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002883 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002884 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002885 return DAG.getNode(Opc, dl, VT, V1);
2886 }
2887
2888 return SDValue();
2889}
2890
2891static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002892 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002893 switch(Opc) {
2894 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002895 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002896 case X86ISD::PSHUFHW:
2897 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002898 case X86ISD::VPERMILPS:
2899 case X86ISD::VPERMILPSY:
2900 case X86ISD::VPERMILPD:
2901 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002902 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2903 }
2904
2905 return SDValue();
2906}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002907
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002908static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2909 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2910 switch(Opc) {
2911 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002912 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002913 case X86ISD::SHUFPD:
2914 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002915 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002916 return DAG.getNode(Opc, dl, VT, V1, V2,
2917 DAG.getConstant(TargetMask, MVT::i8));
2918 }
2919 return SDValue();
2920}
2921
2922static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2923 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2924 switch(Opc) {
2925 default: llvm_unreachable("Unknown x86 shuffle node");
2926 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002927 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002928 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002929 case X86ISD::MOVLPS:
2930 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002931 case X86ISD::MOVSS:
2932 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002933 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002934 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002935 case X86ISD::VUNPCKLPSY:
2936 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002937 case X86ISD::PUNPCKLWD:
2938 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002939 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002940 case X86ISD::PUNPCKLQDQ:
Craig Topper6347e862011-11-21 06:57:39 +00002941 case X86ISD::VPUNPCKLWDY:
2942 case X86ISD::VPUNPCKLDQY:
2943 case X86ISD::VPUNPCKLQDQY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002944 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002945 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002946 case X86ISD::VUNPCKHPSY:
2947 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002948 case X86ISD::PUNPCKHWD:
2949 case X86ISD::PUNPCKHBW:
2950 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002951 case X86ISD::PUNPCKHQDQ:
Craig Topper6347e862011-11-21 06:57:39 +00002952 case X86ISD::VPUNPCKHWDY:
2953 case X86ISD::VPUNPCKHDQY:
2954 case X86ISD::VPUNPCKHQDQY:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002955 return DAG.getNode(Opc, dl, VT, V1, V2);
2956 }
2957 return SDValue();
2958}
2959
Dan Gohmand858e902010-04-17 15:26:15 +00002960SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002961 MachineFunction &MF = DAG.getMachineFunction();
2962 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2963 int ReturnAddrIndex = FuncInfo->getRAIndex();
2964
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002965 if (ReturnAddrIndex == 0) {
2966 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002967 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002968 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002969 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002970 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002971 }
2972
Evan Cheng25ab6902006-09-08 06:48:29 +00002973 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002974}
2975
2976
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002977bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2978 bool hasSymbolicDisplacement) {
2979 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002980 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002981 return false;
2982
2983 // If we don't have a symbolic displacement - we don't have any extra
2984 // restrictions.
2985 if (!hasSymbolicDisplacement)
2986 return true;
2987
2988 // FIXME: Some tweaks might be needed for medium code model.
2989 if (M != CodeModel::Small && M != CodeModel::Kernel)
2990 return false;
2991
2992 // For small code model we assume that latest object is 16MB before end of 31
2993 // bits boundary. We may also accept pretty large negative constants knowing
2994 // that all objects are in the positive half of address space.
2995 if (M == CodeModel::Small && Offset < 16*1024*1024)
2996 return true;
2997
2998 // For kernel code model we know that all object resist in the negative half
2999 // of 32bits address space. We may not accept negative offsets, since they may
3000 // be just off and we may accept pretty large positive ones.
3001 if (M == CodeModel::Kernel && Offset > 0)
3002 return true;
3003
3004 return false;
3005}
3006
Evan Chengef41ff62011-06-23 17:54:54 +00003007/// isCalleePop - Determines whether the callee is required to pop its
3008/// own arguments. Callee pop is necessary to support tail calls.
3009bool X86::isCalleePop(CallingConv::ID CallingConv,
3010 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3011 if (IsVarArg)
3012 return false;
3013
3014 switch (CallingConv) {
3015 default:
3016 return false;
3017 case CallingConv::X86_StdCall:
3018 return !is64Bit;
3019 case CallingConv::X86_FastCall:
3020 return !is64Bit;
3021 case CallingConv::X86_ThisCall:
3022 return !is64Bit;
3023 case CallingConv::Fast:
3024 return TailCallOpt;
3025 case CallingConv::GHC:
3026 return TailCallOpt;
3027 }
3028}
3029
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003030/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3031/// specific condition code, returning the condition code and the LHS/RHS of the
3032/// comparison to make.
3033static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3034 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003035 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003036 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3037 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3038 // X > -1 -> X == 0, jump !sign.
3039 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003040 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003041 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
3042 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003043 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00003044 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003045 // X < 1 -> X <= 0
3046 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003047 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003048 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003049 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003050
Evan Chengd9558e02006-01-06 00:43:03 +00003051 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003052 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003053 case ISD::SETEQ: return X86::COND_E;
3054 case ISD::SETGT: return X86::COND_G;
3055 case ISD::SETGE: return X86::COND_GE;
3056 case ISD::SETLT: return X86::COND_L;
3057 case ISD::SETLE: return X86::COND_LE;
3058 case ISD::SETNE: return X86::COND_NE;
3059 case ISD::SETULT: return X86::COND_B;
3060 case ISD::SETUGT: return X86::COND_A;
3061 case ISD::SETULE: return X86::COND_BE;
3062 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003063 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003064 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003065
Chris Lattner4c78e022008-12-23 23:42:27 +00003066 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003067
Chris Lattner4c78e022008-12-23 23:42:27 +00003068 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003069 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3070 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003071 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3072 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003073 }
3074
Chris Lattner4c78e022008-12-23 23:42:27 +00003075 switch (SetCCOpcode) {
3076 default: break;
3077 case ISD::SETOLT:
3078 case ISD::SETOLE:
3079 case ISD::SETUGT:
3080 case ISD::SETUGE:
3081 std::swap(LHS, RHS);
3082 break;
3083 }
3084
3085 // On a floating point condition, the flags are set as follows:
3086 // ZF PF CF op
3087 // 0 | 0 | 0 | X > Y
3088 // 0 | 0 | 1 | X < Y
3089 // 1 | 0 | 0 | X == Y
3090 // 1 | 1 | 1 | unordered
3091 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003092 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003093 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003094 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003095 case ISD::SETOLT: // flipped
3096 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003097 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003098 case ISD::SETOLE: // flipped
3099 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003100 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003101 case ISD::SETUGT: // flipped
3102 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003103 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003104 case ISD::SETUGE: // flipped
3105 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003106 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003107 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003108 case ISD::SETNE: return X86::COND_NE;
3109 case ISD::SETUO: return X86::COND_P;
3110 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003111 case ISD::SETOEQ:
3112 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003113 }
Evan Chengd9558e02006-01-06 00:43:03 +00003114}
3115
Evan Cheng4a460802006-01-11 00:33:36 +00003116/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3117/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003118/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003119static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003120 switch (X86CC) {
3121 default:
3122 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003123 case X86::COND_B:
3124 case X86::COND_BE:
3125 case X86::COND_E:
3126 case X86::COND_P:
3127 case X86::COND_A:
3128 case X86::COND_AE:
3129 case X86::COND_NE:
3130 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003131 return true;
3132 }
3133}
3134
Evan Chengeb2f9692009-10-27 19:56:55 +00003135/// isFPImmLegal - Returns true if the target can instruction select the
3136/// specified FP immediate natively. If false, the legalizer will
3137/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003138bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003139 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3140 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3141 return true;
3142 }
3143 return false;
3144}
3145
Nate Begeman9008ca62009-04-27 18:41:29 +00003146/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3147/// the specified range (L, H].
3148static bool isUndefOrInRange(int Val, int Low, int Hi) {
3149 return (Val < 0) || (Val >= Low && Val < Hi);
3150}
3151
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003152/// isUndefOrInRange - Return true if every element in Mask, begining
3153/// from position Pos and ending in Pos+Size, falls within the specified
3154/// range (L, L+Pos]. or is undef.
3155static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3156 int Pos, int Size, int Low, int Hi) {
3157 for (int i = Pos, e = Pos+Size; i != e; ++i)
3158 if (!isUndefOrInRange(Mask[i], Low, Hi))
3159 return false;
3160 return true;
3161}
3162
Nate Begeman9008ca62009-04-27 18:41:29 +00003163/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3164/// specified value.
3165static bool isUndefOrEqual(int Val, int CmpVal) {
3166 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003167 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003168 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003169}
3170
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003171/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3172/// from position Pos and ending in Pos+Size, falls within the specified
3173/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003174static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3175 int Pos, int Size, int Low) {
3176 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3177 if (!isUndefOrEqual(Mask[i], Low))
3178 return false;
3179 return true;
3180}
3181
Nate Begeman9008ca62009-04-27 18:41:29 +00003182/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3183/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3184/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003185static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003186 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003187 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003188 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003189 return (Mask[0] < 2 && Mask[1] < 2);
3190 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003191}
3192
Nate Begeman9008ca62009-04-27 18:41:29 +00003193bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003194 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003195 N->getMask(M);
3196 return ::isPSHUFDMask(M, N->getValueType(0));
3197}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003198
Nate Begeman9008ca62009-04-27 18:41:29 +00003199/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3200/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003201static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003202 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003203 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003204
Nate Begeman9008ca62009-04-27 18:41:29 +00003205 // Lower quadword copied in order or undef.
3206 for (int i = 0; i != 4; ++i)
3207 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003208 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003209
Evan Cheng506d3df2006-03-29 23:07:14 +00003210 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003211 for (int i = 4; i != 8; ++i)
3212 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003213 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003214
Evan Cheng506d3df2006-03-29 23:07:14 +00003215 return true;
3216}
3217
Nate Begeman9008ca62009-04-27 18:41:29 +00003218bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003219 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003220 N->getMask(M);
3221 return ::isPSHUFHWMask(M, N->getValueType(0));
3222}
Evan Cheng506d3df2006-03-29 23:07:14 +00003223
Nate Begeman9008ca62009-04-27 18:41:29 +00003224/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3225/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003226static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003227 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003228 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003229
Rafael Espindola15684b22009-04-24 12:40:33 +00003230 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003231 for (int i = 4; i != 8; ++i)
3232 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003233 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003234
Rafael Espindola15684b22009-04-24 12:40:33 +00003235 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003236 for (int i = 0; i != 4; ++i)
3237 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003238 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003239
Rafael Espindola15684b22009-04-24 12:40:33 +00003240 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003241}
3242
Nate Begeman9008ca62009-04-27 18:41:29 +00003243bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003244 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003245 N->getMask(M);
3246 return ::isPSHUFLWMask(M, N->getValueType(0));
3247}
3248
Nate Begemana09008b2009-10-19 02:17:23 +00003249/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3250/// is suitable for input to PALIGNR.
3251static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003252 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003253 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003254 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3255 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003256
Nate Begemana09008b2009-10-19 02:17:23 +00003257 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003258 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003259 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003260
Nate Begemana09008b2009-10-19 02:17:23 +00003261 for (i = 0; i != e; ++i)
3262 if (Mask[i] >= 0)
3263 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003264
Nate Begemana09008b2009-10-19 02:17:23 +00003265 // All undef, not a palignr.
3266 if (i == e)
3267 return false;
3268
Eli Friedman63f8dde2011-07-25 21:36:45 +00003269 // Make sure we're shifting in the right direction.
3270 if (Mask[i] <= i)
3271 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003272
3273 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003274
Nate Begemana09008b2009-10-19 02:17:23 +00003275 // Check the rest of the elements to see if they are consecutive.
3276 for (++i; i != e; ++i) {
3277 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003278 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003279 return false;
3280 }
3281 return true;
3282}
3283
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003284/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3285/// specifies a shuffle of elements that is suitable for input to 256-bit
3286/// VSHUFPSY.
3287static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3288 const X86Subtarget *Subtarget) {
3289 int NumElems = VT.getVectorNumElements();
3290
3291 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3292 return false;
3293
3294 if (NumElems != 8)
3295 return false;
3296
3297 // VSHUFPSY divides the resulting vector into 4 chunks.
3298 // The sources are also splitted into 4 chunks, and each destination
3299 // chunk must come from a different source chunk.
3300 //
3301 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3302 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3303 //
3304 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3305 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3306 //
3307 int QuarterSize = NumElems/4;
3308 int HalfSize = QuarterSize*2;
3309 for (int i = 0; i < QuarterSize; ++i)
3310 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3311 return false;
3312 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3313 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3314 return false;
3315
3316 // The mask of the second half must be the same as the first but with
3317 // the appropriate offsets. This works in the same way as VPERMILPS
3318 // works with masks.
3319 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3320 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3321 return false;
3322 int FstHalfIdx = i-HalfSize;
3323 if (Mask[FstHalfIdx] < 0)
3324 continue;
3325 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3326 return false;
3327 }
3328 for (int i = QuarterSize*3; i < NumElems; ++i) {
3329 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3330 return false;
3331 int FstHalfIdx = i-HalfSize;
3332 if (Mask[FstHalfIdx] < 0)
3333 continue;
3334 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3335 return false;
3336
3337 }
3338
3339 return true;
3340}
3341
3342/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3343/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3344static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3345 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3346 EVT VT = SVOp->getValueType(0);
3347 int NumElems = VT.getVectorNumElements();
3348
3349 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3350 "Only supports v8i32 and v8f32 types");
3351
3352 int HalfSize = NumElems/2;
3353 unsigned Mask = 0;
3354 for (int i = 0; i != NumElems ; ++i) {
3355 if (SVOp->getMaskElt(i) < 0)
3356 continue;
3357 // The mask of the first half must be equal to the second one.
3358 unsigned Shamt = (i%HalfSize)*2;
3359 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3360 Mask |= Elt << Shamt;
3361 }
3362
3363 return Mask;
3364}
3365
3366/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3367/// specifies a shuffle of elements that is suitable for input to 256-bit
3368/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3369/// version and the mask of the second half isn't binded with the first
3370/// one.
3371static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3372 const X86Subtarget *Subtarget) {
3373 int NumElems = VT.getVectorNumElements();
3374
3375 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3376 return false;
3377
3378 if (NumElems != 4)
3379 return false;
3380
3381 // VSHUFPSY divides the resulting vector into 4 chunks.
3382 // The sources are also splitted into 4 chunks, and each destination
3383 // chunk must come from a different source chunk.
3384 //
3385 // SRC1 => X3 X2 X1 X0
3386 // SRC2 => Y3 Y2 Y1 Y0
3387 //
3388 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3389 //
3390 int QuarterSize = NumElems/4;
3391 int HalfSize = QuarterSize*2;
3392 for (int i = 0; i < QuarterSize; ++i)
3393 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3394 return false;
3395 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3396 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3397 return false;
3398 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3399 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3400 return false;
3401 for (int i = QuarterSize*3; i < NumElems; ++i)
3402 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3403 return false;
3404
3405 return true;
3406}
3407
3408/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3409/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3410static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3411 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3412 EVT VT = SVOp->getValueType(0);
3413 int NumElems = VT.getVectorNumElements();
3414
3415 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3416 "Only supports v4i64 and v4f64 types");
3417
3418 int HalfSize = NumElems/2;
3419 unsigned Mask = 0;
3420 for (int i = 0; i != NumElems ; ++i) {
3421 if (SVOp->getMaskElt(i) < 0)
3422 continue;
3423 int Elt = SVOp->getMaskElt(i) % HalfSize;
3424 Mask |= Elt << i;
3425 }
3426
3427 return Mask;
3428}
3429
Evan Cheng14aed5e2006-03-24 01:18:28 +00003430/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003431/// specifies a shuffle of elements that is suitable for input to 128-bit
3432/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003433static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003434 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003435
3436 if (VT.getSizeInBits() != 128)
3437 return false;
3438
Nate Begeman9008ca62009-04-27 18:41:29 +00003439 if (NumElems != 2 && NumElems != 4)
3440 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003441
Nate Begeman9008ca62009-04-27 18:41:29 +00003442 int Half = NumElems / 2;
3443 for (int i = 0; i < Half; ++i)
3444 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003445 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003446 for (int i = Half; i < NumElems; ++i)
3447 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003448 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003449
Evan Cheng14aed5e2006-03-24 01:18:28 +00003450 return true;
3451}
3452
Nate Begeman9008ca62009-04-27 18:41:29 +00003453bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3454 SmallVector<int, 8> M;
3455 N->getMask(M);
3456 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003457}
3458
Evan Cheng213d2cf2007-05-17 18:45:50 +00003459/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003460/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3461/// half elements to come from vector 1 (which would equal the dest.) and
3462/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003463static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003464 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003465
3466 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003467 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003468
Nate Begeman9008ca62009-04-27 18:41:29 +00003469 int Half = NumElems / 2;
3470 for (int i = 0; i < Half; ++i)
3471 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003472 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003473 for (int i = Half; i < NumElems; ++i)
3474 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003475 return false;
3476 return true;
3477}
3478
Nate Begeman9008ca62009-04-27 18:41:29 +00003479static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3480 SmallVector<int, 8> M;
3481 N->getMask(M);
3482 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003483}
3484
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003485/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3486/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003487bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003488 EVT VT = N->getValueType(0);
3489 unsigned NumElems = VT.getVectorNumElements();
3490
3491 if (VT.getSizeInBits() != 128)
3492 return false;
3493
3494 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003495 return false;
3496
Evan Cheng2064a2b2006-03-28 06:50:32 +00003497 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003498 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3499 isUndefOrEqual(N->getMaskElt(1), 7) &&
3500 isUndefOrEqual(N->getMaskElt(2), 2) &&
3501 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003502}
3503
Nate Begeman0b10b912009-11-07 23:17:15 +00003504/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3505/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3506/// <2, 3, 2, 3>
3507bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003508 EVT VT = N->getValueType(0);
3509 unsigned NumElems = VT.getVectorNumElements();
3510
3511 if (VT.getSizeInBits() != 128)
3512 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003513
Nate Begeman0b10b912009-11-07 23:17:15 +00003514 if (NumElems != 4)
3515 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003516
Nate Begeman0b10b912009-11-07 23:17:15 +00003517 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003518 isUndefOrEqual(N->getMaskElt(1), 3) &&
3519 isUndefOrEqual(N->getMaskElt(2), 2) &&
3520 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003521}
3522
Evan Cheng5ced1d82006-04-06 23:23:56 +00003523/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3524/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003525bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3526 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003527
Evan Cheng5ced1d82006-04-06 23:23:56 +00003528 if (NumElems != 2 && NumElems != 4)
3529 return false;
3530
Evan Chengc5cdff22006-04-07 21:53:05 +00003531 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003532 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003533 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003534
Evan Chengc5cdff22006-04-07 21:53:05 +00003535 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003536 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003537 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003538
3539 return true;
3540}
3541
Nate Begeman0b10b912009-11-07 23:17:15 +00003542/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3543/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3544bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003545 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003546
David Greenea20244d2011-03-02 17:23:43 +00003547 if ((NumElems != 2 && NumElems != 4)
3548 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003549 return false;
3550
Evan Chengc5cdff22006-04-07 21:53:05 +00003551 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003552 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003553 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003554
Nate Begeman9008ca62009-04-27 18:41:29 +00003555 for (unsigned i = 0; i < NumElems/2; ++i)
3556 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003557 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003558
3559 return true;
3560}
3561
Evan Cheng0038e592006-03-28 00:39:58 +00003562/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3563/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003564static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003565 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003566 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003567
3568 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3569 "Unsupported vector type for unpckh");
3570
Craig Topper6347e862011-11-21 06:57:39 +00003571 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3572 (!HasAVX2 || NumElts != 16))
Evan Cheng0038e592006-03-28 00:39:58 +00003573 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003574
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003575 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3576 // independently on 128-bit lanes.
3577 unsigned NumLanes = VT.getSizeInBits()/128;
3578 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003579
3580 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003581 unsigned End = NumLaneElts;
3582 for (unsigned s = 0; s < NumLanes; ++s) {
3583 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003584 i != End;
3585 i += 2, ++j) {
3586 int BitI = Mask[i];
3587 int BitI1 = Mask[i+1];
3588 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003589 return false;
David Greenea20244d2011-03-02 17:23:43 +00003590 if (V2IsSplat) {
3591 if (!isUndefOrEqual(BitI1, NumElts))
3592 return false;
3593 } else {
3594 if (!isUndefOrEqual(BitI1, j + NumElts))
3595 return false;
3596 }
Evan Cheng39623da2006-04-20 08:58:49 +00003597 }
David Greenea20244d2011-03-02 17:23:43 +00003598 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003599 Start += NumLaneElts;
3600 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003601 }
David Greenea20244d2011-03-02 17:23:43 +00003602
Evan Cheng0038e592006-03-28 00:39:58 +00003603 return true;
3604}
3605
Craig Topper6347e862011-11-21 06:57:39 +00003606bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003607 SmallVector<int, 8> M;
3608 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003609 return ::isUNPCKLMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003610}
3611
Evan Cheng4fcb9222006-03-28 02:43:26 +00003612/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3613/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003614static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003615 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003616 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003617
3618 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3619 "Unsupported vector type for unpckh");
3620
Craig Topper6347e862011-11-21 06:57:39 +00003621 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3622 (!HasAVX2 || NumElts != 16))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003623 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003624
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003625 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3626 // independently on 128-bit lanes.
3627 unsigned NumLanes = VT.getSizeInBits()/128;
3628 unsigned NumLaneElts = NumElts/NumLanes;
3629
3630 unsigned Start = 0;
3631 unsigned End = NumLaneElts;
3632 for (unsigned l = 0; l != NumLanes; ++l) {
3633 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3634 i != End; i += 2, ++j) {
3635 int BitI = Mask[i];
3636 int BitI1 = Mask[i+1];
3637 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003638 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003639 if (V2IsSplat) {
3640 if (isUndefOrEqual(BitI1, NumElts))
3641 return false;
3642 } else {
3643 if (!isUndefOrEqual(BitI1, j+NumElts))
3644 return false;
3645 }
Evan Cheng39623da2006-04-20 08:58:49 +00003646 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003647 // Process the next 128 bits.
3648 Start += NumLaneElts;
3649 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003650 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003651 return true;
3652}
3653
Craig Topper6347e862011-11-21 06:57:39 +00003654bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003655 SmallVector<int, 8> M;
3656 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003657 return ::isUNPCKHMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003658}
3659
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003660/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3661/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3662/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003663static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003664 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003665 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003666 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003667
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003668 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3669 // FIXME: Need a better way to get rid of this, there's no latency difference
3670 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3671 // the former later. We should also remove the "_undef" special mask.
3672 if (NumElems == 4 && VT.getSizeInBits() == 256)
3673 return false;
3674
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003675 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3676 // independently on 128-bit lanes.
3677 unsigned NumLanes = VT.getSizeInBits() / 128;
3678 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003679
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003680 for (unsigned s = 0; s < NumLanes; ++s) {
3681 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3682 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003683 i += 2, ++j) {
3684 int BitI = Mask[i];
3685 int BitI1 = Mask[i+1];
3686
3687 if (!isUndefOrEqual(BitI, j))
3688 return false;
3689 if (!isUndefOrEqual(BitI1, j))
3690 return false;
3691 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003692 }
David Greenea20244d2011-03-02 17:23:43 +00003693
Rafael Espindola15684b22009-04-24 12:40:33 +00003694 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003695}
3696
Nate Begeman9008ca62009-04-27 18:41:29 +00003697bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3698 SmallVector<int, 8> M;
3699 N->getMask(M);
3700 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3701}
3702
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003703/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3704/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3705/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003706static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003707 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003708 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3709 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003710
Nate Begeman9008ca62009-04-27 18:41:29 +00003711 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3712 int BitI = Mask[i];
3713 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003714 if (!isUndefOrEqual(BitI, j))
3715 return false;
3716 if (!isUndefOrEqual(BitI1, j))
3717 return false;
3718 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003719 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003720}
3721
Nate Begeman9008ca62009-04-27 18:41:29 +00003722bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3723 SmallVector<int, 8> M;
3724 N->getMask(M);
3725 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3726}
3727
Evan Cheng017dcc62006-04-21 01:05:10 +00003728/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3729/// specifies a shuffle of elements that is suitable for input to MOVSS,
3730/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003731static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003732 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003733 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003734
3735 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003736
Nate Begeman9008ca62009-04-27 18:41:29 +00003737 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003738 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003739
Nate Begeman9008ca62009-04-27 18:41:29 +00003740 for (int i = 1; i < NumElts; ++i)
3741 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003742 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003743
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003744 return true;
3745}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003746
Nate Begeman9008ca62009-04-27 18:41:29 +00003747bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3748 SmallVector<int, 8> M;
3749 N->getMask(M);
3750 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003751}
3752
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003753/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3754/// as permutations between 128-bit chunks or halves. As an example: this
3755/// shuffle bellow:
3756/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3757/// The first half comes from the second half of V1 and the second half from the
3758/// the second half of V2.
3759static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3760 const X86Subtarget *Subtarget) {
3761 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3762 return false;
3763
3764 // The shuffle result is divided into half A and half B. In total the two
3765 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3766 // B must come from C, D, E or F.
3767 int HalfSize = VT.getVectorNumElements()/2;
3768 bool MatchA = false, MatchB = false;
3769
3770 // Check if A comes from one of C, D, E, F.
3771 for (int Half = 0; Half < 4; ++Half) {
3772 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3773 MatchA = true;
3774 break;
3775 }
3776 }
3777
3778 // Check if B comes from one of C, D, E, F.
3779 for (int Half = 0; Half < 4; ++Half) {
3780 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3781 MatchB = true;
3782 break;
3783 }
3784 }
3785
3786 return MatchA && MatchB;
3787}
3788
3789/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3790/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3791static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3792 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3793 EVT VT = SVOp->getValueType(0);
3794
3795 int HalfSize = VT.getVectorNumElements()/2;
3796
3797 int FstHalf = 0, SndHalf = 0;
3798 for (int i = 0; i < HalfSize; ++i) {
3799 if (SVOp->getMaskElt(i) > 0) {
3800 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3801 break;
3802 }
3803 }
3804 for (int i = HalfSize; i < HalfSize*2; ++i) {
3805 if (SVOp->getMaskElt(i) > 0) {
3806 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3807 break;
3808 }
3809 }
3810
3811 return (FstHalf | (SndHalf << 4));
3812}
3813
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003814/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3815/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3816/// Note that VPERMIL mask matching is different depending whether theunderlying
3817/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3818/// to the same elements of the low, but to the higher half of the source.
3819/// In VPERMILPD the two lanes could be shuffled independently of each other
3820/// with the same restriction that lanes can't be crossed.
3821static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3822 const X86Subtarget *Subtarget) {
3823 int NumElts = VT.getVectorNumElements();
3824 int NumLanes = VT.getSizeInBits()/128;
3825
3826 if (!Subtarget->hasAVX())
3827 return false;
3828
Eli Friedmandca62d52011-10-10 22:28:47 +00003829 // Only match 256-bit with 64-bit types
3830 if (VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003831 return false;
3832
3833 // The mask on the high lane is independent of the low. Both can match
3834 // any element in inside its own lane, but can't cross.
3835 int LaneSize = NumElts/NumLanes;
3836 for (int l = 0; l < NumLanes; ++l)
3837 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3838 int LaneStart = l*LaneSize;
3839 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3840 return false;
3841 }
3842
3843 return true;
3844}
3845
3846/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3847/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3848/// Note that VPERMIL mask matching is different depending whether theunderlying
3849/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3850/// to the same elements of the low, but to the higher half of the source.
3851/// In VPERMILPD the two lanes could be shuffled independently of each other
3852/// with the same restriction that lanes can't be crossed.
3853static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3854 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003855 unsigned NumElts = VT.getVectorNumElements();
3856 unsigned NumLanes = VT.getSizeInBits()/128;
3857
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003858 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003859 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003860
Eli Friedmandca62d52011-10-10 22:28:47 +00003861 // Only match 256-bit with 32-bit types
3862 if (VT.getSizeInBits() != 256 || NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003863 return false;
3864
3865 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003866 // they can differ if any of the corresponding index in a lane is undef
3867 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003868 int LaneSize = NumElts/NumLanes;
3869 for (int i = 0; i < LaneSize; ++i) {
3870 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003871 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3872 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3873
3874 if (!HighValid || !LowValid)
3875 return false;
3876 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003877 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003878 if (Mask[HighElt]-Mask[i] != LaneSize)
3879 return false;
3880 }
3881
3882 return true;
3883}
3884
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003885/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3886/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3887static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003888 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3889 EVT VT = SVOp->getValueType(0);
3890
3891 int NumElts = VT.getVectorNumElements();
3892 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003893 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003894
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003895 // Although the mask is equal for both lanes do it twice to get the cases
3896 // where a mask will match because the same mask element is undef on the
3897 // first half but valid on the second. This would get pathological cases
3898 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003899 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003900 for (int l = 0; l < NumLanes; ++l) {
3901 for (int i = 0; i < LaneSize; ++i) {
3902 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3903 if (MaskElt < 0)
3904 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003905 if (MaskElt >= LaneSize)
3906 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003907 Mask |= MaskElt << (i*2);
3908 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003909 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003910
3911 return Mask;
3912}
3913
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003914/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3915/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3916static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3917 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3918 EVT VT = SVOp->getValueType(0);
3919
3920 int NumElts = VT.getVectorNumElements();
3921 int NumLanes = VT.getSizeInBits()/128;
3922
3923 unsigned Mask = 0;
3924 int LaneSize = NumElts/NumLanes;
3925 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003926 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3927 int MaskElt = SVOp->getMaskElt(i);
3928 if (MaskElt < 0)
3929 continue;
3930 Mask |= (MaskElt-l*LaneSize) << i;
3931 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003932
3933 return Mask;
3934}
3935
Evan Cheng017dcc62006-04-21 01:05:10 +00003936/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3937/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003938/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003939static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003940 bool V2IsSplat = false, bool V2IsUndef = false) {
3941 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003942 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003943 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003944
Nate Begeman9008ca62009-04-27 18:41:29 +00003945 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003946 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003947
Nate Begeman9008ca62009-04-27 18:41:29 +00003948 for (int i = 1; i < NumOps; ++i)
3949 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3950 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3951 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003952 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003953
Evan Cheng39623da2006-04-20 08:58:49 +00003954 return true;
3955}
3956
Nate Begeman9008ca62009-04-27 18:41:29 +00003957static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003958 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003959 SmallVector<int, 8> M;
3960 N->getMask(M);
3961 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003962}
3963
Evan Chengd9539472006-04-14 21:59:03 +00003964/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3965/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003966/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3967bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3968 const X86Subtarget *Subtarget) {
3969 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003970 return false;
3971
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003972 // The second vector must be undef
3973 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3974 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003975
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003976 EVT VT = N->getValueType(0);
3977 unsigned NumElems = VT.getVectorNumElements();
3978
3979 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3980 (VT.getSizeInBits() == 256 && NumElems != 8))
3981 return false;
3982
3983 // "i+1" is the value the indexed mask element must have
3984 for (unsigned i = 0; i < NumElems; i += 2)
3985 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3986 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003987 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003988
3989 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003990}
3991
3992/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3993/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003994/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3995bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3996 const X86Subtarget *Subtarget) {
3997 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003998 return false;
3999
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004000 // The second vector must be undef
4001 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
4002 return false;
4003
4004 EVT VT = N->getValueType(0);
4005 unsigned NumElems = VT.getVectorNumElements();
4006
4007 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
4008 (VT.getSizeInBits() == 256 && NumElems != 8))
4009 return false;
4010
4011 // "i" is the value the indexed mask element must have
4012 for (unsigned i = 0; i < NumElems; i += 2)
4013 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
4014 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00004015 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00004016
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004017 return true;
Evan Chengd9539472006-04-14 21:59:03 +00004018}
4019
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004020/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
4021/// specifies a shuffle of elements that is suitable for input to 256-bit
4022/// version of MOVDDUP.
4023static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
4024 const X86Subtarget *Subtarget) {
4025 EVT VT = N->getValueType(0);
4026 int NumElts = VT.getVectorNumElements();
4027 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
4028
4029 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
4030 !V2IsUndef || NumElts != 4)
4031 return false;
4032
4033 for (int i = 0; i != NumElts/2; ++i)
4034 if (!isUndefOrEqual(N->getMaskElt(i), 0))
4035 return false;
4036 for (int i = NumElts/2; i != NumElts; ++i)
4037 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
4038 return false;
4039 return true;
4040}
4041
Evan Cheng0b457f02008-09-25 20:50:48 +00004042/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004043/// specifies a shuffle of elements that is suitable for input to 128-bit
4044/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00004045bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004046 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004047
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004048 if (VT.getSizeInBits() != 128)
4049 return false;
4050
4051 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004052 for (int i = 0; i < e; ++i)
4053 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004054 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004055 for (int i = 0; i < e; ++i)
4056 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004057 return false;
4058 return true;
4059}
4060
David Greenec38a03e2011-02-03 15:50:00 +00004061/// isVEXTRACTF128Index - Return true if the specified
4062/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4063/// suitable for input to VEXTRACTF128.
4064bool X86::isVEXTRACTF128Index(SDNode *N) {
4065 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4066 return false;
4067
4068 // The index should be aligned on a 128-bit boundary.
4069 uint64_t Index =
4070 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4071
4072 unsigned VL = N->getValueType(0).getVectorNumElements();
4073 unsigned VBits = N->getValueType(0).getSizeInBits();
4074 unsigned ElSize = VBits / VL;
4075 bool Result = (Index * ElSize) % 128 == 0;
4076
4077 return Result;
4078}
4079
David Greeneccacdc12011-02-04 16:08:29 +00004080/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4081/// operand specifies a subvector insert that is suitable for input to
4082/// VINSERTF128.
4083bool X86::isVINSERTF128Index(SDNode *N) {
4084 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4085 return false;
4086
4087 // The index should be aligned on a 128-bit boundary.
4088 uint64_t Index =
4089 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4090
4091 unsigned VL = N->getValueType(0).getVectorNumElements();
4092 unsigned VBits = N->getValueType(0).getSizeInBits();
4093 unsigned ElSize = VBits / VL;
4094 bool Result = (Index * ElSize) % 128 == 0;
4095
4096 return Result;
4097}
4098
Evan Cheng63d33002006-03-22 08:01:21 +00004099/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004100/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004101unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004102 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4103 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4104
Evan Chengb9df0ca2006-03-22 02:53:00 +00004105 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4106 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004107 for (int i = 0; i < NumOperands; ++i) {
4108 int Val = SVOp->getMaskElt(NumOperands-i-1);
4109 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004110 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004111 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004112 if (i != NumOperands - 1)
4113 Mask <<= Shift;
4114 }
Evan Cheng63d33002006-03-22 08:01:21 +00004115 return Mask;
4116}
4117
Evan Cheng506d3df2006-03-29 23:07:14 +00004118/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004119/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004120unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004121 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004122 unsigned Mask = 0;
4123 // 8 nodes, but we only care about the last 4.
4124 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004125 int Val = SVOp->getMaskElt(i);
4126 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004127 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004128 if (i != 4)
4129 Mask <<= 2;
4130 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004131 return Mask;
4132}
4133
4134/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004135/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004136unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004137 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004138 unsigned Mask = 0;
4139 // 8 nodes, but we only care about the first 4.
4140 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004141 int Val = SVOp->getMaskElt(i);
4142 if (Val >= 0)
4143 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004144 if (i != 0)
4145 Mask <<= 2;
4146 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004147 return Mask;
4148}
4149
Nate Begemana09008b2009-10-19 02:17:23 +00004150/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4151/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4152unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4153 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4154 EVT VVT = N->getValueType(0);
4155 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4156 int Val = 0;
4157
4158 unsigned i, e;
4159 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4160 Val = SVOp->getMaskElt(i);
4161 if (Val >= 0)
4162 break;
4163 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004164 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004165 return (Val - i) * EltSize;
4166}
4167
David Greenec38a03e2011-02-03 15:50:00 +00004168/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4169/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4170/// instructions.
4171unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4172 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4173 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4174
4175 uint64_t Index =
4176 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4177
4178 EVT VecVT = N->getOperand(0).getValueType();
4179 EVT ElVT = VecVT.getVectorElementType();
4180
4181 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004182 return Index / NumElemsPerChunk;
4183}
4184
David Greeneccacdc12011-02-04 16:08:29 +00004185/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4186/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4187/// instructions.
4188unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4189 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4190 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4191
4192 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004193 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004194
4195 EVT VecVT = N->getValueType(0);
4196 EVT ElVT = VecVT.getVectorElementType();
4197
4198 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004199 return Index / NumElemsPerChunk;
4200}
4201
Evan Cheng37b73872009-07-30 08:33:02 +00004202/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4203/// constant +0.0.
4204bool X86::isZeroNode(SDValue Elt) {
4205 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004206 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004207 (isa<ConstantFPSDNode>(Elt) &&
4208 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4209}
4210
Nate Begeman9008ca62009-04-27 18:41:29 +00004211/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4212/// their permute mask.
4213static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4214 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004215 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004216 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004217 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004218
Nate Begeman5a5ca152009-04-29 05:20:52 +00004219 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004220 int idx = SVOp->getMaskElt(i);
4221 if (idx < 0)
4222 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004223 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004224 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004225 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004226 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004227 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004228 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4229 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004230}
4231
Evan Cheng779ccea2007-12-07 21:30:01 +00004232/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4233/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004234static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004235 unsigned NumElems = VT.getVectorNumElements();
4236 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004237 int idx = Mask[i];
4238 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004239 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004240 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004241 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004242 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004243 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004244 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004245}
4246
Evan Cheng533a0aa2006-04-19 20:35:22 +00004247/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4248/// match movhlps. The lower half elements should come from upper half of
4249/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004250/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004251static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004252 EVT VT = Op->getValueType(0);
4253 if (VT.getSizeInBits() != 128)
4254 return false;
4255 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004256 return false;
4257 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004258 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004259 return false;
4260 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004261 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004262 return false;
4263 return true;
4264}
4265
Evan Cheng5ced1d82006-04-06 23:23:56 +00004266/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004267/// is promoted to a vector. It also returns the LoadSDNode by reference if
4268/// required.
4269static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004270 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4271 return false;
4272 N = N->getOperand(0).getNode();
4273 if (!ISD::isNON_EXTLoad(N))
4274 return false;
4275 if (LD)
4276 *LD = cast<LoadSDNode>(N);
4277 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004278}
4279
Dan Gohman65fd6562011-11-03 21:49:52 +00004280// Test whether the given value is a vector value which will be legalized
4281// into a load.
4282static bool WillBeConstantPoolLoad(SDNode *N) {
4283 if (N->getOpcode() != ISD::BUILD_VECTOR)
4284 return false;
4285
4286 // Check for any non-constant elements.
4287 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4288 switch (N->getOperand(i).getNode()->getOpcode()) {
4289 case ISD::UNDEF:
4290 case ISD::ConstantFP:
4291 case ISD::Constant:
4292 break;
4293 default:
4294 return false;
4295 }
4296
4297 // Vectors of all-zeros and all-ones are materialized with special
4298 // instructions rather than being loaded.
4299 return !ISD::isBuildVectorAllZeros(N) &&
4300 !ISD::isBuildVectorAllOnes(N);
4301}
4302
Evan Cheng533a0aa2006-04-19 20:35:22 +00004303/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4304/// match movlp{s|d}. The lower half elements should come from lower half of
4305/// V1 (and in order), and the upper half elements should come from the upper
4306/// half of V2 (and in order). And since V1 will become the source of the
4307/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004308static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4309 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004310 EVT VT = Op->getValueType(0);
4311 if (VT.getSizeInBits() != 128)
4312 return false;
4313
Evan Cheng466685d2006-10-09 20:57:25 +00004314 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004315 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004316 // Is V2 is a vector load, don't do this transformation. We will try to use
4317 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004318 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004319 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004320
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004321 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004322
Evan Cheng533a0aa2006-04-19 20:35:22 +00004323 if (NumElems != 2 && NumElems != 4)
4324 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004325 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004326 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004327 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004328 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004329 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004330 return false;
4331 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004332}
4333
Evan Cheng39623da2006-04-20 08:58:49 +00004334/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4335/// all the same.
4336static bool isSplatVector(SDNode *N) {
4337 if (N->getOpcode() != ISD::BUILD_VECTOR)
4338 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004339
Dan Gohman475871a2008-07-27 21:46:04 +00004340 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004341 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4342 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004343 return false;
4344 return true;
4345}
4346
Evan Cheng213d2cf2007-05-17 18:45:50 +00004347/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004348/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004349/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004350static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004351 SDValue V1 = N->getOperand(0);
4352 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004353 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4354 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004355 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004356 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004357 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004358 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4359 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004360 if (Opc != ISD::BUILD_VECTOR ||
4361 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004362 return false;
4363 } else if (Idx >= 0) {
4364 unsigned Opc = V1.getOpcode();
4365 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4366 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004367 if (Opc != ISD::BUILD_VECTOR ||
4368 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004369 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004370 }
4371 }
4372 return true;
4373}
4374
4375/// getZeroVector - Returns a vector of specified type with all zero elements.
4376///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004377static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004378 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004379 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004380
Dale Johannesen0488fb62010-09-30 23:57:10 +00004381 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004382 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004383 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004384 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004385 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004386 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4387 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4388 } else { // SSE1
4389 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4390 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4391 }
4392 } else if (VT.getSizeInBits() == 256) { // AVX
4393 // 256-bit logic and arithmetic instructions in AVX are
4394 // all floating-point, no support for integer ops. Default
4395 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004396 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004397 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4398 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004399 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004400 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004401}
4402
Chris Lattner8a594482007-11-25 00:24:49 +00004403/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004404/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4405/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4406/// Then bitcast to their original type, ensuring they get CSE'd.
4407static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4408 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004409 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004410 assert((VT.is128BitVector() || VT.is256BitVector())
4411 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004412
Owen Anderson825b72b2009-08-11 20:47:22 +00004413 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004414 SDValue Vec;
4415 if (VT.getSizeInBits() == 256) {
4416 if (HasAVX2) { // AVX2
4417 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4418 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4419 } else { // AVX
4420 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4421 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4422 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4423 Vec = Insert128BitVector(InsV, Vec,
4424 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4425 }
4426 } else {
4427 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004428 }
4429
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004430 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004431}
4432
Evan Cheng39623da2006-04-20 08:58:49 +00004433/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4434/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004435static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004436 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004437 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004438
Evan Cheng39623da2006-04-20 08:58:49 +00004439 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004440 SmallVector<int, 8> MaskVec;
4441 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004442
Nate Begeman5a5ca152009-04-29 05:20:52 +00004443 for (unsigned i = 0; i != NumElems; ++i) {
4444 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004445 MaskVec[i] = NumElems;
4446 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004447 }
Evan Cheng39623da2006-04-20 08:58:49 +00004448 }
Evan Cheng39623da2006-04-20 08:58:49 +00004449 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004450 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4451 SVOp->getOperand(1), &MaskVec[0]);
4452 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004453}
4454
Evan Cheng017dcc62006-04-21 01:05:10 +00004455/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4456/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004457static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004458 SDValue V2) {
4459 unsigned NumElems = VT.getVectorNumElements();
4460 SmallVector<int, 8> Mask;
4461 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004462 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004463 Mask.push_back(i);
4464 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004465}
4466
Nate Begeman9008ca62009-04-27 18:41:29 +00004467/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004468static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004469 SDValue V2) {
4470 unsigned NumElems = VT.getVectorNumElements();
4471 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004472 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004473 Mask.push_back(i);
4474 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004475 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004476 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004477}
4478
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004479/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004480static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004481 SDValue V2) {
4482 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004483 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004484 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004485 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004486 Mask.push_back(i + Half);
4487 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004488 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004489 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004490}
4491
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004492// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004493// a generic shuffle instruction because the target has no such instructions.
4494// Generate shuffles which repeat i16 and i8 several times until they can be
4495// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004496static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004497 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004498 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004499 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004500
Nate Begeman9008ca62009-04-27 18:41:29 +00004501 while (NumElems > 4) {
4502 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004503 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004504 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004505 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004506 EltNo -= NumElems/2;
4507 }
4508 NumElems >>= 1;
4509 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004510 return V;
4511}
Eric Christopherfd179292009-08-27 18:07:15 +00004512
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004513/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4514static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4515 EVT VT = V.getValueType();
4516 DebugLoc dl = V.getDebugLoc();
4517 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4518 && "Vector size not supported");
4519
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004520 if (VT.getSizeInBits() == 128) {
4521 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004522 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004523 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4524 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004525 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004526 // To use VPERMILPS to splat scalars, the second half of indicies must
4527 // refer to the higher part, which is a duplication of the lower one,
4528 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004529 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4530 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004531
4532 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4533 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4534 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004535 }
4536
4537 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4538}
4539
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004540/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004541static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4542 EVT SrcVT = SV->getValueType(0);
4543 SDValue V1 = SV->getOperand(0);
4544 DebugLoc dl = SV->getDebugLoc();
4545
4546 int EltNo = SV->getSplatIndex();
4547 int NumElems = SrcVT.getVectorNumElements();
4548 unsigned Size = SrcVT.getSizeInBits();
4549
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004550 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4551 "Unknown how to promote splat for type");
4552
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004553 // Extract the 128-bit part containing the splat element and update
4554 // the splat element index when it refers to the higher register.
4555 if (Size == 256) {
4556 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4557 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4558 if (Idx > 0)
4559 EltNo -= NumElems/2;
4560 }
4561
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004562 // All i16 and i8 vector types can't be used directly by a generic shuffle
4563 // instruction because the target has no such instruction. Generate shuffles
4564 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004565 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004566 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004567 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004568 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004569
4570 // Recreate the 256-bit vector and place the same 128-bit vector
4571 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004572 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004573 if (Size == 256) {
4574 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4575 DAG.getConstant(0, MVT::i32), DAG, dl);
4576 V1 = Insert128BitVector(InsV, V1,
4577 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4578 }
4579
4580 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004581}
4582
Evan Chengba05f722006-04-21 23:03:30 +00004583/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004584/// vector of zero or undef vector. This produces a shuffle where the low
4585/// element of V2 is swizzled into the zero/undef vector, landing at element
4586/// 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 +00004587static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004588 bool isZero, bool HasXMMInt,
4589 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004590 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004591 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004592 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004593 unsigned NumElems = VT.getVectorNumElements();
4594 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004595 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004596 // If this is the insertion idx, put the low elt of V2 here.
4597 MaskVec.push_back(i == Idx ? NumElems : i);
4598 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004599}
4600
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004601/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4602/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004603static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4604 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004605 if (Depth == 6)
4606 return SDValue(); // Limit search depth.
4607
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004608 SDValue V = SDValue(N, 0);
4609 EVT VT = V.getValueType();
4610 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004611
4612 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4613 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4614 Index = SV->getMaskElt(Index);
4615
4616 if (Index < 0)
4617 return DAG.getUNDEF(VT.getVectorElementType());
4618
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004619 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004620 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004621 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004622 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004623
4624 // Recurse into target specific vector shuffles to find scalars.
4625 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004626 int NumElems = VT.getVectorNumElements();
4627 SmallVector<unsigned, 16> ShuffleMask;
4628 SDValue ImmN;
4629
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004630 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004631 case X86ISD::SHUFPS:
4632 case X86ISD::SHUFPD:
4633 ImmN = N->getOperand(N->getNumOperands()-1);
4634 DecodeSHUFPSMask(NumElems,
4635 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4636 ShuffleMask);
4637 break;
4638 case X86ISD::PUNPCKHBW:
4639 case X86ISD::PUNPCKHWD:
4640 case X86ISD::PUNPCKHDQ:
4641 case X86ISD::PUNPCKHQDQ:
Craig Topper6347e862011-11-21 06:57:39 +00004642 case X86ISD::VPUNPCKHWDY:
4643 case X86ISD::VPUNPCKHDQY:
4644 case X86ISD::VPUNPCKHQDQY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004645 DecodePUNPCKHMask(NumElems, ShuffleMask);
4646 break;
4647 case X86ISD::UNPCKHPS:
4648 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004649 case X86ISD::VUNPCKHPSY:
4650 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004651 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4652 break;
4653 case X86ISD::PUNPCKLBW:
4654 case X86ISD::PUNPCKLWD:
4655 case X86ISD::PUNPCKLDQ:
4656 case X86ISD::PUNPCKLQDQ:
Craig Topper6347e862011-11-21 06:57:39 +00004657 case X86ISD::VPUNPCKLWDY:
4658 case X86ISD::VPUNPCKLDQY:
4659 case X86ISD::VPUNPCKLQDQY:
David Greenec4db4e52011-02-28 19:06:56 +00004660 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004661 break;
4662 case X86ISD::UNPCKLPS:
4663 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004664 case X86ISD::VUNPCKLPSY:
4665 case X86ISD::VUNPCKLPDY:
4666 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004667 break;
4668 case X86ISD::MOVHLPS:
4669 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4670 break;
4671 case X86ISD::MOVLHPS:
4672 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4673 break;
4674 case X86ISD::PSHUFD:
4675 ImmN = N->getOperand(N->getNumOperands()-1);
4676 DecodePSHUFMask(NumElems,
4677 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4678 ShuffleMask);
4679 break;
4680 case X86ISD::PSHUFHW:
4681 ImmN = N->getOperand(N->getNumOperands()-1);
4682 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4683 ShuffleMask);
4684 break;
4685 case X86ISD::PSHUFLW:
4686 ImmN = N->getOperand(N->getNumOperands()-1);
4687 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4688 ShuffleMask);
4689 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004690 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004691 case X86ISD::MOVSD: {
4692 // The index 0 always comes from the first element of the second source,
4693 // this is why MOVSS and MOVSD are used in the first place. The other
4694 // elements come from the other positions of the first source vector.
4695 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004696 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4697 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004698 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004699 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004700 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004701 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004702 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004703 break;
4704 case X86ISD::VPERMILPSY:
4705 ImmN = N->getOperand(N->getNumOperands()-1);
4706 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4707 ShuffleMask);
4708 break;
4709 case X86ISD::VPERMILPD:
4710 ImmN = N->getOperand(N->getNumOperands()-1);
4711 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4712 ShuffleMask);
4713 break;
4714 case X86ISD::VPERMILPDY:
4715 ImmN = N->getOperand(N->getNumOperands()-1);
4716 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4717 ShuffleMask);
4718 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004719 case X86ISD::VPERM2F128:
4720 ImmN = N->getOperand(N->getNumOperands()-1);
4721 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4722 ShuffleMask);
4723 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004724 case X86ISD::MOVDDUP:
4725 case X86ISD::MOVLHPD:
4726 case X86ISD::MOVLPD:
4727 case X86ISD::MOVLPS:
4728 case X86ISD::MOVSHDUP:
4729 case X86ISD::MOVSLDUP:
4730 case X86ISD::PALIGN:
4731 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004732 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004733 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004734 return SDValue();
4735 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004736
4737 Index = ShuffleMask[Index];
4738 if (Index < 0)
4739 return DAG.getUNDEF(VT.getVectorElementType());
4740
4741 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4742 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4743 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004744 }
4745
4746 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004747 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004748 V = V.getOperand(0);
4749 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004750 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004751
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004752 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004753 return SDValue();
4754 }
4755
4756 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4757 return (Index == 0) ? V.getOperand(0)
4758 : DAG.getUNDEF(VT.getVectorElementType());
4759
4760 if (V.getOpcode() == ISD::BUILD_VECTOR)
4761 return V.getOperand(Index);
4762
4763 return SDValue();
4764}
4765
4766/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4767/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004768/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004769static
4770unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4771 bool ZerosFromLeft, SelectionDAG &DAG) {
4772 int i = 0;
4773
4774 while (i < NumElems) {
4775 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004776 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004777 if (!(Elt.getNode() &&
4778 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4779 break;
4780 ++i;
4781 }
4782
4783 return i;
4784}
4785
4786/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4787/// MaskE correspond consecutively to elements from one of the vector operands,
4788/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4789static
4790bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4791 int OpIdx, int NumElems, unsigned &OpNum) {
4792 bool SeenV1 = false;
4793 bool SeenV2 = false;
4794
4795 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4796 int Idx = SVOp->getMaskElt(i);
4797 // Ignore undef indicies
4798 if (Idx < 0)
4799 continue;
4800
4801 if (Idx < NumElems)
4802 SeenV1 = true;
4803 else
4804 SeenV2 = true;
4805
4806 // Only accept consecutive elements from the same vector
4807 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4808 return false;
4809 }
4810
4811 OpNum = SeenV1 ? 0 : 1;
4812 return true;
4813}
4814
4815/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4816/// logical left shift of a vector.
4817static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4818 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4819 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4820 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4821 false /* check zeros from right */, DAG);
4822 unsigned OpSrc;
4823
4824 if (!NumZeros)
4825 return false;
4826
4827 // Considering the elements in the mask that are not consecutive zeros,
4828 // check if they consecutively come from only one of the source vectors.
4829 //
4830 // V1 = {X, A, B, C} 0
4831 // \ \ \ /
4832 // vector_shuffle V1, V2 <1, 2, 3, X>
4833 //
4834 if (!isShuffleMaskConsecutive(SVOp,
4835 0, // Mask Start Index
4836 NumElems-NumZeros-1, // Mask End Index
4837 NumZeros, // Where to start looking in the src vector
4838 NumElems, // Number of elements in vector
4839 OpSrc)) // Which source operand ?
4840 return false;
4841
4842 isLeft = false;
4843 ShAmt = NumZeros;
4844 ShVal = SVOp->getOperand(OpSrc);
4845 return true;
4846}
4847
4848/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4849/// logical left shift of a vector.
4850static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4851 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4852 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4853 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4854 true /* check zeros from left */, DAG);
4855 unsigned OpSrc;
4856
4857 if (!NumZeros)
4858 return false;
4859
4860 // Considering the elements in the mask that are not consecutive zeros,
4861 // check if they consecutively come from only one of the source vectors.
4862 //
4863 // 0 { A, B, X, X } = V2
4864 // / \ / /
4865 // vector_shuffle V1, V2 <X, X, 4, 5>
4866 //
4867 if (!isShuffleMaskConsecutive(SVOp,
4868 NumZeros, // Mask Start Index
4869 NumElems-1, // Mask End Index
4870 0, // Where to start looking in the src vector
4871 NumElems, // Number of elements in vector
4872 OpSrc)) // Which source operand ?
4873 return false;
4874
4875 isLeft = true;
4876 ShAmt = NumZeros;
4877 ShVal = SVOp->getOperand(OpSrc);
4878 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004879}
4880
4881/// isVectorShift - Returns true if the shuffle can be implemented as a
4882/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004883static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004884 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004885 // Although the logic below support any bitwidth size, there are no
4886 // shift instructions which handle more than 128-bit vectors.
4887 if (SVOp->getValueType(0).getSizeInBits() > 128)
4888 return false;
4889
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004890 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4891 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4892 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004893
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004894 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004895}
4896
Evan Chengc78d3b42006-04-24 18:01:45 +00004897/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4898///
Dan Gohman475871a2008-07-27 21:46:04 +00004899static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004900 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004901 SelectionDAG &DAG,
4902 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004903 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004904 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004905
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004906 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004907 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004908 bool First = true;
4909 for (unsigned i = 0; i < 16; ++i) {
4910 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4911 if (ThisIsNonZero && First) {
4912 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004913 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004914 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004915 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004916 First = false;
4917 }
4918
4919 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004920 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004921 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4922 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004923 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004924 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004925 }
4926 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004927 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4928 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4929 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004930 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004931 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004932 } else
4933 ThisElt = LastElt;
4934
Gabor Greifba36cb52008-08-28 21:40:38 +00004935 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004936 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004937 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004938 }
4939 }
4940
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004941 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004942}
4943
Bill Wendlinga348c562007-03-22 18:42:45 +00004944/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004945///
Dan Gohman475871a2008-07-27 21:46:04 +00004946static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004947 unsigned NumNonZero, unsigned NumZero,
4948 SelectionDAG &DAG,
4949 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004950 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004951 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004952
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004953 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004954 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004955 bool First = true;
4956 for (unsigned i = 0; i < 8; ++i) {
4957 bool isNonZero = (NonZeros & (1 << i)) != 0;
4958 if (isNonZero) {
4959 if (First) {
4960 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004961 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004962 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004963 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004964 First = false;
4965 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004966 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004967 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004968 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004969 }
4970 }
4971
4972 return V;
4973}
4974
Evan Chengf26ffe92008-05-29 08:22:04 +00004975/// getVShift - Return a vector logical shift node.
4976///
Owen Andersone50ed302009-08-10 22:56:29 +00004977static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004978 unsigned NumBits, SelectionDAG &DAG,
4979 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004980 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004981 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004982 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004983 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4984 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004985 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004986 DAG.getConstant(NumBits,
4987 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004988}
4989
Dan Gohman475871a2008-07-27 21:46:04 +00004990SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004991X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004992 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004993
Evan Chengc3630942009-12-09 21:00:30 +00004994 // Check if the scalar load can be widened into a vector load. And if
4995 // the address is "base + cst" see if the cst can be "absorbed" into
4996 // the shuffle mask.
4997 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4998 SDValue Ptr = LD->getBasePtr();
4999 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
5000 return SDValue();
5001 EVT PVT = LD->getValueType(0);
5002 if (PVT != MVT::i32 && PVT != MVT::f32)
5003 return SDValue();
5004
5005 int FI = -1;
5006 int64_t Offset = 0;
5007 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
5008 FI = FINode->getIndex();
5009 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00005010 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00005011 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
5012 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
5013 Offset = Ptr.getConstantOperandVal(1);
5014 Ptr = Ptr.getOperand(0);
5015 } else {
5016 return SDValue();
5017 }
5018
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005019 // FIXME: 256-bit vector instructions don't require a strict alignment,
5020 // improve this code to support it better.
5021 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00005022 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005023 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00005024 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005025 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00005026 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00005027 // Can't change the alignment. FIXME: It's possible to compute
5028 // the exact stack offset and reference FI + adjust offset instead.
5029 // If someone *really* cares about this. That's the way to implement it.
5030 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005031 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005032 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00005033 }
5034 }
5035
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005036 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00005037 // Ptr + (Offset & ~15).
5038 if (Offset < 0)
5039 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005040 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00005041 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005042 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00005043 if (StartOffset)
5044 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
5045 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
5046
5047 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005048 int NumElems = VT.getVectorNumElements();
5049
5050 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
5051 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5052 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00005053 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005054 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005055
5056 // Canonicalize it to a v4i32 or v8i32 shuffle.
5057 SmallVector<int, 8> Mask;
5058 for (int i = 0; i < NumElems; ++i)
5059 Mask.push_back(EltNo);
5060
5061 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
5062 return DAG.getNode(ISD::BITCAST, dl, NVT,
5063 DAG.getVectorShuffle(CanonVT, dl, V1,
5064 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00005065 }
5066
5067 return SDValue();
5068}
5069
Michael J. Spencerec38de22010-10-10 22:04:20 +00005070/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5071/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005072/// load which has the same value as a build_vector whose operands are 'elts'.
5073///
5074/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005075///
Nate Begeman1449f292010-03-24 22:19:06 +00005076/// FIXME: we'd also like to handle the case where the last elements are zero
5077/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5078/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005079static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005080 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005081 EVT EltVT = VT.getVectorElementType();
5082 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005083
Nate Begemanfdea31a2010-03-24 20:49:50 +00005084 LoadSDNode *LDBase = NULL;
5085 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005086
Nate Begeman1449f292010-03-24 22:19:06 +00005087 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005088 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005089 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005090 for (unsigned i = 0; i < NumElems; ++i) {
5091 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005092
Nate Begemanfdea31a2010-03-24 20:49:50 +00005093 if (!Elt.getNode() ||
5094 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5095 return SDValue();
5096 if (!LDBase) {
5097 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5098 return SDValue();
5099 LDBase = cast<LoadSDNode>(Elt.getNode());
5100 LastLoadedElt = i;
5101 continue;
5102 }
5103 if (Elt.getOpcode() == ISD::UNDEF)
5104 continue;
5105
5106 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5107 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5108 return SDValue();
5109 LastLoadedElt = i;
5110 }
Nate Begeman1449f292010-03-24 22:19:06 +00005111
5112 // If we have found an entire vector of loads and undefs, then return a large
5113 // load of the entire vector width starting at the base pointer. If we found
5114 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005115 if (LastLoadedElt == NumElems - 1) {
5116 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005117 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005118 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005119 LDBase->isVolatile(), LDBase->isNonTemporal(),
5120 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005121 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005122 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005123 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005124 LDBase->isInvariant(), LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005125 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5126 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005127 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5128 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005129 SDValue ResNode =
5130 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5131 LDBase->getPointerInfo(),
5132 LDBase->getAlignment(),
5133 false/*isVolatile*/, true/*ReadMem*/,
5134 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005135 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005136 }
5137 return SDValue();
5138}
5139
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005140/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
5141/// a vbroadcast node. We support two patterns:
5142/// 1. A splat BUILD_VECTOR which uses a single scalar load.
5143/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
5144/// a scalar load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005145/// The scalar load node is returned when a pattern is found,
5146/// or SDValue() otherwise.
5147static SDValue isVectorBroadcast(SDValue &Op, bool hasAVX2) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005148 EVT VT = Op.getValueType();
5149 SDValue V = Op;
5150
5151 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
5152 V = V.getOperand(0);
5153
5154 //A suspected load to be broadcasted.
5155 SDValue Ld;
5156
5157 switch (V.getOpcode()) {
5158 default:
5159 // Unknown pattern found.
5160 return SDValue();
5161
5162 case ISD::BUILD_VECTOR: {
5163 // The BUILD_VECTOR node must be a splat.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005164 if (!isSplatVector(V.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005165 return SDValue();
5166
5167 Ld = V.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005168
5169 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005170 // of its users are from the BUILD_VECTOR node.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005171 if (!Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005172 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005173 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005174 }
5175
5176 case ISD::VECTOR_SHUFFLE: {
5177 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5178
5179 // Shuffles must have a splat mask where the first element is
5180 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005181 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005182 return SDValue();
5183
5184 SDValue Sc = Op.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005185 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005186 return SDValue();
5187
5188 Ld = Sc.getOperand(0);
5189
5190 // The scalar_to_vector node and the suspected
5191 // load node must have exactly one user.
5192 if (!Sc.hasOneUse() || !Ld.hasOneUse())
5193 return SDValue();
5194 break;
5195 }
5196 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005197
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005198 // The scalar source must be a normal load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005199 if (!ISD::isNormalLoad(Ld.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005200 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005201
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005202 bool Is256 = VT.getSizeInBits() == 256;
5203 bool Is128 = VT.getSizeInBits() == 128;
5204 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5205
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005206 if (hasAVX2) {
5207 // VBroadcast to YMM
5208 if (Is256 && (ScalarSize == 8 || ScalarSize == 16 ||
5209 ScalarSize == 32 || ScalarSize == 64 ))
5210 return Ld;
5211
5212 // VBroadcast to XMM
5213 if (Is128 && (ScalarSize == 8 || ScalarSize == 32 ||
5214 ScalarSize == 16 || ScalarSize == 64 ))
5215 return Ld;
5216 }
5217
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005218 // VBroadcast to YMM
5219 if (Is256 && (ScalarSize == 32 || ScalarSize == 64))
5220 return Ld;
5221
5222 // VBroadcast to XMM
5223 if (Is128 && (ScalarSize == 32))
5224 return Ld;
5225
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005226
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005227 // Unsupported broadcast.
5228 return SDValue();
5229}
5230
Evan Chengc3630942009-12-09 21:00:30 +00005231SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005232X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005233 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005234
David Greenef125a292011-02-08 19:04:41 +00005235 EVT VT = Op.getValueType();
5236 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005237 unsigned NumElems = Op.getNumOperands();
5238
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005239 // Vectors containing all zeros can be matched by pxor and xorps later
5240 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5241 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5242 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005243 if (Op.getValueType() == MVT::v4i32 ||
5244 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005245 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005246
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005247 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005248 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005249
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005250 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005251 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5252 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005253 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper745a86b2011-11-19 22:34:59 +00005254 if (Op.getValueType() == MVT::v4i32 ||
5255 (Op.getValueType() == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005256 return Op;
5257
Craig Topper745a86b2011-11-19 22:34:59 +00005258 return getOnesVector(Op.getValueType(), Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005259 }
5260
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005261 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005262 if (Subtarget->hasAVX() && LD.getNode())
5263 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
5264
Owen Andersone50ed302009-08-10 22:56:29 +00005265 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005266
Evan Cheng0db9fe62006-04-25 20:13:52 +00005267 unsigned NumZero = 0;
5268 unsigned NumNonZero = 0;
5269 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005270 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005271 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005272 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005273 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005274 if (Elt.getOpcode() == ISD::UNDEF)
5275 continue;
5276 Values.insert(Elt);
5277 if (Elt.getOpcode() != ISD::Constant &&
5278 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005279 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005280 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005281 NumZero++;
5282 else {
5283 NonZeros |= (1 << i);
5284 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005285 }
5286 }
5287
Chris Lattner97a2a562010-08-26 05:24:29 +00005288 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5289 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005290 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005291
Chris Lattner67f453a2008-03-09 05:42:06 +00005292 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005293 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005294 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005295 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005296
Chris Lattner62098042008-03-09 01:05:04 +00005297 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5298 // the value are obviously zero, truncate the value to i32 and do the
5299 // insertion that way. Only do this if the value is non-constant or if the
5300 // value is a constant being inserted into element 0. It is cheaper to do
5301 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005302 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005303 (!IsAllConstants || Idx == 0)) {
5304 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005305 // Handle SSE only.
5306 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5307 EVT VecVT = MVT::v4i32;
5308 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005309
Chris Lattner62098042008-03-09 01:05:04 +00005310 // Truncate the value (which may itself be a constant) to i32, and
5311 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005312 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005313 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005314 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005315 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005316
Chris Lattner62098042008-03-09 01:05:04 +00005317 // Now we have our 32-bit value zero extended in the low element of
5318 // a vector. If Idx != 0, swizzle it into place.
5319 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005320 SmallVector<int, 4> Mask;
5321 Mask.push_back(Idx);
5322 for (unsigned i = 1; i != VecElts; ++i)
5323 Mask.push_back(i);
5324 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005325 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005326 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005327 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005328 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005329 }
5330 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005331
Chris Lattner19f79692008-03-08 22:59:52 +00005332 // If we have a constant or non-constant insertion into the low element of
5333 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5334 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005335 // depending on what the source datatype is.
5336 if (Idx == 0) {
5337 if (NumZero == 0) {
5338 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005339 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5340 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005341 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5342 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005343 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005344 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005345 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5346 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005347 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5348 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005349 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5350 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005351 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005352 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005353 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005354 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005355
5356 // Is it a vector logical left shift?
5357 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005358 X86::isZeroNode(Op.getOperand(0)) &&
5359 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005360 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005361 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005362 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005363 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005364 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005365 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005366
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005367 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005368 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005369
Chris Lattner19f79692008-03-08 22:59:52 +00005370 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5371 // is a non-constant being inserted into an element other than the low one,
5372 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5373 // movd/movss) to move this into the low element, then shuffle it into
5374 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005375 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005376 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005377
Evan Cheng0db9fe62006-04-25 20:13:52 +00005378 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005379 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005380 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005381 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005382 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005383 MaskVec.push_back(i == Idx ? 0 : 1);
5384 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005385 }
5386 }
5387
Chris Lattner67f453a2008-03-09 05:42:06 +00005388 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005389 if (Values.size() == 1) {
5390 if (EVTBits == 32) {
5391 // Instead of a shuffle like this:
5392 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5393 // Check if it's possible to issue this instead.
5394 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5395 unsigned Idx = CountTrailingZeros_32(NonZeros);
5396 SDValue Item = Op.getOperand(Idx);
5397 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5398 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5399 }
Dan Gohman475871a2008-07-27 21:46:04 +00005400 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005401 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005402
Dan Gohmana3941172007-07-24 22:55:08 +00005403 // A vector full of immediates; various special cases are already
5404 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005405 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005406 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005407
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005408 // For AVX-length vectors, build the individual 128-bit pieces and use
5409 // shuffles to put them in place.
5410 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5411 SmallVector<SDValue, 32> V;
5412 for (unsigned i = 0; i < NumElems; ++i)
5413 V.push_back(Op.getOperand(i));
5414
5415 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5416
5417 // Build both the lower and upper subvector.
5418 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5419 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5420 NumElems/2);
5421
5422 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005423 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5424 DAG.getConstant(0, MVT::i32), DAG, dl);
5425 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005426 DAG, dl);
5427 }
5428
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005429 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005430 if (EVTBits == 64) {
5431 if (NumNonZero == 1) {
5432 // One half is zero or undef.
5433 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005434 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005435 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005436 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005437 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005438 }
Dan Gohman475871a2008-07-27 21:46:04 +00005439 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005440 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005441
5442 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005443 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005444 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005445 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005446 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005447 }
5448
Bill Wendling826f36f2007-03-28 00:57:11 +00005449 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005450 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24: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
5455 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005456 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005457 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005458 if (NumElems == 4 && NumZero > 0) {
5459 for (unsigned i = 0; i < 4; ++i) {
5460 bool isZero = !(NonZeros & (1 << i));
5461 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005462 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005463 else
Dale Johannesenace16102009-02-03 19:33:06 +00005464 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005465 }
5466
5467 for (unsigned i = 0; i < 2; ++i) {
5468 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5469 default: break;
5470 case 0:
5471 V[i] = V[i*2]; // Must be a zero vector.
5472 break;
5473 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005474 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005475 break;
5476 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005477 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005478 break;
5479 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005480 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005481 break;
5482 }
5483 }
5484
Nate Begeman9008ca62009-04-27 18:41:29 +00005485 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005486 bool Reverse = (NonZeros & 0x3) == 2;
5487 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005488 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005489 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5490 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005491 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5492 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005493 }
5494
Nate Begemanfdea31a2010-03-24 20:49:50 +00005495 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5496 // Check for a build vector of consecutive loads.
5497 for (unsigned i = 0; i < NumElems; ++i)
5498 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005499
Nate Begemanfdea31a2010-03-24 20:49:50 +00005500 // Check for elements which are consecutive loads.
5501 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5502 if (LD.getNode())
5503 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005504
5505 // For SSE 4.1, use insertps to put the high elements into the low element.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005506 if (getSubtarget()->hasSSE41() || getSubtarget()->hasAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005507 SDValue Result;
5508 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5509 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5510 else
5511 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005512
Chris Lattner24faf612010-08-28 17:59:08 +00005513 for (unsigned i = 1; i < NumElems; ++i) {
5514 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5515 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005516 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005517 }
5518 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005519 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005520
Chris Lattner6e80e442010-08-28 17:15:43 +00005521 // Otherwise, expand into a number of unpckl*, start by extending each of
5522 // our (non-undef) elements to the full vector width with the element in the
5523 // bottom slot of the vector (which generates no code for SSE).
5524 for (unsigned i = 0; i < NumElems; ++i) {
5525 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5526 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5527 else
5528 V[i] = DAG.getUNDEF(VT);
5529 }
5530
5531 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005532 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5533 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5534 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005535 unsigned EltStride = NumElems >> 1;
5536 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005537 for (unsigned i = 0; i < EltStride; ++i) {
5538 // If V[i+EltStride] is undef and this is the first round of mixing,
5539 // then it is safe to just drop this shuffle: V[i] is already in the
5540 // right place, the one element (since it's the first round) being
5541 // inserted as undef can be dropped. This isn't safe for successive
5542 // rounds because they will permute elements within both vectors.
5543 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5544 EltStride == NumElems/2)
5545 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005546
Chris Lattner6e80e442010-08-28 17:15:43 +00005547 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005548 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005549 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005550 }
5551 return V[0];
5552 }
Dan Gohman475871a2008-07-27 21:46:04 +00005553 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005554}
5555
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005556// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5557// them in a MMX register. This is better than doing a stack convert.
5558static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005559 DebugLoc dl = Op.getDebugLoc();
5560 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005561
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005562 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5563 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5564 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005565 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005566 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5567 InVec = Op.getOperand(1);
5568 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5569 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005570 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005571 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5572 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5573 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005574 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005575 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5576 Mask[0] = 0; Mask[1] = 2;
5577 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5578 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005579 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005580}
5581
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005582// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5583// to create 256-bit vectors from two other 128-bit ones.
5584static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5585 DebugLoc dl = Op.getDebugLoc();
5586 EVT ResVT = Op.getValueType();
5587
5588 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5589
5590 SDValue V1 = Op.getOperand(0);
5591 SDValue V2 = Op.getOperand(1);
5592 unsigned NumElems = ResVT.getVectorNumElements();
5593
5594 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5595 DAG.getConstant(0, MVT::i32), DAG, dl);
5596 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5597 DAG, dl);
5598}
5599
5600SDValue
5601X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005602 EVT ResVT = Op.getValueType();
5603
5604 assert(Op.getNumOperands() == 2);
5605 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5606 "Unsupported CONCAT_VECTORS for value type");
5607
5608 // We support concatenate two MMX registers and place them in a MMX register.
5609 // This is better than doing a stack convert.
5610 if (ResVT.is128BitVector())
5611 return LowerMMXCONCAT_VECTORS(Op, DAG);
5612
5613 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5614 // from two other 128-bit ones.
5615 return LowerAVXCONCAT_VECTORS(Op, DAG);
5616}
5617
Nate Begemanb9a47b82009-02-23 08:49:38 +00005618// v8i16 shuffles - Prefer shuffles in the following order:
5619// 1. [all] pshuflw, pshufhw, optional move
5620// 2. [ssse3] 1 x pshufb
5621// 3. [ssse3] 2 x pshufb + 1 x por
5622// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005623SDValue
5624X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5625 SelectionDAG &DAG) const {
5626 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005627 SDValue V1 = SVOp->getOperand(0);
5628 SDValue V2 = SVOp->getOperand(1);
5629 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005630 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005631
Nate Begemanb9a47b82009-02-23 08:49:38 +00005632 // Determine if more than 1 of the words in each of the low and high quadwords
5633 // of the result come from the same quadword of one of the two inputs. Undef
5634 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005635 unsigned LoQuad[] = { 0, 0, 0, 0 };
5636 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005637 BitVector InputQuads(4);
5638 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005639 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005640 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005641 MaskVals.push_back(EltIdx);
5642 if (EltIdx < 0) {
5643 ++Quad[0];
5644 ++Quad[1];
5645 ++Quad[2];
5646 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005647 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005648 }
5649 ++Quad[EltIdx / 4];
5650 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005651 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005652
Nate Begemanb9a47b82009-02-23 08:49:38 +00005653 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005654 unsigned MaxQuad = 1;
5655 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005656 if (LoQuad[i] > MaxQuad) {
5657 BestLoQuad = i;
5658 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005659 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005660 }
5661
Nate Begemanb9a47b82009-02-23 08:49:38 +00005662 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005663 MaxQuad = 1;
5664 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005665 if (HiQuad[i] > MaxQuad) {
5666 BestHiQuad = i;
5667 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005668 }
5669 }
5670
Nate Begemanb9a47b82009-02-23 08:49:38 +00005671 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005672 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005673 // single pshufb instruction is necessary. If There are more than 2 input
5674 // quads, disable the next transformation since it does not help SSSE3.
5675 bool V1Used = InputQuads[0] || InputQuads[1];
5676 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005677 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005678 if (InputQuads.count() == 2 && V1Used && V2Used) {
5679 BestLoQuad = InputQuads.find_first();
5680 BestHiQuad = InputQuads.find_next(BestLoQuad);
5681 }
5682 if (InputQuads.count() > 2) {
5683 BestLoQuad = -1;
5684 BestHiQuad = -1;
5685 }
5686 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005687
Nate Begemanb9a47b82009-02-23 08:49:38 +00005688 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5689 // the shuffle mask. If a quad is scored as -1, that means that it contains
5690 // words from all 4 input quadwords.
5691 SDValue NewV;
5692 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005693 SmallVector<int, 8> MaskV;
5694 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5695 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005696 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005697 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5698 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5699 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005700
Nate Begemanb9a47b82009-02-23 08:49:38 +00005701 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5702 // source words for the shuffle, to aid later transformations.
5703 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005704 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005705 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005706 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005707 if (idx != (int)i)
5708 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005709 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005710 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005711 AllWordsInNewV = false;
5712 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005713 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005714
Nate Begemanb9a47b82009-02-23 08:49:38 +00005715 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5716 if (AllWordsInNewV) {
5717 for (int i = 0; i != 8; ++i) {
5718 int idx = MaskVals[i];
5719 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005720 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005721 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 if ((idx != i) && idx < 4)
5723 pshufhw = false;
5724 if ((idx != i) && idx > 3)
5725 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005726 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005727 V1 = NewV;
5728 V2Used = false;
5729 BestLoQuad = 0;
5730 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005731 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005732
Nate Begemanb9a47b82009-02-23 08:49:38 +00005733 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5734 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005735 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005736 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5737 unsigned TargetMask = 0;
5738 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005739 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005740 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5741 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5742 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005743 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005744 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005745 }
Eric Christopherfd179292009-08-27 18:07:15 +00005746
Nate Begemanb9a47b82009-02-23 08:49:38 +00005747 // If we have SSSE3, and all words of the result are from 1 input vector,
5748 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5749 // is present, fall back to case 4.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005750 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005751 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005752
Nate Begemanb9a47b82009-02-23 08:49:38 +00005753 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005754 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005755 // mask, and elements that come from V1 in the V2 mask, so that the two
5756 // results can be OR'd together.
5757 bool TwoInputs = V1Used && V2Used;
5758 for (unsigned i = 0; i != 8; ++i) {
5759 int EltIdx = MaskVals[i] * 2;
5760 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005761 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5762 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005763 continue;
5764 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005765 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5766 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005767 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005768 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005769 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005770 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005771 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005772 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005773 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005774
Nate Begemanb9a47b82009-02-23 08:49:38 +00005775 // Calculate the shuffle mask for the second input, shuffle it, and
5776 // OR it with the first shuffled input.
5777 pshufbMask.clear();
5778 for (unsigned i = 0; i != 8; ++i) {
5779 int EltIdx = MaskVals[i] * 2;
5780 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005781 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5782 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005783 continue;
5784 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005785 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5786 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005787 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005788 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005789 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005790 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005791 MVT::v16i8, &pshufbMask[0], 16));
5792 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005793 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005794 }
5795
5796 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5797 // and update MaskVals with new element order.
5798 BitVector InOrder(8);
5799 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005800 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005801 for (int i = 0; i != 4; ++i) {
5802 int idx = MaskVals[i];
5803 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005804 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005805 InOrder.set(i);
5806 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005807 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005808 InOrder.set(i);
5809 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005810 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005811 }
5812 }
5813 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005814 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005815 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005816 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005817
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005818 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5819 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005820 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5821 NewV.getOperand(0),
5822 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5823 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005824 }
Eric Christopherfd179292009-08-27 18:07:15 +00005825
Nate Begemanb9a47b82009-02-23 08:49:38 +00005826 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5827 // and update MaskVals with the new element order.
5828 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005829 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005830 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005831 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005832 for (unsigned i = 4; i != 8; ++i) {
5833 int idx = MaskVals[i];
5834 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005835 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005836 InOrder.set(i);
5837 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005838 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005839 InOrder.set(i);
5840 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005841 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005842 }
5843 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005844 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005845 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005846
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005847 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5848 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005849 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5850 NewV.getOperand(0),
5851 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5852 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005853 }
Eric Christopherfd179292009-08-27 18:07:15 +00005854
Nate Begemanb9a47b82009-02-23 08:49:38 +00005855 // In case BestHi & BestLo were both -1, which means each quadword has a word
5856 // from each of the four input quadwords, calculate the InOrder bitvector now
5857 // before falling through to the insert/extract cleanup.
5858 if (BestLoQuad == -1 && BestHiQuad == -1) {
5859 NewV = V1;
5860 for (int i = 0; i != 8; ++i)
5861 if (MaskVals[i] < 0 || MaskVals[i] == i)
5862 InOrder.set(i);
5863 }
Eric Christopherfd179292009-08-27 18:07:15 +00005864
Nate Begemanb9a47b82009-02-23 08:49:38 +00005865 // The other elements are put in the right place using pextrw and pinsrw.
5866 for (unsigned i = 0; i != 8; ++i) {
5867 if (InOrder[i])
5868 continue;
5869 int EltIdx = MaskVals[i];
5870 if (EltIdx < 0)
5871 continue;
5872 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005873 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005874 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005875 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005876 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005877 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005878 DAG.getIntPtrConstant(i));
5879 }
5880 return NewV;
5881}
5882
5883// v16i8 shuffles - Prefer shuffles in the following order:
5884// 1. [ssse3] 1 x pshufb
5885// 2. [ssse3] 2 x pshufb + 1 x por
5886// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5887static
Nate Begeman9008ca62009-04-27 18:41:29 +00005888SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005889 SelectionDAG &DAG,
5890 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005891 SDValue V1 = SVOp->getOperand(0);
5892 SDValue V2 = SVOp->getOperand(1);
5893 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005894 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005895 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005896
Nate Begemanb9a47b82009-02-23 08:49:38 +00005897 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005898 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005899 // present, fall back to case 3.
5900 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5901 bool V1Only = true;
5902 bool V2Only = true;
5903 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005904 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005905 if (EltIdx < 0)
5906 continue;
5907 if (EltIdx < 16)
5908 V2Only = false;
5909 else
5910 V1Only = false;
5911 }
Eric Christopherfd179292009-08-27 18:07:15 +00005912
Nate Begemanb9a47b82009-02-23 08:49:38 +00005913 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005914 if (TLI.getSubtarget()->hasSSSE3() || TLI.getSubtarget()->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005915 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005916
Nate Begemanb9a47b82009-02-23 08:49:38 +00005917 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005918 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005919 //
5920 // Otherwise, we have elements from both input vectors, and must zero out
5921 // elements that come from V2 in the first mask, and V1 in the second mask
5922 // so that we can OR them together.
5923 bool TwoInputs = !(V1Only || V2Only);
5924 for (unsigned i = 0; i != 16; ++i) {
5925 int EltIdx = MaskVals[i];
5926 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005927 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005928 continue;
5929 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005930 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005931 }
5932 // If all the elements are from V2, assign it to V1 and return after
5933 // building the first pshufb.
5934 if (V2Only)
5935 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005936 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005937 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005938 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005939 if (!TwoInputs)
5940 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005941
Nate Begemanb9a47b82009-02-23 08:49:38 +00005942 // Calculate the shuffle mask for the second input, shuffle it, and
5943 // OR it with the first shuffled input.
5944 pshufbMask.clear();
5945 for (unsigned i = 0; i != 16; ++i) {
5946 int EltIdx = MaskVals[i];
5947 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005948 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005949 continue;
5950 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005951 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005952 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005953 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005954 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005955 MVT::v16i8, &pshufbMask[0], 16));
5956 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005957 }
Eric Christopherfd179292009-08-27 18:07:15 +00005958
Nate Begemanb9a47b82009-02-23 08:49:38 +00005959 // No SSSE3 - Calculate in place words and then fix all out of place words
5960 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5961 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005962 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5963 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005964 SDValue NewV = V2Only ? V2 : V1;
5965 for (int i = 0; i != 8; ++i) {
5966 int Elt0 = MaskVals[i*2];
5967 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005968
Nate Begemanb9a47b82009-02-23 08:49:38 +00005969 // This word of the result is all undef, skip it.
5970 if (Elt0 < 0 && Elt1 < 0)
5971 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005972
Nate Begemanb9a47b82009-02-23 08:49:38 +00005973 // This word of the result is already in the correct place, skip it.
5974 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5975 continue;
5976 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5977 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005978
Nate Begemanb9a47b82009-02-23 08:49:38 +00005979 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5980 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5981 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005982
5983 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5984 // using a single extract together, load it and store it.
5985 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005986 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005987 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005988 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005989 DAG.getIntPtrConstant(i));
5990 continue;
5991 }
5992
Nate Begemanb9a47b82009-02-23 08:49:38 +00005993 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005994 // source byte is not also odd, shift the extracted word left 8 bits
5995 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005996 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005997 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005998 DAG.getIntPtrConstant(Elt1 / 2));
5999 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006000 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006001 DAG.getConstant(8,
6002 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006003 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006004 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6005 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006006 }
6007 // If Elt0 is defined, extract it from the appropriate source. If the
6008 // source byte is not also even, shift the extracted word right 8 bits. If
6009 // Elt1 was also defined, OR the extracted values together before
6010 // inserting them in the result.
6011 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006012 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006013 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6014 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006015 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006016 DAG.getConstant(8,
6017 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006018 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006019 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6020 DAG.getConstant(0x00FF, MVT::i16));
6021 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006022 : InsElt0;
6023 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006024 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006025 DAG.getIntPtrConstant(i));
6026 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006027 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006028}
6029
Evan Cheng7a831ce2007-12-15 03:00:47 +00006030/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006031/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006032/// done when every pair / quad of shuffle mask elements point to elements in
6033/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006034/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006035static
Nate Begeman9008ca62009-04-27 18:41:29 +00006036SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006037 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00006038 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00006039 SDValue V1 = SVOp->getOperand(0);
6040 SDValue V2 = SVOp->getOperand(1);
6041 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00006042 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006043 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00006044 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006045 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006046 case MVT::v4f32: NewVT = MVT::v2f64; break;
6047 case MVT::v4i32: NewVT = MVT::v2i64; break;
6048 case MVT::v8i16: NewVT = MVT::v4i32; break;
6049 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006050 }
6051
Nate Begeman9008ca62009-04-27 18:41:29 +00006052 int Scale = NumElems / NewWidth;
6053 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00006054 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006055 int StartIdx = -1;
6056 for (int j = 0; j < Scale; ++j) {
6057 int EltIdx = SVOp->getMaskElt(i+j);
6058 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006059 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00006060 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00006061 StartIdx = EltIdx - (EltIdx % Scale);
6062 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00006063 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006064 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006065 if (StartIdx == -1)
6066 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00006067 else
Nate Begeman9008ca62009-04-27 18:41:29 +00006068 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006069 }
6070
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006071 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
6072 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00006073 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006074}
6075
Evan Chengd880b972008-05-09 21:53:03 +00006076/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006077///
Owen Andersone50ed302009-08-10 22:56:29 +00006078static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006079 SDValue SrcOp, SelectionDAG &DAG,
6080 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006081 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006082 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006083 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006084 LD = dyn_cast<LoadSDNode>(SrcOp);
6085 if (!LD) {
6086 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6087 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006088 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006089 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006090 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006091 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006092 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006093 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006094 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006095 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006096 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6097 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6098 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006099 SrcOp.getOperand(0)
6100 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006101 }
6102 }
6103 }
6104
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006105 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006106 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006107 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006108 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006109}
6110
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006111/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
6112/// shuffle node referes to only one lane in the sources.
6113static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
6114 EVT VT = SVOp->getValueType(0);
6115 int NumElems = VT.getVectorNumElements();
6116 int HalfSize = NumElems/2;
6117 SmallVector<int, 16> M;
6118 SVOp->getMask(M);
6119 bool MatchA = false, MatchB = false;
6120
6121 for (int l = 0; l < NumElems*2; l += HalfSize) {
6122 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
6123 MatchA = true;
6124 break;
6125 }
6126 }
6127
6128 for (int l = 0; l < NumElems*2; l += HalfSize) {
6129 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
6130 MatchB = true;
6131 break;
6132 }
6133 }
6134
6135 return MatchA && MatchB;
6136}
6137
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006138/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6139/// which could not be matched by any known target speficic shuffle
6140static SDValue
6141LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006142 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
6143 // If each half of a vector shuffle node referes to only one lane in the
6144 // source vectors, extract each used 128-bit lane and shuffle them using
6145 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
6146 // the work to the legalizer.
6147 DebugLoc dl = SVOp->getDebugLoc();
6148 EVT VT = SVOp->getValueType(0);
6149 int NumElems = VT.getVectorNumElements();
6150 int HalfSize = NumElems/2;
6151
6152 // Extract the reference for each half
6153 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
6154 int FstVecOpNum = 0, SndVecOpNum = 0;
6155 for (int i = 0; i < HalfSize; ++i) {
6156 int Elt = SVOp->getMaskElt(i);
6157 if (SVOp->getMaskElt(i) < 0)
6158 continue;
6159 FstVecOpNum = Elt/NumElems;
6160 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6161 break;
6162 }
6163 for (int i = HalfSize; i < NumElems; ++i) {
6164 int Elt = SVOp->getMaskElt(i);
6165 if (SVOp->getMaskElt(i) < 0)
6166 continue;
6167 SndVecOpNum = Elt/NumElems;
6168 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6169 break;
6170 }
6171
6172 // Extract the subvectors
6173 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
6174 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
6175 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
6176 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
6177
6178 // Generate 128-bit shuffles
6179 SmallVector<int, 16> MaskV1, MaskV2;
6180 for (int i = 0; i < HalfSize; ++i) {
6181 int Elt = SVOp->getMaskElt(i);
6182 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6183 }
6184 for (int i = HalfSize; i < NumElems; ++i) {
6185 int Elt = SVOp->getMaskElt(i);
6186 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6187 }
6188
6189 EVT NVT = V1.getValueType();
6190 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
6191 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
6192
6193 // Concatenate the result back
6194 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
6195 DAG.getConstant(0, MVT::i32), DAG, dl);
6196 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
6197 DAG, dl);
6198 }
6199
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006200 return SDValue();
6201}
6202
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006203/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6204/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006205static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006206LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006207 SDValue V1 = SVOp->getOperand(0);
6208 SDValue V2 = SVOp->getOperand(1);
6209 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006210 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006211
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006212 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6213
Evan Chengace3c172008-07-22 21:13:36 +00006214 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006215 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006216 SmallVector<int, 8> Mask1(4U, -1);
6217 SmallVector<int, 8> PermMask;
6218 SVOp->getMask(PermMask);
6219
Evan Chengace3c172008-07-22 21:13:36 +00006220 unsigned NumHi = 0;
6221 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006222 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006223 int Idx = PermMask[i];
6224 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006225 Locs[i] = std::make_pair(-1, -1);
6226 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006227 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6228 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006229 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006230 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006231 NumLo++;
6232 } else {
6233 Locs[i] = std::make_pair(1, NumHi);
6234 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006235 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006236 NumHi++;
6237 }
6238 }
6239 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006240
Evan Chengace3c172008-07-22 21:13:36 +00006241 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006242 // If no more than two elements come from either vector. This can be
6243 // implemented with two shuffles. First shuffle gather the elements.
6244 // The second shuffle, which takes the first shuffle as both of its
6245 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006246 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006247
Nate Begeman9008ca62009-04-27 18:41:29 +00006248 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006249
Evan Chengace3c172008-07-22 21:13:36 +00006250 for (unsigned i = 0; i != 4; ++i) {
6251 if (Locs[i].first == -1)
6252 continue;
6253 else {
6254 unsigned Idx = (i < 2) ? 0 : 4;
6255 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006256 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006257 }
6258 }
6259
Nate Begeman9008ca62009-04-27 18:41:29 +00006260 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006261 } else if (NumLo == 3 || NumHi == 3) {
6262 // Otherwise, we must have three elements from one vector, call it X, and
6263 // one element from the other, call it Y. First, use a shufps to build an
6264 // intermediate vector with the one element from Y and the element from X
6265 // that will be in the same half in the final destination (the indexes don't
6266 // matter). Then, use a shufps to build the final vector, taking the half
6267 // containing the element from Y from the intermediate, and the other half
6268 // from X.
6269 if (NumHi == 3) {
6270 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006271 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006272 std::swap(V1, V2);
6273 }
6274
6275 // Find the element from V2.
6276 unsigned HiIndex;
6277 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006278 int Val = PermMask[HiIndex];
6279 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006280 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006281 if (Val >= 4)
6282 break;
6283 }
6284
Nate Begeman9008ca62009-04-27 18:41:29 +00006285 Mask1[0] = PermMask[HiIndex];
6286 Mask1[1] = -1;
6287 Mask1[2] = PermMask[HiIndex^1];
6288 Mask1[3] = -1;
6289 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006290
6291 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006292 Mask1[0] = PermMask[0];
6293 Mask1[1] = PermMask[1];
6294 Mask1[2] = HiIndex & 1 ? 6 : 4;
6295 Mask1[3] = HiIndex & 1 ? 4 : 6;
6296 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006297 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006298 Mask1[0] = HiIndex & 1 ? 2 : 0;
6299 Mask1[1] = HiIndex & 1 ? 0 : 2;
6300 Mask1[2] = PermMask[2];
6301 Mask1[3] = PermMask[3];
6302 if (Mask1[2] >= 0)
6303 Mask1[2] += 4;
6304 if (Mask1[3] >= 0)
6305 Mask1[3] += 4;
6306 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006307 }
Evan Chengace3c172008-07-22 21:13:36 +00006308 }
6309
6310 // Break it into (shuffle shuffle_hi, shuffle_lo).
6311 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006312 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006313 SmallVector<int,8> LoMask(4U, -1);
6314 SmallVector<int,8> HiMask(4U, -1);
6315
6316 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006317 unsigned MaskIdx = 0;
6318 unsigned LoIdx = 0;
6319 unsigned HiIdx = 2;
6320 for (unsigned i = 0; i != 4; ++i) {
6321 if (i == 2) {
6322 MaskPtr = &HiMask;
6323 MaskIdx = 1;
6324 LoIdx = 0;
6325 HiIdx = 2;
6326 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006327 int Idx = PermMask[i];
6328 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006329 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006330 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006331 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006332 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006333 LoIdx++;
6334 } else {
6335 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006336 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006337 HiIdx++;
6338 }
6339 }
6340
Nate Begeman9008ca62009-04-27 18:41:29 +00006341 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6342 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6343 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006344 for (unsigned i = 0; i != 4; ++i) {
6345 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006346 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006347 } else {
6348 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006349 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006350 }
6351 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006352 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006353}
6354
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006355static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006356 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006357 V = V.getOperand(0);
6358 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6359 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006360 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6361 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6362 // BUILD_VECTOR (load), undef
6363 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006364 if (MayFoldLoad(V))
6365 return true;
6366 return false;
6367}
6368
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006369// FIXME: the version above should always be used. Since there's
6370// a bug where several vector shuffles can't be folded because the
6371// DAG is not updated during lowering and a node claims to have two
6372// uses while it only has one, use this version, and let isel match
6373// another instruction if the load really happens to have more than
6374// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006375// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006376static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006377 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006378 V = V.getOperand(0);
6379 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6380 V = V.getOperand(0);
6381 if (ISD::isNormalLoad(V.getNode()))
6382 return true;
6383 return false;
6384}
6385
6386/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6387/// a vector extract, and if both can be later optimized into a single load.
6388/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6389/// here because otherwise a target specific shuffle node is going to be
6390/// emitted for this shuffle, and the optimization not done.
6391/// FIXME: This is probably not the best approach, but fix the problem
6392/// until the right path is decided.
6393static
6394bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6395 const TargetLowering &TLI) {
6396 EVT VT = V.getValueType();
6397 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6398
6399 // Be sure that the vector shuffle is present in a pattern like this:
6400 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6401 if (!V.hasOneUse())
6402 return false;
6403
6404 SDNode *N = *V.getNode()->use_begin();
6405 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6406 return false;
6407
6408 SDValue EltNo = N->getOperand(1);
6409 if (!isa<ConstantSDNode>(EltNo))
6410 return false;
6411
6412 // If the bit convert changed the number of elements, it is unsafe
6413 // to examine the mask.
6414 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006415 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006416 EVT SrcVT = V.getOperand(0).getValueType();
6417 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6418 return false;
6419 V = V.getOperand(0);
6420 HasShuffleIntoBitcast = true;
6421 }
6422
6423 // Select the input vector, guarding against out of range extract vector.
6424 unsigned NumElems = VT.getVectorNumElements();
6425 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6426 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6427 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6428
6429 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006430 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006431 V = V.getOperand(0);
6432
6433 if (ISD::isNormalLoad(V.getNode())) {
6434 // Is the original load suitable?
6435 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6436
6437 // FIXME: avoid the multi-use bug that is preventing lots of
6438 // of foldings to be detected, this is still wrong of course, but
6439 // give the temporary desired behavior, and if it happens that
6440 // the load has real more uses, during isel it will not fold, and
6441 // will generate poor code.
6442 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6443 return false;
6444
6445 if (!HasShuffleIntoBitcast)
6446 return true;
6447
6448 // If there's a bitcast before the shuffle, check if the load type and
6449 // alignment is valid.
6450 unsigned Align = LN0->getAlignment();
6451 unsigned NewAlign =
6452 TLI.getTargetData()->getABITypeAlignment(
6453 VT.getTypeForEVT(*DAG.getContext()));
6454
6455 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6456 return false;
6457 }
6458
6459 return true;
6460}
6461
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006462static
Evan Cheng835580f2010-10-07 20:50:20 +00006463SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6464 EVT VT = Op.getValueType();
6465
6466 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006467 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6468 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006469 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6470 V1, DAG));
6471}
6472
6473static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006474SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006475 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006476 SDValue V1 = Op.getOperand(0);
6477 SDValue V2 = Op.getOperand(1);
6478 EVT VT = Op.getValueType();
6479
6480 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6481
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006482 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006483 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6484
Evan Cheng0899f5c2011-08-31 02:05:24 +00006485 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6486 return DAG.getNode(ISD::BITCAST, dl, VT,
6487 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6488 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6489 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006490}
6491
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006492static
6493SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6494 SDValue V1 = Op.getOperand(0);
6495 SDValue V2 = Op.getOperand(1);
6496 EVT VT = Op.getValueType();
6497
6498 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6499 "unsupported shuffle type");
6500
6501 if (V2.getOpcode() == ISD::UNDEF)
6502 V2 = V1;
6503
6504 // v4i32 or v4f32
6505 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6506}
6507
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006508static inline unsigned getSHUFPOpcode(EVT VT) {
6509 switch(VT.getSimpleVT().SimpleTy) {
6510 case MVT::v8i32: // Use fp unit for int unpack.
6511 case MVT::v8f32:
6512 case MVT::v4i32: // Use fp unit for int unpack.
6513 case MVT::v4f32: return X86ISD::SHUFPS;
6514 case MVT::v4i64: // Use fp unit for int unpack.
6515 case MVT::v4f64:
6516 case MVT::v2i64: // Use fp unit for int unpack.
6517 case MVT::v2f64: return X86ISD::SHUFPD;
6518 default:
6519 llvm_unreachable("Unknown type for shufp*");
6520 }
6521 return 0;
6522}
6523
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006524static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006525SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006526 SDValue V1 = Op.getOperand(0);
6527 SDValue V2 = Op.getOperand(1);
6528 EVT VT = Op.getValueType();
6529 unsigned NumElems = VT.getVectorNumElements();
6530
6531 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6532 // operand of these instructions is only memory, so check if there's a
6533 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6534 // same masks.
6535 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006536
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006537 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006538 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006539 CanFoldLoad = true;
6540
6541 // When V1 is a load, it can be folded later into a store in isel, example:
6542 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6543 // turns into:
6544 // (MOVLPSmr addr:$src1, VR128:$src2)
6545 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006546 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006547 CanFoldLoad = true;
6548
Dan Gohman65fd6562011-11-03 21:49:52 +00006549 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006550 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006551 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006552 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6553
6554 if (NumElems == 4)
Dan Gohman65fd6562011-11-03 21:49:52 +00006555 // If we don't care about the second element, procede to use movss.
6556 if (SVOp->getMaskElt(1) != -1)
6557 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006558 }
6559
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006560 // movl and movlp will both match v2i64, but v2i64 is never matched by
6561 // movl earlier because we make it strict to avoid messing with the movlp load
6562 // folding logic (see the code above getMOVLP call). Match it here then,
6563 // this is horrible, but will stay like this until we move all shuffle
6564 // matching to x86 specific nodes. Note that for the 1st condition all
6565 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006566 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006567 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6568 // as to remove this logic from here, as much as possible
6569 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006570 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006571 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006572 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006573
6574 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6575
6576 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006577 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006578 X86::getShuffleSHUFImmediate(SVOp), DAG);
6579}
6580
Craig Topper6347e862011-11-21 06:57:39 +00006581static inline unsigned getUNPCKLOpcode(EVT VT, bool HasAVX2) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006582 switch(VT.getSimpleVT().SimpleTy) {
6583 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6584 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006585 case MVT::v4f32: return X86ISD::UNPCKLPS;
6586 case MVT::v2f64: return X86ISD::UNPCKLPD;
Craig Topper6347e862011-11-21 06:57:39 +00006587 case MVT::v8i32:
6588 if (HasAVX2) return X86ISD::VPUNPCKLDQY;
6589 // else use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006590 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Craig Topper6347e862011-11-21 06:57:39 +00006591 case MVT::v4i64:
6592 if (HasAVX2) return X86ISD::VPUNPCKLQDQY;
6593 // else use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006594 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006595 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6596 case MVT::v8i16: return X86ISD::PUNPCKLWD;
Craig Topper6347e862011-11-21 06:57:39 +00006597 case MVT::v16i16: return X86ISD::VPUNPCKLWDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006598 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006599 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006600 }
6601 return 0;
6602}
6603
Craig Topper6347e862011-11-21 06:57:39 +00006604static inline unsigned getUNPCKHOpcode(EVT VT, bool HasAVX2) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006605 switch(VT.getSimpleVT().SimpleTy) {
6606 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6607 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6608 case MVT::v4f32: return X86ISD::UNPCKHPS;
6609 case MVT::v2f64: return X86ISD::UNPCKHPD;
Craig Topper6347e862011-11-21 06:57:39 +00006610 case MVT::v8i32:
6611 if (HasAVX2) return X86ISD::VPUNPCKHDQY;
6612 // else use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006613 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Craig Topper6347e862011-11-21 06:57:39 +00006614 case MVT::v4i64:
6615 if (HasAVX2) return X86ISD::VPUNPCKHQDQY;
6616 // else use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006617 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006618 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6619 case MVT::v8i16: return X86ISD::PUNPCKHWD;
Craig Topper6347e862011-11-21 06:57:39 +00006620 case MVT::v16i16: return X86ISD::VPUNPCKHWDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006621 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006622 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006623 }
6624 return 0;
6625}
6626
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006627static inline unsigned getVPERMILOpcode(EVT VT) {
6628 switch(VT.getSimpleVT().SimpleTy) {
6629 case MVT::v4i32:
6630 case MVT::v4f32: return X86ISD::VPERMILPS;
6631 case MVT::v2i64:
6632 case MVT::v2f64: return X86ISD::VPERMILPD;
6633 case MVT::v8i32:
6634 case MVT::v8f32: return X86ISD::VPERMILPSY;
6635 case MVT::v4i64:
6636 case MVT::v4f64: return X86ISD::VPERMILPDY;
6637 default:
6638 llvm_unreachable("Unknown type for vpermil");
6639 }
6640 return 0;
6641}
6642
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006643static
6644SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006645 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006646 const X86Subtarget *Subtarget) {
6647 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6648 EVT VT = Op.getValueType();
6649 DebugLoc dl = Op.getDebugLoc();
6650 SDValue V1 = Op.getOperand(0);
6651 SDValue V2 = Op.getOperand(1);
6652
6653 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006654 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006655
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006656 // Handle splat operations
6657 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006658 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006659 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006660 // Special case, this is the only place now where it's allowed to return
6661 // a vector_shuffle operation without using a target specific node, because
6662 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6663 // this be moved to DAGCombine instead?
6664 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006665 return Op;
6666
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006667 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00006668 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00006669 if (Subtarget->hasAVX() && LD.getNode())
6670 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006671
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006672 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006673 if ((Size == 128 && NumElem <= 4) ||
6674 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006675 return SDValue();
6676
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006677 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006678 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006679 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006680
6681 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6682 // do it!
6683 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6684 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6685 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006686 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006687 } else if ((VT == MVT::v4i32 ||
6688 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006689 // FIXME: Figure out a cleaner way to do this.
6690 // Try to make use of movq to zero out the top part.
6691 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6692 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6693 if (NewOp.getNode()) {
6694 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6695 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6696 DAG, Subtarget, dl);
6697 }
6698 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6699 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6700 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6701 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6702 DAG, Subtarget, dl);
6703 }
6704 }
6705 return SDValue();
6706}
6707
Dan Gohman475871a2008-07-27 21:46:04 +00006708SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006709X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006710 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006711 SDValue V1 = Op.getOperand(0);
6712 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006713 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006714 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006715 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006716 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6717 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006718 bool V1IsSplat = false;
6719 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006720 bool HasXMMInt = Subtarget->hasXMMInt();
Craig Topper6347e862011-11-21 06:57:39 +00006721 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006722 MachineFunction &MF = DAG.getMachineFunction();
6723 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006724
Craig Topper3426a3e2011-11-14 06:46:21 +00006725 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006726
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006727 // Vector shuffle lowering takes 3 steps:
6728 //
6729 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6730 // narrowing and commutation of operands should be handled.
6731 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6732 // shuffle nodes.
6733 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6734 // so the shuffle can be broken into other shuffles and the legalizer can
6735 // try the lowering again.
6736 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006737 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006738 // be matched during isel, all of them must be converted to a target specific
6739 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006740
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006741 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6742 // narrowing and commutation of operands should be handled. The actual code
6743 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006744 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006745 if (NewOp.getNode())
6746 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006747
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006748 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6749 // unpckh_undef). Only use pshufd if speed is more important than size.
6750 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006751 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V1,
6752 DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006753 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006754 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6755 DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006756
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006757 if (X86::isMOVDDUPMask(SVOp) &&
6758 (Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
6759 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006760 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006761
Dale Johannesen0488fb62010-09-30 23:57:10 +00006762 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006763 return getMOVHighToLow(Op, dl, DAG);
6764
6765 // Use to match splats
Craig Topper6347e862011-11-21 06:57:39 +00006766 if (HasXMMInt && X86::isUNPCKHMask(SVOp, Subtarget->hasAVX2()) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006767 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper6347e862011-11-21 06:57:39 +00006768 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6769 DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006770
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006771 if (X86::isPSHUFDMask(SVOp)) {
6772 // The actual implementation will match the mask in the if above and then
6773 // during isel it can match several different instructions, not only pshufd
6774 // as its name says, sad but true, emulate the behavior for now...
6775 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6776 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6777
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006778 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6779
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006780 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006781 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6782
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006783 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6784 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006785 }
Eric Christopherfd179292009-08-27 18:07:15 +00006786
Evan Chengf26ffe92008-05-29 08:22:04 +00006787 // Check if this can be converted into a logical shift.
6788 bool isLeft = false;
6789 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006790 SDValue ShVal;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006791 bool isShift = getSubtarget()->hasXMMInt() &&
6792 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006793 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006794 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006795 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006796 EVT EltVT = VT.getVectorElementType();
6797 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006798 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006799 }
Eric Christopherfd179292009-08-27 18:07:15 +00006800
Nate Begeman9008ca62009-04-27 18:41:29 +00006801 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006802 if (V1IsUndef)
6803 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006804 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006805 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006806 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006807 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006808 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6809
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006810 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006811 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6812 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006813 }
Eric Christopherfd179292009-08-27 18:07:15 +00006814
Nate Begeman9008ca62009-04-27 18:41:29 +00006815 // FIXME: fold these into legal mask.
Craig Topper6347e862011-11-21 06:57:39 +00006816 if (X86::isMOVLHPSMask(SVOp) &&
6817 !X86::isUNPCKLMask(SVOp, Subtarget->hasAVX2()))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006818 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006819
Dale Johannesen0488fb62010-09-30 23:57:10 +00006820 if (X86::isMOVHLPSMask(SVOp))
6821 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006822
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006823 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006824 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006825
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006826 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006827 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006828
Dale Johannesen0488fb62010-09-30 23:57:10 +00006829 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006830 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006831
Nate Begeman9008ca62009-04-27 18:41:29 +00006832 if (ShouldXformToMOVHLPS(SVOp) ||
6833 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6834 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006835
Evan Chengf26ffe92008-05-29 08:22:04 +00006836 if (isShift) {
6837 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006838 EVT EltVT = VT.getVectorElementType();
6839 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006840 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006841 }
Eric Christopherfd179292009-08-27 18:07:15 +00006842
Evan Cheng9eca5e82006-10-25 21:49:50 +00006843 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006844 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6845 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006846 V1IsSplat = isSplatVector(V1.getNode());
6847 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006848
Chris Lattner8a594482007-11-25 00:24:49 +00006849 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006850 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006851 Op = CommuteVectorShuffle(SVOp, DAG);
6852 SVOp = cast<ShuffleVectorSDNode>(Op);
6853 V1 = SVOp->getOperand(0);
6854 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006855 std::swap(V1IsSplat, V2IsSplat);
6856 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006857 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006858 }
6859
Nate Begeman9008ca62009-04-27 18:41:29 +00006860 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6861 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006862 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006863 return V1;
6864 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6865 // the instruction selector will not match, so get a canonical MOVL with
6866 // swapped operands to undo the commute.
6867 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006868 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006869
Craig Topper6347e862011-11-21 06:57:39 +00006870 if (X86::isUNPCKLMask(SVOp, Subtarget->hasAVX2()))
6871 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V2,
6872 DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006873
Craig Topper6347e862011-11-21 06:57:39 +00006874 if (X86::isUNPCKHMask(SVOp, Subtarget->hasAVX2()))
6875 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V2,
6876 DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006877
Evan Cheng9bbbb982006-10-25 20:48:19 +00006878 if (V2IsSplat) {
6879 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006880 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006881 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006882 SDValue NewMask = NormalizeMask(SVOp, DAG);
6883 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6884 if (NSVOp != SVOp) {
Craig Topper6347e862011-11-21 06:57:39 +00006885 if (X86::isUNPCKLMask(NSVOp, Subtarget->hasAVX2(), true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006886 return NewMask;
Craig Topper6347e862011-11-21 06:57:39 +00006887 } else if (X86::isUNPCKHMask(NSVOp, Subtarget->hasAVX2(), true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006888 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006889 }
6890 }
6891 }
6892
Evan Cheng9eca5e82006-10-25 21:49:50 +00006893 if (Commuted) {
6894 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006895 // FIXME: this seems wrong.
6896 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6897 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006898
Craig Topper6347e862011-11-21 06:57:39 +00006899 if (X86::isUNPCKLMask(NewSVOp, Subtarget->hasAVX2()))
6900 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V2, V1,
6901 DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006902
Craig Topper6347e862011-11-21 06:57:39 +00006903 if (X86::isUNPCKHMask(NewSVOp, Subtarget->hasAVX2()))
6904 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V2, V1,
6905 DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006906 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006907
Nate Begeman9008ca62009-04-27 18:41:29 +00006908 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006909 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006910 return CommuteVectorShuffle(SVOp, DAG);
6911
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006912 // The checks below are all present in isShuffleMaskLegal, but they are
6913 // inlined here right now to enable us to directly emit target specific
6914 // nodes, and remove one by one until they don't return Op anymore.
6915 SmallVector<int, 16> M;
6916 SVOp->getMask(M);
6917
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006918 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006919 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6920 X86::getShufflePALIGNRImmediate(SVOp),
6921 DAG);
6922
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006923 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6924 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006925 if (VT == MVT::v2f64)
6926 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006927 if (VT == MVT::v2i64)
6928 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6929 }
6930
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006931 if (isPSHUFHWMask(M, VT))
6932 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6933 X86::getShufflePSHUFHWImmediate(SVOp),
6934 DAG);
6935
6936 if (isPSHUFLWMask(M, VT))
6937 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6938 X86::getShufflePSHUFLWImmediate(SVOp),
6939 DAG);
6940
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006941 if (isSHUFPMask(M, VT))
6942 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6943 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006944
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006945 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006946 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V1,
6947 DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006948 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006949 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6950 DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006951
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006952 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006953 // Generate target specific nodes for 128 or 256-bit shuffles only
6954 // supported in the AVX instruction set.
6955 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006956
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006957 // Handle VMOVDDUPY permutations
6958 if (isMOVDDUPYMask(SVOp, Subtarget))
6959 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6960
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006961 // Handle VPERMILPS* permutations
6962 if (isVPERMILPSMask(M, VT, Subtarget))
6963 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6964 getShuffleVPERMILPSImmediate(SVOp), DAG);
6965
6966 // Handle VPERMILPD* permutations
6967 if (isVPERMILPDMask(M, VT, Subtarget))
6968 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6969 getShuffleVPERMILPDImmediate(SVOp), DAG);
6970
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006971 // Handle VPERM2F128 permutations
6972 if (isVPERM2F128Mask(M, VT, Subtarget))
6973 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6974 getShuffleVPERM2F128Immediate(SVOp), DAG);
6975
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006976 // Handle VSHUFPSY permutations
6977 if (isVSHUFPSYMask(M, VT, Subtarget))
6978 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6979 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6980
6981 // Handle VSHUFPDY permutations
6982 if (isVSHUFPDYMask(M, VT, Subtarget))
6983 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6984 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6985
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006986 //===--------------------------------------------------------------------===//
6987 // Since no target specific shuffle was selected for this generic one,
6988 // lower it into other known shuffles. FIXME: this isn't true yet, but
6989 // this is the plan.
6990 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006991
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006992 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6993 if (VT == MVT::v8i16) {
6994 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6995 if (NewOp.getNode())
6996 return NewOp;
6997 }
6998
6999 if (VT == MVT::v16i8) {
7000 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7001 if (NewOp.getNode())
7002 return NewOp;
7003 }
7004
7005 // Handle all 128-bit wide vectors with 4 elements, and match them with
7006 // several different shuffle types.
7007 if (NumElems == 4 && VT.getSizeInBits() == 128)
7008 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7009
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007010 // Handle general 256-bit shuffles
7011 if (VT.is256BitVector())
7012 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7013
Dan Gohman475871a2008-07-27 21:46:04 +00007014 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007015}
7016
Dan Gohman475871a2008-07-27 21:46:04 +00007017SDValue
7018X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007019 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007020 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007021 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007022
7023 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
7024 return SDValue();
7025
Duncan Sands83ec4b62008-06-06 12:08:01 +00007026 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007027 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00007028 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007029 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00007030 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007031 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007032 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007033 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7034 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7035 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007036 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7037 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007038 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007039 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007040 Op.getOperand(0)),
7041 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007042 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00007043 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007044 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00007045 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007046 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00007047 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007048 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7049 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007050 // result has a single use which is a store or a bitcast to i32. And in
7051 // the case of a store, it's not worth it if the index is a constant 0,
7052 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007053 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007054 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007055 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007056 if ((User->getOpcode() != ISD::STORE ||
7057 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7058 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007059 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007060 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007061 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007062 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007063 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007064 Op.getOperand(0)),
7065 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007066 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Pete Coopera77214a2011-11-14 19:38:42 +00007067 } else if (VT == MVT::i32 || VT == MVT::i64) {
7068 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007069 if (isa<ConstantSDNode>(Op.getOperand(1)))
7070 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007071 }
Dan Gohman475871a2008-07-27 21:46:04 +00007072 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007073}
7074
7075
Dan Gohman475871a2008-07-27 21:46:04 +00007076SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007077X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7078 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007079 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007080 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007081
David Greene74a579d2011-02-10 16:57:36 +00007082 SDValue Vec = Op.getOperand(0);
7083 EVT VecVT = Vec.getValueType();
7084
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007085 // If this is a 256-bit vector result, first extract the 128-bit vector and
7086 // then extract the element from the 128-bit vector.
7087 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00007088 DebugLoc dl = Op.getNode()->getDebugLoc();
7089 unsigned NumElems = VecVT.getVectorNumElements();
7090 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007091 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7092
7093 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007094 bool Upper = IdxVal >= NumElems/2;
7095 Vec = Extract128BitVector(Vec,
7096 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007097
David Greene74a579d2011-02-10 16:57:36 +00007098 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007099 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00007100 }
7101
7102 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
7103
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007104 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007105 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007106 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007107 return Res;
7108 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007109
Owen Andersone50ed302009-08-10 22:56:29 +00007110 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007111 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007112 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007113 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007114 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007115 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007116 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007117 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7118 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007119 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007120 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007121 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007122 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007123 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007124 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007125 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007126 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007127 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007128 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007129 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007130 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007131 if (Idx == 0)
7132 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007133
Evan Cheng0db9fe62006-04-25 20:13:52 +00007134 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007135 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007136 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007137 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007138 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007139 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007140 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00007141 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007142 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7143 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7144 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007145 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007146 if (Idx == 0)
7147 return Op;
7148
7149 // UNPCKHPD the element to the lowest double word, then movsd.
7150 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7151 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007152 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007153 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007154 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007155 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007156 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007157 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007158 }
7159
Dan Gohman475871a2008-07-27 21:46:04 +00007160 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007161}
7162
Dan Gohman475871a2008-07-27 21:46:04 +00007163SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007164X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7165 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007166 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007167 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007168 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007169
Dan Gohman475871a2008-07-27 21:46:04 +00007170 SDValue N0 = Op.getOperand(0);
7171 SDValue N1 = Op.getOperand(1);
7172 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007173
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007174 if (VT.getSizeInBits() == 256)
7175 return SDValue();
7176
Dan Gohman8a55ce42009-09-23 21:02:20 +00007177 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007178 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007179 unsigned Opc;
7180 if (VT == MVT::v8i16)
7181 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007182 else if (VT == MVT::v16i8)
7183 Opc = X86ISD::PINSRB;
7184 else
7185 Opc = X86ISD::PINSRB;
7186
Nate Begeman14d12ca2008-02-11 04:19:36 +00007187 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7188 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007189 if (N1.getValueType() != MVT::i32)
7190 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7191 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007192 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007193 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007194 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007195 // Bits [7:6] of the constant are the source select. This will always be
7196 // zero here. The DAG Combiner may combine an extract_elt index into these
7197 // bits. For example (insert (extract, 3), 2) could be matched by putting
7198 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007199 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007200 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007201 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007202 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007203 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007204 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007205 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007206 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Pete Coopera77214a2011-11-14 19:38:42 +00007207 } else if ((EltVT == MVT::i32 || EltVT == MVT::i64) &&
7208 isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007209 // PINSR* works with constant index.
7210 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007211 }
Dan Gohman475871a2008-07-27 21:46:04 +00007212 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007213}
7214
Dan Gohman475871a2008-07-27 21:46:04 +00007215SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007216X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007217 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007218 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007219
David Greene6b381262011-02-09 15:32:06 +00007220 DebugLoc dl = Op.getDebugLoc();
7221 SDValue N0 = Op.getOperand(0);
7222 SDValue N1 = Op.getOperand(1);
7223 SDValue N2 = Op.getOperand(2);
7224
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007225 // If this is a 256-bit vector result, first extract the 128-bit vector,
7226 // insert the element into the extracted half and then place it back.
7227 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007228 if (!isa<ConstantSDNode>(N2))
7229 return SDValue();
7230
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007231 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007232 unsigned NumElems = VT.getVectorNumElements();
7233 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007234 bool Upper = IdxVal >= NumElems/2;
7235 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7236 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007237
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007238 // Insert the element into the desired half.
7239 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7240 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007241
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007242 // Insert the changed part back to the 256-bit vector
7243 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007244 }
7245
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007246 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007247 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7248
Dan Gohman8a55ce42009-09-23 21:02:20 +00007249 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007250 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007251
Dan Gohman8a55ce42009-09-23 21:02:20 +00007252 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007253 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7254 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007255 if (N1.getValueType() != MVT::i32)
7256 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7257 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007258 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007259 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007260 }
Dan Gohman475871a2008-07-27 21:46:04 +00007261 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007262}
7263
Dan Gohman475871a2008-07-27 21:46:04 +00007264SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007265X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007266 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007267 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007268 EVT OpVT = Op.getValueType();
7269
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007270 // If this is a 256-bit vector result, first insert into a 128-bit
7271 // vector and then insert into the 256-bit vector.
7272 if (OpVT.getSizeInBits() > 128) {
7273 // Insert into a 128-bit vector.
7274 EVT VT128 = EVT::getVectorVT(*Context,
7275 OpVT.getVectorElementType(),
7276 OpVT.getVectorNumElements() / 2);
7277
7278 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7279
7280 // Insert the 128-bit vector.
7281 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7282 DAG.getConstant(0, MVT::i32),
7283 DAG, dl);
7284 }
7285
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007286 if (Op.getValueType() == MVT::v1i64 &&
7287 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007288 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007289
Owen Anderson825b72b2009-08-11 20:47:22 +00007290 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007291 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7292 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007293 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007294 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007295}
7296
David Greene91585092011-01-26 15:38:49 +00007297// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7298// a simple subregister reference or explicit instructions to grab
7299// upper bits of a vector.
7300SDValue
7301X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7302 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007303 DebugLoc dl = Op.getNode()->getDebugLoc();
7304 SDValue Vec = Op.getNode()->getOperand(0);
7305 SDValue Idx = Op.getNode()->getOperand(1);
7306
7307 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7308 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7309 return Extract128BitVector(Vec, Idx, DAG, dl);
7310 }
David Greene91585092011-01-26 15:38:49 +00007311 }
7312 return SDValue();
7313}
7314
David Greenecfe33c42011-01-26 19:13:22 +00007315// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7316// simple superregister reference or explicit instructions to insert
7317// the upper bits of a vector.
7318SDValue
7319X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7320 if (Subtarget->hasAVX()) {
7321 DebugLoc dl = Op.getNode()->getDebugLoc();
7322 SDValue Vec = Op.getNode()->getOperand(0);
7323 SDValue SubVec = Op.getNode()->getOperand(1);
7324 SDValue Idx = Op.getNode()->getOperand(2);
7325
7326 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7327 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007328 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007329 }
7330 }
7331 return SDValue();
7332}
7333
Bill Wendling056292f2008-09-16 21:48:12 +00007334// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7335// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7336// one of the above mentioned nodes. It has to be wrapped because otherwise
7337// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7338// be used to form addressing mode. These wrapped nodes will be selected
7339// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007340SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007341X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007342 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007343
Chris Lattner41621a22009-06-26 19:22:52 +00007344 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7345 // global base reg.
7346 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007347 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007348 CodeModel::Model M = getTargetMachine().getCodeModel();
7349
Chris Lattner4f066492009-07-11 20:29:19 +00007350 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007351 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007352 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007353 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007354 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007355 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007356 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007357
Evan Cheng1606e8e2009-03-13 07:51:59 +00007358 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007359 CP->getAlignment(),
7360 CP->getOffset(), OpFlag);
7361 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007362 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007363 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007364 if (OpFlag) {
7365 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007366 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007367 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007368 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007369 }
7370
7371 return Result;
7372}
7373
Dan Gohmand858e902010-04-17 15:26:15 +00007374SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007375 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007376
Chris Lattner18c59872009-06-27 04:16:01 +00007377 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7378 // global base reg.
7379 unsigned char OpFlag = 0;
7380 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007381 CodeModel::Model M = getTargetMachine().getCodeModel();
7382
Chris Lattner4f066492009-07-11 20:29:19 +00007383 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007384 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007385 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007386 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007387 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007388 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007389 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007390
Chris Lattner18c59872009-06-27 04:16:01 +00007391 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7392 OpFlag);
7393 DebugLoc DL = JT->getDebugLoc();
7394 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007395
Chris Lattner18c59872009-06-27 04:16:01 +00007396 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007397 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007398 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7399 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007400 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007401 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007402
Chris Lattner18c59872009-06-27 04:16:01 +00007403 return Result;
7404}
7405
7406SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007407X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007408 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007409
Chris Lattner18c59872009-06-27 04:16:01 +00007410 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7411 // global base reg.
7412 unsigned char OpFlag = 0;
7413 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007414 CodeModel::Model M = getTargetMachine().getCodeModel();
7415
Chris Lattner4f066492009-07-11 20:29:19 +00007416 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007417 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7418 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7419 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007420 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007421 } else if (Subtarget->isPICStyleGOT()) {
7422 OpFlag = X86II::MO_GOT;
7423 } else if (Subtarget->isPICStyleStubPIC()) {
7424 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7425 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7426 OpFlag = X86II::MO_DARWIN_NONLAZY;
7427 }
Eric Christopherfd179292009-08-27 18:07:15 +00007428
Chris Lattner18c59872009-06-27 04:16:01 +00007429 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007430
Chris Lattner18c59872009-06-27 04:16:01 +00007431 DebugLoc DL = Op.getDebugLoc();
7432 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007433
7434
Chris Lattner18c59872009-06-27 04:16:01 +00007435 // With PIC, the address is actually $g + Offset.
7436 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007437 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007438 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7439 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007440 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007441 Result);
7442 }
Eric Christopherfd179292009-08-27 18:07:15 +00007443
Eli Friedman586272d2011-08-11 01:48:05 +00007444 // For symbols that require a load from a stub to get the address, emit the
7445 // load.
7446 if (isGlobalStubReference(OpFlag))
7447 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007448 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007449
Chris Lattner18c59872009-06-27 04:16:01 +00007450 return Result;
7451}
7452
Dan Gohman475871a2008-07-27 21:46:04 +00007453SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007454X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007455 // Create the TargetBlockAddressAddress node.
7456 unsigned char OpFlags =
7457 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007458 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007459 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007460 DebugLoc dl = Op.getDebugLoc();
7461 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7462 /*isTarget=*/true, OpFlags);
7463
Dan Gohmanf705adb2009-10-30 01:28:02 +00007464 if (Subtarget->isPICStyleRIPRel() &&
7465 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007466 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7467 else
7468 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007469
Dan Gohman29cbade2009-11-20 23:18:13 +00007470 // With PIC, the address is actually $g + Offset.
7471 if (isGlobalRelativeToPICBase(OpFlags)) {
7472 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7473 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7474 Result);
7475 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007476
7477 return Result;
7478}
7479
7480SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007481X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007482 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007483 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007484 // Create the TargetGlobalAddress node, folding in the constant
7485 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007486 unsigned char OpFlags =
7487 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007488 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007489 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007490 if (OpFlags == X86II::MO_NO_FLAG &&
7491 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007492 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007493 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007494 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007495 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007496 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007497 }
Eric Christopherfd179292009-08-27 18:07:15 +00007498
Chris Lattner4f066492009-07-11 20:29:19 +00007499 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007500 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007501 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7502 else
7503 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007504
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007505 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007506 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007507 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7508 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007509 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007510 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007511
Chris Lattner36c25012009-07-10 07:34:39 +00007512 // For globals that require a load from a stub to get the address, emit the
7513 // load.
7514 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007515 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007516 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007517
Dan Gohman6520e202008-10-18 02:06:02 +00007518 // If there was a non-zero offset that we didn't fold, create an explicit
7519 // addition for it.
7520 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007521 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007522 DAG.getConstant(Offset, getPointerTy()));
7523
Evan Cheng0db9fe62006-04-25 20:13:52 +00007524 return Result;
7525}
7526
Evan Chengda43bcf2008-09-24 00:05:32 +00007527SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007528X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007529 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007530 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007531 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007532}
7533
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007534static SDValue
7535GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007536 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007537 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007538 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007539 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007540 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007541 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007542 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007543 GA->getOffset(),
7544 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007545 if (InFlag) {
7546 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007547 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007548 } else {
7549 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007550 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007551 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007552
7553 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007554 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007555
Rafael Espindola15f1b662009-04-24 12:59:40 +00007556 SDValue Flag = Chain.getValue(1);
7557 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007558}
7559
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007560// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007561static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007562LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007563 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007564 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007565 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7566 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007567 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007568 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007569 InFlag = Chain.getValue(1);
7570
Chris Lattnerb903bed2009-06-26 21:20:29 +00007571 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007572}
7573
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007574// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007575static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007576LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007577 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007578 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7579 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007580}
7581
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007582// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7583// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007584static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007585 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007586 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007587 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007588
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007589 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7590 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7591 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007592
Michael J. Spencerec38de22010-10-10 22:04:20 +00007593 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007594 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007595 MachinePointerInfo(Ptr),
7596 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007597
Chris Lattnerb903bed2009-06-26 21:20:29 +00007598 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007599 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7600 // initialexec.
7601 unsigned WrapperKind = X86ISD::Wrapper;
7602 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007603 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007604 } else if (is64Bit) {
7605 assert(model == TLSModel::InitialExec);
7606 OperandFlags = X86II::MO_GOTTPOFF;
7607 WrapperKind = X86ISD::WrapperRIP;
7608 } else {
7609 assert(model == TLSModel::InitialExec);
7610 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007611 }
Eric Christopherfd179292009-08-27 18:07:15 +00007612
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007613 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7614 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007615 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007616 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007617 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007618 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007619
Rafael Espindola9a580232009-02-27 13:37:18 +00007620 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007621 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007622 MachinePointerInfo::getGOT(), false, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007623
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007624 // The address of the thread local variable is the add of the thread
7625 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007626 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007627}
7628
Dan Gohman475871a2008-07-27 21:46:04 +00007629SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007630X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007631
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007632 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007633 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007634
Eric Christopher30ef0e52010-06-03 04:07:48 +00007635 if (Subtarget->isTargetELF()) {
7636 // TODO: implement the "local dynamic" model
7637 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007638
Eric Christopher30ef0e52010-06-03 04:07:48 +00007639 // If GV is an alias then use the aliasee for determining
7640 // thread-localness.
7641 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7642 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007643
7644 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007645 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007646
Eric Christopher30ef0e52010-06-03 04:07:48 +00007647 switch (model) {
7648 case TLSModel::GeneralDynamic:
7649 case TLSModel::LocalDynamic: // not implemented
7650 if (Subtarget->is64Bit())
7651 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7652 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007653
Eric Christopher30ef0e52010-06-03 04:07:48 +00007654 case TLSModel::InitialExec:
7655 case TLSModel::LocalExec:
7656 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7657 Subtarget->is64Bit());
7658 }
7659 } else if (Subtarget->isTargetDarwin()) {
7660 // Darwin only has one model of TLS. Lower to that.
7661 unsigned char OpFlag = 0;
7662 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7663 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007664
Eric Christopher30ef0e52010-06-03 04:07:48 +00007665 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7666 // global base reg.
7667 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7668 !Subtarget->is64Bit();
7669 if (PIC32)
7670 OpFlag = X86II::MO_TLVP_PIC_BASE;
7671 else
7672 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007673 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007674 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007675 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007676 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007677 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007678
Eric Christopher30ef0e52010-06-03 04:07:48 +00007679 // With PIC32, the address is actually $g + Offset.
7680 if (PIC32)
7681 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7682 DAG.getNode(X86ISD::GlobalBaseReg,
7683 DebugLoc(), getPointerTy()),
7684 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007685
Eric Christopher30ef0e52010-06-03 04:07:48 +00007686 // Lowering the machine isd will make sure everything is in the right
7687 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007688 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007689 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007690 SDValue Args[] = { Chain, Offset };
7691 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007692
Eric Christopher30ef0e52010-06-03 04:07:48 +00007693 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7694 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7695 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007696
Eric Christopher30ef0e52010-06-03 04:07:48 +00007697 // And our return value (tls address) is in the standard call return value
7698 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007699 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007700 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7701 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007702 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007703
Eric Christopher30ef0e52010-06-03 04:07:48 +00007704 assert(false &&
7705 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007706
Torok Edwinc23197a2009-07-14 16:55:14 +00007707 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007708 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007709}
7710
Evan Cheng0db9fe62006-04-25 20:13:52 +00007711
Nadav Rotem43012222011-05-11 08:12:09 +00007712/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007713/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007714SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007715 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007716 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007717 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007718 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007719 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007720 SDValue ShOpLo = Op.getOperand(0);
7721 SDValue ShOpHi = Op.getOperand(1);
7722 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007723 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007724 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007725 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007726
Dan Gohman475871a2008-07-27 21:46:04 +00007727 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007728 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007729 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7730 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007731 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007732 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7733 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007734 }
Evan Chenge3413162006-01-09 18:33:28 +00007735
Owen Anderson825b72b2009-08-11 20:47:22 +00007736 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7737 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007738 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007739 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007740
Dan Gohman475871a2008-07-27 21:46:04 +00007741 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007742 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007743 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7744 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007745
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007746 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007747 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7748 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007749 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007750 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7751 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007752 }
7753
Dan Gohman475871a2008-07-27 21:46:04 +00007754 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007755 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007756}
Evan Chenga3195e82006-01-12 22:54:21 +00007757
Dan Gohmand858e902010-04-17 15:26:15 +00007758SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7759 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007760 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007761
Dale Johannesen0488fb62010-09-30 23:57:10 +00007762 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007763 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007764
Owen Anderson825b72b2009-08-11 20:47:22 +00007765 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007766 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007767
Eli Friedman36df4992009-05-27 00:47:34 +00007768 // These are really Legal; return the operand so the caller accepts it as
7769 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007770 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007771 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007772 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007773 Subtarget->is64Bit()) {
7774 return Op;
7775 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007776
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007777 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007778 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007779 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007780 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007781 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007782 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007783 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007784 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007785 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007786 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7787}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007788
Owen Andersone50ed302009-08-10 22:56:29 +00007789SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007790 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007791 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007792 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007793 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007794 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007795 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007796 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007797 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007798 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007799 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007800
Chris Lattner492a43e2010-09-22 01:28:21 +00007801 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007802
Stuart Hastings84be9582011-06-02 15:57:11 +00007803 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7804 MachineMemOperand *MMO;
7805 if (FI) {
7806 int SSFI = FI->getIndex();
7807 MMO =
7808 DAG.getMachineFunction()
7809 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7810 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7811 } else {
7812 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7813 StackSlot = StackSlot.getOperand(1);
7814 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007815 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007816 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7817 X86ISD::FILD, DL,
7818 Tys, Ops, array_lengthof(Ops),
7819 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007820
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007821 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007822 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007823 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007824
7825 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7826 // shouldn't be necessary except that RFP cannot be live across
7827 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007828 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007829 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7830 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007831 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007832 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007833 SDValue Ops[] = {
7834 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7835 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007836 MachineMemOperand *MMO =
7837 DAG.getMachineFunction()
7838 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007839 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007840
Chris Lattner492a43e2010-09-22 01:28:21 +00007841 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7842 Ops, array_lengthof(Ops),
7843 Op.getValueType(), MMO);
7844 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007845 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007846 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007847 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007848
Evan Cheng0db9fe62006-04-25 20:13:52 +00007849 return Result;
7850}
7851
Bill Wendling8b8a6362009-01-17 03:56:04 +00007852// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007853SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7854 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007855 // This algorithm is not obvious. Here it is in C code, more or less:
7856 /*
7857 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7858 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7859 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007860
Bill Wendling8b8a6362009-01-17 03:56:04 +00007861 // Copy ints to xmm registers.
7862 __m128i xh = _mm_cvtsi32_si128( hi );
7863 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007864
Bill Wendling8b8a6362009-01-17 03:56:04 +00007865 // Combine into low half of a single xmm register.
7866 __m128i x = _mm_unpacklo_epi32( xh, xl );
7867 __m128d d;
7868 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007869
Bill Wendling8b8a6362009-01-17 03:56:04 +00007870 // Merge in appropriate exponents to give the integer bits the right
7871 // magnitude.
7872 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007873
Bill Wendling8b8a6362009-01-17 03:56:04 +00007874 // Subtract away the biases to deal with the IEEE-754 double precision
7875 // implicit 1.
7876 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007877
Bill Wendling8b8a6362009-01-17 03:56:04 +00007878 // All conversions up to here are exact. The correctly rounded result is
7879 // calculated using the current rounding mode using the following
7880 // horizontal add.
7881 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7882 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7883 // store doesn't really need to be here (except
7884 // maybe to zero the other double)
7885 return sd;
7886 }
7887 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007888
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007889 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007890 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007891
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007892 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007893 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007894 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7895 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7896 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7897 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007898 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007899 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007900
Bill Wendling8b8a6362009-01-17 03:56:04 +00007901 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007902 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007903 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007904 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007905 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007906 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007907 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007908
Owen Anderson825b72b2009-08-11 20:47:22 +00007909 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7910 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007911 Op.getOperand(0),
7912 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007913 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7914 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007915 Op.getOperand(0),
7916 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007917 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7918 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007919 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007920 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007921 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007922 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007923 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007924 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007925 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007926 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007927
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007928 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007929 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007930 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7931 DAG.getUNDEF(MVT::v2f64), ShufMask);
7932 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7933 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007934 DAG.getIntPtrConstant(0));
7935}
7936
Bill Wendling8b8a6362009-01-17 03:56:04 +00007937// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007938SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7939 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007940 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007941 // FP constant to bias correct the final result.
7942 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007943 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007944
7945 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007946 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007947 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007948
Eli Friedmanf3704762011-08-29 21:15:46 +00007949 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007950 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7951 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007952
Owen Anderson825b72b2009-08-11 20:47:22 +00007953 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007954 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007955 DAG.getIntPtrConstant(0));
7956
7957 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007958 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007959 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007960 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007961 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007962 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007963 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007964 MVT::v2f64, Bias)));
7965 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007966 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007967 DAG.getIntPtrConstant(0));
7968
7969 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007970 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007971
7972 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007973 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007974
Owen Anderson825b72b2009-08-11 20:47:22 +00007975 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007976 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007977 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007978 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007979 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007980 }
7981
7982 // Handle final rounding.
7983 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007984}
7985
Dan Gohmand858e902010-04-17 15:26:15 +00007986SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7987 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007988 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007989 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007990
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007991 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007992 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7993 // the optimization here.
7994 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007995 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007996
Owen Andersone50ed302009-08-10 22:56:29 +00007997 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007998 EVT DstVT = Op.getValueType();
7999 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008000 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008001 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008002 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008003
8004 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008005 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008006 if (SrcVT == MVT::i32) {
8007 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8008 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8009 getPointerTy(), StackSlot, WordOff);
8010 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008011 StackSlot, MachinePointerInfo(),
8012 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008013 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008014 OffsetSlot, MachinePointerInfo(),
8015 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008016 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8017 return Fild;
8018 }
8019
8020 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8021 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008022 StackSlot, MachinePointerInfo(),
8023 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008024 // For i64 source, we need to add the appropriate power of 2 if the input
8025 // was negative. This is the same as the optimization in
8026 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8027 // we must be careful to do the computation in x87 extended precision, not
8028 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008029 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8030 MachineMemOperand *MMO =
8031 DAG.getMachineFunction()
8032 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8033 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008034
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008035 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8036 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008037 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8038 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008039
8040 APInt FF(32, 0x5F800000ULL);
8041
8042 // Check whether the sign bit is set.
8043 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8044 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8045 ISD::SETLT);
8046
8047 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8048 SDValue FudgePtr = DAG.getConstantPool(
8049 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8050 getPointerTy());
8051
8052 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8053 SDValue Zero = DAG.getIntPtrConstant(0);
8054 SDValue Four = DAG.getIntPtrConstant(4);
8055 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8056 Zero, Four);
8057 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8058
8059 // Load the value out, extending it from f32 to f80.
8060 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008061 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008062 FudgePtr, MachinePointerInfo::getConstantPool(),
8063 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008064 // Extend everything to 80 bits to force it to be done on x87.
8065 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8066 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008067}
8068
Dan Gohman475871a2008-07-27 21:46:04 +00008069std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00008070FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00008071 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008072
Owen Andersone50ed302009-08-10 22:56:29 +00008073 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008074
8075 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008076 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8077 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008078 }
8079
Owen Anderson825b72b2009-08-11 20:47:22 +00008080 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8081 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00008082 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008083
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008084 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008085 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008086 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008087 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008088 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008089 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008090 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008091 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008092
Evan Cheng87c89352007-10-15 20:11:21 +00008093 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
8094 // stack slot.
8095 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008096 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008097 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008098 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008099
Michael J. Spencerec38de22010-10-10 22:04:20 +00008100
8101
Evan Cheng0db9fe62006-04-25 20:13:52 +00008102 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00008103 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008104 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008105 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8106 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8107 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00008108 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008109
Dan Gohman475871a2008-07-27 21:46:04 +00008110 SDValue Chain = DAG.getEntryNode();
8111 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008112 EVT TheVT = Op.getOperand(0).getValueType();
8113 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008114 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008115 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008116 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008117 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008118 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008119 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008120 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008121 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008122
Chris Lattner492a43e2010-09-22 01:28:21 +00008123 MachineMemOperand *MMO =
8124 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8125 MachineMemOperand::MOLoad, MemSize, MemSize);
8126 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8127 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008128 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008129 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008130 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8131 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008132
Chris Lattner07290932010-09-22 01:05:16 +00008133 MachineMemOperand *MMO =
8134 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8135 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008136
Evan Cheng0db9fe62006-04-25 20:13:52 +00008137 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00008138 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00008139 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8140 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00008141
Chris Lattner27a6c732007-11-24 07:07:01 +00008142 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008143}
8144
Dan Gohmand858e902010-04-17 15:26:15 +00008145SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8146 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008147 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008148 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008149
Eli Friedman948e95a2009-05-23 09:59:16 +00008150 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00008151 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008152 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8153 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008154
Chris Lattner27a6c732007-11-24 07:07:01 +00008155 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008156 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008157 FIST, StackSlot, MachinePointerInfo(),
8158 false, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00008159}
8160
Dan Gohmand858e902010-04-17 15:26:15 +00008161SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8162 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00008163 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
8164 SDValue FIST = Vals.first, StackSlot = Vals.second;
8165 assert(FIST.getNode() && "Unexpected failure");
8166
8167 // Load the result.
8168 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008169 FIST, StackSlot, MachinePointerInfo(),
8170 false, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00008171}
8172
Dan Gohmand858e902010-04-17 15:26:15 +00008173SDValue X86TargetLowering::LowerFABS(SDValue Op,
8174 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008175 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008176 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008177 EVT VT = Op.getValueType();
8178 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008179 if (VT.isVector())
8180 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008181 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008182 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008183 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00008184 CV.push_back(C);
8185 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008186 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008187 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00008188 CV.push_back(C);
8189 CV.push_back(C);
8190 CV.push_back(C);
8191 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008192 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008193 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008194 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008195 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008196 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008197 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008198 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008199}
8200
Dan Gohmand858e902010-04-17 15:26:15 +00008201SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008202 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008203 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008204 EVT VT = Op.getValueType();
8205 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008206 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008207 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008208 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008209 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008210 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008211 CV.push_back(C);
8212 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008213 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008214 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008215 CV.push_back(C);
8216 CV.push_back(C);
8217 CV.push_back(C);
8218 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008219 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008220 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008221 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008222 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008223 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008224 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008225 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008226 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008227 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008228 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008229 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008230 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008231 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008232 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008233 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008234}
8235
Dan Gohmand858e902010-04-17 15:26:15 +00008236SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008237 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008238 SDValue Op0 = Op.getOperand(0);
8239 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008240 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008241 EVT VT = Op.getValueType();
8242 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008243
8244 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008245 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008246 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008247 SrcVT = VT;
8248 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008249 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008250 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008251 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008252 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008253 }
8254
8255 // At this point the operands and the result should have the same
8256 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008257
Evan Cheng68c47cb2007-01-05 07:55:56 +00008258 // First get the sign bit of second operand.
8259 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008260 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008261 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8262 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008263 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008264 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8265 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8266 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8267 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008268 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008269 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008270 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008271 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008272 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008273 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008274 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008275
8276 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008277 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008278 // Op0 is MVT::f32, Op1 is MVT::f64.
8279 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8280 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8281 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008282 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008283 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008284 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008285 }
8286
Evan Cheng73d6cf12007-01-05 21:37:56 +00008287 // Clear first operand sign bit.
8288 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008289 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008290 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8291 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008292 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008293 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8294 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8295 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8296 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008297 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008298 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008299 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008300 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008301 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008302 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008303 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008304
8305 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008306 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008307}
8308
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008309SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8310 SDValue N0 = Op.getOperand(0);
8311 DebugLoc dl = Op.getDebugLoc();
8312 EVT VT = Op.getValueType();
8313
8314 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8315 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8316 DAG.getConstant(1, VT));
8317 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8318}
8319
Dan Gohman076aee32009-03-04 19:44:21 +00008320/// Emit nodes that will be selected as "test Op0,Op0", or something
8321/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008322SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008323 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008324 DebugLoc dl = Op.getDebugLoc();
8325
Dan Gohman31125812009-03-07 01:58:32 +00008326 // CF and OF aren't always set the way we want. Determine which
8327 // of these we need.
8328 bool NeedCF = false;
8329 bool NeedOF = false;
8330 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008331 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008332 case X86::COND_A: case X86::COND_AE:
8333 case X86::COND_B: case X86::COND_BE:
8334 NeedCF = true;
8335 break;
8336 case X86::COND_G: case X86::COND_GE:
8337 case X86::COND_L: case X86::COND_LE:
8338 case X86::COND_O: case X86::COND_NO:
8339 NeedOF = true;
8340 break;
Dan Gohman31125812009-03-07 01:58:32 +00008341 }
8342
Dan Gohman076aee32009-03-04 19:44:21 +00008343 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008344 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8345 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008346 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8347 // Emit a CMP with 0, which is the TEST pattern.
8348 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8349 DAG.getConstant(0, Op.getValueType()));
8350
8351 unsigned Opcode = 0;
8352 unsigned NumOperands = 0;
8353 switch (Op.getNode()->getOpcode()) {
8354 case ISD::ADD:
8355 // Due to an isel shortcoming, be conservative if this add is likely to be
8356 // selected as part of a load-modify-store instruction. When the root node
8357 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8358 // uses of other nodes in the match, such as the ADD in this case. This
8359 // leads to the ADD being left around and reselected, with the result being
8360 // two adds in the output. Alas, even if none our users are stores, that
8361 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8362 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8363 // climbing the DAG back to the root, and it doesn't seem to be worth the
8364 // effort.
8365 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008366 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8367 if (UI->getOpcode() != ISD::CopyToReg &&
8368 UI->getOpcode() != ISD::SETCC &&
8369 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008370 goto default_case;
8371
8372 if (ConstantSDNode *C =
8373 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8374 // An add of one will be selected as an INC.
8375 if (C->getAPIntValue() == 1) {
8376 Opcode = X86ISD::INC;
8377 NumOperands = 1;
8378 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008379 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008380
8381 // An add of negative one (subtract of one) will be selected as a DEC.
8382 if (C->getAPIntValue().isAllOnesValue()) {
8383 Opcode = X86ISD::DEC;
8384 NumOperands = 1;
8385 break;
8386 }
Dan Gohman076aee32009-03-04 19:44:21 +00008387 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008388
8389 // Otherwise use a regular EFLAGS-setting add.
8390 Opcode = X86ISD::ADD;
8391 NumOperands = 2;
8392 break;
8393 case ISD::AND: {
8394 // If the primary and result isn't used, don't bother using X86ISD::AND,
8395 // because a TEST instruction will be better.
8396 bool NonFlagUse = false;
8397 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8398 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8399 SDNode *User = *UI;
8400 unsigned UOpNo = UI.getOperandNo();
8401 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8402 // Look pass truncate.
8403 UOpNo = User->use_begin().getOperandNo();
8404 User = *User->use_begin();
8405 }
8406
8407 if (User->getOpcode() != ISD::BRCOND &&
8408 User->getOpcode() != ISD::SETCC &&
8409 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8410 NonFlagUse = true;
8411 break;
8412 }
Dan Gohman076aee32009-03-04 19:44:21 +00008413 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008414
8415 if (!NonFlagUse)
8416 break;
8417 }
8418 // FALL THROUGH
8419 case ISD::SUB:
8420 case ISD::OR:
8421 case ISD::XOR:
8422 // Due to the ISEL shortcoming noted above, be conservative if this op is
8423 // likely to be selected as part of a load-modify-store instruction.
8424 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8425 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8426 if (UI->getOpcode() == ISD::STORE)
8427 goto default_case;
8428
8429 // Otherwise use a regular EFLAGS-setting instruction.
8430 switch (Op.getNode()->getOpcode()) {
8431 default: llvm_unreachable("unexpected operator!");
8432 case ISD::SUB: Opcode = X86ISD::SUB; break;
8433 case ISD::OR: Opcode = X86ISD::OR; break;
8434 case ISD::XOR: Opcode = X86ISD::XOR; break;
8435 case ISD::AND: Opcode = X86ISD::AND; break;
8436 }
8437
8438 NumOperands = 2;
8439 break;
8440 case X86ISD::ADD:
8441 case X86ISD::SUB:
8442 case X86ISD::INC:
8443 case X86ISD::DEC:
8444 case X86ISD::OR:
8445 case X86ISD::XOR:
8446 case X86ISD::AND:
8447 return SDValue(Op.getNode(), 1);
8448 default:
8449 default_case:
8450 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008451 }
8452
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008453 if (Opcode == 0)
8454 // Emit a CMP with 0, which is the TEST pattern.
8455 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8456 DAG.getConstant(0, Op.getValueType()));
8457
8458 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8459 SmallVector<SDValue, 4> Ops;
8460 for (unsigned i = 0; i != NumOperands; ++i)
8461 Ops.push_back(Op.getOperand(i));
8462
8463 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8464 DAG.ReplaceAllUsesWith(Op, New);
8465 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008466}
8467
8468/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8469/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008470SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008471 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008472 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8473 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008474 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008475
8476 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008477 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008478}
8479
Evan Chengd40d03e2010-01-06 19:38:29 +00008480/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8481/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008482SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8483 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008484 SDValue Op0 = And.getOperand(0);
8485 SDValue Op1 = And.getOperand(1);
8486 if (Op0.getOpcode() == ISD::TRUNCATE)
8487 Op0 = Op0.getOperand(0);
8488 if (Op1.getOpcode() == ISD::TRUNCATE)
8489 Op1 = Op1.getOperand(0);
8490
Evan Chengd40d03e2010-01-06 19:38:29 +00008491 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008492 if (Op1.getOpcode() == ISD::SHL)
8493 std::swap(Op0, Op1);
8494 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008495 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8496 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008497 // If we looked past a truncate, check that it's only truncating away
8498 // known zeros.
8499 unsigned BitWidth = Op0.getValueSizeInBits();
8500 unsigned AndBitWidth = And.getValueSizeInBits();
8501 if (BitWidth > AndBitWidth) {
8502 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8503 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8504 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8505 return SDValue();
8506 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008507 LHS = Op1;
8508 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008509 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008510 } else if (Op1.getOpcode() == ISD::Constant) {
8511 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8512 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008513 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8514 LHS = AndLHS.getOperand(0);
8515 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008516 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008517 }
Evan Cheng0488db92007-09-25 01:57:46 +00008518
Evan Chengd40d03e2010-01-06 19:38:29 +00008519 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008520 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008521 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008522 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008523 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008524 // Also promote i16 to i32 for performance / code size reason.
8525 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008526 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008527 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008528
Evan Chengd40d03e2010-01-06 19:38:29 +00008529 // If the operand types disagree, extend the shift amount to match. Since
8530 // BT ignores high bits (like shifts) we can use anyextend.
8531 if (LHS.getValueType() != RHS.getValueType())
8532 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008533
Evan Chengd40d03e2010-01-06 19:38:29 +00008534 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8535 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8536 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8537 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008538 }
8539
Evan Cheng54de3ea2010-01-05 06:52:31 +00008540 return SDValue();
8541}
8542
Dan Gohmand858e902010-04-17 15:26:15 +00008543SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008544
8545 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8546
Evan Cheng54de3ea2010-01-05 06:52:31 +00008547 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8548 SDValue Op0 = Op.getOperand(0);
8549 SDValue Op1 = Op.getOperand(1);
8550 DebugLoc dl = Op.getDebugLoc();
8551 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8552
8553 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008554 // Lower (X & (1 << N)) == 0 to BT(X, N).
8555 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8556 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008557 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008558 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008559 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008560 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8561 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8562 if (NewSetCC.getNode())
8563 return NewSetCC;
8564 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008565
Chris Lattner481eebc2010-12-19 21:23:48 +00008566 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8567 // these.
8568 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008569 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008570 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8571 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008572
Chris Lattner481eebc2010-12-19 21:23:48 +00008573 // If the input is a setcc, then reuse the input setcc or use a new one with
8574 // the inverted condition.
8575 if (Op0.getOpcode() == X86ISD::SETCC) {
8576 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8577 bool Invert = (CC == ISD::SETNE) ^
8578 cast<ConstantSDNode>(Op1)->isNullValue();
8579 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008580
Evan Cheng2c755ba2010-02-27 07:36:59 +00008581 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008582 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8583 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8584 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008585 }
8586
Evan Chenge5b51ac2010-04-17 06:13:15 +00008587 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008588 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008589 if (X86CC == X86::COND_INVALID)
8590 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008591
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008592 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008593 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008594 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008595}
8596
Craig Topper89af15e2011-09-18 08:03:58 +00008597// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008598// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008599static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008600 EVT VT = Op.getValueType();
8601
Duncan Sands28b77e92011-09-06 19:07:46 +00008602 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008603 "Unsupported value type for operation");
8604
8605 int NumElems = VT.getVectorNumElements();
8606 DebugLoc dl = Op.getDebugLoc();
8607 SDValue CC = Op.getOperand(2);
8608 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8609 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8610
8611 // Extract the LHS vectors
8612 SDValue LHS = Op.getOperand(0);
8613 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8614 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8615
8616 // Extract the RHS vectors
8617 SDValue RHS = Op.getOperand(1);
8618 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8619 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8620
8621 // Issue the operation on the smaller types and concatenate the result back
8622 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8623 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8624 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8625 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8626 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8627}
8628
8629
Dan Gohmand858e902010-04-17 15:26:15 +00008630SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008631 SDValue Cond;
8632 SDValue Op0 = Op.getOperand(0);
8633 SDValue Op1 = Op.getOperand(1);
8634 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008635 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008636 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8637 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008638 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008639
8640 if (isFP) {
8641 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008642 EVT EltVT = Op0.getValueType().getVectorElementType();
8643 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8644
8645 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008646 bool Swap = false;
8647
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008648 // SSE Condition code mapping:
8649 // 0 - EQ
8650 // 1 - LT
8651 // 2 - LE
8652 // 3 - UNORD
8653 // 4 - NEQ
8654 // 5 - NLT
8655 // 6 - NLE
8656 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008657 switch (SetCCOpcode) {
8658 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008659 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008660 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008661 case ISD::SETOGT:
8662 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008663 case ISD::SETLT:
8664 case ISD::SETOLT: SSECC = 1; break;
8665 case ISD::SETOGE:
8666 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008667 case ISD::SETLE:
8668 case ISD::SETOLE: SSECC = 2; break;
8669 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008670 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008671 case ISD::SETNE: SSECC = 4; break;
8672 case ISD::SETULE: Swap = true;
8673 case ISD::SETUGE: SSECC = 5; break;
8674 case ISD::SETULT: Swap = true;
8675 case ISD::SETUGT: SSECC = 6; break;
8676 case ISD::SETO: SSECC = 7; break;
8677 }
8678 if (Swap)
8679 std::swap(Op0, Op1);
8680
Nate Begemanfb8ead02008-07-25 19:05:58 +00008681 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008682 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008683 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008684 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008685 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8686 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008687 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Craig Topper0a150352011-11-09 08:06:13 +00008688 } else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008689 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008690 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8691 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008692 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008693 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008694 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008695 }
8696 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008697 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008698 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008699
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008700 // Break 256-bit integer vector compare into smaller ones.
Craig Topper0a150352011-11-09 08:06:13 +00008701 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008702 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008703
Nate Begeman30a0de92008-07-17 16:51:19 +00008704 // We are handling one of the integer comparisons here. Since SSE only has
8705 // GT and EQ comparisons for integer, swapping operands and multiple
8706 // operations may be required for some comparisons.
8707 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8708 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008709
Craig Topper0a150352011-11-09 08:06:13 +00008710 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008711 default: break;
Craig Topper0a150352011-11-09 08:06:13 +00008712 case MVT::i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
8713 case MVT::i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
8714 case MVT::i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8715 case MVT::i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008716 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008717
Nate Begeman30a0de92008-07-17 16:51:19 +00008718 switch (SetCCOpcode) {
8719 default: break;
8720 case ISD::SETNE: Invert = true;
8721 case ISD::SETEQ: Opc = EQOpc; break;
8722 case ISD::SETLT: Swap = true;
8723 case ISD::SETGT: Opc = GTOpc; break;
8724 case ISD::SETGE: Swap = true;
8725 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8726 case ISD::SETULT: Swap = true;
8727 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8728 case ISD::SETUGE: Swap = true;
8729 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8730 }
8731 if (Swap)
8732 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008733
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008734 // Check that the operation in question is available (most are plain SSE2,
8735 // but PCMPGTQ and PCMPEQQ have different requirements).
8736 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42() && !Subtarget->hasAVX())
8737 return SDValue();
8738 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41() && !Subtarget->hasAVX())
8739 return SDValue();
8740
Nate Begeman30a0de92008-07-17 16:51:19 +00008741 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8742 // bits of the inputs before performing those operations.
8743 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008744 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008745 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8746 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008747 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008748 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8749 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008750 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8751 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008752 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008753
Dale Johannesenace16102009-02-03 19:33:06 +00008754 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008755
8756 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008757 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008758 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008759
Nate Begeman30a0de92008-07-17 16:51:19 +00008760 return Result;
8761}
Evan Cheng0488db92007-09-25 01:57:46 +00008762
Evan Cheng370e5342008-12-03 08:38:43 +00008763// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008764static bool isX86LogicalCmp(SDValue Op) {
8765 unsigned Opc = Op.getNode()->getOpcode();
8766 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8767 return true;
8768 if (Op.getResNo() == 1 &&
8769 (Opc == X86ISD::ADD ||
8770 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008771 Opc == X86ISD::ADC ||
8772 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008773 Opc == X86ISD::SMUL ||
8774 Opc == X86ISD::UMUL ||
8775 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008776 Opc == X86ISD::DEC ||
8777 Opc == X86ISD::OR ||
8778 Opc == X86ISD::XOR ||
8779 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008780 return true;
8781
Chris Lattner9637d5b2010-12-05 07:49:54 +00008782 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8783 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008784
Dan Gohman076aee32009-03-04 19:44:21 +00008785 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008786}
8787
Chris Lattnera2b56002010-12-05 01:23:24 +00008788static bool isZero(SDValue V) {
8789 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8790 return C && C->isNullValue();
8791}
8792
Chris Lattner96908b12010-12-05 02:00:51 +00008793static bool isAllOnes(SDValue V) {
8794 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8795 return C && C->isAllOnesValue();
8796}
8797
Dan Gohmand858e902010-04-17 15:26:15 +00008798SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008799 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008800 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008801 SDValue Op1 = Op.getOperand(1);
8802 SDValue Op2 = Op.getOperand(2);
8803 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008804 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008805
Dan Gohman1a492952009-10-20 16:22:37 +00008806 if (Cond.getOpcode() == ISD::SETCC) {
8807 SDValue NewCond = LowerSETCC(Cond, DAG);
8808 if (NewCond.getNode())
8809 Cond = NewCond;
8810 }
Evan Cheng734503b2006-09-11 02:19:56 +00008811
Chris Lattnera2b56002010-12-05 01:23:24 +00008812 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008813 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008814 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008815 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008816 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008817 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8818 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008819 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008820
Chris Lattnera2b56002010-12-05 01:23:24 +00008821 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008822
8823 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008824 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8825 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008826
8827 SDValue CmpOp0 = Cmp.getOperand(0);
8828 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8829 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008830
Chris Lattner96908b12010-12-05 02:00:51 +00008831 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008832 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8833 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008834
Chris Lattner96908b12010-12-05 02:00:51 +00008835 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8836 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008837
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008838 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008839 if (N2C == 0 || !N2C->isNullValue())
8840 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8841 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008842 }
8843 }
8844
Chris Lattnera2b56002010-12-05 01:23:24 +00008845 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008846 if (Cond.getOpcode() == ISD::AND &&
8847 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8848 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008849 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008850 Cond = Cond.getOperand(0);
8851 }
8852
Evan Cheng3f41d662007-10-08 22:16:29 +00008853 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8854 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008855 unsigned CondOpcode = Cond.getOpcode();
8856 if (CondOpcode == X86ISD::SETCC ||
8857 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008858 CC = Cond.getOperand(0);
8859
Dan Gohman475871a2008-07-27 21:46:04 +00008860 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008861 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008862 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008863
Evan Cheng3f41d662007-10-08 22:16:29 +00008864 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008865 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008866 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008867 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008868
Chris Lattnerd1980a52009-03-12 06:52:53 +00008869 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8870 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008871 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008872 addTest = false;
8873 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008874 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8875 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8876 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8877 Cond.getOperand(0).getValueType() != MVT::i8)) {
8878 SDValue LHS = Cond.getOperand(0);
8879 SDValue RHS = Cond.getOperand(1);
8880 unsigned X86Opcode;
8881 unsigned X86Cond;
8882 SDVTList VTs;
8883 switch (CondOpcode) {
8884 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8885 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8886 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8887 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8888 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8889 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8890 default: llvm_unreachable("unexpected overflowing operator");
8891 }
8892 if (CondOpcode == ISD::UMULO)
8893 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8894 MVT::i32);
8895 else
8896 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8897
8898 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8899
8900 if (CondOpcode == ISD::UMULO)
8901 Cond = X86Op.getValue(2);
8902 else
8903 Cond = X86Op.getValue(1);
8904
8905 CC = DAG.getConstant(X86Cond, MVT::i8);
8906 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008907 }
8908
8909 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008910 // Look pass the truncate.
8911 if (Cond.getOpcode() == ISD::TRUNCATE)
8912 Cond = Cond.getOperand(0);
8913
8914 // We know the result of AND is compared against zero. Try to match
8915 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008916 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008917 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008918 if (NewSetCC.getNode()) {
8919 CC = NewSetCC.getOperand(0);
8920 Cond = NewSetCC.getOperand(1);
8921 addTest = false;
8922 }
8923 }
8924 }
8925
8926 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008927 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008928 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008929 }
8930
Benjamin Kramere915ff32010-12-22 23:09:28 +00008931 // a < b ? -1 : 0 -> RES = ~setcc_carry
8932 // a < b ? 0 : -1 -> RES = setcc_carry
8933 // a >= b ? -1 : 0 -> RES = setcc_carry
8934 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8935 if (Cond.getOpcode() == X86ISD::CMP) {
8936 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8937
8938 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8939 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8940 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8941 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8942 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8943 return DAG.getNOT(DL, Res, Res.getValueType());
8944 return Res;
8945 }
8946 }
8947
Evan Cheng0488db92007-09-25 01:57:46 +00008948 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8949 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008950 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008951 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008952 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008953}
8954
Evan Cheng370e5342008-12-03 08:38:43 +00008955// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8956// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8957// from the AND / OR.
8958static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8959 Opc = Op.getOpcode();
8960 if (Opc != ISD::OR && Opc != ISD::AND)
8961 return false;
8962 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8963 Op.getOperand(0).hasOneUse() &&
8964 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8965 Op.getOperand(1).hasOneUse());
8966}
8967
Evan Cheng961d6d42009-02-02 08:19:07 +00008968// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8969// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008970static bool isXor1OfSetCC(SDValue Op) {
8971 if (Op.getOpcode() != ISD::XOR)
8972 return false;
8973 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8974 if (N1C && N1C->getAPIntValue() == 1) {
8975 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8976 Op.getOperand(0).hasOneUse();
8977 }
8978 return false;
8979}
8980
Dan Gohmand858e902010-04-17 15:26:15 +00008981SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008982 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008983 SDValue Chain = Op.getOperand(0);
8984 SDValue Cond = Op.getOperand(1);
8985 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008986 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008987 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008988 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008989
Dan Gohman1a492952009-10-20 16:22:37 +00008990 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008991 // Check for setcc([su]{add,sub,mul}o == 0).
8992 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8993 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8994 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8995 Cond.getOperand(0).getResNo() == 1 &&
8996 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
8997 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
8998 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
8999 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9000 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9001 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9002 Inverted = true;
9003 Cond = Cond.getOperand(0);
9004 } else {
9005 SDValue NewCond = LowerSETCC(Cond, DAG);
9006 if (NewCond.getNode())
9007 Cond = NewCond;
9008 }
Dan Gohman1a492952009-10-20 16:22:37 +00009009 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009010#if 0
9011 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009012 else if (Cond.getOpcode() == X86ISD::ADD ||
9013 Cond.getOpcode() == X86ISD::SUB ||
9014 Cond.getOpcode() == X86ISD::SMUL ||
9015 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009016 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009017#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009018
Evan Chengad9c0a32009-12-15 00:53:42 +00009019 // Look pass (and (setcc_carry (cmp ...)), 1).
9020 if (Cond.getOpcode() == ISD::AND &&
9021 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9022 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009023 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009024 Cond = Cond.getOperand(0);
9025 }
9026
Evan Cheng3f41d662007-10-08 22:16:29 +00009027 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9028 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009029 unsigned CondOpcode = Cond.getOpcode();
9030 if (CondOpcode == X86ISD::SETCC ||
9031 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009032 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009033
Dan Gohman475871a2008-07-27 21:46:04 +00009034 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009035 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009036 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009037 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009038 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009039 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009040 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009041 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009042 default: break;
9043 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009044 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009045 // These can only come from an arithmetic instruction with overflow,
9046 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009047 Cond = Cond.getNode()->getOperand(1);
9048 addTest = false;
9049 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009050 }
Evan Cheng0488db92007-09-25 01:57:46 +00009051 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009052 }
9053 CondOpcode = Cond.getOpcode();
9054 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9055 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9056 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9057 Cond.getOperand(0).getValueType() != MVT::i8)) {
9058 SDValue LHS = Cond.getOperand(0);
9059 SDValue RHS = Cond.getOperand(1);
9060 unsigned X86Opcode;
9061 unsigned X86Cond;
9062 SDVTList VTs;
9063 switch (CondOpcode) {
9064 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9065 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9066 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9067 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9068 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9069 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9070 default: llvm_unreachable("unexpected overflowing operator");
9071 }
9072 if (Inverted)
9073 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9074 if (CondOpcode == ISD::UMULO)
9075 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9076 MVT::i32);
9077 else
9078 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9079
9080 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9081
9082 if (CondOpcode == ISD::UMULO)
9083 Cond = X86Op.getValue(2);
9084 else
9085 Cond = X86Op.getValue(1);
9086
9087 CC = DAG.getConstant(X86Cond, MVT::i8);
9088 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009089 } else {
9090 unsigned CondOpc;
9091 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9092 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009093 if (CondOpc == ISD::OR) {
9094 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9095 // two branches instead of an explicit OR instruction with a
9096 // separate test.
9097 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009098 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009099 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009100 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009101 Chain, Dest, CC, Cmp);
9102 CC = Cond.getOperand(1).getOperand(0);
9103 Cond = Cmp;
9104 addTest = false;
9105 }
9106 } else { // ISD::AND
9107 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9108 // two branches instead of an explicit AND instruction with a
9109 // separate test. However, we only do this if this block doesn't
9110 // have a fall-through edge, because this requires an explicit
9111 // jmp when the condition is false.
9112 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009113 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009114 Op.getNode()->hasOneUse()) {
9115 X86::CondCode CCode =
9116 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9117 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009118 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009119 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009120 // Look for an unconditional branch following this conditional branch.
9121 // We need this because we need to reverse the successors in order
9122 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009123 if (User->getOpcode() == ISD::BR) {
9124 SDValue FalseBB = User->getOperand(1);
9125 SDNode *NewBR =
9126 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009127 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009128 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009129 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009130
Dale Johannesene4d209d2009-02-03 20:21:25 +00009131 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009132 Chain, Dest, CC, Cmp);
9133 X86::CondCode CCode =
9134 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9135 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009136 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009137 Cond = Cmp;
9138 addTest = false;
9139 }
9140 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009141 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009142 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9143 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9144 // It should be transformed during dag combiner except when the condition
9145 // is set by a arithmetics with overflow node.
9146 X86::CondCode CCode =
9147 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9148 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009149 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009150 Cond = Cond.getOperand(0).getOperand(1);
9151 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009152 } else if (Cond.getOpcode() == ISD::SETCC &&
9153 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9154 // For FCMP_OEQ, we can emit
9155 // two branches instead of an explicit AND instruction with a
9156 // separate test. However, we only do this if this block doesn't
9157 // have a fall-through edge, because this requires an explicit
9158 // jmp when the condition is false.
9159 if (Op.getNode()->hasOneUse()) {
9160 SDNode *User = *Op.getNode()->use_begin();
9161 // Look for an unconditional branch following this conditional branch.
9162 // We need this because we need to reverse the successors in order
9163 // to implement FCMP_OEQ.
9164 if (User->getOpcode() == ISD::BR) {
9165 SDValue FalseBB = User->getOperand(1);
9166 SDNode *NewBR =
9167 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9168 assert(NewBR == User);
9169 (void)NewBR;
9170 Dest = FalseBB;
9171
9172 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9173 Cond.getOperand(0), Cond.getOperand(1));
9174 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9175 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9176 Chain, Dest, CC, Cmp);
9177 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9178 Cond = Cmp;
9179 addTest = false;
9180 }
9181 }
9182 } else if (Cond.getOpcode() == ISD::SETCC &&
9183 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9184 // For FCMP_UNE, we can emit
9185 // two branches instead of an explicit AND instruction with a
9186 // separate test. However, we only do this if this block doesn't
9187 // have a fall-through edge, because this requires an explicit
9188 // jmp when the condition is false.
9189 if (Op.getNode()->hasOneUse()) {
9190 SDNode *User = *Op.getNode()->use_begin();
9191 // Look for an unconditional branch following this conditional branch.
9192 // We need this because we need to reverse the successors in order
9193 // to implement FCMP_UNE.
9194 if (User->getOpcode() == ISD::BR) {
9195 SDValue FalseBB = User->getOperand(1);
9196 SDNode *NewBR =
9197 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9198 assert(NewBR == User);
9199 (void)NewBR;
9200
9201 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9202 Cond.getOperand(0), Cond.getOperand(1));
9203 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9204 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9205 Chain, Dest, CC, Cmp);
9206 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9207 Cond = Cmp;
9208 addTest = false;
9209 Dest = FalseBB;
9210 }
9211 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009212 }
Evan Cheng0488db92007-09-25 01:57:46 +00009213 }
9214
9215 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009216 // Look pass the truncate.
9217 if (Cond.getOpcode() == ISD::TRUNCATE)
9218 Cond = Cond.getOperand(0);
9219
9220 // We know the result of AND is compared against zero. Try to match
9221 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009222 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009223 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9224 if (NewSetCC.getNode()) {
9225 CC = NewSetCC.getOperand(0);
9226 Cond = NewSetCC.getOperand(1);
9227 addTest = false;
9228 }
9229 }
9230 }
9231
9232 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009233 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009234 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009235 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00009236 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009237 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009238}
9239
Anton Korobeynikove060b532007-04-17 19:34:00 +00009240
9241// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9242// Calls to _alloca is needed to probe the stack when allocating more than 4k
9243// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9244// that the guard pages used by the OS virtual memory manager are allocated in
9245// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009246SDValue
9247X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009248 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009249 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
9250 EnableSegmentedStacks) &&
9251 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009252 "are being used");
9253 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009254 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009255
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009256 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009257 SDValue Chain = Op.getOperand(0);
9258 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009259 // FIXME: Ensure alignment here
9260
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009261 bool Is64Bit = Subtarget->is64Bit();
9262 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009263
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009264 if (EnableSegmentedStacks) {
9265 MachineFunction &MF = DAG.getMachineFunction();
9266 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009267
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009268 if (Is64Bit) {
9269 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009270 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009271 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009272
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009273 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
9274 I != E; I++)
9275 if (I->hasNestAttr())
9276 report_fatal_error("Cannot use segmented stacks with functions that "
9277 "have nested arguments.");
9278 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009279
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009280 const TargetRegisterClass *AddrRegClass =
9281 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9282 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9283 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9284 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9285 DAG.getRegister(Vreg, SPTy));
9286 SDValue Ops1[2] = { Value, Chain };
9287 return DAG.getMergeValues(Ops1, 2, dl);
9288 } else {
9289 SDValue Flag;
9290 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009291
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009292 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9293 Flag = Chain.getValue(1);
9294 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009295
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009296 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9297 Flag = Chain.getValue(1);
9298
9299 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9300
9301 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9302 return DAG.getMergeValues(Ops1, 2, dl);
9303 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009304}
9305
Dan Gohmand858e902010-04-17 15:26:15 +00009306SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009307 MachineFunction &MF = DAG.getMachineFunction();
9308 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9309
Dan Gohman69de1932008-02-06 22:27:42 +00009310 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009311 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009312
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009313 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009314 // vastart just stores the address of the VarArgsFrameIndex slot into the
9315 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009316 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9317 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009318 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9319 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009320 }
9321
9322 // __va_list_tag:
9323 // gp_offset (0 - 6 * 8)
9324 // fp_offset (48 - 48 + 8 * 16)
9325 // overflow_arg_area (point to parameters coming in memory).
9326 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009327 SmallVector<SDValue, 8> MemOps;
9328 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009329 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009330 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009331 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9332 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009333 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009334 MemOps.push_back(Store);
9335
9336 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009337 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009338 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009339 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009340 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9341 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009342 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009343 MemOps.push_back(Store);
9344
9345 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009346 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009347 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009348 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9349 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009350 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9351 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009352 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009353 MemOps.push_back(Store);
9354
9355 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009356 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009357 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009358 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9359 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009360 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9361 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009362 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009363 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009364 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009365}
9366
Dan Gohmand858e902010-04-17 15:26:15 +00009367SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009368 assert(Subtarget->is64Bit() &&
9369 "LowerVAARG only handles 64-bit va_arg!");
9370 assert((Subtarget->isTargetLinux() ||
9371 Subtarget->isTargetDarwin()) &&
9372 "Unhandled target in LowerVAARG");
9373 assert(Op.getNode()->getNumOperands() == 4);
9374 SDValue Chain = Op.getOperand(0);
9375 SDValue SrcPtr = Op.getOperand(1);
9376 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9377 unsigned Align = Op.getConstantOperandVal(3);
9378 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009379
Dan Gohman320afb82010-10-12 18:00:49 +00009380 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009381 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009382 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9383 uint8_t ArgMode;
9384
9385 // Decide which area this value should be read from.
9386 // TODO: Implement the AMD64 ABI in its entirety. This simple
9387 // selection mechanism works only for the basic types.
9388 if (ArgVT == MVT::f80) {
9389 llvm_unreachable("va_arg for f80 not yet implemented");
9390 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9391 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9392 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9393 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9394 } else {
9395 llvm_unreachable("Unhandled argument type in LowerVAARG");
9396 }
9397
9398 if (ArgMode == 2) {
9399 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009400 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009401 !(DAG.getMachineFunction()
9402 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009403 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009404 }
9405
9406 // Insert VAARG_64 node into the DAG
9407 // VAARG_64 returns two values: Variable Argument Address, Chain
9408 SmallVector<SDValue, 11> InstOps;
9409 InstOps.push_back(Chain);
9410 InstOps.push_back(SrcPtr);
9411 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9412 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9413 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9414 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9415 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9416 VTs, &InstOps[0], InstOps.size(),
9417 MVT::i64,
9418 MachinePointerInfo(SV),
9419 /*Align=*/0,
9420 /*Volatile=*/false,
9421 /*ReadMem=*/true,
9422 /*WriteMem=*/true);
9423 Chain = VAARG.getValue(1);
9424
9425 // Load the next argument and return it
9426 return DAG.getLoad(ArgVT, dl,
9427 Chain,
9428 VAARG,
9429 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009430 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009431}
9432
Dan Gohmand858e902010-04-17 15:26:15 +00009433SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009434 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009435 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009436 SDValue Chain = Op.getOperand(0);
9437 SDValue DstPtr = Op.getOperand(1);
9438 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009439 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9440 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009441 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009442
Chris Lattnere72f2022010-09-21 05:40:29 +00009443 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009444 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009445 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009446 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009447}
9448
Dan Gohman475871a2008-07-27 21:46:04 +00009449SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009450X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009451 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009452 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009453 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009454 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009455 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009456 case Intrinsic::x86_sse_comieq_ss:
9457 case Intrinsic::x86_sse_comilt_ss:
9458 case Intrinsic::x86_sse_comile_ss:
9459 case Intrinsic::x86_sse_comigt_ss:
9460 case Intrinsic::x86_sse_comige_ss:
9461 case Intrinsic::x86_sse_comineq_ss:
9462 case Intrinsic::x86_sse_ucomieq_ss:
9463 case Intrinsic::x86_sse_ucomilt_ss:
9464 case Intrinsic::x86_sse_ucomile_ss:
9465 case Intrinsic::x86_sse_ucomigt_ss:
9466 case Intrinsic::x86_sse_ucomige_ss:
9467 case Intrinsic::x86_sse_ucomineq_ss:
9468 case Intrinsic::x86_sse2_comieq_sd:
9469 case Intrinsic::x86_sse2_comilt_sd:
9470 case Intrinsic::x86_sse2_comile_sd:
9471 case Intrinsic::x86_sse2_comigt_sd:
9472 case Intrinsic::x86_sse2_comige_sd:
9473 case Intrinsic::x86_sse2_comineq_sd:
9474 case Intrinsic::x86_sse2_ucomieq_sd:
9475 case Intrinsic::x86_sse2_ucomilt_sd:
9476 case Intrinsic::x86_sse2_ucomile_sd:
9477 case Intrinsic::x86_sse2_ucomigt_sd:
9478 case Intrinsic::x86_sse2_ucomige_sd:
9479 case Intrinsic::x86_sse2_ucomineq_sd: {
9480 unsigned Opc = 0;
9481 ISD::CondCode CC = ISD::SETCC_INVALID;
9482 switch (IntNo) {
9483 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009484 case Intrinsic::x86_sse_comieq_ss:
9485 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009486 Opc = X86ISD::COMI;
9487 CC = ISD::SETEQ;
9488 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009489 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009490 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009491 Opc = X86ISD::COMI;
9492 CC = ISD::SETLT;
9493 break;
9494 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009495 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009496 Opc = X86ISD::COMI;
9497 CC = ISD::SETLE;
9498 break;
9499 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009500 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009501 Opc = X86ISD::COMI;
9502 CC = ISD::SETGT;
9503 break;
9504 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009505 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009506 Opc = X86ISD::COMI;
9507 CC = ISD::SETGE;
9508 break;
9509 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009510 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009511 Opc = X86ISD::COMI;
9512 CC = ISD::SETNE;
9513 break;
9514 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009515 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009516 Opc = X86ISD::UCOMI;
9517 CC = ISD::SETEQ;
9518 break;
9519 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009520 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009521 Opc = X86ISD::UCOMI;
9522 CC = ISD::SETLT;
9523 break;
9524 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009525 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009526 Opc = X86ISD::UCOMI;
9527 CC = ISD::SETLE;
9528 break;
9529 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009530 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009531 Opc = X86ISD::UCOMI;
9532 CC = ISD::SETGT;
9533 break;
9534 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009535 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009536 Opc = X86ISD::UCOMI;
9537 CC = ISD::SETGE;
9538 break;
9539 case Intrinsic::x86_sse_ucomineq_ss:
9540 case Intrinsic::x86_sse2_ucomineq_sd:
9541 Opc = X86ISD::UCOMI;
9542 CC = ISD::SETNE;
9543 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009544 }
Evan Cheng734503b2006-09-11 02:19:56 +00009545
Dan Gohman475871a2008-07-27 21:46:04 +00009546 SDValue LHS = Op.getOperand(1);
9547 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009548 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009549 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009550 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9551 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9552 DAG.getConstant(X86CC, MVT::i8), Cond);
9553 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009554 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009555 // Arithmetic intrinsics.
9556 case Intrinsic::x86_sse3_hadd_ps:
9557 case Intrinsic::x86_sse3_hadd_pd:
9558 case Intrinsic::x86_avx_hadd_ps_256:
9559 case Intrinsic::x86_avx_hadd_pd_256:
9560 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9561 Op.getOperand(1), Op.getOperand(2));
9562 case Intrinsic::x86_sse3_hsub_ps:
9563 case Intrinsic::x86_sse3_hsub_pd:
9564 case Intrinsic::x86_avx_hsub_ps_256:
9565 case Intrinsic::x86_avx_hsub_pd_256:
9566 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9567 Op.getOperand(1), Op.getOperand(2));
Craig Topper98fc7292011-11-19 17:46:46 +00009568 case Intrinsic::x86_avx2_psllv_d:
9569 case Intrinsic::x86_avx2_psllv_q:
9570 case Intrinsic::x86_avx2_psllv_d_256:
9571 case Intrinsic::x86_avx2_psllv_q_256:
9572 return DAG.getNode(ISD::SHL, dl, Op.getValueType(),
9573 Op.getOperand(1), Op.getOperand(2));
9574 case Intrinsic::x86_avx2_psrlv_d:
9575 case Intrinsic::x86_avx2_psrlv_q:
9576 case Intrinsic::x86_avx2_psrlv_d_256:
9577 case Intrinsic::x86_avx2_psrlv_q_256:
9578 return DAG.getNode(ISD::SRL, dl, Op.getValueType(),
9579 Op.getOperand(1), Op.getOperand(2));
9580 case Intrinsic::x86_avx2_psrav_d:
9581 case Intrinsic::x86_avx2_psrav_d_256:
9582 return DAG.getNode(ISD::SRA, dl, Op.getValueType(),
9583 Op.getOperand(1), Op.getOperand(2));
9584
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009585 // ptest and testp intrinsics. The intrinsic these come from are designed to
9586 // return an integer value, not just an instruction so lower it to the ptest
9587 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009588 case Intrinsic::x86_sse41_ptestz:
9589 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009590 case Intrinsic::x86_sse41_ptestnzc:
9591 case Intrinsic::x86_avx_ptestz_256:
9592 case Intrinsic::x86_avx_ptestc_256:
9593 case Intrinsic::x86_avx_ptestnzc_256:
9594 case Intrinsic::x86_avx_vtestz_ps:
9595 case Intrinsic::x86_avx_vtestc_ps:
9596 case Intrinsic::x86_avx_vtestnzc_ps:
9597 case Intrinsic::x86_avx_vtestz_pd:
9598 case Intrinsic::x86_avx_vtestc_pd:
9599 case Intrinsic::x86_avx_vtestnzc_pd:
9600 case Intrinsic::x86_avx_vtestz_ps_256:
9601 case Intrinsic::x86_avx_vtestc_ps_256:
9602 case Intrinsic::x86_avx_vtestnzc_ps_256:
9603 case Intrinsic::x86_avx_vtestz_pd_256:
9604 case Intrinsic::x86_avx_vtestc_pd_256:
9605 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9606 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009607 unsigned X86CC = 0;
9608 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009609 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009610 case Intrinsic::x86_avx_vtestz_ps:
9611 case Intrinsic::x86_avx_vtestz_pd:
9612 case Intrinsic::x86_avx_vtestz_ps_256:
9613 case Intrinsic::x86_avx_vtestz_pd_256:
9614 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009615 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009616 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009617 // ZF = 1
9618 X86CC = X86::COND_E;
9619 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009620 case Intrinsic::x86_avx_vtestc_ps:
9621 case Intrinsic::x86_avx_vtestc_pd:
9622 case Intrinsic::x86_avx_vtestc_ps_256:
9623 case Intrinsic::x86_avx_vtestc_pd_256:
9624 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009625 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009626 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009627 // CF = 1
9628 X86CC = X86::COND_B;
9629 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009630 case Intrinsic::x86_avx_vtestnzc_ps:
9631 case Intrinsic::x86_avx_vtestnzc_pd:
9632 case Intrinsic::x86_avx_vtestnzc_ps_256:
9633 case Intrinsic::x86_avx_vtestnzc_pd_256:
9634 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009635 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009636 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009637 // ZF and CF = 0
9638 X86CC = X86::COND_A;
9639 break;
9640 }
Eric Christopherfd179292009-08-27 18:07:15 +00009641
Eric Christopher71c67532009-07-29 00:28:05 +00009642 SDValue LHS = Op.getOperand(1);
9643 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009644 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9645 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009646 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9647 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9648 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009649 }
Evan Cheng5759f972008-05-04 09:15:50 +00009650
9651 // Fix vector shift instructions where the last operand is a non-immediate
9652 // i32 value.
Craig Topper7be5dfd2011-11-12 09:58:49 +00009653 case Intrinsic::x86_avx2_pslli_w:
9654 case Intrinsic::x86_avx2_pslli_d:
9655 case Intrinsic::x86_avx2_pslli_q:
9656 case Intrinsic::x86_avx2_psrli_w:
9657 case Intrinsic::x86_avx2_psrli_d:
9658 case Intrinsic::x86_avx2_psrli_q:
9659 case Intrinsic::x86_avx2_psrai_w:
9660 case Intrinsic::x86_avx2_psrai_d:
Evan Cheng5759f972008-05-04 09:15:50 +00009661 case Intrinsic::x86_sse2_pslli_w:
9662 case Intrinsic::x86_sse2_pslli_d:
9663 case Intrinsic::x86_sse2_pslli_q:
9664 case Intrinsic::x86_sse2_psrli_w:
9665 case Intrinsic::x86_sse2_psrli_d:
9666 case Intrinsic::x86_sse2_psrli_q:
9667 case Intrinsic::x86_sse2_psrai_w:
9668 case Intrinsic::x86_sse2_psrai_d:
9669 case Intrinsic::x86_mmx_pslli_w:
9670 case Intrinsic::x86_mmx_pslli_d:
9671 case Intrinsic::x86_mmx_pslli_q:
9672 case Intrinsic::x86_mmx_psrli_w:
9673 case Intrinsic::x86_mmx_psrli_d:
9674 case Intrinsic::x86_mmx_psrli_q:
9675 case Intrinsic::x86_mmx_psrai_w:
9676 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009677 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009678 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009679 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009680
9681 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009682 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009683 switch (IntNo) {
9684 case Intrinsic::x86_sse2_pslli_w:
9685 NewIntNo = Intrinsic::x86_sse2_psll_w;
9686 break;
9687 case Intrinsic::x86_sse2_pslli_d:
9688 NewIntNo = Intrinsic::x86_sse2_psll_d;
9689 break;
9690 case Intrinsic::x86_sse2_pslli_q:
9691 NewIntNo = Intrinsic::x86_sse2_psll_q;
9692 break;
9693 case Intrinsic::x86_sse2_psrli_w:
9694 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9695 break;
9696 case Intrinsic::x86_sse2_psrli_d:
9697 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9698 break;
9699 case Intrinsic::x86_sse2_psrli_q:
9700 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9701 break;
9702 case Intrinsic::x86_sse2_psrai_w:
9703 NewIntNo = Intrinsic::x86_sse2_psra_w;
9704 break;
9705 case Intrinsic::x86_sse2_psrai_d:
9706 NewIntNo = Intrinsic::x86_sse2_psra_d;
9707 break;
Craig Topper7be5dfd2011-11-12 09:58:49 +00009708 case Intrinsic::x86_avx2_pslli_w:
9709 NewIntNo = Intrinsic::x86_avx2_psll_w;
9710 break;
9711 case Intrinsic::x86_avx2_pslli_d:
9712 NewIntNo = Intrinsic::x86_avx2_psll_d;
9713 break;
9714 case Intrinsic::x86_avx2_pslli_q:
9715 NewIntNo = Intrinsic::x86_avx2_psll_q;
9716 break;
9717 case Intrinsic::x86_avx2_psrli_w:
9718 NewIntNo = Intrinsic::x86_avx2_psrl_w;
9719 break;
9720 case Intrinsic::x86_avx2_psrli_d:
9721 NewIntNo = Intrinsic::x86_avx2_psrl_d;
9722 break;
9723 case Intrinsic::x86_avx2_psrli_q:
9724 NewIntNo = Intrinsic::x86_avx2_psrl_q;
9725 break;
9726 case Intrinsic::x86_avx2_psrai_w:
9727 NewIntNo = Intrinsic::x86_avx2_psra_w;
9728 break;
9729 case Intrinsic::x86_avx2_psrai_d:
9730 NewIntNo = Intrinsic::x86_avx2_psra_d;
9731 break;
Evan Cheng5759f972008-05-04 09:15:50 +00009732 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009733 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009734 switch (IntNo) {
9735 case Intrinsic::x86_mmx_pslli_w:
9736 NewIntNo = Intrinsic::x86_mmx_psll_w;
9737 break;
9738 case Intrinsic::x86_mmx_pslli_d:
9739 NewIntNo = Intrinsic::x86_mmx_psll_d;
9740 break;
9741 case Intrinsic::x86_mmx_pslli_q:
9742 NewIntNo = Intrinsic::x86_mmx_psll_q;
9743 break;
9744 case Intrinsic::x86_mmx_psrli_w:
9745 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9746 break;
9747 case Intrinsic::x86_mmx_psrli_d:
9748 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9749 break;
9750 case Intrinsic::x86_mmx_psrli_q:
9751 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9752 break;
9753 case Intrinsic::x86_mmx_psrai_w:
9754 NewIntNo = Intrinsic::x86_mmx_psra_w;
9755 break;
9756 case Intrinsic::x86_mmx_psrai_d:
9757 NewIntNo = Intrinsic::x86_mmx_psra_d;
9758 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009759 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009760 }
9761 break;
9762 }
9763 }
Mon P Wangefa42202009-09-03 19:56:25 +00009764
9765 // The vector shift intrinsics with scalars uses 32b shift amounts but
9766 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9767 // to be zero.
9768 SDValue ShOps[4];
9769 ShOps[0] = ShAmt;
9770 ShOps[1] = DAG.getConstant(0, MVT::i32);
9771 if (ShAmtVT == MVT::v4i32) {
9772 ShOps[2] = DAG.getUNDEF(MVT::i32);
9773 ShOps[3] = DAG.getUNDEF(MVT::i32);
9774 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9775 } else {
9776 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009777// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009778 }
9779
Owen Andersone50ed302009-08-10 22:56:29 +00009780 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009781 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009782 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009783 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009784 Op.getOperand(1), ShAmt);
9785 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009786 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009787}
Evan Cheng72261582005-12-20 06:22:03 +00009788
Dan Gohmand858e902010-04-17 15:26:15 +00009789SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9790 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009791 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9792 MFI->setReturnAddressIsTaken(true);
9793
Bill Wendling64e87322009-01-16 19:25:27 +00009794 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009795 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009796
9797 if (Depth > 0) {
9798 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9799 SDValue Offset =
9800 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009801 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009802 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009803 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009804 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009805 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009806 }
9807
9808 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009809 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009810 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009811 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009812}
9813
Dan Gohmand858e902010-04-17 15:26:15 +00009814SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009815 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9816 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009817
Owen Andersone50ed302009-08-10 22:56:29 +00009818 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009819 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009820 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9821 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009822 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009823 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009824 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9825 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009826 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009827 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009828}
9829
Dan Gohman475871a2008-07-27 21:46:04 +00009830SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009831 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009832 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009833}
9834
Dan Gohmand858e902010-04-17 15:26:15 +00009835SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009836 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009837 SDValue Chain = Op.getOperand(0);
9838 SDValue Offset = Op.getOperand(1);
9839 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009840 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009841
Dan Gohmand8816272010-08-11 18:14:00 +00009842 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9843 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9844 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009845 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009846
Dan Gohmand8816272010-08-11 18:14:00 +00009847 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9848 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009849 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009850 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9851 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009852 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009853 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009854
Dale Johannesene4d209d2009-02-03 20:21:25 +00009855 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009856 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009857 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009858}
9859
Duncan Sands4a544a72011-09-06 13:37:06 +00009860SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9861 SelectionDAG &DAG) const {
9862 return Op.getOperand(0);
9863}
9864
9865SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9866 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009867 SDValue Root = Op.getOperand(0);
9868 SDValue Trmp = Op.getOperand(1); // trampoline
9869 SDValue FPtr = Op.getOperand(2); // nested function
9870 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009871 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009872
Dan Gohman69de1932008-02-06 22:27:42 +00009873 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009874
9875 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009876 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009877
9878 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009879 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9880 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009881
Evan Cheng0e6a0522011-07-18 20:57:22 +00009882 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9883 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009884
9885 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9886
9887 // Load the pointer to the nested function into R11.
9888 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009889 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009890 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009891 Addr, MachinePointerInfo(TrmpAddr),
9892 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009893
Owen Anderson825b72b2009-08-11 20:47:22 +00009894 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9895 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009896 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9897 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009898 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009899
9900 // Load the 'nest' parameter value into R10.
9901 // R10 is specified in X86CallingConv.td
9902 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009903 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9904 DAG.getConstant(10, MVT::i64));
9905 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009906 Addr, MachinePointerInfo(TrmpAddr, 10),
9907 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009908
Owen Anderson825b72b2009-08-11 20:47:22 +00009909 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9910 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009911 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9912 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009913 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009914
9915 // Jump to the nested function.
9916 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009917 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9918 DAG.getConstant(20, MVT::i64));
9919 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009920 Addr, MachinePointerInfo(TrmpAddr, 20),
9921 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009922
9923 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009924 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9925 DAG.getConstant(22, MVT::i64));
9926 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009927 MachinePointerInfo(TrmpAddr, 22),
9928 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009929
Duncan Sands4a544a72011-09-06 13:37:06 +00009930 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009931 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009932 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009933 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009934 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009935 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009936
9937 switch (CC) {
9938 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009939 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009940 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009941 case CallingConv::X86_StdCall: {
9942 // Pass 'nest' parameter in ECX.
9943 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009944 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009945
9946 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009947 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009948 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009949
Chris Lattner58d74912008-03-12 17:45:29 +00009950 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009951 unsigned InRegCount = 0;
9952 unsigned Idx = 1;
9953
9954 for (FunctionType::param_iterator I = FTy->param_begin(),
9955 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009956 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009957 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009958 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009959
9960 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009961 report_fatal_error("Nest register in use - reduce number of inreg"
9962 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009963 }
9964 }
9965 break;
9966 }
9967 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009968 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009969 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009970 // Pass 'nest' parameter in EAX.
9971 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009972 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009973 break;
9974 }
9975
Dan Gohman475871a2008-07-27 21:46:04 +00009976 SDValue OutChains[4];
9977 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009978
Owen Anderson825b72b2009-08-11 20:47:22 +00009979 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9980 DAG.getConstant(10, MVT::i32));
9981 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009982
Chris Lattnera62fe662010-02-05 19:20:30 +00009983 // This is storing the opcode for MOV32ri.
9984 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009985 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009986 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009987 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009988 Trmp, MachinePointerInfo(TrmpAddr),
9989 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009990
Owen Anderson825b72b2009-08-11 20:47:22 +00009991 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9992 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009993 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9994 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009995 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009996
Chris Lattnera62fe662010-02-05 19:20:30 +00009997 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009998 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9999 DAG.getConstant(5, MVT::i32));
10000 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010001 MachinePointerInfo(TrmpAddr, 5),
10002 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010003
Owen Anderson825b72b2009-08-11 20:47:22 +000010004 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10005 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010006 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10007 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010008 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010009
Duncan Sands4a544a72011-09-06 13:37:06 +000010010 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010011 }
10012}
10013
Dan Gohmand858e902010-04-17 15:26:15 +000010014SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10015 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010016 /*
10017 The rounding mode is in bits 11:10 of FPSR, and has the following
10018 settings:
10019 00 Round to nearest
10020 01 Round to -inf
10021 10 Round to +inf
10022 11 Round to 0
10023
10024 FLT_ROUNDS, on the other hand, expects the following:
10025 -1 Undefined
10026 0 Round to 0
10027 1 Round to nearest
10028 2 Round to +inf
10029 3 Round to -inf
10030
10031 To perform the conversion, we do:
10032 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10033 */
10034
10035 MachineFunction &MF = DAG.getMachineFunction();
10036 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010037 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010038 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010039 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010040 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010041
10042 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010043 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010044 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010045
Michael J. Spencerec38de22010-10-10 22:04:20 +000010046
Chris Lattner2156b792010-09-22 01:11:26 +000010047 MachineMemOperand *MMO =
10048 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10049 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010050
Chris Lattner2156b792010-09-22 01:11:26 +000010051 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10052 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10053 DAG.getVTList(MVT::Other),
10054 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010055
10056 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010057 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010058 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010059
10060 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010061 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010062 DAG.getNode(ISD::SRL, DL, MVT::i16,
10063 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010064 CWD, DAG.getConstant(0x800, MVT::i16)),
10065 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010066 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010067 DAG.getNode(ISD::SRL, DL, MVT::i16,
10068 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010069 CWD, DAG.getConstant(0x400, MVT::i16)),
10070 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010071
Dan Gohman475871a2008-07-27 21:46:04 +000010072 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010073 DAG.getNode(ISD::AND, DL, MVT::i16,
10074 DAG.getNode(ISD::ADD, DL, MVT::i16,
10075 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010076 DAG.getConstant(1, MVT::i16)),
10077 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010078
10079
Duncan Sands83ec4b62008-06-06 12:08:01 +000010080 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010081 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010082}
10083
Dan Gohmand858e902010-04-17 15:26:15 +000010084SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010085 EVT VT = Op.getValueType();
10086 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010087 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010088 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010089
10090 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010091 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010092 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010093 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010094 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010095 }
Evan Cheng18efe262007-12-14 02:13:44 +000010096
Evan Cheng152804e2007-12-14 08:30:15 +000010097 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010098 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010099 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010100
10101 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010102 SDValue Ops[] = {
10103 Op,
10104 DAG.getConstant(NumBits+NumBits-1, OpVT),
10105 DAG.getConstant(X86::COND_E, MVT::i8),
10106 Op.getValue(1)
10107 };
10108 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010109
10110 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010111 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010112
Owen Anderson825b72b2009-08-11 20:47:22 +000010113 if (VT == MVT::i8)
10114 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010115 return Op;
10116}
10117
Dan Gohmand858e902010-04-17 15:26:15 +000010118SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010119 EVT VT = Op.getValueType();
10120 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010121 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010122 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010123
10124 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010125 if (VT == MVT::i8) {
10126 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010127 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010128 }
Evan Cheng152804e2007-12-14 08:30:15 +000010129
10130 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010131 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010132 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010133
10134 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010135 SDValue Ops[] = {
10136 Op,
10137 DAG.getConstant(NumBits, OpVT),
10138 DAG.getConstant(X86::COND_E, MVT::i8),
10139 Op.getValue(1)
10140 };
10141 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010142
Owen Anderson825b72b2009-08-11 20:47:22 +000010143 if (VT == MVT::i8)
10144 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010145 return Op;
10146}
10147
Craig Topper13894fa2011-08-24 06:14:18 +000010148// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10149// ones, and then concatenate the result back.
10150static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010151 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010152
10153 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
10154 "Unsupported value type for operation");
10155
10156 int NumElems = VT.getVectorNumElements();
10157 DebugLoc dl = Op.getDebugLoc();
10158 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10159 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10160
10161 // Extract the LHS vectors
10162 SDValue LHS = Op.getOperand(0);
10163 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10164 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10165
10166 // Extract the RHS vectors
10167 SDValue RHS = Op.getOperand(1);
10168 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
10169 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
10170
10171 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10172 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10173
10174 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10175 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10176 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10177}
10178
10179SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
10180 assert(Op.getValueType().getSizeInBits() == 256 &&
10181 Op.getValueType().isInteger() &&
10182 "Only handle AVX 256-bit vector integer operation");
10183 return Lower256IntArith(Op, DAG);
10184}
10185
10186SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
10187 assert(Op.getValueType().getSizeInBits() == 256 &&
10188 Op.getValueType().isInteger() &&
10189 "Only handle AVX 256-bit vector integer operation");
10190 return Lower256IntArith(Op, DAG);
10191}
10192
10193SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10194 EVT VT = Op.getValueType();
10195
10196 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +000010197 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010198 return Lower256IntArith(Op, DAG);
10199
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010200 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010201
Craig Topperaaa643c2011-11-09 07:28:55 +000010202 SDValue A = Op.getOperand(0);
10203 SDValue B = Op.getOperand(1);
10204
10205 if (VT == MVT::v4i64) {
10206 assert(Subtarget->hasAVX2() && "Lowering v4i64 multiply requires AVX2");
10207
10208 // ulong2 Ahi = __builtin_ia32_psrlqi256( a, 32);
10209 // ulong2 Bhi = __builtin_ia32_psrlqi256( b, 32);
10210 // ulong2 AloBlo = __builtin_ia32_pmuludq256( a, b );
10211 // ulong2 AloBhi = __builtin_ia32_pmuludq256( a, Bhi );
10212 // ulong2 AhiBlo = __builtin_ia32_pmuludq256( Ahi, b );
10213 //
10214 // AloBhi = __builtin_ia32_psllqi256( AloBhi, 32 );
10215 // AhiBlo = __builtin_ia32_psllqi256( AhiBlo, 32 );
10216 // return AloBlo + AloBhi + AhiBlo;
10217
10218 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10219 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10220 A, DAG.getConstant(32, MVT::i32));
10221 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10222 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10223 B, DAG.getConstant(32, MVT::i32));
10224 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10225 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10226 A, B);
10227 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10228 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10229 A, Bhi);
10230 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10231 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10232 Ahi, B);
10233 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10234 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10235 AloBhi, DAG.getConstant(32, MVT::i32));
10236 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10237 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10238 AhiBlo, DAG.getConstant(32, MVT::i32));
10239 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10240 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
10241 return Res;
10242 }
10243
10244 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
10245
Mon P Wangaf9b9522008-12-18 21:42:19 +000010246 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
10247 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
10248 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
10249 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
10250 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
10251 //
10252 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
10253 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
10254 // return AloBlo + AloBhi + AhiBlo;
10255
Dale Johannesene4d209d2009-02-03 20:21:25 +000010256 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010257 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10258 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010259 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010260 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10261 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010262 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010263 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010264 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010265 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010266 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010267 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010268 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010269 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010270 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010271 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010272 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10273 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010274 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010275 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10276 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010277 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10278 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010279 return Res;
10280}
10281
Nadav Rotem43012222011-05-11 08:12:09 +000010282SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10283
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010284 EVT VT = Op.getValueType();
10285 DebugLoc dl = Op.getDebugLoc();
10286 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010287 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010288 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010289
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010290 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010291 return SDValue();
10292
Nadav Rotem43012222011-05-11 08:12:09 +000010293 // Optimize shl/srl/sra with constant shift amount.
10294 if (isSplatVector(Amt.getNode())) {
10295 SDValue SclrAmt = Amt->getOperand(0);
10296 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10297 uint64_t ShiftAmt = C->getZExtValue();
10298
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010299 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SHL) {
10300 // Make a large shift.
10301 SDValue SHL =
10302 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10303 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10304 R, DAG.getConstant(ShiftAmt, MVT::i32));
10305 // Zero out the rightmost bits.
10306 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10307 MVT::i8));
10308 return DAG.getNode(ISD::AND, dl, VT, SHL,
10309 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10310 }
10311
Nadav Rotem43012222011-05-11 08:12:09 +000010312 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
10313 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10314 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10315 R, DAG.getConstant(ShiftAmt, MVT::i32));
10316
10317 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
10318 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10319 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10320 R, DAG.getConstant(ShiftAmt, MVT::i32));
10321
10322 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
10323 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10324 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10325 R, DAG.getConstant(ShiftAmt, MVT::i32));
10326
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010327 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRL) {
10328 // Make a large shift.
10329 SDValue SRL =
10330 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10331 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10332 R, DAG.getConstant(ShiftAmt, MVT::i32));
10333 // Zero out the leftmost bits.
10334 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10335 MVT::i8));
10336 return DAG.getNode(ISD::AND, dl, VT, SRL,
10337 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10338 }
10339
Nadav Rotem43012222011-05-11 08:12:09 +000010340 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
10341 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10342 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10343 R, DAG.getConstant(ShiftAmt, MVT::i32));
10344
10345 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
10346 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10347 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
10348 R, DAG.getConstant(ShiftAmt, MVT::i32));
10349
10350 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
10351 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10352 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10353 R, DAG.getConstant(ShiftAmt, MVT::i32));
10354
10355 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
10356 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10357 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
10358 R, DAG.getConstant(ShiftAmt, MVT::i32));
10359
10360 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
10361 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10362 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
10363 R, DAG.getConstant(ShiftAmt, MVT::i32));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010364
10365 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRA) {
10366 if (ShiftAmt == 7) {
10367 // R s>> 7 === R s< 0
10368 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10369 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10370 }
10371
10372 // R s>> a === ((R u>> a) ^ m) - m
10373 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10374 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10375 MVT::i8));
10376 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10377 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10378 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10379 return Res;
10380 }
Craig Topper46154eb2011-11-11 07:39:23 +000010381
Craig Topper0d86d462011-11-20 00:12:05 +000010382 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10383 if (Op.getOpcode() == ISD::SHL) {
10384 // Make a large shift.
10385 SDValue SHL =
10386 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10387 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
10388 R, DAG.getConstant(ShiftAmt, MVT::i32));
10389 // Zero out the rightmost bits.
10390 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10391 MVT::i8));
10392 return DAG.getNode(ISD::AND, dl, VT, SHL,
10393 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010394 }
Craig Topper0d86d462011-11-20 00:12:05 +000010395 if (Op.getOpcode() == ISD::SRL) {
10396 // Make a large shift.
10397 SDValue SRL =
10398 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10399 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
10400 R, DAG.getConstant(ShiftAmt, MVT::i32));
10401 // Zero out the leftmost bits.
10402 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10403 MVT::i8));
10404 return DAG.getNode(ISD::AND, dl, VT, SRL,
10405 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10406 }
10407 if (Op.getOpcode() == ISD::SRA) {
10408 if (ShiftAmt == 7) {
10409 // R s>> 7 === R s< 0
10410 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10411 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10412 }
10413
10414 // R s>> a === ((R u>> a) ^ m) - m
10415 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10416 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10417 MVT::i8));
10418 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10419 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10420 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10421 return Res;
10422 }
10423 }
Nadav Rotem43012222011-05-11 08:12:09 +000010424 }
10425 }
10426
10427 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010428 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010429 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10430 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10431 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
10432
10433 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010434
Nate Begeman51409212010-07-28 00:21:48 +000010435 std::vector<Constant*> CV(4, CI);
10436 Constant *C = ConstantVector::get(CV);
10437 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10438 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010439 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010440 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010441
10442 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010443 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010444 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10445 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10446 }
Nadav Rotem43012222011-05-11 08:12:09 +000010447 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010448 // a = a << 5;
10449 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10450 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10451 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
10452
10453 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
10454 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
10455
10456 std::vector<Constant*> CVM1(16, CM1);
10457 std::vector<Constant*> CVM2(16, CM2);
10458 Constant *C = ConstantVector::get(CVM1);
10459 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10460 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010461 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010462 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010463
10464 // r = pblendv(r, psllw(r & (char16)15, 4), a);
10465 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10466 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10467 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10468 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010469 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010470 // a += a
10471 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010472
Nate Begeman51409212010-07-28 00:21:48 +000010473 C = ConstantVector::get(CVM2);
10474 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10475 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010476 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010477 false, false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010478
Nate Begeman51409212010-07-28 00:21:48 +000010479 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10480 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10481 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10482 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10483 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010484 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010485 // a += a
10486 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010487
Nate Begeman51409212010-07-28 00:21:48 +000010488 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010489 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10490 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010491 return R;
10492 }
Craig Topper46154eb2011-11-11 07:39:23 +000010493
10494 // Decompose 256-bit shifts into smaller 128-bit shifts.
10495 if (VT.getSizeInBits() == 256) {
10496 int NumElems = VT.getVectorNumElements();
10497 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10498 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10499
10500 // Extract the two vectors
10501 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10502 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10503 DAG, dl);
10504
10505 // Recreate the shift amount vectors
10506 SDValue Amt1, Amt2;
10507 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10508 // Constant shift amount
10509 SmallVector<SDValue, 4> Amt1Csts;
10510 SmallVector<SDValue, 4> Amt2Csts;
10511 for (int i = 0; i < NumElems/2; ++i)
10512 Amt1Csts.push_back(Amt->getOperand(i));
10513 for (int i = NumElems/2; i < NumElems; ++i)
10514 Amt2Csts.push_back(Amt->getOperand(i));
10515
10516 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10517 &Amt1Csts[0], NumElems/2);
10518 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10519 &Amt2Csts[0], NumElems/2);
10520 } else {
10521 // Variable shift amount
10522 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10523 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10524 DAG, dl);
10525 }
10526
10527 // Issue new vector shifts for the smaller types
10528 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10529 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10530
10531 // Concatenate the result back
10532 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10533 }
10534
Nate Begeman51409212010-07-28 00:21:48 +000010535 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010536}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010537
Dan Gohmand858e902010-04-17 15:26:15 +000010538SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010539 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10540 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010541 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10542 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010543 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010544 SDValue LHS = N->getOperand(0);
10545 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010546 unsigned BaseOp = 0;
10547 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010548 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010549 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010550 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010551 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010552 // A subtract of one will be selected as a INC. Note that INC doesn't
10553 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010554 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10555 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010556 BaseOp = X86ISD::INC;
10557 Cond = X86::COND_O;
10558 break;
10559 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010560 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010561 Cond = X86::COND_O;
10562 break;
10563 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010564 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010565 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010566 break;
10567 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010568 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10569 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010570 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10571 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010572 BaseOp = X86ISD::DEC;
10573 Cond = X86::COND_O;
10574 break;
10575 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010576 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010577 Cond = X86::COND_O;
10578 break;
10579 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010580 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010581 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010582 break;
10583 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010584 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010585 Cond = X86::COND_O;
10586 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010587 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10588 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10589 MVT::i32);
10590 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010591
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010592 SDValue SetCC =
10593 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10594 DAG.getConstant(X86::COND_O, MVT::i32),
10595 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010596
Dan Gohman6e5fda22011-07-22 18:45:15 +000010597 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010598 }
Bill Wendling74c37652008-12-09 22:08:41 +000010599 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010600
Bill Wendling61edeb52008-12-02 01:06:39 +000010601 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010602 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010603 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010604
Bill Wendling61edeb52008-12-02 01:06:39 +000010605 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010606 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10607 DAG.getConstant(Cond, MVT::i32),
10608 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010609
Dan Gohman6e5fda22011-07-22 18:45:15 +000010610 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010611}
10612
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010613SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10614 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010615 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10616 EVT VT = Op.getValueType();
10617
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010618 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010619 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10620 ExtraVT.getScalarType().getSizeInBits();
10621 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10622
10623 unsigned SHLIntrinsicsID = 0;
10624 unsigned SRAIntrinsicsID = 0;
10625 switch (VT.getSimpleVT().SimpleTy) {
10626 default:
10627 return SDValue();
Craig Toppera124f942011-11-21 01:12:36 +000010628 case MVT::v4i32:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010629 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10630 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10631 break;
Craig Toppera124f942011-11-21 01:12:36 +000010632 case MVT::v8i16:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010633 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10634 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10635 break;
Craig Toppera124f942011-11-21 01:12:36 +000010636 case MVT::v8i32:
10637 case MVT::v16i16:
10638 if (!Subtarget->hasAVX())
10639 return SDValue();
10640 if (!Subtarget->hasAVX2()) {
10641 // needs to be split
10642 int NumElems = VT.getVectorNumElements();
10643 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10644 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10645
10646 // Extract the LHS vectors
10647 SDValue LHS = Op.getOperand(0);
10648 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10649 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10650
10651 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10652 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10653
10654 EVT ExtraEltVT = ExtraVT.getVectorElementType();
10655 int ExtraNumElems = ExtraVT.getVectorNumElements();
10656 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10657 ExtraNumElems/2);
10658 SDValue Extra = DAG.getValueType(ExtraVT);
10659
10660 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10661 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
10662
10663 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10664 }
10665 if (VT == MVT::v8i32) {
10666 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_d;
10667 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_d;
10668 } else {
10669 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_w;
10670 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_w;
10671 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010672 }
10673
10674 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10675 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
Craig Toppera124f942011-11-21 01:12:36 +000010676 Op.getOperand(0), ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010677
Nadav Rotema7934dd2011-10-10 19:31:45 +000010678 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10679 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10680 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010681 }
10682
10683 return SDValue();
10684}
10685
10686
Eric Christopher9a9d2752010-07-22 02:48:34 +000010687SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10688 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010689
Eric Christopher77ed1352011-07-08 00:04:56 +000010690 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10691 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010692 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010693 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010694 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010695 SDValue Ops[] = {
10696 DAG.getRegister(X86::ESP, MVT::i32), // Base
10697 DAG.getTargetConstant(1, MVT::i8), // Scale
10698 DAG.getRegister(0, MVT::i32), // Index
10699 DAG.getTargetConstant(0, MVT::i32), // Disp
10700 DAG.getRegister(0, MVT::i32), // Segment.
10701 Zero,
10702 Chain
10703 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010704 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010705 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10706 array_lengthof(Ops));
10707 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010708 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010709
Eric Christopher9a9d2752010-07-22 02:48:34 +000010710 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010711 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010712 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010713
Chris Lattner132929a2010-08-14 17:26:09 +000010714 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10715 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10716 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10717 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010718
Chris Lattner132929a2010-08-14 17:26:09 +000010719 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10720 if (!Op1 && !Op2 && !Op3 && Op4)
10721 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010722
Chris Lattner132929a2010-08-14 17:26:09 +000010723 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10724 if (Op1 && !Op2 && !Op3 && !Op4)
10725 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010726
10727 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010728 // (MFENCE)>;
10729 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010730}
10731
Eli Friedman14648462011-07-27 22:21:52 +000010732SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10733 SelectionDAG &DAG) const {
10734 DebugLoc dl = Op.getDebugLoc();
10735 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10736 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10737 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10738 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10739
10740 // The only fence that needs an instruction is a sequentially-consistent
10741 // cross-thread fence.
10742 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10743 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10744 // no-sse2). There isn't any reason to disable it if the target processor
10745 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010746 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010747 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10748
10749 SDValue Chain = Op.getOperand(0);
10750 SDValue Zero = DAG.getConstant(0, MVT::i32);
10751 SDValue Ops[] = {
10752 DAG.getRegister(X86::ESP, MVT::i32), // Base
10753 DAG.getTargetConstant(1, MVT::i8), // Scale
10754 DAG.getRegister(0, MVT::i32), // Index
10755 DAG.getTargetConstant(0, MVT::i32), // Disp
10756 DAG.getRegister(0, MVT::i32), // Segment.
10757 Zero,
10758 Chain
10759 };
10760 SDNode *Res =
10761 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10762 array_lengthof(Ops));
10763 return SDValue(Res, 0);
10764 }
10765
10766 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10767 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10768}
10769
10770
Dan Gohmand858e902010-04-17 15:26:15 +000010771SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010772 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010773 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010774 unsigned Reg = 0;
10775 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010776 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010777 default:
10778 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010779 case MVT::i8: Reg = X86::AL; size = 1; break;
10780 case MVT::i16: Reg = X86::AX; size = 2; break;
10781 case MVT::i32: Reg = X86::EAX; size = 4; break;
10782 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010783 assert(Subtarget->is64Bit() && "Node not type legal!");
10784 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010785 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010786 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010787 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010788 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010789 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010790 Op.getOperand(1),
10791 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010792 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010793 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010794 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010795 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10796 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10797 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010798 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010799 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010800 return cpOut;
10801}
10802
Duncan Sands1607f052008-12-01 11:39:25 +000010803SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010804 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010805 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010806 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010807 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010808 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010809 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010810 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10811 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010812 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010813 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10814 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010815 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010816 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010817 rdx.getValue(1)
10818 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010819 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010820}
10821
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010822SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010823 SelectionDAG &DAG) const {
10824 EVT SrcVT = Op.getOperand(0).getValueType();
10825 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010826 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010827 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010828 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010829 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010830 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010831 // i64 <=> MMX conversions are Legal.
10832 if (SrcVT==MVT::i64 && DstVT.isVector())
10833 return Op;
10834 if (DstVT==MVT::i64 && SrcVT.isVector())
10835 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010836 // MMX <=> MMX conversions are Legal.
10837 if (SrcVT.isVector() && DstVT.isVector())
10838 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010839 // All other conversions need to be expanded.
10840 return SDValue();
10841}
Chris Lattner5b856542010-12-20 00:59:46 +000010842
Dan Gohmand858e902010-04-17 15:26:15 +000010843SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010844 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010845 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010846 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010847 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010848 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010849 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010850 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010851 Node->getOperand(0),
10852 Node->getOperand(1), negOp,
10853 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010854 cast<AtomicSDNode>(Node)->getAlignment(),
10855 cast<AtomicSDNode>(Node)->getOrdering(),
10856 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010857}
10858
Eli Friedman327236c2011-08-24 20:50:09 +000010859static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10860 SDNode *Node = Op.getNode();
10861 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010862 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010863
10864 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010865 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10866 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10867 // (The only way to get a 16-byte store is cmpxchg16b)
10868 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10869 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10870 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010871 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10872 cast<AtomicSDNode>(Node)->getMemoryVT(),
10873 Node->getOperand(0),
10874 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010875 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010876 cast<AtomicSDNode>(Node)->getOrdering(),
10877 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010878 return Swap.getValue(1);
10879 }
10880 // Other atomic stores have a simple pattern.
10881 return Op;
10882}
10883
Chris Lattner5b856542010-12-20 00:59:46 +000010884static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10885 EVT VT = Op.getNode()->getValueType(0);
10886
10887 // Let legalize expand this if it isn't a legal type yet.
10888 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10889 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010890
Chris Lattner5b856542010-12-20 00:59:46 +000010891 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010892
Chris Lattner5b856542010-12-20 00:59:46 +000010893 unsigned Opc;
10894 bool ExtraOp = false;
10895 switch (Op.getOpcode()) {
10896 default: assert(0 && "Invalid code");
10897 case ISD::ADDC: Opc = X86ISD::ADD; break;
10898 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10899 case ISD::SUBC: Opc = X86ISD::SUB; break;
10900 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10901 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010902
Chris Lattner5b856542010-12-20 00:59:46 +000010903 if (!ExtraOp)
10904 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10905 Op.getOperand(1));
10906 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10907 Op.getOperand(1), Op.getOperand(2));
10908}
10909
Evan Cheng0db9fe62006-04-25 20:13:52 +000010910/// LowerOperation - Provide custom lowering hooks for some operations.
10911///
Dan Gohmand858e902010-04-17 15:26:15 +000010912SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010913 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010914 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010915 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010916 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010917 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010918 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10919 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010920 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010921 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010922 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010923 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10924 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10925 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010926 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010927 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010928 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10929 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10930 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010931 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010932 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010933 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010934 case ISD::SHL_PARTS:
10935 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010936 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010937 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010938 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010939 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010940 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010941 case ISD::FABS: return LowerFABS(Op, DAG);
10942 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010943 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010944 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010945 case ISD::SETCC: return LowerSETCC(Op, DAG);
10946 case ISD::SELECT: return LowerSELECT(Op, DAG);
10947 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010948 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010949 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010950 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010951 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010952 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010953 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10954 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010955 case ISD::FRAME_TO_ARGS_OFFSET:
10956 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010957 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010958 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010959 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10960 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010961 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010962 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10963 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010964 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010965 case ISD::SRA:
10966 case ISD::SRL:
10967 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010968 case ISD::SADDO:
10969 case ISD::UADDO:
10970 case ISD::SSUBO:
10971 case ISD::USUBO:
10972 case ISD::SMULO:
10973 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010974 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010975 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010976 case ISD::ADDC:
10977 case ISD::ADDE:
10978 case ISD::SUBC:
10979 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010980 case ISD::ADD: return LowerADD(Op, DAG);
10981 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010982 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010983}
10984
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010985static void ReplaceATOMIC_LOAD(SDNode *Node,
10986 SmallVectorImpl<SDValue> &Results,
10987 SelectionDAG &DAG) {
10988 DebugLoc dl = Node->getDebugLoc();
10989 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10990
10991 // Convert wide load -> cmpxchg8b/cmpxchg16b
10992 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10993 // (The only way to get a 16-byte load is cmpxchg16b)
10994 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010995 SDValue Zero = DAG.getConstant(0, VT);
10996 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010997 Node->getOperand(0),
10998 Node->getOperand(1), Zero, Zero,
10999 cast<AtomicSDNode>(Node)->getMemOperand(),
11000 cast<AtomicSDNode>(Node)->getOrdering(),
11001 cast<AtomicSDNode>(Node)->getSynchScope());
11002 Results.push_back(Swap.getValue(0));
11003 Results.push_back(Swap.getValue(1));
11004}
11005
Duncan Sands1607f052008-12-01 11:39:25 +000011006void X86TargetLowering::
11007ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011008 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011009 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011010 assert (Node->getValueType(0) == MVT::i64 &&
11011 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011012
11013 SDValue Chain = Node->getOperand(0);
11014 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011015 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011016 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011017 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011018 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011019 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011020 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011021 SDValue Result =
11022 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11023 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011024 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011025 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011026 Results.push_back(Result.getValue(2));
11027}
11028
Duncan Sands126d9072008-07-04 11:47:58 +000011029/// ReplaceNodeResults - Replace a node with an illegal result type
11030/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011031void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11032 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011033 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011034 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011035 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011036 default:
Duncan Sands1607f052008-12-01 11:39:25 +000011037 assert(false && "Do not know how to custom type legalize this operation!");
11038 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011039 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011040 case ISD::ADDC:
11041 case ISD::ADDE:
11042 case ISD::SUBC:
11043 case ISD::SUBE:
11044 // We don't want to expand or promote these.
11045 return;
Duncan Sands1607f052008-12-01 11:39:25 +000011046 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000011047 std::pair<SDValue,SDValue> Vals =
11048 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000011049 SDValue FIST = Vals.first, StackSlot = Vals.second;
11050 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011051 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011052 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000011053 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011054 MachinePointerInfo(),
11055 false, false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000011056 }
11057 return;
11058 }
11059 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011060 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011061 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011062 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011063 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011064 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011065 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011066 eax.getValue(2));
11067 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11068 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011069 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011070 Results.push_back(edx.getValue(1));
11071 return;
11072 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011073 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011074 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011075 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011076 bool Regs64bit = T == MVT::i128;
11077 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011078 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011079 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11080 DAG.getConstant(0, HalfT));
11081 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11082 DAG.getConstant(1, HalfT));
11083 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11084 Regs64bit ? X86::RAX : X86::EAX,
11085 cpInL, SDValue());
11086 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11087 Regs64bit ? X86::RDX : X86::EDX,
11088 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011089 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011090 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11091 DAG.getConstant(0, HalfT));
11092 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11093 DAG.getConstant(1, HalfT));
11094 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11095 Regs64bit ? X86::RBX : X86::EBX,
11096 swapInL, cpInH.getValue(1));
11097 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
11098 Regs64bit ? X86::RCX : X86::ECX,
11099 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011100 SDValue Ops[] = { swapInH.getValue(0),
11101 N->getOperand(1),
11102 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011103 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011104 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011105 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11106 X86ISD::LCMPXCHG8_DAG;
11107 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011108 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011109 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11110 Regs64bit ? X86::RAX : X86::EAX,
11111 HalfT, Result.getValue(1));
11112 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11113 Regs64bit ? X86::RDX : X86::EDX,
11114 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011115 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011116 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011117 Results.push_back(cpOutH.getValue(1));
11118 return;
11119 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011120 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000011121 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
11122 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011123 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000011124 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
11125 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011126 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000011127 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
11128 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011129 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000011130 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
11131 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011132 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000011133 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
11134 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011135 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000011136 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
11137 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011138 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000011139 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
11140 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011141 case ISD::ATOMIC_LOAD:
11142 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011143 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011144}
11145
Evan Cheng72261582005-12-20 06:22:03 +000011146const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11147 switch (Opcode) {
11148 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011149 case X86ISD::BSF: return "X86ISD::BSF";
11150 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011151 case X86ISD::SHLD: return "X86ISD::SHLD";
11152 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011153 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011154 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011155 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011156 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011157 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011158 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011159 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11160 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11161 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011162 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011163 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011164 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011165 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011166 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011167 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011168 case X86ISD::COMI: return "X86ISD::COMI";
11169 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011170 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011171 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011172 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11173 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011174 case X86ISD::CMOV: return "X86ISD::CMOV";
11175 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011176 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011177 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11178 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011179 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011180 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011181 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011182 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011183 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011184 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11185 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011186 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011187 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011188 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011189 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011190 case X86ISD::BLENDV: return "X86ISD::BLENDV";
11191 case X86ISD::FHADD: return "X86ISD::FHADD";
11192 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011193 case X86ISD::FMAX: return "X86ISD::FMAX";
11194 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000011195 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11196 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011197 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011198 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011199 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011200 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011201 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011202 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11203 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011204 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11205 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11206 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11207 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11208 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11209 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011210 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
11211 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000011212 case X86ISD::VSHL: return "X86ISD::VSHL";
11213 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000011214 case X86ISD::CMPPD: return "X86ISD::CMPPD";
11215 case X86ISD::CMPPS: return "X86ISD::CMPPS";
11216 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
11217 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
11218 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
11219 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
11220 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
11221 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
11222 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
11223 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011224 case X86ISD::ADD: return "X86ISD::ADD";
11225 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011226 case X86ISD::ADC: return "X86ISD::ADC";
11227 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011228 case X86ISD::SMUL: return "X86ISD::SMUL";
11229 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011230 case X86ISD::INC: return "X86ISD::INC";
11231 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011232 case X86ISD::OR: return "X86ISD::OR";
11233 case X86ISD::XOR: return "X86ISD::XOR";
11234 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011235 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011236 case X86ISD::BLSI: return "X86ISD::BLSI";
11237 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11238 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011239 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011240 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011241 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011242 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11243 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11244 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
11245 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
11246 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
11247 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
11248 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
11249 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
11250 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011251 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011252 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011253 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011254 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11255 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011256 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11257 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11258 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
11259 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
11260 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
11261 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11262 case X86ISD::MOVSS: return "X86ISD::MOVSS";
11263 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
11264 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
Craig Topper6347e862011-11-21 06:57:39 +000011265 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
David Greenefbf05d32011-02-22 23:31:46 +000011266 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011267 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
11268 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
11269 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
11270 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
11271 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
11272 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
Craig Topper6347e862011-11-21 06:57:39 +000011273 case X86ISD::VPUNPCKLWDY: return "X86ISD::VPUNPCKLWDY";
11274 case X86ISD::VPUNPCKLDQY: return "X86ISD::VPUNPCKLDQY";
11275 case X86ISD::VPUNPCKLQDQY: return "X86ISD::VPUNPCKLQDQY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011276 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
11277 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
11278 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
11279 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Craig Topper6347e862011-11-21 06:57:39 +000011280 case X86ISD::VPUNPCKHWDY: return "X86ISD::VPUNPCKHWDY";
11281 case X86ISD::VPUNPCKHDQY: return "X86ISD::VPUNPCKHDQY";
11282 case X86ISD::VPUNPCKHQDQY: return "X86ISD::VPUNPCKHQDQY";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011283 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000011284 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
11285 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
11286 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
11287 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000011288 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011289 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011290 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011291 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011292 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011293 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000011294 }
11295}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011296
Chris Lattnerc9addb72007-03-30 23:15:24 +000011297// isLegalAddressingMode - Return true if the addressing mode represented
11298// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011299bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011300 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011301 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011302 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011303 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011304
Chris Lattnerc9addb72007-03-30 23:15:24 +000011305 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011306 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011307 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011308
Chris Lattnerc9addb72007-03-30 23:15:24 +000011309 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011310 unsigned GVFlags =
11311 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011312
Chris Lattnerdfed4132009-07-10 07:38:24 +000011313 // If a reference to this global requires an extra load, we can't fold it.
11314 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011315 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011316
Chris Lattnerdfed4132009-07-10 07:38:24 +000011317 // If BaseGV requires a register for the PIC base, we cannot also have a
11318 // BaseReg specified.
11319 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011320 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011321
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011322 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011323 if ((M != CodeModel::Small || R != Reloc::Static) &&
11324 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011325 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011326 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011327
Chris Lattnerc9addb72007-03-30 23:15:24 +000011328 switch (AM.Scale) {
11329 case 0:
11330 case 1:
11331 case 2:
11332 case 4:
11333 case 8:
11334 // These scales always work.
11335 break;
11336 case 3:
11337 case 5:
11338 case 9:
11339 // These scales are formed with basereg+scalereg. Only accept if there is
11340 // no basereg yet.
11341 if (AM.HasBaseReg)
11342 return false;
11343 break;
11344 default: // Other stuff never works.
11345 return false;
11346 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011347
Chris Lattnerc9addb72007-03-30 23:15:24 +000011348 return true;
11349}
11350
11351
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011352bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011353 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011354 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011355 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11356 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011357 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011358 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011359 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011360}
11361
Owen Andersone50ed302009-08-10 22:56:29 +000011362bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011363 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011364 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011365 unsigned NumBits1 = VT1.getSizeInBits();
11366 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011367 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011368 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011369 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011370}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011371
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011372bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011373 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011374 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011375}
11376
Owen Andersone50ed302009-08-10 22:56:29 +000011377bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011378 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011379 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011380}
11381
Owen Andersone50ed302009-08-10 22:56:29 +000011382bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011383 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011384 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011385}
11386
Evan Cheng60c07e12006-07-05 22:17:51 +000011387/// isShuffleMaskLegal - Targets can use this to indicate that they only
11388/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11389/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11390/// are assumed to be legal.
11391bool
Eric Christopherfd179292009-08-27 18:07:15 +000011392X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011393 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011394 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011395 if (VT.getSizeInBits() == 64)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000011396 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX());
Nate Begeman9008ca62009-04-27 18:41:29 +000011397
Nate Begemana09008b2009-10-19 02:17:23 +000011398 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011399 return (VT.getVectorNumElements() == 2 ||
11400 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11401 isMOVLMask(M, VT) ||
11402 isSHUFPMask(M, VT) ||
11403 isPSHUFDMask(M, VT) ||
11404 isPSHUFHWMask(M, VT) ||
11405 isPSHUFLWMask(M, VT) ||
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000011406 isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()) ||
Craig Topper6347e862011-11-21 06:57:39 +000011407 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11408 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011409 isUNPCKL_v_undef_Mask(M, VT) ||
11410 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011411}
11412
Dan Gohman7d8143f2008-04-09 20:09:42 +000011413bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011414X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011415 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011416 unsigned NumElts = VT.getVectorNumElements();
11417 // FIXME: This collection of masks seems suspect.
11418 if (NumElts == 2)
11419 return true;
11420 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11421 return (isMOVLMask(Mask, VT) ||
11422 isCommutedMOVLMask(Mask, VT, true) ||
11423 isSHUFPMask(Mask, VT) ||
11424 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011425 }
11426 return false;
11427}
11428
11429//===----------------------------------------------------------------------===//
11430// X86 Scheduler Hooks
11431//===----------------------------------------------------------------------===//
11432
Mon P Wang63307c32008-05-05 19:05:59 +000011433// private utility function
11434MachineBasicBlock *
11435X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11436 MachineBasicBlock *MBB,
11437 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011438 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011439 unsigned LoadOpc,
11440 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011441 unsigned notOpc,
11442 unsigned EAXreg,
11443 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011444 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011445 // For the atomic bitwise operator, we generate
11446 // thisMBB:
11447 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011448 // ld t1 = [bitinstr.addr]
11449 // op t2 = t1, [bitinstr.val]
11450 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011451 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11452 // bz newMBB
11453 // fallthrough -->nextMBB
11454 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11455 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011456 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011457 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011458
Mon P Wang63307c32008-05-05 19:05:59 +000011459 /// First build the CFG
11460 MachineFunction *F = MBB->getParent();
11461 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011462 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11463 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11464 F->insert(MBBIter, newMBB);
11465 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011466
Dan Gohman14152b42010-07-06 20:24:04 +000011467 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11468 nextMBB->splice(nextMBB->begin(), thisMBB,
11469 llvm::next(MachineBasicBlock::iterator(bInstr)),
11470 thisMBB->end());
11471 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011472
Mon P Wang63307c32008-05-05 19:05:59 +000011473 // Update thisMBB to fall through to newMBB
11474 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011475
Mon P Wang63307c32008-05-05 19:05:59 +000011476 // newMBB jumps to itself and fall through to nextMBB
11477 newMBB->addSuccessor(nextMBB);
11478 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011479
Mon P Wang63307c32008-05-05 19:05:59 +000011480 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011481 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011482 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011483 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011484 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011485 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011486 int numArgs = bInstr->getNumOperands() - 1;
11487 for (int i=0; i < numArgs; ++i)
11488 argOpers[i] = &bInstr->getOperand(i+1);
11489
11490 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011491 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011492 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011493
Dale Johannesen140be2d2008-08-19 18:47:28 +000011494 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011495 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011496 for (int i=0; i <= lastAddrIndx; ++i)
11497 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011498
Dale Johannesen140be2d2008-08-19 18:47:28 +000011499 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011500 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011501 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011502 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011503 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011504 tt = t1;
11505
Dale Johannesen140be2d2008-08-19 18:47:28 +000011506 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011507 assert((argOpers[valArgIndx]->isReg() ||
11508 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011509 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011510 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011511 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011512 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011513 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011514 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011515 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011516
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011517 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011518 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011519
Dale Johannesene4d209d2009-02-03 20:21:25 +000011520 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011521 for (int i=0; i <= lastAddrIndx; ++i)
11522 (*MIB).addOperand(*argOpers[i]);
11523 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011524 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011525 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11526 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011527
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011528 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011529 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011530
Mon P Wang63307c32008-05-05 19:05:59 +000011531 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011532 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011533
Dan Gohman14152b42010-07-06 20:24:04 +000011534 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011535 return nextMBB;
11536}
11537
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011538// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011539MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011540X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11541 MachineBasicBlock *MBB,
11542 unsigned regOpcL,
11543 unsigned regOpcH,
11544 unsigned immOpcL,
11545 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011546 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011547 // For the atomic bitwise operator, we generate
11548 // thisMBB (instructions are in pairs, except cmpxchg8b)
11549 // ld t1,t2 = [bitinstr.addr]
11550 // newMBB:
11551 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11552 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011553 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011554 // mov ECX, EBX <- t5, t6
11555 // mov EAX, EDX <- t1, t2
11556 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11557 // mov t3, t4 <- EAX, EDX
11558 // bz newMBB
11559 // result in out1, out2
11560 // fallthrough -->nextMBB
11561
11562 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11563 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011564 const unsigned NotOpc = X86::NOT32r;
11565 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11566 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11567 MachineFunction::iterator MBBIter = MBB;
11568 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011569
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011570 /// First build the CFG
11571 MachineFunction *F = MBB->getParent();
11572 MachineBasicBlock *thisMBB = MBB;
11573 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11574 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11575 F->insert(MBBIter, newMBB);
11576 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011577
Dan Gohman14152b42010-07-06 20:24:04 +000011578 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11579 nextMBB->splice(nextMBB->begin(), thisMBB,
11580 llvm::next(MachineBasicBlock::iterator(bInstr)),
11581 thisMBB->end());
11582 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011583
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011584 // Update thisMBB to fall through to newMBB
11585 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011586
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011587 // newMBB jumps to itself and fall through to nextMBB
11588 newMBB->addSuccessor(nextMBB);
11589 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011590
Dale Johannesene4d209d2009-02-03 20:21:25 +000011591 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011592 // Insert instructions into newMBB based on incoming instruction
11593 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011594 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011595 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011596 MachineOperand& dest1Oper = bInstr->getOperand(0);
11597 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011598 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11599 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011600 argOpers[i] = &bInstr->getOperand(i+2);
11601
Dan Gohman71ea4e52010-05-14 21:01:44 +000011602 // We use some of the operands multiple times, so conservatively just
11603 // clear any kill flags that might be present.
11604 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11605 argOpers[i]->setIsKill(false);
11606 }
11607
Evan Chengad5b52f2010-01-08 19:14:57 +000011608 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011609 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011610
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011611 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011612 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011613 for (int i=0; i <= lastAddrIndx; ++i)
11614 (*MIB).addOperand(*argOpers[i]);
11615 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011616 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011617 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011618 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011619 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011620 MachineOperand newOp3 = *(argOpers[3]);
11621 if (newOp3.isImm())
11622 newOp3.setImm(newOp3.getImm()+4);
11623 else
11624 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011625 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011626 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011627
11628 // t3/4 are defined later, at the bottom of the loop
11629 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11630 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011631 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011632 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011633 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011634 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11635
Evan Cheng306b4ca2010-01-08 23:41:50 +000011636 // The subsequent operations should be using the destination registers of
11637 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011638 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011639 t1 = F->getRegInfo().createVirtualRegister(RC);
11640 t2 = F->getRegInfo().createVirtualRegister(RC);
11641 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11642 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011643 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011644 t1 = dest1Oper.getReg();
11645 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011646 }
11647
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011648 int valArgIndx = lastAddrIndx + 1;
11649 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011650 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011651 "invalid operand");
11652 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11653 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011654 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011655 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011656 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011657 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011658 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011659 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011660 (*MIB).addOperand(*argOpers[valArgIndx]);
11661 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011662 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011663 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011664 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011665 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011666 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011667 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011668 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011669 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011670 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011671 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011672
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011673 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011674 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011675 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011676 MIB.addReg(t2);
11677
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011678 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011679 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011680 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011681 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011682
Dale Johannesene4d209d2009-02-03 20:21:25 +000011683 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011684 for (int i=0; i <= lastAddrIndx; ++i)
11685 (*MIB).addOperand(*argOpers[i]);
11686
11687 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011688 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11689 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011690
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011691 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011692 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011693 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011694 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011695
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011696 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011697 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011698
Dan Gohman14152b42010-07-06 20:24:04 +000011699 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011700 return nextMBB;
11701}
11702
11703// private utility function
11704MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011705X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11706 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011707 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011708 // For the atomic min/max operator, we generate
11709 // thisMBB:
11710 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011711 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011712 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011713 // cmp t1, t2
11714 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011715 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011716 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11717 // bz newMBB
11718 // fallthrough -->nextMBB
11719 //
11720 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11721 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011722 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011723 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011724
Mon P Wang63307c32008-05-05 19:05:59 +000011725 /// First build the CFG
11726 MachineFunction *F = MBB->getParent();
11727 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011728 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11729 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11730 F->insert(MBBIter, newMBB);
11731 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011732
Dan Gohman14152b42010-07-06 20:24:04 +000011733 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11734 nextMBB->splice(nextMBB->begin(), thisMBB,
11735 llvm::next(MachineBasicBlock::iterator(mInstr)),
11736 thisMBB->end());
11737 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011738
Mon P Wang63307c32008-05-05 19:05:59 +000011739 // Update thisMBB to fall through to newMBB
11740 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011741
Mon P Wang63307c32008-05-05 19:05:59 +000011742 // newMBB jumps to newMBB and fall through to nextMBB
11743 newMBB->addSuccessor(nextMBB);
11744 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011745
Dale Johannesene4d209d2009-02-03 20:21:25 +000011746 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011747 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011748 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011749 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011750 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011751 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011752 int numArgs = mInstr->getNumOperands() - 1;
11753 for (int i=0; i < numArgs; ++i)
11754 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011755
Mon P Wang63307c32008-05-05 19:05:59 +000011756 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011757 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011758 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011759
Mon P Wangab3e7472008-05-05 22:56:23 +000011760 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011761 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011762 for (int i=0; i <= lastAddrIndx; ++i)
11763 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011764
Mon P Wang63307c32008-05-05 19:05:59 +000011765 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011766 assert((argOpers[valArgIndx]->isReg() ||
11767 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011768 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011769
11770 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011771 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011772 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011773 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011774 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011775 (*MIB).addOperand(*argOpers[valArgIndx]);
11776
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011777 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011778 MIB.addReg(t1);
11779
Dale Johannesene4d209d2009-02-03 20:21:25 +000011780 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011781 MIB.addReg(t1);
11782 MIB.addReg(t2);
11783
11784 // Generate movc
11785 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011786 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011787 MIB.addReg(t2);
11788 MIB.addReg(t1);
11789
11790 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011791 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011792 for (int i=0; i <= lastAddrIndx; ++i)
11793 (*MIB).addOperand(*argOpers[i]);
11794 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011795 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011796 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11797 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011798
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011799 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011800 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011801
Mon P Wang63307c32008-05-05 19:05:59 +000011802 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011803 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011804
Dan Gohman14152b42010-07-06 20:24:04 +000011805 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011806 return nextMBB;
11807}
11808
Eric Christopherf83a5de2009-08-27 18:08:16 +000011809// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011810// or XMM0_V32I8 in AVX all of this code can be replaced with that
11811// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011812MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011813X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011814 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011815 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11816 "Target must have SSE4.2 or AVX features enabled");
11817
Eric Christopherb120ab42009-08-18 22:50:32 +000011818 DebugLoc dl = MI->getDebugLoc();
11819 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011820 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011821 if (!Subtarget->hasAVX()) {
11822 if (memArg)
11823 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11824 else
11825 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11826 } else {
11827 if (memArg)
11828 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11829 else
11830 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11831 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011832
Eric Christopher41c902f2010-11-30 08:20:21 +000011833 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011834 for (unsigned i = 0; i < numArgs; ++i) {
11835 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011836 if (!(Op.isReg() && Op.isImplicit()))
11837 MIB.addOperand(Op);
11838 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011839 BuildMI(*BB, MI, dl,
11840 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11841 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011842 .addReg(X86::XMM0);
11843
Dan Gohman14152b42010-07-06 20:24:04 +000011844 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011845 return BB;
11846}
11847
11848MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011849X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011850 DebugLoc dl = MI->getDebugLoc();
11851 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011852
Eric Christopher228232b2010-11-30 07:20:12 +000011853 // Address into RAX/EAX, other two args into ECX, EDX.
11854 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11855 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11856 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11857 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011858 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011859
Eric Christopher228232b2010-11-30 07:20:12 +000011860 unsigned ValOps = X86::AddrNumOperands;
11861 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11862 .addReg(MI->getOperand(ValOps).getReg());
11863 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11864 .addReg(MI->getOperand(ValOps+1).getReg());
11865
11866 // The instruction doesn't actually take any operands though.
11867 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011868
Eric Christopher228232b2010-11-30 07:20:12 +000011869 MI->eraseFromParent(); // The pseudo is gone now.
11870 return BB;
11871}
11872
11873MachineBasicBlock *
11874X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011875 DebugLoc dl = MI->getDebugLoc();
11876 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011877
Eric Christopher228232b2010-11-30 07:20:12 +000011878 // First arg in ECX, the second in EAX.
11879 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11880 .addReg(MI->getOperand(0).getReg());
11881 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11882 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011883
Eric Christopher228232b2010-11-30 07:20:12 +000011884 // The instruction doesn't actually take any operands though.
11885 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011886
Eric Christopher228232b2010-11-30 07:20:12 +000011887 MI->eraseFromParent(); // The pseudo is gone now.
11888 return BB;
11889}
11890
11891MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011892X86TargetLowering::EmitVAARG64WithCustomInserter(
11893 MachineInstr *MI,
11894 MachineBasicBlock *MBB) const {
11895 // Emit va_arg instruction on X86-64.
11896
11897 // Operands to this pseudo-instruction:
11898 // 0 ) Output : destination address (reg)
11899 // 1-5) Input : va_list address (addr, i64mem)
11900 // 6 ) ArgSize : Size (in bytes) of vararg type
11901 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11902 // 8 ) Align : Alignment of type
11903 // 9 ) EFLAGS (implicit-def)
11904
11905 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11906 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11907
11908 unsigned DestReg = MI->getOperand(0).getReg();
11909 MachineOperand &Base = MI->getOperand(1);
11910 MachineOperand &Scale = MI->getOperand(2);
11911 MachineOperand &Index = MI->getOperand(3);
11912 MachineOperand &Disp = MI->getOperand(4);
11913 MachineOperand &Segment = MI->getOperand(5);
11914 unsigned ArgSize = MI->getOperand(6).getImm();
11915 unsigned ArgMode = MI->getOperand(7).getImm();
11916 unsigned Align = MI->getOperand(8).getImm();
11917
11918 // Memory Reference
11919 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11920 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11921 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11922
11923 // Machine Information
11924 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11925 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11926 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11927 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11928 DebugLoc DL = MI->getDebugLoc();
11929
11930 // struct va_list {
11931 // i32 gp_offset
11932 // i32 fp_offset
11933 // i64 overflow_area (address)
11934 // i64 reg_save_area (address)
11935 // }
11936 // sizeof(va_list) = 24
11937 // alignment(va_list) = 8
11938
11939 unsigned TotalNumIntRegs = 6;
11940 unsigned TotalNumXMMRegs = 8;
11941 bool UseGPOffset = (ArgMode == 1);
11942 bool UseFPOffset = (ArgMode == 2);
11943 unsigned MaxOffset = TotalNumIntRegs * 8 +
11944 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11945
11946 /* Align ArgSize to a multiple of 8 */
11947 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11948 bool NeedsAlign = (Align > 8);
11949
11950 MachineBasicBlock *thisMBB = MBB;
11951 MachineBasicBlock *overflowMBB;
11952 MachineBasicBlock *offsetMBB;
11953 MachineBasicBlock *endMBB;
11954
11955 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11956 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11957 unsigned OffsetReg = 0;
11958
11959 if (!UseGPOffset && !UseFPOffset) {
11960 // If we only pull from the overflow region, we don't create a branch.
11961 // We don't need to alter control flow.
11962 OffsetDestReg = 0; // unused
11963 OverflowDestReg = DestReg;
11964
11965 offsetMBB = NULL;
11966 overflowMBB = thisMBB;
11967 endMBB = thisMBB;
11968 } else {
11969 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11970 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11971 // If not, pull from overflow_area. (branch to overflowMBB)
11972 //
11973 // thisMBB
11974 // | .
11975 // | .
11976 // offsetMBB overflowMBB
11977 // | .
11978 // | .
11979 // endMBB
11980
11981 // Registers for the PHI in endMBB
11982 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11983 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11984
11985 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11986 MachineFunction *MF = MBB->getParent();
11987 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11988 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11989 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11990
11991 MachineFunction::iterator MBBIter = MBB;
11992 ++MBBIter;
11993
11994 // Insert the new basic blocks
11995 MF->insert(MBBIter, offsetMBB);
11996 MF->insert(MBBIter, overflowMBB);
11997 MF->insert(MBBIter, endMBB);
11998
11999 // Transfer the remainder of MBB and its successor edges to endMBB.
12000 endMBB->splice(endMBB->begin(), thisMBB,
12001 llvm::next(MachineBasicBlock::iterator(MI)),
12002 thisMBB->end());
12003 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12004
12005 // Make offsetMBB and overflowMBB successors of thisMBB
12006 thisMBB->addSuccessor(offsetMBB);
12007 thisMBB->addSuccessor(overflowMBB);
12008
12009 // endMBB is a successor of both offsetMBB and overflowMBB
12010 offsetMBB->addSuccessor(endMBB);
12011 overflowMBB->addSuccessor(endMBB);
12012
12013 // Load the offset value into a register
12014 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12015 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12016 .addOperand(Base)
12017 .addOperand(Scale)
12018 .addOperand(Index)
12019 .addDisp(Disp, UseFPOffset ? 4 : 0)
12020 .addOperand(Segment)
12021 .setMemRefs(MMOBegin, MMOEnd);
12022
12023 // Check if there is enough room left to pull this argument.
12024 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12025 .addReg(OffsetReg)
12026 .addImm(MaxOffset + 8 - ArgSizeA8);
12027
12028 // Branch to "overflowMBB" if offset >= max
12029 // Fall through to "offsetMBB" otherwise
12030 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12031 .addMBB(overflowMBB);
12032 }
12033
12034 // In offsetMBB, emit code to use the reg_save_area.
12035 if (offsetMBB) {
12036 assert(OffsetReg != 0);
12037
12038 // Read the reg_save_area address.
12039 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12040 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12041 .addOperand(Base)
12042 .addOperand(Scale)
12043 .addOperand(Index)
12044 .addDisp(Disp, 16)
12045 .addOperand(Segment)
12046 .setMemRefs(MMOBegin, MMOEnd);
12047
12048 // Zero-extend the offset
12049 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12050 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12051 .addImm(0)
12052 .addReg(OffsetReg)
12053 .addImm(X86::sub_32bit);
12054
12055 // Add the offset to the reg_save_area to get the final address.
12056 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12057 .addReg(OffsetReg64)
12058 .addReg(RegSaveReg);
12059
12060 // Compute the offset for the next argument
12061 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12062 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12063 .addReg(OffsetReg)
12064 .addImm(UseFPOffset ? 16 : 8);
12065
12066 // Store it back into the va_list.
12067 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12068 .addOperand(Base)
12069 .addOperand(Scale)
12070 .addOperand(Index)
12071 .addDisp(Disp, UseFPOffset ? 4 : 0)
12072 .addOperand(Segment)
12073 .addReg(NextOffsetReg)
12074 .setMemRefs(MMOBegin, MMOEnd);
12075
12076 // Jump to endMBB
12077 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12078 .addMBB(endMBB);
12079 }
12080
12081 //
12082 // Emit code to use overflow area
12083 //
12084
12085 // Load the overflow_area address into a register.
12086 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12087 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12088 .addOperand(Base)
12089 .addOperand(Scale)
12090 .addOperand(Index)
12091 .addDisp(Disp, 8)
12092 .addOperand(Segment)
12093 .setMemRefs(MMOBegin, MMOEnd);
12094
12095 // If we need to align it, do so. Otherwise, just copy the address
12096 // to OverflowDestReg.
12097 if (NeedsAlign) {
12098 // Align the overflow address
12099 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12100 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12101
12102 // aligned_addr = (addr + (align-1)) & ~(align-1)
12103 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12104 .addReg(OverflowAddrReg)
12105 .addImm(Align-1);
12106
12107 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12108 .addReg(TmpReg)
12109 .addImm(~(uint64_t)(Align-1));
12110 } else {
12111 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12112 .addReg(OverflowAddrReg);
12113 }
12114
12115 // Compute the next overflow address after this argument.
12116 // (the overflow address should be kept 8-byte aligned)
12117 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12118 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12119 .addReg(OverflowDestReg)
12120 .addImm(ArgSizeA8);
12121
12122 // Store the new overflow address.
12123 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12124 .addOperand(Base)
12125 .addOperand(Scale)
12126 .addOperand(Index)
12127 .addDisp(Disp, 8)
12128 .addOperand(Segment)
12129 .addReg(NextAddrReg)
12130 .setMemRefs(MMOBegin, MMOEnd);
12131
12132 // If we branched, emit the PHI to the front of endMBB.
12133 if (offsetMBB) {
12134 BuildMI(*endMBB, endMBB->begin(), DL,
12135 TII->get(X86::PHI), DestReg)
12136 .addReg(OffsetDestReg).addMBB(offsetMBB)
12137 .addReg(OverflowDestReg).addMBB(overflowMBB);
12138 }
12139
12140 // Erase the pseudo instruction
12141 MI->eraseFromParent();
12142
12143 return endMBB;
12144}
12145
12146MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012147X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12148 MachineInstr *MI,
12149 MachineBasicBlock *MBB) const {
12150 // Emit code to save XMM registers to the stack. The ABI says that the
12151 // number of registers to save is given in %al, so it's theoretically
12152 // possible to do an indirect jump trick to avoid saving all of them,
12153 // however this code takes a simpler approach and just executes all
12154 // of the stores if %al is non-zero. It's less code, and it's probably
12155 // easier on the hardware branch predictor, and stores aren't all that
12156 // expensive anyway.
12157
12158 // Create the new basic blocks. One block contains all the XMM stores,
12159 // and one block is the final destination regardless of whether any
12160 // stores were performed.
12161 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12162 MachineFunction *F = MBB->getParent();
12163 MachineFunction::iterator MBBIter = MBB;
12164 ++MBBIter;
12165 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12166 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12167 F->insert(MBBIter, XMMSaveMBB);
12168 F->insert(MBBIter, EndMBB);
12169
Dan Gohman14152b42010-07-06 20:24:04 +000012170 // Transfer the remainder of MBB and its successor edges to EndMBB.
12171 EndMBB->splice(EndMBB->begin(), MBB,
12172 llvm::next(MachineBasicBlock::iterator(MI)),
12173 MBB->end());
12174 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12175
Dan Gohmand6708ea2009-08-15 01:38:56 +000012176 // The original block will now fall through to the XMM save block.
12177 MBB->addSuccessor(XMMSaveMBB);
12178 // The XMMSaveMBB will fall through to the end block.
12179 XMMSaveMBB->addSuccessor(EndMBB);
12180
12181 // Now add the instructions.
12182 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12183 DebugLoc DL = MI->getDebugLoc();
12184
12185 unsigned CountReg = MI->getOperand(0).getReg();
12186 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12187 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12188
12189 if (!Subtarget->isTargetWin64()) {
12190 // If %al is 0, branch around the XMM save block.
12191 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012192 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012193 MBB->addSuccessor(EndMBB);
12194 }
12195
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012196 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012197 // In the XMM save block, save all the XMM argument registers.
12198 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12199 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012200 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012201 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012202 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012203 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012204 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012205 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012206 .addFrameIndex(RegSaveFrameIndex)
12207 .addImm(/*Scale=*/1)
12208 .addReg(/*IndexReg=*/0)
12209 .addImm(/*Disp=*/Offset)
12210 .addReg(/*Segment=*/0)
12211 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012212 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012213 }
12214
Dan Gohman14152b42010-07-06 20:24:04 +000012215 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012216
12217 return EndMBB;
12218}
Mon P Wang63307c32008-05-05 19:05:59 +000012219
Evan Cheng60c07e12006-07-05 22:17:51 +000012220MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012221X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012222 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012223 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12224 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012225
Chris Lattner52600972009-09-02 05:57:00 +000012226 // To "insert" a SELECT_CC instruction, we actually have to insert the
12227 // diamond control-flow pattern. The incoming instruction knows the
12228 // destination vreg to set, the condition code register to branch on, the
12229 // true/false values to select between, and a branch opcode to use.
12230 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12231 MachineFunction::iterator It = BB;
12232 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012233
Chris Lattner52600972009-09-02 05:57:00 +000012234 // thisMBB:
12235 // ...
12236 // TrueVal = ...
12237 // cmpTY ccX, r1, r2
12238 // bCC copy1MBB
12239 // fallthrough --> copy0MBB
12240 MachineBasicBlock *thisMBB = BB;
12241 MachineFunction *F = BB->getParent();
12242 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12243 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012244 F->insert(It, copy0MBB);
12245 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012246
Bill Wendling730c07e2010-06-25 20:48:10 +000012247 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12248 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000012249 if (!MI->killsRegister(X86::EFLAGS)) {
12250 copy0MBB->addLiveIn(X86::EFLAGS);
12251 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012252 }
12253
Dan Gohman14152b42010-07-06 20:24:04 +000012254 // Transfer the remainder of BB and its successor edges to sinkMBB.
12255 sinkMBB->splice(sinkMBB->begin(), BB,
12256 llvm::next(MachineBasicBlock::iterator(MI)),
12257 BB->end());
12258 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12259
12260 // Add the true and fallthrough blocks as its successors.
12261 BB->addSuccessor(copy0MBB);
12262 BB->addSuccessor(sinkMBB);
12263
12264 // Create the conditional branch instruction.
12265 unsigned Opc =
12266 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12267 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12268
Chris Lattner52600972009-09-02 05:57:00 +000012269 // copy0MBB:
12270 // %FalseValue = ...
12271 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012272 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012273
Chris Lattner52600972009-09-02 05:57:00 +000012274 // sinkMBB:
12275 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12276 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012277 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12278 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012279 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12280 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12281
Dan Gohman14152b42010-07-06 20:24:04 +000012282 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012283 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012284}
12285
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012286MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012287X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12288 bool Is64Bit) const {
12289 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12290 DebugLoc DL = MI->getDebugLoc();
12291 MachineFunction *MF = BB->getParent();
12292 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12293
12294 assert(EnableSegmentedStacks);
12295
12296 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12297 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12298
12299 // BB:
12300 // ... [Till the alloca]
12301 // If stacklet is not large enough, jump to mallocMBB
12302 //
12303 // bumpMBB:
12304 // Allocate by subtracting from RSP
12305 // Jump to continueMBB
12306 //
12307 // mallocMBB:
12308 // Allocate by call to runtime
12309 //
12310 // continueMBB:
12311 // ...
12312 // [rest of original BB]
12313 //
12314
12315 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12316 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12317 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12318
12319 MachineRegisterInfo &MRI = MF->getRegInfo();
12320 const TargetRegisterClass *AddrRegClass =
12321 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12322
12323 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12324 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12325 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012326 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012327 sizeVReg = MI->getOperand(1).getReg(),
12328 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12329
12330 MachineFunction::iterator MBBIter = BB;
12331 ++MBBIter;
12332
12333 MF->insert(MBBIter, bumpMBB);
12334 MF->insert(MBBIter, mallocMBB);
12335 MF->insert(MBBIter, continueMBB);
12336
12337 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12338 (MachineBasicBlock::iterator(MI)), BB->end());
12339 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12340
12341 // Add code to the main basic block to check if the stack limit has been hit,
12342 // and if so, jump to mallocMBB otherwise to bumpMBB.
12343 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012344 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012345 .addReg(tmpSPVReg).addReg(sizeVReg);
12346 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
12347 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012348 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012349 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12350
12351 // bumpMBB simply decreases the stack pointer, since we know the current
12352 // stacklet has enough space.
12353 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012354 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012355 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012356 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012357 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12358
12359 // Calls into a routine in libgcc to allocate more space from the heap.
12360 if (Is64Bit) {
12361 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12362 .addReg(sizeVReg);
12363 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12364 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12365 } else {
12366 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12367 .addImm(12);
12368 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12369 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12370 .addExternalSymbol("__morestack_allocate_stack_space");
12371 }
12372
12373 if (!Is64Bit)
12374 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12375 .addImm(16);
12376
12377 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12378 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12379 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12380
12381 // Set up the CFG correctly.
12382 BB->addSuccessor(bumpMBB);
12383 BB->addSuccessor(mallocMBB);
12384 mallocMBB->addSuccessor(continueMBB);
12385 bumpMBB->addSuccessor(continueMBB);
12386
12387 // Take care of the PHI nodes.
12388 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12389 MI->getOperand(0).getReg())
12390 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12391 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12392
12393 // Delete the original pseudo instruction.
12394 MI->eraseFromParent();
12395
12396 // And we're done.
12397 return continueMBB;
12398}
12399
12400MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012401X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012402 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012403 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12404 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012405
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012406 assert(!Subtarget->isTargetEnvMacho());
12407
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012408 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12409 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012410
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012411 if (Subtarget->isTargetWin64()) {
12412 if (Subtarget->isTargetCygMing()) {
12413 // ___chkstk(Mingw64):
12414 // Clobbers R10, R11, RAX and EFLAGS.
12415 // Updates RSP.
12416 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12417 .addExternalSymbol("___chkstk")
12418 .addReg(X86::RAX, RegState::Implicit)
12419 .addReg(X86::RSP, RegState::Implicit)
12420 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12421 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12422 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12423 } else {
12424 // __chkstk(MSVCRT): does not update stack pointer.
12425 // Clobbers R10, R11 and EFLAGS.
12426 // FIXME: RAX(allocated size) might be reused and not killed.
12427 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12428 .addExternalSymbol("__chkstk")
12429 .addReg(X86::RAX, RegState::Implicit)
12430 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12431 // RAX has the offset to subtracted from RSP.
12432 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12433 .addReg(X86::RSP)
12434 .addReg(X86::RAX);
12435 }
12436 } else {
12437 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012438 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12439
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012440 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12441 .addExternalSymbol(StackProbeSymbol)
12442 .addReg(X86::EAX, RegState::Implicit)
12443 .addReg(X86::ESP, RegState::Implicit)
12444 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12445 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12446 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12447 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012448
Dan Gohman14152b42010-07-06 20:24:04 +000012449 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012450 return BB;
12451}
Chris Lattner52600972009-09-02 05:57:00 +000012452
12453MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012454X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12455 MachineBasicBlock *BB) const {
12456 // This is pretty easy. We're taking the value that we received from
12457 // our load from the relocation, sticking it in either RDI (x86-64)
12458 // or EAX and doing an indirect call. The return value will then
12459 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012460 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012461 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012462 DebugLoc DL = MI->getDebugLoc();
12463 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012464
12465 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012466 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012467
Eric Christopher30ef0e52010-06-03 04:07:48 +000012468 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012469 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12470 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012471 .addReg(X86::RIP)
12472 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012473 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012474 MI->getOperand(3).getTargetFlags())
12475 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012476 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012477 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012478 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012479 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12480 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012481 .addReg(0)
12482 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012483 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012484 MI->getOperand(3).getTargetFlags())
12485 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012486 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012487 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012488 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012489 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12490 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012491 .addReg(TII->getGlobalBaseReg(F))
12492 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012493 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +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 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012499
Dan Gohman14152b42010-07-06 20:24:04 +000012500 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012501 return BB;
12502}
12503
12504MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012505X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012506 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012507 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000012508 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012509 case X86::TAILJMPd64:
12510 case X86::TAILJMPr64:
12511 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000012512 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012513 case X86::TCRETURNdi64:
12514 case X86::TCRETURNri64:
12515 case X86::TCRETURNmi64:
12516 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12517 // On AMD64, additional defs should be added before register allocation.
12518 if (!Subtarget->isTargetWin64()) {
12519 MI->addRegisterDefined(X86::RSI);
12520 MI->addRegisterDefined(X86::RDI);
12521 MI->addRegisterDefined(X86::XMM6);
12522 MI->addRegisterDefined(X86::XMM7);
12523 MI->addRegisterDefined(X86::XMM8);
12524 MI->addRegisterDefined(X86::XMM9);
12525 MI->addRegisterDefined(X86::XMM10);
12526 MI->addRegisterDefined(X86::XMM11);
12527 MI->addRegisterDefined(X86::XMM12);
12528 MI->addRegisterDefined(X86::XMM13);
12529 MI->addRegisterDefined(X86::XMM14);
12530 MI->addRegisterDefined(X86::XMM15);
12531 }
12532 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012533 case X86::WIN_ALLOCA:
12534 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012535 case X86::SEG_ALLOCA_32:
12536 return EmitLoweredSegAlloca(MI, BB, false);
12537 case X86::SEG_ALLOCA_64:
12538 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012539 case X86::TLSCall_32:
12540 case X86::TLSCall_64:
12541 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012542 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012543 case X86::CMOV_FR32:
12544 case X86::CMOV_FR64:
12545 case X86::CMOV_V4F32:
12546 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012547 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012548 case X86::CMOV_V8F32:
12549 case X86::CMOV_V4F64:
12550 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012551 case X86::CMOV_GR16:
12552 case X86::CMOV_GR32:
12553 case X86::CMOV_RFP32:
12554 case X86::CMOV_RFP64:
12555 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012556 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012557
Dale Johannesen849f2142007-07-03 00:53:03 +000012558 case X86::FP32_TO_INT16_IN_MEM:
12559 case X86::FP32_TO_INT32_IN_MEM:
12560 case X86::FP32_TO_INT64_IN_MEM:
12561 case X86::FP64_TO_INT16_IN_MEM:
12562 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012563 case X86::FP64_TO_INT64_IN_MEM:
12564 case X86::FP80_TO_INT16_IN_MEM:
12565 case X86::FP80_TO_INT32_IN_MEM:
12566 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012567 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12568 DebugLoc DL = MI->getDebugLoc();
12569
Evan Cheng60c07e12006-07-05 22:17:51 +000012570 // Change the floating point control register to use "round towards zero"
12571 // mode when truncating to an integer value.
12572 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012573 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012574 addFrameReference(BuildMI(*BB, MI, DL,
12575 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012576
12577 // Load the old value of the high byte of the control word...
12578 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012579 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012580 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012581 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012582
12583 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012584 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012585 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012586
12587 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012588 addFrameReference(BuildMI(*BB, MI, DL,
12589 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012590
12591 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012592 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012593 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012594
12595 // Get the X86 opcode to use.
12596 unsigned Opc;
12597 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012598 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012599 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12600 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12601 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12602 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12603 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12604 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012605 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12606 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12607 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012608 }
12609
12610 X86AddressMode AM;
12611 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012612 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012613 AM.BaseType = X86AddressMode::RegBase;
12614 AM.Base.Reg = Op.getReg();
12615 } else {
12616 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012617 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012618 }
12619 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012620 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012621 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012622 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012623 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012624 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012625 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012626 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012627 AM.GV = Op.getGlobal();
12628 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012629 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012630 }
Dan Gohman14152b42010-07-06 20:24:04 +000012631 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012632 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012633
12634 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012635 addFrameReference(BuildMI(*BB, MI, DL,
12636 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012637
Dan Gohman14152b42010-07-06 20:24:04 +000012638 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012639 return BB;
12640 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012641 // String/text processing lowering.
12642 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012643 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012644 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12645 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012646 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012647 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12648 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012649 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012650 return EmitPCMP(MI, BB, 5, false /* in mem */);
12651 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012652 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012653 return EmitPCMP(MI, BB, 5, true /* in mem */);
12654
Eric Christopher228232b2010-11-30 07:20:12 +000012655 // Thread synchronization.
12656 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012657 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012658 case X86::MWAIT:
12659 return EmitMwait(MI, BB);
12660
Eric Christopherb120ab42009-08-18 22:50:32 +000012661 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012662 case X86::ATOMAND32:
12663 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012664 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012665 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012666 X86::NOT32r, X86::EAX,
12667 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012668 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012669 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12670 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012671 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012672 X86::NOT32r, X86::EAX,
12673 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012674 case X86::ATOMXOR32:
12675 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012676 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012677 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012678 X86::NOT32r, X86::EAX,
12679 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012680 case X86::ATOMNAND32:
12681 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012682 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012683 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012684 X86::NOT32r, X86::EAX,
12685 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012686 case X86::ATOMMIN32:
12687 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12688 case X86::ATOMMAX32:
12689 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12690 case X86::ATOMUMIN32:
12691 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12692 case X86::ATOMUMAX32:
12693 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012694
12695 case X86::ATOMAND16:
12696 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12697 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012698 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012699 X86::NOT16r, X86::AX,
12700 X86::GR16RegisterClass);
12701 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012702 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012703 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012704 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012705 X86::NOT16r, X86::AX,
12706 X86::GR16RegisterClass);
12707 case X86::ATOMXOR16:
12708 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12709 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012710 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012711 X86::NOT16r, X86::AX,
12712 X86::GR16RegisterClass);
12713 case X86::ATOMNAND16:
12714 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12715 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012716 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012717 X86::NOT16r, X86::AX,
12718 X86::GR16RegisterClass, true);
12719 case X86::ATOMMIN16:
12720 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12721 case X86::ATOMMAX16:
12722 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12723 case X86::ATOMUMIN16:
12724 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12725 case X86::ATOMUMAX16:
12726 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12727
12728 case X86::ATOMAND8:
12729 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12730 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012731 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012732 X86::NOT8r, X86::AL,
12733 X86::GR8RegisterClass);
12734 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012735 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012736 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012737 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012738 X86::NOT8r, X86::AL,
12739 X86::GR8RegisterClass);
12740 case X86::ATOMXOR8:
12741 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12742 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012743 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012744 X86::NOT8r, X86::AL,
12745 X86::GR8RegisterClass);
12746 case X86::ATOMNAND8:
12747 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12748 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012749 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012750 X86::NOT8r, X86::AL,
12751 X86::GR8RegisterClass, true);
12752 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012753 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012754 case X86::ATOMAND64:
12755 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012756 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012757 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012758 X86::NOT64r, X86::RAX,
12759 X86::GR64RegisterClass);
12760 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012761 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12762 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012763 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012764 X86::NOT64r, X86::RAX,
12765 X86::GR64RegisterClass);
12766 case X86::ATOMXOR64:
12767 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012768 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012769 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012770 X86::NOT64r, X86::RAX,
12771 X86::GR64RegisterClass);
12772 case X86::ATOMNAND64:
12773 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12774 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012775 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012776 X86::NOT64r, X86::RAX,
12777 X86::GR64RegisterClass, true);
12778 case X86::ATOMMIN64:
12779 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12780 case X86::ATOMMAX64:
12781 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12782 case X86::ATOMUMIN64:
12783 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12784 case X86::ATOMUMAX64:
12785 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012786
12787 // This group does 64-bit operations on a 32-bit host.
12788 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012789 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012790 X86::AND32rr, X86::AND32rr,
12791 X86::AND32ri, X86::AND32ri,
12792 false);
12793 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012794 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012795 X86::OR32rr, X86::OR32rr,
12796 X86::OR32ri, X86::OR32ri,
12797 false);
12798 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012799 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012800 X86::XOR32rr, X86::XOR32rr,
12801 X86::XOR32ri, X86::XOR32ri,
12802 false);
12803 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012804 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012805 X86::AND32rr, X86::AND32rr,
12806 X86::AND32ri, X86::AND32ri,
12807 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012808 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012809 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012810 X86::ADD32rr, X86::ADC32rr,
12811 X86::ADD32ri, X86::ADC32ri,
12812 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012813 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012814 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012815 X86::SUB32rr, X86::SBB32rr,
12816 X86::SUB32ri, X86::SBB32ri,
12817 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012818 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012819 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012820 X86::MOV32rr, X86::MOV32rr,
12821 X86::MOV32ri, X86::MOV32ri,
12822 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012823 case X86::VASTART_SAVE_XMM_REGS:
12824 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012825
12826 case X86::VAARG_64:
12827 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012828 }
12829}
12830
12831//===----------------------------------------------------------------------===//
12832// X86 Optimization Hooks
12833//===----------------------------------------------------------------------===//
12834
Dan Gohman475871a2008-07-27 21:46:04 +000012835void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012836 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012837 APInt &KnownZero,
12838 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012839 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012840 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012841 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012842 assert((Opc >= ISD::BUILTIN_OP_END ||
12843 Opc == ISD::INTRINSIC_WO_CHAIN ||
12844 Opc == ISD::INTRINSIC_W_CHAIN ||
12845 Opc == ISD::INTRINSIC_VOID) &&
12846 "Should use MaskedValueIsZero if you don't know whether Op"
12847 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012848
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012849 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012850 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012851 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012852 case X86ISD::ADD:
12853 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012854 case X86ISD::ADC:
12855 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012856 case X86ISD::SMUL:
12857 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012858 case X86ISD::INC:
12859 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012860 case X86ISD::OR:
12861 case X86ISD::XOR:
12862 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012863 // These nodes' second result is a boolean.
12864 if (Op.getResNo() == 0)
12865 break;
12866 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012867 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012868 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12869 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012870 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012871 case ISD::INTRINSIC_WO_CHAIN: {
12872 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12873 unsigned NumLoBits = 0;
12874 switch (IntId) {
12875 default: break;
12876 case Intrinsic::x86_sse_movmsk_ps:
12877 case Intrinsic::x86_avx_movmsk_ps_256:
12878 case Intrinsic::x86_sse2_movmsk_pd:
12879 case Intrinsic::x86_avx_movmsk_pd_256:
12880 case Intrinsic::x86_mmx_pmovmskb:
12881 case Intrinsic::x86_sse2_pmovmskb_128: {
12882 // High bits of movmskp{s|d}, pmovmskb are known zero.
12883 switch (IntId) {
12884 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12885 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12886 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12887 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12888 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12889 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
12890 }
12891 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12892 Mask.getBitWidth() - NumLoBits);
12893 break;
12894 }
12895 }
12896 break;
12897 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012898 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012899}
Chris Lattner259e97c2006-01-31 19:43:35 +000012900
Owen Andersonbc146b02010-09-21 20:42:50 +000012901unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12902 unsigned Depth) const {
12903 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12904 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12905 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012906
Owen Andersonbc146b02010-09-21 20:42:50 +000012907 // Fallback case.
12908 return 1;
12909}
12910
Evan Cheng206ee9d2006-07-07 08:33:52 +000012911/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012912/// node is a GlobalAddress + offset.
12913bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012914 const GlobalValue* &GA,
12915 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012916 if (N->getOpcode() == X86ISD::Wrapper) {
12917 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012918 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012919 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012920 return true;
12921 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012922 }
Evan Chengad4196b2008-05-12 19:56:52 +000012923 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012924}
12925
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012926/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12927/// same as extracting the high 128-bit part of 256-bit vector and then
12928/// inserting the result into the low part of a new 256-bit vector
12929static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12930 EVT VT = SVOp->getValueType(0);
12931 int NumElems = VT.getVectorNumElements();
12932
12933 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12934 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12935 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12936 SVOp->getMaskElt(j) >= 0)
12937 return false;
12938
12939 return true;
12940}
12941
12942/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12943/// same as extracting the low 128-bit part of 256-bit vector and then
12944/// inserting the result into the high part of a new 256-bit vector
12945static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12946 EVT VT = SVOp->getValueType(0);
12947 int NumElems = VT.getVectorNumElements();
12948
12949 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12950 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12951 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12952 SVOp->getMaskElt(j) >= 0)
12953 return false;
12954
12955 return true;
12956}
12957
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012958/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12959static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12960 TargetLowering::DAGCombinerInfo &DCI) {
12961 DebugLoc dl = N->getDebugLoc();
12962 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12963 SDValue V1 = SVOp->getOperand(0);
12964 SDValue V2 = SVOp->getOperand(1);
12965 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012966 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012967
12968 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12969 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12970 //
12971 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012972 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012973 // V UNDEF BUILD_VECTOR UNDEF
12974 // \ / \ /
12975 // CONCAT_VECTOR CONCAT_VECTOR
12976 // \ /
12977 // \ /
12978 // RESULT: V + zero extended
12979 //
12980 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12981 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12982 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12983 return SDValue();
12984
12985 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12986 return SDValue();
12987
12988 // To match the shuffle mask, the first half of the mask should
12989 // be exactly the first vector, and all the rest a splat with the
12990 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012991 for (int i = 0; i < NumElems/2; ++i)
12992 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12993 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12994 return SDValue();
12995
12996 // Emit a zeroed vector and insert the desired subvector on its
12997 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012998 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012999 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
13000 DAG.getConstant(0, MVT::i32), DAG, dl);
13001 return DCI.CombineTo(N, InsV);
13002 }
13003
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013004 //===--------------------------------------------------------------------===//
13005 // Combine some shuffles into subvector extracts and inserts:
13006 //
13007
13008 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13009 if (isShuffleHigh128VectorInsertLow(SVOp)) {
13010 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
13011 DAG, dl);
13012 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
13013 V, DAG.getConstant(0, MVT::i32), DAG, dl);
13014 return DCI.CombineTo(N, InsV);
13015 }
13016
13017 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13018 if (isShuffleLow128VectorInsertHigh(SVOp)) {
13019 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
13020 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
13021 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
13022 return DCI.CombineTo(N, InsV);
13023 }
13024
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013025 return SDValue();
13026}
13027
13028/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013029static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013030 TargetLowering::DAGCombinerInfo &DCI,
13031 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013032 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013033 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013034
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013035 // Don't create instructions with illegal types after legalize types has run.
13036 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13037 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
13038 return SDValue();
13039
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013040 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
13041 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
13042 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013043 return PerformShuffleCombine256(N, DAG, DCI);
13044
13045 // Only handle 128 wide vector from here on.
13046 if (VT.getSizeInBits() != 128)
13047 return SDValue();
13048
13049 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
13050 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
13051 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000013052 SmallVector<SDValue, 16> Elts;
13053 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013054 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000013055
Nate Begemanfdea31a2010-03-24 20:49:50 +000013056 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000013057}
Evan Chengd880b972008-05-09 21:53:03 +000013058
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000013059/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
13060/// generation and convert it from being a bunch of shuffles and extracts
13061/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013062static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
13063 const TargetLowering &TLI) {
13064 SDValue InputVector = N->getOperand(0);
13065
13066 // Only operate on vectors of 4 elements, where the alternative shuffling
13067 // gets to be more expensive.
13068 if (InputVector.getValueType() != MVT::v4i32)
13069 return SDValue();
13070
13071 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
13072 // single use which is a sign-extend or zero-extend, and all elements are
13073 // used.
13074 SmallVector<SDNode *, 4> Uses;
13075 unsigned ExtractedElements = 0;
13076 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
13077 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
13078 if (UI.getUse().getResNo() != InputVector.getResNo())
13079 return SDValue();
13080
13081 SDNode *Extract = *UI;
13082 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13083 return SDValue();
13084
13085 if (Extract->getValueType(0) != MVT::i32)
13086 return SDValue();
13087 if (!Extract->hasOneUse())
13088 return SDValue();
13089 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
13090 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
13091 return SDValue();
13092 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
13093 return SDValue();
13094
13095 // Record which element was extracted.
13096 ExtractedElements |=
13097 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
13098
13099 Uses.push_back(Extract);
13100 }
13101
13102 // If not all the elements were used, this may not be worthwhile.
13103 if (ExtractedElements != 15)
13104 return SDValue();
13105
13106 // Ok, we've now decided to do the transformation.
13107 DebugLoc dl = InputVector.getDebugLoc();
13108
13109 // Store the value to a temporary stack slot.
13110 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013111 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13112 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013113
13114 // Replace each use (extract) with a load of the appropriate element.
13115 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13116 UE = Uses.end(); UI != UE; ++UI) {
13117 SDNode *Extract = *UI;
13118
Nadav Rotem86694292011-05-17 08:31:57 +000013119 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013120 SDValue Idx = Extract->getOperand(1);
13121 unsigned EltSize =
13122 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
13123 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
13124 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
13125
Nadav Rotem86694292011-05-17 08:31:57 +000013126 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013127 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013128
13129 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013130 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013131 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013132 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013133
13134 // Replace the exact with the load.
13135 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13136 }
13137
13138 // The replacement was made in place; don't return anything.
13139 return SDValue();
13140}
13141
Duncan Sands6bcd2192011-09-17 16:49:39 +000013142/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13143/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013144static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013145 const X86Subtarget *Subtarget) {
13146 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013147 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013148 // Get the LHS/RHS of the select.
13149 SDValue LHS = N->getOperand(1);
13150 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013151 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013152
Dan Gohman670e5392009-09-21 18:03:22 +000013153 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013154 // instructions match the semantics of the common C idiom x<y?x:y but not
13155 // x<=y?x:y, because of how they handle negative zero (which can be
13156 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013157 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13158 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
13159 (Subtarget->hasXMMInt() ||
13160 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013161 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013162
Chris Lattner47b4ce82009-03-11 05:48:52 +000013163 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013164 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013165 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13166 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013167 switch (CC) {
13168 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013169 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013170 // Converting this to a min would handle NaNs incorrectly, and swapping
13171 // the operands would cause it to handle comparisons between positive
13172 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013173 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000013174 if (!UnsafeFPMath &&
13175 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13176 break;
13177 std::swap(LHS, RHS);
13178 }
Dan Gohman670e5392009-09-21 18:03:22 +000013179 Opcode = X86ISD::FMIN;
13180 break;
13181 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013182 // Converting this to a min would handle comparisons between positive
13183 // and negative zero incorrectly.
13184 if (!UnsafeFPMath &&
13185 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13186 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013187 Opcode = X86ISD::FMIN;
13188 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013189 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013190 // Converting this to a min would handle both negative zeros and NaNs
13191 // incorrectly, but we can swap the operands to fix both.
13192 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013193 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013194 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013195 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013196 Opcode = X86ISD::FMIN;
13197 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013198
Dan Gohman670e5392009-09-21 18:03:22 +000013199 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013200 // Converting this to a max would handle comparisons between positive
13201 // and negative zero incorrectly.
13202 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013203 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013204 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013205 Opcode = X86ISD::FMAX;
13206 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013207 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013208 // Converting this to a max would handle NaNs incorrectly, and swapping
13209 // the operands would cause it to handle comparisons between positive
13210 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013211 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000013212 if (!UnsafeFPMath &&
13213 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13214 break;
13215 std::swap(LHS, RHS);
13216 }
Dan Gohman670e5392009-09-21 18:03:22 +000013217 Opcode = X86ISD::FMAX;
13218 break;
13219 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013220 // Converting this to a max would handle both negative zeros and NaNs
13221 // incorrectly, but we can swap the operands to fix both.
13222 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013223 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013224 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013225 case ISD::SETGE:
13226 Opcode = X86ISD::FMAX;
13227 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013228 }
Dan Gohman670e5392009-09-21 18:03:22 +000013229 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013230 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13231 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013232 switch (CC) {
13233 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013234 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013235 // Converting this to a min would handle comparisons between positive
13236 // and negative zero incorrectly, and swapping the operands would
13237 // cause it to handle NaNs incorrectly.
13238 if (!UnsafeFPMath &&
13239 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013240 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013241 break;
13242 std::swap(LHS, RHS);
13243 }
Dan Gohman670e5392009-09-21 18:03:22 +000013244 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013245 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013246 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013247 // Converting this to a min would handle NaNs incorrectly.
13248 if (!UnsafeFPMath &&
13249 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13250 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013251 Opcode = X86ISD::FMIN;
13252 break;
13253 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013254 // Converting this to a min would handle both negative zeros and NaNs
13255 // incorrectly, but we can swap the operands to fix both.
13256 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013257 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013258 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013259 case ISD::SETGE:
13260 Opcode = X86ISD::FMIN;
13261 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013262
Dan Gohman670e5392009-09-21 18:03:22 +000013263 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013264 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013265 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013266 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013267 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013268 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013269 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013270 // Converting this to a max would handle comparisons between positive
13271 // and negative zero incorrectly, and swapping the operands would
13272 // cause it to handle NaNs incorrectly.
13273 if (!UnsafeFPMath &&
13274 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013275 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013276 break;
13277 std::swap(LHS, RHS);
13278 }
Dan Gohman670e5392009-09-21 18:03:22 +000013279 Opcode = X86ISD::FMAX;
13280 break;
13281 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013282 // Converting this to a max would handle both negative zeros and NaNs
13283 // incorrectly, but we can swap the operands to fix both.
13284 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013285 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013286 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013287 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013288 Opcode = X86ISD::FMAX;
13289 break;
13290 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013291 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013292
Chris Lattner47b4ce82009-03-11 05:48:52 +000013293 if (Opcode)
13294 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013295 }
Eric Christopherfd179292009-08-27 18:07:15 +000013296
Chris Lattnerd1980a52009-03-12 06:52:53 +000013297 // If this is a select between two integer constants, try to do some
13298 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013299 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13300 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013301 // Don't do this for crazy integer types.
13302 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13303 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013304 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013305 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013306
Chris Lattnercee56e72009-03-13 05:53:31 +000013307 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013308 // Efficiently invertible.
13309 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13310 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13311 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13312 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013313 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013314 }
Eric Christopherfd179292009-08-27 18:07:15 +000013315
Chris Lattnerd1980a52009-03-12 06:52:53 +000013316 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013317 if (FalseC->getAPIntValue() == 0 &&
13318 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013319 if (NeedsCondInvert) // Invert the condition if needed.
13320 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13321 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013322
Chris Lattnerd1980a52009-03-12 06:52:53 +000013323 // Zero extend the condition if needed.
13324 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013325
Chris Lattnercee56e72009-03-13 05:53:31 +000013326 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013327 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013328 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013329 }
Eric Christopherfd179292009-08-27 18:07:15 +000013330
Chris Lattner97a29a52009-03-13 05:22:11 +000013331 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013332 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013333 if (NeedsCondInvert) // Invert the condition if needed.
13334 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13335 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013336
Chris Lattner97a29a52009-03-13 05:22:11 +000013337 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013338 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13339 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013340 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013341 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013342 }
Eric Christopherfd179292009-08-27 18:07:15 +000013343
Chris Lattnercee56e72009-03-13 05:53:31 +000013344 // Optimize cases that will turn into an LEA instruction. This requires
13345 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013346 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013347 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013348 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013349
Chris Lattnercee56e72009-03-13 05:53:31 +000013350 bool isFastMultiplier = false;
13351 if (Diff < 10) {
13352 switch ((unsigned char)Diff) {
13353 default: break;
13354 case 1: // result = add base, cond
13355 case 2: // result = lea base( , cond*2)
13356 case 3: // result = lea base(cond, cond*2)
13357 case 4: // result = lea base( , cond*4)
13358 case 5: // result = lea base(cond, cond*4)
13359 case 8: // result = lea base( , cond*8)
13360 case 9: // result = lea base(cond, cond*8)
13361 isFastMultiplier = true;
13362 break;
13363 }
13364 }
Eric Christopherfd179292009-08-27 18:07:15 +000013365
Chris Lattnercee56e72009-03-13 05:53:31 +000013366 if (isFastMultiplier) {
13367 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13368 if (NeedsCondInvert) // Invert the condition if needed.
13369 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13370 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013371
Chris Lattnercee56e72009-03-13 05:53:31 +000013372 // Zero extend the condition if needed.
13373 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13374 Cond);
13375 // Scale the condition by the difference.
13376 if (Diff != 1)
13377 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13378 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013379
Chris Lattnercee56e72009-03-13 05:53:31 +000013380 // Add the base if non-zero.
13381 if (FalseC->getAPIntValue() != 0)
13382 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13383 SDValue(FalseC, 0));
13384 return Cond;
13385 }
Eric Christopherfd179292009-08-27 18:07:15 +000013386 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013387 }
13388 }
Eric Christopherfd179292009-08-27 18:07:15 +000013389
Dan Gohman475871a2008-07-27 21:46:04 +000013390 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013391}
13392
Chris Lattnerd1980a52009-03-12 06:52:53 +000013393/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13394static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13395 TargetLowering::DAGCombinerInfo &DCI) {
13396 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013397
Chris Lattnerd1980a52009-03-12 06:52:53 +000013398 // If the flag operand isn't dead, don't touch this CMOV.
13399 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13400 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013401
Evan Chengb5a55d92011-05-24 01:48:22 +000013402 SDValue FalseOp = N->getOperand(0);
13403 SDValue TrueOp = N->getOperand(1);
13404 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13405 SDValue Cond = N->getOperand(3);
13406 if (CC == X86::COND_E || CC == X86::COND_NE) {
13407 switch (Cond.getOpcode()) {
13408 default: break;
13409 case X86ISD::BSR:
13410 case X86ISD::BSF:
13411 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13412 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13413 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13414 }
13415 }
13416
Chris Lattnerd1980a52009-03-12 06:52:53 +000013417 // If this is a select between two integer constants, try to do some
13418 // optimizations. Note that the operands are ordered the opposite of SELECT
13419 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013420 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13421 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013422 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13423 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013424 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13425 CC = X86::GetOppositeBranchCondition(CC);
13426 std::swap(TrueC, FalseC);
13427 }
Eric Christopherfd179292009-08-27 18:07:15 +000013428
Chris Lattnerd1980a52009-03-12 06:52:53 +000013429 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013430 // This is efficient for any integer data type (including i8/i16) and
13431 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013432 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013433 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13434 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013435
Chris Lattnerd1980a52009-03-12 06:52:53 +000013436 // Zero extend the condition if needed.
13437 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013438
Chris Lattnerd1980a52009-03-12 06:52:53 +000013439 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13440 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013441 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013442 if (N->getNumValues() == 2) // Dead flag value?
13443 return DCI.CombineTo(N, Cond, SDValue());
13444 return Cond;
13445 }
Eric Christopherfd179292009-08-27 18:07:15 +000013446
Chris Lattnercee56e72009-03-13 05:53:31 +000013447 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13448 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013449 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013450 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13451 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013452
Chris Lattner97a29a52009-03-13 05:22:11 +000013453 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013454 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13455 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013456 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13457 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013458
Chris Lattner97a29a52009-03-13 05:22:11 +000013459 if (N->getNumValues() == 2) // Dead flag value?
13460 return DCI.CombineTo(N, Cond, SDValue());
13461 return Cond;
13462 }
Eric Christopherfd179292009-08-27 18:07:15 +000013463
Chris Lattnercee56e72009-03-13 05:53:31 +000013464 // Optimize cases that will turn into an LEA instruction. This requires
13465 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013466 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013467 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013468 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013469
Chris Lattnercee56e72009-03-13 05:53:31 +000013470 bool isFastMultiplier = false;
13471 if (Diff < 10) {
13472 switch ((unsigned char)Diff) {
13473 default: break;
13474 case 1: // result = add base, cond
13475 case 2: // result = lea base( , cond*2)
13476 case 3: // result = lea base(cond, cond*2)
13477 case 4: // result = lea base( , cond*4)
13478 case 5: // result = lea base(cond, cond*4)
13479 case 8: // result = lea base( , cond*8)
13480 case 9: // result = lea base(cond, cond*8)
13481 isFastMultiplier = true;
13482 break;
13483 }
13484 }
Eric Christopherfd179292009-08-27 18:07:15 +000013485
Chris Lattnercee56e72009-03-13 05:53:31 +000013486 if (isFastMultiplier) {
13487 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013488 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13489 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013490 // Zero extend the condition if needed.
13491 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13492 Cond);
13493 // Scale the condition by the difference.
13494 if (Diff != 1)
13495 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13496 DAG.getConstant(Diff, Cond.getValueType()));
13497
13498 // Add the base if non-zero.
13499 if (FalseC->getAPIntValue() != 0)
13500 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13501 SDValue(FalseC, 0));
13502 if (N->getNumValues() == 2) // Dead flag value?
13503 return DCI.CombineTo(N, Cond, SDValue());
13504 return Cond;
13505 }
Eric Christopherfd179292009-08-27 18:07:15 +000013506 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013507 }
13508 }
13509 return SDValue();
13510}
13511
13512
Evan Cheng0b0cd912009-03-28 05:57:29 +000013513/// PerformMulCombine - Optimize a single multiply with constant into two
13514/// in order to implement it with two cheaper instructions, e.g.
13515/// LEA + SHL, LEA + LEA.
13516static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13517 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013518 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13519 return SDValue();
13520
Owen Andersone50ed302009-08-10 22:56:29 +000013521 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013522 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013523 return SDValue();
13524
13525 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13526 if (!C)
13527 return SDValue();
13528 uint64_t MulAmt = C->getZExtValue();
13529 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13530 return SDValue();
13531
13532 uint64_t MulAmt1 = 0;
13533 uint64_t MulAmt2 = 0;
13534 if ((MulAmt % 9) == 0) {
13535 MulAmt1 = 9;
13536 MulAmt2 = MulAmt / 9;
13537 } else if ((MulAmt % 5) == 0) {
13538 MulAmt1 = 5;
13539 MulAmt2 = MulAmt / 5;
13540 } else if ((MulAmt % 3) == 0) {
13541 MulAmt1 = 3;
13542 MulAmt2 = MulAmt / 3;
13543 }
13544 if (MulAmt2 &&
13545 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13546 DebugLoc DL = N->getDebugLoc();
13547
13548 if (isPowerOf2_64(MulAmt2) &&
13549 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13550 // If second multiplifer is pow2, issue it first. We want the multiply by
13551 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13552 // is an add.
13553 std::swap(MulAmt1, MulAmt2);
13554
13555 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013556 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013557 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013558 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013559 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013560 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013561 DAG.getConstant(MulAmt1, VT));
13562
Eric Christopherfd179292009-08-27 18:07:15 +000013563 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013564 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013565 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013566 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013567 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013568 DAG.getConstant(MulAmt2, VT));
13569
13570 // Do not add new nodes to DAG combiner worklist.
13571 DCI.CombineTo(N, NewMul, false);
13572 }
13573 return SDValue();
13574}
13575
Evan Chengad9c0a32009-12-15 00:53:42 +000013576static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13577 SDValue N0 = N->getOperand(0);
13578 SDValue N1 = N->getOperand(1);
13579 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13580 EVT VT = N0.getValueType();
13581
13582 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13583 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013584 if (VT.isInteger() && !VT.isVector() &&
13585 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013586 N0.getOperand(1).getOpcode() == ISD::Constant) {
13587 SDValue N00 = N0.getOperand(0);
13588 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13589 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13590 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13591 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13592 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13593 APInt ShAmt = N1C->getAPIntValue();
13594 Mask = Mask.shl(ShAmt);
13595 if (Mask != 0)
13596 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13597 N00, DAG.getConstant(Mask, VT));
13598 }
13599 }
13600
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013601
13602 // Hardware support for vector shifts is sparse which makes us scalarize the
13603 // vector operations in many cases. Also, on sandybridge ADD is faster than
13604 // shl.
13605 // (shl V, 1) -> add V,V
13606 if (isSplatVector(N1.getNode())) {
13607 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13608 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13609 // We shift all of the values by one. In many cases we do not have
13610 // hardware support for this operation. This is better expressed as an ADD
13611 // of two values.
13612 if (N1C && (1 == N1C->getZExtValue())) {
13613 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13614 }
13615 }
13616
Evan Chengad9c0a32009-12-15 00:53:42 +000013617 return SDValue();
13618}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013619
Nate Begeman740ab032009-01-26 00:52:55 +000013620/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13621/// when possible.
13622static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13623 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013624 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013625 if (N->getOpcode() == ISD::SHL) {
13626 SDValue V = PerformSHLCombine(N, DAG);
13627 if (V.getNode()) return V;
13628 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013629
Nate Begeman740ab032009-01-26 00:52:55 +000013630 // On X86 with SSE2 support, we can transform this to a vector shift if
13631 // all elements are shifted by the same amount. We can't do this in legalize
13632 // because the a constant vector is typically transformed to a constant pool
13633 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013634 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013635 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013636
Craig Topper7be5dfd2011-11-12 09:58:49 +000013637 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
13638 (!Subtarget->hasAVX2() ||
13639 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013640 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013641
Mon P Wang3becd092009-01-28 08:12:05 +000013642 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013643 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013644 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013645 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013646 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13647 unsigned NumElts = VT.getVectorNumElements();
13648 unsigned i = 0;
13649 for (; i != NumElts; ++i) {
13650 SDValue Arg = ShAmtOp.getOperand(i);
13651 if (Arg.getOpcode() == ISD::UNDEF) continue;
13652 BaseShAmt = Arg;
13653 break;
13654 }
13655 for (; i != NumElts; ++i) {
13656 SDValue Arg = ShAmtOp.getOperand(i);
13657 if (Arg.getOpcode() == ISD::UNDEF) continue;
13658 if (Arg != BaseShAmt) {
13659 return SDValue();
13660 }
13661 }
13662 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013663 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013664 SDValue InVec = ShAmtOp.getOperand(0);
13665 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13666 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13667 unsigned i = 0;
13668 for (; i != NumElts; ++i) {
13669 SDValue Arg = InVec.getOperand(i);
13670 if (Arg.getOpcode() == ISD::UNDEF) continue;
13671 BaseShAmt = Arg;
13672 break;
13673 }
13674 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13675 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013676 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013677 if (C->getZExtValue() == SplatIdx)
13678 BaseShAmt = InVec.getOperand(1);
13679 }
13680 }
13681 if (BaseShAmt.getNode() == 0)
13682 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13683 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013684 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013685 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013686
Mon P Wangefa42202009-09-03 19:56:25 +000013687 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013688 if (EltVT.bitsGT(MVT::i32))
13689 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13690 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013691 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013692
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013693 // The shift amount is identical so we can do a vector shift.
13694 SDValue ValOp = N->getOperand(0);
13695 switch (N->getOpcode()) {
13696 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013697 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013698 break;
13699 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013700 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013701 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013702 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013703 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013704 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013705 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013706 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013707 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013708 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013709 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013710 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013711 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013712 if (VT == MVT::v4i64)
13713 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13714 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
13715 ValOp, BaseShAmt);
13716 if (VT == MVT::v8i32)
13717 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13718 DAG.getConstant(Intrinsic::x86_avx2_pslli_d, MVT::i32),
13719 ValOp, BaseShAmt);
13720 if (VT == MVT::v16i16)
13721 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13722 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
13723 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013724 break;
13725 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013726 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013727 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013728 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013729 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013730 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013731 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013732 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013733 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013734 if (VT == MVT::v8i32)
13735 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13736 DAG.getConstant(Intrinsic::x86_avx2_psrai_d, MVT::i32),
13737 ValOp, BaseShAmt);
13738 if (VT == MVT::v16i16)
13739 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13740 DAG.getConstant(Intrinsic::x86_avx2_psrai_w, MVT::i32),
13741 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013742 break;
13743 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013744 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013745 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013746 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013747 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013748 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013749 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013750 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013751 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013752 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013753 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013754 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013755 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013756 if (VT == MVT::v4i64)
13757 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13758 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
13759 ValOp, BaseShAmt);
13760 if (VT == MVT::v8i32)
13761 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13762 DAG.getConstant(Intrinsic::x86_avx2_psrli_d, MVT::i32),
13763 ValOp, BaseShAmt);
13764 if (VT == MVT::v16i16)
13765 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13766 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
13767 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013768 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013769 }
13770 return SDValue();
13771}
13772
Nate Begemanb65c1752010-12-17 22:55:37 +000013773
Stuart Hastings865f0932011-06-03 23:53:54 +000013774// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13775// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13776// and friends. Likewise for OR -> CMPNEQSS.
13777static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13778 TargetLowering::DAGCombinerInfo &DCI,
13779 const X86Subtarget *Subtarget) {
13780 unsigned opcode;
13781
13782 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13783 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013784 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013785 SDValue N0 = N->getOperand(0);
13786 SDValue N1 = N->getOperand(1);
13787 SDValue CMP0 = N0->getOperand(1);
13788 SDValue CMP1 = N1->getOperand(1);
13789 DebugLoc DL = N->getDebugLoc();
13790
13791 // The SETCCs should both refer to the same CMP.
13792 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13793 return SDValue();
13794
13795 SDValue CMP00 = CMP0->getOperand(0);
13796 SDValue CMP01 = CMP0->getOperand(1);
13797 EVT VT = CMP00.getValueType();
13798
13799 if (VT == MVT::f32 || VT == MVT::f64) {
13800 bool ExpectingFlags = false;
13801 // Check for any users that want flags:
13802 for (SDNode::use_iterator UI = N->use_begin(),
13803 UE = N->use_end();
13804 !ExpectingFlags && UI != UE; ++UI)
13805 switch (UI->getOpcode()) {
13806 default:
13807 case ISD::BR_CC:
13808 case ISD::BRCOND:
13809 case ISD::SELECT:
13810 ExpectingFlags = true;
13811 break;
13812 case ISD::CopyToReg:
13813 case ISD::SIGN_EXTEND:
13814 case ISD::ZERO_EXTEND:
13815 case ISD::ANY_EXTEND:
13816 break;
13817 }
13818
13819 if (!ExpectingFlags) {
13820 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13821 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13822
13823 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13824 X86::CondCode tmp = cc0;
13825 cc0 = cc1;
13826 cc1 = tmp;
13827 }
13828
13829 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13830 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13831 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13832 X86ISD::NodeType NTOperator = is64BitFP ?
13833 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13834 // FIXME: need symbolic constants for these magic numbers.
13835 // See X86ATTInstPrinter.cpp:printSSECC().
13836 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13837 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13838 DAG.getConstant(x86cc, MVT::i8));
13839 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13840 OnesOrZeroesF);
13841 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13842 DAG.getConstant(1, MVT::i32));
13843 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13844 return OneBitOfTruth;
13845 }
13846 }
13847 }
13848 }
13849 return SDValue();
13850}
13851
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013852/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13853/// so it can be folded inside ANDNP.
13854static bool CanFoldXORWithAllOnes(const SDNode *N) {
13855 EVT VT = N->getValueType(0);
13856
13857 // Match direct AllOnes for 128 and 256-bit vectors
13858 if (ISD::isBuildVectorAllOnes(N))
13859 return true;
13860
13861 // Look through a bit convert.
13862 if (N->getOpcode() == ISD::BITCAST)
13863 N = N->getOperand(0).getNode();
13864
13865 // Sometimes the operand may come from a insert_subvector building a 256-bit
13866 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013867 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013868 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13869 SDValue V1 = N->getOperand(0);
13870 SDValue V2 = N->getOperand(1);
13871
13872 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13873 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13874 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13875 ISD::isBuildVectorAllOnes(V2.getNode()))
13876 return true;
13877 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013878
13879 return false;
13880}
13881
Nate Begemanb65c1752010-12-17 22:55:37 +000013882static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13883 TargetLowering::DAGCombinerInfo &DCI,
13884 const X86Subtarget *Subtarget) {
13885 if (DCI.isBeforeLegalizeOps())
13886 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013887
Stuart Hastings865f0932011-06-03 23:53:54 +000013888 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13889 if (R.getNode())
13890 return R;
13891
Craig Topper54a11172011-10-14 07:06:56 +000013892 EVT VT = N->getValueType(0);
13893
Craig Topperb4c94572011-10-21 06:55:01 +000013894 // Create ANDN, BLSI, and BLSR instructions
13895 // BLSI is X & (-X)
13896 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013897 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13898 SDValue N0 = N->getOperand(0);
13899 SDValue N1 = N->getOperand(1);
13900 DebugLoc DL = N->getDebugLoc();
13901
13902 // Check LHS for not
13903 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13904 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13905 // Check RHS for not
13906 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13907 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13908
Craig Topperb4c94572011-10-21 06:55:01 +000013909 // Check LHS for neg
13910 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13911 isZero(N0.getOperand(0)))
13912 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13913
13914 // Check RHS for neg
13915 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13916 isZero(N1.getOperand(0)))
13917 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13918
13919 // Check LHS for X-1
13920 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13921 isAllOnes(N0.getOperand(1)))
13922 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13923
13924 // Check RHS for X-1
13925 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13926 isAllOnes(N1.getOperand(1)))
13927 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13928
Craig Topper54a11172011-10-14 07:06:56 +000013929 return SDValue();
13930 }
13931
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013932 // Want to form ANDNP nodes:
13933 // 1) In the hopes of then easily combining them with OR and AND nodes
13934 // to form PBLEND/PSIGN.
13935 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013936 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013937 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013938
Nate Begemanb65c1752010-12-17 22:55:37 +000013939 SDValue N0 = N->getOperand(0);
13940 SDValue N1 = N->getOperand(1);
13941 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013942
Nate Begemanb65c1752010-12-17 22:55:37 +000013943 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013944 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013945 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13946 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013947 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013948
13949 // Check RHS for vnot
13950 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013951 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13952 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013953 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013954
Nate Begemanb65c1752010-12-17 22:55:37 +000013955 return SDValue();
13956}
13957
Evan Cheng760d1942010-01-04 21:22:48 +000013958static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013959 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013960 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013961 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013962 return SDValue();
13963
Stuart Hastings865f0932011-06-03 23:53:54 +000013964 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13965 if (R.getNode())
13966 return R;
13967
Evan Cheng760d1942010-01-04 21:22:48 +000013968 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000013969
Evan Cheng760d1942010-01-04 21:22:48 +000013970 SDValue N0 = N->getOperand(0);
13971 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013972
Nate Begemanb65c1752010-12-17 22:55:37 +000013973 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000013974 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
13975 if (!(Subtarget->hasSSSE3() || Subtarget->hasAVX()) ||
13976 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
13977 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013978
Craig Topper1666cb62011-11-19 07:07:26 +000013979 // Canonicalize pandn to RHS
13980 if (N0.getOpcode() == X86ISD::ANDNP)
13981 std::swap(N0, N1);
13982 // or (and (m, x), (pandn m, y))
13983 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
13984 SDValue Mask = N1.getOperand(0);
13985 SDValue X = N1.getOperand(1);
13986 SDValue Y;
13987 if (N0.getOperand(0) == Mask)
13988 Y = N0.getOperand(1);
13989 if (N0.getOperand(1) == Mask)
13990 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013991
Craig Topper1666cb62011-11-19 07:07:26 +000013992 // Check to see if the mask appeared in both the AND and ANDNP and
13993 if (!Y.getNode())
13994 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013995
Craig Topper1666cb62011-11-19 07:07:26 +000013996 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13997 if (Mask.getOpcode() != ISD::BITCAST ||
13998 X.getOpcode() != ISD::BITCAST ||
13999 Y.getOpcode() != ISD::BITCAST)
14000 return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000014001
Craig Topper1666cb62011-11-19 07:07:26 +000014002 // Look through mask bitcast.
14003 Mask = Mask.getOperand(0);
14004 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014005
Craig Topper1666cb62011-11-19 07:07:26 +000014006 // Validate that the Mask operand is a vector sra node. The sra node
14007 // will be an intrinsic.
14008 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
14009 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014010
Craig Topper1666cb62011-11-19 07:07:26 +000014011 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
14012 // there is no psrai.b
14013 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
14014 case Intrinsic::x86_sse2_psrai_w:
14015 case Intrinsic::x86_sse2_psrai_d:
14016 case Intrinsic::x86_avx2_psrai_w:
14017 case Intrinsic::x86_avx2_psrai_d:
14018 break;
14019 default: return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000014020 }
Craig Topper1666cb62011-11-19 07:07:26 +000014021
14022 // Check that the SRA is all signbits.
14023 SDValue SraC = Mask.getOperand(2);
14024 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
14025 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
14026 if ((SraAmt + 1) != EltBits)
14027 return SDValue();
14028
14029 DebugLoc DL = N->getDebugLoc();
14030
14031 // Now we know we at least have a plendvb with the mask val. See if
14032 // we can form a psignb/w/d.
14033 // psign = x.type == y.type == mask.type && y = sub(0, x);
14034 X = X.getOperand(0);
14035 Y = Y.getOperand(0);
14036 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
14037 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Topper31133842011-11-19 07:33:10 +000014038 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType() &&
14039 (EltBits == 8 || EltBits == 16 || EltBits == 32)) {
14040 SDValue Sign = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X,
14041 Mask.getOperand(1));
14042 return DAG.getNode(ISD::BITCAST, DL, VT, Sign);
Craig Topper1666cb62011-11-19 07:07:26 +000014043 }
14044 // PBLENDVB only available on SSE 4.1
14045 if (!(Subtarget->hasSSE41() || Subtarget->hasAVX()))
14046 return SDValue();
14047
14048 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
14049
14050 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
14051 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
14052 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
14053 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, X, Y);
14054 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000014055 }
14056 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014057
Craig Topper1666cb62011-11-19 07:07:26 +000014058 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
14059 return SDValue();
14060
Nate Begemanb65c1752010-12-17 22:55:37 +000014061 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000014062 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
14063 std::swap(N0, N1);
14064 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
14065 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000014066 if (!N0.hasOneUse() || !N1.hasOneUse())
14067 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000014068
14069 SDValue ShAmt0 = N0.getOperand(1);
14070 if (ShAmt0.getValueType() != MVT::i8)
14071 return SDValue();
14072 SDValue ShAmt1 = N1.getOperand(1);
14073 if (ShAmt1.getValueType() != MVT::i8)
14074 return SDValue();
14075 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
14076 ShAmt0 = ShAmt0.getOperand(0);
14077 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
14078 ShAmt1 = ShAmt1.getOperand(0);
14079
14080 DebugLoc DL = N->getDebugLoc();
14081 unsigned Opc = X86ISD::SHLD;
14082 SDValue Op0 = N0.getOperand(0);
14083 SDValue Op1 = N1.getOperand(0);
14084 if (ShAmt0.getOpcode() == ISD::SUB) {
14085 Opc = X86ISD::SHRD;
14086 std::swap(Op0, Op1);
14087 std::swap(ShAmt0, ShAmt1);
14088 }
14089
Evan Cheng8b1190a2010-04-28 01:18:01 +000014090 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000014091 if (ShAmt1.getOpcode() == ISD::SUB) {
14092 SDValue Sum = ShAmt1.getOperand(0);
14093 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000014094 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
14095 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
14096 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
14097 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000014098 return DAG.getNode(Opc, DL, VT,
14099 Op0, Op1,
14100 DAG.getNode(ISD::TRUNCATE, DL,
14101 MVT::i8, ShAmt0));
14102 }
14103 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
14104 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
14105 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000014106 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000014107 return DAG.getNode(Opc, DL, VT,
14108 N0.getOperand(0), N1.getOperand(0),
14109 DAG.getNode(ISD::TRUNCATE, DL,
14110 MVT::i8, ShAmt0));
14111 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014112
Evan Cheng760d1942010-01-04 21:22:48 +000014113 return SDValue();
14114}
14115
Craig Topperb4c94572011-10-21 06:55:01 +000014116static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
14117 TargetLowering::DAGCombinerInfo &DCI,
14118 const X86Subtarget *Subtarget) {
14119 if (DCI.isBeforeLegalizeOps())
14120 return SDValue();
14121
14122 EVT VT = N->getValueType(0);
14123
14124 if (VT != MVT::i32 && VT != MVT::i64)
14125 return SDValue();
14126
14127 // Create BLSMSK instructions by finding X ^ (X-1)
14128 SDValue N0 = N->getOperand(0);
14129 SDValue N1 = N->getOperand(1);
14130 DebugLoc DL = N->getDebugLoc();
14131
14132 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14133 isAllOnes(N0.getOperand(1)))
14134 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
14135
14136 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14137 isAllOnes(N1.getOperand(1)))
14138 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
14139
14140 return SDValue();
14141}
14142
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014143/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
14144static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
14145 const X86Subtarget *Subtarget) {
14146 LoadSDNode *Ld = cast<LoadSDNode>(N);
14147 EVT RegVT = Ld->getValueType(0);
14148 EVT MemVT = Ld->getMemoryVT();
14149 DebugLoc dl = Ld->getDebugLoc();
14150 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14151
14152 ISD::LoadExtType Ext = Ld->getExtensionType();
14153
Nadav Rotemca6f2962011-09-18 19:00:23 +000014154 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014155 // shuffle. We need SSE4 for the shuffles.
14156 // TODO: It is possible to support ZExt by zeroing the undef values
14157 // during the shuffle phase or after the shuffle.
14158 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
14159 assert(MemVT != RegVT && "Cannot extend to the same type");
14160 assert(MemVT.isVector() && "Must load a vector from memory");
14161
14162 unsigned NumElems = RegVT.getVectorNumElements();
14163 unsigned RegSz = RegVT.getSizeInBits();
14164 unsigned MemSz = MemVT.getSizeInBits();
14165 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000014166 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014167 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
14168
14169 // Attempt to load the original value using a single load op.
14170 // Find a scalar type which is equal to the loaded word size.
14171 MVT SclrLoadTy = MVT::i8;
14172 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14173 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14174 MVT Tp = (MVT::SimpleValueType)tp;
14175 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
14176 SclrLoadTy = Tp;
14177 break;
14178 }
14179 }
14180
14181 // Proceed if a load word is found.
14182 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
14183
14184 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
14185 RegSz/SclrLoadTy.getSizeInBits());
14186
14187 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
14188 RegSz/MemVT.getScalarType().getSizeInBits());
14189 // Can't shuffle using an illegal type.
14190 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14191
14192 // Perform a single load.
14193 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
14194 Ld->getBasePtr(),
14195 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014196 Ld->isNonTemporal(), Ld->isInvariant(),
14197 Ld->getAlignment());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014198
14199 // Insert the word loaded into a vector.
14200 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
14201 LoadUnitVecVT, ScalarLoad);
14202
14203 // Bitcast the loaded value to a vector of the original element type, in
14204 // the size of the target vector type.
14205 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
14206 unsigned SizeRatio = RegSz/MemSz;
14207
14208 // Redistribute the loaded elements into the different locations.
14209 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14210 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
14211
14212 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
14213 DAG.getUNDEF(SlicedVec.getValueType()),
14214 ShuffleVec.data());
14215
14216 // Bitcast to the requested type.
14217 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
14218 // Replace the original load with the new sequence
14219 // and return the new chain.
14220 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
14221 return SDValue(ScalarLoad.getNode(), 1);
14222 }
14223
14224 return SDValue();
14225}
14226
Chris Lattner149a4e52008-02-22 02:09:43 +000014227/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014228static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000014229 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000014230 StoreSDNode *St = cast<StoreSDNode>(N);
14231 EVT VT = St->getValue().getValueType();
14232 EVT StVT = St->getMemoryVT();
14233 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000014234 SDValue StoredVal = St->getOperand(1);
14235 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14236
14237 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000014238 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
14239 // 128-bit ones. If in the future the cost becomes only one memory access the
14240 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000014241 if (VT.getSizeInBits() == 256 &&
14242 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
14243 StoredVal.getNumOperands() == 2) {
14244
14245 SDValue Value0 = StoredVal.getOperand(0);
14246 SDValue Value1 = StoredVal.getOperand(1);
14247
14248 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
14249 SDValue Ptr0 = St->getBasePtr();
14250 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
14251
14252 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
14253 St->getPointerInfo(), St->isVolatile(),
14254 St->isNonTemporal(), St->getAlignment());
14255 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
14256 St->getPointerInfo(), St->isVolatile(),
14257 St->isNonTemporal(), St->getAlignment());
14258 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
14259 }
Nadav Rotem614061b2011-08-10 19:30:14 +000014260
14261 // Optimize trunc store (of multiple scalars) to shuffle and store.
14262 // First, pack all of the elements in one place. Next, store to memory
14263 // in fewer chunks.
14264 if (St->isTruncatingStore() && VT.isVector()) {
14265 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14266 unsigned NumElems = VT.getVectorNumElements();
14267 assert(StVT != VT && "Cannot truncate to the same type");
14268 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
14269 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
14270
14271 // From, To sizes and ElemCount must be pow of two
14272 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014273 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000014274 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014275 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014276
Nadav Rotem614061b2011-08-10 19:30:14 +000014277 unsigned SizeRatio = FromSz / ToSz;
14278
14279 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
14280
14281 // Create a type on which we perform the shuffle
14282 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
14283 StVT.getScalarType(), NumElems*SizeRatio);
14284
14285 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
14286
14287 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
14288 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14289 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
14290
14291 // Can't shuffle using an illegal type
14292 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14293
14294 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
14295 DAG.getUNDEF(WideVec.getValueType()),
14296 ShuffleVec.data());
14297 // At this point all of the data is stored at the bottom of the
14298 // register. We now need to save it to mem.
14299
14300 // Find the largest store unit
14301 MVT StoreType = MVT::i8;
14302 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14303 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14304 MVT Tp = (MVT::SimpleValueType)tp;
14305 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
14306 StoreType = Tp;
14307 }
14308
14309 // Bitcast the original vector into a vector of store-size units
14310 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
14311 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
14312 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
14313 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
14314 SmallVector<SDValue, 8> Chains;
14315 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
14316 TLI.getPointerTy());
14317 SDValue Ptr = St->getBasePtr();
14318
14319 // Perform one or more big stores into memory.
14320 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
14321 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14322 StoreType, ShuffWide,
14323 DAG.getIntPtrConstant(i));
14324 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14325 St->getPointerInfo(), St->isVolatile(),
14326 St->isNonTemporal(), St->getAlignment());
14327 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14328 Chains.push_back(Ch);
14329 }
14330
14331 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14332 Chains.size());
14333 }
14334
14335
Chris Lattner149a4e52008-02-22 02:09:43 +000014336 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14337 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014338 // A preferable solution to the general problem is to figure out the right
14339 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014340
14341 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014342 if (VT.getSizeInBits() != 64)
14343 return SDValue();
14344
Devang Patel578efa92009-06-05 21:57:13 +000014345 const Function *F = DAG.getMachineFunction().getFunction();
14346 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000014347 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000014348 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000014349 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014350 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014351 isa<LoadSDNode>(St->getValue()) &&
14352 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14353 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014354 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014355 LoadSDNode *Ld = 0;
14356 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014357 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014358 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014359 // Must be a store of a load. We currently handle two cases: the load
14360 // is a direct child, and it's under an intervening TokenFactor. It is
14361 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014362 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014363 Ld = cast<LoadSDNode>(St->getChain());
14364 else if (St->getValue().hasOneUse() &&
14365 ChainVal->getOpcode() == ISD::TokenFactor) {
14366 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014367 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014368 TokenFactorIndex = i;
14369 Ld = cast<LoadSDNode>(St->getValue());
14370 } else
14371 Ops.push_back(ChainVal->getOperand(i));
14372 }
14373 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014374
Evan Cheng536e6672009-03-12 05:59:15 +000014375 if (!Ld || !ISD::isNormalLoad(Ld))
14376 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014377
Evan Cheng536e6672009-03-12 05:59:15 +000014378 // If this is not the MMX case, i.e. we are just turning i64 load/store
14379 // into f64 load/store, avoid the transformation if there are multiple
14380 // uses of the loaded value.
14381 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14382 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014383
Evan Cheng536e6672009-03-12 05:59:15 +000014384 DebugLoc LdDL = Ld->getDebugLoc();
14385 DebugLoc StDL = N->getDebugLoc();
14386 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14387 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14388 // pair instead.
14389 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014390 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014391 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14392 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014393 Ld->isNonTemporal(), Ld->isInvariant(),
14394 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014395 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014396 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014397 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014398 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014399 Ops.size());
14400 }
Evan Cheng536e6672009-03-12 05:59:15 +000014401 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014402 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014403 St->isVolatile(), St->isNonTemporal(),
14404 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014405 }
Evan Cheng536e6672009-03-12 05:59:15 +000014406
14407 // Otherwise, lower to two pairs of 32-bit loads / stores.
14408 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014409 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14410 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014411
Owen Anderson825b72b2009-08-11 20:47:22 +000014412 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014413 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014414 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014415 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014416 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014417 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014418 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014419 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014420 MinAlign(Ld->getAlignment(), 4));
14421
14422 SDValue NewChain = LoLd.getValue(1);
14423 if (TokenFactorIndex != -1) {
14424 Ops.push_back(LoLd);
14425 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014426 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014427 Ops.size());
14428 }
14429
14430 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014431 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14432 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014433
14434 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014435 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014436 St->isVolatile(), St->isNonTemporal(),
14437 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014438 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014439 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014440 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014441 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014442 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014443 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014444 }
Dan Gohman475871a2008-07-27 21:46:04 +000014445 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014446}
14447
Duncan Sands17470be2011-09-22 20:15:48 +000014448/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14449/// and return the operands for the horizontal operation in LHS and RHS. A
14450/// horizontal operation performs the binary operation on successive elements
14451/// of its first operand, then on successive elements of its second operand,
14452/// returning the resulting values in a vector. For example, if
14453/// A = < float a0, float a1, float a2, float a3 >
14454/// and
14455/// B = < float b0, float b1, float b2, float b3 >
14456/// then the result of doing a horizontal operation on A and B is
14457/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14458/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14459/// A horizontal-op B, for some already available A and B, and if so then LHS is
14460/// set to A, RHS to B, and the routine returns 'true'.
14461/// Note that the binary operation should have the property that if one of the
14462/// operands is UNDEF then the result is UNDEF.
14463static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool isCommutative) {
14464 // Look for the following pattern: if
14465 // A = < float a0, float a1, float a2, float a3 >
14466 // B = < float b0, float b1, float b2, float b3 >
14467 // and
14468 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14469 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14470 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14471 // which is A horizontal-op B.
14472
14473 // At least one of the operands should be a vector shuffle.
14474 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14475 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14476 return false;
14477
14478 EVT VT = LHS.getValueType();
14479 unsigned N = VT.getVectorNumElements();
14480
14481 // View LHS in the form
14482 // LHS = VECTOR_SHUFFLE A, B, LMask
14483 // If LHS is not a shuffle then pretend it is the shuffle
14484 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14485 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14486 // type VT.
14487 SDValue A, B;
14488 SmallVector<int, 8> LMask(N);
14489 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14490 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14491 A = LHS.getOperand(0);
14492 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14493 B = LHS.getOperand(1);
14494 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
14495 } else {
14496 if (LHS.getOpcode() != ISD::UNDEF)
14497 A = LHS;
14498 for (unsigned i = 0; i != N; ++i)
14499 LMask[i] = i;
14500 }
14501
14502 // Likewise, view RHS in the form
14503 // RHS = VECTOR_SHUFFLE C, D, RMask
14504 SDValue C, D;
14505 SmallVector<int, 8> RMask(N);
14506 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14507 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14508 C = RHS.getOperand(0);
14509 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14510 D = RHS.getOperand(1);
14511 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
14512 } else {
14513 if (RHS.getOpcode() != ISD::UNDEF)
14514 C = RHS;
14515 for (unsigned i = 0; i != N; ++i)
14516 RMask[i] = i;
14517 }
14518
14519 // Check that the shuffles are both shuffling the same vectors.
14520 if (!(A == C && B == D) && !(A == D && B == C))
14521 return false;
14522
14523 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14524 if (!A.getNode() && !B.getNode())
14525 return false;
14526
14527 // If A and B occur in reverse order in RHS, then "swap" them (which means
14528 // rewriting the mask).
14529 if (A != C)
14530 for (unsigned i = 0; i != N; ++i) {
14531 unsigned Idx = RMask[i];
14532 if (Idx < N)
14533 RMask[i] += N;
14534 else if (Idx < 2*N)
14535 RMask[i] -= N;
14536 }
14537
14538 // At this point LHS and RHS are equivalent to
14539 // LHS = VECTOR_SHUFFLE A, B, LMask
14540 // RHS = VECTOR_SHUFFLE A, B, RMask
14541 // Check that the masks correspond to performing a horizontal operation.
14542 for (unsigned i = 0; i != N; ++i) {
14543 unsigned LIdx = LMask[i], RIdx = RMask[i];
14544
14545 // Ignore any UNDEF components.
14546 if (LIdx >= 2*N || RIdx >= 2*N || (!A.getNode() && (LIdx < N || RIdx < N))
14547 || (!B.getNode() && (LIdx >= N || RIdx >= N)))
14548 continue;
14549
14550 // Check that successive elements are being operated on. If not, this is
14551 // not a horizontal operation.
14552 if (!(LIdx == 2*i && RIdx == 2*i + 1) &&
14553 !(isCommutative && LIdx == 2*i + 1 && RIdx == 2*i))
14554 return false;
14555 }
14556
14557 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14558 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14559 return true;
14560}
14561
14562/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14563static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14564 const X86Subtarget *Subtarget) {
14565 EVT VT = N->getValueType(0);
14566 SDValue LHS = N->getOperand(0);
14567 SDValue RHS = N->getOperand(1);
14568
14569 // Try to synthesize horizontal adds from adds of shuffles.
14570 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
14571 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
14572 isHorizontalBinOp(LHS, RHS, true))
14573 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14574 return SDValue();
14575}
14576
14577/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14578static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14579 const X86Subtarget *Subtarget) {
14580 EVT VT = N->getValueType(0);
14581 SDValue LHS = N->getOperand(0);
14582 SDValue RHS = N->getOperand(1);
14583
14584 // Try to synthesize horizontal subs from subs of shuffles.
14585 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
14586 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
14587 isHorizontalBinOp(LHS, RHS, false))
14588 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14589 return SDValue();
14590}
14591
Chris Lattner6cf73262008-01-25 06:14:17 +000014592/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14593/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014594static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014595 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14596 // F[X]OR(0.0, x) -> x
14597 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014598 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14599 if (C->getValueAPF().isPosZero())
14600 return N->getOperand(1);
14601 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14602 if (C->getValueAPF().isPosZero())
14603 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014604 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014605}
14606
14607/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014608static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014609 // FAND(0.0, x) -> 0.0
14610 // FAND(x, 0.0) -> 0.0
14611 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14612 if (C->getValueAPF().isPosZero())
14613 return N->getOperand(0);
14614 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14615 if (C->getValueAPF().isPosZero())
14616 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014617 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014618}
14619
Dan Gohmane5af2d32009-01-29 01:59:02 +000014620static SDValue PerformBTCombine(SDNode *N,
14621 SelectionDAG &DAG,
14622 TargetLowering::DAGCombinerInfo &DCI) {
14623 // BT ignores high bits in the bit index operand.
14624 SDValue Op1 = N->getOperand(1);
14625 if (Op1.hasOneUse()) {
14626 unsigned BitWidth = Op1.getValueSizeInBits();
14627 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14628 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014629 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14630 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014631 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014632 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14633 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14634 DCI.CommitTargetLoweringOpt(TLO);
14635 }
14636 return SDValue();
14637}
Chris Lattner83e6c992006-10-04 06:57:07 +000014638
Eli Friedman7a5e5552009-06-07 06:52:44 +000014639static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14640 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014641 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014642 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014643 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014644 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014645 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014646 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014647 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014648 }
14649 return SDValue();
14650}
14651
Evan Cheng2e489c42009-12-16 00:53:11 +000014652static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14653 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14654 // (and (i32 x86isd::setcc_carry), 1)
14655 // This eliminates the zext. This transformation is necessary because
14656 // ISD::SETCC is always legalized to i8.
14657 DebugLoc dl = N->getDebugLoc();
14658 SDValue N0 = N->getOperand(0);
14659 EVT VT = N->getValueType(0);
14660 if (N0.getOpcode() == ISD::AND &&
14661 N0.hasOneUse() &&
14662 N0.getOperand(0).hasOneUse()) {
14663 SDValue N00 = N0.getOperand(0);
14664 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14665 return SDValue();
14666 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14667 if (!C || C->getZExtValue() != 1)
14668 return SDValue();
14669 return DAG.getNode(ISD::AND, dl, VT,
14670 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14671 N00.getOperand(0), N00.getOperand(1)),
14672 DAG.getConstant(1, VT));
14673 }
14674
14675 return SDValue();
14676}
14677
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014678// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14679static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14680 unsigned X86CC = N->getConstantOperandVal(0);
14681 SDValue EFLAG = N->getOperand(1);
14682 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014683
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014684 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14685 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14686 // cases.
14687 if (X86CC == X86::COND_B)
14688 return DAG.getNode(ISD::AND, DL, MVT::i8,
14689 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14690 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14691 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014692
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014693 return SDValue();
14694}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014695
Benjamin Kramer1396c402011-06-18 11:09:41 +000014696static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14697 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014698 SDValue Op0 = N->getOperand(0);
14699 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14700 // a 32-bit target where SSE doesn't support i64->FP operations.
14701 if (Op0.getOpcode() == ISD::LOAD) {
14702 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14703 EVT VT = Ld->getValueType(0);
14704 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14705 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14706 !XTLI->getSubtarget()->is64Bit() &&
14707 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014708 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14709 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014710 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14711 return FILDChain;
14712 }
14713 }
14714 return SDValue();
14715}
14716
Chris Lattner23a01992010-12-20 01:37:09 +000014717// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14718static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14719 X86TargetLowering::DAGCombinerInfo &DCI) {
14720 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14721 // the result is either zero or one (depending on the input carry bit).
14722 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14723 if (X86::isZeroNode(N->getOperand(0)) &&
14724 X86::isZeroNode(N->getOperand(1)) &&
14725 // We don't have a good way to replace an EFLAGS use, so only do this when
14726 // dead right now.
14727 SDValue(N, 1).use_empty()) {
14728 DebugLoc DL = N->getDebugLoc();
14729 EVT VT = N->getValueType(0);
14730 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14731 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14732 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14733 DAG.getConstant(X86::COND_B,MVT::i8),
14734 N->getOperand(2)),
14735 DAG.getConstant(1, VT));
14736 return DCI.CombineTo(N, Res1, CarryOut);
14737 }
14738
14739 return SDValue();
14740}
14741
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014742// fold (add Y, (sete X, 0)) -> adc 0, Y
14743// (add Y, (setne X, 0)) -> sbb -1, Y
14744// (sub (sete X, 0), Y) -> sbb 0, Y
14745// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014746static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014747 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014748
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014749 // Look through ZExts.
14750 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14751 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14752 return SDValue();
14753
14754 SDValue SetCC = Ext.getOperand(0);
14755 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14756 return SDValue();
14757
14758 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14759 if (CC != X86::COND_E && CC != X86::COND_NE)
14760 return SDValue();
14761
14762 SDValue Cmp = SetCC.getOperand(1);
14763 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014764 !X86::isZeroNode(Cmp.getOperand(1)) ||
14765 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014766 return SDValue();
14767
14768 SDValue CmpOp0 = Cmp.getOperand(0);
14769 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14770 DAG.getConstant(1, CmpOp0.getValueType()));
14771
14772 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14773 if (CC == X86::COND_NE)
14774 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14775 DL, OtherVal.getValueType(), OtherVal,
14776 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14777 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14778 DL, OtherVal.getValueType(), OtherVal,
14779 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14780}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014781
Craig Topper54f952a2011-11-19 09:02:40 +000014782/// PerformADDCombine - Do target-specific dag combines on integer adds.
14783static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
14784 const X86Subtarget *Subtarget) {
14785 EVT VT = N->getValueType(0);
14786 SDValue Op0 = N->getOperand(0);
14787 SDValue Op1 = N->getOperand(1);
14788
14789 // Try to synthesize horizontal adds from adds of shuffles.
14790 if ((Subtarget->hasSSSE3() || Subtarget->hasAVX()) &&
14791 (VT == MVT::v8i16 || VT == MVT::v4i32) &&
14792 isHorizontalBinOp(Op0, Op1, true))
14793 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
14794
14795 return OptimizeConditionalInDecrement(N, DAG);
14796}
14797
14798static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
14799 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014800 SDValue Op0 = N->getOperand(0);
14801 SDValue Op1 = N->getOperand(1);
14802
14803 // X86 can't encode an immediate LHS of a sub. See if we can push the
14804 // negation into a preceding instruction.
14805 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014806 // If the RHS of the sub is a XOR with one use and a constant, invert the
14807 // immediate. Then add one to the LHS of the sub so we can turn
14808 // X-Y -> X+~Y+1, saving one register.
14809 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14810 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014811 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014812 EVT VT = Op0.getValueType();
14813 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14814 Op1.getOperand(0),
14815 DAG.getConstant(~XorC, VT));
14816 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014817 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014818 }
14819 }
14820
Craig Topper54f952a2011-11-19 09:02:40 +000014821 // Try to synthesize horizontal adds from adds of shuffles.
14822 EVT VT = N->getValueType(0);
14823 if ((Subtarget->hasSSSE3() || Subtarget->hasAVX()) &&
14824 (VT == MVT::v8i16 || VT == MVT::v4i32) &&
14825 isHorizontalBinOp(Op0, Op1, false))
14826 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
14827
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014828 return OptimizeConditionalInDecrement(N, DAG);
14829}
14830
Dan Gohman475871a2008-07-27 21:46:04 +000014831SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014832 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014833 SelectionDAG &DAG = DCI.DAG;
14834 switch (N->getOpcode()) {
14835 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014836 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014837 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014838 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014839 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014840 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Craig Topper54f952a2011-11-19 09:02:40 +000014841 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
14842 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000014843 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014844 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014845 case ISD::SHL:
14846 case ISD::SRA:
14847 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014848 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014849 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014850 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014851 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014852 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014853 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014854 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14855 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014856 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014857 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14858 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014859 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014860 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014861 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014862 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014863 case X86ISD::SHUFPS: // Handle all target specific shuffles
14864 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014865 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014866 case X86ISD::PUNPCKHBW:
14867 case X86ISD::PUNPCKHWD:
14868 case X86ISD::PUNPCKHDQ:
14869 case X86ISD::PUNPCKHQDQ:
Craig Topper6347e862011-11-21 06:57:39 +000014870 case X86ISD::VPUNPCKHWDY:
14871 case X86ISD::VPUNPCKHDQY:
14872 case X86ISD::VPUNPCKHQDQY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014873 case X86ISD::UNPCKHPS:
14874 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000014875 case X86ISD::VUNPCKHPSY:
14876 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014877 case X86ISD::PUNPCKLBW:
14878 case X86ISD::PUNPCKLWD:
14879 case X86ISD::PUNPCKLDQ:
14880 case X86ISD::PUNPCKLQDQ:
Craig Topper6347e862011-11-21 06:57:39 +000014881 case X86ISD::VPUNPCKLWDY:
14882 case X86ISD::VPUNPCKLDQY:
14883 case X86ISD::VPUNPCKLQDQY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014884 case X86ISD::UNPCKLPS:
14885 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000014886 case X86ISD::VUNPCKLPSY:
14887 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014888 case X86ISD::MOVHLPS:
14889 case X86ISD::MOVLHPS:
14890 case X86ISD::PSHUFD:
14891 case X86ISD::PSHUFHW:
14892 case X86ISD::PSHUFLW:
14893 case X86ISD::MOVSS:
14894 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000014895 case X86ISD::VPERMILPS:
14896 case X86ISD::VPERMILPSY:
14897 case X86ISD::VPERMILPD:
14898 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000014899 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014900 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014901 }
14902
Dan Gohman475871a2008-07-27 21:46:04 +000014903 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014904}
14905
Evan Chenge5b51ac2010-04-17 06:13:15 +000014906/// isTypeDesirableForOp - Return true if the target has native support for
14907/// the specified value type and it is 'desirable' to use the type for the
14908/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14909/// instruction encodings are longer and some i16 instructions are slow.
14910bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14911 if (!isTypeLegal(VT))
14912 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014913 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014914 return true;
14915
14916 switch (Opc) {
14917 default:
14918 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014919 case ISD::LOAD:
14920 case ISD::SIGN_EXTEND:
14921 case ISD::ZERO_EXTEND:
14922 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014923 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014924 case ISD::SRL:
14925 case ISD::SUB:
14926 case ISD::ADD:
14927 case ISD::MUL:
14928 case ISD::AND:
14929 case ISD::OR:
14930 case ISD::XOR:
14931 return false;
14932 }
14933}
14934
14935/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014936/// beneficial for dag combiner to promote the specified node. If true, it
14937/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014938bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014939 EVT VT = Op.getValueType();
14940 if (VT != MVT::i16)
14941 return false;
14942
Evan Cheng4c26e932010-04-19 19:29:22 +000014943 bool Promote = false;
14944 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014945 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014946 default: break;
14947 case ISD::LOAD: {
14948 LoadSDNode *LD = cast<LoadSDNode>(Op);
14949 // If the non-extending load has a single use and it's not live out, then it
14950 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014951 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14952 Op.hasOneUse()*/) {
14953 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14954 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14955 // The only case where we'd want to promote LOAD (rather then it being
14956 // promoted as an operand is when it's only use is liveout.
14957 if (UI->getOpcode() != ISD::CopyToReg)
14958 return false;
14959 }
14960 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014961 Promote = true;
14962 break;
14963 }
14964 case ISD::SIGN_EXTEND:
14965 case ISD::ZERO_EXTEND:
14966 case ISD::ANY_EXTEND:
14967 Promote = true;
14968 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014969 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014970 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014971 SDValue N0 = Op.getOperand(0);
14972 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014973 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014974 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014975 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014976 break;
14977 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014978 case ISD::ADD:
14979 case ISD::MUL:
14980 case ISD::AND:
14981 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014982 case ISD::XOR:
14983 Commute = true;
14984 // fallthrough
14985 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014986 SDValue N0 = Op.getOperand(0);
14987 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014988 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014989 return false;
14990 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014991 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014992 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014993 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014994 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014995 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014996 }
14997 }
14998
14999 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000015000 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000015001}
15002
Evan Cheng60c07e12006-07-05 22:17:51 +000015003//===----------------------------------------------------------------------===//
15004// X86 Inline Assembly Support
15005//===----------------------------------------------------------------------===//
15006
Chris Lattnerb8105652009-07-20 17:51:36 +000015007bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
15008 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000015009
15010 std::string AsmStr = IA->getAsmString();
15011
15012 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000015013 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000015014 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000015015
15016 switch (AsmPieces.size()) {
15017 default: return false;
15018 case 1:
15019 AsmStr = AsmPieces[0];
15020 AsmPieces.clear();
15021 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
15022
Chris Lattner7a2bdde2011-04-15 05:18:47 +000015023 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000015024 // we will turn this bswap into something that will be lowered to logical ops
15025 // instead of emitting the bswap asm. For now, we don't support 486 or lower
15026 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000015027 // bswap $0
15028 if (AsmPieces.size() == 2 &&
15029 (AsmPieces[0] == "bswap" ||
15030 AsmPieces[0] == "bswapq" ||
15031 AsmPieces[0] == "bswapl") &&
15032 (AsmPieces[1] == "$0" ||
15033 AsmPieces[1] == "${0:q}")) {
15034 // No need to check constraints, nothing other than the equivalent of
15035 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015036 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000015037 if (!Ty || Ty->getBitWidth() % 16 != 0)
15038 return false;
15039 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000015040 }
15041 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000015042 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000015043 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000015044 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000015045 AsmPieces[1] == "$$8," &&
15046 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000015047 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
15048 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000015049 const std::string &ConstraintsStr = IA->getConstraintString();
15050 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000015051 std::sort(AsmPieces.begin(), AsmPieces.end());
15052 if (AsmPieces.size() == 4 &&
15053 AsmPieces[0] == "~{cc}" &&
15054 AsmPieces[1] == "~{dirflag}" &&
15055 AsmPieces[2] == "~{flags}" &&
15056 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015057 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000015058 if (!Ty || Ty->getBitWidth() % 16 != 0)
15059 return false;
15060 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000015061 }
Chris Lattnerb8105652009-07-20 17:51:36 +000015062 }
15063 break;
15064 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000015065 if (CI->getType()->isIntegerTy(32) &&
15066 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
15067 SmallVector<StringRef, 4> Words;
15068 SplitString(AsmPieces[0], Words, " \t,");
15069 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
15070 Words[2] == "${0:w}") {
15071 Words.clear();
15072 SplitString(AsmPieces[1], Words, " \t,");
15073 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
15074 Words[2] == "$0") {
15075 Words.clear();
15076 SplitString(AsmPieces[2], Words, " \t,");
15077 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
15078 Words[2] == "${0:w}") {
15079 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000015080 const std::string &ConstraintsStr = IA->getConstraintString();
15081 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000015082 std::sort(AsmPieces.begin(), AsmPieces.end());
15083 if (AsmPieces.size() == 4 &&
15084 AsmPieces[0] == "~{cc}" &&
15085 AsmPieces[1] == "~{dirflag}" &&
15086 AsmPieces[2] == "~{flags}" &&
15087 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015088 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000015089 if (!Ty || Ty->getBitWidth() % 16 != 0)
15090 return false;
15091 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000015092 }
15093 }
15094 }
15095 }
15096 }
Evan Cheng55d42002011-01-08 01:24:27 +000015097
15098 if (CI->getType()->isIntegerTy(64)) {
15099 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
15100 if (Constraints.size() >= 2 &&
15101 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
15102 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
15103 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
15104 SmallVector<StringRef, 4> Words;
15105 SplitString(AsmPieces[0], Words, " \t");
15106 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000015107 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000015108 SplitString(AsmPieces[1], Words, " \t");
15109 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
15110 Words.clear();
15111 SplitString(AsmPieces[2], Words, " \t,");
15112 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
15113 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015114 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000015115 if (!Ty || Ty->getBitWidth() % 16 != 0)
15116 return false;
15117 return IntrinsicLowering::LowerToByteSwap(CI);
15118 }
Chris Lattnerb8105652009-07-20 17:51:36 +000015119 }
15120 }
15121 }
15122 }
15123 break;
15124 }
15125 return false;
15126}
15127
15128
15129
Chris Lattnerf4dff842006-07-11 02:54:03 +000015130/// getConstraintType - Given a constraint letter, return the type of
15131/// constraint it is for this target.
15132X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000015133X86TargetLowering::getConstraintType(const std::string &Constraint) const {
15134 if (Constraint.size() == 1) {
15135 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000015136 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000015137 case 'q':
15138 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000015139 case 'f':
15140 case 't':
15141 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000015142 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000015143 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000015144 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000015145 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000015146 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000015147 case 'a':
15148 case 'b':
15149 case 'c':
15150 case 'd':
15151 case 'S':
15152 case 'D':
15153 case 'A':
15154 return C_Register;
15155 case 'I':
15156 case 'J':
15157 case 'K':
15158 case 'L':
15159 case 'M':
15160 case 'N':
15161 case 'G':
15162 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000015163 case 'e':
15164 case 'Z':
15165 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000015166 default:
15167 break;
15168 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000015169 }
Chris Lattner4234f572007-03-25 02:14:49 +000015170 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000015171}
15172
John Thompson44ab89e2010-10-29 17:29:13 +000015173/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000015174/// This object must already have been set up with the operand type
15175/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000015176TargetLowering::ConstraintWeight
15177 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000015178 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000015179 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015180 Value *CallOperandVal = info.CallOperandVal;
15181 // If we don't have a value, we can't do a match,
15182 // but allow it at the lowest weight.
15183 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000015184 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015185 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000015186 // Look at the constraint type.
15187 switch (*constraint) {
15188 default:
John Thompson44ab89e2010-10-29 17:29:13 +000015189 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
15190 case 'R':
15191 case 'q':
15192 case 'Q':
15193 case 'a':
15194 case 'b':
15195 case 'c':
15196 case 'd':
15197 case 'S':
15198 case 'D':
15199 case 'A':
15200 if (CallOperandVal->getType()->isIntegerTy())
15201 weight = CW_SpecificReg;
15202 break;
15203 case 'f':
15204 case 't':
15205 case 'u':
15206 if (type->isFloatingPointTy())
15207 weight = CW_SpecificReg;
15208 break;
15209 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000015210 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000015211 weight = CW_SpecificReg;
15212 break;
15213 case 'x':
15214 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015215 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000015216 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015217 break;
15218 case 'I':
15219 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
15220 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000015221 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015222 }
15223 break;
John Thompson44ab89e2010-10-29 17:29:13 +000015224 case 'J':
15225 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15226 if (C->getZExtValue() <= 63)
15227 weight = CW_Constant;
15228 }
15229 break;
15230 case 'K':
15231 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15232 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
15233 weight = CW_Constant;
15234 }
15235 break;
15236 case 'L':
15237 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15238 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
15239 weight = CW_Constant;
15240 }
15241 break;
15242 case 'M':
15243 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15244 if (C->getZExtValue() <= 3)
15245 weight = CW_Constant;
15246 }
15247 break;
15248 case 'N':
15249 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15250 if (C->getZExtValue() <= 0xff)
15251 weight = CW_Constant;
15252 }
15253 break;
15254 case 'G':
15255 case 'C':
15256 if (dyn_cast<ConstantFP>(CallOperandVal)) {
15257 weight = CW_Constant;
15258 }
15259 break;
15260 case 'e':
15261 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15262 if ((C->getSExtValue() >= -0x80000000LL) &&
15263 (C->getSExtValue() <= 0x7fffffffLL))
15264 weight = CW_Constant;
15265 }
15266 break;
15267 case 'Z':
15268 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15269 if (C->getZExtValue() <= 0xffffffff)
15270 weight = CW_Constant;
15271 }
15272 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015273 }
15274 return weight;
15275}
15276
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015277/// LowerXConstraint - try to replace an X constraint, which matches anything,
15278/// with another that has more specific requirements based on the type of the
15279/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000015280const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000015281LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000015282 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
15283 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000015284 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015285 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000015286 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015287 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000015288 return "x";
15289 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015290
Chris Lattner5e764232008-04-26 23:02:14 +000015291 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015292}
15293
Chris Lattner48884cd2007-08-25 00:47:38 +000015294/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
15295/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000015296void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000015297 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000015298 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000015299 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000015300 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000015301
Eric Christopher100c8332011-06-02 23:16:42 +000015302 // Only support length 1 constraints for now.
15303 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000015304
Eric Christopher100c8332011-06-02 23:16:42 +000015305 char ConstraintLetter = Constraint[0];
15306 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015307 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000015308 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000015309 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015310 if (C->getZExtValue() <= 31) {
15311 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015312 break;
15313 }
Devang Patel84f7fd22007-03-17 00:13:28 +000015314 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015315 return;
Evan Cheng364091e2008-09-22 23:57:37 +000015316 case 'J':
15317 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015318 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000015319 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15320 break;
15321 }
15322 }
15323 return;
15324 case 'K':
15325 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015326 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000015327 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15328 break;
15329 }
15330 }
15331 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000015332 case 'N':
15333 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015334 if (C->getZExtValue() <= 255) {
15335 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015336 break;
15337 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000015338 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015339 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000015340 case 'e': {
15341 // 32-bit signed value
15342 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015343 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15344 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015345 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015346 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000015347 break;
15348 }
15349 // FIXME gcc accepts some relocatable values here too, but only in certain
15350 // memory models; it's complicated.
15351 }
15352 return;
15353 }
15354 case 'Z': {
15355 // 32-bit unsigned value
15356 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015357 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15358 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015359 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15360 break;
15361 }
15362 }
15363 // FIXME gcc accepts some relocatable values here too, but only in certain
15364 // memory models; it's complicated.
15365 return;
15366 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015367 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015368 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000015369 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015370 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015371 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000015372 break;
15373 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015374
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015375 // In any sort of PIC mode addresses need to be computed at runtime by
15376 // adding in a register or some sort of table lookup. These can't
15377 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000015378 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015379 return;
15380
Chris Lattnerdc43a882007-05-03 16:52:29 +000015381 // If we are in non-pic codegen mode, we allow the address of a global (with
15382 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000015383 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015384 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000015385
Chris Lattner49921962009-05-08 18:23:14 +000015386 // Match either (GA), (GA+C), (GA+C1+C2), etc.
15387 while (1) {
15388 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15389 Offset += GA->getOffset();
15390 break;
15391 } else if (Op.getOpcode() == ISD::ADD) {
15392 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15393 Offset += C->getZExtValue();
15394 Op = Op.getOperand(0);
15395 continue;
15396 }
15397 } else if (Op.getOpcode() == ISD::SUB) {
15398 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15399 Offset += -C->getZExtValue();
15400 Op = Op.getOperand(0);
15401 continue;
15402 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015403 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015404
Chris Lattner49921962009-05-08 18:23:14 +000015405 // Otherwise, this isn't something we can handle, reject it.
15406 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015407 }
Eric Christopherfd179292009-08-27 18:07:15 +000015408
Dan Gohman46510a72010-04-15 01:51:59 +000015409 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015410 // If we require an extra load to get this address, as in PIC mode, we
15411 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015412 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15413 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015414 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015415
Devang Patel0d881da2010-07-06 22:08:15 +000015416 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15417 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015418 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015419 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015420 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015421
Gabor Greifba36cb52008-08-28 21:40:38 +000015422 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015423 Ops.push_back(Result);
15424 return;
15425 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015426 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015427}
15428
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015429std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015430X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015431 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015432 // First, see if this is a constraint that directly corresponds to an LLVM
15433 // register class.
15434 if (Constraint.size() == 1) {
15435 // GCC Constraint Letters
15436 switch (Constraint[0]) {
15437 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015438 // TODO: Slight differences here in allocation order and leaving
15439 // RIP in the class. Do they matter any more here than they do
15440 // in the normal allocation?
15441 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15442 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015443 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015444 return std::make_pair(0U, X86::GR32RegisterClass);
15445 else if (VT == MVT::i16)
15446 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015447 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015448 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015449 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015450 return std::make_pair(0U, X86::GR64RegisterClass);
15451 break;
15452 }
15453 // 32-bit fallthrough
15454 case 'Q': // Q_REGS
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::GR32_ABCDRegisterClass);
15457 else if (VT == MVT::i16)
15458 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
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::GR8_ABCD_LRegisterClass);
15461 else if (VT == MVT::i64)
15462 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15463 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015464 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015465 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015466 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015467 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015468 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015469 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015470 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015471 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015472 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015473 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015474 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015475 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15476 if (VT == MVT::i16)
15477 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15478 if (VT == MVT::i32 || !Subtarget->is64Bit())
15479 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15480 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015481 case 'f': // FP Stack registers.
15482 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15483 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015484 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015485 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015486 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015487 return std::make_pair(0U, X86::RFP64RegisterClass);
15488 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015489 case 'y': // MMX_REGS if MMX allowed.
15490 if (!Subtarget->hasMMX()) break;
15491 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015492 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015493 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015494 // FALL THROUGH.
15495 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015496 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015497
Owen Anderson825b72b2009-08-11 20:47:22 +000015498 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015499 default: break;
15500 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015501 case MVT::f32:
15502 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015503 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015504 case MVT::f64:
15505 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015506 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015507 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015508 case MVT::v16i8:
15509 case MVT::v8i16:
15510 case MVT::v4i32:
15511 case MVT::v2i64:
15512 case MVT::v4f32:
15513 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015514 return std::make_pair(0U, X86::VR128RegisterClass);
15515 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015516 break;
15517 }
15518 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015519
Chris Lattnerf76d1802006-07-31 23:26:50 +000015520 // Use the default implementation in TargetLowering to convert the register
15521 // constraint into a member of a register class.
15522 std::pair<unsigned, const TargetRegisterClass*> Res;
15523 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015524
15525 // Not found as a standard register?
15526 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015527 // Map st(0) -> st(7) -> ST0
15528 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15529 tolower(Constraint[1]) == 's' &&
15530 tolower(Constraint[2]) == 't' &&
15531 Constraint[3] == '(' &&
15532 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15533 Constraint[5] == ')' &&
15534 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015535
Chris Lattner56d77c72009-09-13 22:41:48 +000015536 Res.first = X86::ST0+Constraint[4]-'0';
15537 Res.second = X86::RFP80RegisterClass;
15538 return Res;
15539 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015540
Chris Lattner56d77c72009-09-13 22:41:48 +000015541 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015542 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015543 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015544 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015545 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015546 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015547
15548 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015549 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015550 Res.first = X86::EFLAGS;
15551 Res.second = X86::CCRRegisterClass;
15552 return Res;
15553 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015554
Dale Johannesen330169f2008-11-13 21:52:36 +000015555 // 'A' means EAX + EDX.
15556 if (Constraint == "A") {
15557 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015558 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015559 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015560 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015561 return Res;
15562 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015563
Chris Lattnerf76d1802006-07-31 23:26:50 +000015564 // Otherwise, check to see if this is a register class of the wrong value
15565 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15566 // turn into {ax},{dx}.
15567 if (Res.second->hasType(VT))
15568 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015569
Chris Lattnerf76d1802006-07-31 23:26:50 +000015570 // All of the single-register GCC register classes map their values onto
15571 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15572 // really want an 8-bit or 32-bit register, map to the appropriate register
15573 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015574 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015575 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015576 unsigned DestReg = 0;
15577 switch (Res.first) {
15578 default: break;
15579 case X86::AX: DestReg = X86::AL; break;
15580 case X86::DX: DestReg = X86::DL; break;
15581 case X86::CX: DestReg = X86::CL; break;
15582 case X86::BX: DestReg = X86::BL; break;
15583 }
15584 if (DestReg) {
15585 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015586 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015587 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015588 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015589 unsigned DestReg = 0;
15590 switch (Res.first) {
15591 default: break;
15592 case X86::AX: DestReg = X86::EAX; break;
15593 case X86::DX: DestReg = X86::EDX; break;
15594 case X86::CX: DestReg = X86::ECX; break;
15595 case X86::BX: DestReg = X86::EBX; break;
15596 case X86::SI: DestReg = X86::ESI; break;
15597 case X86::DI: DestReg = X86::EDI; break;
15598 case X86::BP: DestReg = X86::EBP; break;
15599 case X86::SP: DestReg = X86::ESP; break;
15600 }
15601 if (DestReg) {
15602 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015603 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015604 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015605 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015606 unsigned DestReg = 0;
15607 switch (Res.first) {
15608 default: break;
15609 case X86::AX: DestReg = X86::RAX; break;
15610 case X86::DX: DestReg = X86::RDX; break;
15611 case X86::CX: DestReg = X86::RCX; break;
15612 case X86::BX: DestReg = X86::RBX; break;
15613 case X86::SI: DestReg = X86::RSI; break;
15614 case X86::DI: DestReg = X86::RDI; break;
15615 case X86::BP: DestReg = X86::RBP; break;
15616 case X86::SP: DestReg = X86::RSP; break;
15617 }
15618 if (DestReg) {
15619 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015620 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015621 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015622 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015623 } else if (Res.second == X86::FR32RegisterClass ||
15624 Res.second == X86::FR64RegisterClass ||
15625 Res.second == X86::VR128RegisterClass) {
15626 // Handle references to XMM physical registers that got mapped into the
15627 // wrong class. This can happen with constraints like {xmm0} where the
15628 // target independent register mapper will just pick the first match it can
15629 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015630 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015631 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015632 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015633 Res.second = X86::FR64RegisterClass;
15634 else if (X86::VR128RegisterClass->hasType(VT))
15635 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015636 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015637
Chris Lattnerf76d1802006-07-31 23:26:50 +000015638 return Res;
15639}