blob: dc1aa42b95262931d60273cfc28460f2d4caa60a [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);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000259 } else if (!TM.Options.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
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000273 if (!TM.Options.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);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000316 } else if (!TM.Options.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);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000540 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000541 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
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000547 if (!TM.Options.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
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000579 } else if (!TM.Options.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
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000608 if (!TM.Options.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 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000612 } else if (!TM.Options.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
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000623 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
625 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
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.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000642 if (!TM.Options.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
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000661 if (!TM.Options.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
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000666 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
667 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
668 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
669 setOperationAction(ISD::FRINT, MVT::f80, Expand);
670 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000671 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000672 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000673
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000674 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
676 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
677 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000678
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 setOperationAction(ISD::FLOG, MVT::f80, Expand);
680 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
681 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
682 setOperationAction(ISD::FEXP, MVT::f80, Expand);
683 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000684
Mon P Wangf007a8b2008-11-06 05:31:54 +0000685 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000686 // (for widening) or expand (for scalarization). Then we will selectively
687 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000688 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
689 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
690 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000706 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
707 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000708 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000729 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000730 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000739 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000740 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000744 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000745 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
746 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
747 setTruncStoreAction((MVT::SimpleValueType)VT,
748 (MVT::SimpleValueType)InnerVT, Expand);
749 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
750 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
751 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000752 }
753
Evan Chengc7ce29b2009-02-13 22:36:38 +0000754 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
755 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000756 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000757 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000758 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000759 }
760
Dale Johannesen0488fb62010-09-30 23:57:10 +0000761 // MMX-sized vectors (other than x86mmx) are expected to be expanded
762 // into smaller operations.
763 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
764 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
765 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
766 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
767 setOperationAction(ISD::AND, MVT::v8i8, Expand);
768 setOperationAction(ISD::AND, MVT::v4i16, Expand);
769 setOperationAction(ISD::AND, MVT::v2i32, Expand);
770 setOperationAction(ISD::AND, MVT::v1i64, Expand);
771 setOperationAction(ISD::OR, MVT::v8i8, Expand);
772 setOperationAction(ISD::OR, MVT::v4i16, Expand);
773 setOperationAction(ISD::OR, MVT::v2i32, Expand);
774 setOperationAction(ISD::OR, MVT::v1i64, Expand);
775 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
776 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
777 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
778 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
779 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
780 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
781 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
782 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
783 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
784 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
785 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
786 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
787 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000788 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
789 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
790 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
791 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000792
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000793 if (!TM.Options.UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000795
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
797 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
798 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
799 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
800 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
801 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
802 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
803 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
804 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
805 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
806 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000807 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000808 }
809
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000810 if (!TM.Options.UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000812
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000813 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
814 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
816 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
817 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
818 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000819
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
821 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
822 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
823 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
824 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
825 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
826 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
827 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
828 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
829 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
830 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
831 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
832 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
833 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
834 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
835 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000836
Nadav Rotem354efd82011-09-18 14:57:03 +0000837 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000838 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
839 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
840 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000841
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
843 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
844 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
845 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
846 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000847
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000848 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
849 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
850 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
851 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
852 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
853
Evan Cheng2c3ae372006-04-12 21:21:57 +0000854 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000855 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
856 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000857 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000858 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000859 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000860 // Do not attempt to custom lower non-128-bit vectors
861 if (!VT.is128BitVector())
862 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 setOperationAction(ISD::BUILD_VECTOR,
864 VT.getSimpleVT().SimpleTy, Custom);
865 setOperationAction(ISD::VECTOR_SHUFFLE,
866 VT.getSimpleVT().SimpleTy, Custom);
867 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
868 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000869 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000870
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
872 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
873 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
874 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000877
Nate Begemancdd1eec2008-02-12 22:51:28 +0000878 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
880 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000881 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000882
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000883 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
885 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000886 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000887
888 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000889 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000890 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000891
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000894 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000896 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000898 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000900 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000902 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000903
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000905
Evan Cheng2c3ae372006-04-12 21:21:57 +0000906 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
908 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
909 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
910 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000911
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
913 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000914 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000915
Craig Topperc0d82852011-11-22 00:44:41 +0000916 if (Subtarget->hasSSE41orAVX()) {
Nate Begeman14d12ca2008-02-11 04:19:36 +0000917 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000918 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000919
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000920 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
921 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
922 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
923 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
924 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000925
Nate Begeman14d12ca2008-02-11 04:19:36 +0000926 // i8 and i16 vectors are custom , because the source register and source
927 // source memory operand types are not the same width. f32 vectors are
928 // custom since the immediate controlling the insert encodes additional
929 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
931 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
932 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
933 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000934
Owen Anderson825b72b2009-08-11 20:47:22 +0000935 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
936 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
937 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
938 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000939
Pete Coopera77214a2011-11-14 19:38:42 +0000940 // FIXME: these should be Legal but thats only for the case where
941 // the index is constant. For now custom expand to deal with that
Nate Begeman14d12ca2008-02-11 04:19:36 +0000942 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000943 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
944 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000945 }
946 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000947
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000948 if (Subtarget->hasXMMInt()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000949 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000950 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000951
Nadav Rotem43012222011-05-11 08:12:09 +0000952 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000953 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000954
Nadav Rotem43012222011-05-11 08:12:09 +0000955 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000956 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000957
958 if (Subtarget->hasAVX2()) {
959 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
960 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
961
962 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
963 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
964
965 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
966 } else {
967 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
968 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
969
970 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
971 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
972
973 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
974 }
Nadav Rotem43012222011-05-11 08:12:09 +0000975 }
976
Craig Topperc0d82852011-11-22 00:44:41 +0000977 if (Subtarget->hasSSE42orAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000978 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000979
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000980 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000981 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
982 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
983 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
984 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
985 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
986 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000987
Owen Anderson825b72b2009-08-11 20:47:22 +0000988 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000989 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
990 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000991
Owen Anderson825b72b2009-08-11 20:47:22 +0000992 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
993 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
994 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
995 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
996 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
997 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000998
Owen Anderson825b72b2009-08-11 20:47:22 +0000999 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1000 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1001 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1002 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1003 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1004 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001005
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001006 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1007 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001008 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001009
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00001010 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
1011 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
1012 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
1013 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
1014 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1015 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1016
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001017 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1018 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1019
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001020 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1021 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1022
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001023 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001024 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001025
Duncan Sands28b77e92011-09-06 19:07:46 +00001026 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1027 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1028 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1029 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001030
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001031 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1032 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1033 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1034
Craig Topperaaa643c2011-11-09 07:28:55 +00001035 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1036 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1037 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1038 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001039
Craig Topperaaa643c2011-11-09 07:28:55 +00001040 if (Subtarget->hasAVX2()) {
1041 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1042 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1043 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1044 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001045
Craig Topperaaa643c2011-11-09 07:28:55 +00001046 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1047 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1048 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1049 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001050
Craig Topperaaa643c2011-11-09 07:28:55 +00001051 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1052 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1053 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001054 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001055
1056 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001057
1058 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1059 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1060
1061 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1062 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1063
1064 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001065 } else {
1066 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1067 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1068 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1069 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1070
1071 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1072 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1073 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1074 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1075
1076 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1077 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1078 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1079 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001080
1081 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1082 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1083
1084 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1085 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1086
1087 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001088 }
Craig Topper13894fa2011-08-24 06:14:18 +00001089
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001090 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001091 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001092 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1093 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1094 EVT VT = SVT;
1095
1096 // Extract subvector is special because the value type
1097 // (result) is 128-bit but the source is 256-bit wide.
1098 if (VT.is128BitVector())
1099 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1100
1101 // Do not attempt to custom lower other non-256-bit vectors
1102 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001103 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001104
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001105 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1106 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1107 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1108 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001109 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001110 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001111 }
1112
David Greene54d8eba2011-01-27 22:38:56 +00001113 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001114 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1115 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1116 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001117
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001118 // Do not attempt to promote non-256-bit vectors
1119 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001120 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001121
1122 setOperationAction(ISD::AND, SVT, Promote);
1123 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1124 setOperationAction(ISD::OR, SVT, Promote);
1125 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1126 setOperationAction(ISD::XOR, SVT, Promote);
1127 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1128 setOperationAction(ISD::LOAD, SVT, Promote);
1129 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1130 setOperationAction(ISD::SELECT, SVT, Promote);
1131 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001132 }
David Greene9b9838d2009-06-29 16:47:10 +00001133 }
1134
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001135 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1136 // of this type with custom code.
1137 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1138 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1139 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1140 }
1141
Evan Cheng6be2c582006-04-05 23:38:46 +00001142 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001143 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001144
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001145
Eli Friedman962f5492010-06-02 19:35:46 +00001146 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1147 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001148 //
Eli Friedman962f5492010-06-02 19:35:46 +00001149 // FIXME: We really should do custom legalization for addition and
1150 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1151 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001152 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1153 // Add/Sub/Mul with overflow operations are custom lowered.
1154 MVT VT = IntVTs[i];
1155 setOperationAction(ISD::SADDO, VT, Custom);
1156 setOperationAction(ISD::UADDO, VT, Custom);
1157 setOperationAction(ISD::SSUBO, VT, Custom);
1158 setOperationAction(ISD::USUBO, VT, Custom);
1159 setOperationAction(ISD::SMULO, VT, Custom);
1160 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001161 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001162
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001163 // There are no 8-bit 3-address imul/mul instructions
1164 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1165 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001166
Evan Chengd54f2d52009-03-31 19:38:51 +00001167 if (!Subtarget->is64Bit()) {
1168 // These libcalls are not available in 32-bit.
1169 setLibcallName(RTLIB::SHL_I128, 0);
1170 setLibcallName(RTLIB::SRL_I128, 0);
1171 setLibcallName(RTLIB::SRA_I128, 0);
1172 }
1173
Evan Cheng206ee9d2006-07-07 08:33:52 +00001174 // We have target-specific dag combine patterns for the following nodes:
1175 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001176 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001177 setTargetDAGCombine(ISD::BUILD_VECTOR);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001178 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001179 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001180 setTargetDAGCombine(ISD::SHL);
1181 setTargetDAGCombine(ISD::SRA);
1182 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001183 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001184 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001185 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001186 setTargetDAGCombine(ISD::FADD);
1187 setTargetDAGCombine(ISD::FSUB);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001188 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001189 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001190 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001191 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001192 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001193 if (Subtarget->is64Bit())
1194 setTargetDAGCombine(ISD::MUL);
Craig Topperb4c94572011-10-21 06:55:01 +00001195 if (Subtarget->hasBMI())
1196 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001197
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001198 computeRegisterProperties();
1199
Evan Cheng05219282011-01-06 06:52:41 +00001200 // On Darwin, -Os means optimize for size without hurting performance,
1201 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001202 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001203 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001204 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001205 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1206 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1207 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001208 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001209 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001210
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001211 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001212}
1213
Scott Michel5b8f82e2008-03-10 15:42:14 +00001214
Duncan Sands28b77e92011-09-06 19:07:46 +00001215EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1216 if (!VT.isVector()) return MVT::i8;
1217 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001218}
1219
1220
Evan Cheng29286502008-01-23 23:17:41 +00001221/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1222/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001223static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001224 if (MaxAlign == 16)
1225 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001226 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001227 if (VTy->getBitWidth() == 128)
1228 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001229 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001230 unsigned EltAlign = 0;
1231 getMaxByValAlign(ATy->getElementType(), EltAlign);
1232 if (EltAlign > MaxAlign)
1233 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001234 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001235 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1236 unsigned EltAlign = 0;
1237 getMaxByValAlign(STy->getElementType(i), EltAlign);
1238 if (EltAlign > MaxAlign)
1239 MaxAlign = EltAlign;
1240 if (MaxAlign == 16)
1241 break;
1242 }
1243 }
1244 return;
1245}
1246
1247/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1248/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001249/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1250/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001251unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001252 if (Subtarget->is64Bit()) {
1253 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001254 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001255 if (TyAlign > 8)
1256 return TyAlign;
1257 return 8;
1258 }
1259
Evan Cheng29286502008-01-23 23:17:41 +00001260 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001261 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001262 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001263 return Align;
1264}
Chris Lattner2b02a442007-02-25 08:29:00 +00001265
Evan Chengf0df0312008-05-15 08:39:06 +00001266/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001267/// and store operations as a result of memset, memcpy, and memmove
1268/// lowering. If DstAlign is zero that means it's safe to destination
1269/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1270/// means there isn't a need to check it against alignment requirement,
1271/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001272/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001273/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1274/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1275/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001276/// It returns EVT::Other if the type should be determined using generic
1277/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001278EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001279X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1280 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001281 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001282 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001283 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001284 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1285 // linux. This is because the stack realignment code can't handle certain
1286 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001287 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001288 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001289 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001290 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001291 (Subtarget->isUnalignedMemAccessFast() ||
1292 ((DstAlign == 0 || DstAlign >= 16) &&
1293 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001294 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001295 if (Subtarget->hasAVX() &&
1296 Subtarget->getStackAlignment() >= 32)
1297 return MVT::v8f32;
1298 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001299 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001300 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001301 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001302 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001303 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001304 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001305 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001306 // Do not use f64 to lower memcpy if source is string constant. It's
1307 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001308 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001309 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001310 }
Evan Chengf0df0312008-05-15 08:39:06 +00001311 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001312 return MVT::i64;
1313 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001314}
1315
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001316/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1317/// current function. The returned value is a member of the
1318/// MachineJumpTableInfo::JTEntryKind enum.
1319unsigned X86TargetLowering::getJumpTableEncoding() const {
1320 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1321 // symbol.
1322 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1323 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001324 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001325
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001326 // Otherwise, use the normal jump table encoding heuristics.
1327 return TargetLowering::getJumpTableEncoding();
1328}
1329
Chris Lattnerc64daab2010-01-26 05:02:42 +00001330const MCExpr *
1331X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1332 const MachineBasicBlock *MBB,
1333 unsigned uid,MCContext &Ctx) const{
1334 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1335 Subtarget->isPICStyleGOT());
1336 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1337 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001338 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1339 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001340}
1341
Evan Chengcc415862007-11-09 01:32:10 +00001342/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1343/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001344SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001345 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001346 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001347 // This doesn't have DebugLoc associated with it, but is not really the
1348 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001349 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001350 return Table;
1351}
1352
Chris Lattner589c6f62010-01-26 06:28:43 +00001353/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1354/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1355/// MCExpr.
1356const MCExpr *X86TargetLowering::
1357getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1358 MCContext &Ctx) const {
1359 // X86-64 uses RIP relative addressing based on the jump table label.
1360 if (Subtarget->isPICStyleRIPRel())
1361 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1362
1363 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001364 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001365}
1366
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001367// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001368std::pair<const TargetRegisterClass*, uint8_t>
1369X86TargetLowering::findRepresentativeClass(EVT VT) const{
1370 const TargetRegisterClass *RRC = 0;
1371 uint8_t Cost = 1;
1372 switch (VT.getSimpleVT().SimpleTy) {
1373 default:
1374 return TargetLowering::findRepresentativeClass(VT);
1375 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1376 RRC = (Subtarget->is64Bit()
1377 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1378 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001379 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001380 RRC = X86::VR64RegisterClass;
1381 break;
1382 case MVT::f32: case MVT::f64:
1383 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1384 case MVT::v4f32: case MVT::v2f64:
1385 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1386 case MVT::v4f64:
1387 RRC = X86::VR128RegisterClass;
1388 break;
1389 }
1390 return std::make_pair(RRC, Cost);
1391}
1392
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001393bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1394 unsigned &Offset) const {
1395 if (!Subtarget->isTargetLinux())
1396 return false;
1397
1398 if (Subtarget->is64Bit()) {
1399 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1400 Offset = 0x28;
1401 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1402 AddressSpace = 256;
1403 else
1404 AddressSpace = 257;
1405 } else {
1406 // %gs:0x14 on i386
1407 Offset = 0x14;
1408 AddressSpace = 256;
1409 }
1410 return true;
1411}
1412
1413
Chris Lattner2b02a442007-02-25 08:29:00 +00001414//===----------------------------------------------------------------------===//
1415// Return Value Calling Convention Implementation
1416//===----------------------------------------------------------------------===//
1417
Chris Lattner59ed56b2007-02-28 04:55:35 +00001418#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001419
Michael J. Spencerec38de22010-10-10 22:04:20 +00001420bool
Eric Christopher471e4222011-06-08 23:55:35 +00001421X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1422 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001423 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001424 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001425 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001426 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001427 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001428 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001429}
1430
Dan Gohman98ca4f22009-08-05 01:29:28 +00001431SDValue
1432X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001433 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001434 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001435 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001436 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001437 MachineFunction &MF = DAG.getMachineFunction();
1438 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001439
Chris Lattner9774c912007-02-27 05:28:59 +00001440 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001441 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001442 RVLocs, *DAG.getContext());
1443 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001444
Evan Chengdcea1632010-02-04 02:40:39 +00001445 // Add the regs to the liveout set for the function.
1446 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1447 for (unsigned i = 0; i != RVLocs.size(); ++i)
1448 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1449 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001450
Dan Gohman475871a2008-07-27 21:46:04 +00001451 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001452
Dan Gohman475871a2008-07-27 21:46:04 +00001453 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001454 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1455 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001456 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1457 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001458
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001459 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001460 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1461 CCValAssign &VA = RVLocs[i];
1462 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001463 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001464 EVT ValVT = ValToCopy.getValueType();
1465
Dale Johannesenc4510512010-09-24 19:05:48 +00001466 // If this is x86-64, and we disabled SSE, we can't return FP values,
1467 // or SSE or MMX vectors.
1468 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1469 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001470 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001471 report_fatal_error("SSE register return with SSE disabled");
1472 }
1473 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1474 // llvm-gcc has never done it right and no one has noticed, so this
1475 // should be OK for now.
1476 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001477 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001478 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001479
Chris Lattner447ff682008-03-11 03:23:40 +00001480 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1481 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001482 if (VA.getLocReg() == X86::ST0 ||
1483 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001484 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1485 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001486 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001487 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001488 RetOps.push_back(ValToCopy);
1489 // Don't emit a copytoreg.
1490 continue;
1491 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001492
Evan Cheng242b38b2009-02-23 09:03:22 +00001493 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1494 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001495 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001496 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001497 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001498 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001499 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1500 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001501 // If we don't have SSE2 available, convert to v4f32 so the generated
1502 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001503 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001504 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001505 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001506 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001507 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001508
Dale Johannesendd64c412009-02-04 00:33:20 +00001509 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001510 Flag = Chain.getValue(1);
1511 }
Dan Gohman61a92132008-04-21 23:59:07 +00001512
1513 // The x86-64 ABI for returning structs by value requires that we copy
1514 // the sret argument into %rax for the return. We saved the argument into
1515 // a virtual register in the entry block, so now we copy the value out
1516 // and into %rax.
1517 if (Subtarget->is64Bit() &&
1518 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1519 MachineFunction &MF = DAG.getMachineFunction();
1520 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1521 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001522 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001523 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001524 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001525
Dale Johannesendd64c412009-02-04 00:33:20 +00001526 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001527 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001528
1529 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001530 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001531 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001532
Chris Lattner447ff682008-03-11 03:23:40 +00001533 RetOps[0] = Chain; // Update chain.
1534
1535 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001536 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001537 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001538
1539 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001540 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001541}
1542
Evan Cheng3d2125c2010-11-30 23:55:39 +00001543bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1544 if (N->getNumValues() != 1)
1545 return false;
1546 if (!N->hasNUsesOfValue(1, 0))
1547 return false;
1548
1549 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001550 if (Copy->getOpcode() != ISD::CopyToReg &&
1551 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001552 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001553
1554 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001555 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001556 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001557 if (UI->getOpcode() != X86ISD::RET_FLAG)
1558 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001559 HasRet = true;
1560 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001561
Evan Cheng1bf891a2010-12-01 22:59:46 +00001562 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001563}
1564
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001565EVT
1566X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001567 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001568 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001569 // TODO: Is this also valid on 32-bit?
1570 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001571 ReturnMVT = MVT::i8;
1572 else
1573 ReturnMVT = MVT::i32;
1574
1575 EVT MinVT = getRegisterType(Context, ReturnMVT);
1576 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001577}
1578
Dan Gohman98ca4f22009-08-05 01:29:28 +00001579/// LowerCallResult - Lower the result values of a call into the
1580/// appropriate copies out of appropriate physical registers.
1581///
1582SDValue
1583X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001584 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001585 const SmallVectorImpl<ISD::InputArg> &Ins,
1586 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001587 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001588
Chris Lattnere32bbf62007-02-28 07:09:55 +00001589 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001590 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001591 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001592 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1593 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001594 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001595
Chris Lattner3085e152007-02-25 08:59:22 +00001596 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001597 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001598 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001599 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001600
Torok Edwin3f142c32009-02-01 18:15:56 +00001601 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001602 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001603 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001604 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001605 }
1606
Evan Cheng79fb3b42009-02-20 20:43:02 +00001607 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001608
1609 // If this is a call to a function that returns an fp value on the floating
1610 // point stack, we must guarantee the the value is popped from the stack, so
1611 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001612 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001613 // instead.
1614 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1615 // If we prefer to use the value in xmm registers, copy it out as f80 and
1616 // use a truncate to move it from fp stack reg to xmm reg.
1617 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001618 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001619 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1620 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001621 Val = Chain.getValue(0);
1622
1623 // Round the f80 to the right size, which also moves it to the appropriate
1624 // xmm register.
1625 if (CopyVT != VA.getValVT())
1626 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1627 // This truncation won't change the value.
1628 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001629 } else {
1630 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1631 CopyVT, InFlag).getValue(1);
1632 Val = Chain.getValue(0);
1633 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001634 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001635 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001636 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001637
Dan Gohman98ca4f22009-08-05 01:29:28 +00001638 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001639}
1640
1641
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001642//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001643// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001644//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001645// StdCall calling convention seems to be standard for many Windows' API
1646// routines and around. It differs from C calling convention just a little:
1647// callee should clean up the stack, not caller. Symbols should be also
1648// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001649// For info on fast calling convention see Fast Calling Convention (tail call)
1650// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001651
Dan Gohman98ca4f22009-08-05 01:29:28 +00001652/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001653/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001654static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1655 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001656 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001657
Dan Gohman98ca4f22009-08-05 01:29:28 +00001658 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001659}
1660
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001661/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001662/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001663static bool
1664ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1665 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001666 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001667
Dan Gohman98ca4f22009-08-05 01:29:28 +00001668 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001669}
1670
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001671/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1672/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001673/// the specific parameter attribute. The copy will be passed as a byval
1674/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001675static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001676CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001677 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1678 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001679 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001680
Dale Johannesendd64c412009-02-04 00:33:20 +00001681 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001682 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001683 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001684}
1685
Chris Lattner29689432010-03-11 00:22:57 +00001686/// IsTailCallConvention - Return true if the calling convention is one that
1687/// supports tail call optimization.
1688static bool IsTailCallConvention(CallingConv::ID CC) {
1689 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1690}
1691
Evan Cheng485fafc2011-03-21 01:19:09 +00001692bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1693 if (!CI->isTailCall())
1694 return false;
1695
1696 CallSite CS(CI);
1697 CallingConv::ID CalleeCC = CS.getCallingConv();
1698 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1699 return false;
1700
1701 return true;
1702}
1703
Evan Cheng0c439eb2010-01-27 00:07:07 +00001704/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1705/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001706static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1707 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001708 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001709}
1710
Dan Gohman98ca4f22009-08-05 01:29:28 +00001711SDValue
1712X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001713 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001714 const SmallVectorImpl<ISD::InputArg> &Ins,
1715 DebugLoc dl, SelectionDAG &DAG,
1716 const CCValAssign &VA,
1717 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001718 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001719 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001720 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001721 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1722 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001723 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001724 EVT ValVT;
1725
1726 // If value is passed by pointer we have address passed instead of the value
1727 // itself.
1728 if (VA.getLocInfo() == CCValAssign::Indirect)
1729 ValVT = VA.getLocVT();
1730 else
1731 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001732
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001733 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001734 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001735 // In case of tail call optimization mark all arguments mutable. Since they
1736 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001737 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001738 unsigned Bytes = Flags.getByValSize();
1739 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1740 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001741 return DAG.getFrameIndex(FI, getPointerTy());
1742 } else {
1743 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001744 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001745 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1746 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001747 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001748 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001749 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001750}
1751
Dan Gohman475871a2008-07-27 21:46:04 +00001752SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001753X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001754 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001755 bool isVarArg,
1756 const SmallVectorImpl<ISD::InputArg> &Ins,
1757 DebugLoc dl,
1758 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001759 SmallVectorImpl<SDValue> &InVals)
1760 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001761 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001762 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001763
Gordon Henriksen86737662008-01-05 16:56:59 +00001764 const Function* Fn = MF.getFunction();
1765 if (Fn->hasExternalLinkage() &&
1766 Subtarget->isTargetCygMing() &&
1767 Fn->getName() == "main")
1768 FuncInfo->setForceFramePointer(true);
1769
Evan Cheng1bc78042006-04-26 01:20:17 +00001770 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001771 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001772 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001773
Chris Lattner29689432010-03-11 00:22:57 +00001774 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1775 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001776
Chris Lattner638402b2007-02-28 07:00:42 +00001777 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001778 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001779 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001780 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001781
1782 // Allocate shadow area for Win64
1783 if (IsWin64) {
1784 CCInfo.AllocateStack(32, 8);
1785 }
1786
Duncan Sands45907662010-10-31 13:21:44 +00001787 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001788
Chris Lattnerf39f7712007-02-28 05:46:49 +00001789 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001790 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001791 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1792 CCValAssign &VA = ArgLocs[i];
1793 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1794 // places.
1795 assert(VA.getValNo() != LastVal &&
1796 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001797 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001798 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001799
Chris Lattnerf39f7712007-02-28 05:46:49 +00001800 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001801 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001802 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001803 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001804 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001805 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001806 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001807 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001808 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001809 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001810 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001811 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1812 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001813 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001814 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001815 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001816 RC = X86::VR64RegisterClass;
1817 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001818 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001819
Devang Patel68e6bee2011-02-21 23:21:26 +00001820 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001821 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001822
Chris Lattnerf39f7712007-02-28 05:46:49 +00001823 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1824 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1825 // right size.
1826 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001827 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001828 DAG.getValueType(VA.getValVT()));
1829 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001830 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001831 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001832 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001833 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001834
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001835 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001836 // Handle MMX values passed in XMM regs.
1837 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001838 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1839 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001840 } else
1841 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001842 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001843 } else {
1844 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001845 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001846 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001847
1848 // If value is passed via pointer - do a load.
1849 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001850 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001851 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001852
Dan Gohman98ca4f22009-08-05 01:29:28 +00001853 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001854 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001855
Dan Gohman61a92132008-04-21 23:59:07 +00001856 // The x86-64 ABI for returning structs by value requires that we copy
1857 // the sret argument into %rax for the return. Save the argument into
1858 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001859 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001860 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1861 unsigned Reg = FuncInfo->getSRetReturnReg();
1862 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001863 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001864 FuncInfo->setSRetReturnReg(Reg);
1865 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001866 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001867 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001868 }
1869
Chris Lattnerf39f7712007-02-28 05:46:49 +00001870 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001871 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001872 if (FuncIsMadeTailCallSafe(CallConv,
1873 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001874 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001875
Evan Cheng1bc78042006-04-26 01:20:17 +00001876 // If the function takes variable number of arguments, make a frame index for
1877 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001878 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001879 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1880 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001881 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001882 }
1883 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001884 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1885
1886 // FIXME: We should really autogenerate these arrays
1887 static const unsigned GPR64ArgRegsWin64[] = {
1888 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001889 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001890 static const unsigned GPR64ArgRegs64Bit[] = {
1891 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1892 };
1893 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001894 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1895 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1896 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001897 const unsigned *GPR64ArgRegs;
1898 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001899
1900 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001901 // The XMM registers which might contain var arg parameters are shadowed
1902 // in their paired GPR. So we only need to save the GPR to their home
1903 // slots.
1904 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001905 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001906 } else {
1907 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1908 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001909
1910 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001911 }
1912 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1913 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001914
Devang Patel578efa92009-06-05 21:57:13 +00001915 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001916 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001917 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001918 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
1919 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001920 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001921 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
1922 !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001923 // Kernel mode asks for SSE to be disabled, so don't push them
1924 // on the stack.
1925 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001926
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001927 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001928 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001929 // Get to the caller-allocated home save location. Add 8 to account
1930 // for the return address.
1931 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001932 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001933 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001934 // Fixup to set vararg frame on shadow area (4 x i64).
1935 if (NumIntRegs < 4)
1936 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001937 } else {
1938 // For X86-64, if there are vararg parameters that are passed via
1939 // registers, then we must store them to their spots on the stack so they
1940 // may be loaded by deferencing the result of va_next.
1941 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1942 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1943 FuncInfo->setRegSaveFrameIndex(
1944 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001945 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001946 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001947
Gordon Henriksen86737662008-01-05 16:56:59 +00001948 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001949 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001950 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1951 getPointerTy());
1952 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001953 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001954 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1955 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001956 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001957 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001958 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001959 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001960 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001961 MachinePointerInfo::getFixedStack(
1962 FuncInfo->getRegSaveFrameIndex(), Offset),
1963 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001964 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001965 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001966 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001967
Dan Gohmanface41a2009-08-16 21:24:25 +00001968 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1969 // Now store the XMM (fp + vector) parameter registers.
1970 SmallVector<SDValue, 11> SaveXMMOps;
1971 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001972
Devang Patel68e6bee2011-02-21 23:21:26 +00001973 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001974 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1975 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001976
Dan Gohman1e93df62010-04-17 14:41:14 +00001977 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1978 FuncInfo->getRegSaveFrameIndex()));
1979 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1980 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001981
Dan Gohmanface41a2009-08-16 21:24:25 +00001982 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001983 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001984 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001985 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1986 SaveXMMOps.push_back(Val);
1987 }
1988 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1989 MVT::Other,
1990 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001991 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001992
1993 if (!MemOps.empty())
1994 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1995 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001996 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001997 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001998
Gordon Henriksen86737662008-01-05 16:56:59 +00001999 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002000 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2001 MF.getTarget().Options.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.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002101 if (!MF.getTarget().Options.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;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002129 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2130 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002131 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002132
Gordon Henriksen86737662008-01-05 16:56:59 +00002133 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002134 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002135 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002136 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002137 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2138 FPDiff = NumBytesCallerPushed - NumBytes;
2139
2140 // Set the delta of movement of the returnaddr stackslot.
2141 // But only set if delta is greater than previous delta.
2142 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2143 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2144 }
2145
Evan Chengf22f9b32010-02-06 03:28:46 +00002146 if (!IsSibcall)
2147 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002148
Dan Gohman475871a2008-07-27 21:46:04 +00002149 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002150 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002151 if (isTailCall && FPDiff)
2152 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2153 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002154
Dan Gohman475871a2008-07-27 21:46:04 +00002155 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2156 SmallVector<SDValue, 8> MemOpChains;
2157 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002158
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002159 // Walk the register/memloc assignments, inserting copies/loads. In the case
2160 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002161 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2162 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002163 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002164 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002165 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002166 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002167
Chris Lattner423c5f42007-02-28 05:31:48 +00002168 // Promote the value if needed.
2169 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002170 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002171 case CCValAssign::Full: break;
2172 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002173 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002174 break;
2175 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002176 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002177 break;
2178 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002179 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2180 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002181 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002182 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2183 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002184 } else
2185 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2186 break;
2187 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002188 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002189 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002190 case CCValAssign::Indirect: {
2191 // Store the argument.
2192 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002193 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002194 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002195 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002196 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002197 Arg = SpillSlot;
2198 break;
2199 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002200 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002201
Chris Lattner423c5f42007-02-28 05:31:48 +00002202 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002203 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2204 if (isVarArg && IsWin64) {
2205 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2206 // shadow reg if callee is a varargs function.
2207 unsigned ShadowReg = 0;
2208 switch (VA.getLocReg()) {
2209 case X86::XMM0: ShadowReg = X86::RCX; break;
2210 case X86::XMM1: ShadowReg = X86::RDX; break;
2211 case X86::XMM2: ShadowReg = X86::R8; break;
2212 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002213 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002214 if (ShadowReg)
2215 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002216 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002217 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002218 assert(VA.isMemLoc());
2219 if (StackPtr.getNode() == 0)
2220 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2221 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2222 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002223 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002224 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002225
Evan Cheng32fe1032006-05-25 00:59:30 +00002226 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002227 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002228 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002229
Evan Cheng347d5f72006-04-28 21:29:37 +00002230 // Build a sequence of copy-to-reg nodes chained together with token chain
2231 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002232 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002233 // Tail call byval lowering might overwrite argument registers so in case of
2234 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002235 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002236 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002237 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002238 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002239 InFlag = Chain.getValue(1);
2240 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002241
Chris Lattner88e1fd52009-07-09 04:24:46 +00002242 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002243 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2244 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002245 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002246 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2247 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002248 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002249 InFlag);
2250 InFlag = Chain.getValue(1);
2251 } else {
2252 // If we are tail calling and generating PIC/GOT style code load the
2253 // address of the callee into ECX. The value in ecx is used as target of
2254 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2255 // for tail calls on PIC/GOT architectures. Normally we would just put the
2256 // address of GOT into ebx and then call target@PLT. But for tail calls
2257 // ebx would be restored (since ebx is callee saved) before jumping to the
2258 // target@PLT.
2259
2260 // Note: The actual moving to ECX is done further down.
2261 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2262 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2263 !G->getGlobal()->hasProtectedVisibility())
2264 Callee = LowerGlobalAddress(Callee, DAG);
2265 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002266 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002267 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002268 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002269
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002270 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002271 // From AMD64 ABI document:
2272 // For calls that may call functions that use varargs or stdargs
2273 // (prototype-less calls or calls to functions containing ellipsis (...) in
2274 // the declaration) %al is used as hidden argument to specify the number
2275 // of SSE registers used. The contents of %al do not need to match exactly
2276 // the number of registers, but must be an ubound on the number of SSE
2277 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002278
Gordon Henriksen86737662008-01-05 16:56:59 +00002279 // Count the number of XMM registers allocated.
2280 static const unsigned XMMArgRegs[] = {
2281 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2282 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2283 };
2284 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002285 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002286 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002287
Dale Johannesendd64c412009-02-04 00:33:20 +00002288 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002289 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002290 InFlag = Chain.getValue(1);
2291 }
2292
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002293
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002294 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002295 if (isTailCall) {
2296 // Force all the incoming stack arguments to be loaded from the stack
2297 // before any new outgoing arguments are stored to the stack, because the
2298 // outgoing stack slots may alias the incoming argument stack slots, and
2299 // the alias isn't otherwise explicit. This is slightly more conservative
2300 // than necessary, because it means that each store effectively depends
2301 // on every argument instead of just those arguments it would clobber.
2302 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2303
Dan Gohman475871a2008-07-27 21:46:04 +00002304 SmallVector<SDValue, 8> MemOpChains2;
2305 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002306 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002307 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002308 InFlag = SDValue();
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002309 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002310 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2311 CCValAssign &VA = ArgLocs[i];
2312 if (VA.isRegLoc())
2313 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002314 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002315 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002316 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002317 // Create frame index.
2318 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002319 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002320 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002321 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002322
Duncan Sands276dcbd2008-03-21 09:14:45 +00002323 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002324 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002325 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002326 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002327 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002328 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002329 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002330
Dan Gohman98ca4f22009-08-05 01:29:28 +00002331 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2332 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002333 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002334 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002335 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002336 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002337 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002338 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002339 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002340 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002341 }
2342 }
2343
2344 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002345 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002346 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002347
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002348 // Copy arguments to their registers.
2349 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002350 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002351 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002352 InFlag = Chain.getValue(1);
2353 }
Dan Gohman475871a2008-07-27 21:46:04 +00002354 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002355
Gordon Henriksen86737662008-01-05 16:56:59 +00002356 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002357 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002358 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002359 }
2360
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002361 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2362 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2363 // In the 64-bit large code model, we have to make all calls
2364 // through a register, since the call instruction's 32-bit
2365 // pc-relative offset may not be large enough to hold the whole
2366 // address.
2367 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002368 // If the callee is a GlobalAddress node (quite common, every direct call
2369 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2370 // it.
2371
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002372 // We should use extra load for direct calls to dllimported functions in
2373 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002374 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002375 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002376 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002377 bool ExtraLoad = false;
2378 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002379
Chris Lattner48a7d022009-07-09 05:02:21 +00002380 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2381 // external symbols most go through the PLT in PIC mode. If the symbol
2382 // has hidden or protected visibility, or if it is static or local, then
2383 // we don't need to use the PLT - we can directly call it.
2384 if (Subtarget->isTargetELF() &&
2385 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002386 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002387 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002388 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002389 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002390 (!Subtarget->getTargetTriple().isMacOSX() ||
2391 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002392 // PC-relative references to external symbols should go through $stub,
2393 // unless we're building with the leopard linker or later, which
2394 // automatically synthesizes these stubs.
2395 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002396 } else if (Subtarget->isPICStyleRIPRel() &&
2397 isa<Function>(GV) &&
2398 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2399 // If the function is marked as non-lazy, generate an indirect call
2400 // which loads from the GOT directly. This avoids runtime overhead
2401 // at the cost of eager binding (and one extra byte of encoding).
2402 OpFlags = X86II::MO_GOTPCREL;
2403 WrapperKind = X86ISD::WrapperRIP;
2404 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002405 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002406
Devang Patel0d881da2010-07-06 22:08:15 +00002407 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002408 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002409
2410 // Add a wrapper if needed.
2411 if (WrapperKind != ISD::DELETED_NODE)
2412 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2413 // Add extra indirection if needed.
2414 if (ExtraLoad)
2415 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2416 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002417 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002418 }
Bill Wendling056292f2008-09-16 21:48:12 +00002419 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002420 unsigned char OpFlags = 0;
2421
Evan Cheng1bf891a2010-12-01 22:59:46 +00002422 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2423 // external symbols should go through the PLT.
2424 if (Subtarget->isTargetELF() &&
2425 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2426 OpFlags = X86II::MO_PLT;
2427 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002428 (!Subtarget->getTargetTriple().isMacOSX() ||
2429 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002430 // PC-relative references to external symbols should go through $stub,
2431 // unless we're building with the leopard linker or later, which
2432 // automatically synthesizes these stubs.
2433 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002434 }
Eric Christopherfd179292009-08-27 18:07:15 +00002435
Chris Lattner48a7d022009-07-09 05:02:21 +00002436 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2437 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002438 }
2439
Chris Lattnerd96d0722007-02-25 06:40:16 +00002440 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002441 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002442 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002443
Evan Chengf22f9b32010-02-06 03:28:46 +00002444 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002445 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2446 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002447 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002448 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002449
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002450 Ops.push_back(Chain);
2451 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002452
Dan Gohman98ca4f22009-08-05 01:29:28 +00002453 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002454 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002455
Gordon Henriksen86737662008-01-05 16:56:59 +00002456 // Add argument registers to the end of the list so that they are known live
2457 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002458 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2459 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2460 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002461
Evan Cheng586ccac2008-03-18 23:36:35 +00002462 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002463 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002464 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2465
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002466 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002467 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002468 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002469
Gabor Greifba36cb52008-08-28 21:40:38 +00002470 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002471 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002472
Dan Gohman98ca4f22009-08-05 01:29:28 +00002473 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002474 // We used to do:
2475 //// If this is the first return lowered for this function, add the regs
2476 //// to the liveout set for the function.
2477 // This isn't right, although it's probably harmless on x86; liveouts
2478 // should be computed from returns not tail calls. Consider a void
2479 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002480 return DAG.getNode(X86ISD::TC_RETURN, dl,
2481 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002482 }
2483
Dale Johannesenace16102009-02-03 19:33:06 +00002484 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002485 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002486
Chris Lattner2d297092006-05-23 18:50:38 +00002487 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002488 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002489 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2490 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002491 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002492 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002493 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002494 // pops the hidden struct pointer, so we have to push it back.
2495 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002496 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002497 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002498 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002499
Gordon Henriksenae636f82008-01-03 16:47:34 +00002500 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002501 if (!IsSibcall) {
2502 Chain = DAG.getCALLSEQ_END(Chain,
2503 DAG.getIntPtrConstant(NumBytes, true),
2504 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2505 true),
2506 InFlag);
2507 InFlag = Chain.getValue(1);
2508 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002509
Chris Lattner3085e152007-02-25 08:59:22 +00002510 // Handle result values, copying them out of physregs into vregs that we
2511 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002512 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2513 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002514}
2515
Evan Cheng25ab6902006-09-08 06:48:29 +00002516
2517//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002518// Fast Calling Convention (tail call) implementation
2519//===----------------------------------------------------------------------===//
2520
2521// Like std call, callee cleans arguments, convention except that ECX is
2522// reserved for storing the tail called function address. Only 2 registers are
2523// free for argument passing (inreg). Tail call optimization is performed
2524// provided:
2525// * tailcallopt is enabled
2526// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002527// On X86_64 architecture with GOT-style position independent code only local
2528// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002529// To keep the stack aligned according to platform abi the function
2530// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2531// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002532// If a tail called function callee has more arguments than the caller the
2533// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002534// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002535// original REtADDR, but before the saved framepointer or the spilled registers
2536// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2537// stack layout:
2538// arg1
2539// arg2
2540// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002541// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002542// move area ]
2543// (possible EBP)
2544// ESI
2545// EDI
2546// local1 ..
2547
2548/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2549/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002550unsigned
2551X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2552 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002553 MachineFunction &MF = DAG.getMachineFunction();
2554 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002555 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002556 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002557 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002558 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002559 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002560 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2561 // Number smaller than 12 so just add the difference.
2562 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2563 } else {
2564 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002565 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002566 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002567 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002568 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002569}
2570
Evan Cheng5f941932010-02-05 02:21:12 +00002571/// MatchingStackOffset - Return true if the given stack call argument is
2572/// already available in the same position (relatively) of the caller's
2573/// incoming argument stack.
2574static
2575bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2576 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2577 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002578 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2579 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002580 if (Arg.getOpcode() == ISD::CopyFromReg) {
2581 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002582 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002583 return false;
2584 MachineInstr *Def = MRI->getVRegDef(VR);
2585 if (!Def)
2586 return false;
2587 if (!Flags.isByVal()) {
2588 if (!TII->isLoadFromStackSlot(Def, FI))
2589 return false;
2590 } else {
2591 unsigned Opcode = Def->getOpcode();
2592 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2593 Def->getOperand(1).isFI()) {
2594 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002595 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002596 } else
2597 return false;
2598 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002599 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2600 if (Flags.isByVal())
2601 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002602 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002603 // define @foo(%struct.X* %A) {
2604 // tail call @bar(%struct.X* byval %A)
2605 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002606 return false;
2607 SDValue Ptr = Ld->getBasePtr();
2608 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2609 if (!FINode)
2610 return false;
2611 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002612 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002613 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002614 FI = FINode->getIndex();
2615 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002616 } else
2617 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002618
Evan Cheng4cae1332010-03-05 08:38:04 +00002619 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002620 if (!MFI->isFixedObjectIndex(FI))
2621 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002622 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002623}
2624
Dan Gohman98ca4f22009-08-05 01:29:28 +00002625/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2626/// for tail call optimization. Targets which want to do tail call
2627/// optimization should implement this function.
2628bool
2629X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002630 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002631 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002632 bool isCalleeStructRet,
2633 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002634 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002635 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002636 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002637 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002638 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002639 CalleeCC != CallingConv::C)
2640 return false;
2641
Evan Cheng7096ae42010-01-29 06:45:59 +00002642 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002643 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002644 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002645 CallingConv::ID CallerCC = CallerF->getCallingConv();
2646 bool CCMatch = CallerCC == CalleeCC;
2647
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002648 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002649 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002650 return true;
2651 return false;
2652 }
2653
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002654 // Look for obvious safe cases to perform tail call optimization that do not
2655 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002656
Evan Cheng2c12cb42010-03-26 16:26:03 +00002657 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2658 // emit a special epilogue.
2659 if (RegInfo->needsStackRealignment(MF))
2660 return false;
2661
Evan Chenga375d472010-03-15 18:54:48 +00002662 // Also avoid sibcall optimization if either caller or callee uses struct
2663 // return semantics.
2664 if (isCalleeStructRet || isCallerStructRet)
2665 return false;
2666
Chad Rosier2416da32011-06-24 21:15:36 +00002667 // An stdcall caller is expected to clean up its arguments; the callee
2668 // isn't going to do that.
2669 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2670 return false;
2671
Chad Rosier871f6642011-05-18 19:59:50 +00002672 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002673 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002674 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002675
2676 // Optimizing for varargs on Win64 is unlikely to be safe without
2677 // additional testing.
2678 if (Subtarget->isTargetWin64())
2679 return false;
2680
Chad Rosier871f6642011-05-18 19:59:50 +00002681 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002682 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2683 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002684
Chad Rosier871f6642011-05-18 19:59:50 +00002685 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2686 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2687 if (!ArgLocs[i].isRegLoc())
2688 return false;
2689 }
2690
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002691 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2692 // Therefore if it's not used by the call it is not safe to optimize this into
2693 // a sibcall.
2694 bool Unused = false;
2695 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2696 if (!Ins[i].Used) {
2697 Unused = true;
2698 break;
2699 }
2700 }
2701 if (Unused) {
2702 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002703 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2704 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002705 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002706 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002707 CCValAssign &VA = RVLocs[i];
2708 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2709 return false;
2710 }
2711 }
2712
Evan Cheng13617962010-04-30 01:12:32 +00002713 // If the calling conventions do not match, then we'd better make sure the
2714 // results are returned in the same way as what the caller expects.
2715 if (!CCMatch) {
2716 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002717 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2718 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002719 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2720
2721 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002722 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2723 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002724 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2725
2726 if (RVLocs1.size() != RVLocs2.size())
2727 return false;
2728 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2729 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2730 return false;
2731 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2732 return false;
2733 if (RVLocs1[i].isRegLoc()) {
2734 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2735 return false;
2736 } else {
2737 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2738 return false;
2739 }
2740 }
2741 }
2742
Evan Chenga6bff982010-01-30 01:22:00 +00002743 // If the callee takes no arguments then go on to check the results of the
2744 // call.
2745 if (!Outs.empty()) {
2746 // Check if stack adjustment is needed. For now, do not do this if any
2747 // argument is passed on the stack.
2748 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002749 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2750 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002751
2752 // Allocate shadow area for Win64
2753 if (Subtarget->isTargetWin64()) {
2754 CCInfo.AllocateStack(32, 8);
2755 }
2756
Duncan Sands45907662010-10-31 13:21:44 +00002757 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002758 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002759 MachineFunction &MF = DAG.getMachineFunction();
2760 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2761 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002762
2763 // Check if the arguments are already laid out in the right way as
2764 // the caller's fixed stack objects.
2765 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002766 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2767 const X86InstrInfo *TII =
2768 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002769 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2770 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002771 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002772 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002773 if (VA.getLocInfo() == CCValAssign::Indirect)
2774 return false;
2775 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002776 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2777 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002778 return false;
2779 }
2780 }
2781 }
Evan Cheng9c044672010-05-29 01:35:22 +00002782
2783 // If the tailcall address may be in a register, then make sure it's
2784 // possible to register allocate for it. In 32-bit, the call address can
2785 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002786 // callee-saved registers are restored. These happen to be the same
2787 // registers used to pass 'inreg' arguments so watch out for those.
2788 if (!Subtarget->is64Bit() &&
2789 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002790 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002791 unsigned NumInRegs = 0;
2792 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2793 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002794 if (!VA.isRegLoc())
2795 continue;
2796 unsigned Reg = VA.getLocReg();
2797 switch (Reg) {
2798 default: break;
2799 case X86::EAX: case X86::EDX: case X86::ECX:
2800 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002801 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002802 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002803 }
2804 }
2805 }
Evan Chenga6bff982010-01-30 01:22:00 +00002806 }
Evan Chengb1712452010-01-27 06:25:16 +00002807
Evan Cheng86809cc2010-02-03 03:28:02 +00002808 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002809}
2810
Dan Gohman3df24e62008-09-03 23:12:08 +00002811FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002812X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2813 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002814}
2815
2816
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002817//===----------------------------------------------------------------------===//
2818// Other Lowering Hooks
2819//===----------------------------------------------------------------------===//
2820
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002821static bool MayFoldLoad(SDValue Op) {
2822 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2823}
2824
2825static bool MayFoldIntoStore(SDValue Op) {
2826 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2827}
2828
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002829static bool isTargetShuffle(unsigned Opcode) {
2830 switch(Opcode) {
2831 default: return false;
2832 case X86ISD::PSHUFD:
2833 case X86ISD::PSHUFHW:
2834 case X86ISD::PSHUFLW:
2835 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002836 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002837 case X86ISD::SHUFPS:
2838 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002839 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002840 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002841 case X86ISD::MOVLPS:
2842 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002843 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002844 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002845 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002846 case X86ISD::MOVSS:
2847 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002848 case X86ISD::UNPCKL:
2849 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002850 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002851 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002852 return true;
2853 }
2854 return false;
2855}
2856
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002857static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002858 SDValue V1, SelectionDAG &DAG) {
2859 switch(Opc) {
2860 default: llvm_unreachable("Unknown x86 shuffle node");
2861 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002862 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002863 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002864 return DAG.getNode(Opc, dl, VT, V1);
2865 }
2866
2867 return SDValue();
2868}
2869
2870static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002871 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002872 switch(Opc) {
2873 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002874 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002875 case X86ISD::PSHUFHW:
2876 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002877 case X86ISD::VPERMILP:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002878 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2879 }
2880
2881 return SDValue();
2882}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002883
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002884static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2885 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2886 switch(Opc) {
2887 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002888 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002889 case X86ISD::SHUFPD:
2890 case X86ISD::SHUFPS:
Craig Topperec24e612011-11-30 07:47:51 +00002891 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002892 return DAG.getNode(Opc, dl, VT, V1, V2,
2893 DAG.getConstant(TargetMask, MVT::i8));
2894 }
2895 return SDValue();
2896}
2897
2898static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2899 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2900 switch(Opc) {
2901 default: llvm_unreachable("Unknown x86 shuffle node");
2902 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002903 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002904 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002905 case X86ISD::MOVLPS:
2906 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002907 case X86ISD::MOVSS:
2908 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002909 case X86ISD::UNPCKL:
2910 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002911 return DAG.getNode(Opc, dl, VT, V1, V2);
2912 }
2913 return SDValue();
2914}
2915
Dan Gohmand858e902010-04-17 15:26:15 +00002916SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002917 MachineFunction &MF = DAG.getMachineFunction();
2918 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2919 int ReturnAddrIndex = FuncInfo->getRAIndex();
2920
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002921 if (ReturnAddrIndex == 0) {
2922 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002923 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002924 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002925 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002926 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002927 }
2928
Evan Cheng25ab6902006-09-08 06:48:29 +00002929 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002930}
2931
2932
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002933bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2934 bool hasSymbolicDisplacement) {
2935 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002936 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002937 return false;
2938
2939 // If we don't have a symbolic displacement - we don't have any extra
2940 // restrictions.
2941 if (!hasSymbolicDisplacement)
2942 return true;
2943
2944 // FIXME: Some tweaks might be needed for medium code model.
2945 if (M != CodeModel::Small && M != CodeModel::Kernel)
2946 return false;
2947
2948 // For small code model we assume that latest object is 16MB before end of 31
2949 // bits boundary. We may also accept pretty large negative constants knowing
2950 // that all objects are in the positive half of address space.
2951 if (M == CodeModel::Small && Offset < 16*1024*1024)
2952 return true;
2953
2954 // For kernel code model we know that all object resist in the negative half
2955 // of 32bits address space. We may not accept negative offsets, since they may
2956 // be just off and we may accept pretty large positive ones.
2957 if (M == CodeModel::Kernel && Offset > 0)
2958 return true;
2959
2960 return false;
2961}
2962
Evan Chengef41ff62011-06-23 17:54:54 +00002963/// isCalleePop - Determines whether the callee is required to pop its
2964/// own arguments. Callee pop is necessary to support tail calls.
2965bool X86::isCalleePop(CallingConv::ID CallingConv,
2966 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2967 if (IsVarArg)
2968 return false;
2969
2970 switch (CallingConv) {
2971 default:
2972 return false;
2973 case CallingConv::X86_StdCall:
2974 return !is64Bit;
2975 case CallingConv::X86_FastCall:
2976 return !is64Bit;
2977 case CallingConv::X86_ThisCall:
2978 return !is64Bit;
2979 case CallingConv::Fast:
2980 return TailCallOpt;
2981 case CallingConv::GHC:
2982 return TailCallOpt;
2983 }
2984}
2985
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002986/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2987/// specific condition code, returning the condition code and the LHS/RHS of the
2988/// comparison to make.
2989static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2990 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002991 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002992 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2993 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2994 // X > -1 -> X == 0, jump !sign.
2995 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002996 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002997 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2998 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002999 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00003000 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003001 // X < 1 -> X <= 0
3002 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003003 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003004 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003005 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003006
Evan Chengd9558e02006-01-06 00:43:03 +00003007 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003008 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003009 case ISD::SETEQ: return X86::COND_E;
3010 case ISD::SETGT: return X86::COND_G;
3011 case ISD::SETGE: return X86::COND_GE;
3012 case ISD::SETLT: return X86::COND_L;
3013 case ISD::SETLE: return X86::COND_LE;
3014 case ISD::SETNE: return X86::COND_NE;
3015 case ISD::SETULT: return X86::COND_B;
3016 case ISD::SETUGT: return X86::COND_A;
3017 case ISD::SETULE: return X86::COND_BE;
3018 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003019 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003020 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003021
Chris Lattner4c78e022008-12-23 23:42:27 +00003022 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003023
Chris Lattner4c78e022008-12-23 23:42:27 +00003024 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003025 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3026 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003027 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3028 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003029 }
3030
Chris Lattner4c78e022008-12-23 23:42:27 +00003031 switch (SetCCOpcode) {
3032 default: break;
3033 case ISD::SETOLT:
3034 case ISD::SETOLE:
3035 case ISD::SETUGT:
3036 case ISD::SETUGE:
3037 std::swap(LHS, RHS);
3038 break;
3039 }
3040
3041 // On a floating point condition, the flags are set as follows:
3042 // ZF PF CF op
3043 // 0 | 0 | 0 | X > Y
3044 // 0 | 0 | 1 | X < Y
3045 // 1 | 0 | 0 | X == Y
3046 // 1 | 1 | 1 | unordered
3047 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003048 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003049 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003050 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003051 case ISD::SETOLT: // flipped
3052 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003053 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003054 case ISD::SETOLE: // flipped
3055 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003056 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003057 case ISD::SETUGT: // flipped
3058 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003059 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003060 case ISD::SETUGE: // flipped
3061 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003062 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003063 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003064 case ISD::SETNE: return X86::COND_NE;
3065 case ISD::SETUO: return X86::COND_P;
3066 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003067 case ISD::SETOEQ:
3068 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003069 }
Evan Chengd9558e02006-01-06 00:43:03 +00003070}
3071
Evan Cheng4a460802006-01-11 00:33:36 +00003072/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3073/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003074/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003075static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003076 switch (X86CC) {
3077 default:
3078 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003079 case X86::COND_B:
3080 case X86::COND_BE:
3081 case X86::COND_E:
3082 case X86::COND_P:
3083 case X86::COND_A:
3084 case X86::COND_AE:
3085 case X86::COND_NE:
3086 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003087 return true;
3088 }
3089}
3090
Evan Chengeb2f9692009-10-27 19:56:55 +00003091/// isFPImmLegal - Returns true if the target can instruction select the
3092/// specified FP immediate natively. If false, the legalizer will
3093/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003094bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003095 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3096 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3097 return true;
3098 }
3099 return false;
3100}
3101
Nate Begeman9008ca62009-04-27 18:41:29 +00003102/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3103/// the specified range (L, H].
3104static bool isUndefOrInRange(int Val, int Low, int Hi) {
3105 return (Val < 0) || (Val >= Low && Val < Hi);
3106}
3107
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003108/// isUndefOrInRange - Return true if every element in Mask, begining
3109/// from position Pos and ending in Pos+Size, falls within the specified
3110/// range (L, L+Pos]. or is undef.
3111static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3112 int Pos, int Size, int Low, int Hi) {
3113 for (int i = Pos, e = Pos+Size; i != e; ++i)
3114 if (!isUndefOrInRange(Mask[i], Low, Hi))
3115 return false;
3116 return true;
3117}
3118
Nate Begeman9008ca62009-04-27 18:41:29 +00003119/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3120/// specified value.
3121static bool isUndefOrEqual(int Val, int CmpVal) {
3122 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003123 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003124 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003125}
3126
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003127/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3128/// from position Pos and ending in Pos+Size, falls within the specified
3129/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003130static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3131 int Pos, int Size, int Low) {
3132 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3133 if (!isUndefOrEqual(Mask[i], Low))
3134 return false;
3135 return true;
3136}
3137
Nate Begeman9008ca62009-04-27 18:41:29 +00003138/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3139/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3140/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003141static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003142 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003143 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003144 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003145 return (Mask[0] < 2 && Mask[1] < 2);
3146 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003147}
3148
Nate Begeman9008ca62009-04-27 18:41:29 +00003149bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003150 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003151 N->getMask(M);
3152 return ::isPSHUFDMask(M, N->getValueType(0));
3153}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003154
Nate Begeman9008ca62009-04-27 18:41:29 +00003155/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3156/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003157static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003158 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003159 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003160
Nate Begeman9008ca62009-04-27 18:41:29 +00003161 // Lower quadword copied in order or undef.
3162 for (int i = 0; i != 4; ++i)
3163 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003164 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003165
Evan Cheng506d3df2006-03-29 23:07:14 +00003166 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003167 for (int i = 4; i != 8; ++i)
3168 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003169 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003170
Evan Cheng506d3df2006-03-29 23:07:14 +00003171 return true;
3172}
3173
Nate Begeman9008ca62009-04-27 18:41:29 +00003174bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003175 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003176 N->getMask(M);
3177 return ::isPSHUFHWMask(M, N->getValueType(0));
3178}
Evan Cheng506d3df2006-03-29 23:07:14 +00003179
Nate Begeman9008ca62009-04-27 18:41:29 +00003180/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3181/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003182static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003183 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003184 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003185
Rafael Espindola15684b22009-04-24 12:40:33 +00003186 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003187 for (int i = 4; i != 8; ++i)
3188 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003189 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003190
Rafael Espindola15684b22009-04-24 12:40:33 +00003191 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003192 for (int i = 0; i != 4; ++i)
3193 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003194 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003195
Rafael Espindola15684b22009-04-24 12:40:33 +00003196 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003197}
3198
Nate Begeman9008ca62009-04-27 18:41:29 +00003199bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003200 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003201 N->getMask(M);
3202 return ::isPSHUFLWMask(M, N->getValueType(0));
3203}
3204
Nate Begemana09008b2009-10-19 02:17:23 +00003205/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3206/// is suitable for input to PALIGNR.
3207static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003208 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003209 int i, e = VT.getVectorNumElements();
Craig Topper1dc0fbc2011-12-05 07:27:14 +00003210 if (VT.getSizeInBits() != 128)
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003211 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003212
Nate Begemana09008b2009-10-19 02:17:23 +00003213 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003214 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003215 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003216
Nate Begemana09008b2009-10-19 02:17:23 +00003217 for (i = 0; i != e; ++i)
3218 if (Mask[i] >= 0)
3219 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003220
Nate Begemana09008b2009-10-19 02:17:23 +00003221 // All undef, not a palignr.
3222 if (i == e)
3223 return false;
3224
Eli Friedman63f8dde2011-07-25 21:36:45 +00003225 // Make sure we're shifting in the right direction.
3226 if (Mask[i] <= i)
3227 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003228
3229 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003230
Nate Begemana09008b2009-10-19 02:17:23 +00003231 // Check the rest of the elements to see if they are consecutive.
3232 for (++i; i != e; ++i) {
3233 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003234 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003235 return false;
3236 }
3237 return true;
3238}
3239
Craig Topper9d7025b2011-11-27 21:41:12 +00003240/// isVSHUFPYMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003241/// specifies a shuffle of elements that is suitable for input to 256-bit
3242/// VSHUFPSY.
Craig Topper9d7025b2011-11-27 21:41:12 +00003243static bool isVSHUFPYMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper1ff73d72011-12-06 04:59:07 +00003244 bool HasAVX, bool Commuted = false) {
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003245 int NumElems = VT.getVectorNumElements();
3246
Craig Topper71c4c122011-11-28 01:14:24 +00003247 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003248 return false;
3249
Craig Topper9d7025b2011-11-27 21:41:12 +00003250 if (NumElems != 4 && NumElems != 8)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003251 return false;
3252
3253 // VSHUFPSY divides the resulting vector into 4 chunks.
3254 // The sources are also splitted into 4 chunks, and each destination
3255 // chunk must come from a different source chunk.
3256 //
3257 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3258 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3259 //
3260 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3261 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3262 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003263 // VSHUFPDY divides the resulting vector into 4 chunks.
3264 // The sources are also splitted into 4 chunks, and each destination
3265 // chunk must come from a different source chunk.
3266 //
3267 // SRC1 => X3 X2 X1 X0
3268 // SRC2 => Y3 Y2 Y1 Y0
3269 //
3270 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3271 //
Craig Topper1ff73d72011-12-06 04:59:07 +00003272 unsigned QuarterSize = NumElems/4;
3273 unsigned HalfSize = QuarterSize*2;
3274 for (unsigned l = 0; l != 2; ++l) {
3275 unsigned LaneStart = l*HalfSize;
3276 for (unsigned s = 0; s != 2; ++s) {
3277 unsigned QuarterStart = s*QuarterSize;
3278 unsigned Src = (Commuted) ? (1-s) : s;
3279 unsigned SrcStart = Src*NumElems + LaneStart;
3280 for (unsigned i = 0; i != QuarterSize; ++i) {
3281 int Idx = Mask[i+QuarterStart+LaneStart];
3282 if (!isUndefOrInRange(Idx, SrcStart, SrcStart+HalfSize))
3283 return false;
3284 // For VSHUFPSY, the mask of the second half must be the same as the first
3285 // but with the appropriate offsets. This works in the same way as
3286 // VPERMILPS works with masks.
3287 if (NumElems == 4 || l == 0 || Mask[i+QuarterStart] < 0)
3288 continue;
3289 if (!isUndefOrEqual(Idx, Mask[i+QuarterStart]+HalfSize))
3290 return false;
3291 }
3292 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003293 }
3294
3295 return true;
3296}
3297
Craig Topper9d7025b2011-11-27 21:41:12 +00003298/// getShuffleVSHUFPYImmediate - Return the appropriate immediate to shuffle
3299/// the specified VECTOR_MASK mask with VSHUFPSY/VSHUFPDY instructions.
3300static unsigned getShuffleVSHUFPYImmediate(SDNode *N) {
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003301 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3302 EVT VT = SVOp->getValueType(0);
3303 int NumElems = VT.getVectorNumElements();
3304
Craig Topper9d7025b2011-11-27 21:41:12 +00003305 assert(VT.getSizeInBits() == 256 && "Only supports 256-bit types");
3306 assert((NumElems == 4 || NumElems == 8) && "Only supports v4 and v8 types");
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003307
3308 int HalfSize = NumElems/2;
Craig Topper9d7025b2011-11-27 21:41:12 +00003309 unsigned Mul = (NumElems == 8) ? 2 : 1;
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003310 unsigned Mask = 0;
Craig Topper71c4c122011-11-28 01:14:24 +00003311 for (int i = 0; i != NumElems; ++i) {
Craig Topper9d7025b2011-11-27 21:41:12 +00003312 int Elt = SVOp->getMaskElt(i);
3313 if (Elt < 0)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003314 continue;
Craig Topper9d7025b2011-11-27 21:41:12 +00003315 Elt %= HalfSize;
3316 unsigned Shamt = i;
3317 // For VSHUFPSY, the mask of the first half must be equal to the second one.
3318 if (NumElems == 8) Shamt %= HalfSize;
3319 Mask |= Elt << (Shamt*Mul);
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003320 }
3321
3322 return Mask;
3323}
3324
Elena Demikhovsky52a35a82011-11-23 10:23:16 +00003325/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3326/// the two vector operands have swapped position.
Craig Topperbeabc6c2011-12-05 06:56:46 +00003327static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3328 unsigned NumElems) {
Elena Demikhovsky52a35a82011-11-23 10:23:16 +00003329 for (unsigned i = 0; i != NumElems; ++i) {
3330 int idx = Mask[i];
3331 if (idx < 0)
3332 continue;
3333 else if (idx < (int)NumElems)
3334 Mask[i] = idx + NumElems;
3335 else
3336 Mask[i] = idx - NumElems;
3337 }
3338}
3339
Evan Cheng14aed5e2006-03-24 01:18:28 +00003340/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003341/// specifies a shuffle of elements that is suitable for input to 128-bit
Craig Topper1ff73d72011-12-06 04:59:07 +00003342/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3343/// reverse of what x86 shuffles want.
3344static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT,
3345 bool Commuted = false) {
3346 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003347
3348 if (VT.getSizeInBits() != 128)
3349 return false;
3350
Nate Begeman9008ca62009-04-27 18:41:29 +00003351 if (NumElems != 2 && NumElems != 4)
3352 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003353
Craig Topper1ff73d72011-12-06 04:59:07 +00003354 unsigned Half = NumElems / 2;
3355 unsigned SrcStart = Commuted ? NumElems : 0;
3356 for (unsigned i = 0; i != Half; ++i)
3357 if (!isUndefOrInRange(Mask[i], SrcStart, SrcStart+NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003358 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003359 SrcStart = Commuted ? 0 : NumElems;
3360 for (unsigned i = Half; i != NumElems; ++i)
3361 if (!isUndefOrInRange(Mask[i], SrcStart, SrcStart+NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003362 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003363
Evan Cheng14aed5e2006-03-24 01:18:28 +00003364 return true;
3365}
3366
Nate Begeman9008ca62009-04-27 18:41:29 +00003367bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3368 SmallVector<int, 8> M;
3369 N->getMask(M);
3370 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003371}
3372
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003373/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3374/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003375bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003376 EVT VT = N->getValueType(0);
3377 unsigned NumElems = VT.getVectorNumElements();
3378
3379 if (VT.getSizeInBits() != 128)
3380 return false;
3381
3382 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003383 return false;
3384
Evan Cheng2064a2b2006-03-28 06:50:32 +00003385 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003386 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3387 isUndefOrEqual(N->getMaskElt(1), 7) &&
3388 isUndefOrEqual(N->getMaskElt(2), 2) &&
3389 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003390}
3391
Nate Begeman0b10b912009-11-07 23:17:15 +00003392/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3393/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3394/// <2, 3, 2, 3>
3395bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003396 EVT VT = N->getValueType(0);
3397 unsigned NumElems = VT.getVectorNumElements();
3398
3399 if (VT.getSizeInBits() != 128)
3400 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003401
Nate Begeman0b10b912009-11-07 23:17:15 +00003402 if (NumElems != 4)
3403 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003404
Nate Begeman0b10b912009-11-07 23:17:15 +00003405 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003406 isUndefOrEqual(N->getMaskElt(1), 3) &&
3407 isUndefOrEqual(N->getMaskElt(2), 2) &&
3408 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003409}
3410
Evan Cheng5ced1d82006-04-06 23:23:56 +00003411/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3412/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003413bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3414 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003415
Evan Cheng5ced1d82006-04-06 23:23:56 +00003416 if (NumElems != 2 && NumElems != 4)
3417 return false;
3418
Evan Chengc5cdff22006-04-07 21:53:05 +00003419 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003420 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003421 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003422
Evan Chengc5cdff22006-04-07 21:53:05 +00003423 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003424 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003425 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003426
3427 return true;
3428}
3429
Nate Begeman0b10b912009-11-07 23:17:15 +00003430/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3431/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3432bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003433 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003434
David Greenea20244d2011-03-02 17:23:43 +00003435 if ((NumElems != 2 && NumElems != 4)
3436 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003437 return false;
3438
Evan Chengc5cdff22006-04-07 21:53:05 +00003439 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003440 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003441 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003442
Nate Begeman9008ca62009-04-27 18:41:29 +00003443 for (unsigned i = 0; i < NumElems/2; ++i)
3444 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003445 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003446
3447 return true;
3448}
3449
Evan Cheng0038e592006-03-28 00:39:58 +00003450/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3451/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003452static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003453 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003454 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003455
3456 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3457 "Unsupported vector type for unpckh");
3458
Craig Topper6347e862011-11-21 06:57:39 +00003459 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003460 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003461 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003462
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003463 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3464 // independently on 128-bit lanes.
3465 unsigned NumLanes = VT.getSizeInBits()/128;
3466 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003467
3468 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003469 unsigned End = NumLaneElts;
3470 for (unsigned s = 0; s < NumLanes; ++s) {
3471 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003472 i != End;
3473 i += 2, ++j) {
3474 int BitI = Mask[i];
3475 int BitI1 = Mask[i+1];
3476 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003477 return false;
David Greenea20244d2011-03-02 17:23:43 +00003478 if (V2IsSplat) {
3479 if (!isUndefOrEqual(BitI1, NumElts))
3480 return false;
3481 } else {
3482 if (!isUndefOrEqual(BitI1, j + NumElts))
3483 return false;
3484 }
Evan Cheng39623da2006-04-20 08:58:49 +00003485 }
David Greenea20244d2011-03-02 17:23:43 +00003486 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003487 Start += NumLaneElts;
3488 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003489 }
David Greenea20244d2011-03-02 17:23:43 +00003490
Evan Cheng0038e592006-03-28 00:39:58 +00003491 return true;
3492}
3493
Craig Topper6347e862011-11-21 06:57:39 +00003494bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003495 SmallVector<int, 8> M;
3496 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003497 return ::isUNPCKLMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003498}
3499
Evan Cheng4fcb9222006-03-28 02:43:26 +00003500/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3501/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003502static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003503 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003504 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003505
3506 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3507 "Unsupported vector type for unpckh");
3508
Craig Topper6347e862011-11-21 06:57:39 +00003509 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003510 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003511 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003512
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003513 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3514 // independently on 128-bit lanes.
3515 unsigned NumLanes = VT.getSizeInBits()/128;
3516 unsigned NumLaneElts = NumElts/NumLanes;
3517
3518 unsigned Start = 0;
3519 unsigned End = NumLaneElts;
3520 for (unsigned l = 0; l != NumLanes; ++l) {
3521 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3522 i != End; i += 2, ++j) {
3523 int BitI = Mask[i];
3524 int BitI1 = Mask[i+1];
3525 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003526 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003527 if (V2IsSplat) {
3528 if (isUndefOrEqual(BitI1, NumElts))
3529 return false;
3530 } else {
3531 if (!isUndefOrEqual(BitI1, j+NumElts))
3532 return false;
3533 }
Evan Cheng39623da2006-04-20 08:58:49 +00003534 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003535 // Process the next 128 bits.
3536 Start += NumLaneElts;
3537 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003538 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003539 return true;
3540}
3541
Craig Topper6347e862011-11-21 06:57:39 +00003542bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003543 SmallVector<int, 8> M;
3544 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003545 return ::isUNPCKHMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003546}
3547
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003548/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3549/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3550/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003551static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003552 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003553 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003554 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003555
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003556 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3557 // FIXME: Need a better way to get rid of this, there's no latency difference
3558 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3559 // the former later. We should also remove the "_undef" special mask.
3560 if (NumElems == 4 && VT.getSizeInBits() == 256)
3561 return false;
3562
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003563 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3564 // independently on 128-bit lanes.
3565 unsigned NumLanes = VT.getSizeInBits() / 128;
3566 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003567
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003568 for (unsigned s = 0; s < NumLanes; ++s) {
3569 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3570 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003571 i += 2, ++j) {
3572 int BitI = Mask[i];
3573 int BitI1 = Mask[i+1];
3574
3575 if (!isUndefOrEqual(BitI, j))
3576 return false;
3577 if (!isUndefOrEqual(BitI1, j))
3578 return false;
3579 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003580 }
David Greenea20244d2011-03-02 17:23:43 +00003581
Rafael Espindola15684b22009-04-24 12:40:33 +00003582 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003583}
3584
Nate Begeman9008ca62009-04-27 18:41:29 +00003585bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3586 SmallVector<int, 8> M;
3587 N->getMask(M);
3588 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3589}
3590
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003591/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3592/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3593/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003594static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003595 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003596 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3597 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003598
Nate Begeman9008ca62009-04-27 18:41:29 +00003599 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3600 int BitI = Mask[i];
3601 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003602 if (!isUndefOrEqual(BitI, j))
3603 return false;
3604 if (!isUndefOrEqual(BitI1, j))
3605 return false;
3606 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003607 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003608}
3609
Nate Begeman9008ca62009-04-27 18:41:29 +00003610bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3611 SmallVector<int, 8> M;
3612 N->getMask(M);
3613 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3614}
3615
Evan Cheng017dcc62006-04-21 01:05:10 +00003616/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3617/// specifies a shuffle of elements that is suitable for input to MOVSS,
3618/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003619static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003620 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003621 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003622
3623 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003624
Nate Begeman9008ca62009-04-27 18:41:29 +00003625 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003626 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003627
Nate Begeman9008ca62009-04-27 18:41:29 +00003628 for (int i = 1; i < NumElts; ++i)
3629 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003630 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003631
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003632 return true;
3633}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003634
Nate Begeman9008ca62009-04-27 18:41:29 +00003635bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3636 SmallVector<int, 8> M;
3637 N->getMask(M);
3638 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003639}
3640
Craig Topper70b883b2011-11-28 10:14:51 +00003641/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003642/// as permutations between 128-bit chunks or halves. As an example: this
3643/// shuffle bellow:
3644/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3645/// The first half comes from the second half of V1 and the second half from the
3646/// the second half of V2.
Craig Topper70b883b2011-11-28 10:14:51 +00003647static bool isVPERM2X128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3648 bool HasAVX) {
3649 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003650 return false;
3651
3652 // The shuffle result is divided into half A and half B. In total the two
3653 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3654 // B must come from C, D, E or F.
3655 int HalfSize = VT.getVectorNumElements()/2;
3656 bool MatchA = false, MatchB = false;
3657
3658 // Check if A comes from one of C, D, E, F.
3659 for (int Half = 0; Half < 4; ++Half) {
3660 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3661 MatchA = true;
3662 break;
3663 }
3664 }
3665
3666 // Check if B comes from one of C, D, E, F.
3667 for (int Half = 0; Half < 4; ++Half) {
3668 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3669 MatchB = true;
3670 break;
3671 }
3672 }
3673
3674 return MatchA && MatchB;
3675}
3676
Craig Topper70b883b2011-11-28 10:14:51 +00003677/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3678/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
3679static unsigned getShuffleVPERM2X128Immediate(SDNode *N) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003680 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3681 EVT VT = SVOp->getValueType(0);
3682
3683 int HalfSize = VT.getVectorNumElements()/2;
3684
3685 int FstHalf = 0, SndHalf = 0;
3686 for (int i = 0; i < HalfSize; ++i) {
3687 if (SVOp->getMaskElt(i) > 0) {
3688 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3689 break;
3690 }
3691 }
3692 for (int i = HalfSize; i < HalfSize*2; ++i) {
3693 if (SVOp->getMaskElt(i) > 0) {
3694 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3695 break;
3696 }
3697 }
3698
3699 return (FstHalf | (SndHalf << 4));
3700}
3701
Craig Topper70b883b2011-11-28 10:14:51 +00003702/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003703/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3704/// Note that VPERMIL mask matching is different depending whether theunderlying
3705/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3706/// to the same elements of the low, but to the higher half of the source.
3707/// In VPERMILPD the two lanes could be shuffled independently of each other
3708/// with the same restriction that lanes can't be crossed.
Craig Topper70b883b2011-11-28 10:14:51 +00003709static bool isVPERMILPMask(const SmallVectorImpl<int> &Mask, EVT VT,
3710 bool HasAVX) {
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003711 int NumElts = VT.getVectorNumElements();
3712 int NumLanes = VT.getSizeInBits()/128;
3713
Craig Topper70b883b2011-11-28 10:14:51 +00003714 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003715 return false;
3716
Craig Topper70b883b2011-11-28 10:14:51 +00003717 // Only match 256-bit with 32/64-bit types
3718 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003719 return false;
3720
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003721 int LaneSize = NumElts/NumLanes;
Craig Topper70b883b2011-11-28 10:14:51 +00003722 for (int l = 0; l != NumLanes; ++l) {
3723 int LaneStart = l*LaneSize;
3724 for (int i = 0; i != LaneSize; ++i) {
3725 if (!isUndefOrInRange(Mask[i+LaneStart], LaneStart, LaneStart+LaneSize))
3726 return false;
3727 if (NumElts == 4 || l == 0)
3728 continue;
3729 // VPERMILPS handling
3730 if (Mask[i] < 0)
3731 continue;
3732 if (!isUndefOrEqual(Mask[i+LaneStart], Mask[i]+LaneSize))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003733 return false;
3734 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003735 }
3736
3737 return true;
3738}
3739
Craig Topper70b883b2011-11-28 10:14:51 +00003740/// getShuffleVPERMILPImmediate - Return the appropriate immediate to shuffle
3741/// the specified VECTOR_MASK mask with VPERMILPS/D* instructions.
3742static unsigned getShuffleVPERMILPImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003743 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3744 EVT VT = SVOp->getValueType(0);
3745
3746 int NumElts = VT.getVectorNumElements();
3747 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003748 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003749
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003750 // Although the mask is equal for both lanes do it twice to get the cases
3751 // where a mask will match because the same mask element is undef on the
3752 // first half but valid on the second. This would get pathological cases
3753 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Craig Topper70b883b2011-11-28 10:14:51 +00003754 unsigned Shift = (LaneSize == 4) ? 2 : 1;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003755 unsigned Mask = 0;
Craig Topper70b883b2011-11-28 10:14:51 +00003756 for (int i = 0; i != NumElts; ++i) {
3757 int MaskElt = SVOp->getMaskElt(i);
3758 if (MaskElt < 0)
3759 continue;
3760 MaskElt %= LaneSize;
3761 unsigned Shamt = i;
3762 // VPERMILPSY, the mask of the first half must be equal to the second one
3763 if (NumElts == 8) Shamt %= LaneSize;
3764 Mask |= MaskElt << (Shamt*Shift);
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003765 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003766
3767 return Mask;
3768}
3769
Evan Cheng017dcc62006-04-21 01:05:10 +00003770/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3771/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003772/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003773static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003774 bool V2IsSplat = false, bool V2IsUndef = false) {
3775 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003776 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003777 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003778
Nate Begeman9008ca62009-04-27 18:41:29 +00003779 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003780 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003781
Nate Begeman9008ca62009-04-27 18:41:29 +00003782 for (int i = 1; i < NumOps; ++i)
3783 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3784 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3785 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003786 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003787
Evan Cheng39623da2006-04-20 08:58:49 +00003788 return true;
3789}
3790
Nate Begeman9008ca62009-04-27 18:41:29 +00003791static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003792 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003793 SmallVector<int, 8> M;
3794 N->getMask(M);
3795 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003796}
3797
Evan Chengd9539472006-04-14 21:59:03 +00003798/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3799/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003800/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3801bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3802 const X86Subtarget *Subtarget) {
Craig Topperc0d82852011-11-22 00:44:41 +00003803 if (!Subtarget->hasSSE3orAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003804 return false;
3805
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003806 // The second vector must be undef
3807 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3808 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003809
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003810 EVT VT = N->getValueType(0);
3811 unsigned NumElems = VT.getVectorNumElements();
3812
3813 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3814 (VT.getSizeInBits() == 256 && NumElems != 8))
3815 return false;
3816
3817 // "i+1" is the value the indexed mask element must have
3818 for (unsigned i = 0; i < NumElems; i += 2)
3819 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3820 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003821 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003822
3823 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003824}
3825
3826/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3827/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003828/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3829bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3830 const X86Subtarget *Subtarget) {
Craig Topperc0d82852011-11-22 00:44:41 +00003831 if (!Subtarget->hasSSE3orAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003832 return false;
3833
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003834 // The second vector must be undef
3835 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3836 return false;
3837
3838 EVT VT = N->getValueType(0);
3839 unsigned NumElems = VT.getVectorNumElements();
3840
3841 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3842 (VT.getSizeInBits() == 256 && NumElems != 8))
3843 return false;
3844
3845 // "i" is the value the indexed mask element must have
3846 for (unsigned i = 0; i < NumElems; i += 2)
3847 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3848 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003849 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003850
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003851 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003852}
3853
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003854/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3855/// specifies a shuffle of elements that is suitable for input to 256-bit
3856/// version of MOVDDUP.
Craig Topperbeabc6c2011-12-05 06:56:46 +00003857static bool isMOVDDUPYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3858 bool HasAVX) {
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003859 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003860
Craig Topperbeabc6c2011-12-05 06:56:46 +00003861 if (!HasAVX || VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003862 return false;
3863
3864 for (int i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003865 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003866 return false;
3867 for (int i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003868 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003869 return false;
3870 return true;
3871}
3872
Evan Cheng0b457f02008-09-25 20:50:48 +00003873/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003874/// specifies a shuffle of elements that is suitable for input to 128-bit
3875/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003876bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003877 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003878
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003879 if (VT.getSizeInBits() != 128)
3880 return false;
3881
3882 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003883 for (int i = 0; i < e; ++i)
3884 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003885 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003886 for (int i = 0; i < e; ++i)
3887 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003888 return false;
3889 return true;
3890}
3891
David Greenec38a03e2011-02-03 15:50:00 +00003892/// isVEXTRACTF128Index - Return true if the specified
3893/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3894/// suitable for input to VEXTRACTF128.
3895bool X86::isVEXTRACTF128Index(SDNode *N) {
3896 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3897 return false;
3898
3899 // The index should be aligned on a 128-bit boundary.
3900 uint64_t Index =
3901 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3902
3903 unsigned VL = N->getValueType(0).getVectorNumElements();
3904 unsigned VBits = N->getValueType(0).getSizeInBits();
3905 unsigned ElSize = VBits / VL;
3906 bool Result = (Index * ElSize) % 128 == 0;
3907
3908 return Result;
3909}
3910
David Greeneccacdc12011-02-04 16:08:29 +00003911/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3912/// operand specifies a subvector insert that is suitable for input to
3913/// VINSERTF128.
3914bool X86::isVINSERTF128Index(SDNode *N) {
3915 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3916 return false;
3917
3918 // The index should be aligned on a 128-bit boundary.
3919 uint64_t Index =
3920 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3921
3922 unsigned VL = N->getValueType(0).getVectorNumElements();
3923 unsigned VBits = N->getValueType(0).getSizeInBits();
3924 unsigned ElSize = VBits / VL;
3925 bool Result = (Index * ElSize) % 128 == 0;
3926
3927 return Result;
3928}
3929
Evan Cheng63d33002006-03-22 08:01:21 +00003930/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003931/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003932unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003933 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3934 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3935
Evan Chengb9df0ca2006-03-22 02:53:00 +00003936 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3937 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003938 for (int i = 0; i < NumOperands; ++i) {
3939 int Val = SVOp->getMaskElt(NumOperands-i-1);
3940 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003941 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003942 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003943 if (i != NumOperands - 1)
3944 Mask <<= Shift;
3945 }
Evan Cheng63d33002006-03-22 08:01:21 +00003946 return Mask;
3947}
3948
Evan Cheng506d3df2006-03-29 23:07:14 +00003949/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003950/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003951unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003952 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003953 unsigned Mask = 0;
3954 // 8 nodes, but we only care about the last 4.
3955 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003956 int Val = SVOp->getMaskElt(i);
3957 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003958 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003959 if (i != 4)
3960 Mask <<= 2;
3961 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003962 return Mask;
3963}
3964
3965/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003966/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003967unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003968 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003969 unsigned Mask = 0;
3970 // 8 nodes, but we only care about the first 4.
3971 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003972 int Val = SVOp->getMaskElt(i);
3973 if (Val >= 0)
3974 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003975 if (i != 0)
3976 Mask <<= 2;
3977 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003978 return Mask;
3979}
3980
Nate Begemana09008b2009-10-19 02:17:23 +00003981/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3982/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3983unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3984 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3985 EVT VVT = N->getValueType(0);
3986 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3987 int Val = 0;
3988
3989 unsigned i, e;
3990 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3991 Val = SVOp->getMaskElt(i);
3992 if (Val >= 0)
3993 break;
3994 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00003995 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00003996 return (Val - i) * EltSize;
3997}
3998
David Greenec38a03e2011-02-03 15:50:00 +00003999/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4000/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4001/// instructions.
4002unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4003 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4004 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4005
4006 uint64_t Index =
4007 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4008
4009 EVT VecVT = N->getOperand(0).getValueType();
4010 EVT ElVT = VecVT.getVectorElementType();
4011
4012 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004013 return Index / NumElemsPerChunk;
4014}
4015
David Greeneccacdc12011-02-04 16:08:29 +00004016/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4017/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4018/// instructions.
4019unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4020 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4021 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4022
4023 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004024 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004025
4026 EVT VecVT = N->getValueType(0);
4027 EVT ElVT = VecVT.getVectorElementType();
4028
4029 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004030 return Index / NumElemsPerChunk;
4031}
4032
Evan Cheng37b73872009-07-30 08:33:02 +00004033/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4034/// constant +0.0.
4035bool X86::isZeroNode(SDValue Elt) {
4036 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004037 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004038 (isa<ConstantFPSDNode>(Elt) &&
4039 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4040}
4041
Nate Begeman9008ca62009-04-27 18:41:29 +00004042/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4043/// their permute mask.
4044static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4045 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004046 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004047 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004048 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004049
Nate Begeman5a5ca152009-04-29 05:20:52 +00004050 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004051 int idx = SVOp->getMaskElt(i);
4052 if (idx < 0)
4053 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004054 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004055 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004056 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004057 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004058 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004059 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4060 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004061}
4062
Evan Cheng533a0aa2006-04-19 20:35:22 +00004063/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4064/// match movhlps. The lower half elements should come from upper half of
4065/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004066/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004067static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004068 EVT VT = Op->getValueType(0);
4069 if (VT.getSizeInBits() != 128)
4070 return false;
4071 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004072 return false;
4073 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004074 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004075 return false;
4076 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004077 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004078 return false;
4079 return true;
4080}
4081
Evan Cheng5ced1d82006-04-06 23:23:56 +00004082/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004083/// is promoted to a vector. It also returns the LoadSDNode by reference if
4084/// required.
4085static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004086 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4087 return false;
4088 N = N->getOperand(0).getNode();
4089 if (!ISD::isNON_EXTLoad(N))
4090 return false;
4091 if (LD)
4092 *LD = cast<LoadSDNode>(N);
4093 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004094}
4095
Dan Gohman65fd6562011-11-03 21:49:52 +00004096// Test whether the given value is a vector value which will be legalized
4097// into a load.
4098static bool WillBeConstantPoolLoad(SDNode *N) {
4099 if (N->getOpcode() != ISD::BUILD_VECTOR)
4100 return false;
4101
4102 // Check for any non-constant elements.
4103 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4104 switch (N->getOperand(i).getNode()->getOpcode()) {
4105 case ISD::UNDEF:
4106 case ISD::ConstantFP:
4107 case ISD::Constant:
4108 break;
4109 default:
4110 return false;
4111 }
4112
4113 // Vectors of all-zeros and all-ones are materialized with special
4114 // instructions rather than being loaded.
4115 return !ISD::isBuildVectorAllZeros(N) &&
4116 !ISD::isBuildVectorAllOnes(N);
4117}
4118
Evan Cheng533a0aa2006-04-19 20:35:22 +00004119/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4120/// match movlp{s|d}. The lower half elements should come from lower half of
4121/// V1 (and in order), and the upper half elements should come from the upper
4122/// half of V2 (and in order). And since V1 will become the source of the
4123/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004124static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4125 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004126 EVT VT = Op->getValueType(0);
4127 if (VT.getSizeInBits() != 128)
4128 return false;
4129
Evan Cheng466685d2006-10-09 20:57:25 +00004130 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004131 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004132 // Is V2 is a vector load, don't do this transformation. We will try to use
4133 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004134 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004135 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004136
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004137 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004138
Evan Cheng533a0aa2006-04-19 20:35:22 +00004139 if (NumElems != 2 && NumElems != 4)
4140 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004141 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004142 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004143 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004144 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004145 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004146 return false;
4147 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004148}
4149
Evan Cheng39623da2006-04-20 08:58:49 +00004150/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4151/// all the same.
4152static bool isSplatVector(SDNode *N) {
4153 if (N->getOpcode() != ISD::BUILD_VECTOR)
4154 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004155
Dan Gohman475871a2008-07-27 21:46:04 +00004156 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004157 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4158 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004159 return false;
4160 return true;
4161}
4162
Evan Cheng213d2cf2007-05-17 18:45:50 +00004163/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004164/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004165/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004166static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004167 SDValue V1 = N->getOperand(0);
4168 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004169 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4170 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004171 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004172 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004173 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004174 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4175 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004176 if (Opc != ISD::BUILD_VECTOR ||
4177 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004178 return false;
4179 } else if (Idx >= 0) {
4180 unsigned Opc = V1.getOpcode();
4181 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4182 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004183 if (Opc != ISD::BUILD_VECTOR ||
4184 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004185 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004186 }
4187 }
4188 return true;
4189}
4190
4191/// getZeroVector - Returns a vector of specified type with all zero elements.
4192///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004193static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004194 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004195 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004196
Dale Johannesen0488fb62010-09-30 23:57:10 +00004197 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004198 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004199 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004200 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004201 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004202 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4203 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4204 } else { // SSE1
4205 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4206 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4207 }
4208 } else if (VT.getSizeInBits() == 256) { // AVX
4209 // 256-bit logic and arithmetic instructions in AVX are
4210 // all floating-point, no support for integer ops. Default
4211 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004212 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004213 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4214 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004215 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004216 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004217}
4218
Chris Lattner8a594482007-11-25 00:24:49 +00004219/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004220/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4221/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4222/// Then bitcast to their original type, ensuring they get CSE'd.
4223static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4224 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004225 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004226 assert((VT.is128BitVector() || VT.is256BitVector())
4227 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004228
Owen Anderson825b72b2009-08-11 20:47:22 +00004229 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004230 SDValue Vec;
4231 if (VT.getSizeInBits() == 256) {
4232 if (HasAVX2) { // AVX2
4233 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4234 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4235 } else { // AVX
4236 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4237 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4238 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4239 Vec = Insert128BitVector(InsV, Vec,
4240 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4241 }
4242 } else {
4243 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004244 }
4245
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004246 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004247}
4248
Evan Cheng39623da2006-04-20 08:58:49 +00004249/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4250/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004251static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004252 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004253 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004254
Evan Cheng39623da2006-04-20 08:58:49 +00004255 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004256 SmallVector<int, 8> MaskVec;
4257 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004258
Nate Begeman5a5ca152009-04-29 05:20:52 +00004259 for (unsigned i = 0; i != NumElems; ++i) {
4260 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004261 MaskVec[i] = NumElems;
4262 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004263 }
Evan Cheng39623da2006-04-20 08:58:49 +00004264 }
Evan Cheng39623da2006-04-20 08:58:49 +00004265 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004266 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4267 SVOp->getOperand(1), &MaskVec[0]);
4268 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004269}
4270
Evan Cheng017dcc62006-04-21 01:05:10 +00004271/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4272/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004273static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004274 SDValue V2) {
4275 unsigned NumElems = VT.getVectorNumElements();
4276 SmallVector<int, 8> Mask;
4277 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004278 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004279 Mask.push_back(i);
4280 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004281}
4282
Nate Begeman9008ca62009-04-27 18:41:29 +00004283/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004284static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004285 SDValue V2) {
4286 unsigned NumElems = VT.getVectorNumElements();
4287 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004288 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004289 Mask.push_back(i);
4290 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004291 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004292 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004293}
4294
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004295/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004296static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004297 SDValue V2) {
4298 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004299 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004300 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004301 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004302 Mask.push_back(i + Half);
4303 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004304 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004305 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004306}
4307
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004308// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004309// a generic shuffle instruction because the target has no such instructions.
4310// Generate shuffles which repeat i16 and i8 several times until they can be
4311// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004312static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004313 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004314 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004315 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004316
Nate Begeman9008ca62009-04-27 18:41:29 +00004317 while (NumElems > 4) {
4318 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004319 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004320 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004321 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004322 EltNo -= NumElems/2;
4323 }
4324 NumElems >>= 1;
4325 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004326 return V;
4327}
Eric Christopherfd179292009-08-27 18:07:15 +00004328
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004329/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4330static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4331 EVT VT = V.getValueType();
4332 DebugLoc dl = V.getDebugLoc();
4333 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4334 && "Vector size not supported");
4335
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004336 if (VT.getSizeInBits() == 128) {
4337 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004338 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004339 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4340 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004341 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004342 // To use VPERMILPS to splat scalars, the second half of indicies must
4343 // refer to the higher part, which is a duplication of the lower one,
4344 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004345 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4346 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004347
4348 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4349 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4350 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004351 }
4352
4353 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4354}
4355
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004356/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004357static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4358 EVT SrcVT = SV->getValueType(0);
4359 SDValue V1 = SV->getOperand(0);
4360 DebugLoc dl = SV->getDebugLoc();
4361
4362 int EltNo = SV->getSplatIndex();
4363 int NumElems = SrcVT.getVectorNumElements();
4364 unsigned Size = SrcVT.getSizeInBits();
4365
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004366 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4367 "Unknown how to promote splat for type");
4368
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004369 // Extract the 128-bit part containing the splat element and update
4370 // the splat element index when it refers to the higher register.
4371 if (Size == 256) {
4372 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4373 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4374 if (Idx > 0)
4375 EltNo -= NumElems/2;
4376 }
4377
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004378 // All i16 and i8 vector types can't be used directly by a generic shuffle
4379 // instruction because the target has no such instruction. Generate shuffles
4380 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004381 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004382 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004383 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004384 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004385
4386 // Recreate the 256-bit vector and place the same 128-bit vector
4387 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004388 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004389 if (Size == 256) {
4390 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4391 DAG.getConstant(0, MVT::i32), DAG, dl);
4392 V1 = Insert128BitVector(InsV, V1,
4393 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4394 }
4395
4396 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004397}
4398
Evan Chengba05f722006-04-21 23:03:30 +00004399/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004400/// vector of zero or undef vector. This produces a shuffle where the low
4401/// element of V2 is swizzled into the zero/undef vector, landing at element
4402/// 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 +00004403static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004404 bool isZero, bool HasXMMInt,
4405 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004406 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004407 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004408 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004409 unsigned NumElems = VT.getVectorNumElements();
4410 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004411 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004412 // If this is the insertion idx, put the low elt of V2 here.
4413 MaskVec.push_back(i == Idx ? NumElems : i);
4414 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004415}
4416
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004417/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4418/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004419static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4420 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004421 if (Depth == 6)
4422 return SDValue(); // Limit search depth.
4423
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004424 SDValue V = SDValue(N, 0);
4425 EVT VT = V.getValueType();
4426 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004427
4428 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4429 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4430 Index = SV->getMaskElt(Index);
4431
4432 if (Index < 0)
4433 return DAG.getUNDEF(VT.getVectorElementType());
4434
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004435 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004436 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004437 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004438 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004439
4440 // Recurse into target specific vector shuffles to find scalars.
4441 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004442 int NumElems = VT.getVectorNumElements();
4443 SmallVector<unsigned, 16> ShuffleMask;
4444 SDValue ImmN;
4445
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004446 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004447 case X86ISD::SHUFPS:
4448 case X86ISD::SHUFPD:
4449 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper36e36ac2011-11-29 07:49:05 +00004450 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4451 ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004452 break;
Craig Topper34671b82011-12-06 08:21:25 +00004453 case X86ISD::UNPCKH:
Craig Topper3d8c2ce2011-12-06 05:31:16 +00004454 DecodeUNPCKHMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004455 break;
Craig Topper34671b82011-12-06 08:21:25 +00004456 case X86ISD::UNPCKL:
Craig Topper3d8c2ce2011-12-06 05:31:16 +00004457 DecodeUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004458 break;
4459 case X86ISD::MOVHLPS:
4460 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4461 break;
4462 case X86ISD::MOVLHPS:
4463 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4464 break;
4465 case X86ISD::PSHUFD:
4466 ImmN = N->getOperand(N->getNumOperands()-1);
4467 DecodePSHUFMask(NumElems,
4468 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4469 ShuffleMask);
4470 break;
4471 case X86ISD::PSHUFHW:
4472 ImmN = N->getOperand(N->getNumOperands()-1);
4473 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4474 ShuffleMask);
4475 break;
4476 case X86ISD::PSHUFLW:
4477 ImmN = N->getOperand(N->getNumOperands()-1);
4478 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4479 ShuffleMask);
4480 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004481 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004482 case X86ISD::MOVSD: {
4483 // The index 0 always comes from the first element of the second source,
4484 // this is why MOVSS and MOVSD are used in the first place. The other
4485 // elements come from the other positions of the first source vector.
4486 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004487 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4488 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004489 }
Craig Topper316cd2a2011-11-30 06:25:25 +00004490 case X86ISD::VPERMILP:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004491 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper316cd2a2011-11-30 06:25:25 +00004492 DecodeVPERMILPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004493 ShuffleMask);
4494 break;
Craig Topperec24e612011-11-30 07:47:51 +00004495 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004496 ImmN = N->getOperand(N->getNumOperands()-1);
4497 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4498 ShuffleMask);
4499 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004500 case X86ISD::MOVDDUP:
4501 case X86ISD::MOVLHPD:
4502 case X86ISD::MOVLPD:
4503 case X86ISD::MOVLPS:
4504 case X86ISD::MOVSHDUP:
4505 case X86ISD::MOVSLDUP:
4506 case X86ISD::PALIGN:
4507 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004508 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004509 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004510 return SDValue();
4511 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004512
4513 Index = ShuffleMask[Index];
4514 if (Index < 0)
4515 return DAG.getUNDEF(VT.getVectorElementType());
4516
4517 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4518 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4519 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004520 }
4521
4522 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004523 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004524 V = V.getOperand(0);
4525 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004526 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004527
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004528 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004529 return SDValue();
4530 }
4531
4532 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4533 return (Index == 0) ? V.getOperand(0)
4534 : DAG.getUNDEF(VT.getVectorElementType());
4535
4536 if (V.getOpcode() == ISD::BUILD_VECTOR)
4537 return V.getOperand(Index);
4538
4539 return SDValue();
4540}
4541
4542/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4543/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004544/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004545static
4546unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4547 bool ZerosFromLeft, SelectionDAG &DAG) {
4548 int i = 0;
4549
4550 while (i < NumElems) {
4551 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004552 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004553 if (!(Elt.getNode() &&
4554 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4555 break;
4556 ++i;
4557 }
4558
4559 return i;
4560}
4561
4562/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4563/// MaskE correspond consecutively to elements from one of the vector operands,
4564/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4565static
4566bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4567 int OpIdx, int NumElems, unsigned &OpNum) {
4568 bool SeenV1 = false;
4569 bool SeenV2 = false;
4570
4571 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4572 int Idx = SVOp->getMaskElt(i);
4573 // Ignore undef indicies
4574 if (Idx < 0)
4575 continue;
4576
4577 if (Idx < NumElems)
4578 SeenV1 = true;
4579 else
4580 SeenV2 = true;
4581
4582 // Only accept consecutive elements from the same vector
4583 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4584 return false;
4585 }
4586
4587 OpNum = SeenV1 ? 0 : 1;
4588 return true;
4589}
4590
4591/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4592/// logical left shift of a vector.
4593static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4594 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4595 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4596 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4597 false /* check zeros from right */, DAG);
4598 unsigned OpSrc;
4599
4600 if (!NumZeros)
4601 return false;
4602
4603 // Considering the elements in the mask that are not consecutive zeros,
4604 // check if they consecutively come from only one of the source vectors.
4605 //
4606 // V1 = {X, A, B, C} 0
4607 // \ \ \ /
4608 // vector_shuffle V1, V2 <1, 2, 3, X>
4609 //
4610 if (!isShuffleMaskConsecutive(SVOp,
4611 0, // Mask Start Index
4612 NumElems-NumZeros-1, // Mask End Index
4613 NumZeros, // Where to start looking in the src vector
4614 NumElems, // Number of elements in vector
4615 OpSrc)) // Which source operand ?
4616 return false;
4617
4618 isLeft = false;
4619 ShAmt = NumZeros;
4620 ShVal = SVOp->getOperand(OpSrc);
4621 return true;
4622}
4623
4624/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4625/// logical left shift of a vector.
4626static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4627 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4628 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4629 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4630 true /* check zeros from left */, DAG);
4631 unsigned OpSrc;
4632
4633 if (!NumZeros)
4634 return false;
4635
4636 // Considering the elements in the mask that are not consecutive zeros,
4637 // check if they consecutively come from only one of the source vectors.
4638 //
4639 // 0 { A, B, X, X } = V2
4640 // / \ / /
4641 // vector_shuffle V1, V2 <X, X, 4, 5>
4642 //
4643 if (!isShuffleMaskConsecutive(SVOp,
4644 NumZeros, // Mask Start Index
4645 NumElems-1, // Mask End Index
4646 0, // Where to start looking in the src vector
4647 NumElems, // Number of elements in vector
4648 OpSrc)) // Which source operand ?
4649 return false;
4650
4651 isLeft = true;
4652 ShAmt = NumZeros;
4653 ShVal = SVOp->getOperand(OpSrc);
4654 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004655}
4656
4657/// isVectorShift - Returns true if the shuffle can be implemented as a
4658/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004659static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004660 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004661 // Although the logic below support any bitwidth size, there are no
4662 // shift instructions which handle more than 128-bit vectors.
4663 if (SVOp->getValueType(0).getSizeInBits() > 128)
4664 return false;
4665
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004666 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4667 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4668 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004669
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004670 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004671}
4672
Evan Chengc78d3b42006-04-24 18:01:45 +00004673/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4674///
Dan Gohman475871a2008-07-27 21:46:04 +00004675static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004676 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004677 SelectionDAG &DAG,
4678 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004679 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004680 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004681
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004682 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004683 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004684 bool First = true;
4685 for (unsigned i = 0; i < 16; ++i) {
4686 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4687 if (ThisIsNonZero && First) {
4688 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004689 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004690 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004691 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004692 First = false;
4693 }
4694
4695 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004696 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004697 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4698 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004699 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004700 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004701 }
4702 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004703 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4704 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4705 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004706 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004707 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004708 } else
4709 ThisElt = LastElt;
4710
Gabor Greifba36cb52008-08-28 21:40:38 +00004711 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004712 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004713 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004714 }
4715 }
4716
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004717 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004718}
4719
Bill Wendlinga348c562007-03-22 18:42:45 +00004720/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004721///
Dan Gohman475871a2008-07-27 21:46:04 +00004722static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004723 unsigned NumNonZero, unsigned NumZero,
4724 SelectionDAG &DAG,
4725 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004726 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004727 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004728
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004729 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004730 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004731 bool First = true;
4732 for (unsigned i = 0; i < 8; ++i) {
4733 bool isNonZero = (NonZeros & (1 << i)) != 0;
4734 if (isNonZero) {
4735 if (First) {
4736 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004737 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004738 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004739 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004740 First = false;
4741 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004742 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004743 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004744 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004745 }
4746 }
4747
4748 return V;
4749}
4750
Evan Chengf26ffe92008-05-29 08:22:04 +00004751/// getVShift - Return a vector logical shift node.
4752///
Owen Andersone50ed302009-08-10 22:56:29 +00004753static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004754 unsigned NumBits, SelectionDAG &DAG,
4755 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004756 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004757 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004758 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004759 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4760 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004761 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004762 DAG.getConstant(NumBits,
4763 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004764}
4765
Dan Gohman475871a2008-07-27 21:46:04 +00004766SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004767X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004768 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004769
Evan Chengc3630942009-12-09 21:00:30 +00004770 // Check if the scalar load can be widened into a vector load. And if
4771 // the address is "base + cst" see if the cst can be "absorbed" into
4772 // the shuffle mask.
4773 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4774 SDValue Ptr = LD->getBasePtr();
4775 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4776 return SDValue();
4777 EVT PVT = LD->getValueType(0);
4778 if (PVT != MVT::i32 && PVT != MVT::f32)
4779 return SDValue();
4780
4781 int FI = -1;
4782 int64_t Offset = 0;
4783 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4784 FI = FINode->getIndex();
4785 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004786 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004787 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4788 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4789 Offset = Ptr.getConstantOperandVal(1);
4790 Ptr = Ptr.getOperand(0);
4791 } else {
4792 return SDValue();
4793 }
4794
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004795 // FIXME: 256-bit vector instructions don't require a strict alignment,
4796 // improve this code to support it better.
4797 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004798 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004799 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004800 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004801 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004802 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004803 // Can't change the alignment. FIXME: It's possible to compute
4804 // the exact stack offset and reference FI + adjust offset instead.
4805 // If someone *really* cares about this. That's the way to implement it.
4806 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004807 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004808 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004809 }
4810 }
4811
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004812 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004813 // Ptr + (Offset & ~15).
4814 if (Offset < 0)
4815 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004816 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004817 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004818 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004819 if (StartOffset)
4820 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4821 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4822
4823 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004824 int NumElems = VT.getVectorNumElements();
4825
4826 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4827 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4828 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004829 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004830 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004831
4832 // Canonicalize it to a v4i32 or v8i32 shuffle.
4833 SmallVector<int, 8> Mask;
4834 for (int i = 0; i < NumElems; ++i)
4835 Mask.push_back(EltNo);
4836
4837 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4838 return DAG.getNode(ISD::BITCAST, dl, NVT,
4839 DAG.getVectorShuffle(CanonVT, dl, V1,
4840 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004841 }
4842
4843 return SDValue();
4844}
4845
Michael J. Spencerec38de22010-10-10 22:04:20 +00004846/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4847/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004848/// load which has the same value as a build_vector whose operands are 'elts'.
4849///
4850/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004851///
Nate Begeman1449f292010-03-24 22:19:06 +00004852/// FIXME: we'd also like to handle the case where the last elements are zero
4853/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4854/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004855static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004856 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004857 EVT EltVT = VT.getVectorElementType();
4858 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004859
Nate Begemanfdea31a2010-03-24 20:49:50 +00004860 LoadSDNode *LDBase = NULL;
4861 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004862
Nate Begeman1449f292010-03-24 22:19:06 +00004863 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004864 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004865 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004866 for (unsigned i = 0; i < NumElems; ++i) {
4867 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004868
Nate Begemanfdea31a2010-03-24 20:49:50 +00004869 if (!Elt.getNode() ||
4870 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4871 return SDValue();
4872 if (!LDBase) {
4873 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4874 return SDValue();
4875 LDBase = cast<LoadSDNode>(Elt.getNode());
4876 LastLoadedElt = i;
4877 continue;
4878 }
4879 if (Elt.getOpcode() == ISD::UNDEF)
4880 continue;
4881
4882 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4883 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4884 return SDValue();
4885 LastLoadedElt = i;
4886 }
Nate Begeman1449f292010-03-24 22:19:06 +00004887
4888 // If we have found an entire vector of loads and undefs, then return a large
4889 // load of the entire vector width starting at the base pointer. If we found
4890 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004891 if (LastLoadedElt == NumElems - 1) {
4892 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004893 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004894 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004895 LDBase->isVolatile(), LDBase->isNonTemporal(),
4896 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004897 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004898 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004899 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004900 LDBase->isInvariant(), LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00004901 } else if (NumElems == 4 && LastLoadedElt == 1 &&
4902 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004903 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4904 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00004905 SDValue ResNode =
4906 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
4907 LDBase->getPointerInfo(),
4908 LDBase->getAlignment(),
4909 false/*isVolatile*/, true/*ReadMem*/,
4910 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004911 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004912 }
4913 return SDValue();
4914}
4915
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004916/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
4917/// a vbroadcast node. We support two patterns:
4918/// 1. A splat BUILD_VECTOR which uses a single scalar load.
4919/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
4920/// a scalar load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004921/// The scalar load node is returned when a pattern is found,
4922/// or SDValue() otherwise.
4923static SDValue isVectorBroadcast(SDValue &Op, bool hasAVX2) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004924 EVT VT = Op.getValueType();
4925 SDValue V = Op;
4926
4927 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
4928 V = V.getOperand(0);
4929
4930 //A suspected load to be broadcasted.
4931 SDValue Ld;
4932
4933 switch (V.getOpcode()) {
4934 default:
4935 // Unknown pattern found.
4936 return SDValue();
4937
4938 case ISD::BUILD_VECTOR: {
4939 // The BUILD_VECTOR node must be a splat.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004940 if (!isSplatVector(V.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004941 return SDValue();
4942
4943 Ld = V.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004944
4945 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004946 // of its users are from the BUILD_VECTOR node.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004947 if (!Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004948 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004949 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004950 }
4951
4952 case ISD::VECTOR_SHUFFLE: {
4953 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
4954
4955 // Shuffles must have a splat mask where the first element is
4956 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004957 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004958 return SDValue();
4959
4960 SDValue Sc = Op.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004961 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004962 return SDValue();
4963
4964 Ld = Sc.getOperand(0);
4965
4966 // The scalar_to_vector node and the suspected
4967 // load node must have exactly one user.
4968 if (!Sc.hasOneUse() || !Ld.hasOneUse())
4969 return SDValue();
4970 break;
4971 }
4972 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004973
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004974 // The scalar source must be a normal load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004975 if (!ISD::isNormalLoad(Ld.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004976 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004977
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004978 bool Is256 = VT.getSizeInBits() == 256;
4979 bool Is128 = VT.getSizeInBits() == 128;
4980 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
4981
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004982 if (hasAVX2) {
4983 // VBroadcast to YMM
4984 if (Is256 && (ScalarSize == 8 || ScalarSize == 16 ||
4985 ScalarSize == 32 || ScalarSize == 64 ))
4986 return Ld;
4987
4988 // VBroadcast to XMM
4989 if (Is128 && (ScalarSize == 8 || ScalarSize == 32 ||
4990 ScalarSize == 16 || ScalarSize == 64 ))
4991 return Ld;
4992 }
4993
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004994 // VBroadcast to YMM
4995 if (Is256 && (ScalarSize == 32 || ScalarSize == 64))
4996 return Ld;
4997
4998 // VBroadcast to XMM
4999 if (Is128 && (ScalarSize == 32))
5000 return Ld;
5001
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005002
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005003 // Unsupported broadcast.
5004 return SDValue();
5005}
5006
Evan Chengc3630942009-12-09 21:00:30 +00005007SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005008X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005009 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005010
David Greenef125a292011-02-08 19:04:41 +00005011 EVT VT = Op.getValueType();
5012 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005013 unsigned NumElems = Op.getNumOperands();
5014
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005015 // Vectors containing all zeros can be matched by pxor and xorps later
5016 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5017 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5018 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005019 if (Op.getValueType() == MVT::v4i32 ||
5020 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005021 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005022
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005023 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005024 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005025
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005026 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005027 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5028 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005029 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper745a86b2011-11-19 22:34:59 +00005030 if (Op.getValueType() == MVT::v4i32 ||
5031 (Op.getValueType() == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005032 return Op;
5033
Craig Topper745a86b2011-11-19 22:34:59 +00005034 return getOnesVector(Op.getValueType(), Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005035 }
5036
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005037 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005038 if (Subtarget->hasAVX() && LD.getNode())
5039 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
5040
Owen Andersone50ed302009-08-10 22:56:29 +00005041 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005042
Evan Cheng0db9fe62006-04-25 20:13:52 +00005043 unsigned NumZero = 0;
5044 unsigned NumNonZero = 0;
5045 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005046 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005047 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005048 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005049 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005050 if (Elt.getOpcode() == ISD::UNDEF)
5051 continue;
5052 Values.insert(Elt);
5053 if (Elt.getOpcode() != ISD::Constant &&
5054 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005055 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005056 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005057 NumZero++;
5058 else {
5059 NonZeros |= (1 << i);
5060 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005061 }
5062 }
5063
Chris Lattner97a2a562010-08-26 05:24:29 +00005064 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5065 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005066 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005067
Chris Lattner67f453a2008-03-09 05:42:06 +00005068 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005069 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005070 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005071 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005072
Chris Lattner62098042008-03-09 01:05:04 +00005073 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5074 // the value are obviously zero, truncate the value to i32 and do the
5075 // insertion that way. Only do this if the value is non-constant or if the
5076 // value is a constant being inserted into element 0. It is cheaper to do
5077 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005078 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005079 (!IsAllConstants || Idx == 0)) {
5080 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005081 // Handle SSE only.
5082 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5083 EVT VecVT = MVT::v4i32;
5084 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005085
Chris Lattner62098042008-03-09 01:05:04 +00005086 // Truncate the value (which may itself be a constant) to i32, and
5087 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005088 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005089 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005090 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005091 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005092
Chris Lattner62098042008-03-09 01:05:04 +00005093 // Now we have our 32-bit value zero extended in the low element of
5094 // a vector. If Idx != 0, swizzle it into place.
5095 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005096 SmallVector<int, 4> Mask;
5097 Mask.push_back(Idx);
5098 for (unsigned i = 1; i != VecElts; ++i)
5099 Mask.push_back(i);
5100 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005101 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005102 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005103 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005104 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005105 }
5106 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005107
Chris Lattner19f79692008-03-08 22:59:52 +00005108 // If we have a constant or non-constant insertion into the low element of
5109 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5110 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005111 // depending on what the source datatype is.
5112 if (Idx == 0) {
5113 if (NumZero == 0) {
5114 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005115 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5116 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005117 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5118 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005119 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005120 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005121 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5122 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005123 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5124 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005125 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5126 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005127 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005128 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005129 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005130 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005131
5132 // Is it a vector logical left shift?
5133 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005134 X86::isZeroNode(Op.getOperand(0)) &&
5135 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005136 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005137 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005138 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005139 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005140 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005141 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005142
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005143 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005144 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005145
Chris Lattner19f79692008-03-08 22:59:52 +00005146 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5147 // is a non-constant being inserted into an element other than the low one,
5148 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5149 // movd/movss) to move this into the low element, then shuffle it into
5150 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005151 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005152 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005153
Evan Cheng0db9fe62006-04-25 20:13:52 +00005154 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005155 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005156 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005157 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005158 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005159 MaskVec.push_back(i == Idx ? 0 : 1);
5160 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005161 }
5162 }
5163
Chris Lattner67f453a2008-03-09 05:42:06 +00005164 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005165 if (Values.size() == 1) {
5166 if (EVTBits == 32) {
5167 // Instead of a shuffle like this:
5168 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5169 // Check if it's possible to issue this instead.
5170 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5171 unsigned Idx = CountTrailingZeros_32(NonZeros);
5172 SDValue Item = Op.getOperand(Idx);
5173 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5174 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5175 }
Dan Gohman475871a2008-07-27 21:46:04 +00005176 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005177 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005178
Dan Gohmana3941172007-07-24 22:55:08 +00005179 // A vector full of immediates; various special cases are already
5180 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005181 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005182 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005183
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005184 // For AVX-length vectors, build the individual 128-bit pieces and use
5185 // shuffles to put them in place.
5186 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5187 SmallVector<SDValue, 32> V;
5188 for (unsigned i = 0; i < NumElems; ++i)
5189 V.push_back(Op.getOperand(i));
5190
5191 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5192
5193 // Build both the lower and upper subvector.
5194 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5195 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5196 NumElems/2);
5197
5198 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005199 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5200 DAG.getConstant(0, MVT::i32), DAG, dl);
5201 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005202 DAG, dl);
5203 }
5204
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005205 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005206 if (EVTBits == 64) {
5207 if (NumNonZero == 1) {
5208 // One half is zero or undef.
5209 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005210 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005211 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005212 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005213 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005214 }
Dan Gohman475871a2008-07-27 21:46:04 +00005215 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005216 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005217
5218 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005219 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005220 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005221 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005222 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005223 }
5224
Bill Wendling826f36f2007-03-28 00:57:11 +00005225 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005226 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005227 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005228 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005229 }
5230
5231 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005232 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005233 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005234 if (NumElems == 4 && NumZero > 0) {
5235 for (unsigned i = 0; i < 4; ++i) {
5236 bool isZero = !(NonZeros & (1 << i));
5237 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005238 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005239 else
Dale Johannesenace16102009-02-03 19:33:06 +00005240 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005241 }
5242
5243 for (unsigned i = 0; i < 2; ++i) {
5244 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5245 default: break;
5246 case 0:
5247 V[i] = V[i*2]; // Must be a zero vector.
5248 break;
5249 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005250 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005251 break;
5252 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005253 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005254 break;
5255 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005256 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005257 break;
5258 }
5259 }
5260
Nate Begeman9008ca62009-04-27 18:41:29 +00005261 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005262 bool Reverse = (NonZeros & 0x3) == 2;
5263 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005264 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005265 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5266 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005267 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5268 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005269 }
5270
Nate Begemanfdea31a2010-03-24 20:49:50 +00005271 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5272 // Check for a build vector of consecutive loads.
5273 for (unsigned i = 0; i < NumElems; ++i)
5274 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005275
Nate Begemanfdea31a2010-03-24 20:49:50 +00005276 // Check for elements which are consecutive loads.
5277 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5278 if (LD.getNode())
5279 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005280
5281 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperc0d82852011-11-22 00:44:41 +00005282 if (getSubtarget()->hasSSE41orAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005283 SDValue Result;
5284 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5285 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5286 else
5287 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005288
Chris Lattner24faf612010-08-28 17:59:08 +00005289 for (unsigned i = 1; i < NumElems; ++i) {
5290 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5291 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005292 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005293 }
5294 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005295 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005296
Chris Lattner6e80e442010-08-28 17:15:43 +00005297 // Otherwise, expand into a number of unpckl*, start by extending each of
5298 // our (non-undef) elements to the full vector width with the element in the
5299 // bottom slot of the vector (which generates no code for SSE).
5300 for (unsigned i = 0; i < NumElems; ++i) {
5301 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5302 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5303 else
5304 V[i] = DAG.getUNDEF(VT);
5305 }
5306
5307 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005308 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5309 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5310 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005311 unsigned EltStride = NumElems >> 1;
5312 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005313 for (unsigned i = 0; i < EltStride; ++i) {
5314 // If V[i+EltStride] is undef and this is the first round of mixing,
5315 // then it is safe to just drop this shuffle: V[i] is already in the
5316 // right place, the one element (since it's the first round) being
5317 // inserted as undef can be dropped. This isn't safe for successive
5318 // rounds because they will permute elements within both vectors.
5319 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5320 EltStride == NumElems/2)
5321 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005322
Chris Lattner6e80e442010-08-28 17:15:43 +00005323 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005324 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005325 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005326 }
5327 return V[0];
5328 }
Dan Gohman475871a2008-07-27 21:46:04 +00005329 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005330}
5331
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005332// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5333// them in a MMX register. This is better than doing a stack convert.
5334static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005335 DebugLoc dl = Op.getDebugLoc();
5336 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005337
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005338 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5339 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5340 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005341 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005342 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5343 InVec = Op.getOperand(1);
5344 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5345 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005346 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005347 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5348 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5349 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005350 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005351 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5352 Mask[0] = 0; Mask[1] = 2;
5353 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5354 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005355 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005356}
5357
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005358// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5359// to create 256-bit vectors from two other 128-bit ones.
5360static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5361 DebugLoc dl = Op.getDebugLoc();
5362 EVT ResVT = Op.getValueType();
5363
5364 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5365
5366 SDValue V1 = Op.getOperand(0);
5367 SDValue V2 = Op.getOperand(1);
5368 unsigned NumElems = ResVT.getVectorNumElements();
5369
5370 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5371 DAG.getConstant(0, MVT::i32), DAG, dl);
5372 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5373 DAG, dl);
5374}
5375
5376SDValue
5377X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005378 EVT ResVT = Op.getValueType();
5379
5380 assert(Op.getNumOperands() == 2);
5381 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5382 "Unsupported CONCAT_VECTORS for value type");
5383
5384 // We support concatenate two MMX registers and place them in a MMX register.
5385 // This is better than doing a stack convert.
5386 if (ResVT.is128BitVector())
5387 return LowerMMXCONCAT_VECTORS(Op, DAG);
5388
5389 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5390 // from two other 128-bit ones.
5391 return LowerAVXCONCAT_VECTORS(Op, DAG);
5392}
5393
Nate Begemanb9a47b82009-02-23 08:49:38 +00005394// v8i16 shuffles - Prefer shuffles in the following order:
5395// 1. [all] pshuflw, pshufhw, optional move
5396// 2. [ssse3] 1 x pshufb
5397// 3. [ssse3] 2 x pshufb + 1 x por
5398// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005399SDValue
5400X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5401 SelectionDAG &DAG) const {
5402 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005403 SDValue V1 = SVOp->getOperand(0);
5404 SDValue V2 = SVOp->getOperand(1);
5405 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005406 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005407
Nate Begemanb9a47b82009-02-23 08:49:38 +00005408 // Determine if more than 1 of the words in each of the low and high quadwords
5409 // of the result come from the same quadword of one of the two inputs. Undef
5410 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005411 unsigned LoQuad[] = { 0, 0, 0, 0 };
5412 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005413 BitVector InputQuads(4);
5414 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005415 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005416 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005417 MaskVals.push_back(EltIdx);
5418 if (EltIdx < 0) {
5419 ++Quad[0];
5420 ++Quad[1];
5421 ++Quad[2];
5422 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005423 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005424 }
5425 ++Quad[EltIdx / 4];
5426 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005427 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005428
Nate Begemanb9a47b82009-02-23 08:49:38 +00005429 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005430 unsigned MaxQuad = 1;
5431 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005432 if (LoQuad[i] > MaxQuad) {
5433 BestLoQuad = i;
5434 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005435 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005436 }
5437
Nate Begemanb9a47b82009-02-23 08:49:38 +00005438 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005439 MaxQuad = 1;
5440 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005441 if (HiQuad[i] > MaxQuad) {
5442 BestHiQuad = i;
5443 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005444 }
5445 }
5446
Nate Begemanb9a47b82009-02-23 08:49:38 +00005447 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005448 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005449 // single pshufb instruction is necessary. If There are more than 2 input
5450 // quads, disable the next transformation since it does not help SSSE3.
5451 bool V1Used = InputQuads[0] || InputQuads[1];
5452 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperc0d82852011-11-22 00:44:41 +00005453 if (Subtarget->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005454 if (InputQuads.count() == 2 && V1Used && V2Used) {
5455 BestLoQuad = InputQuads.find_first();
5456 BestHiQuad = InputQuads.find_next(BestLoQuad);
5457 }
5458 if (InputQuads.count() > 2) {
5459 BestLoQuad = -1;
5460 BestHiQuad = -1;
5461 }
5462 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005463
Nate Begemanb9a47b82009-02-23 08:49:38 +00005464 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5465 // the shuffle mask. If a quad is scored as -1, that means that it contains
5466 // words from all 4 input quadwords.
5467 SDValue NewV;
5468 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005469 SmallVector<int, 8> MaskV;
5470 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5471 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005472 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005473 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5474 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5475 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005476
Nate Begemanb9a47b82009-02-23 08:49:38 +00005477 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5478 // source words for the shuffle, to aid later transformations.
5479 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005480 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005481 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005482 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005483 if (idx != (int)i)
5484 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005485 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005486 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005487 AllWordsInNewV = false;
5488 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005489 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005490
Nate Begemanb9a47b82009-02-23 08:49:38 +00005491 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5492 if (AllWordsInNewV) {
5493 for (int i = 0; i != 8; ++i) {
5494 int idx = MaskVals[i];
5495 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005496 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005497 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005498 if ((idx != i) && idx < 4)
5499 pshufhw = false;
5500 if ((idx != i) && idx > 3)
5501 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005502 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005503 V1 = NewV;
5504 V2Used = false;
5505 BestLoQuad = 0;
5506 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005507 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005508
Nate Begemanb9a47b82009-02-23 08:49:38 +00005509 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5510 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005511 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005512 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5513 unsigned TargetMask = 0;
5514 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005515 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005516 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5517 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5518 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005519 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005520 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005521 }
Eric Christopherfd179292009-08-27 18:07:15 +00005522
Nate Begemanb9a47b82009-02-23 08:49:38 +00005523 // If we have SSSE3, and all words of the result are from 1 input vector,
5524 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5525 // is present, fall back to case 4.
Craig Topperc0d82852011-11-22 00:44:41 +00005526 if (Subtarget->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005527 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005528
Nate Begemanb9a47b82009-02-23 08:49:38 +00005529 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005530 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005531 // mask, and elements that come from V1 in the V2 mask, so that the two
5532 // results can be OR'd together.
5533 bool TwoInputs = V1Used && V2Used;
5534 for (unsigned i = 0; i != 8; ++i) {
5535 int EltIdx = MaskVals[i] * 2;
5536 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005537 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5538 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005539 continue;
5540 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005541 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5542 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005543 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005544 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005545 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005546 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005547 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005548 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005549 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005550
Nate Begemanb9a47b82009-02-23 08:49:38 +00005551 // Calculate the shuffle mask for the second input, shuffle it, and
5552 // OR it with the first shuffled input.
5553 pshufbMask.clear();
5554 for (unsigned i = 0; i != 8; ++i) {
5555 int EltIdx = MaskVals[i] * 2;
5556 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005557 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5558 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005559 continue;
5560 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005561 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5562 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005563 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005564 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005565 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005566 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005567 MVT::v16i8, &pshufbMask[0], 16));
5568 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005569 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005570 }
5571
5572 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5573 // and update MaskVals with new element order.
5574 BitVector InOrder(8);
5575 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005576 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005577 for (int i = 0; i != 4; ++i) {
5578 int idx = MaskVals[i];
5579 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005580 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005581 InOrder.set(i);
5582 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005583 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005584 InOrder.set(i);
5585 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005586 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005587 }
5588 }
5589 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005590 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005591 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005592 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005593
Craig Topperc0d82852011-11-22 00:44:41 +00005594 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3orAVX())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005595 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5596 NewV.getOperand(0),
5597 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5598 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005599 }
Eric Christopherfd179292009-08-27 18:07:15 +00005600
Nate Begemanb9a47b82009-02-23 08:49:38 +00005601 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5602 // and update MaskVals with the new element order.
5603 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005604 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005605 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005606 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005607 for (unsigned i = 4; i != 8; ++i) {
5608 int idx = MaskVals[i];
5609 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005610 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005611 InOrder.set(i);
5612 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005613 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005614 InOrder.set(i);
5615 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005616 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005617 }
5618 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005619 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005620 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005621
Craig Topperc0d82852011-11-22 00:44:41 +00005622 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3orAVX())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005623 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5624 NewV.getOperand(0),
5625 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5626 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005627 }
Eric Christopherfd179292009-08-27 18:07:15 +00005628
Nate Begemanb9a47b82009-02-23 08:49:38 +00005629 // In case BestHi & BestLo were both -1, which means each quadword has a word
5630 // from each of the four input quadwords, calculate the InOrder bitvector now
5631 // before falling through to the insert/extract cleanup.
5632 if (BestLoQuad == -1 && BestHiQuad == -1) {
5633 NewV = V1;
5634 for (int i = 0; i != 8; ++i)
5635 if (MaskVals[i] < 0 || MaskVals[i] == i)
5636 InOrder.set(i);
5637 }
Eric Christopherfd179292009-08-27 18:07:15 +00005638
Nate Begemanb9a47b82009-02-23 08:49:38 +00005639 // The other elements are put in the right place using pextrw and pinsrw.
5640 for (unsigned i = 0; i != 8; ++i) {
5641 if (InOrder[i])
5642 continue;
5643 int EltIdx = MaskVals[i];
5644 if (EltIdx < 0)
5645 continue;
5646 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005647 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005648 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005649 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005650 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005651 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005652 DAG.getIntPtrConstant(i));
5653 }
5654 return NewV;
5655}
5656
5657// v16i8 shuffles - Prefer shuffles in the following order:
5658// 1. [ssse3] 1 x pshufb
5659// 2. [ssse3] 2 x pshufb + 1 x por
5660// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5661static
Nate Begeman9008ca62009-04-27 18:41:29 +00005662SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005663 SelectionDAG &DAG,
5664 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005665 SDValue V1 = SVOp->getOperand(0);
5666 SDValue V2 = SVOp->getOperand(1);
5667 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005668 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005669 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005670
Nate Begemanb9a47b82009-02-23 08:49:38 +00005671 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005672 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005673 // present, fall back to case 3.
5674 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5675 bool V1Only = true;
5676 bool V2Only = true;
5677 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005678 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005679 if (EltIdx < 0)
5680 continue;
5681 if (EltIdx < 16)
5682 V2Only = false;
5683 else
5684 V1Only = false;
5685 }
Eric Christopherfd179292009-08-27 18:07:15 +00005686
Nate Begemanb9a47b82009-02-23 08:49:38 +00005687 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperc0d82852011-11-22 00:44:41 +00005688 if (TLI.getSubtarget()->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005689 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005690
Nate Begemanb9a47b82009-02-23 08:49:38 +00005691 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005692 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005693 //
5694 // Otherwise, we have elements from both input vectors, and must zero out
5695 // elements that come from V2 in the first mask, and V1 in the second mask
5696 // so that we can OR them together.
5697 bool TwoInputs = !(V1Only || V2Only);
5698 for (unsigned i = 0; i != 16; ++i) {
5699 int EltIdx = MaskVals[i];
5700 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005701 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005702 continue;
5703 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005704 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005705 }
5706 // If all the elements are from V2, assign it to V1 and return after
5707 // building the first pshufb.
5708 if (V2Only)
5709 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005710 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005711 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005712 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005713 if (!TwoInputs)
5714 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005715
Nate Begemanb9a47b82009-02-23 08:49:38 +00005716 // Calculate the shuffle mask for the second input, shuffle it, and
5717 // OR it with the first shuffled input.
5718 pshufbMask.clear();
5719 for (unsigned i = 0; i != 16; ++i) {
5720 int EltIdx = MaskVals[i];
5721 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005722 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005723 continue;
5724 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005725 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005726 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005727 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005728 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005729 MVT::v16i8, &pshufbMask[0], 16));
5730 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005731 }
Eric Christopherfd179292009-08-27 18:07:15 +00005732
Nate Begemanb9a47b82009-02-23 08:49:38 +00005733 // No SSSE3 - Calculate in place words and then fix all out of place words
5734 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5735 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005736 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5737 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005738 SDValue NewV = V2Only ? V2 : V1;
5739 for (int i = 0; i != 8; ++i) {
5740 int Elt0 = MaskVals[i*2];
5741 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005742
Nate Begemanb9a47b82009-02-23 08:49:38 +00005743 // This word of the result is all undef, skip it.
5744 if (Elt0 < 0 && Elt1 < 0)
5745 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005746
Nate Begemanb9a47b82009-02-23 08:49:38 +00005747 // This word of the result is already in the correct place, skip it.
5748 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5749 continue;
5750 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5751 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005752
Nate Begemanb9a47b82009-02-23 08:49:38 +00005753 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5754 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5755 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005756
5757 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5758 // using a single extract together, load it and store it.
5759 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005760 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005761 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005762 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005763 DAG.getIntPtrConstant(i));
5764 continue;
5765 }
5766
Nate Begemanb9a47b82009-02-23 08:49:38 +00005767 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005768 // source byte is not also odd, shift the extracted word left 8 bits
5769 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005770 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005771 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005772 DAG.getIntPtrConstant(Elt1 / 2));
5773 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005774 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005775 DAG.getConstant(8,
5776 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005777 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005778 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5779 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005780 }
5781 // If Elt0 is defined, extract it from the appropriate source. If the
5782 // source byte is not also even, shift the extracted word right 8 bits. If
5783 // Elt1 was also defined, OR the extracted values together before
5784 // inserting them in the result.
5785 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005786 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005787 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5788 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005789 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005790 DAG.getConstant(8,
5791 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005792 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005793 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5794 DAG.getConstant(0x00FF, MVT::i16));
5795 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005796 : InsElt0;
5797 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005798 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005799 DAG.getIntPtrConstant(i));
5800 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005801 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005802}
5803
Evan Cheng7a831ce2007-12-15 03:00:47 +00005804/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005805/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005806/// done when every pair / quad of shuffle mask elements point to elements in
5807/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005808/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005809static
Nate Begeman9008ca62009-04-27 18:41:29 +00005810SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005811 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005812 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005813 SDValue V1 = SVOp->getOperand(0);
5814 SDValue V2 = SVOp->getOperand(1);
5815 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005816 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005817 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005818 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005819 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005820 case MVT::v4f32: NewVT = MVT::v2f64; break;
5821 case MVT::v4i32: NewVT = MVT::v2i64; break;
5822 case MVT::v8i16: NewVT = MVT::v4i32; break;
5823 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005824 }
5825
Nate Begeman9008ca62009-04-27 18:41:29 +00005826 int Scale = NumElems / NewWidth;
5827 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005828 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005829 int StartIdx = -1;
5830 for (int j = 0; j < Scale; ++j) {
5831 int EltIdx = SVOp->getMaskElt(i+j);
5832 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005833 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005834 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005835 StartIdx = EltIdx - (EltIdx % Scale);
5836 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005837 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005838 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005839 if (StartIdx == -1)
5840 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005841 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005842 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005843 }
5844
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005845 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5846 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005847 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005848}
5849
Evan Chengd880b972008-05-09 21:53:03 +00005850/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005851///
Owen Andersone50ed302009-08-10 22:56:29 +00005852static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005853 SDValue SrcOp, SelectionDAG &DAG,
5854 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005855 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005856 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005857 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005858 LD = dyn_cast<LoadSDNode>(SrcOp);
5859 if (!LD) {
5860 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5861 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005862 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005863 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005864 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005865 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005866 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005867 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005868 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005869 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005870 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5871 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5872 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005873 SrcOp.getOperand(0)
5874 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005875 }
5876 }
5877 }
5878
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005879 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005880 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005881 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005882 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005883}
5884
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005885/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5886/// shuffle node referes to only one lane in the sources.
5887static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5888 EVT VT = SVOp->getValueType(0);
5889 int NumElems = VT.getVectorNumElements();
5890 int HalfSize = NumElems/2;
5891 SmallVector<int, 16> M;
5892 SVOp->getMask(M);
5893 bool MatchA = false, MatchB = false;
5894
5895 for (int l = 0; l < NumElems*2; l += HalfSize) {
5896 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5897 MatchA = true;
5898 break;
5899 }
5900 }
5901
5902 for (int l = 0; l < NumElems*2; l += HalfSize) {
5903 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5904 MatchB = true;
5905 break;
5906 }
5907 }
5908
5909 return MatchA && MatchB;
5910}
5911
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005912/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5913/// which could not be matched by any known target speficic shuffle
5914static SDValue
5915LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005916 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5917 // If each half of a vector shuffle node referes to only one lane in the
5918 // source vectors, extract each used 128-bit lane and shuffle them using
5919 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5920 // the work to the legalizer.
5921 DebugLoc dl = SVOp->getDebugLoc();
5922 EVT VT = SVOp->getValueType(0);
5923 int NumElems = VT.getVectorNumElements();
5924 int HalfSize = NumElems/2;
5925
5926 // Extract the reference for each half
5927 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5928 int FstVecOpNum = 0, SndVecOpNum = 0;
5929 for (int i = 0; i < HalfSize; ++i) {
5930 int Elt = SVOp->getMaskElt(i);
5931 if (SVOp->getMaskElt(i) < 0)
5932 continue;
5933 FstVecOpNum = Elt/NumElems;
5934 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5935 break;
5936 }
5937 for (int i = HalfSize; i < NumElems; ++i) {
5938 int Elt = SVOp->getMaskElt(i);
5939 if (SVOp->getMaskElt(i) < 0)
5940 continue;
5941 SndVecOpNum = Elt/NumElems;
5942 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5943 break;
5944 }
5945
5946 // Extract the subvectors
5947 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5948 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5949 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5950 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5951
5952 // Generate 128-bit shuffles
5953 SmallVector<int, 16> MaskV1, MaskV2;
5954 for (int i = 0; i < HalfSize; ++i) {
5955 int Elt = SVOp->getMaskElt(i);
5956 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5957 }
5958 for (int i = HalfSize; i < NumElems; ++i) {
5959 int Elt = SVOp->getMaskElt(i);
5960 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5961 }
5962
5963 EVT NVT = V1.getValueType();
5964 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5965 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5966
5967 // Concatenate the result back
5968 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
5969 DAG.getConstant(0, MVT::i32), DAG, dl);
5970 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5971 DAG, dl);
5972 }
5973
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005974 return SDValue();
5975}
5976
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005977/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
5978/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00005979static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005980LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005981 SDValue V1 = SVOp->getOperand(0);
5982 SDValue V2 = SVOp->getOperand(1);
5983 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005984 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005985
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005986 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
5987
Evan Chengace3c172008-07-22 21:13:36 +00005988 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005989 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005990 SmallVector<int, 8> Mask1(4U, -1);
5991 SmallVector<int, 8> PermMask;
5992 SVOp->getMask(PermMask);
5993
Evan Chengace3c172008-07-22 21:13:36 +00005994 unsigned NumHi = 0;
5995 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005996 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005997 int Idx = PermMask[i];
5998 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005999 Locs[i] = std::make_pair(-1, -1);
6000 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006001 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6002 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006003 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006004 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006005 NumLo++;
6006 } else {
6007 Locs[i] = std::make_pair(1, NumHi);
6008 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006009 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006010 NumHi++;
6011 }
6012 }
6013 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006014
Evan Chengace3c172008-07-22 21:13:36 +00006015 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006016 // If no more than two elements come from either vector. This can be
6017 // implemented with two shuffles. First shuffle gather the elements.
6018 // The second shuffle, which takes the first shuffle as both of its
6019 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006020 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006021
Nate Begeman9008ca62009-04-27 18:41:29 +00006022 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006023
Evan Chengace3c172008-07-22 21:13:36 +00006024 for (unsigned i = 0; i != 4; ++i) {
6025 if (Locs[i].first == -1)
6026 continue;
6027 else {
6028 unsigned Idx = (i < 2) ? 0 : 4;
6029 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006030 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006031 }
6032 }
6033
Nate Begeman9008ca62009-04-27 18:41:29 +00006034 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006035 } else if (NumLo == 3 || NumHi == 3) {
6036 // Otherwise, we must have three elements from one vector, call it X, and
6037 // one element from the other, call it Y. First, use a shufps to build an
6038 // intermediate vector with the one element from Y and the element from X
6039 // that will be in the same half in the final destination (the indexes don't
6040 // matter). Then, use a shufps to build the final vector, taking the half
6041 // containing the element from Y from the intermediate, and the other half
6042 // from X.
6043 if (NumHi == 3) {
6044 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006045 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006046 std::swap(V1, V2);
6047 }
6048
6049 // Find the element from V2.
6050 unsigned HiIndex;
6051 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006052 int Val = PermMask[HiIndex];
6053 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006054 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006055 if (Val >= 4)
6056 break;
6057 }
6058
Nate Begeman9008ca62009-04-27 18:41:29 +00006059 Mask1[0] = PermMask[HiIndex];
6060 Mask1[1] = -1;
6061 Mask1[2] = PermMask[HiIndex^1];
6062 Mask1[3] = -1;
6063 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006064
6065 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006066 Mask1[0] = PermMask[0];
6067 Mask1[1] = PermMask[1];
6068 Mask1[2] = HiIndex & 1 ? 6 : 4;
6069 Mask1[3] = HiIndex & 1 ? 4 : 6;
6070 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006071 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006072 Mask1[0] = HiIndex & 1 ? 2 : 0;
6073 Mask1[1] = HiIndex & 1 ? 0 : 2;
6074 Mask1[2] = PermMask[2];
6075 Mask1[3] = PermMask[3];
6076 if (Mask1[2] >= 0)
6077 Mask1[2] += 4;
6078 if (Mask1[3] >= 0)
6079 Mask1[3] += 4;
6080 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006081 }
Evan Chengace3c172008-07-22 21:13:36 +00006082 }
6083
6084 // Break it into (shuffle shuffle_hi, shuffle_lo).
6085 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006086 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006087 SmallVector<int,8> LoMask(4U, -1);
6088 SmallVector<int,8> HiMask(4U, -1);
6089
6090 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006091 unsigned MaskIdx = 0;
6092 unsigned LoIdx = 0;
6093 unsigned HiIdx = 2;
6094 for (unsigned i = 0; i != 4; ++i) {
6095 if (i == 2) {
6096 MaskPtr = &HiMask;
6097 MaskIdx = 1;
6098 LoIdx = 0;
6099 HiIdx = 2;
6100 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006101 int Idx = PermMask[i];
6102 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006103 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006104 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006105 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006106 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006107 LoIdx++;
6108 } else {
6109 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006110 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006111 HiIdx++;
6112 }
6113 }
6114
Nate Begeman9008ca62009-04-27 18:41:29 +00006115 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6116 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6117 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006118 for (unsigned i = 0; i != 4; ++i) {
6119 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006120 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006121 } else {
6122 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006123 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006124 }
6125 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006126 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006127}
6128
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006129static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006130 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006131 V = V.getOperand(0);
6132 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6133 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006134 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6135 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6136 // BUILD_VECTOR (load), undef
6137 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006138 if (MayFoldLoad(V))
6139 return true;
6140 return false;
6141}
6142
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006143// FIXME: the version above should always be used. Since there's
6144// a bug where several vector shuffles can't be folded because the
6145// DAG is not updated during lowering and a node claims to have two
6146// uses while it only has one, use this version, and let isel match
6147// another instruction if the load really happens to have more than
6148// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006149// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006150static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006151 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006152 V = V.getOperand(0);
6153 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6154 V = V.getOperand(0);
6155 if (ISD::isNormalLoad(V.getNode()))
6156 return true;
6157 return false;
6158}
6159
6160/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6161/// a vector extract, and if both can be later optimized into a single load.
6162/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6163/// here because otherwise a target specific shuffle node is going to be
6164/// emitted for this shuffle, and the optimization not done.
6165/// FIXME: This is probably not the best approach, but fix the problem
6166/// until the right path is decided.
6167static
6168bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6169 const TargetLowering &TLI) {
6170 EVT VT = V.getValueType();
6171 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6172
6173 // Be sure that the vector shuffle is present in a pattern like this:
6174 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6175 if (!V.hasOneUse())
6176 return false;
6177
6178 SDNode *N = *V.getNode()->use_begin();
6179 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6180 return false;
6181
6182 SDValue EltNo = N->getOperand(1);
6183 if (!isa<ConstantSDNode>(EltNo))
6184 return false;
6185
6186 // If the bit convert changed the number of elements, it is unsafe
6187 // to examine the mask.
6188 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006189 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006190 EVT SrcVT = V.getOperand(0).getValueType();
6191 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6192 return false;
6193 V = V.getOperand(0);
6194 HasShuffleIntoBitcast = true;
6195 }
6196
6197 // Select the input vector, guarding against out of range extract vector.
6198 unsigned NumElems = VT.getVectorNumElements();
6199 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6200 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6201 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6202
6203 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006204 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006205 V = V.getOperand(0);
6206
6207 if (ISD::isNormalLoad(V.getNode())) {
6208 // Is the original load suitable?
6209 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6210
6211 // FIXME: avoid the multi-use bug that is preventing lots of
6212 // of foldings to be detected, this is still wrong of course, but
6213 // give the temporary desired behavior, and if it happens that
6214 // the load has real more uses, during isel it will not fold, and
6215 // will generate poor code.
6216 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6217 return false;
6218
6219 if (!HasShuffleIntoBitcast)
6220 return true;
6221
6222 // If there's a bitcast before the shuffle, check if the load type and
6223 // alignment is valid.
6224 unsigned Align = LN0->getAlignment();
6225 unsigned NewAlign =
6226 TLI.getTargetData()->getABITypeAlignment(
6227 VT.getTypeForEVT(*DAG.getContext()));
6228
6229 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6230 return false;
6231 }
6232
6233 return true;
6234}
6235
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006236static
Evan Cheng835580f2010-10-07 20:50:20 +00006237SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6238 EVT VT = Op.getValueType();
6239
6240 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006241 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6242 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006243 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6244 V1, DAG));
6245}
6246
6247static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006248SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006249 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006250 SDValue V1 = Op.getOperand(0);
6251 SDValue V2 = Op.getOperand(1);
6252 EVT VT = Op.getValueType();
6253
6254 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6255
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006256 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006257 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6258
Evan Cheng0899f5c2011-08-31 02:05:24 +00006259 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6260 return DAG.getNode(ISD::BITCAST, dl, VT,
6261 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6262 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6263 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006264}
6265
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006266static
6267SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6268 SDValue V1 = Op.getOperand(0);
6269 SDValue V2 = Op.getOperand(1);
6270 EVT VT = Op.getValueType();
6271
6272 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6273 "unsupported shuffle type");
6274
6275 if (V2.getOpcode() == ISD::UNDEF)
6276 V2 = V1;
6277
6278 // v4i32 or v4f32
6279 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6280}
6281
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006282static inline unsigned getSHUFPOpcode(EVT VT) {
6283 switch(VT.getSimpleVT().SimpleTy) {
6284 case MVT::v8i32: // Use fp unit for int unpack.
6285 case MVT::v8f32:
6286 case MVT::v4i32: // Use fp unit for int unpack.
6287 case MVT::v4f32: return X86ISD::SHUFPS;
6288 case MVT::v4i64: // Use fp unit for int unpack.
6289 case MVT::v4f64:
6290 case MVT::v2i64: // Use fp unit for int unpack.
6291 case MVT::v2f64: return X86ISD::SHUFPD;
6292 default:
6293 llvm_unreachable("Unknown type for shufp*");
6294 }
6295 return 0;
6296}
6297
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006298static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006299SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006300 SDValue V1 = Op.getOperand(0);
6301 SDValue V2 = Op.getOperand(1);
6302 EVT VT = Op.getValueType();
6303 unsigned NumElems = VT.getVectorNumElements();
6304
6305 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6306 // operand of these instructions is only memory, so check if there's a
6307 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6308 // same masks.
6309 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006310
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006311 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006312 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006313 CanFoldLoad = true;
6314
6315 // When V1 is a load, it can be folded later into a store in isel, example:
6316 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6317 // turns into:
6318 // (MOVLPSmr addr:$src1, VR128:$src2)
6319 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006320 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006321 CanFoldLoad = true;
6322
Dan Gohman65fd6562011-11-03 21:49:52 +00006323 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006324 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006325 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006326 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6327
6328 if (NumElems == 4)
Dan Gohman65fd6562011-11-03 21:49:52 +00006329 // If we don't care about the second element, procede to use movss.
6330 if (SVOp->getMaskElt(1) != -1)
6331 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006332 }
6333
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006334 // movl and movlp will both match v2i64, but v2i64 is never matched by
6335 // movl earlier because we make it strict to avoid messing with the movlp load
6336 // folding logic (see the code above getMOVLP call). Match it here then,
6337 // this is horrible, but will stay like this until we move all shuffle
6338 // matching to x86 specific nodes. Note that for the 1st condition all
6339 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006340 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006341 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6342 // as to remove this logic from here, as much as possible
6343 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006344 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006345 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006346 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006347
6348 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6349
6350 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006351 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006352 X86::getShuffleSHUFImmediate(SVOp), DAG);
6353}
6354
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006355static
6356SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006357 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006358 const X86Subtarget *Subtarget) {
6359 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6360 EVT VT = Op.getValueType();
6361 DebugLoc dl = Op.getDebugLoc();
6362 SDValue V1 = Op.getOperand(0);
6363 SDValue V2 = Op.getOperand(1);
6364
6365 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006366 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006367
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006368 // Handle splat operations
6369 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006370 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006371 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006372 // Special case, this is the only place now where it's allowed to return
6373 // a vector_shuffle operation without using a target specific node, because
6374 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6375 // this be moved to DAGCombine instead?
6376 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006377 return Op;
6378
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006379 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00006380 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00006381 if (Subtarget->hasAVX() && LD.getNode())
6382 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006383
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006384 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006385 if ((Size == 128 && NumElem <= 4) ||
6386 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006387 return SDValue();
6388
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006389 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006390 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006391 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006392
6393 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6394 // do it!
6395 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6396 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6397 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006398 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006399 } else if ((VT == MVT::v4i32 ||
6400 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006401 // FIXME: Figure out a cleaner way to do this.
6402 // Try to make use of movq to zero out the top part.
6403 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6404 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6405 if (NewOp.getNode()) {
6406 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6407 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6408 DAG, Subtarget, dl);
6409 }
6410 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6411 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6412 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6413 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6414 DAG, Subtarget, dl);
6415 }
6416 }
6417 return SDValue();
6418}
6419
Dan Gohman475871a2008-07-27 21:46:04 +00006420SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006421X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006422 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006423 SDValue V1 = Op.getOperand(0);
6424 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006425 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006426 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006427 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006428 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006429 bool V1IsSplat = false;
6430 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006431 bool HasXMMInt = Subtarget->hasXMMInt();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006432 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006433 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006434 MachineFunction &MF = DAG.getMachineFunction();
6435 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006436
Craig Topper3426a3e2011-11-14 06:46:21 +00006437 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006438
Craig Topper38034c52011-11-26 22:55:48 +00006439 assert(V1.getOpcode() != ISD::UNDEF && "Op 1 of shuffle should not be undef");
6440
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006441 // Vector shuffle lowering takes 3 steps:
6442 //
6443 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6444 // narrowing and commutation of operands should be handled.
6445 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6446 // shuffle nodes.
6447 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6448 // so the shuffle can be broken into other shuffles and the legalizer can
6449 // try the lowering again.
6450 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006451 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006452 // be matched during isel, all of them must be converted to a target specific
6453 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006454
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006455 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6456 // narrowing and commutation of operands should be handled. The actual code
6457 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006458 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006459 if (NewOp.getNode())
6460 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006461
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006462 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6463 // unpckh_undef). Only use pshufd if speed is more important than size.
6464 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Craig Topper34671b82011-12-06 08:21:25 +00006465 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006466 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Craig Topper34671b82011-12-06 08:21:25 +00006467 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006468
Craig Topperc0d82852011-11-22 00:44:41 +00006469 if (X86::isMOVDDUPMask(SVOp) && Subtarget->hasSSE3orAVX() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006470 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006471 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006472
Dale Johannesen0488fb62010-09-30 23:57:10 +00006473 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006474 return getMOVHighToLow(Op, dl, DAG);
6475
6476 // Use to match splats
Craig Topperc0d82852011-11-22 00:44:41 +00006477 if (HasXMMInt && X86::isUNPCKHMask(SVOp, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006478 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006479 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006480
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006481 if (X86::isPSHUFDMask(SVOp)) {
6482 // The actual implementation will match the mask in the if above and then
6483 // during isel it can match several different instructions, not only pshufd
6484 // as its name says, sad but true, emulate the behavior for now...
6485 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6486 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6487
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006488 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6489
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006490 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006491 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6492
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006493 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6494 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006495 }
Eric Christopherfd179292009-08-27 18:07:15 +00006496
Evan Chengf26ffe92008-05-29 08:22:04 +00006497 // Check if this can be converted into a logical shift.
6498 bool isLeft = false;
6499 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006500 SDValue ShVal;
Craig Topperc0d82852011-11-22 00:44:41 +00006501 bool isShift = HasXMMInt && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006502 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006503 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006504 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006505 EVT EltVT = VT.getVectorElementType();
6506 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006507 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006508 }
Eric Christopherfd179292009-08-27 18:07:15 +00006509
Nate Begeman9008ca62009-04-27 18:41:29 +00006510 if (X86::isMOVLMask(SVOp)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006511 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006512 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006513 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006514 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006515 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6516
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006517 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006518 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6519 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006520 }
Eric Christopherfd179292009-08-27 18:07:15 +00006521
Nate Begeman9008ca62009-04-27 18:41:29 +00006522 // FIXME: fold these into legal mask.
Craig Topperc0d82852011-11-22 00:44:41 +00006523 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp, HasAVX2))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006524 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006525
Dale Johannesen0488fb62010-09-30 23:57:10 +00006526 if (X86::isMOVHLPSMask(SVOp))
6527 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006528
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006529 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006530 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006531
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006532 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006533 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006534
Dale Johannesen0488fb62010-09-30 23:57:10 +00006535 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006536 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006537
Nate Begeman9008ca62009-04-27 18:41:29 +00006538 if (ShouldXformToMOVHLPS(SVOp) ||
6539 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6540 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006541
Evan Chengf26ffe92008-05-29 08:22:04 +00006542 if (isShift) {
6543 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006544 EVT EltVT = VT.getVectorElementType();
6545 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006546 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006547 }
Eric Christopherfd179292009-08-27 18:07:15 +00006548
Evan Cheng9eca5e82006-10-25 21:49:50 +00006549 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006550 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6551 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006552 V1IsSplat = isSplatVector(V1.getNode());
6553 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006554
Chris Lattner8a594482007-11-25 00:24:49 +00006555 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper38034c52011-11-26 22:55:48 +00006556 if (V1IsSplat && !V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006557 Op = CommuteVectorShuffle(SVOp, DAG);
6558 SVOp = cast<ShuffleVectorSDNode>(Op);
6559 V1 = SVOp->getOperand(0);
6560 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006561 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006562 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006563 }
6564
Craig Topperbeabc6c2011-12-05 06:56:46 +00006565 SmallVector<int, 32> M;
6566 SVOp->getMask(M);
6567
6568 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006569 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006570 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006571 return V1;
6572 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6573 // the instruction selector will not match, so get a canonical MOVL with
6574 // swapped operands to undo the commute.
6575 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006576 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006577
Craig Topperbeabc6c2011-12-05 06:56:46 +00006578 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006579 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006580
Craig Topperbeabc6c2011-12-05 06:56:46 +00006581 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006582 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006583
Evan Cheng9bbbb982006-10-25 20:48:19 +00006584 if (V2IsSplat) {
6585 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006586 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006587 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006588 SDValue NewMask = NormalizeMask(SVOp, DAG);
6589 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6590 if (NSVOp != SVOp) {
Craig Topperc0d82852011-11-22 00:44:41 +00006591 if (X86::isUNPCKLMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006592 return NewMask;
Craig Topperc0d82852011-11-22 00:44:41 +00006593 } else if (X86::isUNPCKHMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006594 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006595 }
6596 }
6597 }
6598
Evan Cheng9eca5e82006-10-25 21:49:50 +00006599 if (Commuted) {
6600 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006601 // FIXME: this seems wrong.
6602 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6603 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006604
Craig Topperc0d82852011-11-22 00:44:41 +00006605 if (X86::isUNPCKLMask(NewSVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006606 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006607
Craig Topperc0d82852011-11-22 00:44:41 +00006608 if (X86::isUNPCKHMask(NewSVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006609 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006610 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006611
Nate Begeman9008ca62009-04-27 18:41:29 +00006612 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1ff73d72011-12-06 04:59:07 +00006613 if (!V2IsUndef && (isSHUFPMask(M, VT, /* Commuted */ true) ||
6614 isVSHUFPYMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006615 return CommuteVectorShuffle(SVOp, DAG);
6616
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006617 // The checks below are all present in isShuffleMaskLegal, but they are
6618 // inlined here right now to enable us to directly emit target specific
6619 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006620
Craig Topperc0d82852011-11-22 00:44:41 +00006621 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006622 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6623 X86::getShufflePALIGNRImmediate(SVOp),
6624 DAG);
6625
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006626 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6627 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006628 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006629 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006630 }
6631
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006632 if (isPSHUFHWMask(M, VT))
6633 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6634 X86::getShufflePSHUFHWImmediate(SVOp),
6635 DAG);
6636
6637 if (isPSHUFLWMask(M, VT))
6638 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6639 X86::getShufflePSHUFLWImmediate(SVOp),
6640 DAG);
6641
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006642 if (isSHUFPMask(M, VT))
6643 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6644 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006645
Craig Topperbeabc6c2011-12-05 06:56:46 +00006646 if (isUNPCKL_v_undef_Mask(M, VT))
Craig Topper34671b82011-12-06 08:21:25 +00006647 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topperbeabc6c2011-12-05 06:56:46 +00006648 if (isUNPCKH_v_undef_Mask(M, VT))
Craig Topper34671b82011-12-06 08:21:25 +00006649 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006650
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006651 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006652 // Generate target specific nodes for 128 or 256-bit shuffles only
6653 // supported in the AVX instruction set.
6654 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006655
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006656 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006657 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006658 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6659
Craig Topper70b883b2011-11-28 10:14:51 +00006660 // Handle VPERMILPS/D* permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006661 if (isVPERMILPMask(M, VT, HasAVX))
Craig Topper316cd2a2011-11-30 06:25:25 +00006662 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006663 getShuffleVPERMILPImmediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006664
Craig Topper70b883b2011-11-28 10:14:51 +00006665 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006666 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006667 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006668 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006669
Craig Topper70b883b2011-11-28 10:14:51 +00006670 // Handle VSHUFPS/DY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006671 if (isVSHUFPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006672 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
Craig Topper9d7025b2011-11-27 21:41:12 +00006673 getShuffleVSHUFPYImmediate(SVOp), DAG);
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006674
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006675 //===--------------------------------------------------------------------===//
6676 // Since no target specific shuffle was selected for this generic one,
6677 // lower it into other known shuffles. FIXME: this isn't true yet, but
6678 // this is the plan.
6679 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006680
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006681 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6682 if (VT == MVT::v8i16) {
6683 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6684 if (NewOp.getNode())
6685 return NewOp;
6686 }
6687
6688 if (VT == MVT::v16i8) {
6689 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6690 if (NewOp.getNode())
6691 return NewOp;
6692 }
6693
6694 // Handle all 128-bit wide vectors with 4 elements, and match them with
6695 // several different shuffle types.
6696 if (NumElems == 4 && VT.getSizeInBits() == 128)
6697 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6698
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006699 // Handle general 256-bit shuffles
6700 if (VT.is256BitVector())
6701 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6702
Dan Gohman475871a2008-07-27 21:46:04 +00006703 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006704}
6705
Dan Gohman475871a2008-07-27 21:46:04 +00006706SDValue
6707X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006708 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006709 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006710 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006711
6712 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6713 return SDValue();
6714
Duncan Sands83ec4b62008-06-06 12:08:01 +00006715 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006716 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006717 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006718 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006719 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006720 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006721 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006722 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6723 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6724 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006725 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6726 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006727 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006728 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006729 Op.getOperand(0)),
6730 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006731 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006732 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006733 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006734 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006735 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006736 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006737 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6738 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006739 // result has a single use which is a store or a bitcast to i32. And in
6740 // the case of a store, it's not worth it if the index is a constant 0,
6741 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006742 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006743 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006744 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006745 if ((User->getOpcode() != ISD::STORE ||
6746 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6747 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006748 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006749 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006750 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006751 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006752 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006753 Op.getOperand(0)),
6754 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006755 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Pete Coopera77214a2011-11-14 19:38:42 +00006756 } else if (VT == MVT::i32 || VT == MVT::i64) {
6757 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006758 if (isa<ConstantSDNode>(Op.getOperand(1)))
6759 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006760 }
Dan Gohman475871a2008-07-27 21:46:04 +00006761 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006762}
6763
6764
Dan Gohman475871a2008-07-27 21:46:04 +00006765SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006766X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6767 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006768 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006769 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006770
David Greene74a579d2011-02-10 16:57:36 +00006771 SDValue Vec = Op.getOperand(0);
6772 EVT VecVT = Vec.getValueType();
6773
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006774 // If this is a 256-bit vector result, first extract the 128-bit vector and
6775 // then extract the element from the 128-bit vector.
6776 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006777 DebugLoc dl = Op.getNode()->getDebugLoc();
6778 unsigned NumElems = VecVT.getVectorNumElements();
6779 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006780 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6781
6782 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006783 bool Upper = IdxVal >= NumElems/2;
6784 Vec = Extract128BitVector(Vec,
6785 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006786
David Greene74a579d2011-02-10 16:57:36 +00006787 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006788 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006789 }
6790
6791 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6792
Craig Topperc0d82852011-11-22 00:44:41 +00006793 if (Subtarget->hasSSE41orAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006794 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006795 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006796 return Res;
6797 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006798
Owen Andersone50ed302009-08-10 22:56:29 +00006799 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006800 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006801 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006802 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006803 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006804 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006805 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006806 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6807 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006808 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006809 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006810 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006811 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006812 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006813 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006814 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006815 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006816 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006817 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006818 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006819 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006820 if (Idx == 0)
6821 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006822
Evan Cheng0db9fe62006-04-25 20:13:52 +00006823 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006824 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006825 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006826 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006827 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006828 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006829 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006830 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006831 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6832 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6833 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006834 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006835 if (Idx == 0)
6836 return Op;
6837
6838 // UNPCKHPD the element to the lowest double word, then movsd.
6839 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6840 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006841 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006842 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006843 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006844 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006845 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006846 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006847 }
6848
Dan Gohman475871a2008-07-27 21:46:04 +00006849 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006850}
6851
Dan Gohman475871a2008-07-27 21:46:04 +00006852SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006853X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6854 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006855 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006856 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006857 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006858
Dan Gohman475871a2008-07-27 21:46:04 +00006859 SDValue N0 = Op.getOperand(0);
6860 SDValue N1 = Op.getOperand(1);
6861 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006862
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006863 if (VT.getSizeInBits() == 256)
6864 return SDValue();
6865
Dan Gohman8a55ce42009-09-23 21:02:20 +00006866 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006867 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006868 unsigned Opc;
6869 if (VT == MVT::v8i16)
6870 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006871 else if (VT == MVT::v16i8)
6872 Opc = X86ISD::PINSRB;
6873 else
6874 Opc = X86ISD::PINSRB;
6875
Nate Begeman14d12ca2008-02-11 04:19:36 +00006876 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6877 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006878 if (N1.getValueType() != MVT::i32)
6879 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6880 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006881 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006882 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006883 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006884 // Bits [7:6] of the constant are the source select. This will always be
6885 // zero here. The DAG Combiner may combine an extract_elt index into these
6886 // bits. For example (insert (extract, 3), 2) could be matched by putting
6887 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006888 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006889 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006890 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006891 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006892 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006893 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006894 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006895 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Pete Coopera77214a2011-11-14 19:38:42 +00006896 } else if ((EltVT == MVT::i32 || EltVT == MVT::i64) &&
6897 isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006898 // PINSR* works with constant index.
6899 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006900 }
Dan Gohman475871a2008-07-27 21:46:04 +00006901 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006902}
6903
Dan Gohman475871a2008-07-27 21:46:04 +00006904SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006905X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006906 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006907 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006908
David Greene6b381262011-02-09 15:32:06 +00006909 DebugLoc dl = Op.getDebugLoc();
6910 SDValue N0 = Op.getOperand(0);
6911 SDValue N1 = Op.getOperand(1);
6912 SDValue N2 = Op.getOperand(2);
6913
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006914 // If this is a 256-bit vector result, first extract the 128-bit vector,
6915 // insert the element into the extracted half and then place it back.
6916 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00006917 if (!isa<ConstantSDNode>(N2))
6918 return SDValue();
6919
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006920 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00006921 unsigned NumElems = VT.getVectorNumElements();
6922 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006923 bool Upper = IdxVal >= NumElems/2;
6924 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
6925 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00006926
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006927 // Insert the element into the desired half.
6928 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
6929 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00006930
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006931 // Insert the changed part back to the 256-bit vector
6932 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00006933 }
6934
Craig Topperc0d82852011-11-22 00:44:41 +00006935 if (Subtarget->hasSSE41orAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00006936 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6937
Dan Gohman8a55ce42009-09-23 21:02:20 +00006938 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006939 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006940
Dan Gohman8a55ce42009-09-23 21:02:20 +00006941 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006942 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6943 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006944 if (N1.getValueType() != MVT::i32)
6945 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6946 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006947 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006948 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006949 }
Dan Gohman475871a2008-07-27 21:46:04 +00006950 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006951}
6952
Dan Gohman475871a2008-07-27 21:46:04 +00006953SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006954X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00006955 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006956 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006957 EVT OpVT = Op.getValueType();
6958
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00006959 // If this is a 256-bit vector result, first insert into a 128-bit
6960 // vector and then insert into the 256-bit vector.
6961 if (OpVT.getSizeInBits() > 128) {
6962 // Insert into a 128-bit vector.
6963 EVT VT128 = EVT::getVectorVT(*Context,
6964 OpVT.getVectorElementType(),
6965 OpVT.getVectorNumElements() / 2);
6966
6967 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6968
6969 // Insert the 128-bit vector.
6970 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6971 DAG.getConstant(0, MVT::i32),
6972 DAG, dl);
6973 }
6974
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006975 if (Op.getValueType() == MVT::v1i64 &&
6976 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006977 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006978
Owen Anderson825b72b2009-08-11 20:47:22 +00006979 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006980 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6981 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006982 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006983 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006984}
6985
David Greene91585092011-01-26 15:38:49 +00006986// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6987// a simple subregister reference or explicit instructions to grab
6988// upper bits of a vector.
6989SDValue
6990X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6991 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006992 DebugLoc dl = Op.getNode()->getDebugLoc();
6993 SDValue Vec = Op.getNode()->getOperand(0);
6994 SDValue Idx = Op.getNode()->getOperand(1);
6995
6996 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6997 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6998 return Extract128BitVector(Vec, Idx, DAG, dl);
6999 }
David Greene91585092011-01-26 15:38:49 +00007000 }
7001 return SDValue();
7002}
7003
David Greenecfe33c42011-01-26 19:13:22 +00007004// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7005// simple superregister reference or explicit instructions to insert
7006// the upper bits of a vector.
7007SDValue
7008X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7009 if (Subtarget->hasAVX()) {
7010 DebugLoc dl = Op.getNode()->getDebugLoc();
7011 SDValue Vec = Op.getNode()->getOperand(0);
7012 SDValue SubVec = Op.getNode()->getOperand(1);
7013 SDValue Idx = Op.getNode()->getOperand(2);
7014
7015 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7016 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007017 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007018 }
7019 }
7020 return SDValue();
7021}
7022
Bill Wendling056292f2008-09-16 21:48:12 +00007023// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7024// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7025// one of the above mentioned nodes. It has to be wrapped because otherwise
7026// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7027// be used to form addressing mode. These wrapped nodes will be selected
7028// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007029SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007030X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007031 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007032
Chris Lattner41621a22009-06-26 19:22:52 +00007033 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7034 // global base reg.
7035 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007036 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007037 CodeModel::Model M = getTargetMachine().getCodeModel();
7038
Chris Lattner4f066492009-07-11 20:29:19 +00007039 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007040 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007041 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007042 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007043 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007044 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007045 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007046
Evan Cheng1606e8e2009-03-13 07:51:59 +00007047 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007048 CP->getAlignment(),
7049 CP->getOffset(), OpFlag);
7050 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007051 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007052 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007053 if (OpFlag) {
7054 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007055 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007056 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007057 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007058 }
7059
7060 return Result;
7061}
7062
Dan Gohmand858e902010-04-17 15:26:15 +00007063SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007064 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007065
Chris Lattner18c59872009-06-27 04:16:01 +00007066 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7067 // global base reg.
7068 unsigned char OpFlag = 0;
7069 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007070 CodeModel::Model M = getTargetMachine().getCodeModel();
7071
Chris Lattner4f066492009-07-11 20:29:19 +00007072 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007073 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007074 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007075 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007076 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007077 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007078 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007079
Chris Lattner18c59872009-06-27 04:16:01 +00007080 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7081 OpFlag);
7082 DebugLoc DL = JT->getDebugLoc();
7083 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007084
Chris Lattner18c59872009-06-27 04:16:01 +00007085 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007086 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007087 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7088 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007089 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007090 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007091
Chris Lattner18c59872009-06-27 04:16:01 +00007092 return Result;
7093}
7094
7095SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007096X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007097 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007098
Chris Lattner18c59872009-06-27 04:16:01 +00007099 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7100 // global base reg.
7101 unsigned char OpFlag = 0;
7102 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007103 CodeModel::Model M = getTargetMachine().getCodeModel();
7104
Chris Lattner4f066492009-07-11 20:29:19 +00007105 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007106 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7107 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7108 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007109 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007110 } else if (Subtarget->isPICStyleGOT()) {
7111 OpFlag = X86II::MO_GOT;
7112 } else if (Subtarget->isPICStyleStubPIC()) {
7113 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7114 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7115 OpFlag = X86II::MO_DARWIN_NONLAZY;
7116 }
Eric Christopherfd179292009-08-27 18:07:15 +00007117
Chris Lattner18c59872009-06-27 04:16:01 +00007118 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007119
Chris Lattner18c59872009-06-27 04:16:01 +00007120 DebugLoc DL = Op.getDebugLoc();
7121 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007122
7123
Chris Lattner18c59872009-06-27 04:16:01 +00007124 // With PIC, the address is actually $g + Offset.
7125 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007126 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007127 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7128 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007129 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007130 Result);
7131 }
Eric Christopherfd179292009-08-27 18:07:15 +00007132
Eli Friedman586272d2011-08-11 01:48:05 +00007133 // For symbols that require a load from a stub to get the address, emit the
7134 // load.
7135 if (isGlobalStubReference(OpFlag))
7136 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007137 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007138
Chris Lattner18c59872009-06-27 04:16:01 +00007139 return Result;
7140}
7141
Dan Gohman475871a2008-07-27 21:46:04 +00007142SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007143X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007144 // Create the TargetBlockAddressAddress node.
7145 unsigned char OpFlags =
7146 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007147 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007148 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007149 DebugLoc dl = Op.getDebugLoc();
7150 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7151 /*isTarget=*/true, OpFlags);
7152
Dan Gohmanf705adb2009-10-30 01:28:02 +00007153 if (Subtarget->isPICStyleRIPRel() &&
7154 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007155 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7156 else
7157 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007158
Dan Gohman29cbade2009-11-20 23:18:13 +00007159 // With PIC, the address is actually $g + Offset.
7160 if (isGlobalRelativeToPICBase(OpFlags)) {
7161 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7162 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7163 Result);
7164 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007165
7166 return Result;
7167}
7168
7169SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007170X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007171 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007172 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007173 // Create the TargetGlobalAddress node, folding in the constant
7174 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007175 unsigned char OpFlags =
7176 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007177 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007178 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007179 if (OpFlags == X86II::MO_NO_FLAG &&
7180 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007181 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007182 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007183 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007184 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007185 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007186 }
Eric Christopherfd179292009-08-27 18:07:15 +00007187
Chris Lattner4f066492009-07-11 20:29:19 +00007188 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007189 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007190 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7191 else
7192 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007193
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007194 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007195 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007196 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7197 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007198 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007199 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007200
Chris Lattner36c25012009-07-10 07:34:39 +00007201 // For globals that require a load from a stub to get the address, emit the
7202 // load.
7203 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007204 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007205 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007206
Dan Gohman6520e202008-10-18 02:06:02 +00007207 // If there was a non-zero offset that we didn't fold, create an explicit
7208 // addition for it.
7209 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007210 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007211 DAG.getConstant(Offset, getPointerTy()));
7212
Evan Cheng0db9fe62006-04-25 20:13:52 +00007213 return Result;
7214}
7215
Evan Chengda43bcf2008-09-24 00:05:32 +00007216SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007217X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007218 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007219 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007220 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007221}
7222
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007223static SDValue
7224GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007225 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007226 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007227 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007228 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007229 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007230 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007231 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007232 GA->getOffset(),
7233 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007234 if (InFlag) {
7235 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007236 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007237 } else {
7238 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007239 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007240 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007241
7242 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007243 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007244
Rafael Espindola15f1b662009-04-24 12:59:40 +00007245 SDValue Flag = Chain.getValue(1);
7246 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007247}
7248
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007249// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007250static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007251LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007252 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007253 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007254 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7255 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007256 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007257 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007258 InFlag = Chain.getValue(1);
7259
Chris Lattnerb903bed2009-06-26 21:20:29 +00007260 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007261}
7262
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007263// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007264static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007265LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007266 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007267 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7268 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007269}
7270
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007271// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7272// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007273static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007274 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007275 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007276 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007277
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007278 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7279 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7280 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007281
Michael J. Spencerec38de22010-10-10 22:04:20 +00007282 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007283 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007284 MachinePointerInfo(Ptr),
7285 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007286
Chris Lattnerb903bed2009-06-26 21:20:29 +00007287 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007288 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7289 // initialexec.
7290 unsigned WrapperKind = X86ISD::Wrapper;
7291 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007292 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007293 } else if (is64Bit) {
7294 assert(model == TLSModel::InitialExec);
7295 OperandFlags = X86II::MO_GOTTPOFF;
7296 WrapperKind = X86ISD::WrapperRIP;
7297 } else {
7298 assert(model == TLSModel::InitialExec);
7299 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007300 }
Eric Christopherfd179292009-08-27 18:07:15 +00007301
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007302 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7303 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007304 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007305 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007306 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007307 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007308
Rafael Espindola9a580232009-02-27 13:37:18 +00007309 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007310 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007311 MachinePointerInfo::getGOT(), false, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007312
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007313 // The address of the thread local variable is the add of the thread
7314 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007315 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007316}
7317
Dan Gohman475871a2008-07-27 21:46:04 +00007318SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007319X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007320
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007321 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007322 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007323
Eric Christopher30ef0e52010-06-03 04:07:48 +00007324 if (Subtarget->isTargetELF()) {
7325 // TODO: implement the "local dynamic" model
7326 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007327
Eric Christopher30ef0e52010-06-03 04:07:48 +00007328 // If GV is an alias then use the aliasee for determining
7329 // thread-localness.
7330 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7331 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007332
7333 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007334 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007335
Eric Christopher30ef0e52010-06-03 04:07:48 +00007336 switch (model) {
7337 case TLSModel::GeneralDynamic:
7338 case TLSModel::LocalDynamic: // not implemented
7339 if (Subtarget->is64Bit())
7340 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7341 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007342
Eric Christopher30ef0e52010-06-03 04:07:48 +00007343 case TLSModel::InitialExec:
7344 case TLSModel::LocalExec:
7345 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7346 Subtarget->is64Bit());
7347 }
7348 } else if (Subtarget->isTargetDarwin()) {
7349 // Darwin only has one model of TLS. Lower to that.
7350 unsigned char OpFlag = 0;
7351 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7352 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007353
Eric Christopher30ef0e52010-06-03 04:07:48 +00007354 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7355 // global base reg.
7356 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7357 !Subtarget->is64Bit();
7358 if (PIC32)
7359 OpFlag = X86II::MO_TLVP_PIC_BASE;
7360 else
7361 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007362 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007363 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007364 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007365 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007366 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007367
Eric Christopher30ef0e52010-06-03 04:07:48 +00007368 // With PIC32, the address is actually $g + Offset.
7369 if (PIC32)
7370 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7371 DAG.getNode(X86ISD::GlobalBaseReg,
7372 DebugLoc(), getPointerTy()),
7373 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007374
Eric Christopher30ef0e52010-06-03 04:07:48 +00007375 // Lowering the machine isd will make sure everything is in the right
7376 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007377 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007378 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007379 SDValue Args[] = { Chain, Offset };
7380 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007381
Eric Christopher30ef0e52010-06-03 04:07:48 +00007382 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7383 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7384 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007385
Eric Christopher30ef0e52010-06-03 04:07:48 +00007386 // And our return value (tls address) is in the standard call return value
7387 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007388 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007389 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7390 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007391 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007392
Eric Christopher30ef0e52010-06-03 04:07:48 +00007393 assert(false &&
7394 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007395
Torok Edwinc23197a2009-07-14 16:55:14 +00007396 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007397 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007398}
7399
Evan Cheng0db9fe62006-04-25 20:13:52 +00007400
Nadav Rotem43012222011-05-11 08:12:09 +00007401/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007402/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007403SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007404 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007405 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007406 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007407 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007408 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007409 SDValue ShOpLo = Op.getOperand(0);
7410 SDValue ShOpHi = Op.getOperand(1);
7411 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007412 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007413 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007414 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007415
Dan Gohman475871a2008-07-27 21:46:04 +00007416 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007417 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007418 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7419 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007420 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007421 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7422 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007423 }
Evan Chenge3413162006-01-09 18:33:28 +00007424
Owen Anderson825b72b2009-08-11 20:47:22 +00007425 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7426 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007427 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007428 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007429
Dan Gohman475871a2008-07-27 21:46:04 +00007430 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007431 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007432 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7433 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007434
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007435 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007436 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7437 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007438 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007439 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7440 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007441 }
7442
Dan Gohman475871a2008-07-27 21:46:04 +00007443 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007444 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007445}
Evan Chenga3195e82006-01-12 22:54:21 +00007446
Dan Gohmand858e902010-04-17 15:26:15 +00007447SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7448 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007449 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007450
Dale Johannesen0488fb62010-09-30 23:57:10 +00007451 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007452 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007453
Owen Anderson825b72b2009-08-11 20:47:22 +00007454 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007455 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007456
Eli Friedman36df4992009-05-27 00:47:34 +00007457 // These are really Legal; return the operand so the caller accepts it as
7458 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007459 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007460 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007461 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007462 Subtarget->is64Bit()) {
7463 return Op;
7464 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007465
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007466 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007467 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007468 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007469 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007470 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007471 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007472 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007473 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007474 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007475 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7476}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007477
Owen Andersone50ed302009-08-10 22:56:29 +00007478SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007479 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007480 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007481 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007482 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007483 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007484 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007485 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007486 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007487 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007488 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007489
Chris Lattner492a43e2010-09-22 01:28:21 +00007490 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007491
Stuart Hastings84be9582011-06-02 15:57:11 +00007492 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7493 MachineMemOperand *MMO;
7494 if (FI) {
7495 int SSFI = FI->getIndex();
7496 MMO =
7497 DAG.getMachineFunction()
7498 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7499 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7500 } else {
7501 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7502 StackSlot = StackSlot.getOperand(1);
7503 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007504 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007505 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7506 X86ISD::FILD, DL,
7507 Tys, Ops, array_lengthof(Ops),
7508 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007509
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007510 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007511 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007512 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007513
7514 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7515 // shouldn't be necessary except that RFP cannot be live across
7516 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007517 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007518 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7519 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007520 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007521 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007522 SDValue Ops[] = {
7523 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7524 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007525 MachineMemOperand *MMO =
7526 DAG.getMachineFunction()
7527 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007528 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007529
Chris Lattner492a43e2010-09-22 01:28:21 +00007530 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7531 Ops, array_lengthof(Ops),
7532 Op.getValueType(), MMO);
7533 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007534 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007535 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007536 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007537
Evan Cheng0db9fe62006-04-25 20:13:52 +00007538 return Result;
7539}
7540
Bill Wendling8b8a6362009-01-17 03:56:04 +00007541// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007542SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7543 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007544 // This algorithm is not obvious. Here it is in C code, more or less:
7545 /*
7546 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7547 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7548 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007549
Bill Wendling8b8a6362009-01-17 03:56:04 +00007550 // Copy ints to xmm registers.
7551 __m128i xh = _mm_cvtsi32_si128( hi );
7552 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007553
Bill Wendling8b8a6362009-01-17 03:56:04 +00007554 // Combine into low half of a single xmm register.
7555 __m128i x = _mm_unpacklo_epi32( xh, xl );
7556 __m128d d;
7557 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007558
Bill Wendling8b8a6362009-01-17 03:56:04 +00007559 // Merge in appropriate exponents to give the integer bits the right
7560 // magnitude.
7561 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007562
Bill Wendling8b8a6362009-01-17 03:56:04 +00007563 // Subtract away the biases to deal with the IEEE-754 double precision
7564 // implicit 1.
7565 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007566
Bill Wendling8b8a6362009-01-17 03:56:04 +00007567 // All conversions up to here are exact. The correctly rounded result is
7568 // calculated using the current rounding mode using the following
7569 // horizontal add.
7570 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7571 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7572 // store doesn't really need to be here (except
7573 // maybe to zero the other double)
7574 return sd;
7575 }
7576 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007577
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007578 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007579 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007580
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007581 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007582 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007583 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7584 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7585 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7586 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007587 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007588 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007589
Bill Wendling8b8a6362009-01-17 03:56:04 +00007590 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007591 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007592 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007593 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007594 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007595 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007596 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007597
Owen Anderson825b72b2009-08-11 20:47:22 +00007598 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7599 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007600 Op.getOperand(0),
7601 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007602 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7603 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007604 Op.getOperand(0),
7605 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007606 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7607 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007608 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007609 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007610 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007611 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007612 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007613 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007614 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007615 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007616
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007617 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007618 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007619 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7620 DAG.getUNDEF(MVT::v2f64), ShufMask);
7621 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7622 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007623 DAG.getIntPtrConstant(0));
7624}
7625
Bill Wendling8b8a6362009-01-17 03:56:04 +00007626// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007627SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7628 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007629 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007630 // FP constant to bias correct the final result.
7631 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007632 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007633
7634 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007635 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007636 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007637
Eli Friedmanf3704762011-08-29 21:15:46 +00007638 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007639 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7640 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007641
Owen Anderson825b72b2009-08-11 20:47:22 +00007642 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007643 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007644 DAG.getIntPtrConstant(0));
7645
7646 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007647 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007648 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007649 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007650 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007651 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007652 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007653 MVT::v2f64, Bias)));
7654 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007655 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007656 DAG.getIntPtrConstant(0));
7657
7658 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007659 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007660
7661 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007662 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007663
Owen Anderson825b72b2009-08-11 20:47:22 +00007664 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007665 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007666 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007667 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007668 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007669 }
7670
7671 // Handle final rounding.
7672 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007673}
7674
Dan Gohmand858e902010-04-17 15:26:15 +00007675SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7676 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007677 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007678 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007679
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007680 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007681 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7682 // the optimization here.
7683 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007684 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007685
Owen Andersone50ed302009-08-10 22:56:29 +00007686 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007687 EVT DstVT = Op.getValueType();
7688 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007689 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007690 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007691 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007692
7693 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007694 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007695 if (SrcVT == MVT::i32) {
7696 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7697 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7698 getPointerTy(), StackSlot, WordOff);
7699 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007700 StackSlot, MachinePointerInfo(),
7701 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007702 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007703 OffsetSlot, MachinePointerInfo(),
7704 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007705 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7706 return Fild;
7707 }
7708
7709 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7710 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007711 StackSlot, MachinePointerInfo(),
7712 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007713 // For i64 source, we need to add the appropriate power of 2 if the input
7714 // was negative. This is the same as the optimization in
7715 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7716 // we must be careful to do the computation in x87 extended precision, not
7717 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007718 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7719 MachineMemOperand *MMO =
7720 DAG.getMachineFunction()
7721 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7722 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007723
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007724 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7725 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007726 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7727 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007728
7729 APInt FF(32, 0x5F800000ULL);
7730
7731 // Check whether the sign bit is set.
7732 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7733 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7734 ISD::SETLT);
7735
7736 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7737 SDValue FudgePtr = DAG.getConstantPool(
7738 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7739 getPointerTy());
7740
7741 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7742 SDValue Zero = DAG.getIntPtrConstant(0);
7743 SDValue Four = DAG.getIntPtrConstant(4);
7744 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7745 Zero, Four);
7746 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7747
7748 // Load the value out, extending it from f32 to f80.
7749 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007750 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007751 FudgePtr, MachinePointerInfo::getConstantPool(),
7752 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007753 // Extend everything to 80 bits to force it to be done on x87.
7754 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7755 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007756}
7757
Dan Gohman475871a2008-07-27 21:46:04 +00007758std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007759FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007760 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007761
Owen Andersone50ed302009-08-10 22:56:29 +00007762 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007763
7764 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007765 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7766 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007767 }
7768
Owen Anderson825b72b2009-08-11 20:47:22 +00007769 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7770 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007771 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007772
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007773 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007774 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007775 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007776 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007777 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007778 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007779 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007780 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007781
Evan Cheng87c89352007-10-15 20:11:21 +00007782 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7783 // stack slot.
7784 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007785 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007786 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007787 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007788
Michael J. Spencerec38de22010-10-10 22:04:20 +00007789
7790
Evan Cheng0db9fe62006-04-25 20:13:52 +00007791 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007792 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007793 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007794 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7795 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7796 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007797 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007798
Dan Gohman475871a2008-07-27 21:46:04 +00007799 SDValue Chain = DAG.getEntryNode();
7800 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007801 EVT TheVT = Op.getOperand(0).getValueType();
7802 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007803 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007804 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007805 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007806 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007807 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007808 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007809 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007810 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007811
Chris Lattner492a43e2010-09-22 01:28:21 +00007812 MachineMemOperand *MMO =
7813 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7814 MachineMemOperand::MOLoad, MemSize, MemSize);
7815 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7816 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007817 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007818 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007819 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7820 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007821
Chris Lattner07290932010-09-22 01:05:16 +00007822 MachineMemOperand *MMO =
7823 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7824 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007825
Evan Cheng0db9fe62006-04-25 20:13:52 +00007826 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007827 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007828 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7829 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007830
Chris Lattner27a6c732007-11-24 07:07:01 +00007831 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007832}
7833
Dan Gohmand858e902010-04-17 15:26:15 +00007834SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7835 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007836 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007837 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007838
Eli Friedman948e95a2009-05-23 09:59:16 +00007839 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007840 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007841 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7842 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007843
Chris Lattner27a6c732007-11-24 07:07:01 +00007844 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007845 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007846 FIST, StackSlot, MachinePointerInfo(),
7847 false, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007848}
7849
Dan Gohmand858e902010-04-17 15:26:15 +00007850SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7851 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007852 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7853 SDValue FIST = Vals.first, StackSlot = Vals.second;
7854 assert(FIST.getNode() && "Unexpected failure");
7855
7856 // Load the result.
7857 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007858 FIST, StackSlot, MachinePointerInfo(),
7859 false, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007860}
7861
Dan Gohmand858e902010-04-17 15:26:15 +00007862SDValue X86TargetLowering::LowerFABS(SDValue Op,
7863 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007864 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007865 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007866 EVT VT = Op.getValueType();
7867 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007868 if (VT.isVector())
7869 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007870 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007871 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007872 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007873 CV.push_back(C);
7874 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007875 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007876 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007877 CV.push_back(C);
7878 CV.push_back(C);
7879 CV.push_back(C);
7880 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007881 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007882 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007883 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007884 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007885 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007886 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007887 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007888}
7889
Dan Gohmand858e902010-04-17 15:26:15 +00007890SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007891 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007892 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007893 EVT VT = Op.getValueType();
7894 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007895 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007896 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007897 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007898 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007899 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007900 CV.push_back(C);
7901 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007902 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007903 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007904 CV.push_back(C);
7905 CV.push_back(C);
7906 CV.push_back(C);
7907 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007908 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007909 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007910 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007911 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007912 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007913 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007914 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007915 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007916 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007917 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007918 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007919 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007920 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007921 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007922 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007923}
7924
Dan Gohmand858e902010-04-17 15:26:15 +00007925SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007926 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007927 SDValue Op0 = Op.getOperand(0);
7928 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007929 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007930 EVT VT = Op.getValueType();
7931 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007932
7933 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007934 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007935 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007936 SrcVT = VT;
7937 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007938 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007939 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007940 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007941 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007942 }
7943
7944 // At this point the operands and the result should have the same
7945 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007946
Evan Cheng68c47cb2007-01-05 07:55:56 +00007947 // First get the sign bit of second operand.
7948 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007949 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007950 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7951 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007952 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007953 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7954 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7955 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7956 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007957 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007958 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007959 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007960 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007961 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007962 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007963 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007964
7965 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007966 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007967 // Op0 is MVT::f32, Op1 is MVT::f64.
7968 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7969 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7970 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007971 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007972 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007973 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007974 }
7975
Evan Cheng73d6cf12007-01-05 21:37:56 +00007976 // Clear first operand sign bit.
7977 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007978 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007979 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7980 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007981 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007982 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7983 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7984 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7985 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007986 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007987 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007988 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007989 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007990 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007991 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007992 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007993
7994 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007995 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007996}
7997
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00007998SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
7999 SDValue N0 = Op.getOperand(0);
8000 DebugLoc dl = Op.getDebugLoc();
8001 EVT VT = Op.getValueType();
8002
8003 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8004 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8005 DAG.getConstant(1, VT));
8006 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8007}
8008
Dan Gohman076aee32009-03-04 19:44:21 +00008009/// Emit nodes that will be selected as "test Op0,Op0", or something
8010/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008011SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008012 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008013 DebugLoc dl = Op.getDebugLoc();
8014
Dan Gohman31125812009-03-07 01:58:32 +00008015 // CF and OF aren't always set the way we want. Determine which
8016 // of these we need.
8017 bool NeedCF = false;
8018 bool NeedOF = false;
8019 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008020 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008021 case X86::COND_A: case X86::COND_AE:
8022 case X86::COND_B: case X86::COND_BE:
8023 NeedCF = true;
8024 break;
8025 case X86::COND_G: case X86::COND_GE:
8026 case X86::COND_L: case X86::COND_LE:
8027 case X86::COND_O: case X86::COND_NO:
8028 NeedOF = true;
8029 break;
Dan Gohman31125812009-03-07 01:58:32 +00008030 }
8031
Dan Gohman076aee32009-03-04 19:44:21 +00008032 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008033 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8034 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008035 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8036 // Emit a CMP with 0, which is the TEST pattern.
8037 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8038 DAG.getConstant(0, Op.getValueType()));
8039
8040 unsigned Opcode = 0;
8041 unsigned NumOperands = 0;
8042 switch (Op.getNode()->getOpcode()) {
8043 case ISD::ADD:
8044 // Due to an isel shortcoming, be conservative if this add is likely to be
8045 // selected as part of a load-modify-store instruction. When the root node
8046 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8047 // uses of other nodes in the match, such as the ADD in this case. This
8048 // leads to the ADD being left around and reselected, with the result being
8049 // two adds in the output. Alas, even if none our users are stores, that
8050 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8051 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8052 // climbing the DAG back to the root, and it doesn't seem to be worth the
8053 // effort.
8054 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008055 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8056 if (UI->getOpcode() != ISD::CopyToReg &&
8057 UI->getOpcode() != ISD::SETCC &&
8058 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008059 goto default_case;
8060
8061 if (ConstantSDNode *C =
8062 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8063 // An add of one will be selected as an INC.
8064 if (C->getAPIntValue() == 1) {
8065 Opcode = X86ISD::INC;
8066 NumOperands = 1;
8067 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008068 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008069
8070 // An add of negative one (subtract of one) will be selected as a DEC.
8071 if (C->getAPIntValue().isAllOnesValue()) {
8072 Opcode = X86ISD::DEC;
8073 NumOperands = 1;
8074 break;
8075 }
Dan Gohman076aee32009-03-04 19:44:21 +00008076 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008077
8078 // Otherwise use a regular EFLAGS-setting add.
8079 Opcode = X86ISD::ADD;
8080 NumOperands = 2;
8081 break;
8082 case ISD::AND: {
8083 // If the primary and result isn't used, don't bother using X86ISD::AND,
8084 // because a TEST instruction will be better.
8085 bool NonFlagUse = false;
8086 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8087 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8088 SDNode *User = *UI;
8089 unsigned UOpNo = UI.getOperandNo();
8090 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8091 // Look pass truncate.
8092 UOpNo = User->use_begin().getOperandNo();
8093 User = *User->use_begin();
8094 }
8095
8096 if (User->getOpcode() != ISD::BRCOND &&
8097 User->getOpcode() != ISD::SETCC &&
8098 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8099 NonFlagUse = true;
8100 break;
8101 }
Dan Gohman076aee32009-03-04 19:44:21 +00008102 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008103
8104 if (!NonFlagUse)
8105 break;
8106 }
8107 // FALL THROUGH
8108 case ISD::SUB:
8109 case ISD::OR:
8110 case ISD::XOR:
8111 // Due to the ISEL shortcoming noted above, be conservative if this op is
8112 // likely to be selected as part of a load-modify-store instruction.
8113 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8114 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8115 if (UI->getOpcode() == ISD::STORE)
8116 goto default_case;
8117
8118 // Otherwise use a regular EFLAGS-setting instruction.
8119 switch (Op.getNode()->getOpcode()) {
8120 default: llvm_unreachable("unexpected operator!");
8121 case ISD::SUB: Opcode = X86ISD::SUB; break;
8122 case ISD::OR: Opcode = X86ISD::OR; break;
8123 case ISD::XOR: Opcode = X86ISD::XOR; break;
8124 case ISD::AND: Opcode = X86ISD::AND; break;
8125 }
8126
8127 NumOperands = 2;
8128 break;
8129 case X86ISD::ADD:
8130 case X86ISD::SUB:
8131 case X86ISD::INC:
8132 case X86ISD::DEC:
8133 case X86ISD::OR:
8134 case X86ISD::XOR:
8135 case X86ISD::AND:
8136 return SDValue(Op.getNode(), 1);
8137 default:
8138 default_case:
8139 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008140 }
8141
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008142 if (Opcode == 0)
8143 // Emit a CMP with 0, which is the TEST pattern.
8144 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8145 DAG.getConstant(0, Op.getValueType()));
8146
8147 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8148 SmallVector<SDValue, 4> Ops;
8149 for (unsigned i = 0; i != NumOperands; ++i)
8150 Ops.push_back(Op.getOperand(i));
8151
8152 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8153 DAG.ReplaceAllUsesWith(Op, New);
8154 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008155}
8156
8157/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8158/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008159SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008160 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008161 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8162 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008163 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008164
8165 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008166 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008167}
8168
Evan Chengd40d03e2010-01-06 19:38:29 +00008169/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8170/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008171SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8172 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008173 SDValue Op0 = And.getOperand(0);
8174 SDValue Op1 = And.getOperand(1);
8175 if (Op0.getOpcode() == ISD::TRUNCATE)
8176 Op0 = Op0.getOperand(0);
8177 if (Op1.getOpcode() == ISD::TRUNCATE)
8178 Op1 = Op1.getOperand(0);
8179
Evan Chengd40d03e2010-01-06 19:38:29 +00008180 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008181 if (Op1.getOpcode() == ISD::SHL)
8182 std::swap(Op0, Op1);
8183 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008184 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8185 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008186 // If we looked past a truncate, check that it's only truncating away
8187 // known zeros.
8188 unsigned BitWidth = Op0.getValueSizeInBits();
8189 unsigned AndBitWidth = And.getValueSizeInBits();
8190 if (BitWidth > AndBitWidth) {
8191 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8192 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8193 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8194 return SDValue();
8195 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008196 LHS = Op1;
8197 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008198 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008199 } else if (Op1.getOpcode() == ISD::Constant) {
8200 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008201 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008202 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008203
8204 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008205 LHS = AndLHS.getOperand(0);
8206 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008207 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008208
8209 // Use BT if the immediate can't be encoded in a TEST instruction.
8210 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8211 LHS = AndLHS;
8212 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8213 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008214 }
Evan Cheng0488db92007-09-25 01:57:46 +00008215
Evan Chengd40d03e2010-01-06 19:38:29 +00008216 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008217 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008218 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008219 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008220 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008221 // Also promote i16 to i32 for performance / code size reason.
8222 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008223 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008224 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008225
Evan Chengd40d03e2010-01-06 19:38:29 +00008226 // If the operand types disagree, extend the shift amount to match. Since
8227 // BT ignores high bits (like shifts) we can use anyextend.
8228 if (LHS.getValueType() != RHS.getValueType())
8229 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008230
Evan Chengd40d03e2010-01-06 19:38:29 +00008231 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8232 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8233 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8234 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008235 }
8236
Evan Cheng54de3ea2010-01-05 06:52:31 +00008237 return SDValue();
8238}
8239
Dan Gohmand858e902010-04-17 15:26:15 +00008240SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008241
8242 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8243
Evan Cheng54de3ea2010-01-05 06:52:31 +00008244 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8245 SDValue Op0 = Op.getOperand(0);
8246 SDValue Op1 = Op.getOperand(1);
8247 DebugLoc dl = Op.getDebugLoc();
8248 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8249
8250 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008251 // Lower (X & (1 << N)) == 0 to BT(X, N).
8252 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8253 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008254 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008255 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008256 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008257 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8258 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8259 if (NewSetCC.getNode())
8260 return NewSetCC;
8261 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008262
Chris Lattner481eebc2010-12-19 21:23:48 +00008263 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8264 // these.
8265 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008266 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008267 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8268 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008269
Chris Lattner481eebc2010-12-19 21:23:48 +00008270 // If the input is a setcc, then reuse the input setcc or use a new one with
8271 // the inverted condition.
8272 if (Op0.getOpcode() == X86ISD::SETCC) {
8273 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8274 bool Invert = (CC == ISD::SETNE) ^
8275 cast<ConstantSDNode>(Op1)->isNullValue();
8276 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008277
Evan Cheng2c755ba2010-02-27 07:36:59 +00008278 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008279 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8280 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8281 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008282 }
8283
Evan Chenge5b51ac2010-04-17 06:13:15 +00008284 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008285 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008286 if (X86CC == X86::COND_INVALID)
8287 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008288
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008289 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008290 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008291 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008292}
8293
Craig Topper89af15e2011-09-18 08:03:58 +00008294// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008295// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008296static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008297 EVT VT = Op.getValueType();
8298
Duncan Sands28b77e92011-09-06 19:07:46 +00008299 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008300 "Unsupported value type for operation");
8301
8302 int NumElems = VT.getVectorNumElements();
8303 DebugLoc dl = Op.getDebugLoc();
8304 SDValue CC = Op.getOperand(2);
8305 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8306 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8307
8308 // Extract the LHS vectors
8309 SDValue LHS = Op.getOperand(0);
8310 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8311 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8312
8313 // Extract the RHS vectors
8314 SDValue RHS = Op.getOperand(1);
8315 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8316 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8317
8318 // Issue the operation on the smaller types and concatenate the result back
8319 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8320 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8321 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8322 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8323 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8324}
8325
8326
Dan Gohmand858e902010-04-17 15:26:15 +00008327SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008328 SDValue Cond;
8329 SDValue Op0 = Op.getOperand(0);
8330 SDValue Op1 = Op.getOperand(1);
8331 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008332 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008333 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8334 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008335 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008336
8337 if (isFP) {
8338 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008339 EVT EltVT = Op0.getValueType().getVectorElementType();
8340 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8341
8342 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008343 bool Swap = false;
8344
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008345 // SSE Condition code mapping:
8346 // 0 - EQ
8347 // 1 - LT
8348 // 2 - LE
8349 // 3 - UNORD
8350 // 4 - NEQ
8351 // 5 - NLT
8352 // 6 - NLE
8353 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008354 switch (SetCCOpcode) {
8355 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008356 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008357 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008358 case ISD::SETOGT:
8359 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008360 case ISD::SETLT:
8361 case ISD::SETOLT: SSECC = 1; break;
8362 case ISD::SETOGE:
8363 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008364 case ISD::SETLE:
8365 case ISD::SETOLE: SSECC = 2; break;
8366 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008367 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008368 case ISD::SETNE: SSECC = 4; break;
8369 case ISD::SETULE: Swap = true;
8370 case ISD::SETUGE: SSECC = 5; break;
8371 case ISD::SETULT: Swap = true;
8372 case ISD::SETUGT: SSECC = 6; break;
8373 case ISD::SETO: SSECC = 7; break;
8374 }
8375 if (Swap)
8376 std::swap(Op0, Op1);
8377
Nate Begemanfb8ead02008-07-25 19:05:58 +00008378 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008379 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008380 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008381 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008382 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8383 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008384 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Craig Topper0a150352011-11-09 08:06:13 +00008385 } else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008386 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008387 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8388 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008389 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008390 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008391 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008392 }
8393 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008394 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008395 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008396
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008397 // Break 256-bit integer vector compare into smaller ones.
Craig Topper0a150352011-11-09 08:06:13 +00008398 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008399 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008400
Nate Begeman30a0de92008-07-17 16:51:19 +00008401 // We are handling one of the integer comparisons here. Since SSE only has
8402 // GT and EQ comparisons for integer, swapping operands and multiple
8403 // operations may be required for some comparisons.
8404 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8405 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008406
Craig Topper0a150352011-11-09 08:06:13 +00008407 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008408 default: break;
Craig Topper0a150352011-11-09 08:06:13 +00008409 case MVT::i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
8410 case MVT::i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
8411 case MVT::i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8412 case MVT::i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008413 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008414
Nate Begeman30a0de92008-07-17 16:51:19 +00008415 switch (SetCCOpcode) {
8416 default: break;
8417 case ISD::SETNE: Invert = true;
8418 case ISD::SETEQ: Opc = EQOpc; break;
8419 case ISD::SETLT: Swap = true;
8420 case ISD::SETGT: Opc = GTOpc; break;
8421 case ISD::SETGE: Swap = true;
8422 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8423 case ISD::SETULT: Swap = true;
8424 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8425 case ISD::SETUGE: Swap = true;
8426 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8427 }
8428 if (Swap)
8429 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008430
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008431 // Check that the operation in question is available (most are plain SSE2,
8432 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topperc0d82852011-11-22 00:44:41 +00008433 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42orAVX())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008434 return SDValue();
Craig Topperc0d82852011-11-22 00:44:41 +00008435 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41orAVX())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008436 return SDValue();
8437
Nate Begeman30a0de92008-07-17 16:51:19 +00008438 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8439 // bits of the inputs before performing those operations.
8440 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008441 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008442 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8443 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008444 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008445 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8446 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008447 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8448 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008449 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008450
Dale Johannesenace16102009-02-03 19:33:06 +00008451 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008452
8453 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008454 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008455 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008456
Nate Begeman30a0de92008-07-17 16:51:19 +00008457 return Result;
8458}
Evan Cheng0488db92007-09-25 01:57:46 +00008459
Evan Cheng370e5342008-12-03 08:38:43 +00008460// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008461static bool isX86LogicalCmp(SDValue Op) {
8462 unsigned Opc = Op.getNode()->getOpcode();
8463 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8464 return true;
8465 if (Op.getResNo() == 1 &&
8466 (Opc == X86ISD::ADD ||
8467 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008468 Opc == X86ISD::ADC ||
8469 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008470 Opc == X86ISD::SMUL ||
8471 Opc == X86ISD::UMUL ||
8472 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008473 Opc == X86ISD::DEC ||
8474 Opc == X86ISD::OR ||
8475 Opc == X86ISD::XOR ||
8476 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008477 return true;
8478
Chris Lattner9637d5b2010-12-05 07:49:54 +00008479 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8480 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008481
Dan Gohman076aee32009-03-04 19:44:21 +00008482 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008483}
8484
Chris Lattnera2b56002010-12-05 01:23:24 +00008485static bool isZero(SDValue V) {
8486 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8487 return C && C->isNullValue();
8488}
8489
Chris Lattner96908b12010-12-05 02:00:51 +00008490static bool isAllOnes(SDValue V) {
8491 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8492 return C && C->isAllOnesValue();
8493}
8494
Dan Gohmand858e902010-04-17 15:26:15 +00008495SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008496 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008497 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008498 SDValue Op1 = Op.getOperand(1);
8499 SDValue Op2 = Op.getOperand(2);
8500 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008501 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008502
Dan Gohman1a492952009-10-20 16:22:37 +00008503 if (Cond.getOpcode() == ISD::SETCC) {
8504 SDValue NewCond = LowerSETCC(Cond, DAG);
8505 if (NewCond.getNode())
8506 Cond = NewCond;
8507 }
Evan Cheng734503b2006-09-11 02:19:56 +00008508
Chris Lattnera2b56002010-12-05 01:23:24 +00008509 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008510 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008511 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008512 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008513 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008514 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8515 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008516 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008517
Chris Lattnera2b56002010-12-05 01:23:24 +00008518 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008519
8520 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008521 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8522 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008523
8524 SDValue CmpOp0 = Cmp.getOperand(0);
8525 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8526 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008527
Chris Lattner96908b12010-12-05 02:00:51 +00008528 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008529 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8530 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008531
Chris Lattner96908b12010-12-05 02:00:51 +00008532 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8533 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008534
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008535 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008536 if (N2C == 0 || !N2C->isNullValue())
8537 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8538 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008539 }
8540 }
8541
Chris Lattnera2b56002010-12-05 01:23:24 +00008542 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008543 if (Cond.getOpcode() == ISD::AND &&
8544 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8545 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008546 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008547 Cond = Cond.getOperand(0);
8548 }
8549
Evan Cheng3f41d662007-10-08 22:16:29 +00008550 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8551 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008552 unsigned CondOpcode = Cond.getOpcode();
8553 if (CondOpcode == X86ISD::SETCC ||
8554 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008555 CC = Cond.getOperand(0);
8556
Dan Gohman475871a2008-07-27 21:46:04 +00008557 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008558 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008559 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008560
Evan Cheng3f41d662007-10-08 22:16:29 +00008561 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008562 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008563 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008564 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008565
Chris Lattnerd1980a52009-03-12 06:52:53 +00008566 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8567 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008568 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008569 addTest = false;
8570 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008571 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8572 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8573 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8574 Cond.getOperand(0).getValueType() != MVT::i8)) {
8575 SDValue LHS = Cond.getOperand(0);
8576 SDValue RHS = Cond.getOperand(1);
8577 unsigned X86Opcode;
8578 unsigned X86Cond;
8579 SDVTList VTs;
8580 switch (CondOpcode) {
8581 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8582 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8583 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8584 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8585 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8586 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8587 default: llvm_unreachable("unexpected overflowing operator");
8588 }
8589 if (CondOpcode == ISD::UMULO)
8590 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8591 MVT::i32);
8592 else
8593 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8594
8595 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8596
8597 if (CondOpcode == ISD::UMULO)
8598 Cond = X86Op.getValue(2);
8599 else
8600 Cond = X86Op.getValue(1);
8601
8602 CC = DAG.getConstant(X86Cond, MVT::i8);
8603 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008604 }
8605
8606 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008607 // Look pass the truncate.
8608 if (Cond.getOpcode() == ISD::TRUNCATE)
8609 Cond = Cond.getOperand(0);
8610
8611 // We know the result of AND is compared against zero. Try to match
8612 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008613 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008614 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008615 if (NewSetCC.getNode()) {
8616 CC = NewSetCC.getOperand(0);
8617 Cond = NewSetCC.getOperand(1);
8618 addTest = false;
8619 }
8620 }
8621 }
8622
8623 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008624 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008625 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008626 }
8627
Benjamin Kramere915ff32010-12-22 23:09:28 +00008628 // a < b ? -1 : 0 -> RES = ~setcc_carry
8629 // a < b ? 0 : -1 -> RES = setcc_carry
8630 // a >= b ? -1 : 0 -> RES = setcc_carry
8631 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8632 if (Cond.getOpcode() == X86ISD::CMP) {
8633 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8634
8635 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8636 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8637 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8638 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8639 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8640 return DAG.getNOT(DL, Res, Res.getValueType());
8641 return Res;
8642 }
8643 }
8644
Evan Cheng0488db92007-09-25 01:57:46 +00008645 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8646 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008647 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008648 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008649 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008650}
8651
Evan Cheng370e5342008-12-03 08:38:43 +00008652// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8653// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8654// from the AND / OR.
8655static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8656 Opc = Op.getOpcode();
8657 if (Opc != ISD::OR && Opc != ISD::AND)
8658 return false;
8659 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8660 Op.getOperand(0).hasOneUse() &&
8661 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8662 Op.getOperand(1).hasOneUse());
8663}
8664
Evan Cheng961d6d42009-02-02 08:19:07 +00008665// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8666// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008667static bool isXor1OfSetCC(SDValue Op) {
8668 if (Op.getOpcode() != ISD::XOR)
8669 return false;
8670 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8671 if (N1C && N1C->getAPIntValue() == 1) {
8672 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8673 Op.getOperand(0).hasOneUse();
8674 }
8675 return false;
8676}
8677
Dan Gohmand858e902010-04-17 15:26:15 +00008678SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008679 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008680 SDValue Chain = Op.getOperand(0);
8681 SDValue Cond = Op.getOperand(1);
8682 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008683 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008684 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008685 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008686
Dan Gohman1a492952009-10-20 16:22:37 +00008687 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008688 // Check for setcc([su]{add,sub,mul}o == 0).
8689 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8690 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8691 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8692 Cond.getOperand(0).getResNo() == 1 &&
8693 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
8694 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
8695 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
8696 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
8697 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
8698 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
8699 Inverted = true;
8700 Cond = Cond.getOperand(0);
8701 } else {
8702 SDValue NewCond = LowerSETCC(Cond, DAG);
8703 if (NewCond.getNode())
8704 Cond = NewCond;
8705 }
Dan Gohman1a492952009-10-20 16:22:37 +00008706 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008707#if 0
8708 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008709 else if (Cond.getOpcode() == X86ISD::ADD ||
8710 Cond.getOpcode() == X86ISD::SUB ||
8711 Cond.getOpcode() == X86ISD::SMUL ||
8712 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008713 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008714#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008715
Evan Chengad9c0a32009-12-15 00:53:42 +00008716 // Look pass (and (setcc_carry (cmp ...)), 1).
8717 if (Cond.getOpcode() == ISD::AND &&
8718 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8719 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008720 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008721 Cond = Cond.getOperand(0);
8722 }
8723
Evan Cheng3f41d662007-10-08 22:16:29 +00008724 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8725 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008726 unsigned CondOpcode = Cond.getOpcode();
8727 if (CondOpcode == X86ISD::SETCC ||
8728 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008729 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008730
Dan Gohman475871a2008-07-27 21:46:04 +00008731 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008732 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008733 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008734 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008735 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008736 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008737 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008738 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008739 default: break;
8740 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008741 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008742 // These can only come from an arithmetic instruction with overflow,
8743 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008744 Cond = Cond.getNode()->getOperand(1);
8745 addTest = false;
8746 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008747 }
Evan Cheng0488db92007-09-25 01:57:46 +00008748 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008749 }
8750 CondOpcode = Cond.getOpcode();
8751 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8752 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8753 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8754 Cond.getOperand(0).getValueType() != MVT::i8)) {
8755 SDValue LHS = Cond.getOperand(0);
8756 SDValue RHS = Cond.getOperand(1);
8757 unsigned X86Opcode;
8758 unsigned X86Cond;
8759 SDVTList VTs;
8760 switch (CondOpcode) {
8761 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8762 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8763 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8764 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8765 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8766 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8767 default: llvm_unreachable("unexpected overflowing operator");
8768 }
8769 if (Inverted)
8770 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
8771 if (CondOpcode == ISD::UMULO)
8772 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8773 MVT::i32);
8774 else
8775 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8776
8777 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
8778
8779 if (CondOpcode == ISD::UMULO)
8780 Cond = X86Op.getValue(2);
8781 else
8782 Cond = X86Op.getValue(1);
8783
8784 CC = DAG.getConstant(X86Cond, MVT::i8);
8785 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00008786 } else {
8787 unsigned CondOpc;
8788 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8789 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008790 if (CondOpc == ISD::OR) {
8791 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8792 // two branches instead of an explicit OR instruction with a
8793 // separate test.
8794 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008795 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008796 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008797 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008798 Chain, Dest, CC, Cmp);
8799 CC = Cond.getOperand(1).getOperand(0);
8800 Cond = Cmp;
8801 addTest = false;
8802 }
8803 } else { // ISD::AND
8804 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8805 // two branches instead of an explicit AND instruction with a
8806 // separate test. However, we only do this if this block doesn't
8807 // have a fall-through edge, because this requires an explicit
8808 // jmp when the condition is false.
8809 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008810 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008811 Op.getNode()->hasOneUse()) {
8812 X86::CondCode CCode =
8813 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8814 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008815 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008816 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008817 // Look for an unconditional branch following this conditional branch.
8818 // We need this because we need to reverse the successors in order
8819 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008820 if (User->getOpcode() == ISD::BR) {
8821 SDValue FalseBB = User->getOperand(1);
8822 SDNode *NewBR =
8823 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008824 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008825 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008826 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008827
Dale Johannesene4d209d2009-02-03 20:21:25 +00008828 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008829 Chain, Dest, CC, Cmp);
8830 X86::CondCode CCode =
8831 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8832 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008833 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008834 Cond = Cmp;
8835 addTest = false;
8836 }
8837 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008838 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008839 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8840 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8841 // It should be transformed during dag combiner except when the condition
8842 // is set by a arithmetics with overflow node.
8843 X86::CondCode CCode =
8844 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8845 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008846 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008847 Cond = Cond.getOperand(0).getOperand(1);
8848 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00008849 } else if (Cond.getOpcode() == ISD::SETCC &&
8850 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
8851 // For FCMP_OEQ, we can emit
8852 // two branches instead of an explicit AND instruction with a
8853 // separate test. However, we only do this if this block doesn't
8854 // have a fall-through edge, because this requires an explicit
8855 // jmp when the condition is false.
8856 if (Op.getNode()->hasOneUse()) {
8857 SDNode *User = *Op.getNode()->use_begin();
8858 // Look for an unconditional branch following this conditional branch.
8859 // We need this because we need to reverse the successors in order
8860 // to implement FCMP_OEQ.
8861 if (User->getOpcode() == ISD::BR) {
8862 SDValue FalseBB = User->getOperand(1);
8863 SDNode *NewBR =
8864 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8865 assert(NewBR == User);
8866 (void)NewBR;
8867 Dest = FalseBB;
8868
8869 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
8870 Cond.getOperand(0), Cond.getOperand(1));
8871 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8872 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8873 Chain, Dest, CC, Cmp);
8874 CC = DAG.getConstant(X86::COND_P, MVT::i8);
8875 Cond = Cmp;
8876 addTest = false;
8877 }
8878 }
8879 } else if (Cond.getOpcode() == ISD::SETCC &&
8880 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
8881 // For FCMP_UNE, we can emit
8882 // two branches instead of an explicit AND instruction with a
8883 // separate test. However, we only do this if this block doesn't
8884 // have a fall-through edge, because this requires an explicit
8885 // jmp when the condition is false.
8886 if (Op.getNode()->hasOneUse()) {
8887 SDNode *User = *Op.getNode()->use_begin();
8888 // Look for an unconditional branch following this conditional branch.
8889 // We need this because we need to reverse the successors in order
8890 // to implement FCMP_UNE.
8891 if (User->getOpcode() == ISD::BR) {
8892 SDValue FalseBB = User->getOperand(1);
8893 SDNode *NewBR =
8894 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8895 assert(NewBR == User);
8896 (void)NewBR;
8897
8898 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
8899 Cond.getOperand(0), Cond.getOperand(1));
8900 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8901 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8902 Chain, Dest, CC, Cmp);
8903 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
8904 Cond = Cmp;
8905 addTest = false;
8906 Dest = FalseBB;
8907 }
8908 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008909 }
Evan Cheng0488db92007-09-25 01:57:46 +00008910 }
8911
8912 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008913 // Look pass the truncate.
8914 if (Cond.getOpcode() == ISD::TRUNCATE)
8915 Cond = Cond.getOperand(0);
8916
8917 // We know the result of AND is compared against zero. Try to match
8918 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008919 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008920 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8921 if (NewSetCC.getNode()) {
8922 CC = NewSetCC.getOperand(0);
8923 Cond = NewSetCC.getOperand(1);
8924 addTest = false;
8925 }
8926 }
8927 }
8928
8929 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008930 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008931 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008932 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008933 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008934 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008935}
8936
Anton Korobeynikove060b532007-04-17 19:34:00 +00008937
8938// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8939// Calls to _alloca is needed to probe the stack when allocating more than 4k
8940// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8941// that the guard pages used by the OS virtual memory manager are allocated in
8942// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008943SDValue
8944X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008945 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008946 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008947 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008948 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008949 "are being used");
8950 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008951 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008952
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008953 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008954 SDValue Chain = Op.getOperand(0);
8955 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008956 // FIXME: Ensure alignment here
8957
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008958 bool Is64Bit = Subtarget->is64Bit();
8959 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008960
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008961 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008962 MachineFunction &MF = DAG.getMachineFunction();
8963 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008964
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008965 if (Is64Bit) {
8966 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008967 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008968 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008969
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008970 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8971 I != E; I++)
8972 if (I->hasNestAttr())
8973 report_fatal_error("Cannot use segmented stacks with functions that "
8974 "have nested arguments.");
8975 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008976
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008977 const TargetRegisterClass *AddrRegClass =
8978 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8979 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8980 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8981 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8982 DAG.getRegister(Vreg, SPTy));
8983 SDValue Ops1[2] = { Value, Chain };
8984 return DAG.getMergeValues(Ops1, 2, dl);
8985 } else {
8986 SDValue Flag;
8987 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008988
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008989 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8990 Flag = Chain.getValue(1);
8991 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008992
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008993 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
8994 Flag = Chain.getValue(1);
8995
8996 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
8997
8998 SDValue Ops1[2] = { Chain.getValue(0), Chain };
8999 return DAG.getMergeValues(Ops1, 2, dl);
9000 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009001}
9002
Dan Gohmand858e902010-04-17 15:26:15 +00009003SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009004 MachineFunction &MF = DAG.getMachineFunction();
9005 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9006
Dan Gohman69de1932008-02-06 22:27:42 +00009007 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009008 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009009
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009010 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009011 // vastart just stores the address of the VarArgsFrameIndex slot into the
9012 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009013 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9014 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009015 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9016 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009017 }
9018
9019 // __va_list_tag:
9020 // gp_offset (0 - 6 * 8)
9021 // fp_offset (48 - 48 + 8 * 16)
9022 // overflow_arg_area (point to parameters coming in memory).
9023 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009024 SmallVector<SDValue, 8> MemOps;
9025 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009026 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009027 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009028 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9029 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009030 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009031 MemOps.push_back(Store);
9032
9033 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009034 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009035 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009036 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009037 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9038 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009039 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009040 MemOps.push_back(Store);
9041
9042 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009043 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009044 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009045 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9046 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009047 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9048 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009049 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009050 MemOps.push_back(Store);
9051
9052 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009053 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009054 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009055 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9056 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009057 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9058 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009059 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009060 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009061 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009062}
9063
Dan Gohmand858e902010-04-17 15:26:15 +00009064SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009065 assert(Subtarget->is64Bit() &&
9066 "LowerVAARG only handles 64-bit va_arg!");
9067 assert((Subtarget->isTargetLinux() ||
9068 Subtarget->isTargetDarwin()) &&
9069 "Unhandled target in LowerVAARG");
9070 assert(Op.getNode()->getNumOperands() == 4);
9071 SDValue Chain = Op.getOperand(0);
9072 SDValue SrcPtr = Op.getOperand(1);
9073 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9074 unsigned Align = Op.getConstantOperandVal(3);
9075 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009076
Dan Gohman320afb82010-10-12 18:00:49 +00009077 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009078 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009079 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9080 uint8_t ArgMode;
9081
9082 // Decide which area this value should be read from.
9083 // TODO: Implement the AMD64 ABI in its entirety. This simple
9084 // selection mechanism works only for the basic types.
9085 if (ArgVT == MVT::f80) {
9086 llvm_unreachable("va_arg for f80 not yet implemented");
9087 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9088 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9089 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9090 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9091 } else {
9092 llvm_unreachable("Unhandled argument type in LowerVAARG");
9093 }
9094
9095 if (ArgMode == 2) {
9096 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009097 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009098 !(DAG.getMachineFunction()
9099 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009100 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009101 }
9102
9103 // Insert VAARG_64 node into the DAG
9104 // VAARG_64 returns two values: Variable Argument Address, Chain
9105 SmallVector<SDValue, 11> InstOps;
9106 InstOps.push_back(Chain);
9107 InstOps.push_back(SrcPtr);
9108 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9109 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9110 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9111 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9112 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9113 VTs, &InstOps[0], InstOps.size(),
9114 MVT::i64,
9115 MachinePointerInfo(SV),
9116 /*Align=*/0,
9117 /*Volatile=*/false,
9118 /*ReadMem=*/true,
9119 /*WriteMem=*/true);
9120 Chain = VAARG.getValue(1);
9121
9122 // Load the next argument and return it
9123 return DAG.getLoad(ArgVT, dl,
9124 Chain,
9125 VAARG,
9126 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009127 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009128}
9129
Dan Gohmand858e902010-04-17 15:26:15 +00009130SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009131 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009132 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009133 SDValue Chain = Op.getOperand(0);
9134 SDValue DstPtr = Op.getOperand(1);
9135 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009136 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9137 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009138 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009139
Chris Lattnere72f2022010-09-21 05:40:29 +00009140 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009141 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009142 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009143 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009144}
9145
Dan Gohman475871a2008-07-27 21:46:04 +00009146SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009147X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009148 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009149 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009150 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009151 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009152 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009153 case Intrinsic::x86_sse_comieq_ss:
9154 case Intrinsic::x86_sse_comilt_ss:
9155 case Intrinsic::x86_sse_comile_ss:
9156 case Intrinsic::x86_sse_comigt_ss:
9157 case Intrinsic::x86_sse_comige_ss:
9158 case Intrinsic::x86_sse_comineq_ss:
9159 case Intrinsic::x86_sse_ucomieq_ss:
9160 case Intrinsic::x86_sse_ucomilt_ss:
9161 case Intrinsic::x86_sse_ucomile_ss:
9162 case Intrinsic::x86_sse_ucomigt_ss:
9163 case Intrinsic::x86_sse_ucomige_ss:
9164 case Intrinsic::x86_sse_ucomineq_ss:
9165 case Intrinsic::x86_sse2_comieq_sd:
9166 case Intrinsic::x86_sse2_comilt_sd:
9167 case Intrinsic::x86_sse2_comile_sd:
9168 case Intrinsic::x86_sse2_comigt_sd:
9169 case Intrinsic::x86_sse2_comige_sd:
9170 case Intrinsic::x86_sse2_comineq_sd:
9171 case Intrinsic::x86_sse2_ucomieq_sd:
9172 case Intrinsic::x86_sse2_ucomilt_sd:
9173 case Intrinsic::x86_sse2_ucomile_sd:
9174 case Intrinsic::x86_sse2_ucomigt_sd:
9175 case Intrinsic::x86_sse2_ucomige_sd:
9176 case Intrinsic::x86_sse2_ucomineq_sd: {
9177 unsigned Opc = 0;
9178 ISD::CondCode CC = ISD::SETCC_INVALID;
9179 switch (IntNo) {
9180 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009181 case Intrinsic::x86_sse_comieq_ss:
9182 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009183 Opc = X86ISD::COMI;
9184 CC = ISD::SETEQ;
9185 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009186 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009187 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009188 Opc = X86ISD::COMI;
9189 CC = ISD::SETLT;
9190 break;
9191 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009192 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009193 Opc = X86ISD::COMI;
9194 CC = ISD::SETLE;
9195 break;
9196 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009197 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009198 Opc = X86ISD::COMI;
9199 CC = ISD::SETGT;
9200 break;
9201 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009202 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009203 Opc = X86ISD::COMI;
9204 CC = ISD::SETGE;
9205 break;
9206 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009207 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009208 Opc = X86ISD::COMI;
9209 CC = ISD::SETNE;
9210 break;
9211 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009212 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009213 Opc = X86ISD::UCOMI;
9214 CC = ISD::SETEQ;
9215 break;
9216 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009217 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009218 Opc = X86ISD::UCOMI;
9219 CC = ISD::SETLT;
9220 break;
9221 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009222 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009223 Opc = X86ISD::UCOMI;
9224 CC = ISD::SETLE;
9225 break;
9226 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009227 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009228 Opc = X86ISD::UCOMI;
9229 CC = ISD::SETGT;
9230 break;
9231 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009232 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009233 Opc = X86ISD::UCOMI;
9234 CC = ISD::SETGE;
9235 break;
9236 case Intrinsic::x86_sse_ucomineq_ss:
9237 case Intrinsic::x86_sse2_ucomineq_sd:
9238 Opc = X86ISD::UCOMI;
9239 CC = ISD::SETNE;
9240 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009241 }
Evan Cheng734503b2006-09-11 02:19:56 +00009242
Dan Gohman475871a2008-07-27 21:46:04 +00009243 SDValue LHS = Op.getOperand(1);
9244 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009245 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009246 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009247 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9248 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9249 DAG.getConstant(X86CC, MVT::i8), Cond);
9250 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009251 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009252 // Arithmetic intrinsics.
9253 case Intrinsic::x86_sse3_hadd_ps:
9254 case Intrinsic::x86_sse3_hadd_pd:
9255 case Intrinsic::x86_avx_hadd_ps_256:
9256 case Intrinsic::x86_avx_hadd_pd_256:
9257 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9258 Op.getOperand(1), Op.getOperand(2));
9259 case Intrinsic::x86_sse3_hsub_ps:
9260 case Intrinsic::x86_sse3_hsub_pd:
9261 case Intrinsic::x86_avx_hsub_ps_256:
9262 case Intrinsic::x86_avx_hsub_pd_256:
9263 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9264 Op.getOperand(1), Op.getOperand(2));
Craig Topper98fc7292011-11-19 17:46:46 +00009265 case Intrinsic::x86_avx2_psllv_d:
9266 case Intrinsic::x86_avx2_psllv_q:
9267 case Intrinsic::x86_avx2_psllv_d_256:
9268 case Intrinsic::x86_avx2_psllv_q_256:
9269 return DAG.getNode(ISD::SHL, dl, Op.getValueType(),
9270 Op.getOperand(1), Op.getOperand(2));
9271 case Intrinsic::x86_avx2_psrlv_d:
9272 case Intrinsic::x86_avx2_psrlv_q:
9273 case Intrinsic::x86_avx2_psrlv_d_256:
9274 case Intrinsic::x86_avx2_psrlv_q_256:
9275 return DAG.getNode(ISD::SRL, dl, Op.getValueType(),
9276 Op.getOperand(1), Op.getOperand(2));
9277 case Intrinsic::x86_avx2_psrav_d:
9278 case Intrinsic::x86_avx2_psrav_d_256:
9279 return DAG.getNode(ISD::SRA, dl, Op.getValueType(),
9280 Op.getOperand(1), Op.getOperand(2));
9281
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009282 // ptest and testp intrinsics. The intrinsic these come from are designed to
9283 // return an integer value, not just an instruction so lower it to the ptest
9284 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009285 case Intrinsic::x86_sse41_ptestz:
9286 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009287 case Intrinsic::x86_sse41_ptestnzc:
9288 case Intrinsic::x86_avx_ptestz_256:
9289 case Intrinsic::x86_avx_ptestc_256:
9290 case Intrinsic::x86_avx_ptestnzc_256:
9291 case Intrinsic::x86_avx_vtestz_ps:
9292 case Intrinsic::x86_avx_vtestc_ps:
9293 case Intrinsic::x86_avx_vtestnzc_ps:
9294 case Intrinsic::x86_avx_vtestz_pd:
9295 case Intrinsic::x86_avx_vtestc_pd:
9296 case Intrinsic::x86_avx_vtestnzc_pd:
9297 case Intrinsic::x86_avx_vtestz_ps_256:
9298 case Intrinsic::x86_avx_vtestc_ps_256:
9299 case Intrinsic::x86_avx_vtestnzc_ps_256:
9300 case Intrinsic::x86_avx_vtestz_pd_256:
9301 case Intrinsic::x86_avx_vtestc_pd_256:
9302 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9303 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009304 unsigned X86CC = 0;
9305 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009306 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009307 case Intrinsic::x86_avx_vtestz_ps:
9308 case Intrinsic::x86_avx_vtestz_pd:
9309 case Intrinsic::x86_avx_vtestz_ps_256:
9310 case Intrinsic::x86_avx_vtestz_pd_256:
9311 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009312 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009313 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009314 // ZF = 1
9315 X86CC = X86::COND_E;
9316 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009317 case Intrinsic::x86_avx_vtestc_ps:
9318 case Intrinsic::x86_avx_vtestc_pd:
9319 case Intrinsic::x86_avx_vtestc_ps_256:
9320 case Intrinsic::x86_avx_vtestc_pd_256:
9321 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009322 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009323 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009324 // CF = 1
9325 X86CC = X86::COND_B;
9326 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009327 case Intrinsic::x86_avx_vtestnzc_ps:
9328 case Intrinsic::x86_avx_vtestnzc_pd:
9329 case Intrinsic::x86_avx_vtestnzc_ps_256:
9330 case Intrinsic::x86_avx_vtestnzc_pd_256:
9331 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009332 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009333 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009334 // ZF and CF = 0
9335 X86CC = X86::COND_A;
9336 break;
9337 }
Eric Christopherfd179292009-08-27 18:07:15 +00009338
Eric Christopher71c67532009-07-29 00:28:05 +00009339 SDValue LHS = Op.getOperand(1);
9340 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009341 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9342 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009343 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9344 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9345 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009346 }
Evan Cheng5759f972008-05-04 09:15:50 +00009347
9348 // Fix vector shift instructions where the last operand is a non-immediate
9349 // i32 value.
Craig Topper7be5dfd2011-11-12 09:58:49 +00009350 case Intrinsic::x86_avx2_pslli_w:
9351 case Intrinsic::x86_avx2_pslli_d:
9352 case Intrinsic::x86_avx2_pslli_q:
9353 case Intrinsic::x86_avx2_psrli_w:
9354 case Intrinsic::x86_avx2_psrli_d:
9355 case Intrinsic::x86_avx2_psrli_q:
9356 case Intrinsic::x86_avx2_psrai_w:
9357 case Intrinsic::x86_avx2_psrai_d:
Evan Cheng5759f972008-05-04 09:15:50 +00009358 case Intrinsic::x86_sse2_pslli_w:
9359 case Intrinsic::x86_sse2_pslli_d:
9360 case Intrinsic::x86_sse2_pslli_q:
9361 case Intrinsic::x86_sse2_psrli_w:
9362 case Intrinsic::x86_sse2_psrli_d:
9363 case Intrinsic::x86_sse2_psrli_q:
9364 case Intrinsic::x86_sse2_psrai_w:
9365 case Intrinsic::x86_sse2_psrai_d:
9366 case Intrinsic::x86_mmx_pslli_w:
9367 case Intrinsic::x86_mmx_pslli_d:
9368 case Intrinsic::x86_mmx_pslli_q:
9369 case Intrinsic::x86_mmx_psrli_w:
9370 case Intrinsic::x86_mmx_psrli_d:
9371 case Intrinsic::x86_mmx_psrli_q:
9372 case Intrinsic::x86_mmx_psrai_w:
9373 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009374 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009375 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009376 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009377
9378 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009379 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009380 switch (IntNo) {
9381 case Intrinsic::x86_sse2_pslli_w:
9382 NewIntNo = Intrinsic::x86_sse2_psll_w;
9383 break;
9384 case Intrinsic::x86_sse2_pslli_d:
9385 NewIntNo = Intrinsic::x86_sse2_psll_d;
9386 break;
9387 case Intrinsic::x86_sse2_pslli_q:
9388 NewIntNo = Intrinsic::x86_sse2_psll_q;
9389 break;
9390 case Intrinsic::x86_sse2_psrli_w:
9391 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9392 break;
9393 case Intrinsic::x86_sse2_psrli_d:
9394 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9395 break;
9396 case Intrinsic::x86_sse2_psrli_q:
9397 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9398 break;
9399 case Intrinsic::x86_sse2_psrai_w:
9400 NewIntNo = Intrinsic::x86_sse2_psra_w;
9401 break;
9402 case Intrinsic::x86_sse2_psrai_d:
9403 NewIntNo = Intrinsic::x86_sse2_psra_d;
9404 break;
Craig Topper7be5dfd2011-11-12 09:58:49 +00009405 case Intrinsic::x86_avx2_pslli_w:
9406 NewIntNo = Intrinsic::x86_avx2_psll_w;
9407 break;
9408 case Intrinsic::x86_avx2_pslli_d:
9409 NewIntNo = Intrinsic::x86_avx2_psll_d;
9410 break;
9411 case Intrinsic::x86_avx2_pslli_q:
9412 NewIntNo = Intrinsic::x86_avx2_psll_q;
9413 break;
9414 case Intrinsic::x86_avx2_psrli_w:
9415 NewIntNo = Intrinsic::x86_avx2_psrl_w;
9416 break;
9417 case Intrinsic::x86_avx2_psrli_d:
9418 NewIntNo = Intrinsic::x86_avx2_psrl_d;
9419 break;
9420 case Intrinsic::x86_avx2_psrli_q:
9421 NewIntNo = Intrinsic::x86_avx2_psrl_q;
9422 break;
9423 case Intrinsic::x86_avx2_psrai_w:
9424 NewIntNo = Intrinsic::x86_avx2_psra_w;
9425 break;
9426 case Intrinsic::x86_avx2_psrai_d:
9427 NewIntNo = Intrinsic::x86_avx2_psra_d;
9428 break;
Evan Cheng5759f972008-05-04 09:15:50 +00009429 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009430 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009431 switch (IntNo) {
9432 case Intrinsic::x86_mmx_pslli_w:
9433 NewIntNo = Intrinsic::x86_mmx_psll_w;
9434 break;
9435 case Intrinsic::x86_mmx_pslli_d:
9436 NewIntNo = Intrinsic::x86_mmx_psll_d;
9437 break;
9438 case Intrinsic::x86_mmx_pslli_q:
9439 NewIntNo = Intrinsic::x86_mmx_psll_q;
9440 break;
9441 case Intrinsic::x86_mmx_psrli_w:
9442 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9443 break;
9444 case Intrinsic::x86_mmx_psrli_d:
9445 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9446 break;
9447 case Intrinsic::x86_mmx_psrli_q:
9448 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9449 break;
9450 case Intrinsic::x86_mmx_psrai_w:
9451 NewIntNo = Intrinsic::x86_mmx_psra_w;
9452 break;
9453 case Intrinsic::x86_mmx_psrai_d:
9454 NewIntNo = Intrinsic::x86_mmx_psra_d;
9455 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009456 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009457 }
9458 break;
9459 }
9460 }
Mon P Wangefa42202009-09-03 19:56:25 +00009461
9462 // The vector shift intrinsics with scalars uses 32b shift amounts but
9463 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9464 // to be zero.
9465 SDValue ShOps[4];
9466 ShOps[0] = ShAmt;
9467 ShOps[1] = DAG.getConstant(0, MVT::i32);
9468 if (ShAmtVT == MVT::v4i32) {
9469 ShOps[2] = DAG.getUNDEF(MVT::i32);
9470 ShOps[3] = DAG.getUNDEF(MVT::i32);
9471 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9472 } else {
9473 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009474// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009475 }
9476
Owen Andersone50ed302009-08-10 22:56:29 +00009477 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009478 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009479 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009480 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009481 Op.getOperand(1), ShAmt);
9482 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009483 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009484}
Evan Cheng72261582005-12-20 06:22:03 +00009485
Dan Gohmand858e902010-04-17 15:26:15 +00009486SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9487 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009488 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9489 MFI->setReturnAddressIsTaken(true);
9490
Bill Wendling64e87322009-01-16 19:25:27 +00009491 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009492 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009493
9494 if (Depth > 0) {
9495 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9496 SDValue Offset =
9497 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009498 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009499 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009500 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009501 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009502 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009503 }
9504
9505 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009506 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009507 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009508 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009509}
9510
Dan Gohmand858e902010-04-17 15:26:15 +00009511SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009512 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9513 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009514
Owen Andersone50ed302009-08-10 22:56:29 +00009515 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009516 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009517 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9518 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009519 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009520 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009521 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9522 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009523 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009524 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009525}
9526
Dan Gohman475871a2008-07-27 21:46:04 +00009527SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009528 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009529 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009530}
9531
Dan Gohmand858e902010-04-17 15:26:15 +00009532SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009533 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009534 SDValue Chain = Op.getOperand(0);
9535 SDValue Offset = Op.getOperand(1);
9536 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009537 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009538
Dan Gohmand8816272010-08-11 18:14:00 +00009539 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9540 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9541 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009542 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009543
Dan Gohmand8816272010-08-11 18:14:00 +00009544 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9545 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009546 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009547 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9548 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009549 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009550 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009551
Dale Johannesene4d209d2009-02-03 20:21:25 +00009552 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009553 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009554 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009555}
9556
Duncan Sands4a544a72011-09-06 13:37:06 +00009557SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9558 SelectionDAG &DAG) const {
9559 return Op.getOperand(0);
9560}
9561
9562SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9563 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009564 SDValue Root = Op.getOperand(0);
9565 SDValue Trmp = Op.getOperand(1); // trampoline
9566 SDValue FPtr = Op.getOperand(2); // nested function
9567 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009568 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009569
Dan Gohman69de1932008-02-06 22:27:42 +00009570 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009571
9572 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009573 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009574
9575 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009576 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9577 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009578
Evan Cheng0e6a0522011-07-18 20:57:22 +00009579 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9580 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009581
9582 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9583
9584 // Load the pointer to the nested function into R11.
9585 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009586 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009587 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009588 Addr, MachinePointerInfo(TrmpAddr),
9589 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009590
Owen Anderson825b72b2009-08-11 20:47:22 +00009591 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9592 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009593 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9594 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009595 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009596
9597 // Load the 'nest' parameter value into R10.
9598 // R10 is specified in X86CallingConv.td
9599 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009600 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9601 DAG.getConstant(10, MVT::i64));
9602 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009603 Addr, MachinePointerInfo(TrmpAddr, 10),
9604 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009605
Owen Anderson825b72b2009-08-11 20:47:22 +00009606 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9607 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009608 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9609 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009610 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009611
9612 // Jump to the nested function.
9613 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009614 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9615 DAG.getConstant(20, MVT::i64));
9616 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009617 Addr, MachinePointerInfo(TrmpAddr, 20),
9618 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009619
9620 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009621 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9622 DAG.getConstant(22, MVT::i64));
9623 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009624 MachinePointerInfo(TrmpAddr, 22),
9625 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009626
Duncan Sands4a544a72011-09-06 13:37:06 +00009627 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009628 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009629 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009630 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009631 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009632 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009633
9634 switch (CC) {
9635 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009636 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009637 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009638 case CallingConv::X86_StdCall: {
9639 // Pass 'nest' parameter in ECX.
9640 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009641 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009642
9643 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009644 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009645 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009646
Chris Lattner58d74912008-03-12 17:45:29 +00009647 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009648 unsigned InRegCount = 0;
9649 unsigned Idx = 1;
9650
9651 for (FunctionType::param_iterator I = FTy->param_begin(),
9652 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009653 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009654 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009655 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009656
9657 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009658 report_fatal_error("Nest register in use - reduce number of inreg"
9659 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009660 }
9661 }
9662 break;
9663 }
9664 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009665 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009666 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009667 // Pass 'nest' parameter in EAX.
9668 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009669 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009670 break;
9671 }
9672
Dan Gohman475871a2008-07-27 21:46:04 +00009673 SDValue OutChains[4];
9674 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009675
Owen Anderson825b72b2009-08-11 20:47:22 +00009676 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9677 DAG.getConstant(10, MVT::i32));
9678 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009679
Chris Lattnera62fe662010-02-05 19:20:30 +00009680 // This is storing the opcode for MOV32ri.
9681 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009682 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009683 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009684 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009685 Trmp, MachinePointerInfo(TrmpAddr),
9686 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009687
Owen Anderson825b72b2009-08-11 20:47:22 +00009688 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9689 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009690 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9691 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009692 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009693
Chris Lattnera62fe662010-02-05 19:20:30 +00009694 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009695 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9696 DAG.getConstant(5, MVT::i32));
9697 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009698 MachinePointerInfo(TrmpAddr, 5),
9699 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009700
Owen Anderson825b72b2009-08-11 20:47:22 +00009701 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9702 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009703 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9704 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009705 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009706
Duncan Sands4a544a72011-09-06 13:37:06 +00009707 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009708 }
9709}
9710
Dan Gohmand858e902010-04-17 15:26:15 +00009711SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9712 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009713 /*
9714 The rounding mode is in bits 11:10 of FPSR, and has the following
9715 settings:
9716 00 Round to nearest
9717 01 Round to -inf
9718 10 Round to +inf
9719 11 Round to 0
9720
9721 FLT_ROUNDS, on the other hand, expects the following:
9722 -1 Undefined
9723 0 Round to 0
9724 1 Round to nearest
9725 2 Round to +inf
9726 3 Round to -inf
9727
9728 To perform the conversion, we do:
9729 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9730 */
9731
9732 MachineFunction &MF = DAG.getMachineFunction();
9733 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009734 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009735 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009736 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009737 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009738
9739 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009740 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009741 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009742
Michael J. Spencerec38de22010-10-10 22:04:20 +00009743
Chris Lattner2156b792010-09-22 01:11:26 +00009744 MachineMemOperand *MMO =
9745 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9746 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009747
Chris Lattner2156b792010-09-22 01:11:26 +00009748 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9749 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9750 DAG.getVTList(MVT::Other),
9751 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009752
9753 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009754 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009755 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009756
9757 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009758 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009759 DAG.getNode(ISD::SRL, DL, MVT::i16,
9760 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009761 CWD, DAG.getConstant(0x800, MVT::i16)),
9762 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009763 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009764 DAG.getNode(ISD::SRL, DL, MVT::i16,
9765 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009766 CWD, DAG.getConstant(0x400, MVT::i16)),
9767 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009768
Dan Gohman475871a2008-07-27 21:46:04 +00009769 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009770 DAG.getNode(ISD::AND, DL, MVT::i16,
9771 DAG.getNode(ISD::ADD, DL, MVT::i16,
9772 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009773 DAG.getConstant(1, MVT::i16)),
9774 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009775
9776
Duncan Sands83ec4b62008-06-06 12:08:01 +00009777 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009778 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009779}
9780
Dan Gohmand858e902010-04-17 15:26:15 +00009781SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009782 EVT VT = Op.getValueType();
9783 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009784 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009785 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009786
9787 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009788 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009789 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009790 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009791 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009792 }
Evan Cheng18efe262007-12-14 02:13:44 +00009793
Evan Cheng152804e2007-12-14 08:30:15 +00009794 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009795 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009796 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009797
9798 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009799 SDValue Ops[] = {
9800 Op,
9801 DAG.getConstant(NumBits+NumBits-1, OpVT),
9802 DAG.getConstant(X86::COND_E, MVT::i8),
9803 Op.getValue(1)
9804 };
9805 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009806
9807 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009808 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009809
Owen Anderson825b72b2009-08-11 20:47:22 +00009810 if (VT == MVT::i8)
9811 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009812 return Op;
9813}
9814
Dan Gohmand858e902010-04-17 15:26:15 +00009815SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009816 EVT VT = Op.getValueType();
9817 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009818 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009819 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009820
9821 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009822 if (VT == MVT::i8) {
9823 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009824 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009825 }
Evan Cheng152804e2007-12-14 08:30:15 +00009826
9827 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009828 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009829 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009830
9831 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009832 SDValue Ops[] = {
9833 Op,
9834 DAG.getConstant(NumBits, OpVT),
9835 DAG.getConstant(X86::COND_E, MVT::i8),
9836 Op.getValue(1)
9837 };
9838 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009839
Owen Anderson825b72b2009-08-11 20:47:22 +00009840 if (VT == MVT::i8)
9841 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009842 return Op;
9843}
9844
Craig Topper13894fa2011-08-24 06:14:18 +00009845// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9846// ones, and then concatenate the result back.
9847static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009848 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009849
9850 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9851 "Unsupported value type for operation");
9852
9853 int NumElems = VT.getVectorNumElements();
9854 DebugLoc dl = Op.getDebugLoc();
9855 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9856 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9857
9858 // Extract the LHS vectors
9859 SDValue LHS = Op.getOperand(0);
9860 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9861 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9862
9863 // Extract the RHS vectors
9864 SDValue RHS = Op.getOperand(1);
9865 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9866 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9867
9868 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9869 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9870
9871 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9872 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9873 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9874}
9875
9876SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9877 assert(Op.getValueType().getSizeInBits() == 256 &&
9878 Op.getValueType().isInteger() &&
9879 "Only handle AVX 256-bit vector integer operation");
9880 return Lower256IntArith(Op, DAG);
9881}
9882
9883SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9884 assert(Op.getValueType().getSizeInBits() == 256 &&
9885 Op.getValueType().isInteger() &&
9886 "Only handle AVX 256-bit vector integer operation");
9887 return Lower256IntArith(Op, DAG);
9888}
9889
9890SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9891 EVT VT = Op.getValueType();
9892
9893 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +00009894 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +00009895 return Lower256IntArith(Op, DAG);
9896
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009897 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009898
Craig Topperaaa643c2011-11-09 07:28:55 +00009899 SDValue A = Op.getOperand(0);
9900 SDValue B = Op.getOperand(1);
9901
9902 if (VT == MVT::v4i64) {
9903 assert(Subtarget->hasAVX2() && "Lowering v4i64 multiply requires AVX2");
9904
9905 // ulong2 Ahi = __builtin_ia32_psrlqi256( a, 32);
9906 // ulong2 Bhi = __builtin_ia32_psrlqi256( b, 32);
9907 // ulong2 AloBlo = __builtin_ia32_pmuludq256( a, b );
9908 // ulong2 AloBhi = __builtin_ia32_pmuludq256( a, Bhi );
9909 // ulong2 AhiBlo = __builtin_ia32_pmuludq256( Ahi, b );
9910 //
9911 // AloBhi = __builtin_ia32_psllqi256( AloBhi, 32 );
9912 // AhiBlo = __builtin_ia32_psllqi256( AhiBlo, 32 );
9913 // return AloBlo + AloBhi + AhiBlo;
9914
9915 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9916 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
9917 A, DAG.getConstant(32, MVT::i32));
9918 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9919 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
9920 B, DAG.getConstant(32, MVT::i32));
9921 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9922 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9923 A, B);
9924 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9925 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9926 A, Bhi);
9927 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9928 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9929 Ahi, B);
9930 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9931 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
9932 AloBhi, DAG.getConstant(32, MVT::i32));
9933 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9934 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
9935 AhiBlo, DAG.getConstant(32, MVT::i32));
9936 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9937 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
9938 return Res;
9939 }
9940
9941 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
9942
Mon P Wangaf9b9522008-12-18 21:42:19 +00009943 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9944 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9945 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9946 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9947 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9948 //
9949 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9950 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9951 // return AloBlo + AloBhi + AhiBlo;
9952
Dale Johannesene4d209d2009-02-03 20:21:25 +00009953 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009954 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9955 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009956 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009957 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9958 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009959 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009960 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009961 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009962 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009963 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009964 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009965 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009966 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009967 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009968 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009969 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9970 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009971 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009972 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9973 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009974 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9975 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009976 return Res;
9977}
9978
Nadav Rotem43012222011-05-11 08:12:09 +00009979SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
9980
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009981 EVT VT = Op.getValueType();
9982 DebugLoc dl = Op.getDebugLoc();
9983 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009984 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009985 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009986
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00009987 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009988 return SDValue();
9989
Nadav Rotem43012222011-05-11 08:12:09 +00009990 // Optimize shl/srl/sra with constant shift amount.
9991 if (isSplatVector(Amt.getNode())) {
9992 SDValue SclrAmt = Amt->getOperand(0);
9993 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
9994 uint64_t ShiftAmt = C->getZExtValue();
9995
Benjamin Kramerdade3c12011-10-30 17:31:21 +00009996 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SHL) {
9997 // Make a large shift.
9998 SDValue SHL =
9999 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10000 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10001 R, DAG.getConstant(ShiftAmt, MVT::i32));
10002 // Zero out the rightmost bits.
10003 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10004 MVT::i8));
10005 return DAG.getNode(ISD::AND, dl, VT, SHL,
10006 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10007 }
10008
Nadav Rotem43012222011-05-11 08:12:09 +000010009 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
10010 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10011 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10012 R, DAG.getConstant(ShiftAmt, MVT::i32));
10013
10014 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
10015 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10016 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10017 R, DAG.getConstant(ShiftAmt, MVT::i32));
10018
10019 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
10020 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10021 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10022 R, DAG.getConstant(ShiftAmt, MVT::i32));
10023
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010024 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRL) {
10025 // Make a large shift.
10026 SDValue SRL =
10027 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10028 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10029 R, DAG.getConstant(ShiftAmt, MVT::i32));
10030 // Zero out the leftmost bits.
10031 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10032 MVT::i8));
10033 return DAG.getNode(ISD::AND, dl, VT, SRL,
10034 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10035 }
10036
Nadav Rotem43012222011-05-11 08:12:09 +000010037 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
10038 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10039 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10040 R, DAG.getConstant(ShiftAmt, MVT::i32));
10041
10042 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
10043 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10044 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
10045 R, DAG.getConstant(ShiftAmt, MVT::i32));
10046
10047 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
10048 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10049 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10050 R, DAG.getConstant(ShiftAmt, MVT::i32));
10051
10052 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
10053 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10054 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
10055 R, DAG.getConstant(ShiftAmt, MVT::i32));
10056
10057 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
10058 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10059 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
10060 R, DAG.getConstant(ShiftAmt, MVT::i32));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010061
10062 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRA) {
10063 if (ShiftAmt == 7) {
10064 // R s>> 7 === R s< 0
10065 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10066 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10067 }
10068
10069 // R s>> a === ((R u>> a) ^ m) - m
10070 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10071 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10072 MVT::i8));
10073 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10074 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10075 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10076 return Res;
10077 }
Craig Topper46154eb2011-11-11 07:39:23 +000010078
Craig Topper0d86d462011-11-20 00:12:05 +000010079 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10080 if (Op.getOpcode() == ISD::SHL) {
10081 // Make a large shift.
10082 SDValue SHL =
10083 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10084 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
10085 R, DAG.getConstant(ShiftAmt, MVT::i32));
10086 // Zero out the rightmost bits.
10087 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10088 MVT::i8));
10089 return DAG.getNode(ISD::AND, dl, VT, SHL,
10090 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010091 }
Craig Topper0d86d462011-11-20 00:12:05 +000010092 if (Op.getOpcode() == ISD::SRL) {
10093 // Make a large shift.
10094 SDValue SRL =
10095 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10096 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
10097 R, DAG.getConstant(ShiftAmt, MVT::i32));
10098 // Zero out the leftmost bits.
10099 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10100 MVT::i8));
10101 return DAG.getNode(ISD::AND, dl, VT, SRL,
10102 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10103 }
10104 if (Op.getOpcode() == ISD::SRA) {
10105 if (ShiftAmt == 7) {
10106 // R s>> 7 === R s< 0
10107 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10108 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10109 }
10110
10111 // R s>> a === ((R u>> a) ^ m) - m
10112 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10113 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10114 MVT::i8));
10115 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10116 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10117 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10118 return Res;
10119 }
10120 }
Nadav Rotem43012222011-05-11 08:12:09 +000010121 }
10122 }
10123
10124 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010125 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010126 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10127 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10128 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
10129
10130 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010131
Nate Begeman51409212010-07-28 00:21:48 +000010132 std::vector<Constant*> CV(4, CI);
10133 Constant *C = ConstantVector::get(CV);
10134 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10135 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010136 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010137 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010138
10139 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010140 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010141 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10142 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10143 }
Nadav Rotem43012222011-05-11 08:12:09 +000010144 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010145 // a = a << 5;
10146 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10147 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10148 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
10149
10150 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
10151 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
10152
10153 std::vector<Constant*> CVM1(16, CM1);
10154 std::vector<Constant*> CVM2(16, CM2);
10155 Constant *C = ConstantVector::get(CVM1);
10156 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10157 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010158 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010159 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010160
10161 // r = pblendv(r, psllw(r & (char16)15, 4), a);
10162 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10163 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10164 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10165 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010166 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010167 // a += a
10168 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010169
Nate Begeman51409212010-07-28 00:21:48 +000010170 C = ConstantVector::get(CVM2);
10171 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10172 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010173 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010174 false, false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010175
Nate Begeman51409212010-07-28 00:21:48 +000010176 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10177 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10178 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10179 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10180 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010181 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010182 // a += a
10183 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010184
Nate Begeman51409212010-07-28 00:21:48 +000010185 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010186 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10187 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010188 return R;
10189 }
Craig Topper46154eb2011-11-11 07:39:23 +000010190
10191 // Decompose 256-bit shifts into smaller 128-bit shifts.
10192 if (VT.getSizeInBits() == 256) {
10193 int NumElems = VT.getVectorNumElements();
10194 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10195 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10196
10197 // Extract the two vectors
10198 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10199 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10200 DAG, dl);
10201
10202 // Recreate the shift amount vectors
10203 SDValue Amt1, Amt2;
10204 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10205 // Constant shift amount
10206 SmallVector<SDValue, 4> Amt1Csts;
10207 SmallVector<SDValue, 4> Amt2Csts;
10208 for (int i = 0; i < NumElems/2; ++i)
10209 Amt1Csts.push_back(Amt->getOperand(i));
10210 for (int i = NumElems/2; i < NumElems; ++i)
10211 Amt2Csts.push_back(Amt->getOperand(i));
10212
10213 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10214 &Amt1Csts[0], NumElems/2);
10215 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10216 &Amt2Csts[0], NumElems/2);
10217 } else {
10218 // Variable shift amount
10219 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10220 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10221 DAG, dl);
10222 }
10223
10224 // Issue new vector shifts for the smaller types
10225 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10226 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10227
10228 // Concatenate the result back
10229 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10230 }
10231
Nate Begeman51409212010-07-28 00:21:48 +000010232 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010233}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010234
Dan Gohmand858e902010-04-17 15:26:15 +000010235SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010236 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10237 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010238 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10239 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010240 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010241 SDValue LHS = N->getOperand(0);
10242 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010243 unsigned BaseOp = 0;
10244 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010245 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010246 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010247 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010248 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010249 // A subtract of one will be selected as a INC. Note that INC doesn't
10250 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010251 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10252 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010253 BaseOp = X86ISD::INC;
10254 Cond = X86::COND_O;
10255 break;
10256 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010257 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010258 Cond = X86::COND_O;
10259 break;
10260 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010261 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010262 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010263 break;
10264 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010265 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10266 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010267 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10268 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010269 BaseOp = X86ISD::DEC;
10270 Cond = X86::COND_O;
10271 break;
10272 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010273 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010274 Cond = X86::COND_O;
10275 break;
10276 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010277 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010278 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010279 break;
10280 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010281 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010282 Cond = X86::COND_O;
10283 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010284 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10285 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10286 MVT::i32);
10287 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010288
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010289 SDValue SetCC =
10290 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10291 DAG.getConstant(X86::COND_O, MVT::i32),
10292 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010293
Dan Gohman6e5fda22011-07-22 18:45:15 +000010294 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010295 }
Bill Wendling74c37652008-12-09 22:08:41 +000010296 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010297
Bill Wendling61edeb52008-12-02 01:06:39 +000010298 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010299 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010300 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010301
Bill Wendling61edeb52008-12-02 01:06:39 +000010302 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010303 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10304 DAG.getConstant(Cond, MVT::i32),
10305 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010306
Dan Gohman6e5fda22011-07-22 18:45:15 +000010307 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010308}
10309
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010310SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10311 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010312 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10313 EVT VT = Op.getValueType();
10314
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010315 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010316 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10317 ExtraVT.getScalarType().getSizeInBits();
10318 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10319
10320 unsigned SHLIntrinsicsID = 0;
10321 unsigned SRAIntrinsicsID = 0;
10322 switch (VT.getSimpleVT().SimpleTy) {
10323 default:
10324 return SDValue();
Craig Toppera124f942011-11-21 01:12:36 +000010325 case MVT::v4i32:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010326 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10327 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10328 break;
Craig Toppera124f942011-11-21 01:12:36 +000010329 case MVT::v8i16:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010330 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10331 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10332 break;
Craig Toppera124f942011-11-21 01:12:36 +000010333 case MVT::v8i32:
10334 case MVT::v16i16:
10335 if (!Subtarget->hasAVX())
10336 return SDValue();
10337 if (!Subtarget->hasAVX2()) {
10338 // needs to be split
10339 int NumElems = VT.getVectorNumElements();
10340 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10341 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10342
10343 // Extract the LHS vectors
10344 SDValue LHS = Op.getOperand(0);
10345 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10346 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10347
10348 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10349 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10350
10351 EVT ExtraEltVT = ExtraVT.getVectorElementType();
10352 int ExtraNumElems = ExtraVT.getVectorNumElements();
10353 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10354 ExtraNumElems/2);
10355 SDValue Extra = DAG.getValueType(ExtraVT);
10356
10357 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10358 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
10359
10360 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10361 }
10362 if (VT == MVT::v8i32) {
10363 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_d;
10364 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_d;
10365 } else {
10366 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_w;
10367 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_w;
10368 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010369 }
10370
10371 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10372 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
Craig Toppera124f942011-11-21 01:12:36 +000010373 Op.getOperand(0), ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010374
Nadav Rotema7934dd2011-10-10 19:31:45 +000010375 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10376 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10377 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010378 }
10379
10380 return SDValue();
10381}
10382
10383
Eric Christopher9a9d2752010-07-22 02:48:34 +000010384SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10385 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010386
Eric Christopher77ed1352011-07-08 00:04:56 +000010387 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10388 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010389 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010390 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010391 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010392 SDValue Ops[] = {
10393 DAG.getRegister(X86::ESP, MVT::i32), // Base
10394 DAG.getTargetConstant(1, MVT::i8), // Scale
10395 DAG.getRegister(0, MVT::i32), // Index
10396 DAG.getTargetConstant(0, MVT::i32), // Disp
10397 DAG.getRegister(0, MVT::i32), // Segment.
10398 Zero,
10399 Chain
10400 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010401 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010402 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10403 array_lengthof(Ops));
10404 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010405 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010406
Eric Christopher9a9d2752010-07-22 02:48:34 +000010407 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010408 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010409 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010410
Chris Lattner132929a2010-08-14 17:26:09 +000010411 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10412 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10413 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10414 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010415
Chris Lattner132929a2010-08-14 17:26:09 +000010416 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10417 if (!Op1 && !Op2 && !Op3 && Op4)
10418 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010419
Chris Lattner132929a2010-08-14 17:26:09 +000010420 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10421 if (Op1 && !Op2 && !Op3 && !Op4)
10422 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010423
10424 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010425 // (MFENCE)>;
10426 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010427}
10428
Eli Friedman14648462011-07-27 22:21:52 +000010429SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10430 SelectionDAG &DAG) const {
10431 DebugLoc dl = Op.getDebugLoc();
10432 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10433 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10434 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10435 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10436
10437 // The only fence that needs an instruction is a sequentially-consistent
10438 // cross-thread fence.
10439 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10440 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10441 // no-sse2). There isn't any reason to disable it if the target processor
10442 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010443 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010444 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10445
10446 SDValue Chain = Op.getOperand(0);
10447 SDValue Zero = DAG.getConstant(0, MVT::i32);
10448 SDValue Ops[] = {
10449 DAG.getRegister(X86::ESP, MVT::i32), // Base
10450 DAG.getTargetConstant(1, MVT::i8), // Scale
10451 DAG.getRegister(0, MVT::i32), // Index
10452 DAG.getTargetConstant(0, MVT::i32), // Disp
10453 DAG.getRegister(0, MVT::i32), // Segment.
10454 Zero,
10455 Chain
10456 };
10457 SDNode *Res =
10458 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10459 array_lengthof(Ops));
10460 return SDValue(Res, 0);
10461 }
10462
10463 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10464 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10465}
10466
10467
Dan Gohmand858e902010-04-17 15:26:15 +000010468SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010469 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010470 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010471 unsigned Reg = 0;
10472 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010473 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010474 default:
10475 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010476 case MVT::i8: Reg = X86::AL; size = 1; break;
10477 case MVT::i16: Reg = X86::AX; size = 2; break;
10478 case MVT::i32: Reg = X86::EAX; size = 4; break;
10479 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010480 assert(Subtarget->is64Bit() && "Node not type legal!");
10481 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010482 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010483 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010484 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010485 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010486 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010487 Op.getOperand(1),
10488 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010489 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010490 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010491 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010492 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10493 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10494 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010495 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010496 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010497 return cpOut;
10498}
10499
Duncan Sands1607f052008-12-01 11:39:25 +000010500SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010501 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010502 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010503 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010504 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010505 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010506 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010507 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10508 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010509 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010510 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10511 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010512 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010513 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010514 rdx.getValue(1)
10515 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010516 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010517}
10518
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010519SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010520 SelectionDAG &DAG) const {
10521 EVT SrcVT = Op.getOperand(0).getValueType();
10522 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010523 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010524 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010525 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010526 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010527 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010528 // i64 <=> MMX conversions are Legal.
10529 if (SrcVT==MVT::i64 && DstVT.isVector())
10530 return Op;
10531 if (DstVT==MVT::i64 && SrcVT.isVector())
10532 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010533 // MMX <=> MMX conversions are Legal.
10534 if (SrcVT.isVector() && DstVT.isVector())
10535 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010536 // All other conversions need to be expanded.
10537 return SDValue();
10538}
Chris Lattner5b856542010-12-20 00:59:46 +000010539
Dan Gohmand858e902010-04-17 15:26:15 +000010540SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010541 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010542 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010543 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010544 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010545 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010546 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010547 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010548 Node->getOperand(0),
10549 Node->getOperand(1), negOp,
10550 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010551 cast<AtomicSDNode>(Node)->getAlignment(),
10552 cast<AtomicSDNode>(Node)->getOrdering(),
10553 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010554}
10555
Eli Friedman327236c2011-08-24 20:50:09 +000010556static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10557 SDNode *Node = Op.getNode();
10558 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010559 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010560
10561 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010562 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10563 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10564 // (The only way to get a 16-byte store is cmpxchg16b)
10565 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10566 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10567 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010568 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10569 cast<AtomicSDNode>(Node)->getMemoryVT(),
10570 Node->getOperand(0),
10571 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010572 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010573 cast<AtomicSDNode>(Node)->getOrdering(),
10574 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010575 return Swap.getValue(1);
10576 }
10577 // Other atomic stores have a simple pattern.
10578 return Op;
10579}
10580
Chris Lattner5b856542010-12-20 00:59:46 +000010581static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10582 EVT VT = Op.getNode()->getValueType(0);
10583
10584 // Let legalize expand this if it isn't a legal type yet.
10585 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10586 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010587
Chris Lattner5b856542010-12-20 00:59:46 +000010588 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010589
Chris Lattner5b856542010-12-20 00:59:46 +000010590 unsigned Opc;
10591 bool ExtraOp = false;
10592 switch (Op.getOpcode()) {
10593 default: assert(0 && "Invalid code");
10594 case ISD::ADDC: Opc = X86ISD::ADD; break;
10595 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10596 case ISD::SUBC: Opc = X86ISD::SUB; break;
10597 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10598 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010599
Chris Lattner5b856542010-12-20 00:59:46 +000010600 if (!ExtraOp)
10601 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10602 Op.getOperand(1));
10603 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10604 Op.getOperand(1), Op.getOperand(2));
10605}
10606
Evan Cheng0db9fe62006-04-25 20:13:52 +000010607/// LowerOperation - Provide custom lowering hooks for some operations.
10608///
Dan Gohmand858e902010-04-17 15:26:15 +000010609SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010610 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010611 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010612 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010613 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010614 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010615 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10616 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010617 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010618 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010619 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010620 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10621 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10622 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010623 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010624 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010625 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10626 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10627 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010628 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010629 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010630 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010631 case ISD::SHL_PARTS:
10632 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010633 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010634 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010635 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010636 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010637 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010638 case ISD::FABS: return LowerFABS(Op, DAG);
10639 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010640 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010641 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010642 case ISD::SETCC: return LowerSETCC(Op, DAG);
10643 case ISD::SELECT: return LowerSELECT(Op, DAG);
10644 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010645 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010646 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010647 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010648 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010649 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010650 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10651 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010652 case ISD::FRAME_TO_ARGS_OFFSET:
10653 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010654 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010655 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010656 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10657 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010658 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010659 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10660 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010661 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010662 case ISD::SRA:
10663 case ISD::SRL:
10664 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010665 case ISD::SADDO:
10666 case ISD::UADDO:
10667 case ISD::SSUBO:
10668 case ISD::USUBO:
10669 case ISD::SMULO:
10670 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010671 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010672 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010673 case ISD::ADDC:
10674 case ISD::ADDE:
10675 case ISD::SUBC:
10676 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010677 case ISD::ADD: return LowerADD(Op, DAG);
10678 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010679 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010680}
10681
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010682static void ReplaceATOMIC_LOAD(SDNode *Node,
10683 SmallVectorImpl<SDValue> &Results,
10684 SelectionDAG &DAG) {
10685 DebugLoc dl = Node->getDebugLoc();
10686 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10687
10688 // Convert wide load -> cmpxchg8b/cmpxchg16b
10689 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10690 // (The only way to get a 16-byte load is cmpxchg16b)
10691 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010692 SDValue Zero = DAG.getConstant(0, VT);
10693 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010694 Node->getOperand(0),
10695 Node->getOperand(1), Zero, Zero,
10696 cast<AtomicSDNode>(Node)->getMemOperand(),
10697 cast<AtomicSDNode>(Node)->getOrdering(),
10698 cast<AtomicSDNode>(Node)->getSynchScope());
10699 Results.push_back(Swap.getValue(0));
10700 Results.push_back(Swap.getValue(1));
10701}
10702
Duncan Sands1607f052008-12-01 11:39:25 +000010703void X86TargetLowering::
10704ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010705 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010706 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000010707 assert (Node->getValueType(0) == MVT::i64 &&
10708 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010709
10710 SDValue Chain = Node->getOperand(0);
10711 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010712 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010713 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010714 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010715 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010716 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010717 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010718 SDValue Result =
10719 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10720 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010721 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010722 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010723 Results.push_back(Result.getValue(2));
10724}
10725
Duncan Sands126d9072008-07-04 11:47:58 +000010726/// ReplaceNodeResults - Replace a node with an illegal result type
10727/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010728void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10729 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010730 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010731 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010732 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010733 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010734 assert(false && "Do not know how to custom type legalize this operation!");
10735 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010736 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010737 case ISD::ADDC:
10738 case ISD::ADDE:
10739 case ISD::SUBC:
10740 case ISD::SUBE:
10741 // We don't want to expand or promote these.
10742 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010743 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010744 std::pair<SDValue,SDValue> Vals =
10745 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010746 SDValue FIST = Vals.first, StackSlot = Vals.second;
10747 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010748 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010749 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010750 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010751 MachinePointerInfo(),
10752 false, false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010753 }
10754 return;
10755 }
10756 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010757 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010758 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010759 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010760 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010761 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010762 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010763 eax.getValue(2));
10764 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10765 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010766 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010767 Results.push_back(edx.getValue(1));
10768 return;
10769 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010770 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010771 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010772 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010773 bool Regs64bit = T == MVT::i128;
10774 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010775 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010776 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10777 DAG.getConstant(0, HalfT));
10778 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10779 DAG.getConstant(1, HalfT));
10780 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10781 Regs64bit ? X86::RAX : X86::EAX,
10782 cpInL, SDValue());
10783 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10784 Regs64bit ? X86::RDX : X86::EDX,
10785 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010786 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010787 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10788 DAG.getConstant(0, HalfT));
10789 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10790 DAG.getConstant(1, HalfT));
10791 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10792 Regs64bit ? X86::RBX : X86::EBX,
10793 swapInL, cpInH.getValue(1));
10794 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10795 Regs64bit ? X86::RCX : X86::ECX,
10796 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010797 SDValue Ops[] = { swapInH.getValue(0),
10798 N->getOperand(1),
10799 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010800 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010801 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010802 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10803 X86ISD::LCMPXCHG8_DAG;
10804 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010805 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010806 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10807 Regs64bit ? X86::RAX : X86::EAX,
10808 HalfT, Result.getValue(1));
10809 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10810 Regs64bit ? X86::RDX : X86::EDX,
10811 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010812 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010813 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010814 Results.push_back(cpOutH.getValue(1));
10815 return;
10816 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010817 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010818 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10819 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010820 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010821 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10822 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010823 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010824 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10825 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010826 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010827 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10828 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010829 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010830 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10831 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010832 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010833 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10834 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010835 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010836 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10837 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010838 case ISD::ATOMIC_LOAD:
10839 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010840 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010841}
10842
Evan Cheng72261582005-12-20 06:22:03 +000010843const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10844 switch (Opcode) {
10845 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010846 case X86ISD::BSF: return "X86ISD::BSF";
10847 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010848 case X86ISD::SHLD: return "X86ISD::SHLD";
10849 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010850 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010851 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010852 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010853 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010854 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010855 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010856 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10857 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10858 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010859 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010860 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010861 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010862 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010863 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010864 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010865 case X86ISD::COMI: return "X86ISD::COMI";
10866 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010867 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010868 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010869 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10870 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010871 case X86ISD::CMOV: return "X86ISD::CMOV";
10872 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010873 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010874 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10875 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010876 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010877 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010878 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010879 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010880 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010881 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10882 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010883 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010884 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010885 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000010886 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000010887 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Craig Topperfe033152011-12-06 09:31:36 +000010888 case X86ISD::HADD: return "X86ISD::HADD";
10889 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000010890 case X86ISD::FHADD: return "X86ISD::FHADD";
10891 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000010892 case X86ISD::FMAX: return "X86ISD::FMAX";
10893 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010894 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10895 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010896 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010897 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010898 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010899 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010900 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010901 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10902 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010903 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10904 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10905 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10906 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10907 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10908 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010909 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10910 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010911 case X86ISD::VSHL: return "X86ISD::VSHL";
10912 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010913 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10914 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10915 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10916 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10917 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10918 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10919 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10920 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10921 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10922 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010923 case X86ISD::ADD: return "X86ISD::ADD";
10924 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010925 case X86ISD::ADC: return "X86ISD::ADC";
10926 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010927 case X86ISD::SMUL: return "X86ISD::SMUL";
10928 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010929 case X86ISD::INC: return "X86ISD::INC";
10930 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010931 case X86ISD::OR: return "X86ISD::OR";
10932 case X86ISD::XOR: return "X86ISD::XOR";
10933 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000010934 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000010935 case X86ISD::BLSI: return "X86ISD::BLSI";
10936 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
10937 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000010938 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010939 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010940 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010941 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10942 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10943 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10944 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10945 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10946 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10947 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10948 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10949 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010950 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010951 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010952 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010953 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10954 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010955 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10956 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10957 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10958 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10959 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10960 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10961 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000010962 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
10963 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010964 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000010965 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000010966 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010967 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010968 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010969 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010970 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010971 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010972 }
10973}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010974
Chris Lattnerc9addb72007-03-30 23:15:24 +000010975// isLegalAddressingMode - Return true if the addressing mode represented
10976// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010977bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010978 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010979 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010980 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010981 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010982
Chris Lattnerc9addb72007-03-30 23:15:24 +000010983 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010984 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010985 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010986
Chris Lattnerc9addb72007-03-30 23:15:24 +000010987 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010988 unsigned GVFlags =
10989 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010990
Chris Lattnerdfed4132009-07-10 07:38:24 +000010991 // If a reference to this global requires an extra load, we can't fold it.
10992 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010993 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010994
Chris Lattnerdfed4132009-07-10 07:38:24 +000010995 // If BaseGV requires a register for the PIC base, we cannot also have a
10996 // BaseReg specified.
10997 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000010998 return false;
Evan Cheng52787842007-08-01 23:46:47 +000010999
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011000 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011001 if ((M != CodeModel::Small || R != Reloc::Static) &&
11002 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011003 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011004 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011005
Chris Lattnerc9addb72007-03-30 23:15:24 +000011006 switch (AM.Scale) {
11007 case 0:
11008 case 1:
11009 case 2:
11010 case 4:
11011 case 8:
11012 // These scales always work.
11013 break;
11014 case 3:
11015 case 5:
11016 case 9:
11017 // These scales are formed with basereg+scalereg. Only accept if there is
11018 // no basereg yet.
11019 if (AM.HasBaseReg)
11020 return false;
11021 break;
11022 default: // Other stuff never works.
11023 return false;
11024 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011025
Chris Lattnerc9addb72007-03-30 23:15:24 +000011026 return true;
11027}
11028
11029
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011030bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011031 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011032 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011033 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11034 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011035 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011036 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011037 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011038}
11039
Owen Andersone50ed302009-08-10 22:56:29 +000011040bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011041 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011042 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011043 unsigned NumBits1 = VT1.getSizeInBits();
11044 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011045 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011046 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011047 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011048}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011049
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011050bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011051 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011052 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011053}
11054
Owen Andersone50ed302009-08-10 22:56:29 +000011055bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011056 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011057 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011058}
11059
Owen Andersone50ed302009-08-10 22:56:29 +000011060bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011061 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011062 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011063}
11064
Evan Cheng60c07e12006-07-05 22:17:51 +000011065/// isShuffleMaskLegal - Targets can use this to indicate that they only
11066/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11067/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11068/// are assumed to be legal.
11069bool
Eric Christopherfd179292009-08-27 18:07:15 +000011070X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011071 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011072 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011073 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011074 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011075
Nate Begemana09008b2009-10-19 02:17:23 +000011076 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011077 return (VT.getVectorNumElements() == 2 ||
11078 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11079 isMOVLMask(M, VT) ||
11080 isSHUFPMask(M, VT) ||
11081 isPSHUFDMask(M, VT) ||
11082 isPSHUFHWMask(M, VT) ||
11083 isPSHUFLWMask(M, VT) ||
Craig Topperc0d82852011-11-22 00:44:41 +000011084 isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX()) ||
Craig Topper6347e862011-11-21 06:57:39 +000011085 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11086 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011087 isUNPCKL_v_undef_Mask(M, VT) ||
11088 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011089}
11090
Dan Gohman7d8143f2008-04-09 20:09:42 +000011091bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011092X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011093 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011094 unsigned NumElts = VT.getVectorNumElements();
11095 // FIXME: This collection of masks seems suspect.
11096 if (NumElts == 2)
11097 return true;
11098 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11099 return (isMOVLMask(Mask, VT) ||
11100 isCommutedMOVLMask(Mask, VT, true) ||
11101 isSHUFPMask(Mask, VT) ||
Craig Topper1ff73d72011-12-06 04:59:07 +000011102 isSHUFPMask(Mask, VT, /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011103 }
11104 return false;
11105}
11106
11107//===----------------------------------------------------------------------===//
11108// X86 Scheduler Hooks
11109//===----------------------------------------------------------------------===//
11110
Mon P Wang63307c32008-05-05 19:05:59 +000011111// private utility function
11112MachineBasicBlock *
11113X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11114 MachineBasicBlock *MBB,
11115 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011116 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011117 unsigned LoadOpc,
11118 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011119 unsigned notOpc,
11120 unsigned EAXreg,
11121 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011122 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011123 // For the atomic bitwise operator, we generate
11124 // thisMBB:
11125 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011126 // ld t1 = [bitinstr.addr]
11127 // op t2 = t1, [bitinstr.val]
11128 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011129 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11130 // bz newMBB
11131 // fallthrough -->nextMBB
11132 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11133 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011134 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011135 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011136
Mon P Wang63307c32008-05-05 19:05:59 +000011137 /// First build the CFG
11138 MachineFunction *F = MBB->getParent();
11139 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011140 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11141 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11142 F->insert(MBBIter, newMBB);
11143 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011144
Dan Gohman14152b42010-07-06 20:24:04 +000011145 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11146 nextMBB->splice(nextMBB->begin(), thisMBB,
11147 llvm::next(MachineBasicBlock::iterator(bInstr)),
11148 thisMBB->end());
11149 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011150
Mon P Wang63307c32008-05-05 19:05:59 +000011151 // Update thisMBB to fall through to newMBB
11152 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011153
Mon P Wang63307c32008-05-05 19:05:59 +000011154 // newMBB jumps to itself and fall through to nextMBB
11155 newMBB->addSuccessor(nextMBB);
11156 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011157
Mon P Wang63307c32008-05-05 19:05:59 +000011158 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011159 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011160 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011161 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011162 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011163 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011164 int numArgs = bInstr->getNumOperands() - 1;
11165 for (int i=0; i < numArgs; ++i)
11166 argOpers[i] = &bInstr->getOperand(i+1);
11167
11168 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011169 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011170 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011171
Dale Johannesen140be2d2008-08-19 18:47:28 +000011172 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011173 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011174 for (int i=0; i <= lastAddrIndx; ++i)
11175 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011176
Dale Johannesen140be2d2008-08-19 18:47:28 +000011177 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011178 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011179 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011180 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011181 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011182 tt = t1;
11183
Dale Johannesen140be2d2008-08-19 18:47:28 +000011184 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011185 assert((argOpers[valArgIndx]->isReg() ||
11186 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011187 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011188 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011189 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011190 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011191 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011192 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011193 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011194
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011195 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011196 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011197
Dale Johannesene4d209d2009-02-03 20:21:25 +000011198 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011199 for (int i=0; i <= lastAddrIndx; ++i)
11200 (*MIB).addOperand(*argOpers[i]);
11201 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011202 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011203 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11204 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011205
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011206 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011207 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011208
Mon P Wang63307c32008-05-05 19:05:59 +000011209 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011210 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011211
Dan Gohman14152b42010-07-06 20:24:04 +000011212 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011213 return nextMBB;
11214}
11215
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011216// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011217MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011218X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11219 MachineBasicBlock *MBB,
11220 unsigned regOpcL,
11221 unsigned regOpcH,
11222 unsigned immOpcL,
11223 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011224 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011225 // For the atomic bitwise operator, we generate
11226 // thisMBB (instructions are in pairs, except cmpxchg8b)
11227 // ld t1,t2 = [bitinstr.addr]
11228 // newMBB:
11229 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11230 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011231 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011232 // mov ECX, EBX <- t5, t6
11233 // mov EAX, EDX <- t1, t2
11234 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11235 // mov t3, t4 <- EAX, EDX
11236 // bz newMBB
11237 // result in out1, out2
11238 // fallthrough -->nextMBB
11239
11240 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11241 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011242 const unsigned NotOpc = X86::NOT32r;
11243 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11244 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11245 MachineFunction::iterator MBBIter = MBB;
11246 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011247
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011248 /// First build the CFG
11249 MachineFunction *F = MBB->getParent();
11250 MachineBasicBlock *thisMBB = MBB;
11251 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11252 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11253 F->insert(MBBIter, newMBB);
11254 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011255
Dan Gohman14152b42010-07-06 20:24:04 +000011256 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11257 nextMBB->splice(nextMBB->begin(), thisMBB,
11258 llvm::next(MachineBasicBlock::iterator(bInstr)),
11259 thisMBB->end());
11260 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011261
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011262 // Update thisMBB to fall through to newMBB
11263 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011264
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011265 // newMBB jumps to itself and fall through to nextMBB
11266 newMBB->addSuccessor(nextMBB);
11267 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011268
Dale Johannesene4d209d2009-02-03 20:21:25 +000011269 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011270 // Insert instructions into newMBB based on incoming instruction
11271 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011272 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011273 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011274 MachineOperand& dest1Oper = bInstr->getOperand(0);
11275 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011276 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11277 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011278 argOpers[i] = &bInstr->getOperand(i+2);
11279
Dan Gohman71ea4e52010-05-14 21:01:44 +000011280 // We use some of the operands multiple times, so conservatively just
11281 // clear any kill flags that might be present.
11282 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11283 argOpers[i]->setIsKill(false);
11284 }
11285
Evan Chengad5b52f2010-01-08 19:14:57 +000011286 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011287 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011288
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011289 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011290 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011291 for (int i=0; i <= lastAddrIndx; ++i)
11292 (*MIB).addOperand(*argOpers[i]);
11293 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011294 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011295 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011296 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011297 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011298 MachineOperand newOp3 = *(argOpers[3]);
11299 if (newOp3.isImm())
11300 newOp3.setImm(newOp3.getImm()+4);
11301 else
11302 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011303 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011304 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011305
11306 // t3/4 are defined later, at the bottom of the loop
11307 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11308 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011309 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011310 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011311 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011312 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11313
Evan Cheng306b4ca2010-01-08 23:41:50 +000011314 // The subsequent operations should be using the destination registers of
11315 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011316 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011317 t1 = F->getRegInfo().createVirtualRegister(RC);
11318 t2 = F->getRegInfo().createVirtualRegister(RC);
11319 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11320 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011321 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011322 t1 = dest1Oper.getReg();
11323 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011324 }
11325
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011326 int valArgIndx = lastAddrIndx + 1;
11327 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011328 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011329 "invalid operand");
11330 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11331 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011332 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011333 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011334 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011335 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011336 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011337 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011338 (*MIB).addOperand(*argOpers[valArgIndx]);
11339 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011340 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011341 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011342 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011343 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011344 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011345 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011346 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011347 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011348 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011349 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011350
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011351 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011352 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011353 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011354 MIB.addReg(t2);
11355
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011356 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011357 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011358 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011359 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011360
Dale Johannesene4d209d2009-02-03 20:21:25 +000011361 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011362 for (int i=0; i <= lastAddrIndx; ++i)
11363 (*MIB).addOperand(*argOpers[i]);
11364
11365 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011366 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11367 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011368
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011369 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011370 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011371 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011372 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011373
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011374 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011375 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011376
Dan Gohman14152b42010-07-06 20:24:04 +000011377 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011378 return nextMBB;
11379}
11380
11381// private utility function
11382MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011383X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11384 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011385 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011386 // For the atomic min/max operator, we generate
11387 // thisMBB:
11388 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011389 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011390 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011391 // cmp t1, t2
11392 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011393 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011394 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11395 // bz newMBB
11396 // fallthrough -->nextMBB
11397 //
11398 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11399 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011400 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011401 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011402
Mon P Wang63307c32008-05-05 19:05:59 +000011403 /// First build the CFG
11404 MachineFunction *F = MBB->getParent();
11405 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011406 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11407 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11408 F->insert(MBBIter, newMBB);
11409 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011410
Dan Gohman14152b42010-07-06 20:24:04 +000011411 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11412 nextMBB->splice(nextMBB->begin(), thisMBB,
11413 llvm::next(MachineBasicBlock::iterator(mInstr)),
11414 thisMBB->end());
11415 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011416
Mon P Wang63307c32008-05-05 19:05:59 +000011417 // Update thisMBB to fall through to newMBB
11418 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011419
Mon P Wang63307c32008-05-05 19:05:59 +000011420 // newMBB jumps to newMBB and fall through to nextMBB
11421 newMBB->addSuccessor(nextMBB);
11422 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011423
Dale Johannesene4d209d2009-02-03 20:21:25 +000011424 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011425 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011426 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011427 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011428 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011429 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011430 int numArgs = mInstr->getNumOperands() - 1;
11431 for (int i=0; i < numArgs; ++i)
11432 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011433
Mon P Wang63307c32008-05-05 19:05:59 +000011434 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011435 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011436 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011437
Mon P Wangab3e7472008-05-05 22:56:23 +000011438 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011439 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011440 for (int i=0; i <= lastAddrIndx; ++i)
11441 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011442
Mon P Wang63307c32008-05-05 19:05:59 +000011443 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011444 assert((argOpers[valArgIndx]->isReg() ||
11445 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011446 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011447
11448 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011449 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011450 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011451 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011452 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011453 (*MIB).addOperand(*argOpers[valArgIndx]);
11454
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011455 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011456 MIB.addReg(t1);
11457
Dale Johannesene4d209d2009-02-03 20:21:25 +000011458 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011459 MIB.addReg(t1);
11460 MIB.addReg(t2);
11461
11462 // Generate movc
11463 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011464 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011465 MIB.addReg(t2);
11466 MIB.addReg(t1);
11467
11468 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011469 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011470 for (int i=0; i <= lastAddrIndx; ++i)
11471 (*MIB).addOperand(*argOpers[i]);
11472 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011473 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011474 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11475 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011476
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011477 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011478 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011479
Mon P Wang63307c32008-05-05 19:05:59 +000011480 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011481 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011482
Dan Gohman14152b42010-07-06 20:24:04 +000011483 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011484 return nextMBB;
11485}
11486
Eric Christopherf83a5de2009-08-27 18:08:16 +000011487// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011488// or XMM0_V32I8 in AVX all of this code can be replaced with that
11489// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011490MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011491X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011492 unsigned numArgs, bool memArg) const {
Craig Topperc0d82852011-11-22 00:44:41 +000011493 assert(Subtarget->hasSSE42orAVX() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011494 "Target must have SSE4.2 or AVX features enabled");
11495
Eric Christopherb120ab42009-08-18 22:50:32 +000011496 DebugLoc dl = MI->getDebugLoc();
11497 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011498 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011499 if (!Subtarget->hasAVX()) {
11500 if (memArg)
11501 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11502 else
11503 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11504 } else {
11505 if (memArg)
11506 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11507 else
11508 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11509 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011510
Eric Christopher41c902f2010-11-30 08:20:21 +000011511 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011512 for (unsigned i = 0; i < numArgs; ++i) {
11513 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011514 if (!(Op.isReg() && Op.isImplicit()))
11515 MIB.addOperand(Op);
11516 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011517 BuildMI(*BB, MI, dl,
11518 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11519 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011520 .addReg(X86::XMM0);
11521
Dan Gohman14152b42010-07-06 20:24:04 +000011522 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011523 return BB;
11524}
11525
11526MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011527X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011528 DebugLoc dl = MI->getDebugLoc();
11529 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011530
Eric Christopher228232b2010-11-30 07:20:12 +000011531 // Address into RAX/EAX, other two args into ECX, EDX.
11532 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11533 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11534 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11535 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011536 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011537
Eric Christopher228232b2010-11-30 07:20:12 +000011538 unsigned ValOps = X86::AddrNumOperands;
11539 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11540 .addReg(MI->getOperand(ValOps).getReg());
11541 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11542 .addReg(MI->getOperand(ValOps+1).getReg());
11543
11544 // The instruction doesn't actually take any operands though.
11545 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011546
Eric Christopher228232b2010-11-30 07:20:12 +000011547 MI->eraseFromParent(); // The pseudo is gone now.
11548 return BB;
11549}
11550
11551MachineBasicBlock *
11552X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011553 DebugLoc dl = MI->getDebugLoc();
11554 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011555
Eric Christopher228232b2010-11-30 07:20:12 +000011556 // First arg in ECX, the second in EAX.
11557 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11558 .addReg(MI->getOperand(0).getReg());
11559 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11560 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011561
Eric Christopher228232b2010-11-30 07:20:12 +000011562 // The instruction doesn't actually take any operands though.
11563 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011564
Eric Christopher228232b2010-11-30 07:20:12 +000011565 MI->eraseFromParent(); // The pseudo is gone now.
11566 return BB;
11567}
11568
11569MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011570X86TargetLowering::EmitVAARG64WithCustomInserter(
11571 MachineInstr *MI,
11572 MachineBasicBlock *MBB) const {
11573 // Emit va_arg instruction on X86-64.
11574
11575 // Operands to this pseudo-instruction:
11576 // 0 ) Output : destination address (reg)
11577 // 1-5) Input : va_list address (addr, i64mem)
11578 // 6 ) ArgSize : Size (in bytes) of vararg type
11579 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11580 // 8 ) Align : Alignment of type
11581 // 9 ) EFLAGS (implicit-def)
11582
11583 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11584 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11585
11586 unsigned DestReg = MI->getOperand(0).getReg();
11587 MachineOperand &Base = MI->getOperand(1);
11588 MachineOperand &Scale = MI->getOperand(2);
11589 MachineOperand &Index = MI->getOperand(3);
11590 MachineOperand &Disp = MI->getOperand(4);
11591 MachineOperand &Segment = MI->getOperand(5);
11592 unsigned ArgSize = MI->getOperand(6).getImm();
11593 unsigned ArgMode = MI->getOperand(7).getImm();
11594 unsigned Align = MI->getOperand(8).getImm();
11595
11596 // Memory Reference
11597 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11598 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11599 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11600
11601 // Machine Information
11602 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11603 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11604 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11605 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11606 DebugLoc DL = MI->getDebugLoc();
11607
11608 // struct va_list {
11609 // i32 gp_offset
11610 // i32 fp_offset
11611 // i64 overflow_area (address)
11612 // i64 reg_save_area (address)
11613 // }
11614 // sizeof(va_list) = 24
11615 // alignment(va_list) = 8
11616
11617 unsigned TotalNumIntRegs = 6;
11618 unsigned TotalNumXMMRegs = 8;
11619 bool UseGPOffset = (ArgMode == 1);
11620 bool UseFPOffset = (ArgMode == 2);
11621 unsigned MaxOffset = TotalNumIntRegs * 8 +
11622 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11623
11624 /* Align ArgSize to a multiple of 8 */
11625 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11626 bool NeedsAlign = (Align > 8);
11627
11628 MachineBasicBlock *thisMBB = MBB;
11629 MachineBasicBlock *overflowMBB;
11630 MachineBasicBlock *offsetMBB;
11631 MachineBasicBlock *endMBB;
11632
11633 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11634 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11635 unsigned OffsetReg = 0;
11636
11637 if (!UseGPOffset && !UseFPOffset) {
11638 // If we only pull from the overflow region, we don't create a branch.
11639 // We don't need to alter control flow.
11640 OffsetDestReg = 0; // unused
11641 OverflowDestReg = DestReg;
11642
11643 offsetMBB = NULL;
11644 overflowMBB = thisMBB;
11645 endMBB = thisMBB;
11646 } else {
11647 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11648 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11649 // If not, pull from overflow_area. (branch to overflowMBB)
11650 //
11651 // thisMBB
11652 // | .
11653 // | .
11654 // offsetMBB overflowMBB
11655 // | .
11656 // | .
11657 // endMBB
11658
11659 // Registers for the PHI in endMBB
11660 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11661 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11662
11663 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11664 MachineFunction *MF = MBB->getParent();
11665 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11666 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11667 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11668
11669 MachineFunction::iterator MBBIter = MBB;
11670 ++MBBIter;
11671
11672 // Insert the new basic blocks
11673 MF->insert(MBBIter, offsetMBB);
11674 MF->insert(MBBIter, overflowMBB);
11675 MF->insert(MBBIter, endMBB);
11676
11677 // Transfer the remainder of MBB and its successor edges to endMBB.
11678 endMBB->splice(endMBB->begin(), thisMBB,
11679 llvm::next(MachineBasicBlock::iterator(MI)),
11680 thisMBB->end());
11681 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11682
11683 // Make offsetMBB and overflowMBB successors of thisMBB
11684 thisMBB->addSuccessor(offsetMBB);
11685 thisMBB->addSuccessor(overflowMBB);
11686
11687 // endMBB is a successor of both offsetMBB and overflowMBB
11688 offsetMBB->addSuccessor(endMBB);
11689 overflowMBB->addSuccessor(endMBB);
11690
11691 // Load the offset value into a register
11692 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11693 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11694 .addOperand(Base)
11695 .addOperand(Scale)
11696 .addOperand(Index)
11697 .addDisp(Disp, UseFPOffset ? 4 : 0)
11698 .addOperand(Segment)
11699 .setMemRefs(MMOBegin, MMOEnd);
11700
11701 // Check if there is enough room left to pull this argument.
11702 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11703 .addReg(OffsetReg)
11704 .addImm(MaxOffset + 8 - ArgSizeA8);
11705
11706 // Branch to "overflowMBB" if offset >= max
11707 // Fall through to "offsetMBB" otherwise
11708 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11709 .addMBB(overflowMBB);
11710 }
11711
11712 // In offsetMBB, emit code to use the reg_save_area.
11713 if (offsetMBB) {
11714 assert(OffsetReg != 0);
11715
11716 // Read the reg_save_area address.
11717 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11718 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11719 .addOperand(Base)
11720 .addOperand(Scale)
11721 .addOperand(Index)
11722 .addDisp(Disp, 16)
11723 .addOperand(Segment)
11724 .setMemRefs(MMOBegin, MMOEnd);
11725
11726 // Zero-extend the offset
11727 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11728 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11729 .addImm(0)
11730 .addReg(OffsetReg)
11731 .addImm(X86::sub_32bit);
11732
11733 // Add the offset to the reg_save_area to get the final address.
11734 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11735 .addReg(OffsetReg64)
11736 .addReg(RegSaveReg);
11737
11738 // Compute the offset for the next argument
11739 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11740 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11741 .addReg(OffsetReg)
11742 .addImm(UseFPOffset ? 16 : 8);
11743
11744 // Store it back into the va_list.
11745 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11746 .addOperand(Base)
11747 .addOperand(Scale)
11748 .addOperand(Index)
11749 .addDisp(Disp, UseFPOffset ? 4 : 0)
11750 .addOperand(Segment)
11751 .addReg(NextOffsetReg)
11752 .setMemRefs(MMOBegin, MMOEnd);
11753
11754 // Jump to endMBB
11755 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11756 .addMBB(endMBB);
11757 }
11758
11759 //
11760 // Emit code to use overflow area
11761 //
11762
11763 // Load the overflow_area address into a register.
11764 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11765 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11766 .addOperand(Base)
11767 .addOperand(Scale)
11768 .addOperand(Index)
11769 .addDisp(Disp, 8)
11770 .addOperand(Segment)
11771 .setMemRefs(MMOBegin, MMOEnd);
11772
11773 // If we need to align it, do so. Otherwise, just copy the address
11774 // to OverflowDestReg.
11775 if (NeedsAlign) {
11776 // Align the overflow address
11777 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11778 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11779
11780 // aligned_addr = (addr + (align-1)) & ~(align-1)
11781 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11782 .addReg(OverflowAddrReg)
11783 .addImm(Align-1);
11784
11785 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11786 .addReg(TmpReg)
11787 .addImm(~(uint64_t)(Align-1));
11788 } else {
11789 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11790 .addReg(OverflowAddrReg);
11791 }
11792
11793 // Compute the next overflow address after this argument.
11794 // (the overflow address should be kept 8-byte aligned)
11795 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11796 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11797 .addReg(OverflowDestReg)
11798 .addImm(ArgSizeA8);
11799
11800 // Store the new overflow address.
11801 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11802 .addOperand(Base)
11803 .addOperand(Scale)
11804 .addOperand(Index)
11805 .addDisp(Disp, 8)
11806 .addOperand(Segment)
11807 .addReg(NextAddrReg)
11808 .setMemRefs(MMOBegin, MMOEnd);
11809
11810 // If we branched, emit the PHI to the front of endMBB.
11811 if (offsetMBB) {
11812 BuildMI(*endMBB, endMBB->begin(), DL,
11813 TII->get(X86::PHI), DestReg)
11814 .addReg(OffsetDestReg).addMBB(offsetMBB)
11815 .addReg(OverflowDestReg).addMBB(overflowMBB);
11816 }
11817
11818 // Erase the pseudo instruction
11819 MI->eraseFromParent();
11820
11821 return endMBB;
11822}
11823
11824MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011825X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11826 MachineInstr *MI,
11827 MachineBasicBlock *MBB) const {
11828 // Emit code to save XMM registers to the stack. The ABI says that the
11829 // number of registers to save is given in %al, so it's theoretically
11830 // possible to do an indirect jump trick to avoid saving all of them,
11831 // however this code takes a simpler approach and just executes all
11832 // of the stores if %al is non-zero. It's less code, and it's probably
11833 // easier on the hardware branch predictor, and stores aren't all that
11834 // expensive anyway.
11835
11836 // Create the new basic blocks. One block contains all the XMM stores,
11837 // and one block is the final destination regardless of whether any
11838 // stores were performed.
11839 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11840 MachineFunction *F = MBB->getParent();
11841 MachineFunction::iterator MBBIter = MBB;
11842 ++MBBIter;
11843 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11844 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11845 F->insert(MBBIter, XMMSaveMBB);
11846 F->insert(MBBIter, EndMBB);
11847
Dan Gohman14152b42010-07-06 20:24:04 +000011848 // Transfer the remainder of MBB and its successor edges to EndMBB.
11849 EndMBB->splice(EndMBB->begin(), MBB,
11850 llvm::next(MachineBasicBlock::iterator(MI)),
11851 MBB->end());
11852 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11853
Dan Gohmand6708ea2009-08-15 01:38:56 +000011854 // The original block will now fall through to the XMM save block.
11855 MBB->addSuccessor(XMMSaveMBB);
11856 // The XMMSaveMBB will fall through to the end block.
11857 XMMSaveMBB->addSuccessor(EndMBB);
11858
11859 // Now add the instructions.
11860 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11861 DebugLoc DL = MI->getDebugLoc();
11862
11863 unsigned CountReg = MI->getOperand(0).getReg();
11864 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11865 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11866
11867 if (!Subtarget->isTargetWin64()) {
11868 // If %al is 0, branch around the XMM save block.
11869 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011870 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011871 MBB->addSuccessor(EndMBB);
11872 }
11873
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011874 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011875 // In the XMM save block, save all the XMM argument registers.
11876 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11877 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011878 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011879 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011880 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011881 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011882 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011883 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011884 .addFrameIndex(RegSaveFrameIndex)
11885 .addImm(/*Scale=*/1)
11886 .addReg(/*IndexReg=*/0)
11887 .addImm(/*Disp=*/Offset)
11888 .addReg(/*Segment=*/0)
11889 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011890 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011891 }
11892
Dan Gohman14152b42010-07-06 20:24:04 +000011893 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011894
11895 return EndMBB;
11896}
Mon P Wang63307c32008-05-05 19:05:59 +000011897
Evan Cheng60c07e12006-07-05 22:17:51 +000011898MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011899X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011900 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011901 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11902 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011903
Chris Lattner52600972009-09-02 05:57:00 +000011904 // To "insert" a SELECT_CC instruction, we actually have to insert the
11905 // diamond control-flow pattern. The incoming instruction knows the
11906 // destination vreg to set, the condition code register to branch on, the
11907 // true/false values to select between, and a branch opcode to use.
11908 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11909 MachineFunction::iterator It = BB;
11910 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011911
Chris Lattner52600972009-09-02 05:57:00 +000011912 // thisMBB:
11913 // ...
11914 // TrueVal = ...
11915 // cmpTY ccX, r1, r2
11916 // bCC copy1MBB
11917 // fallthrough --> copy0MBB
11918 MachineBasicBlock *thisMBB = BB;
11919 MachineFunction *F = BB->getParent();
11920 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11921 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011922 F->insert(It, copy0MBB);
11923 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011924
Bill Wendling730c07e2010-06-25 20:48:10 +000011925 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11926 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011927 if (!MI->killsRegister(X86::EFLAGS)) {
11928 copy0MBB->addLiveIn(X86::EFLAGS);
11929 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011930 }
11931
Dan Gohman14152b42010-07-06 20:24:04 +000011932 // Transfer the remainder of BB and its successor edges to sinkMBB.
11933 sinkMBB->splice(sinkMBB->begin(), BB,
11934 llvm::next(MachineBasicBlock::iterator(MI)),
11935 BB->end());
11936 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11937
11938 // Add the true and fallthrough blocks as its successors.
11939 BB->addSuccessor(copy0MBB);
11940 BB->addSuccessor(sinkMBB);
11941
11942 // Create the conditional branch instruction.
11943 unsigned Opc =
11944 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11945 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11946
Chris Lattner52600972009-09-02 05:57:00 +000011947 // copy0MBB:
11948 // %FalseValue = ...
11949 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011950 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011951
Chris Lattner52600972009-09-02 05:57:00 +000011952 // sinkMBB:
11953 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11954 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011955 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11956 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011957 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11958 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11959
Dan Gohman14152b42010-07-06 20:24:04 +000011960 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011961 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011962}
11963
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011964MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011965X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11966 bool Is64Bit) const {
11967 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11968 DebugLoc DL = MI->getDebugLoc();
11969 MachineFunction *MF = BB->getParent();
11970 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11971
Nick Lewycky8a8d4792011-12-02 22:16:29 +000011972 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011973
11974 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11975 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11976
11977 // BB:
11978 // ... [Till the alloca]
11979 // If stacklet is not large enough, jump to mallocMBB
11980 //
11981 // bumpMBB:
11982 // Allocate by subtracting from RSP
11983 // Jump to continueMBB
11984 //
11985 // mallocMBB:
11986 // Allocate by call to runtime
11987 //
11988 // continueMBB:
11989 // ...
11990 // [rest of original BB]
11991 //
11992
11993 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11994 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11995 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11996
11997 MachineRegisterInfo &MRI = MF->getRegInfo();
11998 const TargetRegisterClass *AddrRegClass =
11999 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12000
12001 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12002 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12003 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012004 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012005 sizeVReg = MI->getOperand(1).getReg(),
12006 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12007
12008 MachineFunction::iterator MBBIter = BB;
12009 ++MBBIter;
12010
12011 MF->insert(MBBIter, bumpMBB);
12012 MF->insert(MBBIter, mallocMBB);
12013 MF->insert(MBBIter, continueMBB);
12014
12015 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12016 (MachineBasicBlock::iterator(MI)), BB->end());
12017 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12018
12019 // Add code to the main basic block to check if the stack limit has been hit,
12020 // and if so, jump to mallocMBB otherwise to bumpMBB.
12021 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012022 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012023 .addReg(tmpSPVReg).addReg(sizeVReg);
12024 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
12025 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012026 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012027 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12028
12029 // bumpMBB simply decreases the stack pointer, since we know the current
12030 // stacklet has enough space.
12031 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012032 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012033 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012034 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012035 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12036
12037 // Calls into a routine in libgcc to allocate more space from the heap.
12038 if (Is64Bit) {
12039 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12040 .addReg(sizeVReg);
12041 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12042 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12043 } else {
12044 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12045 .addImm(12);
12046 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12047 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12048 .addExternalSymbol("__morestack_allocate_stack_space");
12049 }
12050
12051 if (!Is64Bit)
12052 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12053 .addImm(16);
12054
12055 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12056 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12057 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12058
12059 // Set up the CFG correctly.
12060 BB->addSuccessor(bumpMBB);
12061 BB->addSuccessor(mallocMBB);
12062 mallocMBB->addSuccessor(continueMBB);
12063 bumpMBB->addSuccessor(continueMBB);
12064
12065 // Take care of the PHI nodes.
12066 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12067 MI->getOperand(0).getReg())
12068 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12069 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12070
12071 // Delete the original pseudo instruction.
12072 MI->eraseFromParent();
12073
12074 // And we're done.
12075 return continueMBB;
12076}
12077
12078MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012079X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012080 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012081 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12082 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012083
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012084 assert(!Subtarget->isTargetEnvMacho());
12085
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012086 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12087 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012088
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012089 if (Subtarget->isTargetWin64()) {
12090 if (Subtarget->isTargetCygMing()) {
12091 // ___chkstk(Mingw64):
12092 // Clobbers R10, R11, RAX and EFLAGS.
12093 // Updates RSP.
12094 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12095 .addExternalSymbol("___chkstk")
12096 .addReg(X86::RAX, RegState::Implicit)
12097 .addReg(X86::RSP, RegState::Implicit)
12098 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12099 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12100 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12101 } else {
12102 // __chkstk(MSVCRT): does not update stack pointer.
12103 // Clobbers R10, R11 and EFLAGS.
12104 // FIXME: RAX(allocated size) might be reused and not killed.
12105 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12106 .addExternalSymbol("__chkstk")
12107 .addReg(X86::RAX, RegState::Implicit)
12108 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12109 // RAX has the offset to subtracted from RSP.
12110 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12111 .addReg(X86::RSP)
12112 .addReg(X86::RAX);
12113 }
12114 } else {
12115 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012116 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12117
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012118 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12119 .addExternalSymbol(StackProbeSymbol)
12120 .addReg(X86::EAX, RegState::Implicit)
12121 .addReg(X86::ESP, RegState::Implicit)
12122 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12123 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12124 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12125 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012126
Dan Gohman14152b42010-07-06 20:24:04 +000012127 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012128 return BB;
12129}
Chris Lattner52600972009-09-02 05:57:00 +000012130
12131MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012132X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12133 MachineBasicBlock *BB) const {
12134 // This is pretty easy. We're taking the value that we received from
12135 // our load from the relocation, sticking it in either RDI (x86-64)
12136 // or EAX and doing an indirect call. The return value will then
12137 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012138 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012139 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012140 DebugLoc DL = MI->getDebugLoc();
12141 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012142
12143 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012144 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012145
Eric Christopher30ef0e52010-06-03 04:07:48 +000012146 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012147 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12148 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012149 .addReg(X86::RIP)
12150 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012151 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012152 MI->getOperand(3).getTargetFlags())
12153 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012154 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012155 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012156 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012157 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12158 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012159 .addReg(0)
12160 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012161 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012162 MI->getOperand(3).getTargetFlags())
12163 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012164 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012165 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012166 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012167 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12168 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012169 .addReg(TII->getGlobalBaseReg(F))
12170 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012171 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012172 MI->getOperand(3).getTargetFlags())
12173 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012174 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012175 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012176 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012177
Dan Gohman14152b42010-07-06 20:24:04 +000012178 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012179 return BB;
12180}
12181
12182MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012183X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012184 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012185 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000012186 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012187 case X86::TAILJMPd64:
12188 case X86::TAILJMPr64:
12189 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000012190 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012191 case X86::TCRETURNdi64:
12192 case X86::TCRETURNri64:
12193 case X86::TCRETURNmi64:
12194 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12195 // On AMD64, additional defs should be added before register allocation.
12196 if (!Subtarget->isTargetWin64()) {
12197 MI->addRegisterDefined(X86::RSI);
12198 MI->addRegisterDefined(X86::RDI);
12199 MI->addRegisterDefined(X86::XMM6);
12200 MI->addRegisterDefined(X86::XMM7);
12201 MI->addRegisterDefined(X86::XMM8);
12202 MI->addRegisterDefined(X86::XMM9);
12203 MI->addRegisterDefined(X86::XMM10);
12204 MI->addRegisterDefined(X86::XMM11);
12205 MI->addRegisterDefined(X86::XMM12);
12206 MI->addRegisterDefined(X86::XMM13);
12207 MI->addRegisterDefined(X86::XMM14);
12208 MI->addRegisterDefined(X86::XMM15);
12209 }
12210 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012211 case X86::WIN_ALLOCA:
12212 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012213 case X86::SEG_ALLOCA_32:
12214 return EmitLoweredSegAlloca(MI, BB, false);
12215 case X86::SEG_ALLOCA_64:
12216 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012217 case X86::TLSCall_32:
12218 case X86::TLSCall_64:
12219 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012220 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012221 case X86::CMOV_FR32:
12222 case X86::CMOV_FR64:
12223 case X86::CMOV_V4F32:
12224 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012225 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012226 case X86::CMOV_V8F32:
12227 case X86::CMOV_V4F64:
12228 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012229 case X86::CMOV_GR16:
12230 case X86::CMOV_GR32:
12231 case X86::CMOV_RFP32:
12232 case X86::CMOV_RFP64:
12233 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012234 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012235
Dale Johannesen849f2142007-07-03 00:53:03 +000012236 case X86::FP32_TO_INT16_IN_MEM:
12237 case X86::FP32_TO_INT32_IN_MEM:
12238 case X86::FP32_TO_INT64_IN_MEM:
12239 case X86::FP64_TO_INT16_IN_MEM:
12240 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012241 case X86::FP64_TO_INT64_IN_MEM:
12242 case X86::FP80_TO_INT16_IN_MEM:
12243 case X86::FP80_TO_INT32_IN_MEM:
12244 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012245 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12246 DebugLoc DL = MI->getDebugLoc();
12247
Evan Cheng60c07e12006-07-05 22:17:51 +000012248 // Change the floating point control register to use "round towards zero"
12249 // mode when truncating to an integer value.
12250 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012251 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012252 addFrameReference(BuildMI(*BB, MI, DL,
12253 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012254
12255 // Load the old value of the high byte of the control word...
12256 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012257 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012258 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012259 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012260
12261 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012262 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012263 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012264
12265 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012266 addFrameReference(BuildMI(*BB, MI, DL,
12267 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012268
12269 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012270 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012271 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012272
12273 // Get the X86 opcode to use.
12274 unsigned Opc;
12275 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012276 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012277 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12278 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12279 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12280 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12281 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12282 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012283 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12284 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12285 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012286 }
12287
12288 X86AddressMode AM;
12289 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012290 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012291 AM.BaseType = X86AddressMode::RegBase;
12292 AM.Base.Reg = Op.getReg();
12293 } else {
12294 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012295 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012296 }
12297 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012298 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012299 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012300 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012301 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012302 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012303 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012304 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012305 AM.GV = Op.getGlobal();
12306 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012307 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012308 }
Dan Gohman14152b42010-07-06 20:24:04 +000012309 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012310 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012311
12312 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012313 addFrameReference(BuildMI(*BB, MI, DL,
12314 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012315
Dan Gohman14152b42010-07-06 20:24:04 +000012316 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012317 return BB;
12318 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012319 // String/text processing lowering.
12320 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012321 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012322 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12323 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012324 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012325 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12326 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012327 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012328 return EmitPCMP(MI, BB, 5, false /* in mem */);
12329 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012330 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012331 return EmitPCMP(MI, BB, 5, true /* in mem */);
12332
Eric Christopher228232b2010-11-30 07:20:12 +000012333 // Thread synchronization.
12334 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012335 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012336 case X86::MWAIT:
12337 return EmitMwait(MI, BB);
12338
Eric Christopherb120ab42009-08-18 22:50:32 +000012339 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012340 case X86::ATOMAND32:
12341 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012342 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012343 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012344 X86::NOT32r, X86::EAX,
12345 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012346 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012347 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12348 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012349 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012350 X86::NOT32r, X86::EAX,
12351 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012352 case X86::ATOMXOR32:
12353 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012354 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012355 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012356 X86::NOT32r, X86::EAX,
12357 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012358 case X86::ATOMNAND32:
12359 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012360 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012361 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012362 X86::NOT32r, X86::EAX,
12363 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012364 case X86::ATOMMIN32:
12365 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12366 case X86::ATOMMAX32:
12367 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12368 case X86::ATOMUMIN32:
12369 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12370 case X86::ATOMUMAX32:
12371 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012372
12373 case X86::ATOMAND16:
12374 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12375 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012376 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012377 X86::NOT16r, X86::AX,
12378 X86::GR16RegisterClass);
12379 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012380 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012381 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012382 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012383 X86::NOT16r, X86::AX,
12384 X86::GR16RegisterClass);
12385 case X86::ATOMXOR16:
12386 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12387 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012388 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012389 X86::NOT16r, X86::AX,
12390 X86::GR16RegisterClass);
12391 case X86::ATOMNAND16:
12392 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12393 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012394 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012395 X86::NOT16r, X86::AX,
12396 X86::GR16RegisterClass, true);
12397 case X86::ATOMMIN16:
12398 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12399 case X86::ATOMMAX16:
12400 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12401 case X86::ATOMUMIN16:
12402 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12403 case X86::ATOMUMAX16:
12404 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12405
12406 case X86::ATOMAND8:
12407 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12408 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012409 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012410 X86::NOT8r, X86::AL,
12411 X86::GR8RegisterClass);
12412 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012413 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012414 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012415 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012416 X86::NOT8r, X86::AL,
12417 X86::GR8RegisterClass);
12418 case X86::ATOMXOR8:
12419 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12420 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012421 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012422 X86::NOT8r, X86::AL,
12423 X86::GR8RegisterClass);
12424 case X86::ATOMNAND8:
12425 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12426 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012427 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012428 X86::NOT8r, X86::AL,
12429 X86::GR8RegisterClass, true);
12430 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012431 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012432 case X86::ATOMAND64:
12433 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012434 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012435 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012436 X86::NOT64r, X86::RAX,
12437 X86::GR64RegisterClass);
12438 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012439 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12440 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012441 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012442 X86::NOT64r, X86::RAX,
12443 X86::GR64RegisterClass);
12444 case X86::ATOMXOR64:
12445 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012446 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012447 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012448 X86::NOT64r, X86::RAX,
12449 X86::GR64RegisterClass);
12450 case X86::ATOMNAND64:
12451 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12452 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012453 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012454 X86::NOT64r, X86::RAX,
12455 X86::GR64RegisterClass, true);
12456 case X86::ATOMMIN64:
12457 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12458 case X86::ATOMMAX64:
12459 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12460 case X86::ATOMUMIN64:
12461 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12462 case X86::ATOMUMAX64:
12463 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012464
12465 // This group does 64-bit operations on a 32-bit host.
12466 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012467 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012468 X86::AND32rr, X86::AND32rr,
12469 X86::AND32ri, X86::AND32ri,
12470 false);
12471 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012472 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012473 X86::OR32rr, X86::OR32rr,
12474 X86::OR32ri, X86::OR32ri,
12475 false);
12476 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012477 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012478 X86::XOR32rr, X86::XOR32rr,
12479 X86::XOR32ri, X86::XOR32ri,
12480 false);
12481 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012482 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012483 X86::AND32rr, X86::AND32rr,
12484 X86::AND32ri, X86::AND32ri,
12485 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012486 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012487 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012488 X86::ADD32rr, X86::ADC32rr,
12489 X86::ADD32ri, X86::ADC32ri,
12490 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012491 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012492 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012493 X86::SUB32rr, X86::SBB32rr,
12494 X86::SUB32ri, X86::SBB32ri,
12495 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012496 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012497 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012498 X86::MOV32rr, X86::MOV32rr,
12499 X86::MOV32ri, X86::MOV32ri,
12500 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012501 case X86::VASTART_SAVE_XMM_REGS:
12502 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012503
12504 case X86::VAARG_64:
12505 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012506 }
12507}
12508
12509//===----------------------------------------------------------------------===//
12510// X86 Optimization Hooks
12511//===----------------------------------------------------------------------===//
12512
Dan Gohman475871a2008-07-27 21:46:04 +000012513void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012514 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012515 APInt &KnownZero,
12516 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012517 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012518 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012519 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012520 assert((Opc >= ISD::BUILTIN_OP_END ||
12521 Opc == ISD::INTRINSIC_WO_CHAIN ||
12522 Opc == ISD::INTRINSIC_W_CHAIN ||
12523 Opc == ISD::INTRINSIC_VOID) &&
12524 "Should use MaskedValueIsZero if you don't know whether Op"
12525 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012526
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012527 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012528 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012529 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012530 case X86ISD::ADD:
12531 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012532 case X86ISD::ADC:
12533 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012534 case X86ISD::SMUL:
12535 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012536 case X86ISD::INC:
12537 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012538 case X86ISD::OR:
12539 case X86ISD::XOR:
12540 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012541 // These nodes' second result is a boolean.
12542 if (Op.getResNo() == 0)
12543 break;
12544 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012545 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012546 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12547 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012548 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012549 case ISD::INTRINSIC_WO_CHAIN: {
12550 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12551 unsigned NumLoBits = 0;
12552 switch (IntId) {
12553 default: break;
12554 case Intrinsic::x86_sse_movmsk_ps:
12555 case Intrinsic::x86_avx_movmsk_ps_256:
12556 case Intrinsic::x86_sse2_movmsk_pd:
12557 case Intrinsic::x86_avx_movmsk_pd_256:
12558 case Intrinsic::x86_mmx_pmovmskb:
12559 case Intrinsic::x86_sse2_pmovmskb_128: {
12560 // High bits of movmskp{s|d}, pmovmskb are known zero.
12561 switch (IntId) {
12562 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12563 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12564 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12565 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12566 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12567 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
12568 }
12569 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12570 Mask.getBitWidth() - NumLoBits);
12571 break;
12572 }
12573 }
12574 break;
12575 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012576 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012577}
Chris Lattner259e97c2006-01-31 19:43:35 +000012578
Owen Andersonbc146b02010-09-21 20:42:50 +000012579unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12580 unsigned Depth) const {
12581 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12582 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12583 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012584
Owen Andersonbc146b02010-09-21 20:42:50 +000012585 // Fallback case.
12586 return 1;
12587}
12588
Evan Cheng206ee9d2006-07-07 08:33:52 +000012589/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012590/// node is a GlobalAddress + offset.
12591bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012592 const GlobalValue* &GA,
12593 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012594 if (N->getOpcode() == X86ISD::Wrapper) {
12595 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012596 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012597 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012598 return true;
12599 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012600 }
Evan Chengad4196b2008-05-12 19:56:52 +000012601 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012602}
12603
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012604/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12605/// same as extracting the high 128-bit part of 256-bit vector and then
12606/// inserting the result into the low part of a new 256-bit vector
12607static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12608 EVT VT = SVOp->getValueType(0);
12609 int NumElems = VT.getVectorNumElements();
12610
12611 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12612 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12613 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12614 SVOp->getMaskElt(j) >= 0)
12615 return false;
12616
12617 return true;
12618}
12619
12620/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12621/// same as extracting the low 128-bit part of 256-bit vector and then
12622/// inserting the result into the high part of a new 256-bit vector
12623static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12624 EVT VT = SVOp->getValueType(0);
12625 int NumElems = VT.getVectorNumElements();
12626
12627 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12628 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12629 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12630 SVOp->getMaskElt(j) >= 0)
12631 return false;
12632
12633 return true;
12634}
12635
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012636/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12637static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12638 TargetLowering::DAGCombinerInfo &DCI) {
12639 DebugLoc dl = N->getDebugLoc();
12640 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12641 SDValue V1 = SVOp->getOperand(0);
12642 SDValue V2 = SVOp->getOperand(1);
12643 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012644 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012645
12646 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12647 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12648 //
12649 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012650 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012651 // V UNDEF BUILD_VECTOR UNDEF
12652 // \ / \ /
12653 // CONCAT_VECTOR CONCAT_VECTOR
12654 // \ /
12655 // \ /
12656 // RESULT: V + zero extended
12657 //
12658 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12659 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12660 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12661 return SDValue();
12662
12663 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12664 return SDValue();
12665
12666 // To match the shuffle mask, the first half of the mask should
12667 // be exactly the first vector, and all the rest a splat with the
12668 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012669 for (int i = 0; i < NumElems/2; ++i)
12670 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12671 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12672 return SDValue();
12673
12674 // Emit a zeroed vector and insert the desired subvector on its
12675 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012676 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012677 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12678 DAG.getConstant(0, MVT::i32), DAG, dl);
12679 return DCI.CombineTo(N, InsV);
12680 }
12681
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012682 //===--------------------------------------------------------------------===//
12683 // Combine some shuffles into subvector extracts and inserts:
12684 //
12685
12686 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12687 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12688 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12689 DAG, dl);
12690 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12691 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12692 return DCI.CombineTo(N, InsV);
12693 }
12694
12695 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12696 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12697 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12698 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12699 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12700 return DCI.CombineTo(N, InsV);
12701 }
12702
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012703 return SDValue();
12704}
12705
12706/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012707static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012708 TargetLowering::DAGCombinerInfo &DCI,
12709 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012710 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012711 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012712
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012713 // Don't create instructions with illegal types after legalize types has run.
12714 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12715 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12716 return SDValue();
12717
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012718 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12719 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12720 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012721 return PerformShuffleCombine256(N, DAG, DCI);
12722
12723 // Only handle 128 wide vector from here on.
12724 if (VT.getSizeInBits() != 128)
12725 return SDValue();
12726
12727 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12728 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12729 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012730 SmallVector<SDValue, 16> Elts;
12731 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012732 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012733
Nate Begemanfdea31a2010-03-24 20:49:50 +000012734 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012735}
Evan Chengd880b972008-05-09 21:53:03 +000012736
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012737/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12738/// generation and convert it from being a bunch of shuffles and extracts
12739/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012740static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12741 const TargetLowering &TLI) {
12742 SDValue InputVector = N->getOperand(0);
12743
12744 // Only operate on vectors of 4 elements, where the alternative shuffling
12745 // gets to be more expensive.
12746 if (InputVector.getValueType() != MVT::v4i32)
12747 return SDValue();
12748
12749 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12750 // single use which is a sign-extend or zero-extend, and all elements are
12751 // used.
12752 SmallVector<SDNode *, 4> Uses;
12753 unsigned ExtractedElements = 0;
12754 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12755 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12756 if (UI.getUse().getResNo() != InputVector.getResNo())
12757 return SDValue();
12758
12759 SDNode *Extract = *UI;
12760 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12761 return SDValue();
12762
12763 if (Extract->getValueType(0) != MVT::i32)
12764 return SDValue();
12765 if (!Extract->hasOneUse())
12766 return SDValue();
12767 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12768 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12769 return SDValue();
12770 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12771 return SDValue();
12772
12773 // Record which element was extracted.
12774 ExtractedElements |=
12775 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12776
12777 Uses.push_back(Extract);
12778 }
12779
12780 // If not all the elements were used, this may not be worthwhile.
12781 if (ExtractedElements != 15)
12782 return SDValue();
12783
12784 // Ok, we've now decided to do the transformation.
12785 DebugLoc dl = InputVector.getDebugLoc();
12786
12787 // Store the value to a temporary stack slot.
12788 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012789 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12790 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012791
12792 // Replace each use (extract) with a load of the appropriate element.
12793 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12794 UE = Uses.end(); UI != UE; ++UI) {
12795 SDNode *Extract = *UI;
12796
Nadav Rotem86694292011-05-17 08:31:57 +000012797 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012798 SDValue Idx = Extract->getOperand(1);
12799 unsigned EltSize =
12800 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12801 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12802 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12803
Nadav Rotem86694292011-05-17 08:31:57 +000012804 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012805 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012806
12807 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012808 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012809 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000012810 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012811
12812 // Replace the exact with the load.
12813 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12814 }
12815
12816 // The replacement was made in place; don't return anything.
12817 return SDValue();
12818}
12819
Duncan Sands6bcd2192011-09-17 16:49:39 +000012820/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
12821/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012822static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012823 const X86Subtarget *Subtarget) {
12824 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012825 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012826 // Get the LHS/RHS of the select.
12827 SDValue LHS = N->getOperand(1);
12828 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000012829 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000012830
Dan Gohman670e5392009-09-21 18:03:22 +000012831 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012832 // instructions match the semantics of the common C idiom x<y?x:y but not
12833 // x<=y?x:y, because of how they handle negative zero (which can be
12834 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000012835 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
12836 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
12837 (Subtarget->hasXMMInt() ||
12838 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012839 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012840
Chris Lattner47b4ce82009-03-11 05:48:52 +000012841 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012842 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012843 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12844 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012845 switch (CC) {
12846 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012847 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012848 // Converting this to a min would handle NaNs incorrectly, and swapping
12849 // the operands would cause it to handle comparisons between positive
12850 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012851 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012852 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012853 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12854 break;
12855 std::swap(LHS, RHS);
12856 }
Dan Gohman670e5392009-09-21 18:03:22 +000012857 Opcode = X86ISD::FMIN;
12858 break;
12859 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012860 // Converting this to a min would handle comparisons between positive
12861 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012862 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012863 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12864 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012865 Opcode = X86ISD::FMIN;
12866 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012867 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012868 // Converting this to a min would handle both negative zeros and NaNs
12869 // incorrectly, but we can swap the operands to fix both.
12870 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012871 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012872 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012873 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012874 Opcode = X86ISD::FMIN;
12875 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012876
Dan Gohman670e5392009-09-21 18:03:22 +000012877 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012878 // Converting this to a max would handle comparisons between positive
12879 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012880 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012881 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012882 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012883 Opcode = X86ISD::FMAX;
12884 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012885 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012886 // Converting this to a max would handle NaNs incorrectly, and swapping
12887 // the operands would cause it to handle comparisons between positive
12888 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012889 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012890 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012891 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12892 break;
12893 std::swap(LHS, RHS);
12894 }
Dan Gohman670e5392009-09-21 18:03:22 +000012895 Opcode = X86ISD::FMAX;
12896 break;
12897 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012898 // Converting this to a max would handle both negative zeros and NaNs
12899 // incorrectly, but we can swap the operands to fix both.
12900 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012901 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012902 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012903 case ISD::SETGE:
12904 Opcode = X86ISD::FMAX;
12905 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012906 }
Dan Gohman670e5392009-09-21 18:03:22 +000012907 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012908 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12909 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012910 switch (CC) {
12911 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012912 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012913 // Converting this to a min would handle comparisons between positive
12914 // and negative zero incorrectly, and swapping the operands would
12915 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012916 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012917 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012918 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012919 break;
12920 std::swap(LHS, RHS);
12921 }
Dan Gohman670e5392009-09-21 18:03:22 +000012922 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012923 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012924 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012925 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012926 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012927 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12928 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012929 Opcode = X86ISD::FMIN;
12930 break;
12931 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012932 // Converting this to a min would handle both negative zeros and NaNs
12933 // incorrectly, but we can swap the operands to fix both.
12934 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012935 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012936 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012937 case ISD::SETGE:
12938 Opcode = X86ISD::FMIN;
12939 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012940
Dan Gohman670e5392009-09-21 18:03:22 +000012941 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012942 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012943 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012944 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012945 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012946 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012947 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012948 // Converting this to a max would handle comparisons between positive
12949 // and negative zero incorrectly, and swapping the operands would
12950 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012951 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012952 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012953 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012954 break;
12955 std::swap(LHS, RHS);
12956 }
Dan Gohman670e5392009-09-21 18:03:22 +000012957 Opcode = X86ISD::FMAX;
12958 break;
12959 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012960 // Converting this to a max would handle both negative zeros and NaNs
12961 // incorrectly, but we can swap the operands to fix both.
12962 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012963 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012964 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012965 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012966 Opcode = X86ISD::FMAX;
12967 break;
12968 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012969 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012970
Chris Lattner47b4ce82009-03-11 05:48:52 +000012971 if (Opcode)
12972 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012973 }
Eric Christopherfd179292009-08-27 18:07:15 +000012974
Chris Lattnerd1980a52009-03-12 06:52:53 +000012975 // If this is a select between two integer constants, try to do some
12976 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012977 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12978 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012979 // Don't do this for crazy integer types.
12980 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12981 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012982 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012983 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012984
Chris Lattnercee56e72009-03-13 05:53:31 +000012985 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012986 // Efficiently invertible.
12987 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12988 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12989 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12990 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012991 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012992 }
Eric Christopherfd179292009-08-27 18:07:15 +000012993
Chris Lattnerd1980a52009-03-12 06:52:53 +000012994 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012995 if (FalseC->getAPIntValue() == 0 &&
12996 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012997 if (NeedsCondInvert) // Invert the condition if needed.
12998 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12999 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013000
Chris Lattnerd1980a52009-03-12 06:52:53 +000013001 // Zero extend the condition if needed.
13002 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013003
Chris Lattnercee56e72009-03-13 05:53:31 +000013004 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013005 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013006 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013007 }
Eric Christopherfd179292009-08-27 18:07:15 +000013008
Chris Lattner97a29a52009-03-13 05:22:11 +000013009 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013010 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013011 if (NeedsCondInvert) // Invert the condition if needed.
13012 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13013 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013014
Chris Lattner97a29a52009-03-13 05:22:11 +000013015 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013016 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13017 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013018 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013019 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013020 }
Eric Christopherfd179292009-08-27 18:07:15 +000013021
Chris Lattnercee56e72009-03-13 05:53:31 +000013022 // Optimize cases that will turn into an LEA instruction. This requires
13023 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013024 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013025 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013026 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013027
Chris Lattnercee56e72009-03-13 05:53:31 +000013028 bool isFastMultiplier = false;
13029 if (Diff < 10) {
13030 switch ((unsigned char)Diff) {
13031 default: break;
13032 case 1: // result = add base, cond
13033 case 2: // result = lea base( , cond*2)
13034 case 3: // result = lea base(cond, cond*2)
13035 case 4: // result = lea base( , cond*4)
13036 case 5: // result = lea base(cond, cond*4)
13037 case 8: // result = lea base( , cond*8)
13038 case 9: // result = lea base(cond, cond*8)
13039 isFastMultiplier = true;
13040 break;
13041 }
13042 }
Eric Christopherfd179292009-08-27 18:07:15 +000013043
Chris Lattnercee56e72009-03-13 05:53:31 +000013044 if (isFastMultiplier) {
13045 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13046 if (NeedsCondInvert) // Invert the condition if needed.
13047 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13048 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013049
Chris Lattnercee56e72009-03-13 05:53:31 +000013050 // Zero extend the condition if needed.
13051 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13052 Cond);
13053 // Scale the condition by the difference.
13054 if (Diff != 1)
13055 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13056 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013057
Chris Lattnercee56e72009-03-13 05:53:31 +000013058 // Add the base if non-zero.
13059 if (FalseC->getAPIntValue() != 0)
13060 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13061 SDValue(FalseC, 0));
13062 return Cond;
13063 }
Eric Christopherfd179292009-08-27 18:07:15 +000013064 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013065 }
13066 }
Eric Christopherfd179292009-08-27 18:07:15 +000013067
Dan Gohman475871a2008-07-27 21:46:04 +000013068 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013069}
13070
Chris Lattnerd1980a52009-03-12 06:52:53 +000013071/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13072static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13073 TargetLowering::DAGCombinerInfo &DCI) {
13074 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013075
Chris Lattnerd1980a52009-03-12 06:52:53 +000013076 // If the flag operand isn't dead, don't touch this CMOV.
13077 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13078 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013079
Evan Chengb5a55d92011-05-24 01:48:22 +000013080 SDValue FalseOp = N->getOperand(0);
13081 SDValue TrueOp = N->getOperand(1);
13082 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13083 SDValue Cond = N->getOperand(3);
13084 if (CC == X86::COND_E || CC == X86::COND_NE) {
13085 switch (Cond.getOpcode()) {
13086 default: break;
13087 case X86ISD::BSR:
13088 case X86ISD::BSF:
13089 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13090 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13091 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13092 }
13093 }
13094
Chris Lattnerd1980a52009-03-12 06:52:53 +000013095 // If this is a select between two integer constants, try to do some
13096 // optimizations. Note that the operands are ordered the opposite of SELECT
13097 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013098 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13099 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013100 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13101 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013102 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13103 CC = X86::GetOppositeBranchCondition(CC);
13104 std::swap(TrueC, FalseC);
13105 }
Eric Christopherfd179292009-08-27 18:07:15 +000013106
Chris Lattnerd1980a52009-03-12 06:52:53 +000013107 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013108 // This is efficient for any integer data type (including i8/i16) and
13109 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013110 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013111 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13112 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013113
Chris Lattnerd1980a52009-03-12 06:52:53 +000013114 // Zero extend the condition if needed.
13115 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013116
Chris Lattnerd1980a52009-03-12 06:52:53 +000013117 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13118 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013119 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013120 if (N->getNumValues() == 2) // Dead flag value?
13121 return DCI.CombineTo(N, Cond, SDValue());
13122 return Cond;
13123 }
Eric Christopherfd179292009-08-27 18:07:15 +000013124
Chris Lattnercee56e72009-03-13 05:53:31 +000013125 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13126 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013127 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013128 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13129 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013130
Chris Lattner97a29a52009-03-13 05:22:11 +000013131 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013132 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13133 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013134 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13135 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013136
Chris Lattner97a29a52009-03-13 05:22:11 +000013137 if (N->getNumValues() == 2) // Dead flag value?
13138 return DCI.CombineTo(N, Cond, SDValue());
13139 return Cond;
13140 }
Eric Christopherfd179292009-08-27 18:07:15 +000013141
Chris Lattnercee56e72009-03-13 05:53:31 +000013142 // Optimize cases that will turn into an LEA instruction. This requires
13143 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013144 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013145 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013146 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013147
Chris Lattnercee56e72009-03-13 05:53:31 +000013148 bool isFastMultiplier = false;
13149 if (Diff < 10) {
13150 switch ((unsigned char)Diff) {
13151 default: break;
13152 case 1: // result = add base, cond
13153 case 2: // result = lea base( , cond*2)
13154 case 3: // result = lea base(cond, cond*2)
13155 case 4: // result = lea base( , cond*4)
13156 case 5: // result = lea base(cond, cond*4)
13157 case 8: // result = lea base( , cond*8)
13158 case 9: // result = lea base(cond, cond*8)
13159 isFastMultiplier = true;
13160 break;
13161 }
13162 }
Eric Christopherfd179292009-08-27 18:07:15 +000013163
Chris Lattnercee56e72009-03-13 05:53:31 +000013164 if (isFastMultiplier) {
13165 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013166 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13167 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013168 // Zero extend the condition if needed.
13169 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13170 Cond);
13171 // Scale the condition by the difference.
13172 if (Diff != 1)
13173 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13174 DAG.getConstant(Diff, Cond.getValueType()));
13175
13176 // Add the base if non-zero.
13177 if (FalseC->getAPIntValue() != 0)
13178 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13179 SDValue(FalseC, 0));
13180 if (N->getNumValues() == 2) // Dead flag value?
13181 return DCI.CombineTo(N, Cond, SDValue());
13182 return Cond;
13183 }
Eric Christopherfd179292009-08-27 18:07:15 +000013184 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013185 }
13186 }
13187 return SDValue();
13188}
13189
13190
Evan Cheng0b0cd912009-03-28 05:57:29 +000013191/// PerformMulCombine - Optimize a single multiply with constant into two
13192/// in order to implement it with two cheaper instructions, e.g.
13193/// LEA + SHL, LEA + LEA.
13194static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13195 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013196 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13197 return SDValue();
13198
Owen Andersone50ed302009-08-10 22:56:29 +000013199 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013200 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013201 return SDValue();
13202
13203 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13204 if (!C)
13205 return SDValue();
13206 uint64_t MulAmt = C->getZExtValue();
13207 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13208 return SDValue();
13209
13210 uint64_t MulAmt1 = 0;
13211 uint64_t MulAmt2 = 0;
13212 if ((MulAmt % 9) == 0) {
13213 MulAmt1 = 9;
13214 MulAmt2 = MulAmt / 9;
13215 } else if ((MulAmt % 5) == 0) {
13216 MulAmt1 = 5;
13217 MulAmt2 = MulAmt / 5;
13218 } else if ((MulAmt % 3) == 0) {
13219 MulAmt1 = 3;
13220 MulAmt2 = MulAmt / 3;
13221 }
13222 if (MulAmt2 &&
13223 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13224 DebugLoc DL = N->getDebugLoc();
13225
13226 if (isPowerOf2_64(MulAmt2) &&
13227 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13228 // If second multiplifer is pow2, issue it first. We want the multiply by
13229 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13230 // is an add.
13231 std::swap(MulAmt1, MulAmt2);
13232
13233 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013234 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013235 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013236 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013237 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013238 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013239 DAG.getConstant(MulAmt1, VT));
13240
Eric Christopherfd179292009-08-27 18:07:15 +000013241 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013242 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013243 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013244 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013245 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013246 DAG.getConstant(MulAmt2, VT));
13247
13248 // Do not add new nodes to DAG combiner worklist.
13249 DCI.CombineTo(N, NewMul, false);
13250 }
13251 return SDValue();
13252}
13253
Evan Chengad9c0a32009-12-15 00:53:42 +000013254static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13255 SDValue N0 = N->getOperand(0);
13256 SDValue N1 = N->getOperand(1);
13257 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13258 EVT VT = N0.getValueType();
13259
13260 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13261 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013262 if (VT.isInteger() && !VT.isVector() &&
13263 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013264 N0.getOperand(1).getOpcode() == ISD::Constant) {
13265 SDValue N00 = N0.getOperand(0);
13266 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13267 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13268 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13269 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13270 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13271 APInt ShAmt = N1C->getAPIntValue();
13272 Mask = Mask.shl(ShAmt);
13273 if (Mask != 0)
13274 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13275 N00, DAG.getConstant(Mask, VT));
13276 }
13277 }
13278
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013279
13280 // Hardware support for vector shifts is sparse which makes us scalarize the
13281 // vector operations in many cases. Also, on sandybridge ADD is faster than
13282 // shl.
13283 // (shl V, 1) -> add V,V
13284 if (isSplatVector(N1.getNode())) {
13285 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13286 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13287 // We shift all of the values by one. In many cases we do not have
13288 // hardware support for this operation. This is better expressed as an ADD
13289 // of two values.
13290 if (N1C && (1 == N1C->getZExtValue())) {
13291 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13292 }
13293 }
13294
Evan Chengad9c0a32009-12-15 00:53:42 +000013295 return SDValue();
13296}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013297
Nate Begeman740ab032009-01-26 00:52:55 +000013298/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13299/// when possible.
13300static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13301 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013302 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013303 if (N->getOpcode() == ISD::SHL) {
13304 SDValue V = PerformSHLCombine(N, DAG);
13305 if (V.getNode()) return V;
13306 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013307
Nate Begeman740ab032009-01-26 00:52:55 +000013308 // On X86 with SSE2 support, we can transform this to a vector shift if
13309 // all elements are shifted by the same amount. We can't do this in legalize
13310 // because the a constant vector is typically transformed to a constant pool
13311 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013312 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013313 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013314
Craig Topper7be5dfd2011-11-12 09:58:49 +000013315 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
13316 (!Subtarget->hasAVX2() ||
13317 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013318 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013319
Mon P Wang3becd092009-01-28 08:12:05 +000013320 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013321 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013322 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013323 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013324 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13325 unsigned NumElts = VT.getVectorNumElements();
13326 unsigned i = 0;
13327 for (; i != NumElts; ++i) {
13328 SDValue Arg = ShAmtOp.getOperand(i);
13329 if (Arg.getOpcode() == ISD::UNDEF) continue;
13330 BaseShAmt = Arg;
13331 break;
13332 }
13333 for (; i != NumElts; ++i) {
13334 SDValue Arg = ShAmtOp.getOperand(i);
13335 if (Arg.getOpcode() == ISD::UNDEF) continue;
13336 if (Arg != BaseShAmt) {
13337 return SDValue();
13338 }
13339 }
13340 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013341 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013342 SDValue InVec = ShAmtOp.getOperand(0);
13343 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13344 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13345 unsigned i = 0;
13346 for (; i != NumElts; ++i) {
13347 SDValue Arg = InVec.getOperand(i);
13348 if (Arg.getOpcode() == ISD::UNDEF) continue;
13349 BaseShAmt = Arg;
13350 break;
13351 }
13352 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13353 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013354 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013355 if (C->getZExtValue() == SplatIdx)
13356 BaseShAmt = InVec.getOperand(1);
13357 }
13358 }
13359 if (BaseShAmt.getNode() == 0)
13360 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13361 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013362 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013363 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013364
Mon P Wangefa42202009-09-03 19:56:25 +000013365 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013366 if (EltVT.bitsGT(MVT::i32))
13367 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13368 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013369 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013370
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013371 // The shift amount is identical so we can do a vector shift.
13372 SDValue ValOp = N->getOperand(0);
13373 switch (N->getOpcode()) {
13374 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013375 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013376 break;
13377 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013378 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013379 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013380 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013381 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013382 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013383 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013384 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013385 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013386 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013387 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013388 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013389 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013390 if (VT == MVT::v4i64)
13391 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13392 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
13393 ValOp, BaseShAmt);
13394 if (VT == MVT::v8i32)
13395 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13396 DAG.getConstant(Intrinsic::x86_avx2_pslli_d, MVT::i32),
13397 ValOp, BaseShAmt);
13398 if (VT == MVT::v16i16)
13399 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13400 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
13401 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013402 break;
13403 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013404 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013405 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013406 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013407 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013408 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013409 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013410 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013411 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013412 if (VT == MVT::v8i32)
13413 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13414 DAG.getConstant(Intrinsic::x86_avx2_psrai_d, MVT::i32),
13415 ValOp, BaseShAmt);
13416 if (VT == MVT::v16i16)
13417 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13418 DAG.getConstant(Intrinsic::x86_avx2_psrai_w, MVT::i32),
13419 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013420 break;
13421 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013422 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013423 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013424 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013425 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013426 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013427 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013428 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013429 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013430 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013431 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013432 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013433 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013434 if (VT == MVT::v4i64)
13435 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13436 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
13437 ValOp, BaseShAmt);
13438 if (VT == MVT::v8i32)
13439 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13440 DAG.getConstant(Intrinsic::x86_avx2_psrli_d, MVT::i32),
13441 ValOp, BaseShAmt);
13442 if (VT == MVT::v16i16)
13443 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13444 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
13445 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013446 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013447 }
13448 return SDValue();
13449}
13450
Nate Begemanb65c1752010-12-17 22:55:37 +000013451
Stuart Hastings865f0932011-06-03 23:53:54 +000013452// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13453// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13454// and friends. Likewise for OR -> CMPNEQSS.
13455static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13456 TargetLowering::DAGCombinerInfo &DCI,
13457 const X86Subtarget *Subtarget) {
13458 unsigned opcode;
13459
13460 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13461 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013462 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013463 SDValue N0 = N->getOperand(0);
13464 SDValue N1 = N->getOperand(1);
13465 SDValue CMP0 = N0->getOperand(1);
13466 SDValue CMP1 = N1->getOperand(1);
13467 DebugLoc DL = N->getDebugLoc();
13468
13469 // The SETCCs should both refer to the same CMP.
13470 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13471 return SDValue();
13472
13473 SDValue CMP00 = CMP0->getOperand(0);
13474 SDValue CMP01 = CMP0->getOperand(1);
13475 EVT VT = CMP00.getValueType();
13476
13477 if (VT == MVT::f32 || VT == MVT::f64) {
13478 bool ExpectingFlags = false;
13479 // Check for any users that want flags:
13480 for (SDNode::use_iterator UI = N->use_begin(),
13481 UE = N->use_end();
13482 !ExpectingFlags && UI != UE; ++UI)
13483 switch (UI->getOpcode()) {
13484 default:
13485 case ISD::BR_CC:
13486 case ISD::BRCOND:
13487 case ISD::SELECT:
13488 ExpectingFlags = true;
13489 break;
13490 case ISD::CopyToReg:
13491 case ISD::SIGN_EXTEND:
13492 case ISD::ZERO_EXTEND:
13493 case ISD::ANY_EXTEND:
13494 break;
13495 }
13496
13497 if (!ExpectingFlags) {
13498 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13499 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13500
13501 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13502 X86::CondCode tmp = cc0;
13503 cc0 = cc1;
13504 cc1 = tmp;
13505 }
13506
13507 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13508 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13509 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13510 X86ISD::NodeType NTOperator = is64BitFP ?
13511 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13512 // FIXME: need symbolic constants for these magic numbers.
13513 // See X86ATTInstPrinter.cpp:printSSECC().
13514 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13515 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13516 DAG.getConstant(x86cc, MVT::i8));
13517 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13518 OnesOrZeroesF);
13519 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13520 DAG.getConstant(1, MVT::i32));
13521 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13522 return OneBitOfTruth;
13523 }
13524 }
13525 }
13526 }
13527 return SDValue();
13528}
13529
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013530/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13531/// so it can be folded inside ANDNP.
13532static bool CanFoldXORWithAllOnes(const SDNode *N) {
13533 EVT VT = N->getValueType(0);
13534
13535 // Match direct AllOnes for 128 and 256-bit vectors
13536 if (ISD::isBuildVectorAllOnes(N))
13537 return true;
13538
13539 // Look through a bit convert.
13540 if (N->getOpcode() == ISD::BITCAST)
13541 N = N->getOperand(0).getNode();
13542
13543 // Sometimes the operand may come from a insert_subvector building a 256-bit
13544 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013545 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013546 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13547 SDValue V1 = N->getOperand(0);
13548 SDValue V2 = N->getOperand(1);
13549
13550 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13551 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13552 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13553 ISD::isBuildVectorAllOnes(V2.getNode()))
13554 return true;
13555 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013556
13557 return false;
13558}
13559
Nate Begemanb65c1752010-12-17 22:55:37 +000013560static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13561 TargetLowering::DAGCombinerInfo &DCI,
13562 const X86Subtarget *Subtarget) {
13563 if (DCI.isBeforeLegalizeOps())
13564 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013565
Stuart Hastings865f0932011-06-03 23:53:54 +000013566 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13567 if (R.getNode())
13568 return R;
13569
Craig Topper54a11172011-10-14 07:06:56 +000013570 EVT VT = N->getValueType(0);
13571
Craig Topperb4c94572011-10-21 06:55:01 +000013572 // Create ANDN, BLSI, and BLSR instructions
13573 // BLSI is X & (-X)
13574 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013575 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13576 SDValue N0 = N->getOperand(0);
13577 SDValue N1 = N->getOperand(1);
13578 DebugLoc DL = N->getDebugLoc();
13579
13580 // Check LHS for not
13581 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13582 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13583 // Check RHS for not
13584 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13585 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13586
Craig Topperb4c94572011-10-21 06:55:01 +000013587 // Check LHS for neg
13588 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13589 isZero(N0.getOperand(0)))
13590 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13591
13592 // Check RHS for neg
13593 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13594 isZero(N1.getOperand(0)))
13595 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13596
13597 // Check LHS for X-1
13598 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13599 isAllOnes(N0.getOperand(1)))
13600 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13601
13602 // Check RHS for X-1
13603 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13604 isAllOnes(N1.getOperand(1)))
13605 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13606
Craig Topper54a11172011-10-14 07:06:56 +000013607 return SDValue();
13608 }
13609
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013610 // Want to form ANDNP nodes:
13611 // 1) In the hopes of then easily combining them with OR and AND nodes
13612 // to form PBLEND/PSIGN.
13613 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013614 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013615 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013616
Nate Begemanb65c1752010-12-17 22:55:37 +000013617 SDValue N0 = N->getOperand(0);
13618 SDValue N1 = N->getOperand(1);
13619 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013620
Nate Begemanb65c1752010-12-17 22:55:37 +000013621 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013622 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013623 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13624 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013625 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013626
13627 // Check RHS for vnot
13628 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013629 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13630 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013631 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013632
Nate Begemanb65c1752010-12-17 22:55:37 +000013633 return SDValue();
13634}
13635
Evan Cheng760d1942010-01-04 21:22:48 +000013636static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013637 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013638 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013639 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013640 return SDValue();
13641
Stuart Hastings865f0932011-06-03 23:53:54 +000013642 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13643 if (R.getNode())
13644 return R;
13645
Evan Cheng760d1942010-01-04 21:22:48 +000013646 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000013647
Evan Cheng760d1942010-01-04 21:22:48 +000013648 SDValue N0 = N->getOperand(0);
13649 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013650
Nate Begemanb65c1752010-12-17 22:55:37 +000013651 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000013652 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperc0d82852011-11-22 00:44:41 +000013653 if (!Subtarget->hasSSSE3orAVX() ||
Craig Topper1666cb62011-11-19 07:07:26 +000013654 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
13655 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013656
Craig Topper1666cb62011-11-19 07:07:26 +000013657 // Canonicalize pandn to RHS
13658 if (N0.getOpcode() == X86ISD::ANDNP)
13659 std::swap(N0, N1);
13660 // or (and (m, x), (pandn m, y))
13661 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
13662 SDValue Mask = N1.getOperand(0);
13663 SDValue X = N1.getOperand(1);
13664 SDValue Y;
13665 if (N0.getOperand(0) == Mask)
13666 Y = N0.getOperand(1);
13667 if (N0.getOperand(1) == Mask)
13668 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013669
Craig Topper1666cb62011-11-19 07:07:26 +000013670 // Check to see if the mask appeared in both the AND and ANDNP and
13671 if (!Y.getNode())
13672 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013673
Craig Topper1666cb62011-11-19 07:07:26 +000013674 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13675 if (Mask.getOpcode() != ISD::BITCAST ||
13676 X.getOpcode() != ISD::BITCAST ||
13677 Y.getOpcode() != ISD::BITCAST)
13678 return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013679
Craig Topper1666cb62011-11-19 07:07:26 +000013680 // Look through mask bitcast.
13681 Mask = Mask.getOperand(0);
13682 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013683
Craig Topper1666cb62011-11-19 07:07:26 +000013684 // Validate that the Mask operand is a vector sra node. The sra node
13685 // will be an intrinsic.
13686 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13687 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013688
Craig Topper1666cb62011-11-19 07:07:26 +000013689 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13690 // there is no psrai.b
13691 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13692 case Intrinsic::x86_sse2_psrai_w:
13693 case Intrinsic::x86_sse2_psrai_d:
13694 case Intrinsic::x86_avx2_psrai_w:
13695 case Intrinsic::x86_avx2_psrai_d:
13696 break;
13697 default: return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013698 }
Craig Topper1666cb62011-11-19 07:07:26 +000013699
13700 // Check that the SRA is all signbits.
13701 SDValue SraC = Mask.getOperand(2);
13702 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13703 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13704 if ((SraAmt + 1) != EltBits)
13705 return SDValue();
13706
13707 DebugLoc DL = N->getDebugLoc();
13708
13709 // Now we know we at least have a plendvb with the mask val. See if
13710 // we can form a psignb/w/d.
13711 // psign = x.type == y.type == mask.type && y = sub(0, x);
13712 X = X.getOperand(0);
13713 Y = Y.getOperand(0);
13714 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13715 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Topper31133842011-11-19 07:33:10 +000013716 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType() &&
13717 (EltBits == 8 || EltBits == 16 || EltBits == 32)) {
13718 SDValue Sign = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X,
13719 Mask.getOperand(1));
13720 return DAG.getNode(ISD::BITCAST, DL, VT, Sign);
Craig Topper1666cb62011-11-19 07:07:26 +000013721 }
13722 // PBLENDVB only available on SSE 4.1
Craig Topperc0d82852011-11-22 00:44:41 +000013723 if (!Subtarget->hasSSE41orAVX())
Craig Topper1666cb62011-11-19 07:07:26 +000013724 return SDValue();
13725
13726 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
13727
13728 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
13729 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
13730 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000013731 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000013732 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000013733 }
13734 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013735
Craig Topper1666cb62011-11-19 07:07:26 +000013736 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
13737 return SDValue();
13738
Nate Begemanb65c1752010-12-17 22:55:37 +000013739 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013740 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13741 std::swap(N0, N1);
13742 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13743 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013744 if (!N0.hasOneUse() || !N1.hasOneUse())
13745 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013746
13747 SDValue ShAmt0 = N0.getOperand(1);
13748 if (ShAmt0.getValueType() != MVT::i8)
13749 return SDValue();
13750 SDValue ShAmt1 = N1.getOperand(1);
13751 if (ShAmt1.getValueType() != MVT::i8)
13752 return SDValue();
13753 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13754 ShAmt0 = ShAmt0.getOperand(0);
13755 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13756 ShAmt1 = ShAmt1.getOperand(0);
13757
13758 DebugLoc DL = N->getDebugLoc();
13759 unsigned Opc = X86ISD::SHLD;
13760 SDValue Op0 = N0.getOperand(0);
13761 SDValue Op1 = N1.getOperand(0);
13762 if (ShAmt0.getOpcode() == ISD::SUB) {
13763 Opc = X86ISD::SHRD;
13764 std::swap(Op0, Op1);
13765 std::swap(ShAmt0, ShAmt1);
13766 }
13767
Evan Cheng8b1190a2010-04-28 01:18:01 +000013768 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013769 if (ShAmt1.getOpcode() == ISD::SUB) {
13770 SDValue Sum = ShAmt1.getOperand(0);
13771 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013772 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13773 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13774 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13775 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013776 return DAG.getNode(Opc, DL, VT,
13777 Op0, Op1,
13778 DAG.getNode(ISD::TRUNCATE, DL,
13779 MVT::i8, ShAmt0));
13780 }
13781 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13782 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13783 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013784 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013785 return DAG.getNode(Opc, DL, VT,
13786 N0.getOperand(0), N1.getOperand(0),
13787 DAG.getNode(ISD::TRUNCATE, DL,
13788 MVT::i8, ShAmt0));
13789 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013790
Evan Cheng760d1942010-01-04 21:22:48 +000013791 return SDValue();
13792}
13793
Craig Topperb4c94572011-10-21 06:55:01 +000013794static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
13795 TargetLowering::DAGCombinerInfo &DCI,
13796 const X86Subtarget *Subtarget) {
13797 if (DCI.isBeforeLegalizeOps())
13798 return SDValue();
13799
13800 EVT VT = N->getValueType(0);
13801
13802 if (VT != MVT::i32 && VT != MVT::i64)
13803 return SDValue();
13804
13805 // Create BLSMSK instructions by finding X ^ (X-1)
13806 SDValue N0 = N->getOperand(0);
13807 SDValue N1 = N->getOperand(1);
13808 DebugLoc DL = N->getDebugLoc();
13809
13810 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13811 isAllOnes(N0.getOperand(1)))
13812 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
13813
13814 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13815 isAllOnes(N1.getOperand(1)))
13816 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
13817
13818 return SDValue();
13819}
13820
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013821/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
13822static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
13823 const X86Subtarget *Subtarget) {
13824 LoadSDNode *Ld = cast<LoadSDNode>(N);
13825 EVT RegVT = Ld->getValueType(0);
13826 EVT MemVT = Ld->getMemoryVT();
13827 DebugLoc dl = Ld->getDebugLoc();
13828 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13829
13830 ISD::LoadExtType Ext = Ld->getExtensionType();
13831
Nadav Rotemca6f2962011-09-18 19:00:23 +000013832 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013833 // shuffle. We need SSE4 for the shuffles.
13834 // TODO: It is possible to support ZExt by zeroing the undef values
13835 // during the shuffle phase or after the shuffle.
13836 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
13837 assert(MemVT != RegVT && "Cannot extend to the same type");
13838 assert(MemVT.isVector() && "Must load a vector from memory");
13839
13840 unsigned NumElems = RegVT.getVectorNumElements();
13841 unsigned RegSz = RegVT.getSizeInBits();
13842 unsigned MemSz = MemVT.getSizeInBits();
13843 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000013844 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013845 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
13846
13847 // Attempt to load the original value using a single load op.
13848 // Find a scalar type which is equal to the loaded word size.
13849 MVT SclrLoadTy = MVT::i8;
13850 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13851 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13852 MVT Tp = (MVT::SimpleValueType)tp;
13853 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
13854 SclrLoadTy = Tp;
13855 break;
13856 }
13857 }
13858
13859 // Proceed if a load word is found.
13860 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
13861
13862 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
13863 RegSz/SclrLoadTy.getSizeInBits());
13864
13865 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
13866 RegSz/MemVT.getScalarType().getSizeInBits());
13867 // Can't shuffle using an illegal type.
13868 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13869
13870 // Perform a single load.
13871 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
13872 Ld->getBasePtr(),
13873 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013874 Ld->isNonTemporal(), Ld->isInvariant(),
13875 Ld->getAlignment());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013876
13877 // Insert the word loaded into a vector.
13878 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
13879 LoadUnitVecVT, ScalarLoad);
13880
13881 // Bitcast the loaded value to a vector of the original element type, in
13882 // the size of the target vector type.
13883 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
13884 unsigned SizeRatio = RegSz/MemSz;
13885
13886 // Redistribute the loaded elements into the different locations.
13887 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13888 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
13889
13890 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
13891 DAG.getUNDEF(SlicedVec.getValueType()),
13892 ShuffleVec.data());
13893
13894 // Bitcast to the requested type.
13895 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
13896 // Replace the original load with the new sequence
13897 // and return the new chain.
13898 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
13899 return SDValue(ScalarLoad.getNode(), 1);
13900 }
13901
13902 return SDValue();
13903}
13904
Chris Lattner149a4e52008-02-22 02:09:43 +000013905/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013906static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013907 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013908 StoreSDNode *St = cast<StoreSDNode>(N);
13909 EVT VT = St->getValue().getValueType();
13910 EVT StVT = St->getMemoryVT();
13911 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013912 SDValue StoredVal = St->getOperand(1);
13913 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13914
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013915 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013916 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13917 // 128-bit ones. If in the future the cost becomes only one memory access the
13918 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013919 if (VT.getSizeInBits() == 256 &&
13920 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13921 StoredVal.getNumOperands() == 2) {
13922
13923 SDValue Value0 = StoredVal.getOperand(0);
13924 SDValue Value1 = StoredVal.getOperand(1);
13925
13926 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13927 SDValue Ptr0 = St->getBasePtr();
13928 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13929
13930 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13931 St->getPointerInfo(), St->isVolatile(),
13932 St->isNonTemporal(), St->getAlignment());
13933 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13934 St->getPointerInfo(), St->isVolatile(),
13935 St->isNonTemporal(), St->getAlignment());
13936 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13937 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013938
13939 // Optimize trunc store (of multiple scalars) to shuffle and store.
13940 // First, pack all of the elements in one place. Next, store to memory
13941 // in fewer chunks.
13942 if (St->isTruncatingStore() && VT.isVector()) {
13943 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13944 unsigned NumElems = VT.getVectorNumElements();
13945 assert(StVT != VT && "Cannot truncate to the same type");
13946 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13947 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13948
13949 // From, To sizes and ElemCount must be pow of two
13950 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013951 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000013952 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013953 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013954
Nadav Rotem614061b2011-08-10 19:30:14 +000013955 unsigned SizeRatio = FromSz / ToSz;
13956
13957 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13958
13959 // Create a type on which we perform the shuffle
13960 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13961 StVT.getScalarType(), NumElems*SizeRatio);
13962
13963 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13964
13965 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13966 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13967 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13968
13969 // Can't shuffle using an illegal type
13970 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13971
13972 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13973 DAG.getUNDEF(WideVec.getValueType()),
13974 ShuffleVec.data());
13975 // At this point all of the data is stored at the bottom of the
13976 // register. We now need to save it to mem.
13977
13978 // Find the largest store unit
13979 MVT StoreType = MVT::i8;
13980 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13981 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13982 MVT Tp = (MVT::SimpleValueType)tp;
13983 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13984 StoreType = Tp;
13985 }
13986
13987 // Bitcast the original vector into a vector of store-size units
13988 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13989 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13990 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13991 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
13992 SmallVector<SDValue, 8> Chains;
13993 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
13994 TLI.getPointerTy());
13995 SDValue Ptr = St->getBasePtr();
13996
13997 // Perform one or more big stores into memory.
13998 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
13999 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14000 StoreType, ShuffWide,
14001 DAG.getIntPtrConstant(i));
14002 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14003 St->getPointerInfo(), St->isVolatile(),
14004 St->isNonTemporal(), St->getAlignment());
14005 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14006 Chains.push_back(Ch);
14007 }
14008
14009 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14010 Chains.size());
14011 }
14012
14013
Chris Lattner149a4e52008-02-22 02:09:43 +000014014 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14015 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014016 // A preferable solution to the general problem is to figure out the right
14017 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014018
14019 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014020 if (VT.getSizeInBits() != 64)
14021 return SDValue();
14022
Devang Patel578efa92009-06-05 21:57:13 +000014023 const Function *F = DAG.getMachineFunction().getFunction();
14024 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014025 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000014026 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000014027 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014028 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014029 isa<LoadSDNode>(St->getValue()) &&
14030 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14031 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014032 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014033 LoadSDNode *Ld = 0;
14034 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014035 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014036 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014037 // Must be a store of a load. We currently handle two cases: the load
14038 // is a direct child, and it's under an intervening TokenFactor. It is
14039 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014040 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014041 Ld = cast<LoadSDNode>(St->getChain());
14042 else if (St->getValue().hasOneUse() &&
14043 ChainVal->getOpcode() == ISD::TokenFactor) {
14044 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014045 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014046 TokenFactorIndex = i;
14047 Ld = cast<LoadSDNode>(St->getValue());
14048 } else
14049 Ops.push_back(ChainVal->getOperand(i));
14050 }
14051 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014052
Evan Cheng536e6672009-03-12 05:59:15 +000014053 if (!Ld || !ISD::isNormalLoad(Ld))
14054 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014055
Evan Cheng536e6672009-03-12 05:59:15 +000014056 // If this is not the MMX case, i.e. we are just turning i64 load/store
14057 // into f64 load/store, avoid the transformation if there are multiple
14058 // uses of the loaded value.
14059 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14060 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014061
Evan Cheng536e6672009-03-12 05:59:15 +000014062 DebugLoc LdDL = Ld->getDebugLoc();
14063 DebugLoc StDL = N->getDebugLoc();
14064 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14065 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14066 // pair instead.
14067 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014068 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014069 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14070 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014071 Ld->isNonTemporal(), Ld->isInvariant(),
14072 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014073 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014074 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014075 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014076 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014077 Ops.size());
14078 }
Evan Cheng536e6672009-03-12 05:59:15 +000014079 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014080 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014081 St->isVolatile(), St->isNonTemporal(),
14082 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014083 }
Evan Cheng536e6672009-03-12 05:59:15 +000014084
14085 // Otherwise, lower to two pairs of 32-bit loads / stores.
14086 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014087 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14088 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014089
Owen Anderson825b72b2009-08-11 20:47:22 +000014090 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014091 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014092 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014093 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014094 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014095 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014096 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014097 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014098 MinAlign(Ld->getAlignment(), 4));
14099
14100 SDValue NewChain = LoLd.getValue(1);
14101 if (TokenFactorIndex != -1) {
14102 Ops.push_back(LoLd);
14103 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014104 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014105 Ops.size());
14106 }
14107
14108 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014109 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14110 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014111
14112 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014113 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014114 St->isVolatile(), St->isNonTemporal(),
14115 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014116 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014117 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014118 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014119 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014120 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014121 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014122 }
Dan Gohman475871a2008-07-27 21:46:04 +000014123 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014124}
14125
Duncan Sands17470be2011-09-22 20:15:48 +000014126/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14127/// and return the operands for the horizontal operation in LHS and RHS. A
14128/// horizontal operation performs the binary operation on successive elements
14129/// of its first operand, then on successive elements of its second operand,
14130/// returning the resulting values in a vector. For example, if
14131/// A = < float a0, float a1, float a2, float a3 >
14132/// and
14133/// B = < float b0, float b1, float b2, float b3 >
14134/// then the result of doing a horizontal operation on A and B is
14135/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14136/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14137/// A horizontal-op B, for some already available A and B, and if so then LHS is
14138/// set to A, RHS to B, and the routine returns 'true'.
14139/// Note that the binary operation should have the property that if one of the
14140/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014141static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000014142 // Look for the following pattern: if
14143 // A = < float a0, float a1, float a2, float a3 >
14144 // B = < float b0, float b1, float b2, float b3 >
14145 // and
14146 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14147 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14148 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14149 // which is A horizontal-op B.
14150
14151 // At least one of the operands should be a vector shuffle.
14152 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14153 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14154 return false;
14155
14156 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000014157
14158 assert((VT.is128BitVector() || VT.is256BitVector()) &&
14159 "Unsupported vector type for horizontal add/sub");
14160
14161 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
14162 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000014163 unsigned NumElts = VT.getVectorNumElements();
14164 unsigned NumLanes = VT.getSizeInBits()/128;
14165 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000014166 assert((NumLaneElts % 2 == 0) &&
14167 "Vector type should have an even number of elements in each lane");
14168 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000014169
14170 // View LHS in the form
14171 // LHS = VECTOR_SHUFFLE A, B, LMask
14172 // If LHS is not a shuffle then pretend it is the shuffle
14173 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14174 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14175 // type VT.
14176 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000014177 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014178 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14179 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14180 A = LHS.getOperand(0);
14181 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14182 B = LHS.getOperand(1);
14183 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
14184 } else {
14185 if (LHS.getOpcode() != ISD::UNDEF)
14186 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014187 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014188 LMask[i] = i;
14189 }
14190
14191 // Likewise, view RHS in the form
14192 // RHS = VECTOR_SHUFFLE C, D, RMask
14193 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000014194 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014195 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14196 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14197 C = RHS.getOperand(0);
14198 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14199 D = RHS.getOperand(1);
14200 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
14201 } else {
14202 if (RHS.getOpcode() != ISD::UNDEF)
14203 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014204 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014205 RMask[i] = i;
14206 }
14207
14208 // Check that the shuffles are both shuffling the same vectors.
14209 if (!(A == C && B == D) && !(A == D && B == C))
14210 return false;
14211
14212 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14213 if (!A.getNode() && !B.getNode())
14214 return false;
14215
14216 // If A and B occur in reverse order in RHS, then "swap" them (which means
14217 // rewriting the mask).
14218 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000014219 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014220
14221 // At this point LHS and RHS are equivalent to
14222 // LHS = VECTOR_SHUFFLE A, B, LMask
14223 // RHS = VECTOR_SHUFFLE A, B, RMask
14224 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000014225 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000014226 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000014227
Craig Topperf8363302011-12-02 08:18:41 +000014228 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014229 if (LIdx < 0 || RIdx < 0 ||
14230 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
14231 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000014232 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000014233
Craig Topperf8363302011-12-02 08:18:41 +000014234 // Check that successive elements are being operated on. If not, this is
14235 // not a horizontal operation.
14236 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
14237 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000014238 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000014239 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000014240 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000014241 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000014242 }
14243
14244 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14245 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14246 return true;
14247}
14248
14249/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14250static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14251 const X86Subtarget *Subtarget) {
14252 EVT VT = N->getValueType(0);
14253 SDValue LHS = N->getOperand(0);
14254 SDValue RHS = N->getOperand(1);
14255
14256 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topper138a5c62011-12-02 07:16:01 +000014257 if (((Subtarget->hasSSE3orAVX() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
14258 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014259 isHorizontalBinOp(LHS, RHS, true))
14260 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14261 return SDValue();
14262}
14263
14264/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14265static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14266 const X86Subtarget *Subtarget) {
14267 EVT VT = N->getValueType(0);
14268 SDValue LHS = N->getOperand(0);
14269 SDValue RHS = N->getOperand(1);
14270
14271 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topper138a5c62011-12-02 07:16:01 +000014272 if (((Subtarget->hasSSE3orAVX() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
14273 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014274 isHorizontalBinOp(LHS, RHS, false))
14275 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14276 return SDValue();
14277}
14278
Chris Lattner6cf73262008-01-25 06:14:17 +000014279/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14280/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014281static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014282 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14283 // F[X]OR(0.0, x) -> x
14284 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014285 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14286 if (C->getValueAPF().isPosZero())
14287 return N->getOperand(1);
14288 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14289 if (C->getValueAPF().isPosZero())
14290 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014291 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014292}
14293
14294/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014295static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014296 // FAND(0.0, x) -> 0.0
14297 // FAND(x, 0.0) -> 0.0
14298 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14299 if (C->getValueAPF().isPosZero())
14300 return N->getOperand(0);
14301 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14302 if (C->getValueAPF().isPosZero())
14303 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014304 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014305}
14306
Dan Gohmane5af2d32009-01-29 01:59:02 +000014307static SDValue PerformBTCombine(SDNode *N,
14308 SelectionDAG &DAG,
14309 TargetLowering::DAGCombinerInfo &DCI) {
14310 // BT ignores high bits in the bit index operand.
14311 SDValue Op1 = N->getOperand(1);
14312 if (Op1.hasOneUse()) {
14313 unsigned BitWidth = Op1.getValueSizeInBits();
14314 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14315 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014316 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14317 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014318 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014319 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14320 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14321 DCI.CommitTargetLoweringOpt(TLO);
14322 }
14323 return SDValue();
14324}
Chris Lattner83e6c992006-10-04 06:57:07 +000014325
Eli Friedman7a5e5552009-06-07 06:52:44 +000014326static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14327 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014328 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014329 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014330 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014331 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014332 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014333 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014334 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014335 }
14336 return SDValue();
14337}
14338
Evan Cheng2e489c42009-12-16 00:53:11 +000014339static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14340 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14341 // (and (i32 x86isd::setcc_carry), 1)
14342 // This eliminates the zext. This transformation is necessary because
14343 // ISD::SETCC is always legalized to i8.
14344 DebugLoc dl = N->getDebugLoc();
14345 SDValue N0 = N->getOperand(0);
14346 EVT VT = N->getValueType(0);
14347 if (N0.getOpcode() == ISD::AND &&
14348 N0.hasOneUse() &&
14349 N0.getOperand(0).hasOneUse()) {
14350 SDValue N00 = N0.getOperand(0);
14351 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14352 return SDValue();
14353 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14354 if (!C || C->getZExtValue() != 1)
14355 return SDValue();
14356 return DAG.getNode(ISD::AND, dl, VT,
14357 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14358 N00.getOperand(0), N00.getOperand(1)),
14359 DAG.getConstant(1, VT));
14360 }
14361
14362 return SDValue();
14363}
14364
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014365// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14366static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14367 unsigned X86CC = N->getConstantOperandVal(0);
14368 SDValue EFLAG = N->getOperand(1);
14369 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014370
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014371 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14372 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14373 // cases.
14374 if (X86CC == X86::COND_B)
14375 return DAG.getNode(ISD::AND, DL, MVT::i8,
14376 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14377 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14378 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014379
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014380 return SDValue();
14381}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014382
Benjamin Kramer1396c402011-06-18 11:09:41 +000014383static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14384 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014385 SDValue Op0 = N->getOperand(0);
14386 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14387 // a 32-bit target where SSE doesn't support i64->FP operations.
14388 if (Op0.getOpcode() == ISD::LOAD) {
14389 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14390 EVT VT = Ld->getValueType(0);
14391 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14392 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14393 !XTLI->getSubtarget()->is64Bit() &&
14394 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014395 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14396 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014397 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14398 return FILDChain;
14399 }
14400 }
14401 return SDValue();
14402}
14403
Chris Lattner23a01992010-12-20 01:37:09 +000014404// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14405static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14406 X86TargetLowering::DAGCombinerInfo &DCI) {
14407 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14408 // the result is either zero or one (depending on the input carry bit).
14409 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14410 if (X86::isZeroNode(N->getOperand(0)) &&
14411 X86::isZeroNode(N->getOperand(1)) &&
14412 // We don't have a good way to replace an EFLAGS use, so only do this when
14413 // dead right now.
14414 SDValue(N, 1).use_empty()) {
14415 DebugLoc DL = N->getDebugLoc();
14416 EVT VT = N->getValueType(0);
14417 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14418 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14419 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14420 DAG.getConstant(X86::COND_B,MVT::i8),
14421 N->getOperand(2)),
14422 DAG.getConstant(1, VT));
14423 return DCI.CombineTo(N, Res1, CarryOut);
14424 }
14425
14426 return SDValue();
14427}
14428
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014429// fold (add Y, (sete X, 0)) -> adc 0, Y
14430// (add Y, (setne X, 0)) -> sbb -1, Y
14431// (sub (sete X, 0), Y) -> sbb 0, Y
14432// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014433static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014434 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014435
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014436 // Look through ZExts.
14437 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14438 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14439 return SDValue();
14440
14441 SDValue SetCC = Ext.getOperand(0);
14442 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14443 return SDValue();
14444
14445 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14446 if (CC != X86::COND_E && CC != X86::COND_NE)
14447 return SDValue();
14448
14449 SDValue Cmp = SetCC.getOperand(1);
14450 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014451 !X86::isZeroNode(Cmp.getOperand(1)) ||
14452 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014453 return SDValue();
14454
14455 SDValue CmpOp0 = Cmp.getOperand(0);
14456 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14457 DAG.getConstant(1, CmpOp0.getValueType()));
14458
14459 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14460 if (CC == X86::COND_NE)
14461 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14462 DL, OtherVal.getValueType(), OtherVal,
14463 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14464 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14465 DL, OtherVal.getValueType(), OtherVal,
14466 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14467}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014468
Craig Topper54f952a2011-11-19 09:02:40 +000014469/// PerformADDCombine - Do target-specific dag combines on integer adds.
14470static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
14471 const X86Subtarget *Subtarget) {
14472 EVT VT = N->getValueType(0);
14473 SDValue Op0 = N->getOperand(0);
14474 SDValue Op1 = N->getOperand(1);
14475
14476 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperb72039c2011-11-30 09:10:50 +000014477 if (((Subtarget->hasSSSE3orAVX() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
14478 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000014479 isHorizontalBinOp(Op0, Op1, true))
14480 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
14481
14482 return OptimizeConditionalInDecrement(N, DAG);
14483}
14484
14485static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
14486 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014487 SDValue Op0 = N->getOperand(0);
14488 SDValue Op1 = N->getOperand(1);
14489
14490 // X86 can't encode an immediate LHS of a sub. See if we can push the
14491 // negation into a preceding instruction.
14492 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014493 // If the RHS of the sub is a XOR with one use and a constant, invert the
14494 // immediate. Then add one to the LHS of the sub so we can turn
14495 // X-Y -> X+~Y+1, saving one register.
14496 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14497 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014498 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014499 EVT VT = Op0.getValueType();
14500 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14501 Op1.getOperand(0),
14502 DAG.getConstant(~XorC, VT));
14503 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014504 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014505 }
14506 }
14507
Craig Topper54f952a2011-11-19 09:02:40 +000014508 // Try to synthesize horizontal adds from adds of shuffles.
14509 EVT VT = N->getValueType(0);
Craig Topperb72039c2011-11-30 09:10:50 +000014510 if (((Subtarget->hasSSSE3orAVX() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
14511 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
14512 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000014513 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
14514
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014515 return OptimizeConditionalInDecrement(N, DAG);
14516}
14517
Dan Gohman475871a2008-07-27 21:46:04 +000014518SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014519 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014520 SelectionDAG &DAG = DCI.DAG;
14521 switch (N->getOpcode()) {
14522 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014523 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014524 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014525 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014526 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014527 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Craig Topper54f952a2011-11-19 09:02:40 +000014528 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
14529 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000014530 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014531 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014532 case ISD::SHL:
14533 case ISD::SRA:
14534 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014535 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014536 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014537 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014538 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014539 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014540 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014541 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14542 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014543 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014544 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14545 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014546 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014547 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014548 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014549 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014550 case X86ISD::SHUFPS: // Handle all target specific shuffles
14551 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014552 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000014553 case X86ISD::UNPCKH:
14554 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014555 case X86ISD::MOVHLPS:
14556 case X86ISD::MOVLHPS:
14557 case X86ISD::PSHUFD:
14558 case X86ISD::PSHUFHW:
14559 case X86ISD::PSHUFLW:
14560 case X86ISD::MOVSS:
14561 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000014562 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000014563 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014564 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014565 }
14566
Dan Gohman475871a2008-07-27 21:46:04 +000014567 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014568}
14569
Evan Chenge5b51ac2010-04-17 06:13:15 +000014570/// isTypeDesirableForOp - Return true if the target has native support for
14571/// the specified value type and it is 'desirable' to use the type for the
14572/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14573/// instruction encodings are longer and some i16 instructions are slow.
14574bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14575 if (!isTypeLegal(VT))
14576 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014577 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014578 return true;
14579
14580 switch (Opc) {
14581 default:
14582 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014583 case ISD::LOAD:
14584 case ISD::SIGN_EXTEND:
14585 case ISD::ZERO_EXTEND:
14586 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014587 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014588 case ISD::SRL:
14589 case ISD::SUB:
14590 case ISD::ADD:
14591 case ISD::MUL:
14592 case ISD::AND:
14593 case ISD::OR:
14594 case ISD::XOR:
14595 return false;
14596 }
14597}
14598
14599/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014600/// beneficial for dag combiner to promote the specified node. If true, it
14601/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014602bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014603 EVT VT = Op.getValueType();
14604 if (VT != MVT::i16)
14605 return false;
14606
Evan Cheng4c26e932010-04-19 19:29:22 +000014607 bool Promote = false;
14608 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014609 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014610 default: break;
14611 case ISD::LOAD: {
14612 LoadSDNode *LD = cast<LoadSDNode>(Op);
14613 // If the non-extending load has a single use and it's not live out, then it
14614 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014615 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14616 Op.hasOneUse()*/) {
14617 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14618 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14619 // The only case where we'd want to promote LOAD (rather then it being
14620 // promoted as an operand is when it's only use is liveout.
14621 if (UI->getOpcode() != ISD::CopyToReg)
14622 return false;
14623 }
14624 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014625 Promote = true;
14626 break;
14627 }
14628 case ISD::SIGN_EXTEND:
14629 case ISD::ZERO_EXTEND:
14630 case ISD::ANY_EXTEND:
14631 Promote = true;
14632 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014633 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014634 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014635 SDValue N0 = Op.getOperand(0);
14636 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014637 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014638 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014639 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014640 break;
14641 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014642 case ISD::ADD:
14643 case ISD::MUL:
14644 case ISD::AND:
14645 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014646 case ISD::XOR:
14647 Commute = true;
14648 // fallthrough
14649 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014650 SDValue N0 = Op.getOperand(0);
14651 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014652 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014653 return false;
14654 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014655 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014656 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014657 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014658 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014659 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014660 }
14661 }
14662
14663 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014664 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014665}
14666
Evan Cheng60c07e12006-07-05 22:17:51 +000014667//===----------------------------------------------------------------------===//
14668// X86 Inline Assembly Support
14669//===----------------------------------------------------------------------===//
14670
Chris Lattnerb8105652009-07-20 17:51:36 +000014671bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14672 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014673
14674 std::string AsmStr = IA->getAsmString();
14675
14676 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014677 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014678 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014679
14680 switch (AsmPieces.size()) {
14681 default: return false;
14682 case 1:
14683 AsmStr = AsmPieces[0];
14684 AsmPieces.clear();
14685 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14686
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014687 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014688 // we will turn this bswap into something that will be lowered to logical ops
14689 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14690 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014691 // bswap $0
14692 if (AsmPieces.size() == 2 &&
14693 (AsmPieces[0] == "bswap" ||
14694 AsmPieces[0] == "bswapq" ||
14695 AsmPieces[0] == "bswapl") &&
14696 (AsmPieces[1] == "$0" ||
14697 AsmPieces[1] == "${0:q}")) {
14698 // No need to check constraints, nothing other than the equivalent of
14699 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014700 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014701 if (!Ty || Ty->getBitWidth() % 16 != 0)
14702 return false;
14703 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014704 }
14705 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014706 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014707 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014708 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014709 AsmPieces[1] == "$$8," &&
14710 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014711 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14712 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014713 const std::string &ConstraintsStr = IA->getConstraintString();
14714 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014715 std::sort(AsmPieces.begin(), AsmPieces.end());
14716 if (AsmPieces.size() == 4 &&
14717 AsmPieces[0] == "~{cc}" &&
14718 AsmPieces[1] == "~{dirflag}" &&
14719 AsmPieces[2] == "~{flags}" &&
14720 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014721 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014722 if (!Ty || Ty->getBitWidth() % 16 != 0)
14723 return false;
14724 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014725 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014726 }
14727 break;
14728 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014729 if (CI->getType()->isIntegerTy(32) &&
14730 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14731 SmallVector<StringRef, 4> Words;
14732 SplitString(AsmPieces[0], Words, " \t,");
14733 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14734 Words[2] == "${0:w}") {
14735 Words.clear();
14736 SplitString(AsmPieces[1], Words, " \t,");
14737 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14738 Words[2] == "$0") {
14739 Words.clear();
14740 SplitString(AsmPieces[2], Words, " \t,");
14741 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14742 Words[2] == "${0:w}") {
14743 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014744 const std::string &ConstraintsStr = IA->getConstraintString();
14745 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014746 std::sort(AsmPieces.begin(), AsmPieces.end());
14747 if (AsmPieces.size() == 4 &&
14748 AsmPieces[0] == "~{cc}" &&
14749 AsmPieces[1] == "~{dirflag}" &&
14750 AsmPieces[2] == "~{flags}" &&
14751 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014752 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014753 if (!Ty || Ty->getBitWidth() % 16 != 0)
14754 return false;
14755 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014756 }
14757 }
14758 }
14759 }
14760 }
Evan Cheng55d42002011-01-08 01:24:27 +000014761
14762 if (CI->getType()->isIntegerTy(64)) {
14763 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14764 if (Constraints.size() >= 2 &&
14765 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14766 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14767 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14768 SmallVector<StringRef, 4> Words;
14769 SplitString(AsmPieces[0], Words, " \t");
14770 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014771 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014772 SplitString(AsmPieces[1], Words, " \t");
14773 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14774 Words.clear();
14775 SplitString(AsmPieces[2], Words, " \t,");
14776 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14777 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014778 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014779 if (!Ty || Ty->getBitWidth() % 16 != 0)
14780 return false;
14781 return IntrinsicLowering::LowerToByteSwap(CI);
14782 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014783 }
14784 }
14785 }
14786 }
14787 break;
14788 }
14789 return false;
14790}
14791
14792
14793
Chris Lattnerf4dff842006-07-11 02:54:03 +000014794/// getConstraintType - Given a constraint letter, return the type of
14795/// constraint it is for this target.
14796X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014797X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14798 if (Constraint.size() == 1) {
14799 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014800 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014801 case 'q':
14802 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014803 case 'f':
14804 case 't':
14805 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014806 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014807 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014808 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014809 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014810 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014811 case 'a':
14812 case 'b':
14813 case 'c':
14814 case 'd':
14815 case 'S':
14816 case 'D':
14817 case 'A':
14818 return C_Register;
14819 case 'I':
14820 case 'J':
14821 case 'K':
14822 case 'L':
14823 case 'M':
14824 case 'N':
14825 case 'G':
14826 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014827 case 'e':
14828 case 'Z':
14829 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014830 default:
14831 break;
14832 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014833 }
Chris Lattner4234f572007-03-25 02:14:49 +000014834 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014835}
14836
John Thompson44ab89e2010-10-29 17:29:13 +000014837/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014838/// This object must already have been set up with the operand type
14839/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014840TargetLowering::ConstraintWeight
14841 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014842 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014843 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014844 Value *CallOperandVal = info.CallOperandVal;
14845 // If we don't have a value, we can't do a match,
14846 // but allow it at the lowest weight.
14847 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014848 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014849 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014850 // Look at the constraint type.
14851 switch (*constraint) {
14852 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014853 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14854 case 'R':
14855 case 'q':
14856 case 'Q':
14857 case 'a':
14858 case 'b':
14859 case 'c':
14860 case 'd':
14861 case 'S':
14862 case 'D':
14863 case 'A':
14864 if (CallOperandVal->getType()->isIntegerTy())
14865 weight = CW_SpecificReg;
14866 break;
14867 case 'f':
14868 case 't':
14869 case 'u':
14870 if (type->isFloatingPointTy())
14871 weight = CW_SpecificReg;
14872 break;
14873 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014874 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014875 weight = CW_SpecificReg;
14876 break;
14877 case 'x':
14878 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014879 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014880 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014881 break;
14882 case 'I':
14883 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14884 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014885 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014886 }
14887 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014888 case 'J':
14889 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14890 if (C->getZExtValue() <= 63)
14891 weight = CW_Constant;
14892 }
14893 break;
14894 case 'K':
14895 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14896 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14897 weight = CW_Constant;
14898 }
14899 break;
14900 case 'L':
14901 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14902 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14903 weight = CW_Constant;
14904 }
14905 break;
14906 case 'M':
14907 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14908 if (C->getZExtValue() <= 3)
14909 weight = CW_Constant;
14910 }
14911 break;
14912 case 'N':
14913 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14914 if (C->getZExtValue() <= 0xff)
14915 weight = CW_Constant;
14916 }
14917 break;
14918 case 'G':
14919 case 'C':
14920 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14921 weight = CW_Constant;
14922 }
14923 break;
14924 case 'e':
14925 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14926 if ((C->getSExtValue() >= -0x80000000LL) &&
14927 (C->getSExtValue() <= 0x7fffffffLL))
14928 weight = CW_Constant;
14929 }
14930 break;
14931 case 'Z':
14932 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14933 if (C->getZExtValue() <= 0xffffffff)
14934 weight = CW_Constant;
14935 }
14936 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014937 }
14938 return weight;
14939}
14940
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014941/// LowerXConstraint - try to replace an X constraint, which matches anything,
14942/// with another that has more specific requirements based on the type of the
14943/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014944const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014945LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014946 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14947 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014948 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014949 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014950 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014951 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014952 return "x";
14953 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014954
Chris Lattner5e764232008-04-26 23:02:14 +000014955 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014956}
14957
Chris Lattner48884cd2007-08-25 00:47:38 +000014958/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14959/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014960void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014961 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014962 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014963 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014964 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014965
Eric Christopher100c8332011-06-02 23:16:42 +000014966 // Only support length 1 constraints for now.
14967 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014968
Eric Christopher100c8332011-06-02 23:16:42 +000014969 char ConstraintLetter = Constraint[0];
14970 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014971 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014972 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014973 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014974 if (C->getZExtValue() <= 31) {
14975 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014976 break;
14977 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014978 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014979 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014980 case 'J':
14981 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014982 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014983 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14984 break;
14985 }
14986 }
14987 return;
14988 case 'K':
14989 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014990 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000014991 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14992 break;
14993 }
14994 }
14995 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000014996 case 'N':
14997 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014998 if (C->getZExtValue() <= 255) {
14999 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015000 break;
15001 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000015002 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015003 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000015004 case 'e': {
15005 // 32-bit signed value
15006 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015007 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15008 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015009 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015010 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000015011 break;
15012 }
15013 // FIXME gcc accepts some relocatable values here too, but only in certain
15014 // memory models; it's complicated.
15015 }
15016 return;
15017 }
15018 case 'Z': {
15019 // 32-bit unsigned value
15020 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015021 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15022 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015023 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15024 break;
15025 }
15026 }
15027 // FIXME gcc accepts some relocatable values here too, but only in certain
15028 // memory models; it's complicated.
15029 return;
15030 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015031 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015032 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000015033 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015034 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015035 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000015036 break;
15037 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015038
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015039 // In any sort of PIC mode addresses need to be computed at runtime by
15040 // adding in a register or some sort of table lookup. These can't
15041 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000015042 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015043 return;
15044
Chris Lattnerdc43a882007-05-03 16:52:29 +000015045 // If we are in non-pic codegen mode, we allow the address of a global (with
15046 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000015047 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015048 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000015049
Chris Lattner49921962009-05-08 18:23:14 +000015050 // Match either (GA), (GA+C), (GA+C1+C2), etc.
15051 while (1) {
15052 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15053 Offset += GA->getOffset();
15054 break;
15055 } else if (Op.getOpcode() == ISD::ADD) {
15056 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15057 Offset += C->getZExtValue();
15058 Op = Op.getOperand(0);
15059 continue;
15060 }
15061 } else if (Op.getOpcode() == ISD::SUB) {
15062 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15063 Offset += -C->getZExtValue();
15064 Op = Op.getOperand(0);
15065 continue;
15066 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015067 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015068
Chris Lattner49921962009-05-08 18:23:14 +000015069 // Otherwise, this isn't something we can handle, reject it.
15070 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015071 }
Eric Christopherfd179292009-08-27 18:07:15 +000015072
Dan Gohman46510a72010-04-15 01:51:59 +000015073 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015074 // If we require an extra load to get this address, as in PIC mode, we
15075 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015076 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15077 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015078 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015079
Devang Patel0d881da2010-07-06 22:08:15 +000015080 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15081 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015082 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015083 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015084 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015085
Gabor Greifba36cb52008-08-28 21:40:38 +000015086 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015087 Ops.push_back(Result);
15088 return;
15089 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015090 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015091}
15092
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015093std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015094X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015095 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015096 // First, see if this is a constraint that directly corresponds to an LLVM
15097 // register class.
15098 if (Constraint.size() == 1) {
15099 // GCC Constraint Letters
15100 switch (Constraint[0]) {
15101 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015102 // TODO: Slight differences here in allocation order and leaving
15103 // RIP in the class. Do they matter any more here than they do
15104 // in the normal allocation?
15105 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15106 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015107 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015108 return std::make_pair(0U, X86::GR32RegisterClass);
15109 else if (VT == MVT::i16)
15110 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015111 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015112 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015113 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015114 return std::make_pair(0U, X86::GR64RegisterClass);
15115 break;
15116 }
15117 // 32-bit fallthrough
15118 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015119 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015120 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
15121 else if (VT == MVT::i16)
15122 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015123 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015124 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
15125 else if (VT == MVT::i64)
15126 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15127 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015128 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015129 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015130 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015131 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015132 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015133 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015134 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015135 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015136 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015137 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015138 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015139 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15140 if (VT == MVT::i16)
15141 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15142 if (VT == MVT::i32 || !Subtarget->is64Bit())
15143 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15144 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015145 case 'f': // FP Stack registers.
15146 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15147 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015148 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015149 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015150 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015151 return std::make_pair(0U, X86::RFP64RegisterClass);
15152 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015153 case 'y': // MMX_REGS if MMX allowed.
15154 if (!Subtarget->hasMMX()) break;
15155 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015156 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015157 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015158 // FALL THROUGH.
15159 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015160 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015161
Owen Anderson825b72b2009-08-11 20:47:22 +000015162 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015163 default: break;
15164 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015165 case MVT::f32:
15166 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015167 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015168 case MVT::f64:
15169 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015170 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015171 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015172 case MVT::v16i8:
15173 case MVT::v8i16:
15174 case MVT::v4i32:
15175 case MVT::v2i64:
15176 case MVT::v4f32:
15177 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015178 return std::make_pair(0U, X86::VR128RegisterClass);
15179 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015180 break;
15181 }
15182 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015183
Chris Lattnerf76d1802006-07-31 23:26:50 +000015184 // Use the default implementation in TargetLowering to convert the register
15185 // constraint into a member of a register class.
15186 std::pair<unsigned, const TargetRegisterClass*> Res;
15187 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015188
15189 // Not found as a standard register?
15190 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015191 // Map st(0) -> st(7) -> ST0
15192 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15193 tolower(Constraint[1]) == 's' &&
15194 tolower(Constraint[2]) == 't' &&
15195 Constraint[3] == '(' &&
15196 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15197 Constraint[5] == ')' &&
15198 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015199
Chris Lattner56d77c72009-09-13 22:41:48 +000015200 Res.first = X86::ST0+Constraint[4]-'0';
15201 Res.second = X86::RFP80RegisterClass;
15202 return Res;
15203 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015204
Chris Lattner56d77c72009-09-13 22:41:48 +000015205 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015206 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015207 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015208 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015209 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015210 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015211
15212 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015213 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015214 Res.first = X86::EFLAGS;
15215 Res.second = X86::CCRRegisterClass;
15216 return Res;
15217 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015218
Dale Johannesen330169f2008-11-13 21:52:36 +000015219 // 'A' means EAX + EDX.
15220 if (Constraint == "A") {
15221 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015222 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015223 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015224 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015225 return Res;
15226 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015227
Chris Lattnerf76d1802006-07-31 23:26:50 +000015228 // Otherwise, check to see if this is a register class of the wrong value
15229 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15230 // turn into {ax},{dx}.
15231 if (Res.second->hasType(VT))
15232 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015233
Chris Lattnerf76d1802006-07-31 23:26:50 +000015234 // All of the single-register GCC register classes map their values onto
15235 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15236 // really want an 8-bit or 32-bit register, map to the appropriate register
15237 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015238 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015239 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015240 unsigned DestReg = 0;
15241 switch (Res.first) {
15242 default: break;
15243 case X86::AX: DestReg = X86::AL; break;
15244 case X86::DX: DestReg = X86::DL; break;
15245 case X86::CX: DestReg = X86::CL; break;
15246 case X86::BX: DestReg = X86::BL; break;
15247 }
15248 if (DestReg) {
15249 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015250 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015251 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015252 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015253 unsigned DestReg = 0;
15254 switch (Res.first) {
15255 default: break;
15256 case X86::AX: DestReg = X86::EAX; break;
15257 case X86::DX: DestReg = X86::EDX; break;
15258 case X86::CX: DestReg = X86::ECX; break;
15259 case X86::BX: DestReg = X86::EBX; break;
15260 case X86::SI: DestReg = X86::ESI; break;
15261 case X86::DI: DestReg = X86::EDI; break;
15262 case X86::BP: DestReg = X86::EBP; break;
15263 case X86::SP: DestReg = X86::ESP; break;
15264 }
15265 if (DestReg) {
15266 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015267 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015268 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015269 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015270 unsigned DestReg = 0;
15271 switch (Res.first) {
15272 default: break;
15273 case X86::AX: DestReg = X86::RAX; break;
15274 case X86::DX: DestReg = X86::RDX; break;
15275 case X86::CX: DestReg = X86::RCX; break;
15276 case X86::BX: DestReg = X86::RBX; break;
15277 case X86::SI: DestReg = X86::RSI; break;
15278 case X86::DI: DestReg = X86::RDI; break;
15279 case X86::BP: DestReg = X86::RBP; break;
15280 case X86::SP: DestReg = X86::RSP; break;
15281 }
15282 if (DestReg) {
15283 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015284 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015285 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015286 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015287 } else if (Res.second == X86::FR32RegisterClass ||
15288 Res.second == X86::FR64RegisterClass ||
15289 Res.second == X86::VR128RegisterClass) {
15290 // Handle references to XMM physical registers that got mapped into the
15291 // wrong class. This can happen with constraints like {xmm0} where the
15292 // target independent register mapper will just pick the first match it can
15293 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015294 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015295 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015296 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015297 Res.second = X86::FR64RegisterClass;
15298 else if (X86::VR128RegisterClass->hasType(VT))
15299 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015300 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015301
Chris Lattnerf76d1802006-07-31 23:26:50 +000015302 return Res;
15303}