blob: 0b38d556e37c6537f3c4c6dc23c731d47726fb69 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000043#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000044#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000045#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000047#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000048#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000049#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000050#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000053#include "llvm/Target/TargetOptions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000055using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000056
Evan Chengb1712452010-01-27 06:25:16 +000057STATISTIC(NumTailCalls, "Number of tail calls");
58
Evan Cheng10e86422008-04-25 19:11:04 +000059// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000060static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000061 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000062
David Greenea5f26012011-02-07 19:36:54 +000063static SDValue Insert128BitVector(SDValue Result,
64 SDValue Vec,
65 SDValue Idx,
66 SelectionDAG &DAG,
67 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000068
David Greenea5f26012011-02-07 19:36:54 +000069static SDValue Extract128BitVector(SDValue Vec,
70 SDValue Idx,
71 SelectionDAG &DAG,
72 DebugLoc dl);
73
74/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
75/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000076/// simple subregister reference. Idx is an index in the 128 bits we
77/// want. It need not be aligned to a 128-bit bounday. That makes
78/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000079static SDValue Extract128BitVector(SDValue Vec,
80 SDValue Idx,
81 SelectionDAG &DAG,
82 DebugLoc dl) {
83 EVT VT = Vec.getValueType();
84 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000085 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000086 int Factor = VT.getSizeInBits()/128;
87 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
88 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000089
90 // Extract from UNDEF is UNDEF.
91 if (Vec.getOpcode() == ISD::UNDEF)
92 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
93
94 if (isa<ConstantSDNode>(Idx)) {
95 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
96
97 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
98 // we can match to VEXTRACTF128.
99 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
100
101 // This is the index of the first element of the 128-bit chunk
102 // we want.
103 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
104 * ElemsPerChunk);
105
106 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000107 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
108 VecIdx);
109
110 return Result;
111 }
112
113 return SDValue();
114}
115
116/// Generate a DAG to put 128-bits into a vector > 128 bits. This
117/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000118/// simple superregister reference. Idx is an index in the 128 bits
119/// we want. It need not be aligned to a 128-bit bounday. That makes
120/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000121static SDValue Insert128BitVector(SDValue Result,
122 SDValue Vec,
123 SDValue Idx,
124 SelectionDAG &DAG,
125 DebugLoc dl) {
126 if (isa<ConstantSDNode>(Idx)) {
127 EVT VT = Vec.getValueType();
128 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
129
130 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000131 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000132 EVT ResultVT = Result.getValueType();
133
134 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000135 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000136
137 // This is the index of the first element of the 128-bit chunk
138 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000139 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000140 * ElemsPerChunk);
141
142 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000143 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
144 VecIdx);
145 return Result;
146 }
147
148 return SDValue();
149}
150
Chris Lattnerf0144122009-07-28 03:13:23 +0000151static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000152 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
153 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000154
Evan Cheng2bffee22011-02-01 01:14:13 +0000155 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000156 if (is64Bit)
157 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000158 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000159 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000160
Evan Cheng203576a2011-07-20 19:50:42 +0000161 if (Subtarget->isTargetELF())
162 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000163 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000164 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000165 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000166}
167
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000168X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000169 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000170 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000171 X86ScalarSSEf64 = Subtarget->hasXMMInt();
172 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000173 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000174
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000175 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000176 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000177
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000178 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000179 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000180
181 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000182 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000183 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
184 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000185
Eric Christopherde5e1012011-03-11 01:05:58 +0000186 // For 64-bit since we have so many registers use the ILP scheduler, for
187 // 32-bit code use the register pressure specific scheduling.
188 if (Subtarget->is64Bit())
189 setSchedulingPreference(Sched::ILP);
190 else
191 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000192 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000193
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000194 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000195 // Setup Windows compiler runtime calls.
196 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000197 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000198 setLibcallName(RTLIB::SREM_I64, "_allrem");
199 setLibcallName(RTLIB::UREM_I64, "_aullrem");
200 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000201 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000202 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000203 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000204 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000205 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
206 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000208 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
209 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000210 }
211
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000212 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000213 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000214 setUseUnderscoreSetJmp(false);
215 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000216 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000217 // MS runtime is weird: it exports _setjmp, but longjmp!
218 setUseUnderscoreSetJmp(true);
219 setUseUnderscoreLongJmp(false);
220 } else {
221 setUseUnderscoreSetJmp(true);
222 setUseUnderscoreLongJmp(true);
223 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000224
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000227 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000231
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000233
Scott Michelfdc40a02009-02-17 22:15:04 +0000234 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000236 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000238 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
240 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000241
242 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
244 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
246 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000249
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000250 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
251 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
253 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000255
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
258 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000259 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000260 // We have an algorithm for SSE2->double, and we turn this into a
261 // 64-bit FILD followed by conditional FADD for other targets.
262 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000263 // We have an algorithm for SSE2, and we turn this into a 64-bit
264 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000265 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000266 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000267
268 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
269 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
271 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000272
Devang Patel6a784892009-06-05 18:48:29 +0000273 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000274 // SSE has no i16 to fp conversion, only i32
275 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000277 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000279 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
281 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000282 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000283 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
285 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000286 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000287
Dale Johannesen73328d12007-09-19 23:55:34 +0000288 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
289 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
291 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000292
Evan Cheng02568ff2006-01-30 22:13:22 +0000293 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
294 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
296 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000297
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000298 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000300 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000302 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
304 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000305 }
306
307 // Handle FP_TO_UINT by promoting the destination to a larger signed
308 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
310 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000312
Evan Cheng25ab6902006-09-08 06:48:29 +0000313 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
315 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000316 } else if (!UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000317 // Since AVX is a superset of SSE3, only check for SSE here.
318 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000319 // Expand FP_TO_UINT into a select.
320 // FIXME: We would like to use a Custom expander here eventually to do
321 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000323 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000324 // With SSE3 we can use fisttpll to convert to a signed i64; without
325 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000327 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000328
Chris Lattner399610a2006-12-05 18:22:22 +0000329 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000330 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000331 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
332 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000333 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000335 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000336 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000337 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000338 }
Chris Lattner21f66852005-12-23 05:15:23 +0000339
Dan Gohmanb00ee212008-02-18 19:34:53 +0000340 // Scalar integer divide and remainder are lowered to use operations that
341 // produce two results, to match the available instructions. This exposes
342 // the two-result form to trivial CSE, which is able to combine x/y and x%y
343 // into a single instruction.
344 //
345 // Scalar integer multiply-high is also lowered to use two-result
346 // operations, to match the available instructions. However, plain multiply
347 // (low) operations are left as Legal, as there are single-result
348 // instructions for this in x86. Using the two-result multiply instructions
349 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000350 for (unsigned i = 0, e = 4; i != e; ++i) {
351 MVT VT = IntVTs[i];
352 setOperationAction(ISD::MULHS, VT, Expand);
353 setOperationAction(ISD::MULHU, VT, Expand);
354 setOperationAction(ISD::SDIV, VT, Expand);
355 setOperationAction(ISD::UDIV, VT, Expand);
356 setOperationAction(ISD::SREM, VT, Expand);
357 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000358
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000359 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000360 setOperationAction(ISD::ADDC, VT, Custom);
361 setOperationAction(ISD::ADDE, VT, Custom);
362 setOperationAction(ISD::SUBC, VT, Custom);
363 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000364 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000365
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
367 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
368 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
369 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000370 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
375 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
376 setOperationAction(ISD::FREM , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f64 , Expand);
378 setOperationAction(ISD::FREM , MVT::f80 , Expand);
379 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000380
Craig Topper909652f2011-10-14 03:21:46 +0000381 if (Subtarget->hasBMI()) {
382 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
383 } else {
384 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
385 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
386 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
387 if (Subtarget->is64Bit())
388 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
389 }
Craig Topper37f21672011-10-11 06:44:02 +0000390
391 if (Subtarget->hasLZCNT()) {
392 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
393 } else {
394 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
395 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
396 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
397 if (Subtarget->is64Bit())
398 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000399 }
400
Benjamin Kramer1292c222010-12-04 20:32:23 +0000401 if (Subtarget->hasPOPCNT()) {
402 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
403 } else {
404 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
405 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
406 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
407 if (Subtarget->is64Bit())
408 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
409 }
410
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
412 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000413
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000414 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000415 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000416 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000417 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000418 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
420 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
421 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
422 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
423 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000424 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
426 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
427 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
428 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000429 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000431 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000432 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000434
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000435 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
437 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
438 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
439 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000440 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
442 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000443 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000444 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
446 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
447 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
448 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000449 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000450 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000451 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
453 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
454 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000455 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
457 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
458 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000459 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000460
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000461 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000463
Eric Christopher9a9d2752010-07-22 02:48:34 +0000464 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000465 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000466
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000467 // On X86 and X86-64, atomic operations are lowered to locked instructions.
468 // Locked instructions, in turn, have implicit fence semantics (all memory
469 // operations are flushed before issuing the locked instruction, and they
470 // are not buffered), so we can fold away the common pattern of
471 // fence-atomic-fence.
472 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000473
Mon P Wang63307c32008-05-05 19:05:59 +0000474 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000475 for (unsigned i = 0, e = 4; i != e; ++i) {
476 MVT VT = IntVTs[i];
477 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
478 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000479 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000480 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000481
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000482 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000483 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
485 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
486 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
487 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
488 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
489 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
490 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000491 }
492
Eli Friedman43f51ae2011-08-26 21:21:21 +0000493 if (Subtarget->hasCmpxchg16b()) {
494 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
495 }
496
Evan Cheng3c992d22006-03-07 02:02:57 +0000497 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000498 if (!Subtarget->isTargetDarwin() &&
499 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000500 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000502 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000503
Owen Anderson825b72b2009-08-11 20:47:22 +0000504 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
505 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
506 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
507 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000508 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000509 setExceptionPointerRegister(X86::RAX);
510 setExceptionSelectorRegister(X86::RDX);
511 } else {
512 setExceptionPointerRegister(X86::EAX);
513 setExceptionSelectorRegister(X86::EDX);
514 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
516 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000517
Duncan Sands4a544a72011-09-06 13:37:06 +0000518 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
519 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000520
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000522
Nate Begemanacc398c2006-01-25 18:21:52 +0000523 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000524 setOperationAction(ISD::VASTART , MVT::Other, Custom);
525 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000526 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 setOperationAction(ISD::VAARG , MVT::Other, Custom);
528 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000529 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::VAARG , MVT::Other, Expand);
531 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000532 }
Evan Chengae642192007-03-02 23:16:35 +0000533
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
535 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000536
537 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
538 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
539 MVT::i64 : MVT::i32, Custom);
540 else if (EnableSegmentedStacks)
541 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
542 MVT::i64 : MVT::i32, Custom);
543 else
544 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
545 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000546
Evan Chengc7ce29b2009-02-13 22:36:38 +0000547 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000548 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000549 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
551 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000552
Evan Cheng223547a2006-01-31 22:28:30 +0000553 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 setOperationAction(ISD::FABS , MVT::f64, Custom);
555 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000556
557 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 setOperationAction(ISD::FNEG , MVT::f64, Custom);
559 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000560
Evan Cheng68c47cb2007-01-05 07:55:56 +0000561 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
563 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000564
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000565 // Lower this to FGETSIGNx86 plus an AND.
566 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
567 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
568
Evan Chengd25e9e82006-02-02 00:28:23 +0000569 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setOperationAction(ISD::FSIN , MVT::f64, Expand);
571 setOperationAction(ISD::FCOS , MVT::f64, Expand);
572 setOperationAction(ISD::FSIN , MVT::f32, Expand);
573 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000574
Chris Lattnera54aa942006-01-29 06:26:08 +0000575 // Expand FP immediates into loads from the stack, except for the special
576 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000577 addLegalFPImmediate(APFloat(+0.0)); // xorpd
578 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000579 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000580 // Use SSE for f32, x87 for f64.
581 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
583 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000584
585 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000587
588 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000590
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
593 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
595 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000596
597 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::FSIN , MVT::f32, Expand);
599 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600
Nate Begemane1795842008-02-14 08:57:00 +0000601 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602 addLegalFPImmediate(APFloat(+0.0f)); // xorps
603 addLegalFPImmediate(APFloat(+0.0)); // FLD0
604 addLegalFPImmediate(APFloat(+1.0)); // FLD1
605 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
606 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
607
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000608 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
610 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000611 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000612 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000613 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
616 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000617
Owen Anderson825b72b2009-08-11 20:47:22 +0000618 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
619 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
620 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
621 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000622
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000623 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
625 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000627 addLegalFPImmediate(APFloat(+0.0)); // FLD0
628 addLegalFPImmediate(APFloat(+1.0)); // FLD1
629 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
630 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000631 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
632 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
633 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
634 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000635 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000636
Cameron Zwarich33390842011-07-08 21:39:21 +0000637 // We don't support FMA.
638 setOperationAction(ISD::FMA, MVT::f64, Expand);
639 setOperationAction(ISD::FMA, MVT::f32, Expand);
640
Dale Johannesen59a58732007-08-05 18:49:15 +0000641 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000642 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
644 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
645 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000646 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000647 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000648 addLegalFPImmediate(TmpFlt); // FLD0
649 TmpFlt.changeSign();
650 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000651
652 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000653 APFloat TmpFlt2(+1.0);
654 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
655 &ignored);
656 addLegalFPImmediate(TmpFlt2); // FLD1
657 TmpFlt2.changeSign();
658 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
659 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000660
Evan Chengc7ce29b2009-02-13 22:36:38 +0000661 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
663 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000664 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000665
666 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000667 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000668
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000669 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
671 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
672 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000673
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 setOperationAction(ISD::FLOG, MVT::f80, Expand);
675 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
676 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
677 setOperationAction(ISD::FEXP, MVT::f80, Expand);
678 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000679
Mon P Wangf007a8b2008-11-06 05:31:54 +0000680 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000681 // (for widening) or expand (for scalarization). Then we will selectively
682 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
684 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
685 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000701 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
702 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000724 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000725 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000734 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000735 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000739 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000740 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
741 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
742 setTruncStoreAction((MVT::SimpleValueType)VT,
743 (MVT::SimpleValueType)InnerVT, Expand);
744 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
745 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
746 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000747 }
748
Evan Chengc7ce29b2009-02-13 22:36:38 +0000749 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
750 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000751 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000752 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000753 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000754 }
755
Dale Johannesen0488fb62010-09-30 23:57:10 +0000756 // MMX-sized vectors (other than x86mmx) are expected to be expanded
757 // into smaller operations.
758 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
759 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
760 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
761 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
762 setOperationAction(ISD::AND, MVT::v8i8, Expand);
763 setOperationAction(ISD::AND, MVT::v4i16, Expand);
764 setOperationAction(ISD::AND, MVT::v2i32, Expand);
765 setOperationAction(ISD::AND, MVT::v1i64, Expand);
766 setOperationAction(ISD::OR, MVT::v8i8, Expand);
767 setOperationAction(ISD::OR, MVT::v4i16, Expand);
768 setOperationAction(ISD::OR, MVT::v2i32, Expand);
769 setOperationAction(ISD::OR, MVT::v1i64, Expand);
770 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
771 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
772 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
773 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
774 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
775 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
776 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
777 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
778 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
779 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
780 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
781 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
782 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000783 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
784 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
785 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
786 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000787
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000788 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000789 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000790
Owen Anderson825b72b2009-08-11 20:47:22 +0000791 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
792 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
793 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
794 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
795 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
796 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
797 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
798 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
799 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
800 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
801 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000802 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000803 }
804
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000805 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000807
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000808 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
809 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
811 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
812 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
813 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000814
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
816 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
817 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
818 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
819 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
820 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
821 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
822 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
823 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
824 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
825 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
826 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
827 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
828 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
829 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
830 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000831
Nadav Rotem354efd82011-09-18 14:57:03 +0000832 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000833 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
834 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
835 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000836
Owen Anderson825b72b2009-08-11 20:47:22 +0000837 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
838 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
839 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
840 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000842
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000843 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
844 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
845 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
846 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
847 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
848
Evan Cheng2c3ae372006-04-12 21:21:57 +0000849 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000850 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
851 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000852 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000853 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000854 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000855 // Do not attempt to custom lower non-128-bit vectors
856 if (!VT.is128BitVector())
857 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000858 setOperationAction(ISD::BUILD_VECTOR,
859 VT.getSimpleVT().SimpleTy, Custom);
860 setOperationAction(ISD::VECTOR_SHUFFLE,
861 VT.getSimpleVT().SimpleTy, Custom);
862 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
863 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000864 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000865
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
867 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
868 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
869 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
870 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
871 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000872
Nate Begemancdd1eec2008-02-12 22:51:28 +0000873 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
875 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000876 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000877
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000878 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
880 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000881 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000882
883 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000884 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000885 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000886
Owen Andersond6662ad2009-08-10 20:46:15 +0000887 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000889 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000890 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000891 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000893 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000895 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000896 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000897 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000898
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000900
Evan Cheng2c3ae372006-04-12 21:21:57 +0000901 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
903 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
904 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
905 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000906
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
908 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000909 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000910
Craig Topperc0d82852011-11-22 00:44:41 +0000911 if (Subtarget->hasSSE41orAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000912 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
913 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
914 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
915 setOperationAction(ISD::FRINT, MVT::f32, Legal);
916 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
917 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
918 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
919 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
920 setOperationAction(ISD::FRINT, MVT::f64, Legal);
921 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
922
Nate Begeman14d12ca2008-02-11 04:19:36 +0000923 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000924 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000925
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000926 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
927 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
928 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
929 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
930 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000931
Nate Begeman14d12ca2008-02-11 04:19:36 +0000932 // i8 and i16 vectors are custom , because the source register and source
933 // source memory operand types are not the same width. f32 vectors are
934 // custom since the immediate controlling the insert encodes additional
935 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
937 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
938 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
939 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000940
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
942 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
943 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
944 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000945
Pete Coopera77214a2011-11-14 19:38:42 +0000946 // FIXME: these should be Legal but thats only for the case where
947 // the index is constant. For now custom expand to deal with that
Nate Begeman14d12ca2008-02-11 04:19:36 +0000948 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000949 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
950 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000951 }
952 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000953
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000954 if (Subtarget->hasXMMInt()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000955 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000956 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000957
Nadav Rotem43012222011-05-11 08:12:09 +0000958 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000959 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000960
Nadav Rotem43012222011-05-11 08:12:09 +0000961 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000962 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000963
964 if (Subtarget->hasAVX2()) {
965 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
966 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
967
968 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
969 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
970
971 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
972 } else {
973 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
974 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
975
976 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
977 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
978
979 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
980 }
Nadav Rotem43012222011-05-11 08:12:09 +0000981 }
982
Craig Topperc0d82852011-11-22 00:44:41 +0000983 if (Subtarget->hasSSE42orAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000984 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000985
David Greene9b9838d2009-06-29 16:47:10 +0000986 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000987 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
988 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
989 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
990 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
991 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
992 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000993
Owen Anderson825b72b2009-08-11 20:47:22 +0000994 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000995 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
996 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000997
Owen Anderson825b72b2009-08-11 20:47:22 +0000998 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
999 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1000 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1001 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1002 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
1003 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001004
Owen Anderson825b72b2009-08-11 20:47:22 +00001005 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1006 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1007 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1008 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1009 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1010 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001011
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001012 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1013 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001014 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001015
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00001016 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
1017 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
1018 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
1019 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
1020 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1021 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1022
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001023 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1024 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1025
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001026 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1027 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1028
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001029 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001030 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001031
Duncan Sands28b77e92011-09-06 19:07:46 +00001032 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1033 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1034 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1035 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001036
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001037 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1038 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1039 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1040
Craig Topperaaa643c2011-11-09 07:28:55 +00001041 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1042 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1043 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1044 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001045
Craig Topperaaa643c2011-11-09 07:28:55 +00001046 if (Subtarget->hasAVX2()) {
1047 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1048 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1049 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1050 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001051
Craig Topperaaa643c2011-11-09 07:28:55 +00001052 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1053 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1054 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1055 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001056
Craig Topperaaa643c2011-11-09 07:28:55 +00001057 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1058 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1059 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001060 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001061
1062 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001063
1064 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1065 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1066
1067 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1068 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1069
1070 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001071 } else {
1072 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1073 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1074 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1075 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1076
1077 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1078 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1079 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1080 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1081
1082 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1083 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1084 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1085 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001086
1087 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1088 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1089
1090 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1091 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1092
1093 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001094 }
Craig Topper13894fa2011-08-24 06:14:18 +00001095
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001096 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001097 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001098 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1099 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1100 EVT VT = SVT;
1101
1102 // Extract subvector is special because the value type
1103 // (result) is 128-bit but the source is 256-bit wide.
1104 if (VT.is128BitVector())
1105 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1106
1107 // Do not attempt to custom lower other non-256-bit vectors
1108 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001109 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001110
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001111 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1112 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1113 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1114 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001115 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001116 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001117 }
1118
David Greene54d8eba2011-01-27 22:38:56 +00001119 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001120 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1121 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1122 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001123
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001124 // Do not attempt to promote non-256-bit vectors
1125 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001126 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001127
1128 setOperationAction(ISD::AND, SVT, Promote);
1129 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1130 setOperationAction(ISD::OR, SVT, Promote);
1131 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1132 setOperationAction(ISD::XOR, SVT, Promote);
1133 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1134 setOperationAction(ISD::LOAD, SVT, Promote);
1135 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1136 setOperationAction(ISD::SELECT, SVT, Promote);
1137 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001138 }
David Greene9b9838d2009-06-29 16:47:10 +00001139 }
1140
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001141 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1142 // of this type with custom code.
1143 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1144 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1145 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1146 }
1147
Evan Cheng6be2c582006-04-05 23:38:46 +00001148 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001149 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001150
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001151
Eli Friedman962f5492010-06-02 19:35:46 +00001152 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1153 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001154 //
Eli Friedman962f5492010-06-02 19:35:46 +00001155 // FIXME: We really should do custom legalization for addition and
1156 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1157 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001158 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1159 // Add/Sub/Mul with overflow operations are custom lowered.
1160 MVT VT = IntVTs[i];
1161 setOperationAction(ISD::SADDO, VT, Custom);
1162 setOperationAction(ISD::UADDO, VT, Custom);
1163 setOperationAction(ISD::SSUBO, VT, Custom);
1164 setOperationAction(ISD::USUBO, VT, Custom);
1165 setOperationAction(ISD::SMULO, VT, Custom);
1166 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001167 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001168
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001169 // There are no 8-bit 3-address imul/mul instructions
1170 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1171 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001172
Evan Chengd54f2d52009-03-31 19:38:51 +00001173 if (!Subtarget->is64Bit()) {
1174 // These libcalls are not available in 32-bit.
1175 setLibcallName(RTLIB::SHL_I128, 0);
1176 setLibcallName(RTLIB::SRL_I128, 0);
1177 setLibcallName(RTLIB::SRA_I128, 0);
1178 }
1179
Evan Cheng206ee9d2006-07-07 08:33:52 +00001180 // We have target-specific dag combine patterns for the following nodes:
1181 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001182 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001183 setTargetDAGCombine(ISD::BUILD_VECTOR);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001184 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001185 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001186 setTargetDAGCombine(ISD::SHL);
1187 setTargetDAGCombine(ISD::SRA);
1188 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001189 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001190 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001191 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001192 setTargetDAGCombine(ISD::FADD);
1193 setTargetDAGCombine(ISD::FSUB);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001194 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001195 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001196 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001197 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001198 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001199 if (Subtarget->is64Bit())
1200 setTargetDAGCombine(ISD::MUL);
Craig Topperb4c94572011-10-21 06:55:01 +00001201 if (Subtarget->hasBMI())
1202 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001203
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001204 computeRegisterProperties();
1205
Evan Cheng05219282011-01-06 06:52:41 +00001206 // On Darwin, -Os means optimize for size without hurting performance,
1207 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001208 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001209 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001210 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001211 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1212 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1213 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001214 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001215 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001216
1217 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001218}
1219
Scott Michel5b8f82e2008-03-10 15:42:14 +00001220
Duncan Sands28b77e92011-09-06 19:07:46 +00001221EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1222 if (!VT.isVector()) return MVT::i8;
1223 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001224}
1225
1226
Evan Cheng29286502008-01-23 23:17:41 +00001227/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1228/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001229static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001230 if (MaxAlign == 16)
1231 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001232 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001233 if (VTy->getBitWidth() == 128)
1234 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001235 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001236 unsigned EltAlign = 0;
1237 getMaxByValAlign(ATy->getElementType(), EltAlign);
1238 if (EltAlign > MaxAlign)
1239 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001240 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001241 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1242 unsigned EltAlign = 0;
1243 getMaxByValAlign(STy->getElementType(i), EltAlign);
1244 if (EltAlign > MaxAlign)
1245 MaxAlign = EltAlign;
1246 if (MaxAlign == 16)
1247 break;
1248 }
1249 }
1250 return;
1251}
1252
1253/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1254/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001255/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1256/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001257unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001258 if (Subtarget->is64Bit()) {
1259 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001260 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001261 if (TyAlign > 8)
1262 return TyAlign;
1263 return 8;
1264 }
1265
Evan Cheng29286502008-01-23 23:17:41 +00001266 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001267 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001268 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001269 return Align;
1270}
Chris Lattner2b02a442007-02-25 08:29:00 +00001271
Evan Chengf0df0312008-05-15 08:39:06 +00001272/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001273/// and store operations as a result of memset, memcpy, and memmove
1274/// lowering. If DstAlign is zero that means it's safe to destination
1275/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1276/// means there isn't a need to check it against alignment requirement,
1277/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001278/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001279/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1280/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1281/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001282/// It returns EVT::Other if the type should be determined using generic
1283/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001284EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001285X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1286 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001287 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001288 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001289 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001290 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1291 // linux. This is because the stack realignment code can't handle certain
1292 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001293 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001294 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001295 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001296 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001297 (Subtarget->isUnalignedMemAccessFast() ||
1298 ((DstAlign == 0 || DstAlign >= 16) &&
1299 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001300 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001301 if (Subtarget->hasAVX() &&
1302 Subtarget->getStackAlignment() >= 32)
1303 return MVT::v8f32;
1304 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001305 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001306 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001307 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001308 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001309 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001310 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001311 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001312 // Do not use f64 to lower memcpy if source is string constant. It's
1313 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001314 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001315 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001316 }
Evan Chengf0df0312008-05-15 08:39:06 +00001317 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001318 return MVT::i64;
1319 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001320}
1321
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001322/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1323/// current function. The returned value is a member of the
1324/// MachineJumpTableInfo::JTEntryKind enum.
1325unsigned X86TargetLowering::getJumpTableEncoding() const {
1326 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1327 // symbol.
1328 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1329 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001330 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001331
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001332 // Otherwise, use the normal jump table encoding heuristics.
1333 return TargetLowering::getJumpTableEncoding();
1334}
1335
Chris Lattnerc64daab2010-01-26 05:02:42 +00001336const MCExpr *
1337X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1338 const MachineBasicBlock *MBB,
1339 unsigned uid,MCContext &Ctx) const{
1340 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1341 Subtarget->isPICStyleGOT());
1342 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1343 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001344 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1345 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001346}
1347
Evan Chengcc415862007-11-09 01:32:10 +00001348/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1349/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001350SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001351 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001352 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001353 // This doesn't have DebugLoc associated with it, but is not really the
1354 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001355 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001356 return Table;
1357}
1358
Chris Lattner589c6f62010-01-26 06:28:43 +00001359/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1360/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1361/// MCExpr.
1362const MCExpr *X86TargetLowering::
1363getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1364 MCContext &Ctx) const {
1365 // X86-64 uses RIP relative addressing based on the jump table label.
1366 if (Subtarget->isPICStyleRIPRel())
1367 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1368
1369 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001370 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001371}
1372
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001373// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001374std::pair<const TargetRegisterClass*, uint8_t>
1375X86TargetLowering::findRepresentativeClass(EVT VT) const{
1376 const TargetRegisterClass *RRC = 0;
1377 uint8_t Cost = 1;
1378 switch (VT.getSimpleVT().SimpleTy) {
1379 default:
1380 return TargetLowering::findRepresentativeClass(VT);
1381 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1382 RRC = (Subtarget->is64Bit()
1383 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1384 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001385 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001386 RRC = X86::VR64RegisterClass;
1387 break;
1388 case MVT::f32: case MVT::f64:
1389 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1390 case MVT::v4f32: case MVT::v2f64:
1391 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1392 case MVT::v4f64:
1393 RRC = X86::VR128RegisterClass;
1394 break;
1395 }
1396 return std::make_pair(RRC, Cost);
1397}
1398
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001399bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1400 unsigned &Offset) const {
1401 if (!Subtarget->isTargetLinux())
1402 return false;
1403
1404 if (Subtarget->is64Bit()) {
1405 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1406 Offset = 0x28;
1407 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1408 AddressSpace = 256;
1409 else
1410 AddressSpace = 257;
1411 } else {
1412 // %gs:0x14 on i386
1413 Offset = 0x14;
1414 AddressSpace = 256;
1415 }
1416 return true;
1417}
1418
1419
Chris Lattner2b02a442007-02-25 08:29:00 +00001420//===----------------------------------------------------------------------===//
1421// Return Value Calling Convention Implementation
1422//===----------------------------------------------------------------------===//
1423
Chris Lattner59ed56b2007-02-28 04:55:35 +00001424#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001425
Michael J. Spencerec38de22010-10-10 22:04:20 +00001426bool
Eric Christopher471e4222011-06-08 23:55:35 +00001427X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1428 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001429 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001430 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001431 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001432 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001433 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001434 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001435}
1436
Dan Gohman98ca4f22009-08-05 01:29:28 +00001437SDValue
1438X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001439 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001440 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001441 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001442 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001443 MachineFunction &MF = DAG.getMachineFunction();
1444 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001445
Chris Lattner9774c912007-02-27 05:28:59 +00001446 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001447 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001448 RVLocs, *DAG.getContext());
1449 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001450
Evan Chengdcea1632010-02-04 02:40:39 +00001451 // Add the regs to the liveout set for the function.
1452 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1453 for (unsigned i = 0; i != RVLocs.size(); ++i)
1454 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1455 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001456
Dan Gohman475871a2008-07-27 21:46:04 +00001457 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001458
Dan Gohman475871a2008-07-27 21:46:04 +00001459 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001460 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1461 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001462 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1463 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001464
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001465 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001466 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1467 CCValAssign &VA = RVLocs[i];
1468 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001469 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001470 EVT ValVT = ValToCopy.getValueType();
1471
Dale Johannesenc4510512010-09-24 19:05:48 +00001472 // If this is x86-64, and we disabled SSE, we can't return FP values,
1473 // or SSE or MMX vectors.
1474 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1475 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001476 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001477 report_fatal_error("SSE register return with SSE disabled");
1478 }
1479 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1480 // llvm-gcc has never done it right and no one has noticed, so this
1481 // should be OK for now.
1482 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001483 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001484 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001485
Chris Lattner447ff682008-03-11 03:23:40 +00001486 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1487 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001488 if (VA.getLocReg() == X86::ST0 ||
1489 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001490 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1491 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001492 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001493 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001494 RetOps.push_back(ValToCopy);
1495 // Don't emit a copytoreg.
1496 continue;
1497 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001498
Evan Cheng242b38b2009-02-23 09:03:22 +00001499 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1500 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001501 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001502 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001503 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001504 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001505 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1506 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001507 // If we don't have SSE2 available, convert to v4f32 so the generated
1508 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001509 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001510 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001511 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001512 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001513 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001514
Dale Johannesendd64c412009-02-04 00:33:20 +00001515 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001516 Flag = Chain.getValue(1);
1517 }
Dan Gohman61a92132008-04-21 23:59:07 +00001518
1519 // The x86-64 ABI for returning structs by value requires that we copy
1520 // the sret argument into %rax for the return. We saved the argument into
1521 // a virtual register in the entry block, so now we copy the value out
1522 // and into %rax.
1523 if (Subtarget->is64Bit() &&
1524 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1525 MachineFunction &MF = DAG.getMachineFunction();
1526 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1527 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001528 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001529 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001530 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001531
Dale Johannesendd64c412009-02-04 00:33:20 +00001532 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001533 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001534
1535 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001536 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001537 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001538
Chris Lattner447ff682008-03-11 03:23:40 +00001539 RetOps[0] = Chain; // Update chain.
1540
1541 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001542 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001543 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001544
1545 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001546 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001547}
1548
Evan Cheng3d2125c2010-11-30 23:55:39 +00001549bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1550 if (N->getNumValues() != 1)
1551 return false;
1552 if (!N->hasNUsesOfValue(1, 0))
1553 return false;
1554
1555 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001556 if (Copy->getOpcode() != ISD::CopyToReg &&
1557 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001558 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001559
1560 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001561 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001562 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001563 if (UI->getOpcode() != X86ISD::RET_FLAG)
1564 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001565 HasRet = true;
1566 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001567
Evan Cheng1bf891a2010-12-01 22:59:46 +00001568 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001569}
1570
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001571EVT
1572X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001573 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001574 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001575 // TODO: Is this also valid on 32-bit?
1576 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001577 ReturnMVT = MVT::i8;
1578 else
1579 ReturnMVT = MVT::i32;
1580
1581 EVT MinVT = getRegisterType(Context, ReturnMVT);
1582 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001583}
1584
Dan Gohman98ca4f22009-08-05 01:29:28 +00001585/// LowerCallResult - Lower the result values of a call into the
1586/// appropriate copies out of appropriate physical registers.
1587///
1588SDValue
1589X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001590 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001591 const SmallVectorImpl<ISD::InputArg> &Ins,
1592 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001593 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001594
Chris Lattnere32bbf62007-02-28 07:09:55 +00001595 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001596 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001597 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001598 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1599 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001600 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001601
Chris Lattner3085e152007-02-25 08:59:22 +00001602 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001603 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001604 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001605 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001606
Torok Edwin3f142c32009-02-01 18:15:56 +00001607 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001608 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001609 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001610 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001611 }
1612
Evan Cheng79fb3b42009-02-20 20:43:02 +00001613 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001614
1615 // If this is a call to a function that returns an fp value on the floating
1616 // point stack, we must guarantee the the value is popped from the stack, so
1617 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001618 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001619 // instead.
1620 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1621 // If we prefer to use the value in xmm registers, copy it out as f80 and
1622 // use a truncate to move it from fp stack reg to xmm reg.
1623 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001624 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001625 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1626 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001627 Val = Chain.getValue(0);
1628
1629 // Round the f80 to the right size, which also moves it to the appropriate
1630 // xmm register.
1631 if (CopyVT != VA.getValVT())
1632 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1633 // This truncation won't change the value.
1634 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001635 } else {
1636 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1637 CopyVT, InFlag).getValue(1);
1638 Val = Chain.getValue(0);
1639 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001640 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001641 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001642 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001643
Dan Gohman98ca4f22009-08-05 01:29:28 +00001644 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001645}
1646
1647
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001648//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001649// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001650//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001651// StdCall calling convention seems to be standard for many Windows' API
1652// routines and around. It differs from C calling convention just a little:
1653// callee should clean up the stack, not caller. Symbols should be also
1654// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001655// For info on fast calling convention see Fast Calling Convention (tail call)
1656// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001657
Dan Gohman98ca4f22009-08-05 01:29:28 +00001658/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001659/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001660static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1661 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001662 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001663
Dan Gohman98ca4f22009-08-05 01:29:28 +00001664 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001665}
1666
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001667/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001668/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001669static bool
1670ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1671 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001672 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001673
Dan Gohman98ca4f22009-08-05 01:29:28 +00001674 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001675}
1676
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001677/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1678/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001679/// the specific parameter attribute. The copy will be passed as a byval
1680/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001681static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001682CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001683 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1684 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001685 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001686
Dale Johannesendd64c412009-02-04 00:33:20 +00001687 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001688 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001689 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001690}
1691
Chris Lattner29689432010-03-11 00:22:57 +00001692/// IsTailCallConvention - Return true if the calling convention is one that
1693/// supports tail call optimization.
1694static bool IsTailCallConvention(CallingConv::ID CC) {
1695 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1696}
1697
Evan Cheng485fafc2011-03-21 01:19:09 +00001698bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1699 if (!CI->isTailCall())
1700 return false;
1701
1702 CallSite CS(CI);
1703 CallingConv::ID CalleeCC = CS.getCallingConv();
1704 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1705 return false;
1706
1707 return true;
1708}
1709
Evan Cheng0c439eb2010-01-27 00:07:07 +00001710/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1711/// a tailcall target by changing its ABI.
1712static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001713 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001714}
1715
Dan Gohman98ca4f22009-08-05 01:29:28 +00001716SDValue
1717X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001718 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001719 const SmallVectorImpl<ISD::InputArg> &Ins,
1720 DebugLoc dl, SelectionDAG &DAG,
1721 const CCValAssign &VA,
1722 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001723 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001724 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001725 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001726 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001727 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001728 EVT ValVT;
1729
1730 // If value is passed by pointer we have address passed instead of the value
1731 // itself.
1732 if (VA.getLocInfo() == CCValAssign::Indirect)
1733 ValVT = VA.getLocVT();
1734 else
1735 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001736
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001737 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001738 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001739 // In case of tail call optimization mark all arguments mutable. Since they
1740 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001741 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001742 unsigned Bytes = Flags.getByValSize();
1743 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1744 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001745 return DAG.getFrameIndex(FI, getPointerTy());
1746 } else {
1747 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001748 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001749 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1750 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001751 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001752 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001753 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001754}
1755
Dan Gohman475871a2008-07-27 21:46:04 +00001756SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001757X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001758 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001759 bool isVarArg,
1760 const SmallVectorImpl<ISD::InputArg> &Ins,
1761 DebugLoc dl,
1762 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001763 SmallVectorImpl<SDValue> &InVals)
1764 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001765 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001766 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001767
Gordon Henriksen86737662008-01-05 16:56:59 +00001768 const Function* Fn = MF.getFunction();
1769 if (Fn->hasExternalLinkage() &&
1770 Subtarget->isTargetCygMing() &&
1771 Fn->getName() == "main")
1772 FuncInfo->setForceFramePointer(true);
1773
Evan Cheng1bc78042006-04-26 01:20:17 +00001774 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001775 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001776 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001777
Chris Lattner29689432010-03-11 00:22:57 +00001778 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1779 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001780
Chris Lattner638402b2007-02-28 07:00:42 +00001781 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001782 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001783 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001784 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001785
1786 // Allocate shadow area for Win64
1787 if (IsWin64) {
1788 CCInfo.AllocateStack(32, 8);
1789 }
1790
Duncan Sands45907662010-10-31 13:21:44 +00001791 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001792
Chris Lattnerf39f7712007-02-28 05:46:49 +00001793 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001794 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001795 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1796 CCValAssign &VA = ArgLocs[i];
1797 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1798 // places.
1799 assert(VA.getValNo() != LastVal &&
1800 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001801 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001802 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001803
Chris Lattnerf39f7712007-02-28 05:46:49 +00001804 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001805 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001806 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001807 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001808 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001809 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001810 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001811 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001812 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001813 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001814 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001815 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1816 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001817 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001818 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001819 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001820 RC = X86::VR64RegisterClass;
1821 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001822 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001823
Devang Patel68e6bee2011-02-21 23:21:26 +00001824 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001825 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001826
Chris Lattnerf39f7712007-02-28 05:46:49 +00001827 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1828 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1829 // right size.
1830 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001831 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001832 DAG.getValueType(VA.getValVT()));
1833 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001834 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001835 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001836 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001837 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001838
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001839 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001840 // Handle MMX values passed in XMM regs.
1841 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001842 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1843 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001844 } else
1845 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001846 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001847 } else {
1848 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001849 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001850 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001851
1852 // If value is passed via pointer - do a load.
1853 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001854 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001855 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001856
Dan Gohman98ca4f22009-08-05 01:29:28 +00001857 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001858 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001859
Dan Gohman61a92132008-04-21 23:59:07 +00001860 // The x86-64 ABI for returning structs by value requires that we copy
1861 // the sret argument into %rax for the return. Save the argument into
1862 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001863 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001864 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1865 unsigned Reg = FuncInfo->getSRetReturnReg();
1866 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001867 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001868 FuncInfo->setSRetReturnReg(Reg);
1869 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001870 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001871 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001872 }
1873
Chris Lattnerf39f7712007-02-28 05:46:49 +00001874 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001875 // Align stack specially for tail calls.
1876 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001877 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001878
Evan Cheng1bc78042006-04-26 01:20:17 +00001879 // If the function takes variable number of arguments, make a frame index for
1880 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001881 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001882 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1883 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001884 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001885 }
1886 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001887 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1888
1889 // FIXME: We should really autogenerate these arrays
1890 static const unsigned GPR64ArgRegsWin64[] = {
1891 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001892 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001893 static const unsigned GPR64ArgRegs64Bit[] = {
1894 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1895 };
1896 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001897 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1898 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1899 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001900 const unsigned *GPR64ArgRegs;
1901 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001902
1903 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001904 // The XMM registers which might contain var arg parameters are shadowed
1905 // in their paired GPR. So we only need to save the GPR to their home
1906 // slots.
1907 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001908 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001909 } else {
1910 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1911 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001912
1913 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001914 }
1915 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1916 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001917
Devang Patel578efa92009-06-05 21:57:13 +00001918 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001919 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001920 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001921 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001922 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001923 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001924 // Kernel mode asks for SSE to be disabled, so don't push them
1925 // on the stack.
1926 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001927
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001928 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001929 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001930 // Get to the caller-allocated home save location. Add 8 to account
1931 // for the return address.
1932 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001933 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001934 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001935 // Fixup to set vararg frame on shadow area (4 x i64).
1936 if (NumIntRegs < 4)
1937 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001938 } else {
1939 // For X86-64, if there are vararg parameters that are passed via
1940 // registers, then we must store them to their spots on the stack so they
1941 // may be loaded by deferencing the result of va_next.
1942 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1943 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1944 FuncInfo->setRegSaveFrameIndex(
1945 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001946 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001947 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001948
Gordon Henriksen86737662008-01-05 16:56:59 +00001949 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001950 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001951 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1952 getPointerTy());
1953 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001954 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001955 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1956 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001957 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001958 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001959 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001960 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001961 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001962 MachinePointerInfo::getFixedStack(
1963 FuncInfo->getRegSaveFrameIndex(), Offset),
1964 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001965 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001966 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001967 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001968
Dan Gohmanface41a2009-08-16 21:24:25 +00001969 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1970 // Now store the XMM (fp + vector) parameter registers.
1971 SmallVector<SDValue, 11> SaveXMMOps;
1972 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001973
Devang Patel68e6bee2011-02-21 23:21:26 +00001974 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001975 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1976 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001977
Dan Gohman1e93df62010-04-17 14:41:14 +00001978 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1979 FuncInfo->getRegSaveFrameIndex()));
1980 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1981 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001982
Dan Gohmanface41a2009-08-16 21:24:25 +00001983 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001984 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001985 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001986 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1987 SaveXMMOps.push_back(Val);
1988 }
1989 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1990 MVT::Other,
1991 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001992 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001993
1994 if (!MemOps.empty())
1995 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1996 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001997 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001998 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001999
Gordon Henriksen86737662008-01-05 16:56:59 +00002000 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00002001 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002002 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002003 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002004 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002005 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00002006 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00002007 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002008 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002009
Gordon Henriksen86737662008-01-05 16:56:59 +00002010 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002011 // RegSaveFrameIndex is X86-64 only.
2012 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002013 if (CallConv == CallingConv::X86_FastCall ||
2014 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002015 // fastcc functions can't have varargs.
2016 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002017 }
Evan Cheng25caf632006-05-23 21:06:34 +00002018
Rafael Espindola76927d752011-08-30 19:39:58 +00002019 FuncInfo->setArgumentStackSize(StackSize);
2020
Dan Gohman98ca4f22009-08-05 01:29:28 +00002021 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002022}
2023
Dan Gohman475871a2008-07-27 21:46:04 +00002024SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002025X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2026 SDValue StackPtr, SDValue Arg,
2027 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002028 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002029 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002030 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002031 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002032 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002033 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002034 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002035
2036 return DAG.getStore(Chain, dl, Arg, PtrOff,
2037 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002038 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002039}
2040
Bill Wendling64e87322009-01-16 19:25:27 +00002041/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002042/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002043SDValue
2044X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002045 SDValue &OutRetAddr, SDValue Chain,
2046 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002047 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002048 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002049 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002050 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002051
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002052 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002053 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002054 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002055 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002056}
2057
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002058/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002059/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002060static SDValue
2061EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002062 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002063 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002064 // Store the return address to the appropriate stack slot.
2065 if (!FPDiff) return Chain;
2066 // Calculate the new stack slot for the return address.
2067 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002068 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002069 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002070 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002071 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002072 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002073 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002074 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002075 return Chain;
2076}
2077
Dan Gohman98ca4f22009-08-05 01:29:28 +00002078SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002079X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002080 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002081 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002082 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002083 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002084 const SmallVectorImpl<ISD::InputArg> &Ins,
2085 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002086 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002087 MachineFunction &MF = DAG.getMachineFunction();
2088 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002089 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002090 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002091 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002092
Evan Cheng5f941932010-02-05 02:21:12 +00002093 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002094 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002095 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2096 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002097 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002098
2099 // Sibcalls are automatically detected tailcalls which do not require
2100 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002101 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002102 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002103
2104 if (isTailCall)
2105 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002106 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002107
Chris Lattner29689432010-03-11 00:22:57 +00002108 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2109 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002110
Chris Lattner638402b2007-02-28 07:00:42 +00002111 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002112 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002113 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002114 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002115
2116 // Allocate shadow area for Win64
2117 if (IsWin64) {
2118 CCInfo.AllocateStack(32, 8);
2119 }
2120
Duncan Sands45907662010-10-31 13:21:44 +00002121 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002122
Chris Lattner423c5f42007-02-28 05:31:48 +00002123 // Get a count of how many bytes are to be pushed on the stack.
2124 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002125 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002126 // This is a sibcall. The memory operands are available in caller's
2127 // own caller's stack.
2128 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002129 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002130 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002131
Gordon Henriksen86737662008-01-05 16:56:59 +00002132 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002133 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002134 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002135 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002136 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2137 FPDiff = NumBytesCallerPushed - NumBytes;
2138
2139 // Set the delta of movement of the returnaddr stackslot.
2140 // But only set if delta is greater than previous delta.
2141 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2142 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2143 }
2144
Evan Chengf22f9b32010-02-06 03:28:46 +00002145 if (!IsSibcall)
2146 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002147
Dan Gohman475871a2008-07-27 21:46:04 +00002148 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002149 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002150 if (isTailCall && FPDiff)
2151 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2152 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002153
Dan Gohman475871a2008-07-27 21:46:04 +00002154 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2155 SmallVector<SDValue, 8> MemOpChains;
2156 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002157
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002158 // Walk the register/memloc assignments, inserting copies/loads. In the case
2159 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002160 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2161 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002162 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002163 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002164 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002165 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002166
Chris Lattner423c5f42007-02-28 05:31:48 +00002167 // Promote the value if needed.
2168 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002169 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002170 case CCValAssign::Full: break;
2171 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002172 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002173 break;
2174 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002175 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002176 break;
2177 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002178 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2179 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002180 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002181 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2182 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002183 } else
2184 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2185 break;
2186 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002187 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002188 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002189 case CCValAssign::Indirect: {
2190 // Store the argument.
2191 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002192 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002193 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002194 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002195 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002196 Arg = SpillSlot;
2197 break;
2198 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002199 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002200
Chris Lattner423c5f42007-02-28 05:31:48 +00002201 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002202 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2203 if (isVarArg && IsWin64) {
2204 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2205 // shadow reg if callee is a varargs function.
2206 unsigned ShadowReg = 0;
2207 switch (VA.getLocReg()) {
2208 case X86::XMM0: ShadowReg = X86::RCX; break;
2209 case X86::XMM1: ShadowReg = X86::RDX; break;
2210 case X86::XMM2: ShadowReg = X86::R8; break;
2211 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002212 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002213 if (ShadowReg)
2214 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002215 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002216 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002217 assert(VA.isMemLoc());
2218 if (StackPtr.getNode() == 0)
2219 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2220 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2221 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002222 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002223 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002224
Evan Cheng32fe1032006-05-25 00:59:30 +00002225 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002226 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002227 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002228
Evan Cheng347d5f72006-04-28 21:29:37 +00002229 // Build a sequence of copy-to-reg nodes chained together with token chain
2230 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002231 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002232 // Tail call byval lowering might overwrite argument registers so in case of
2233 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002234 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002235 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002236 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002237 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002238 InFlag = Chain.getValue(1);
2239 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002240
Chris Lattner88e1fd52009-07-09 04:24:46 +00002241 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002242 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2243 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002244 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002245 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2246 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002247 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002248 InFlag);
2249 InFlag = Chain.getValue(1);
2250 } else {
2251 // If we are tail calling and generating PIC/GOT style code load the
2252 // address of the callee into ECX. The value in ecx is used as target of
2253 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2254 // for tail calls on PIC/GOT architectures. Normally we would just put the
2255 // address of GOT into ebx and then call target@PLT. But for tail calls
2256 // ebx would be restored (since ebx is callee saved) before jumping to the
2257 // target@PLT.
2258
2259 // Note: The actual moving to ECX is done further down.
2260 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2261 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2262 !G->getGlobal()->hasProtectedVisibility())
2263 Callee = LowerGlobalAddress(Callee, DAG);
2264 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002265 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002266 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002267 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002268
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002269 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002270 // From AMD64 ABI document:
2271 // For calls that may call functions that use varargs or stdargs
2272 // (prototype-less calls or calls to functions containing ellipsis (...) in
2273 // the declaration) %al is used as hidden argument to specify the number
2274 // of SSE registers used. The contents of %al do not need to match exactly
2275 // the number of registers, but must be an ubound on the number of SSE
2276 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002277
Gordon Henriksen86737662008-01-05 16:56:59 +00002278 // Count the number of XMM registers allocated.
2279 static const unsigned XMMArgRegs[] = {
2280 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2281 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2282 };
2283 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002284 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002285 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002286
Dale Johannesendd64c412009-02-04 00:33:20 +00002287 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002288 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002289 InFlag = Chain.getValue(1);
2290 }
2291
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002292
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002293 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002294 if (isTailCall) {
2295 // Force all the incoming stack arguments to be loaded from the stack
2296 // before any new outgoing arguments are stored to the stack, because the
2297 // outgoing stack slots may alias the incoming argument stack slots, and
2298 // the alias isn't otherwise explicit. This is slightly more conservative
2299 // than necessary, because it means that each store effectively depends
2300 // on every argument instead of just those arguments it would clobber.
2301 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2302
Dan Gohman475871a2008-07-27 21:46:04 +00002303 SmallVector<SDValue, 8> MemOpChains2;
2304 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002305 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002306 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002307 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002308 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002309 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2310 CCValAssign &VA = ArgLocs[i];
2311 if (VA.isRegLoc())
2312 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002313 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002314 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002315 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002316 // Create frame index.
2317 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002318 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002319 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002320 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002321
Duncan Sands276dcbd2008-03-21 09:14:45 +00002322 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002323 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002324 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002325 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002326 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002327 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002328 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002329
Dan Gohman98ca4f22009-08-05 01:29:28 +00002330 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2331 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002332 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002333 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002334 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002335 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002336 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002337 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002338 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002339 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002340 }
2341 }
2342
2343 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002344 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002345 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002346
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002347 // Copy arguments to their registers.
2348 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002349 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002350 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002351 InFlag = Chain.getValue(1);
2352 }
Dan Gohman475871a2008-07-27 21:46:04 +00002353 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002354
Gordon Henriksen86737662008-01-05 16:56:59 +00002355 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002356 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002357 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002358 }
2359
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002360 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2361 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2362 // In the 64-bit large code model, we have to make all calls
2363 // through a register, since the call instruction's 32-bit
2364 // pc-relative offset may not be large enough to hold the whole
2365 // address.
2366 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002367 // If the callee is a GlobalAddress node (quite common, every direct call
2368 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2369 // it.
2370
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002371 // We should use extra load for direct calls to dllimported functions in
2372 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002373 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002374 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002375 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002376 bool ExtraLoad = false;
2377 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002378
Chris Lattner48a7d022009-07-09 05:02:21 +00002379 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2380 // external symbols most go through the PLT in PIC mode. If the symbol
2381 // has hidden or protected visibility, or if it is static or local, then
2382 // we don't need to use the PLT - we can directly call it.
2383 if (Subtarget->isTargetELF() &&
2384 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002385 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002386 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002387 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002388 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002389 (!Subtarget->getTargetTriple().isMacOSX() ||
2390 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002391 // PC-relative references to external symbols should go through $stub,
2392 // unless we're building with the leopard linker or later, which
2393 // automatically synthesizes these stubs.
2394 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002395 } else if (Subtarget->isPICStyleRIPRel() &&
2396 isa<Function>(GV) &&
2397 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2398 // If the function is marked as non-lazy, generate an indirect call
2399 // which loads from the GOT directly. This avoids runtime overhead
2400 // at the cost of eager binding (and one extra byte of encoding).
2401 OpFlags = X86II::MO_GOTPCREL;
2402 WrapperKind = X86ISD::WrapperRIP;
2403 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002404 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002405
Devang Patel0d881da2010-07-06 22:08:15 +00002406 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002407 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002408
2409 // Add a wrapper if needed.
2410 if (WrapperKind != ISD::DELETED_NODE)
2411 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2412 // Add extra indirection if needed.
2413 if (ExtraLoad)
2414 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2415 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002416 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002417 }
Bill Wendling056292f2008-09-16 21:48:12 +00002418 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002419 unsigned char OpFlags = 0;
2420
Evan Cheng1bf891a2010-12-01 22:59:46 +00002421 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2422 // external symbols should go through the PLT.
2423 if (Subtarget->isTargetELF() &&
2424 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2425 OpFlags = X86II::MO_PLT;
2426 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002427 (!Subtarget->getTargetTriple().isMacOSX() ||
2428 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002429 // PC-relative references to external symbols should go through $stub,
2430 // unless we're building with the leopard linker or later, which
2431 // automatically synthesizes these stubs.
2432 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002433 }
Eric Christopherfd179292009-08-27 18:07:15 +00002434
Chris Lattner48a7d022009-07-09 05:02:21 +00002435 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2436 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002437 }
2438
Chris Lattnerd96d0722007-02-25 06:40:16 +00002439 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002440 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002441 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002442
Evan Chengf22f9b32010-02-06 03:28:46 +00002443 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002444 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2445 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002446 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002447 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002448
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002449 Ops.push_back(Chain);
2450 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002451
Dan Gohman98ca4f22009-08-05 01:29:28 +00002452 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002453 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002454
Gordon Henriksen86737662008-01-05 16:56:59 +00002455 // Add argument registers to the end of the list so that they are known live
2456 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002457 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2458 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2459 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002460
Evan Cheng586ccac2008-03-18 23:36:35 +00002461 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002462 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002463 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2464
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002465 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002466 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002467 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002468
Gabor Greifba36cb52008-08-28 21:40:38 +00002469 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002470 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002471
Dan Gohman98ca4f22009-08-05 01:29:28 +00002472 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002473 // We used to do:
2474 //// If this is the first return lowered for this function, add the regs
2475 //// to the liveout set for the function.
2476 // This isn't right, although it's probably harmless on x86; liveouts
2477 // should be computed from returns not tail calls. Consider a void
2478 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002479 return DAG.getNode(X86ISD::TC_RETURN, dl,
2480 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002481 }
2482
Dale Johannesenace16102009-02-03 19:33:06 +00002483 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002484 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002485
Chris Lattner2d297092006-05-23 18:50:38 +00002486 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002487 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002488 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002489 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002490 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002491 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002492 // pops the hidden struct pointer, so we have to push it back.
2493 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002494 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002495 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002496 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002497
Gordon Henriksenae636f82008-01-03 16:47:34 +00002498 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002499 if (!IsSibcall) {
2500 Chain = DAG.getCALLSEQ_END(Chain,
2501 DAG.getIntPtrConstant(NumBytes, true),
2502 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2503 true),
2504 InFlag);
2505 InFlag = Chain.getValue(1);
2506 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002507
Chris Lattner3085e152007-02-25 08:59:22 +00002508 // Handle result values, copying them out of physregs into vregs that we
2509 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002510 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2511 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002512}
2513
Evan Cheng25ab6902006-09-08 06:48:29 +00002514
2515//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002516// Fast Calling Convention (tail call) implementation
2517//===----------------------------------------------------------------------===//
2518
2519// Like std call, callee cleans arguments, convention except that ECX is
2520// reserved for storing the tail called function address. Only 2 registers are
2521// free for argument passing (inreg). Tail call optimization is performed
2522// provided:
2523// * tailcallopt is enabled
2524// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002525// On X86_64 architecture with GOT-style position independent code only local
2526// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002527// To keep the stack aligned according to platform abi the function
2528// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2529// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002530// If a tail called function callee has more arguments than the caller the
2531// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002532// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002533// original REtADDR, but before the saved framepointer or the spilled registers
2534// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2535// stack layout:
2536// arg1
2537// arg2
2538// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002539// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002540// move area ]
2541// (possible EBP)
2542// ESI
2543// EDI
2544// local1 ..
2545
2546/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2547/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002548unsigned
2549X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2550 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002551 MachineFunction &MF = DAG.getMachineFunction();
2552 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002553 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002554 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002555 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002556 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002557 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002558 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2559 // Number smaller than 12 so just add the difference.
2560 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2561 } else {
2562 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002563 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002564 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002565 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002566 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002567}
2568
Evan Cheng5f941932010-02-05 02:21:12 +00002569/// MatchingStackOffset - Return true if the given stack call argument is
2570/// already available in the same position (relatively) of the caller's
2571/// incoming argument stack.
2572static
2573bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2574 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2575 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002576 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2577 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002578 if (Arg.getOpcode() == ISD::CopyFromReg) {
2579 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002580 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002581 return false;
2582 MachineInstr *Def = MRI->getVRegDef(VR);
2583 if (!Def)
2584 return false;
2585 if (!Flags.isByVal()) {
2586 if (!TII->isLoadFromStackSlot(Def, FI))
2587 return false;
2588 } else {
2589 unsigned Opcode = Def->getOpcode();
2590 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2591 Def->getOperand(1).isFI()) {
2592 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002593 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002594 } else
2595 return false;
2596 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002597 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2598 if (Flags.isByVal())
2599 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002600 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002601 // define @foo(%struct.X* %A) {
2602 // tail call @bar(%struct.X* byval %A)
2603 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002604 return false;
2605 SDValue Ptr = Ld->getBasePtr();
2606 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2607 if (!FINode)
2608 return false;
2609 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002610 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002611 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002612 FI = FINode->getIndex();
2613 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002614 } else
2615 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002616
Evan Cheng4cae1332010-03-05 08:38:04 +00002617 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002618 if (!MFI->isFixedObjectIndex(FI))
2619 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002620 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002621}
2622
Dan Gohman98ca4f22009-08-05 01:29:28 +00002623/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2624/// for tail call optimization. Targets which want to do tail call
2625/// optimization should implement this function.
2626bool
2627X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002628 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002629 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002630 bool isCalleeStructRet,
2631 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002632 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002633 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002634 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002635 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002636 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002637 CalleeCC != CallingConv::C)
2638 return false;
2639
Evan Cheng7096ae42010-01-29 06:45:59 +00002640 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002641 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002642 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002643 CallingConv::ID CallerCC = CallerF->getCallingConv();
2644 bool CCMatch = CallerCC == CalleeCC;
2645
Dan Gohman1797ed52010-02-08 20:27:50 +00002646 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002647 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002648 return true;
2649 return false;
2650 }
2651
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002652 // Look for obvious safe cases to perform tail call optimization that do not
2653 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002654
Evan Cheng2c12cb42010-03-26 16:26:03 +00002655 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2656 // emit a special epilogue.
2657 if (RegInfo->needsStackRealignment(MF))
2658 return false;
2659
Evan Chenga375d472010-03-15 18:54:48 +00002660 // Also avoid sibcall optimization if either caller or callee uses struct
2661 // return semantics.
2662 if (isCalleeStructRet || isCallerStructRet)
2663 return false;
2664
Chad Rosier2416da32011-06-24 21:15:36 +00002665 // An stdcall caller is expected to clean up its arguments; the callee
2666 // isn't going to do that.
2667 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2668 return false;
2669
Chad Rosier871f6642011-05-18 19:59:50 +00002670 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002671 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002672 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002673
2674 // Optimizing for varargs on Win64 is unlikely to be safe without
2675 // additional testing.
2676 if (Subtarget->isTargetWin64())
2677 return false;
2678
Chad Rosier871f6642011-05-18 19:59:50 +00002679 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002680 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2681 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002682
Chad Rosier871f6642011-05-18 19:59:50 +00002683 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2684 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2685 if (!ArgLocs[i].isRegLoc())
2686 return false;
2687 }
2688
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002689 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2690 // Therefore if it's not used by the call it is not safe to optimize this into
2691 // a sibcall.
2692 bool Unused = false;
2693 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2694 if (!Ins[i].Used) {
2695 Unused = true;
2696 break;
2697 }
2698 }
2699 if (Unused) {
2700 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002701 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2702 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002703 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002704 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002705 CCValAssign &VA = RVLocs[i];
2706 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2707 return false;
2708 }
2709 }
2710
Evan Cheng13617962010-04-30 01:12:32 +00002711 // If the calling conventions do not match, then we'd better make sure the
2712 // results are returned in the same way as what the caller expects.
2713 if (!CCMatch) {
2714 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002715 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2716 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002717 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2718
2719 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002720 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2721 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002722 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2723
2724 if (RVLocs1.size() != RVLocs2.size())
2725 return false;
2726 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2727 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2728 return false;
2729 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2730 return false;
2731 if (RVLocs1[i].isRegLoc()) {
2732 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2733 return false;
2734 } else {
2735 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2736 return false;
2737 }
2738 }
2739 }
2740
Evan Chenga6bff982010-01-30 01:22:00 +00002741 // If the callee takes no arguments then go on to check the results of the
2742 // call.
2743 if (!Outs.empty()) {
2744 // Check if stack adjustment is needed. For now, do not do this if any
2745 // argument is passed on the stack.
2746 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002747 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2748 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002749
2750 // Allocate shadow area for Win64
2751 if (Subtarget->isTargetWin64()) {
2752 CCInfo.AllocateStack(32, 8);
2753 }
2754
Duncan Sands45907662010-10-31 13:21:44 +00002755 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002756 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002757 MachineFunction &MF = DAG.getMachineFunction();
2758 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2759 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002760
2761 // Check if the arguments are already laid out in the right way as
2762 // the caller's fixed stack objects.
2763 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002764 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2765 const X86InstrInfo *TII =
2766 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002767 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2768 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002769 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002770 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002771 if (VA.getLocInfo() == CCValAssign::Indirect)
2772 return false;
2773 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002774 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2775 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002776 return false;
2777 }
2778 }
2779 }
Evan Cheng9c044672010-05-29 01:35:22 +00002780
2781 // If the tailcall address may be in a register, then make sure it's
2782 // possible to register allocate for it. In 32-bit, the call address can
2783 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002784 // callee-saved registers are restored. These happen to be the same
2785 // registers used to pass 'inreg' arguments so watch out for those.
2786 if (!Subtarget->is64Bit() &&
2787 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002788 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002789 unsigned NumInRegs = 0;
2790 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2791 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002792 if (!VA.isRegLoc())
2793 continue;
2794 unsigned Reg = VA.getLocReg();
2795 switch (Reg) {
2796 default: break;
2797 case X86::EAX: case X86::EDX: case X86::ECX:
2798 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002799 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002800 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002801 }
2802 }
2803 }
Evan Chenga6bff982010-01-30 01:22:00 +00002804 }
Evan Chengb1712452010-01-27 06:25:16 +00002805
Evan Cheng86809cc2010-02-03 03:28:02 +00002806 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002807}
2808
Dan Gohman3df24e62008-09-03 23:12:08 +00002809FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002810X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2811 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002812}
2813
2814
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002815//===----------------------------------------------------------------------===//
2816// Other Lowering Hooks
2817//===----------------------------------------------------------------------===//
2818
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002819static bool MayFoldLoad(SDValue Op) {
2820 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2821}
2822
2823static bool MayFoldIntoStore(SDValue Op) {
2824 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2825}
2826
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002827static bool isTargetShuffle(unsigned Opcode) {
2828 switch(Opcode) {
2829 default: return false;
2830 case X86ISD::PSHUFD:
2831 case X86ISD::PSHUFHW:
2832 case X86ISD::PSHUFLW:
2833 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002834 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002835 case X86ISD::SHUFPS:
2836 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002837 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002838 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002839 case X86ISD::MOVLPS:
2840 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002841 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002842 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002843 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002844 case X86ISD::MOVSS:
2845 case X86ISD::MOVSD:
Craig Topper06cb6802011-11-26 20:47:44 +00002846 case X86ISD::UNPCKLP:
2847 case X86ISD::PUNPCKL:
2848 case X86ISD::UNPCKHP:
2849 case X86ISD::PUNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002850 case X86ISD::VPERMILP:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002851 case X86ISD::VPERM2F128:
Craig Topper70b883b2011-11-28 10:14:51 +00002852 case X86ISD::VPERM2I128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002853 return true;
2854 }
2855 return false;
2856}
2857
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002858static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002859 SDValue V1, SelectionDAG &DAG) {
2860 switch(Opc) {
2861 default: llvm_unreachable("Unknown x86 shuffle node");
2862 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002863 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002864 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002865 return DAG.getNode(Opc, dl, VT, V1);
2866 }
2867
2868 return SDValue();
2869}
2870
2871static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002872 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002873 switch(Opc) {
2874 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002875 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002876 case X86ISD::PSHUFHW:
2877 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002878 case X86ISD::VPERMILP:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002879 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2880 }
2881
2882 return SDValue();
2883}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002884
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002885static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2886 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2887 switch(Opc) {
2888 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002889 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002890 case X86ISD::SHUFPD:
2891 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002892 case X86ISD::VPERM2F128:
Craig Topper70b883b2011-11-28 10:14:51 +00002893 case X86ISD::VPERM2I128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002894 return DAG.getNode(Opc, dl, VT, V1, V2,
2895 DAG.getConstant(TargetMask, MVT::i8));
2896 }
2897 return SDValue();
2898}
2899
2900static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2901 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2902 switch(Opc) {
2903 default: llvm_unreachable("Unknown x86 shuffle node");
2904 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002905 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002906 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002907 case X86ISD::MOVLPS:
2908 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002909 case X86ISD::MOVSS:
2910 case X86ISD::MOVSD:
Craig Topper06cb6802011-11-26 20:47:44 +00002911 case X86ISD::UNPCKLP:
2912 case X86ISD::PUNPCKL:
2913 case X86ISD::UNPCKHP:
2914 case X86ISD::PUNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002915 return DAG.getNode(Opc, dl, VT, V1, V2);
2916 }
2917 return SDValue();
2918}
2919
Dan Gohmand858e902010-04-17 15:26:15 +00002920SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002921 MachineFunction &MF = DAG.getMachineFunction();
2922 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2923 int ReturnAddrIndex = FuncInfo->getRAIndex();
2924
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002925 if (ReturnAddrIndex == 0) {
2926 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002927 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002928 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002929 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002930 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002931 }
2932
Evan Cheng25ab6902006-09-08 06:48:29 +00002933 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002934}
2935
2936
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002937bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2938 bool hasSymbolicDisplacement) {
2939 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002940 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002941 return false;
2942
2943 // If we don't have a symbolic displacement - we don't have any extra
2944 // restrictions.
2945 if (!hasSymbolicDisplacement)
2946 return true;
2947
2948 // FIXME: Some tweaks might be needed for medium code model.
2949 if (M != CodeModel::Small && M != CodeModel::Kernel)
2950 return false;
2951
2952 // For small code model we assume that latest object is 16MB before end of 31
2953 // bits boundary. We may also accept pretty large negative constants knowing
2954 // that all objects are in the positive half of address space.
2955 if (M == CodeModel::Small && Offset < 16*1024*1024)
2956 return true;
2957
2958 // For kernel code model we know that all object resist in the negative half
2959 // of 32bits address space. We may not accept negative offsets, since they may
2960 // be just off and we may accept pretty large positive ones.
2961 if (M == CodeModel::Kernel && Offset > 0)
2962 return true;
2963
2964 return false;
2965}
2966
Evan Chengef41ff62011-06-23 17:54:54 +00002967/// isCalleePop - Determines whether the callee is required to pop its
2968/// own arguments. Callee pop is necessary to support tail calls.
2969bool X86::isCalleePop(CallingConv::ID CallingConv,
2970 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2971 if (IsVarArg)
2972 return false;
2973
2974 switch (CallingConv) {
2975 default:
2976 return false;
2977 case CallingConv::X86_StdCall:
2978 return !is64Bit;
2979 case CallingConv::X86_FastCall:
2980 return !is64Bit;
2981 case CallingConv::X86_ThisCall:
2982 return !is64Bit;
2983 case CallingConv::Fast:
2984 return TailCallOpt;
2985 case CallingConv::GHC:
2986 return TailCallOpt;
2987 }
2988}
2989
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002990/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2991/// specific condition code, returning the condition code and the LHS/RHS of the
2992/// comparison to make.
2993static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2994 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002995 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002996 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2997 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2998 // X > -1 -> X == 0, jump !sign.
2999 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003000 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003001 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
3002 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003003 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00003004 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003005 // X < 1 -> X <= 0
3006 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003007 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003008 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003009 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003010
Evan Chengd9558e02006-01-06 00:43:03 +00003011 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003012 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003013 case ISD::SETEQ: return X86::COND_E;
3014 case ISD::SETGT: return X86::COND_G;
3015 case ISD::SETGE: return X86::COND_GE;
3016 case ISD::SETLT: return X86::COND_L;
3017 case ISD::SETLE: return X86::COND_LE;
3018 case ISD::SETNE: return X86::COND_NE;
3019 case ISD::SETULT: return X86::COND_B;
3020 case ISD::SETUGT: return X86::COND_A;
3021 case ISD::SETULE: return X86::COND_BE;
3022 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003023 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003024 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003025
Chris Lattner4c78e022008-12-23 23:42:27 +00003026 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003027
Chris Lattner4c78e022008-12-23 23:42:27 +00003028 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003029 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3030 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003031 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3032 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003033 }
3034
Chris Lattner4c78e022008-12-23 23:42:27 +00003035 switch (SetCCOpcode) {
3036 default: break;
3037 case ISD::SETOLT:
3038 case ISD::SETOLE:
3039 case ISD::SETUGT:
3040 case ISD::SETUGE:
3041 std::swap(LHS, RHS);
3042 break;
3043 }
3044
3045 // On a floating point condition, the flags are set as follows:
3046 // ZF PF CF op
3047 // 0 | 0 | 0 | X > Y
3048 // 0 | 0 | 1 | X < Y
3049 // 1 | 0 | 0 | X == Y
3050 // 1 | 1 | 1 | unordered
3051 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003052 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003053 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003054 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003055 case ISD::SETOLT: // flipped
3056 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003057 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003058 case ISD::SETOLE: // flipped
3059 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003060 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003061 case ISD::SETUGT: // flipped
3062 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003063 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003064 case ISD::SETUGE: // flipped
3065 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003066 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003067 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003068 case ISD::SETNE: return X86::COND_NE;
3069 case ISD::SETUO: return X86::COND_P;
3070 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003071 case ISD::SETOEQ:
3072 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003073 }
Evan Chengd9558e02006-01-06 00:43:03 +00003074}
3075
Evan Cheng4a460802006-01-11 00:33:36 +00003076/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3077/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003078/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003079static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003080 switch (X86CC) {
3081 default:
3082 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003083 case X86::COND_B:
3084 case X86::COND_BE:
3085 case X86::COND_E:
3086 case X86::COND_P:
3087 case X86::COND_A:
3088 case X86::COND_AE:
3089 case X86::COND_NE:
3090 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003091 return true;
3092 }
3093}
3094
Evan Chengeb2f9692009-10-27 19:56:55 +00003095/// isFPImmLegal - Returns true if the target can instruction select the
3096/// specified FP immediate natively. If false, the legalizer will
3097/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003098bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003099 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3100 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3101 return true;
3102 }
3103 return false;
3104}
3105
Nate Begeman9008ca62009-04-27 18:41:29 +00003106/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3107/// the specified range (L, H].
3108static bool isUndefOrInRange(int Val, int Low, int Hi) {
3109 return (Val < 0) || (Val >= Low && Val < Hi);
3110}
3111
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003112/// isUndefOrInRange - Return true if every element in Mask, begining
3113/// from position Pos and ending in Pos+Size, falls within the specified
3114/// range (L, L+Pos]. or is undef.
3115static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3116 int Pos, int Size, int Low, int Hi) {
3117 for (int i = Pos, e = Pos+Size; i != e; ++i)
3118 if (!isUndefOrInRange(Mask[i], Low, Hi))
3119 return false;
3120 return true;
3121}
3122
Nate Begeman9008ca62009-04-27 18:41:29 +00003123/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3124/// specified value.
3125static bool isUndefOrEqual(int Val, int CmpVal) {
3126 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003127 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003128 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003129}
3130
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003131/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3132/// from position Pos and ending in Pos+Size, falls within the specified
3133/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003134static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3135 int Pos, int Size, int Low) {
3136 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3137 if (!isUndefOrEqual(Mask[i], Low))
3138 return false;
3139 return true;
3140}
3141
Nate Begeman9008ca62009-04-27 18:41:29 +00003142/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3143/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3144/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003145static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003146 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003147 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003148 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003149 return (Mask[0] < 2 && Mask[1] < 2);
3150 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003151}
3152
Nate Begeman9008ca62009-04-27 18:41:29 +00003153bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003154 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003155 N->getMask(M);
3156 return ::isPSHUFDMask(M, N->getValueType(0));
3157}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003158
Nate Begeman9008ca62009-04-27 18:41:29 +00003159/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3160/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003161static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003162 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003163 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003164
Nate Begeman9008ca62009-04-27 18:41:29 +00003165 // Lower quadword copied in order or undef.
3166 for (int i = 0; i != 4; ++i)
3167 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003168 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003169
Evan Cheng506d3df2006-03-29 23:07:14 +00003170 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003171 for (int i = 4; i != 8; ++i)
3172 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003173 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003174
Evan Cheng506d3df2006-03-29 23:07:14 +00003175 return true;
3176}
3177
Nate Begeman9008ca62009-04-27 18:41:29 +00003178bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003179 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003180 N->getMask(M);
3181 return ::isPSHUFHWMask(M, N->getValueType(0));
3182}
Evan Cheng506d3df2006-03-29 23:07:14 +00003183
Nate Begeman9008ca62009-04-27 18:41:29 +00003184/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3185/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003186static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003187 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003188 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003189
Rafael Espindola15684b22009-04-24 12:40:33 +00003190 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003191 for (int i = 4; i != 8; ++i)
3192 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003193 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003194
Rafael Espindola15684b22009-04-24 12:40:33 +00003195 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003196 for (int i = 0; i != 4; ++i)
3197 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003198 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003199
Rafael Espindola15684b22009-04-24 12:40:33 +00003200 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003201}
3202
Nate Begeman9008ca62009-04-27 18:41:29 +00003203bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003204 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003205 N->getMask(M);
3206 return ::isPSHUFLWMask(M, N->getValueType(0));
3207}
3208
Nate Begemana09008b2009-10-19 02:17:23 +00003209/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3210/// is suitable for input to PALIGNR.
3211static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003212 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003213 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003214 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3215 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003216
Nate Begemana09008b2009-10-19 02:17:23 +00003217 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003218 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003219 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003220
Nate Begemana09008b2009-10-19 02:17:23 +00003221 for (i = 0; i != e; ++i)
3222 if (Mask[i] >= 0)
3223 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003224
Nate Begemana09008b2009-10-19 02:17:23 +00003225 // All undef, not a palignr.
3226 if (i == e)
3227 return false;
3228
Eli Friedman63f8dde2011-07-25 21:36:45 +00003229 // Make sure we're shifting in the right direction.
3230 if (Mask[i] <= i)
3231 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003232
3233 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003234
Nate Begemana09008b2009-10-19 02:17:23 +00003235 // Check the rest of the elements to see if they are consecutive.
3236 for (++i; i != e; ++i) {
3237 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003238 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003239 return false;
3240 }
3241 return true;
3242}
3243
Craig Topper9d7025b2011-11-27 21:41:12 +00003244/// isVSHUFPYMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003245/// specifies a shuffle of elements that is suitable for input to 256-bit
3246/// VSHUFPSY.
Craig Topper9d7025b2011-11-27 21:41:12 +00003247static bool isVSHUFPYMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper71c4c122011-11-28 01:14:24 +00003248 bool HasAVX) {
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003249 int NumElems = VT.getVectorNumElements();
3250
Craig Topper71c4c122011-11-28 01:14:24 +00003251 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003252 return false;
3253
Craig Topper9d7025b2011-11-27 21:41:12 +00003254 if (NumElems != 4 && NumElems != 8)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003255 return false;
3256
3257 // VSHUFPSY divides the resulting vector into 4 chunks.
3258 // The sources are also splitted into 4 chunks, and each destination
3259 // chunk must come from a different source chunk.
3260 //
3261 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3262 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3263 //
3264 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3265 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3266 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003267 // VSHUFPDY divides the resulting vector into 4 chunks.
3268 // The sources are also splitted into 4 chunks, and each destination
3269 // chunk must come from a different source chunk.
3270 //
3271 // SRC1 => X3 X2 X1 X0
3272 // SRC2 => Y3 Y2 Y1 Y0
3273 //
3274 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3275 //
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003276 int QuarterSize = NumElems/4;
3277 int HalfSize = QuarterSize*2;
3278 for (int i = 0; i < QuarterSize; ++i)
3279 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3280 return false;
3281 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3282 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3283 return false;
3284
Craig Topper9d7025b2011-11-27 21:41:12 +00003285 // For VSHUFPSY, the mask of the second half must be the same as the first
Craig Topper70b883b2011-11-28 10:14:51 +00003286 // but with the appropriate offsets. This works in the same way as
3287 // VPERMILPS works with masks.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003288 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3289 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3290 return false;
Craig Topper9d7025b2011-11-27 21:41:12 +00003291 if (NumElems == 4)
3292 continue;
3293 // VSHUFPSY handling
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003294 int FstHalfIdx = i-HalfSize;
3295 if (Mask[FstHalfIdx] < 0)
3296 continue;
3297 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3298 return false;
3299 }
3300 for (int i = QuarterSize*3; i < NumElems; ++i) {
3301 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3302 return false;
3303 int FstHalfIdx = i-HalfSize;
Craig Topper9d7025b2011-11-27 21:41:12 +00003304 if (NumElems == 4)
3305 continue;
3306 // VSHUFPSY handling
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003307 if (Mask[FstHalfIdx] < 0)
3308 continue;
3309 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3310 return false;
Craig Topper71c4c122011-11-28 01:14:24 +00003311 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003312
Craig Topper71c4c122011-11-28 01:14:24 +00003313 return true;
3314}
3315
3316/// isCommutedVSHUFP() - Returns true if the shuffle mask is exactly
3317/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3318/// half elements to come from vector 1 (which would equal the dest.) and
3319/// the upper half to come from vector 2.
3320static bool isCommutedVSHUFPY(ShuffleVectorSDNode *N, bool HasAVX) {
3321 EVT VT = N->getValueType(0);
3322 int NumElems = VT.getVectorNumElements();
3323 SmallVector<int, 8> Mask;
3324 N->getMask(Mask);
3325
3326 if (!HasAVX || VT.getSizeInBits() != 256)
3327 return false;
3328
3329 if (NumElems != 4 && NumElems != 8)
3330 return false;
3331
3332 // VSHUFPSY divides the resulting vector into 4 chunks.
3333 // The sources are also splitted into 4 chunks, and each destination
3334 // chunk must come from a different source chunk.
3335 //
3336 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3337 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3338 //
3339 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3340 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3341 //
3342 // VSHUFPDY divides the resulting vector into 4 chunks.
3343 // The sources are also splitted into 4 chunks, and each destination
3344 // chunk must come from a different source chunk.
3345 //
3346 // SRC1 => X3 X2 X1 X0
3347 // SRC2 => Y3 Y2 Y1 Y0
3348 //
3349 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3350 //
3351 int QuarterSize = NumElems/4;
3352 int HalfSize = QuarterSize*2;
3353 for (int i = 0; i < QuarterSize; ++i)
3354 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3355 return false;
3356 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3357 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3358 return false;
3359
3360 // For VSHUFPSY, the mask of the second half must be the same as the first
Craig Topper70b883b2011-11-28 10:14:51 +00003361 // but with the appropriate offsets. This works in the same way as
3362 // VPERMILPS works with masks.
Craig Topper71c4c122011-11-28 01:14:24 +00003363 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3364 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3365 return false;
3366 if (NumElems == 4)
3367 continue;
3368 // VSHUFPSY handling
3369 int FstHalfIdx = i-HalfSize;
3370 if (Mask[FstHalfIdx] < 0)
3371 continue;
3372 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3373 return false;
3374 }
3375 for (int i = QuarterSize*3; i < NumElems; ++i) {
3376 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3377 return false;
3378 if (NumElems == 4)
3379 continue;
3380 // VSHUFPSY handling
3381 int FstHalfIdx = i-HalfSize;
3382 if (Mask[FstHalfIdx] < 0)
3383 continue;
3384 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3385 return false;
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003386 }
3387
3388 return true;
3389}
3390
Craig Topper9d7025b2011-11-27 21:41:12 +00003391/// getShuffleVSHUFPYImmediate - Return the appropriate immediate to shuffle
3392/// the specified VECTOR_MASK mask with VSHUFPSY/VSHUFPDY instructions.
3393static unsigned getShuffleVSHUFPYImmediate(SDNode *N) {
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003394 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3395 EVT VT = SVOp->getValueType(0);
3396 int NumElems = VT.getVectorNumElements();
3397
Craig Topper9d7025b2011-11-27 21:41:12 +00003398 assert(VT.getSizeInBits() == 256 && "Only supports 256-bit types");
3399 assert((NumElems == 4 || NumElems == 8) && "Only supports v4 and v8 types");
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003400
3401 int HalfSize = NumElems/2;
Craig Topper9d7025b2011-11-27 21:41:12 +00003402 unsigned Mul = (NumElems == 8) ? 2 : 1;
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003403 unsigned Mask = 0;
Craig Topper71c4c122011-11-28 01:14:24 +00003404 for (int i = 0; i != NumElems; ++i) {
Craig Topper9d7025b2011-11-27 21:41:12 +00003405 int Elt = SVOp->getMaskElt(i);
3406 if (Elt < 0)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003407 continue;
Craig Topper9d7025b2011-11-27 21:41:12 +00003408 Elt %= HalfSize;
3409 unsigned Shamt = i;
3410 // For VSHUFPSY, the mask of the first half must be equal to the second one.
3411 if (NumElems == 8) Shamt %= HalfSize;
3412 Mask |= Elt << (Shamt*Mul);
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003413 }
3414
3415 return Mask;
3416}
3417
Elena Demikhovsky52a35a82011-11-23 10:23:16 +00003418/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3419/// the two vector operands have swapped position.
3420static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
3421 unsigned NumElems = VT.getVectorNumElements();
3422 for (unsigned i = 0; i != NumElems; ++i) {
3423 int idx = Mask[i];
3424 if (idx < 0)
3425 continue;
3426 else if (idx < (int)NumElems)
3427 Mask[i] = idx + NumElems;
3428 else
3429 Mask[i] = idx - NumElems;
3430 }
3431}
3432
Evan Cheng14aed5e2006-03-24 01:18:28 +00003433/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003434/// specifies a shuffle of elements that is suitable for input to 128-bit
3435/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003436static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003437 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003438
3439 if (VT.getSizeInBits() != 128)
3440 return false;
3441
Nate Begeman9008ca62009-04-27 18:41:29 +00003442 if (NumElems != 2 && NumElems != 4)
3443 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003444
Nate Begeman9008ca62009-04-27 18:41:29 +00003445 int Half = NumElems / 2;
3446 for (int i = 0; i < Half; ++i)
3447 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003448 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003449 for (int i = Half; i < NumElems; ++i)
3450 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003451 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003452
Evan Cheng14aed5e2006-03-24 01:18:28 +00003453 return true;
3454}
3455
Nate Begeman9008ca62009-04-27 18:41:29 +00003456bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3457 SmallVector<int, 8> M;
3458 N->getMask(M);
3459 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003460}
3461
Craig Topper71c4c122011-11-28 01:14:24 +00003462/// isCommutedSHUFPMask - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003463/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3464/// half elements to come from vector 1 (which would equal the dest.) and
3465/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003466static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003467 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003468
3469 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003470 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003471
Nate Begeman9008ca62009-04-27 18:41:29 +00003472 int Half = NumElems / 2;
3473 for (int i = 0; i < Half; ++i)
3474 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003475 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003476 for (int i = Half; i < NumElems; ++i)
3477 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003478 return false;
3479 return true;
3480}
3481
Nate Begeman9008ca62009-04-27 18:41:29 +00003482static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3483 SmallVector<int, 8> M;
3484 N->getMask(M);
3485 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003486}
3487
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003488/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3489/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003490bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003491 EVT VT = N->getValueType(0);
3492 unsigned NumElems = VT.getVectorNumElements();
3493
3494 if (VT.getSizeInBits() != 128)
3495 return false;
3496
3497 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003498 return false;
3499
Evan Cheng2064a2b2006-03-28 06:50:32 +00003500 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003501 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3502 isUndefOrEqual(N->getMaskElt(1), 7) &&
3503 isUndefOrEqual(N->getMaskElt(2), 2) &&
3504 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003505}
3506
Nate Begeman0b10b912009-11-07 23:17:15 +00003507/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3508/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3509/// <2, 3, 2, 3>
3510bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003511 EVT VT = N->getValueType(0);
3512 unsigned NumElems = VT.getVectorNumElements();
3513
3514 if (VT.getSizeInBits() != 128)
3515 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003516
Nate Begeman0b10b912009-11-07 23:17:15 +00003517 if (NumElems != 4)
3518 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003519
Nate Begeman0b10b912009-11-07 23:17:15 +00003520 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003521 isUndefOrEqual(N->getMaskElt(1), 3) &&
3522 isUndefOrEqual(N->getMaskElt(2), 2) &&
3523 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003524}
3525
Evan Cheng5ced1d82006-04-06 23:23:56 +00003526/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3527/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003528bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3529 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003530
Evan Cheng5ced1d82006-04-06 23:23:56 +00003531 if (NumElems != 2 && NumElems != 4)
3532 return false;
3533
Evan Chengc5cdff22006-04-07 21:53:05 +00003534 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003535 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003536 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003537
Evan Chengc5cdff22006-04-07 21:53:05 +00003538 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003539 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003540 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003541
3542 return true;
3543}
3544
Nate Begeman0b10b912009-11-07 23:17:15 +00003545/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3546/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3547bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003548 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003549
David Greenea20244d2011-03-02 17:23:43 +00003550 if ((NumElems != 2 && NumElems != 4)
3551 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003552 return false;
3553
Evan Chengc5cdff22006-04-07 21:53:05 +00003554 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003555 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003556 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003557
Nate Begeman9008ca62009-04-27 18:41:29 +00003558 for (unsigned i = 0; i < NumElems/2; ++i)
3559 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003560 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003561
3562 return true;
3563}
3564
Evan Cheng0038e592006-03-28 00:39:58 +00003565/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3566/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003567static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003568 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003569 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003570
3571 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3572 "Unsupported vector type for unpckh");
3573
Craig Topper6347e862011-11-21 06:57:39 +00003574 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003575 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003576 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003577
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003578 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3579 // independently on 128-bit lanes.
3580 unsigned NumLanes = VT.getSizeInBits()/128;
3581 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003582
3583 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003584 unsigned End = NumLaneElts;
3585 for (unsigned s = 0; s < NumLanes; ++s) {
3586 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003587 i != End;
3588 i += 2, ++j) {
3589 int BitI = Mask[i];
3590 int BitI1 = Mask[i+1];
3591 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003592 return false;
David Greenea20244d2011-03-02 17:23:43 +00003593 if (V2IsSplat) {
3594 if (!isUndefOrEqual(BitI1, NumElts))
3595 return false;
3596 } else {
3597 if (!isUndefOrEqual(BitI1, j + NumElts))
3598 return false;
3599 }
Evan Cheng39623da2006-04-20 08:58:49 +00003600 }
David Greenea20244d2011-03-02 17:23:43 +00003601 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003602 Start += NumLaneElts;
3603 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003604 }
David Greenea20244d2011-03-02 17:23:43 +00003605
Evan Cheng0038e592006-03-28 00:39:58 +00003606 return true;
3607}
3608
Craig Topper6347e862011-11-21 06:57:39 +00003609bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003610 SmallVector<int, 8> M;
3611 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003612 return ::isUNPCKLMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003613}
3614
Evan Cheng4fcb9222006-03-28 02:43:26 +00003615/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3616/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003617static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003618 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003619 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003620
3621 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3622 "Unsupported vector type for unpckh");
3623
Craig Topper6347e862011-11-21 06:57:39 +00003624 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003625 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003626 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003627
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003628 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3629 // independently on 128-bit lanes.
3630 unsigned NumLanes = VT.getSizeInBits()/128;
3631 unsigned NumLaneElts = NumElts/NumLanes;
3632
3633 unsigned Start = 0;
3634 unsigned End = NumLaneElts;
3635 for (unsigned l = 0; l != NumLanes; ++l) {
3636 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3637 i != End; i += 2, ++j) {
3638 int BitI = Mask[i];
3639 int BitI1 = Mask[i+1];
3640 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003641 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003642 if (V2IsSplat) {
3643 if (isUndefOrEqual(BitI1, NumElts))
3644 return false;
3645 } else {
3646 if (!isUndefOrEqual(BitI1, j+NumElts))
3647 return false;
3648 }
Evan Cheng39623da2006-04-20 08:58:49 +00003649 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003650 // Process the next 128 bits.
3651 Start += NumLaneElts;
3652 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003653 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003654 return true;
3655}
3656
Craig Topper6347e862011-11-21 06:57:39 +00003657bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003658 SmallVector<int, 8> M;
3659 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003660 return ::isUNPCKHMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003661}
3662
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003663/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3664/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3665/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003666static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003667 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003668 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003669 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003670
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003671 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3672 // FIXME: Need a better way to get rid of this, there's no latency difference
3673 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3674 // the former later. We should also remove the "_undef" special mask.
3675 if (NumElems == 4 && VT.getSizeInBits() == 256)
3676 return false;
3677
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003678 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3679 // independently on 128-bit lanes.
3680 unsigned NumLanes = VT.getSizeInBits() / 128;
3681 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003682
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003683 for (unsigned s = 0; s < NumLanes; ++s) {
3684 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3685 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003686 i += 2, ++j) {
3687 int BitI = Mask[i];
3688 int BitI1 = Mask[i+1];
3689
3690 if (!isUndefOrEqual(BitI, j))
3691 return false;
3692 if (!isUndefOrEqual(BitI1, j))
3693 return false;
3694 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003695 }
David Greenea20244d2011-03-02 17:23:43 +00003696
Rafael Espindola15684b22009-04-24 12:40:33 +00003697 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003698}
3699
Nate Begeman9008ca62009-04-27 18:41:29 +00003700bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3701 SmallVector<int, 8> M;
3702 N->getMask(M);
3703 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3704}
3705
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003706/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3707/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3708/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003709static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003710 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003711 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3712 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003713
Nate Begeman9008ca62009-04-27 18:41:29 +00003714 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3715 int BitI = Mask[i];
3716 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003717 if (!isUndefOrEqual(BitI, j))
3718 return false;
3719 if (!isUndefOrEqual(BitI1, j))
3720 return false;
3721 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003722 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003723}
3724
Nate Begeman9008ca62009-04-27 18:41:29 +00003725bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3726 SmallVector<int, 8> M;
3727 N->getMask(M);
3728 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3729}
3730
Evan Cheng017dcc62006-04-21 01:05:10 +00003731/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3732/// specifies a shuffle of elements that is suitable for input to MOVSS,
3733/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003734static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003735 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003736 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003737
3738 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003739
Nate Begeman9008ca62009-04-27 18:41:29 +00003740 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003741 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003742
Nate Begeman9008ca62009-04-27 18:41:29 +00003743 for (int i = 1; i < NumElts; ++i)
3744 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003745 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003746
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003747 return true;
3748}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003749
Nate Begeman9008ca62009-04-27 18:41:29 +00003750bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3751 SmallVector<int, 8> M;
3752 N->getMask(M);
3753 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003754}
3755
Craig Topper70b883b2011-11-28 10:14:51 +00003756/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003757/// as permutations between 128-bit chunks or halves. As an example: this
3758/// shuffle bellow:
3759/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3760/// The first half comes from the second half of V1 and the second half from the
3761/// the second half of V2.
Craig Topper70b883b2011-11-28 10:14:51 +00003762static bool isVPERM2X128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3763 bool HasAVX) {
3764 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003765 return false;
3766
3767 // The shuffle result is divided into half A and half B. In total the two
3768 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3769 // B must come from C, D, E or F.
3770 int HalfSize = VT.getVectorNumElements()/2;
3771 bool MatchA = false, MatchB = false;
3772
3773 // Check if A comes from one of C, D, E, F.
3774 for (int Half = 0; Half < 4; ++Half) {
3775 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3776 MatchA = true;
3777 break;
3778 }
3779 }
3780
3781 // Check if B comes from one of C, D, E, F.
3782 for (int Half = 0; Half < 4; ++Half) {
3783 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3784 MatchB = true;
3785 break;
3786 }
3787 }
3788
3789 return MatchA && MatchB;
3790}
3791
Craig Topper70b883b2011-11-28 10:14:51 +00003792/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3793/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
3794static unsigned getShuffleVPERM2X128Immediate(SDNode *N) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003795 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3796 EVT VT = SVOp->getValueType(0);
3797
3798 int HalfSize = VT.getVectorNumElements()/2;
3799
3800 int FstHalf = 0, SndHalf = 0;
3801 for (int i = 0; i < HalfSize; ++i) {
3802 if (SVOp->getMaskElt(i) > 0) {
3803 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3804 break;
3805 }
3806 }
3807 for (int i = HalfSize; i < HalfSize*2; ++i) {
3808 if (SVOp->getMaskElt(i) > 0) {
3809 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3810 break;
3811 }
3812 }
3813
3814 return (FstHalf | (SndHalf << 4));
3815}
3816
Craig Topper70b883b2011-11-28 10:14:51 +00003817/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003818/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3819/// Note that VPERMIL mask matching is different depending whether theunderlying
3820/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3821/// to the same elements of the low, but to the higher half of the source.
3822/// In VPERMILPD the two lanes could be shuffled independently of each other
3823/// with the same restriction that lanes can't be crossed.
Craig Topper70b883b2011-11-28 10:14:51 +00003824static bool isVPERMILPMask(const SmallVectorImpl<int> &Mask, EVT VT,
3825 bool HasAVX) {
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003826 int NumElts = VT.getVectorNumElements();
3827 int NumLanes = VT.getSizeInBits()/128;
3828
Craig Topper70b883b2011-11-28 10:14:51 +00003829 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003830 return false;
3831
Craig Topper70b883b2011-11-28 10:14:51 +00003832 // Only match 256-bit with 32/64-bit types
3833 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003834 return false;
3835
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003836 int LaneSize = NumElts/NumLanes;
Craig Topper70b883b2011-11-28 10:14:51 +00003837 for (int l = 0; l != NumLanes; ++l) {
3838 int LaneStart = l*LaneSize;
3839 for (int i = 0; i != LaneSize; ++i) {
3840 if (!isUndefOrInRange(Mask[i+LaneStart], LaneStart, LaneStart+LaneSize))
3841 return false;
3842 if (NumElts == 4 || l == 0)
3843 continue;
3844 // VPERMILPS handling
3845 if (Mask[i] < 0)
3846 continue;
3847 if (!isUndefOrEqual(Mask[i+LaneStart], Mask[i]+LaneSize))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003848 return false;
3849 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003850 }
3851
3852 return true;
3853}
3854
Craig Topper70b883b2011-11-28 10:14:51 +00003855/// getShuffleVPERMILPImmediate - Return the appropriate immediate to shuffle
3856/// the specified VECTOR_MASK mask with VPERMILPS/D* instructions.
3857static unsigned getShuffleVPERMILPImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003858 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3859 EVT VT = SVOp->getValueType(0);
3860
3861 int NumElts = VT.getVectorNumElements();
3862 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003863 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003864
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003865 // Although the mask is equal for both lanes do it twice to get the cases
3866 // where a mask will match because the same mask element is undef on the
3867 // first half but valid on the second. This would get pathological cases
3868 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Craig Topper70b883b2011-11-28 10:14:51 +00003869 unsigned Shift = (LaneSize == 4) ? 2 : 1;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003870 unsigned Mask = 0;
Craig Topper70b883b2011-11-28 10:14:51 +00003871 for (int i = 0; i != NumElts; ++i) {
3872 int MaskElt = SVOp->getMaskElt(i);
3873 if (MaskElt < 0)
3874 continue;
3875 MaskElt %= LaneSize;
3876 unsigned Shamt = i;
3877 // VPERMILPSY, the mask of the first half must be equal to the second one
3878 if (NumElts == 8) Shamt %= LaneSize;
3879 Mask |= MaskElt << (Shamt*Shift);
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003880 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003881
3882 return Mask;
3883}
3884
Evan Cheng017dcc62006-04-21 01:05:10 +00003885/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3886/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003887/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003888static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003889 bool V2IsSplat = false, bool V2IsUndef = false) {
3890 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003891 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003892 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003893
Nate Begeman9008ca62009-04-27 18:41:29 +00003894 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003895 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003896
Nate Begeman9008ca62009-04-27 18:41:29 +00003897 for (int i = 1; i < NumOps; ++i)
3898 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3899 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3900 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003901 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003902
Evan Cheng39623da2006-04-20 08:58:49 +00003903 return true;
3904}
3905
Nate Begeman9008ca62009-04-27 18:41:29 +00003906static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003907 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003908 SmallVector<int, 8> M;
3909 N->getMask(M);
3910 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003911}
3912
Evan Chengd9539472006-04-14 21:59:03 +00003913/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3914/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003915/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3916bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3917 const X86Subtarget *Subtarget) {
Craig Topperc0d82852011-11-22 00:44:41 +00003918 if (!Subtarget->hasSSE3orAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003919 return false;
3920
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003921 // The second vector must be undef
3922 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3923 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003924
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003925 EVT VT = N->getValueType(0);
3926 unsigned NumElems = VT.getVectorNumElements();
3927
3928 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3929 (VT.getSizeInBits() == 256 && NumElems != 8))
3930 return false;
3931
3932 // "i+1" is the value the indexed mask element must have
3933 for (unsigned i = 0; i < NumElems; i += 2)
3934 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3935 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003936 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003937
3938 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003939}
3940
3941/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3942/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003943/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3944bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3945 const X86Subtarget *Subtarget) {
Craig Topperc0d82852011-11-22 00:44:41 +00003946 if (!Subtarget->hasSSE3orAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003947 return false;
3948
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003949 // The second vector must be undef
3950 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3951 return false;
3952
3953 EVT VT = N->getValueType(0);
3954 unsigned NumElems = VT.getVectorNumElements();
3955
3956 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3957 (VT.getSizeInBits() == 256 && NumElems != 8))
3958 return false;
3959
3960 // "i" is the value the indexed mask element must have
3961 for (unsigned i = 0; i < NumElems; i += 2)
3962 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3963 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003964 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003965
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003966 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003967}
3968
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003969/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3970/// specifies a shuffle of elements that is suitable for input to 256-bit
3971/// version of MOVDDUP.
3972static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
3973 const X86Subtarget *Subtarget) {
3974 EVT VT = N->getValueType(0);
3975 int NumElts = VT.getVectorNumElements();
3976 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
3977
3978 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
3979 !V2IsUndef || NumElts != 4)
3980 return false;
3981
3982 for (int i = 0; i != NumElts/2; ++i)
3983 if (!isUndefOrEqual(N->getMaskElt(i), 0))
3984 return false;
3985 for (int i = NumElts/2; i != NumElts; ++i)
3986 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
3987 return false;
3988 return true;
3989}
3990
Evan Cheng0b457f02008-09-25 20:50:48 +00003991/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003992/// specifies a shuffle of elements that is suitable for input to 128-bit
3993/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003994bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003995 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003996
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003997 if (VT.getSizeInBits() != 128)
3998 return false;
3999
4000 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004001 for (int i = 0; i < e; ++i)
4002 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004003 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004004 for (int i = 0; i < e; ++i)
4005 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004006 return false;
4007 return true;
4008}
4009
David Greenec38a03e2011-02-03 15:50:00 +00004010/// isVEXTRACTF128Index - Return true if the specified
4011/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4012/// suitable for input to VEXTRACTF128.
4013bool X86::isVEXTRACTF128Index(SDNode *N) {
4014 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4015 return false;
4016
4017 // The index should be aligned on a 128-bit boundary.
4018 uint64_t Index =
4019 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4020
4021 unsigned VL = N->getValueType(0).getVectorNumElements();
4022 unsigned VBits = N->getValueType(0).getSizeInBits();
4023 unsigned ElSize = VBits / VL;
4024 bool Result = (Index * ElSize) % 128 == 0;
4025
4026 return Result;
4027}
4028
David Greeneccacdc12011-02-04 16:08:29 +00004029/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4030/// operand specifies a subvector insert that is suitable for input to
4031/// VINSERTF128.
4032bool X86::isVINSERTF128Index(SDNode *N) {
4033 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4034 return false;
4035
4036 // The index should be aligned on a 128-bit boundary.
4037 uint64_t Index =
4038 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4039
4040 unsigned VL = N->getValueType(0).getVectorNumElements();
4041 unsigned VBits = N->getValueType(0).getSizeInBits();
4042 unsigned ElSize = VBits / VL;
4043 bool Result = (Index * ElSize) % 128 == 0;
4044
4045 return Result;
4046}
4047
Evan Cheng63d33002006-03-22 08:01:21 +00004048/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004049/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004050unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004051 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4052 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4053
Evan Chengb9df0ca2006-03-22 02:53:00 +00004054 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4055 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004056 for (int i = 0; i < NumOperands; ++i) {
4057 int Val = SVOp->getMaskElt(NumOperands-i-1);
4058 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004059 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004060 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004061 if (i != NumOperands - 1)
4062 Mask <<= Shift;
4063 }
Evan Cheng63d33002006-03-22 08:01:21 +00004064 return Mask;
4065}
4066
Evan Cheng506d3df2006-03-29 23:07:14 +00004067/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004068/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004069unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004070 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004071 unsigned Mask = 0;
4072 // 8 nodes, but we only care about the last 4.
4073 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004074 int Val = SVOp->getMaskElt(i);
4075 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004076 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004077 if (i != 4)
4078 Mask <<= 2;
4079 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004080 return Mask;
4081}
4082
4083/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004084/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004085unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004086 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004087 unsigned Mask = 0;
4088 // 8 nodes, but we only care about the first 4.
4089 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004090 int Val = SVOp->getMaskElt(i);
4091 if (Val >= 0)
4092 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004093 if (i != 0)
4094 Mask <<= 2;
4095 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004096 return Mask;
4097}
4098
Nate Begemana09008b2009-10-19 02:17:23 +00004099/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4100/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4101unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4102 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4103 EVT VVT = N->getValueType(0);
4104 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4105 int Val = 0;
4106
4107 unsigned i, e;
4108 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4109 Val = SVOp->getMaskElt(i);
4110 if (Val >= 0)
4111 break;
4112 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004113 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004114 return (Val - i) * EltSize;
4115}
4116
David Greenec38a03e2011-02-03 15:50:00 +00004117/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4118/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4119/// instructions.
4120unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4121 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4122 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4123
4124 uint64_t Index =
4125 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4126
4127 EVT VecVT = N->getOperand(0).getValueType();
4128 EVT ElVT = VecVT.getVectorElementType();
4129
4130 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004131 return Index / NumElemsPerChunk;
4132}
4133
David Greeneccacdc12011-02-04 16:08:29 +00004134/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4135/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4136/// instructions.
4137unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4138 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4139 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4140
4141 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004142 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004143
4144 EVT VecVT = N->getValueType(0);
4145 EVT ElVT = VecVT.getVectorElementType();
4146
4147 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004148 return Index / NumElemsPerChunk;
4149}
4150
Evan Cheng37b73872009-07-30 08:33:02 +00004151/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4152/// constant +0.0.
4153bool X86::isZeroNode(SDValue Elt) {
4154 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004155 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004156 (isa<ConstantFPSDNode>(Elt) &&
4157 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4158}
4159
Nate Begeman9008ca62009-04-27 18:41:29 +00004160/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4161/// their permute mask.
4162static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4163 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004164 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004165 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004166 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004167
Nate Begeman5a5ca152009-04-29 05:20:52 +00004168 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004169 int idx = SVOp->getMaskElt(i);
4170 if (idx < 0)
4171 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004172 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004173 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004174 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004175 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004176 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004177 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4178 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004179}
4180
Evan Cheng533a0aa2006-04-19 20:35:22 +00004181/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4182/// match movhlps. The lower half elements should come from upper half of
4183/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004184/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004185static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004186 EVT VT = Op->getValueType(0);
4187 if (VT.getSizeInBits() != 128)
4188 return false;
4189 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004190 return false;
4191 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004192 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004193 return false;
4194 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004195 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004196 return false;
4197 return true;
4198}
4199
Evan Cheng5ced1d82006-04-06 23:23:56 +00004200/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004201/// is promoted to a vector. It also returns the LoadSDNode by reference if
4202/// required.
4203static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004204 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4205 return false;
4206 N = N->getOperand(0).getNode();
4207 if (!ISD::isNON_EXTLoad(N))
4208 return false;
4209 if (LD)
4210 *LD = cast<LoadSDNode>(N);
4211 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004212}
4213
Dan Gohman65fd6562011-11-03 21:49:52 +00004214// Test whether the given value is a vector value which will be legalized
4215// into a load.
4216static bool WillBeConstantPoolLoad(SDNode *N) {
4217 if (N->getOpcode() != ISD::BUILD_VECTOR)
4218 return false;
4219
4220 // Check for any non-constant elements.
4221 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4222 switch (N->getOperand(i).getNode()->getOpcode()) {
4223 case ISD::UNDEF:
4224 case ISD::ConstantFP:
4225 case ISD::Constant:
4226 break;
4227 default:
4228 return false;
4229 }
4230
4231 // Vectors of all-zeros and all-ones are materialized with special
4232 // instructions rather than being loaded.
4233 return !ISD::isBuildVectorAllZeros(N) &&
4234 !ISD::isBuildVectorAllOnes(N);
4235}
4236
Evan Cheng533a0aa2006-04-19 20:35:22 +00004237/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4238/// match movlp{s|d}. The lower half elements should come from lower half of
4239/// V1 (and in order), and the upper half elements should come from the upper
4240/// half of V2 (and in order). And since V1 will become the source of the
4241/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004242static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4243 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004244 EVT VT = Op->getValueType(0);
4245 if (VT.getSizeInBits() != 128)
4246 return false;
4247
Evan Cheng466685d2006-10-09 20:57:25 +00004248 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004249 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004250 // Is V2 is a vector load, don't do this transformation. We will try to use
4251 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004252 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004253 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004254
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004255 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004256
Evan Cheng533a0aa2006-04-19 20:35:22 +00004257 if (NumElems != 2 && NumElems != 4)
4258 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004259 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004260 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004261 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004262 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004263 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004264 return false;
4265 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004266}
4267
Evan Cheng39623da2006-04-20 08:58:49 +00004268/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4269/// all the same.
4270static bool isSplatVector(SDNode *N) {
4271 if (N->getOpcode() != ISD::BUILD_VECTOR)
4272 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004273
Dan Gohman475871a2008-07-27 21:46:04 +00004274 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004275 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4276 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004277 return false;
4278 return true;
4279}
4280
Evan Cheng213d2cf2007-05-17 18:45:50 +00004281/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004282/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004283/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004284static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004285 SDValue V1 = N->getOperand(0);
4286 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004287 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4288 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004289 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004290 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004291 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004292 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4293 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004294 if (Opc != ISD::BUILD_VECTOR ||
4295 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004296 return false;
4297 } else if (Idx >= 0) {
4298 unsigned Opc = V1.getOpcode();
4299 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4300 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004301 if (Opc != ISD::BUILD_VECTOR ||
4302 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004303 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004304 }
4305 }
4306 return true;
4307}
4308
4309/// getZeroVector - Returns a vector of specified type with all zero elements.
4310///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004311static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004312 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004313 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004314
Dale Johannesen0488fb62010-09-30 23:57:10 +00004315 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004316 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004317 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004318 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004319 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004320 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4321 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4322 } else { // SSE1
4323 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4324 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4325 }
4326 } else if (VT.getSizeInBits() == 256) { // AVX
4327 // 256-bit logic and arithmetic instructions in AVX are
4328 // all floating-point, no support for integer ops. Default
4329 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004330 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004331 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4332 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004333 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004334 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004335}
4336
Chris Lattner8a594482007-11-25 00:24:49 +00004337/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004338/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4339/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4340/// Then bitcast to their original type, ensuring they get CSE'd.
4341static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4342 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004343 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004344 assert((VT.is128BitVector() || VT.is256BitVector())
4345 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004346
Owen Anderson825b72b2009-08-11 20:47:22 +00004347 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004348 SDValue Vec;
4349 if (VT.getSizeInBits() == 256) {
4350 if (HasAVX2) { // AVX2
4351 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4352 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4353 } else { // AVX
4354 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4355 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4356 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4357 Vec = Insert128BitVector(InsV, Vec,
4358 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4359 }
4360 } else {
4361 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004362 }
4363
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004364 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004365}
4366
Evan Cheng39623da2006-04-20 08:58:49 +00004367/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4368/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004369static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004370 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004371 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004372
Evan Cheng39623da2006-04-20 08:58:49 +00004373 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004374 SmallVector<int, 8> MaskVec;
4375 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004376
Nate Begeman5a5ca152009-04-29 05:20:52 +00004377 for (unsigned i = 0; i != NumElems; ++i) {
4378 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004379 MaskVec[i] = NumElems;
4380 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004381 }
Evan Cheng39623da2006-04-20 08:58:49 +00004382 }
Evan Cheng39623da2006-04-20 08:58:49 +00004383 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004384 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4385 SVOp->getOperand(1), &MaskVec[0]);
4386 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004387}
4388
Evan Cheng017dcc62006-04-21 01:05:10 +00004389/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4390/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004391static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004392 SDValue V2) {
4393 unsigned NumElems = VT.getVectorNumElements();
4394 SmallVector<int, 8> Mask;
4395 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004396 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004397 Mask.push_back(i);
4398 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004399}
4400
Nate Begeman9008ca62009-04-27 18:41:29 +00004401/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004402static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004403 SDValue V2) {
4404 unsigned NumElems = VT.getVectorNumElements();
4405 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004406 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004407 Mask.push_back(i);
4408 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004409 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004410 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004411}
4412
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004413/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004414static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004415 SDValue V2) {
4416 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004417 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004418 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004419 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004420 Mask.push_back(i + Half);
4421 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004422 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004423 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004424}
4425
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004426// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004427// a generic shuffle instruction because the target has no such instructions.
4428// Generate shuffles which repeat i16 and i8 several times until they can be
4429// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004430static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004431 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004432 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004433 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004434
Nate Begeman9008ca62009-04-27 18:41:29 +00004435 while (NumElems > 4) {
4436 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004437 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004438 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004439 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004440 EltNo -= NumElems/2;
4441 }
4442 NumElems >>= 1;
4443 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004444 return V;
4445}
Eric Christopherfd179292009-08-27 18:07:15 +00004446
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004447/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4448static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4449 EVT VT = V.getValueType();
4450 DebugLoc dl = V.getDebugLoc();
4451 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4452 && "Vector size not supported");
4453
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004454 if (VT.getSizeInBits() == 128) {
4455 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004456 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004457 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4458 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004459 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004460 // To use VPERMILPS to splat scalars, the second half of indicies must
4461 // refer to the higher part, which is a duplication of the lower one,
4462 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004463 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4464 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004465
4466 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4467 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4468 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004469 }
4470
4471 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4472}
4473
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004474/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004475static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4476 EVT SrcVT = SV->getValueType(0);
4477 SDValue V1 = SV->getOperand(0);
4478 DebugLoc dl = SV->getDebugLoc();
4479
4480 int EltNo = SV->getSplatIndex();
4481 int NumElems = SrcVT.getVectorNumElements();
4482 unsigned Size = SrcVT.getSizeInBits();
4483
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004484 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4485 "Unknown how to promote splat for type");
4486
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004487 // Extract the 128-bit part containing the splat element and update
4488 // the splat element index when it refers to the higher register.
4489 if (Size == 256) {
4490 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4491 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4492 if (Idx > 0)
4493 EltNo -= NumElems/2;
4494 }
4495
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004496 // All i16 and i8 vector types can't be used directly by a generic shuffle
4497 // instruction because the target has no such instruction. Generate shuffles
4498 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004499 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004500 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004501 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004502 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004503
4504 // Recreate the 256-bit vector and place the same 128-bit vector
4505 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004506 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004507 if (Size == 256) {
4508 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4509 DAG.getConstant(0, MVT::i32), DAG, dl);
4510 V1 = Insert128BitVector(InsV, V1,
4511 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4512 }
4513
4514 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004515}
4516
Evan Chengba05f722006-04-21 23:03:30 +00004517/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004518/// vector of zero or undef vector. This produces a shuffle where the low
4519/// element of V2 is swizzled into the zero/undef vector, landing at element
4520/// 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 +00004521static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004522 bool isZero, bool HasXMMInt,
4523 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004524 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004525 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004526 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004527 unsigned NumElems = VT.getVectorNumElements();
4528 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004529 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004530 // If this is the insertion idx, put the low elt of V2 here.
4531 MaskVec.push_back(i == Idx ? NumElems : i);
4532 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004533}
4534
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004535/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4536/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004537static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4538 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004539 if (Depth == 6)
4540 return SDValue(); // Limit search depth.
4541
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004542 SDValue V = SDValue(N, 0);
4543 EVT VT = V.getValueType();
4544 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004545
4546 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4547 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4548 Index = SV->getMaskElt(Index);
4549
4550 if (Index < 0)
4551 return DAG.getUNDEF(VT.getVectorElementType());
4552
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004553 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004554 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004555 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004556 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004557
4558 // Recurse into target specific vector shuffles to find scalars.
4559 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004560 int NumElems = VT.getVectorNumElements();
4561 SmallVector<unsigned, 16> ShuffleMask;
4562 SDValue ImmN;
4563
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004564 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004565 case X86ISD::SHUFPS:
4566 case X86ISD::SHUFPD:
4567 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper36e36ac2011-11-29 07:49:05 +00004568 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4569 ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004570 break;
Craig Topper06cb6802011-11-26 20:47:44 +00004571 case X86ISD::PUNPCKH:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004572 DecodePUNPCKHMask(NumElems, ShuffleMask);
4573 break;
Craig Topper06cb6802011-11-26 20:47:44 +00004574 case X86ISD::UNPCKHP:
Craig Topperf7de5772011-11-22 01:57:35 +00004575 DecodeUNPCKHPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004576 break;
Craig Topper06cb6802011-11-26 20:47:44 +00004577 case X86ISD::PUNPCKL:
David Greenec4db4e52011-02-28 19:06:56 +00004578 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004579 break;
Craig Topper06cb6802011-11-26 20:47:44 +00004580 case X86ISD::UNPCKLP:
David Greenec4db4e52011-02-28 19:06:56 +00004581 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004582 break;
4583 case X86ISD::MOVHLPS:
4584 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4585 break;
4586 case X86ISD::MOVLHPS:
4587 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4588 break;
4589 case X86ISD::PSHUFD:
4590 ImmN = N->getOperand(N->getNumOperands()-1);
4591 DecodePSHUFMask(NumElems,
4592 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4593 ShuffleMask);
4594 break;
4595 case X86ISD::PSHUFHW:
4596 ImmN = N->getOperand(N->getNumOperands()-1);
4597 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4598 ShuffleMask);
4599 break;
4600 case X86ISD::PSHUFLW:
4601 ImmN = N->getOperand(N->getNumOperands()-1);
4602 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4603 ShuffleMask);
4604 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004605 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004606 case X86ISD::MOVSD: {
4607 // The index 0 always comes from the first element of the second source,
4608 // this is why MOVSS and MOVSD are used in the first place. The other
4609 // elements come from the other positions of the first source vector.
4610 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004611 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4612 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004613 }
Craig Topper316cd2a2011-11-30 06:25:25 +00004614 case X86ISD::VPERMILP:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004615 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper316cd2a2011-11-30 06:25:25 +00004616 DecodeVPERMILPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004617 ShuffleMask);
4618 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004619 case X86ISD::VPERM2F128:
Craig Topper70b883b2011-11-28 10:14:51 +00004620 case X86ISD::VPERM2I128:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004621 ImmN = N->getOperand(N->getNumOperands()-1);
4622 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4623 ShuffleMask);
4624 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004625 case X86ISD::MOVDDUP:
4626 case X86ISD::MOVLHPD:
4627 case X86ISD::MOVLPD:
4628 case X86ISD::MOVLPS:
4629 case X86ISD::MOVSHDUP:
4630 case X86ISD::MOVSLDUP:
4631 case X86ISD::PALIGN:
4632 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004633 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004634 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004635 return SDValue();
4636 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004637
4638 Index = ShuffleMask[Index];
4639 if (Index < 0)
4640 return DAG.getUNDEF(VT.getVectorElementType());
4641
4642 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4643 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4644 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004645 }
4646
4647 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004648 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004649 V = V.getOperand(0);
4650 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004651 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004652
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004653 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004654 return SDValue();
4655 }
4656
4657 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4658 return (Index == 0) ? V.getOperand(0)
4659 : DAG.getUNDEF(VT.getVectorElementType());
4660
4661 if (V.getOpcode() == ISD::BUILD_VECTOR)
4662 return V.getOperand(Index);
4663
4664 return SDValue();
4665}
4666
4667/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4668/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004669/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004670static
4671unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4672 bool ZerosFromLeft, SelectionDAG &DAG) {
4673 int i = 0;
4674
4675 while (i < NumElems) {
4676 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004677 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004678 if (!(Elt.getNode() &&
4679 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4680 break;
4681 ++i;
4682 }
4683
4684 return i;
4685}
4686
4687/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4688/// MaskE correspond consecutively to elements from one of the vector operands,
4689/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4690static
4691bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4692 int OpIdx, int NumElems, unsigned &OpNum) {
4693 bool SeenV1 = false;
4694 bool SeenV2 = false;
4695
4696 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4697 int Idx = SVOp->getMaskElt(i);
4698 // Ignore undef indicies
4699 if (Idx < 0)
4700 continue;
4701
4702 if (Idx < NumElems)
4703 SeenV1 = true;
4704 else
4705 SeenV2 = true;
4706
4707 // Only accept consecutive elements from the same vector
4708 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4709 return false;
4710 }
4711
4712 OpNum = SeenV1 ? 0 : 1;
4713 return true;
4714}
4715
4716/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4717/// logical left shift of a vector.
4718static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4719 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4720 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4721 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4722 false /* check zeros from right */, DAG);
4723 unsigned OpSrc;
4724
4725 if (!NumZeros)
4726 return false;
4727
4728 // Considering the elements in the mask that are not consecutive zeros,
4729 // check if they consecutively come from only one of the source vectors.
4730 //
4731 // V1 = {X, A, B, C} 0
4732 // \ \ \ /
4733 // vector_shuffle V1, V2 <1, 2, 3, X>
4734 //
4735 if (!isShuffleMaskConsecutive(SVOp,
4736 0, // Mask Start Index
4737 NumElems-NumZeros-1, // Mask End Index
4738 NumZeros, // Where to start looking in the src vector
4739 NumElems, // Number of elements in vector
4740 OpSrc)) // Which source operand ?
4741 return false;
4742
4743 isLeft = false;
4744 ShAmt = NumZeros;
4745 ShVal = SVOp->getOperand(OpSrc);
4746 return true;
4747}
4748
4749/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4750/// logical left shift of a vector.
4751static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4752 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4753 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4754 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4755 true /* check zeros from left */, DAG);
4756 unsigned OpSrc;
4757
4758 if (!NumZeros)
4759 return false;
4760
4761 // Considering the elements in the mask that are not consecutive zeros,
4762 // check if they consecutively come from only one of the source vectors.
4763 //
4764 // 0 { A, B, X, X } = V2
4765 // / \ / /
4766 // vector_shuffle V1, V2 <X, X, 4, 5>
4767 //
4768 if (!isShuffleMaskConsecutive(SVOp,
4769 NumZeros, // Mask Start Index
4770 NumElems-1, // Mask End Index
4771 0, // Where to start looking in the src vector
4772 NumElems, // Number of elements in vector
4773 OpSrc)) // Which source operand ?
4774 return false;
4775
4776 isLeft = true;
4777 ShAmt = NumZeros;
4778 ShVal = SVOp->getOperand(OpSrc);
4779 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004780}
4781
4782/// isVectorShift - Returns true if the shuffle can be implemented as a
4783/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004784static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004785 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004786 // Although the logic below support any bitwidth size, there are no
4787 // shift instructions which handle more than 128-bit vectors.
4788 if (SVOp->getValueType(0).getSizeInBits() > 128)
4789 return false;
4790
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004791 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4792 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4793 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004794
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004795 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004796}
4797
Evan Chengc78d3b42006-04-24 18:01:45 +00004798/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4799///
Dan Gohman475871a2008-07-27 21:46:04 +00004800static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004801 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004802 SelectionDAG &DAG,
4803 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004804 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004805 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004806
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004807 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004808 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004809 bool First = true;
4810 for (unsigned i = 0; i < 16; ++i) {
4811 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4812 if (ThisIsNonZero && First) {
4813 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004814 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004815 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004816 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004817 First = false;
4818 }
4819
4820 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004821 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004822 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4823 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004824 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004825 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004826 }
4827 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004828 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4829 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4830 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004831 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004832 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004833 } else
4834 ThisElt = LastElt;
4835
Gabor Greifba36cb52008-08-28 21:40:38 +00004836 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004837 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004838 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004839 }
4840 }
4841
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004842 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004843}
4844
Bill Wendlinga348c562007-03-22 18:42:45 +00004845/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004846///
Dan Gohman475871a2008-07-27 21:46:04 +00004847static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004848 unsigned NumNonZero, unsigned NumZero,
4849 SelectionDAG &DAG,
4850 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004851 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004852 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004853
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004854 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004855 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004856 bool First = true;
4857 for (unsigned i = 0; i < 8; ++i) {
4858 bool isNonZero = (NonZeros & (1 << i)) != 0;
4859 if (isNonZero) {
4860 if (First) {
4861 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004862 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004863 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004864 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004865 First = false;
4866 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004867 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004868 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004869 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004870 }
4871 }
4872
4873 return V;
4874}
4875
Evan Chengf26ffe92008-05-29 08:22:04 +00004876/// getVShift - Return a vector logical shift node.
4877///
Owen Andersone50ed302009-08-10 22:56:29 +00004878static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004879 unsigned NumBits, SelectionDAG &DAG,
4880 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004881 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004882 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004883 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004884 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4885 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004886 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004887 DAG.getConstant(NumBits,
4888 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004889}
4890
Dan Gohman475871a2008-07-27 21:46:04 +00004891SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004892X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004893 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004894
Evan Chengc3630942009-12-09 21:00:30 +00004895 // Check if the scalar load can be widened into a vector load. And if
4896 // the address is "base + cst" see if the cst can be "absorbed" into
4897 // the shuffle mask.
4898 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4899 SDValue Ptr = LD->getBasePtr();
4900 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4901 return SDValue();
4902 EVT PVT = LD->getValueType(0);
4903 if (PVT != MVT::i32 && PVT != MVT::f32)
4904 return SDValue();
4905
4906 int FI = -1;
4907 int64_t Offset = 0;
4908 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4909 FI = FINode->getIndex();
4910 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004911 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004912 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4913 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4914 Offset = Ptr.getConstantOperandVal(1);
4915 Ptr = Ptr.getOperand(0);
4916 } else {
4917 return SDValue();
4918 }
4919
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004920 // FIXME: 256-bit vector instructions don't require a strict alignment,
4921 // improve this code to support it better.
4922 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004923 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004924 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004925 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004926 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004927 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004928 // Can't change the alignment. FIXME: It's possible to compute
4929 // the exact stack offset and reference FI + adjust offset instead.
4930 // If someone *really* cares about this. That's the way to implement it.
4931 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004932 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004933 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004934 }
4935 }
4936
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004937 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004938 // Ptr + (Offset & ~15).
4939 if (Offset < 0)
4940 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004941 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004942 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004943 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004944 if (StartOffset)
4945 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4946 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4947
4948 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004949 int NumElems = VT.getVectorNumElements();
4950
4951 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4952 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4953 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004954 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004955 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004956
4957 // Canonicalize it to a v4i32 or v8i32 shuffle.
4958 SmallVector<int, 8> Mask;
4959 for (int i = 0; i < NumElems; ++i)
4960 Mask.push_back(EltNo);
4961
4962 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4963 return DAG.getNode(ISD::BITCAST, dl, NVT,
4964 DAG.getVectorShuffle(CanonVT, dl, V1,
4965 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004966 }
4967
4968 return SDValue();
4969}
4970
Michael J. Spencerec38de22010-10-10 22:04:20 +00004971/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4972/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004973/// load which has the same value as a build_vector whose operands are 'elts'.
4974///
4975/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004976///
Nate Begeman1449f292010-03-24 22:19:06 +00004977/// FIXME: we'd also like to handle the case where the last elements are zero
4978/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4979/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004980static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004981 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004982 EVT EltVT = VT.getVectorElementType();
4983 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004984
Nate Begemanfdea31a2010-03-24 20:49:50 +00004985 LoadSDNode *LDBase = NULL;
4986 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004987
Nate Begeman1449f292010-03-24 22:19:06 +00004988 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004989 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004990 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004991 for (unsigned i = 0; i < NumElems; ++i) {
4992 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004993
Nate Begemanfdea31a2010-03-24 20:49:50 +00004994 if (!Elt.getNode() ||
4995 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4996 return SDValue();
4997 if (!LDBase) {
4998 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4999 return SDValue();
5000 LDBase = cast<LoadSDNode>(Elt.getNode());
5001 LastLoadedElt = i;
5002 continue;
5003 }
5004 if (Elt.getOpcode() == ISD::UNDEF)
5005 continue;
5006
5007 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5008 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5009 return SDValue();
5010 LastLoadedElt = i;
5011 }
Nate Begeman1449f292010-03-24 22:19:06 +00005012
5013 // If we have found an entire vector of loads and undefs, then return a large
5014 // load of the entire vector width starting at the base pointer. If we found
5015 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005016 if (LastLoadedElt == NumElems - 1) {
5017 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005018 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005019 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005020 LDBase->isVolatile(), LDBase->isNonTemporal(),
5021 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005022 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005023 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005024 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005025 LDBase->isInvariant(), LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005026 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5027 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005028 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5029 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005030 SDValue ResNode =
5031 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5032 LDBase->getPointerInfo(),
5033 LDBase->getAlignment(),
5034 false/*isVolatile*/, true/*ReadMem*/,
5035 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005036 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005037 }
5038 return SDValue();
5039}
5040
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005041/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
5042/// a vbroadcast node. We support two patterns:
5043/// 1. A splat BUILD_VECTOR which uses a single scalar load.
5044/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
5045/// a scalar load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005046/// The scalar load node is returned when a pattern is found,
5047/// or SDValue() otherwise.
5048static SDValue isVectorBroadcast(SDValue &Op, bool hasAVX2) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005049 EVT VT = Op.getValueType();
5050 SDValue V = Op;
5051
5052 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
5053 V = V.getOperand(0);
5054
5055 //A suspected load to be broadcasted.
5056 SDValue Ld;
5057
5058 switch (V.getOpcode()) {
5059 default:
5060 // Unknown pattern found.
5061 return SDValue();
5062
5063 case ISD::BUILD_VECTOR: {
5064 // The BUILD_VECTOR node must be a splat.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005065 if (!isSplatVector(V.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005066 return SDValue();
5067
5068 Ld = V.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005069
5070 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005071 // of its users are from the BUILD_VECTOR node.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005072 if (!Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005073 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005074 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005075 }
5076
5077 case ISD::VECTOR_SHUFFLE: {
5078 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5079
5080 // Shuffles must have a splat mask where the first element is
5081 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005082 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005083 return SDValue();
5084
5085 SDValue Sc = Op.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005086 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005087 return SDValue();
5088
5089 Ld = Sc.getOperand(0);
5090
5091 // The scalar_to_vector node and the suspected
5092 // load node must have exactly one user.
5093 if (!Sc.hasOneUse() || !Ld.hasOneUse())
5094 return SDValue();
5095 break;
5096 }
5097 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005098
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005099 // The scalar source must be a normal load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005100 if (!ISD::isNormalLoad(Ld.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005101 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005102
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005103 bool Is256 = VT.getSizeInBits() == 256;
5104 bool Is128 = VT.getSizeInBits() == 128;
5105 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5106
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005107 if (hasAVX2) {
5108 // VBroadcast to YMM
5109 if (Is256 && (ScalarSize == 8 || ScalarSize == 16 ||
5110 ScalarSize == 32 || ScalarSize == 64 ))
5111 return Ld;
5112
5113 // VBroadcast to XMM
5114 if (Is128 && (ScalarSize == 8 || ScalarSize == 32 ||
5115 ScalarSize == 16 || ScalarSize == 64 ))
5116 return Ld;
5117 }
5118
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005119 // VBroadcast to YMM
5120 if (Is256 && (ScalarSize == 32 || ScalarSize == 64))
5121 return Ld;
5122
5123 // VBroadcast to XMM
5124 if (Is128 && (ScalarSize == 32))
5125 return Ld;
5126
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005127
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005128 // Unsupported broadcast.
5129 return SDValue();
5130}
5131
Evan Chengc3630942009-12-09 21:00:30 +00005132SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005133X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005134 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005135
David Greenef125a292011-02-08 19:04:41 +00005136 EVT VT = Op.getValueType();
5137 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005138 unsigned NumElems = Op.getNumOperands();
5139
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005140 // Vectors containing all zeros can be matched by pxor and xorps later
5141 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5142 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5143 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005144 if (Op.getValueType() == MVT::v4i32 ||
5145 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005146 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005147
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005148 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005149 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005150
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005151 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005152 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5153 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005154 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper745a86b2011-11-19 22:34:59 +00005155 if (Op.getValueType() == MVT::v4i32 ||
5156 (Op.getValueType() == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005157 return Op;
5158
Craig Topper745a86b2011-11-19 22:34:59 +00005159 return getOnesVector(Op.getValueType(), Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005160 }
5161
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005162 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005163 if (Subtarget->hasAVX() && LD.getNode())
5164 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
5165
Owen Andersone50ed302009-08-10 22:56:29 +00005166 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005167
Evan Cheng0db9fe62006-04-25 20:13:52 +00005168 unsigned NumZero = 0;
5169 unsigned NumNonZero = 0;
5170 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005171 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005172 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005173 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005174 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005175 if (Elt.getOpcode() == ISD::UNDEF)
5176 continue;
5177 Values.insert(Elt);
5178 if (Elt.getOpcode() != ISD::Constant &&
5179 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005180 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005181 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005182 NumZero++;
5183 else {
5184 NonZeros |= (1 << i);
5185 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005186 }
5187 }
5188
Chris Lattner97a2a562010-08-26 05:24:29 +00005189 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5190 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005191 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005192
Chris Lattner67f453a2008-03-09 05:42:06 +00005193 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005194 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005195 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005196 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005197
Chris Lattner62098042008-03-09 01:05:04 +00005198 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5199 // the value are obviously zero, truncate the value to i32 and do the
5200 // insertion that way. Only do this if the value is non-constant or if the
5201 // value is a constant being inserted into element 0. It is cheaper to do
5202 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005203 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005204 (!IsAllConstants || Idx == 0)) {
5205 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005206 // Handle SSE only.
5207 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5208 EVT VecVT = MVT::v4i32;
5209 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005210
Chris Lattner62098042008-03-09 01:05:04 +00005211 // Truncate the value (which may itself be a constant) to i32, and
5212 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005213 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005214 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005215 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005216 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005217
Chris Lattner62098042008-03-09 01:05:04 +00005218 // Now we have our 32-bit value zero extended in the low element of
5219 // a vector. If Idx != 0, swizzle it into place.
5220 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005221 SmallVector<int, 4> Mask;
5222 Mask.push_back(Idx);
5223 for (unsigned i = 1; i != VecElts; ++i)
5224 Mask.push_back(i);
5225 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005226 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005227 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005228 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005229 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005230 }
5231 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005232
Chris Lattner19f79692008-03-08 22:59:52 +00005233 // If we have a constant or non-constant insertion into the low element of
5234 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5235 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005236 // depending on what the source datatype is.
5237 if (Idx == 0) {
5238 if (NumZero == 0) {
5239 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005240 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5241 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005242 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5243 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005244 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005245 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005246 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5247 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005248 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5249 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005250 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5251 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005252 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005253 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005254 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005255 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005256
5257 // Is it a vector logical left shift?
5258 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005259 X86::isZeroNode(Op.getOperand(0)) &&
5260 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005261 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005262 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005263 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005264 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005265 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005266 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005267
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005268 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005269 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005270
Chris Lattner19f79692008-03-08 22:59:52 +00005271 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5272 // is a non-constant being inserted into an element other than the low one,
5273 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5274 // movd/movss) to move this into the low element, then shuffle it into
5275 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005276 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005277 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005278
Evan Cheng0db9fe62006-04-25 20:13:52 +00005279 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005280 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005281 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005282 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005283 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005284 MaskVec.push_back(i == Idx ? 0 : 1);
5285 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005286 }
5287 }
5288
Chris Lattner67f453a2008-03-09 05:42:06 +00005289 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005290 if (Values.size() == 1) {
5291 if (EVTBits == 32) {
5292 // Instead of a shuffle like this:
5293 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5294 // Check if it's possible to issue this instead.
5295 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5296 unsigned Idx = CountTrailingZeros_32(NonZeros);
5297 SDValue Item = Op.getOperand(Idx);
5298 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5299 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5300 }
Dan Gohman475871a2008-07-27 21:46:04 +00005301 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005302 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005303
Dan Gohmana3941172007-07-24 22:55:08 +00005304 // A vector full of immediates; various special cases are already
5305 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005306 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005307 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005308
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005309 // For AVX-length vectors, build the individual 128-bit pieces and use
5310 // shuffles to put them in place.
5311 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5312 SmallVector<SDValue, 32> V;
5313 for (unsigned i = 0; i < NumElems; ++i)
5314 V.push_back(Op.getOperand(i));
5315
5316 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5317
5318 // Build both the lower and upper subvector.
5319 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5320 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5321 NumElems/2);
5322
5323 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005324 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5325 DAG.getConstant(0, MVT::i32), DAG, dl);
5326 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005327 DAG, dl);
5328 }
5329
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005330 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005331 if (EVTBits == 64) {
5332 if (NumNonZero == 1) {
5333 // One half is zero or undef.
5334 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005335 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005336 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005337 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005338 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005339 }
Dan Gohman475871a2008-07-27 21:46:04 +00005340 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005341 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005342
5343 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005344 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005345 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005346 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005347 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005348 }
5349
Bill Wendling826f36f2007-03-28 00:57:11 +00005350 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005351 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005352 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005353 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005354 }
5355
5356 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005357 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005358 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005359 if (NumElems == 4 && NumZero > 0) {
5360 for (unsigned i = 0; i < 4; ++i) {
5361 bool isZero = !(NonZeros & (1 << i));
5362 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005363 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005364 else
Dale Johannesenace16102009-02-03 19:33:06 +00005365 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005366 }
5367
5368 for (unsigned i = 0; i < 2; ++i) {
5369 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5370 default: break;
5371 case 0:
5372 V[i] = V[i*2]; // Must be a zero vector.
5373 break;
5374 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005375 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005376 break;
5377 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005378 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005379 break;
5380 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005381 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005382 break;
5383 }
5384 }
5385
Nate Begeman9008ca62009-04-27 18:41:29 +00005386 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005387 bool Reverse = (NonZeros & 0x3) == 2;
5388 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005389 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005390 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5391 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005392 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5393 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005394 }
5395
Nate Begemanfdea31a2010-03-24 20:49:50 +00005396 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5397 // Check for a build vector of consecutive loads.
5398 for (unsigned i = 0; i < NumElems; ++i)
5399 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005400
Nate Begemanfdea31a2010-03-24 20:49:50 +00005401 // Check for elements which are consecutive loads.
5402 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5403 if (LD.getNode())
5404 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005405
5406 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperc0d82852011-11-22 00:44:41 +00005407 if (getSubtarget()->hasSSE41orAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005408 SDValue Result;
5409 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5410 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5411 else
5412 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005413
Chris Lattner24faf612010-08-28 17:59:08 +00005414 for (unsigned i = 1; i < NumElems; ++i) {
5415 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5416 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005417 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005418 }
5419 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005420 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005421
Chris Lattner6e80e442010-08-28 17:15:43 +00005422 // Otherwise, expand into a number of unpckl*, start by extending each of
5423 // our (non-undef) elements to the full vector width with the element in the
5424 // bottom slot of the vector (which generates no code for SSE).
5425 for (unsigned i = 0; i < NumElems; ++i) {
5426 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5427 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5428 else
5429 V[i] = DAG.getUNDEF(VT);
5430 }
5431
5432 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005433 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5434 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5435 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005436 unsigned EltStride = NumElems >> 1;
5437 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005438 for (unsigned i = 0; i < EltStride; ++i) {
5439 // If V[i+EltStride] is undef and this is the first round of mixing,
5440 // then it is safe to just drop this shuffle: V[i] is already in the
5441 // right place, the one element (since it's the first round) being
5442 // inserted as undef can be dropped. This isn't safe for successive
5443 // rounds because they will permute elements within both vectors.
5444 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5445 EltStride == NumElems/2)
5446 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005447
Chris Lattner6e80e442010-08-28 17:15:43 +00005448 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005449 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005450 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005451 }
5452 return V[0];
5453 }
Dan Gohman475871a2008-07-27 21:46:04 +00005454 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005455}
5456
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005457// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5458// them in a MMX register. This is better than doing a stack convert.
5459static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005460 DebugLoc dl = Op.getDebugLoc();
5461 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005462
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005463 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5464 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5465 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005466 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005467 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5468 InVec = Op.getOperand(1);
5469 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5470 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005471 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005472 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5473 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5474 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005475 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005476 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5477 Mask[0] = 0; Mask[1] = 2;
5478 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5479 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005480 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005481}
5482
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005483// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5484// to create 256-bit vectors from two other 128-bit ones.
5485static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5486 DebugLoc dl = Op.getDebugLoc();
5487 EVT ResVT = Op.getValueType();
5488
5489 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5490
5491 SDValue V1 = Op.getOperand(0);
5492 SDValue V2 = Op.getOperand(1);
5493 unsigned NumElems = ResVT.getVectorNumElements();
5494
5495 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5496 DAG.getConstant(0, MVT::i32), DAG, dl);
5497 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5498 DAG, dl);
5499}
5500
5501SDValue
5502X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005503 EVT ResVT = Op.getValueType();
5504
5505 assert(Op.getNumOperands() == 2);
5506 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5507 "Unsupported CONCAT_VECTORS for value type");
5508
5509 // We support concatenate two MMX registers and place them in a MMX register.
5510 // This is better than doing a stack convert.
5511 if (ResVT.is128BitVector())
5512 return LowerMMXCONCAT_VECTORS(Op, DAG);
5513
5514 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5515 // from two other 128-bit ones.
5516 return LowerAVXCONCAT_VECTORS(Op, DAG);
5517}
5518
Nate Begemanb9a47b82009-02-23 08:49:38 +00005519// v8i16 shuffles - Prefer shuffles in the following order:
5520// 1. [all] pshuflw, pshufhw, optional move
5521// 2. [ssse3] 1 x pshufb
5522// 3. [ssse3] 2 x pshufb + 1 x por
5523// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005524SDValue
5525X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5526 SelectionDAG &DAG) const {
5527 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005528 SDValue V1 = SVOp->getOperand(0);
5529 SDValue V2 = SVOp->getOperand(1);
5530 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005531 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005532
Nate Begemanb9a47b82009-02-23 08:49:38 +00005533 // Determine if more than 1 of the words in each of the low and high quadwords
5534 // of the result come from the same quadword of one of the two inputs. Undef
5535 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005536 unsigned LoQuad[] = { 0, 0, 0, 0 };
5537 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005538 BitVector InputQuads(4);
5539 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005540 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005541 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005542 MaskVals.push_back(EltIdx);
5543 if (EltIdx < 0) {
5544 ++Quad[0];
5545 ++Quad[1];
5546 ++Quad[2];
5547 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005548 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005549 }
5550 ++Quad[EltIdx / 4];
5551 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005552 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005553
Nate Begemanb9a47b82009-02-23 08:49:38 +00005554 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005555 unsigned MaxQuad = 1;
5556 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005557 if (LoQuad[i] > MaxQuad) {
5558 BestLoQuad = i;
5559 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005560 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005561 }
5562
Nate Begemanb9a47b82009-02-23 08:49:38 +00005563 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005564 MaxQuad = 1;
5565 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005566 if (HiQuad[i] > MaxQuad) {
5567 BestHiQuad = i;
5568 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005569 }
5570 }
5571
Nate Begemanb9a47b82009-02-23 08:49:38 +00005572 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005573 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005574 // single pshufb instruction is necessary. If There are more than 2 input
5575 // quads, disable the next transformation since it does not help SSSE3.
5576 bool V1Used = InputQuads[0] || InputQuads[1];
5577 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperc0d82852011-11-22 00:44:41 +00005578 if (Subtarget->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005579 if (InputQuads.count() == 2 && V1Used && V2Used) {
5580 BestLoQuad = InputQuads.find_first();
5581 BestHiQuad = InputQuads.find_next(BestLoQuad);
5582 }
5583 if (InputQuads.count() > 2) {
5584 BestLoQuad = -1;
5585 BestHiQuad = -1;
5586 }
5587 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005588
Nate Begemanb9a47b82009-02-23 08:49:38 +00005589 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5590 // the shuffle mask. If a quad is scored as -1, that means that it contains
5591 // words from all 4 input quadwords.
5592 SDValue NewV;
5593 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005594 SmallVector<int, 8> MaskV;
5595 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5596 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005597 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005598 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5599 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5600 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005601
Nate Begemanb9a47b82009-02-23 08:49:38 +00005602 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5603 // source words for the shuffle, to aid later transformations.
5604 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005605 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005606 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005607 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005608 if (idx != (int)i)
5609 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005610 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005611 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005612 AllWordsInNewV = false;
5613 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005614 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005615
Nate Begemanb9a47b82009-02-23 08:49:38 +00005616 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5617 if (AllWordsInNewV) {
5618 for (int i = 0; i != 8; ++i) {
5619 int idx = MaskVals[i];
5620 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005621 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005622 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005623 if ((idx != i) && idx < 4)
5624 pshufhw = false;
5625 if ((idx != i) && idx > 3)
5626 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005627 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005628 V1 = NewV;
5629 V2Used = false;
5630 BestLoQuad = 0;
5631 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005632 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005633
Nate Begemanb9a47b82009-02-23 08:49:38 +00005634 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5635 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005636 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005637 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5638 unsigned TargetMask = 0;
5639 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005640 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005641 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5642 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5643 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005644 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005645 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005646 }
Eric Christopherfd179292009-08-27 18:07:15 +00005647
Nate Begemanb9a47b82009-02-23 08:49:38 +00005648 // If we have SSSE3, and all words of the result are from 1 input vector,
5649 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5650 // is present, fall back to case 4.
Craig Topperc0d82852011-11-22 00:44:41 +00005651 if (Subtarget->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005652 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005653
Nate Begemanb9a47b82009-02-23 08:49:38 +00005654 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005655 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005656 // mask, and elements that come from V1 in the V2 mask, so that the two
5657 // results can be OR'd together.
5658 bool TwoInputs = V1Used && V2Used;
5659 for (unsigned i = 0; i != 8; ++i) {
5660 int EltIdx = MaskVals[i] * 2;
5661 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005662 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5663 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005664 continue;
5665 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005666 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5667 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005668 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005669 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005670 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005671 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005672 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005673 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005674 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005675
Nate Begemanb9a47b82009-02-23 08:49:38 +00005676 // Calculate the shuffle mask for the second input, shuffle it, and
5677 // OR it with the first shuffled input.
5678 pshufbMask.clear();
5679 for (unsigned i = 0; i != 8; ++i) {
5680 int EltIdx = MaskVals[i] * 2;
5681 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005682 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5683 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005684 continue;
5685 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005686 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5687 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005688 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005689 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005690 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005691 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005692 MVT::v16i8, &pshufbMask[0], 16));
5693 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005694 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005695 }
5696
5697 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5698 // and update MaskVals with new element order.
5699 BitVector InOrder(8);
5700 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005701 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005702 for (int i = 0; i != 4; ++i) {
5703 int idx = MaskVals[i];
5704 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005705 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005706 InOrder.set(i);
5707 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005708 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005709 InOrder.set(i);
5710 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005711 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005712 }
5713 }
5714 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005715 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005716 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005717 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005718
Craig Topperc0d82852011-11-22 00:44:41 +00005719 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3orAVX())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005720 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5721 NewV.getOperand(0),
5722 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5723 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005724 }
Eric Christopherfd179292009-08-27 18:07:15 +00005725
Nate Begemanb9a47b82009-02-23 08:49:38 +00005726 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5727 // and update MaskVals with the new element order.
5728 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005729 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005730 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005731 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005732 for (unsigned i = 4; i != 8; ++i) {
5733 int idx = MaskVals[i];
5734 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005735 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005736 InOrder.set(i);
5737 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005738 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005739 InOrder.set(i);
5740 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005741 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005742 }
5743 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005744 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005745 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005746
Craig Topperc0d82852011-11-22 00:44:41 +00005747 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3orAVX())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005748 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5749 NewV.getOperand(0),
5750 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5751 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005752 }
Eric Christopherfd179292009-08-27 18:07:15 +00005753
Nate Begemanb9a47b82009-02-23 08:49:38 +00005754 // In case BestHi & BestLo were both -1, which means each quadword has a word
5755 // from each of the four input quadwords, calculate the InOrder bitvector now
5756 // before falling through to the insert/extract cleanup.
5757 if (BestLoQuad == -1 && BestHiQuad == -1) {
5758 NewV = V1;
5759 for (int i = 0; i != 8; ++i)
5760 if (MaskVals[i] < 0 || MaskVals[i] == i)
5761 InOrder.set(i);
5762 }
Eric Christopherfd179292009-08-27 18:07:15 +00005763
Nate Begemanb9a47b82009-02-23 08:49:38 +00005764 // The other elements are put in the right place using pextrw and pinsrw.
5765 for (unsigned i = 0; i != 8; ++i) {
5766 if (InOrder[i])
5767 continue;
5768 int EltIdx = MaskVals[i];
5769 if (EltIdx < 0)
5770 continue;
5771 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005772 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005773 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005774 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005775 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005776 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005777 DAG.getIntPtrConstant(i));
5778 }
5779 return NewV;
5780}
5781
5782// v16i8 shuffles - Prefer shuffles in the following order:
5783// 1. [ssse3] 1 x pshufb
5784// 2. [ssse3] 2 x pshufb + 1 x por
5785// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5786static
Nate Begeman9008ca62009-04-27 18:41:29 +00005787SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005788 SelectionDAG &DAG,
5789 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005790 SDValue V1 = SVOp->getOperand(0);
5791 SDValue V2 = SVOp->getOperand(1);
5792 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005793 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005794 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005795
Nate Begemanb9a47b82009-02-23 08:49:38 +00005796 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005797 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005798 // present, fall back to case 3.
5799 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5800 bool V1Only = true;
5801 bool V2Only = true;
5802 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005803 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005804 if (EltIdx < 0)
5805 continue;
5806 if (EltIdx < 16)
5807 V2Only = false;
5808 else
5809 V1Only = false;
5810 }
Eric Christopherfd179292009-08-27 18:07:15 +00005811
Nate Begemanb9a47b82009-02-23 08:49:38 +00005812 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperc0d82852011-11-22 00:44:41 +00005813 if (TLI.getSubtarget()->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005814 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005815
Nate Begemanb9a47b82009-02-23 08:49:38 +00005816 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005817 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005818 //
5819 // Otherwise, we have elements from both input vectors, and must zero out
5820 // elements that come from V2 in the first mask, and V1 in the second mask
5821 // so that we can OR them together.
5822 bool TwoInputs = !(V1Only || V2Only);
5823 for (unsigned i = 0; i != 16; ++i) {
5824 int EltIdx = MaskVals[i];
5825 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005826 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005827 continue;
5828 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005829 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005830 }
5831 // If all the elements are from V2, assign it to V1 and return after
5832 // building the first pshufb.
5833 if (V2Only)
5834 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005835 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005836 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005837 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005838 if (!TwoInputs)
5839 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005840
Nate Begemanb9a47b82009-02-23 08:49:38 +00005841 // Calculate the shuffle mask for the second input, shuffle it, and
5842 // OR it with the first shuffled input.
5843 pshufbMask.clear();
5844 for (unsigned i = 0; i != 16; ++i) {
5845 int EltIdx = MaskVals[i];
5846 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005847 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005848 continue;
5849 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005850 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005851 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005852 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005853 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005854 MVT::v16i8, &pshufbMask[0], 16));
5855 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005856 }
Eric Christopherfd179292009-08-27 18:07:15 +00005857
Nate Begemanb9a47b82009-02-23 08:49:38 +00005858 // No SSSE3 - Calculate in place words and then fix all out of place words
5859 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5860 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005861 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5862 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005863 SDValue NewV = V2Only ? V2 : V1;
5864 for (int i = 0; i != 8; ++i) {
5865 int Elt0 = MaskVals[i*2];
5866 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005867
Nate Begemanb9a47b82009-02-23 08:49:38 +00005868 // This word of the result is all undef, skip it.
5869 if (Elt0 < 0 && Elt1 < 0)
5870 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005871
Nate Begemanb9a47b82009-02-23 08:49:38 +00005872 // This word of the result is already in the correct place, skip it.
5873 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5874 continue;
5875 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5876 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005877
Nate Begemanb9a47b82009-02-23 08:49:38 +00005878 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5879 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5880 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005881
5882 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5883 // using a single extract together, load it and store it.
5884 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005885 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005886 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005887 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005888 DAG.getIntPtrConstant(i));
5889 continue;
5890 }
5891
Nate Begemanb9a47b82009-02-23 08:49:38 +00005892 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005893 // source byte is not also odd, shift the extracted word left 8 bits
5894 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005895 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005896 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005897 DAG.getIntPtrConstant(Elt1 / 2));
5898 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005899 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005900 DAG.getConstant(8,
5901 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005902 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005903 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5904 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005905 }
5906 // If Elt0 is defined, extract it from the appropriate source. If the
5907 // source byte is not also even, shift the extracted word right 8 bits. If
5908 // Elt1 was also defined, OR the extracted values together before
5909 // inserting them in the result.
5910 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005911 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005912 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5913 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005914 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005915 DAG.getConstant(8,
5916 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005917 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005918 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5919 DAG.getConstant(0x00FF, MVT::i16));
5920 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005921 : InsElt0;
5922 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005923 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005924 DAG.getIntPtrConstant(i));
5925 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005926 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005927}
5928
Evan Cheng7a831ce2007-12-15 03:00:47 +00005929/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005930/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005931/// done when every pair / quad of shuffle mask elements point to elements in
5932/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005933/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005934static
Nate Begeman9008ca62009-04-27 18:41:29 +00005935SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005936 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005937 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005938 SDValue V1 = SVOp->getOperand(0);
5939 SDValue V2 = SVOp->getOperand(1);
5940 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005941 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005942 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005943 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005944 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005945 case MVT::v4f32: NewVT = MVT::v2f64; break;
5946 case MVT::v4i32: NewVT = MVT::v2i64; break;
5947 case MVT::v8i16: NewVT = MVT::v4i32; break;
5948 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005949 }
5950
Nate Begeman9008ca62009-04-27 18:41:29 +00005951 int Scale = NumElems / NewWidth;
5952 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005953 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005954 int StartIdx = -1;
5955 for (int j = 0; j < Scale; ++j) {
5956 int EltIdx = SVOp->getMaskElt(i+j);
5957 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005958 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005959 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005960 StartIdx = EltIdx - (EltIdx % Scale);
5961 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005962 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005963 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005964 if (StartIdx == -1)
5965 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005966 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005967 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005968 }
5969
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005970 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5971 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005972 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005973}
5974
Evan Chengd880b972008-05-09 21:53:03 +00005975/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005976///
Owen Andersone50ed302009-08-10 22:56:29 +00005977static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005978 SDValue SrcOp, SelectionDAG &DAG,
5979 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005980 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005981 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005982 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005983 LD = dyn_cast<LoadSDNode>(SrcOp);
5984 if (!LD) {
5985 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5986 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005987 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005988 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005989 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005990 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005991 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005992 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005993 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005994 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005995 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5996 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5997 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005998 SrcOp.getOperand(0)
5999 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006000 }
6001 }
6002 }
6003
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006004 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006005 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006006 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006007 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006008}
6009
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006010/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
6011/// shuffle node referes to only one lane in the sources.
6012static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
6013 EVT VT = SVOp->getValueType(0);
6014 int NumElems = VT.getVectorNumElements();
6015 int HalfSize = NumElems/2;
6016 SmallVector<int, 16> M;
6017 SVOp->getMask(M);
6018 bool MatchA = false, MatchB = false;
6019
6020 for (int l = 0; l < NumElems*2; l += HalfSize) {
6021 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
6022 MatchA = true;
6023 break;
6024 }
6025 }
6026
6027 for (int l = 0; l < NumElems*2; l += HalfSize) {
6028 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
6029 MatchB = true;
6030 break;
6031 }
6032 }
6033
6034 return MatchA && MatchB;
6035}
6036
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006037/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6038/// which could not be matched by any known target speficic shuffle
6039static SDValue
6040LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006041 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
6042 // If each half of a vector shuffle node referes to only one lane in the
6043 // source vectors, extract each used 128-bit lane and shuffle them using
6044 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
6045 // the work to the legalizer.
6046 DebugLoc dl = SVOp->getDebugLoc();
6047 EVT VT = SVOp->getValueType(0);
6048 int NumElems = VT.getVectorNumElements();
6049 int HalfSize = NumElems/2;
6050
6051 // Extract the reference for each half
6052 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
6053 int FstVecOpNum = 0, SndVecOpNum = 0;
6054 for (int i = 0; i < HalfSize; ++i) {
6055 int Elt = SVOp->getMaskElt(i);
6056 if (SVOp->getMaskElt(i) < 0)
6057 continue;
6058 FstVecOpNum = Elt/NumElems;
6059 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6060 break;
6061 }
6062 for (int i = HalfSize; i < NumElems; ++i) {
6063 int Elt = SVOp->getMaskElt(i);
6064 if (SVOp->getMaskElt(i) < 0)
6065 continue;
6066 SndVecOpNum = Elt/NumElems;
6067 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6068 break;
6069 }
6070
6071 // Extract the subvectors
6072 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
6073 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
6074 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
6075 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
6076
6077 // Generate 128-bit shuffles
6078 SmallVector<int, 16> MaskV1, MaskV2;
6079 for (int i = 0; i < HalfSize; ++i) {
6080 int Elt = SVOp->getMaskElt(i);
6081 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6082 }
6083 for (int i = HalfSize; i < NumElems; ++i) {
6084 int Elt = SVOp->getMaskElt(i);
6085 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6086 }
6087
6088 EVT NVT = V1.getValueType();
6089 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
6090 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
6091
6092 // Concatenate the result back
6093 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
6094 DAG.getConstant(0, MVT::i32), DAG, dl);
6095 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
6096 DAG, dl);
6097 }
6098
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006099 return SDValue();
6100}
6101
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006102/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6103/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006104static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006105LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006106 SDValue V1 = SVOp->getOperand(0);
6107 SDValue V2 = SVOp->getOperand(1);
6108 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006109 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006110
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006111 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6112
Evan Chengace3c172008-07-22 21:13:36 +00006113 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006114 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006115 SmallVector<int, 8> Mask1(4U, -1);
6116 SmallVector<int, 8> PermMask;
6117 SVOp->getMask(PermMask);
6118
Evan Chengace3c172008-07-22 21:13:36 +00006119 unsigned NumHi = 0;
6120 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006121 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006122 int Idx = PermMask[i];
6123 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006124 Locs[i] = std::make_pair(-1, -1);
6125 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006126 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6127 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006128 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006129 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006130 NumLo++;
6131 } else {
6132 Locs[i] = std::make_pair(1, NumHi);
6133 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006134 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006135 NumHi++;
6136 }
6137 }
6138 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006139
Evan Chengace3c172008-07-22 21:13:36 +00006140 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006141 // If no more than two elements come from either vector. This can be
6142 // implemented with two shuffles. First shuffle gather the elements.
6143 // The second shuffle, which takes the first shuffle as both of its
6144 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006145 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006146
Nate Begeman9008ca62009-04-27 18:41:29 +00006147 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006148
Evan Chengace3c172008-07-22 21:13:36 +00006149 for (unsigned i = 0; i != 4; ++i) {
6150 if (Locs[i].first == -1)
6151 continue;
6152 else {
6153 unsigned Idx = (i < 2) ? 0 : 4;
6154 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006155 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006156 }
6157 }
6158
Nate Begeman9008ca62009-04-27 18:41:29 +00006159 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006160 } else if (NumLo == 3 || NumHi == 3) {
6161 // Otherwise, we must have three elements from one vector, call it X, and
6162 // one element from the other, call it Y. First, use a shufps to build an
6163 // intermediate vector with the one element from Y and the element from X
6164 // that will be in the same half in the final destination (the indexes don't
6165 // matter). Then, use a shufps to build the final vector, taking the half
6166 // containing the element from Y from the intermediate, and the other half
6167 // from X.
6168 if (NumHi == 3) {
6169 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006170 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006171 std::swap(V1, V2);
6172 }
6173
6174 // Find the element from V2.
6175 unsigned HiIndex;
6176 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006177 int Val = PermMask[HiIndex];
6178 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006179 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006180 if (Val >= 4)
6181 break;
6182 }
6183
Nate Begeman9008ca62009-04-27 18:41:29 +00006184 Mask1[0] = PermMask[HiIndex];
6185 Mask1[1] = -1;
6186 Mask1[2] = PermMask[HiIndex^1];
6187 Mask1[3] = -1;
6188 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006189
6190 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006191 Mask1[0] = PermMask[0];
6192 Mask1[1] = PermMask[1];
6193 Mask1[2] = HiIndex & 1 ? 6 : 4;
6194 Mask1[3] = HiIndex & 1 ? 4 : 6;
6195 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006196 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006197 Mask1[0] = HiIndex & 1 ? 2 : 0;
6198 Mask1[1] = HiIndex & 1 ? 0 : 2;
6199 Mask1[2] = PermMask[2];
6200 Mask1[3] = PermMask[3];
6201 if (Mask1[2] >= 0)
6202 Mask1[2] += 4;
6203 if (Mask1[3] >= 0)
6204 Mask1[3] += 4;
6205 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006206 }
Evan Chengace3c172008-07-22 21:13:36 +00006207 }
6208
6209 // Break it into (shuffle shuffle_hi, shuffle_lo).
6210 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006211 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006212 SmallVector<int,8> LoMask(4U, -1);
6213 SmallVector<int,8> HiMask(4U, -1);
6214
6215 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006216 unsigned MaskIdx = 0;
6217 unsigned LoIdx = 0;
6218 unsigned HiIdx = 2;
6219 for (unsigned i = 0; i != 4; ++i) {
6220 if (i == 2) {
6221 MaskPtr = &HiMask;
6222 MaskIdx = 1;
6223 LoIdx = 0;
6224 HiIdx = 2;
6225 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006226 int Idx = PermMask[i];
6227 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006228 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006229 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006230 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006231 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006232 LoIdx++;
6233 } else {
6234 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006235 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006236 HiIdx++;
6237 }
6238 }
6239
Nate Begeman9008ca62009-04-27 18:41:29 +00006240 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6241 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6242 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006243 for (unsigned i = 0; i != 4; ++i) {
6244 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006245 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006246 } else {
6247 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006248 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006249 }
6250 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006251 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006252}
6253
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006254static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006255 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006256 V = V.getOperand(0);
6257 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6258 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006259 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6260 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6261 // BUILD_VECTOR (load), undef
6262 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006263 if (MayFoldLoad(V))
6264 return true;
6265 return false;
6266}
6267
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006268// FIXME: the version above should always be used. Since there's
6269// a bug where several vector shuffles can't be folded because the
6270// DAG is not updated during lowering and a node claims to have two
6271// uses while it only has one, use this version, and let isel match
6272// another instruction if the load really happens to have more than
6273// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006274// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006275static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006276 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006277 V = V.getOperand(0);
6278 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6279 V = V.getOperand(0);
6280 if (ISD::isNormalLoad(V.getNode()))
6281 return true;
6282 return false;
6283}
6284
6285/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6286/// a vector extract, and if both can be later optimized into a single load.
6287/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6288/// here because otherwise a target specific shuffle node is going to be
6289/// emitted for this shuffle, and the optimization not done.
6290/// FIXME: This is probably not the best approach, but fix the problem
6291/// until the right path is decided.
6292static
6293bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6294 const TargetLowering &TLI) {
6295 EVT VT = V.getValueType();
6296 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6297
6298 // Be sure that the vector shuffle is present in a pattern like this:
6299 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6300 if (!V.hasOneUse())
6301 return false;
6302
6303 SDNode *N = *V.getNode()->use_begin();
6304 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6305 return false;
6306
6307 SDValue EltNo = N->getOperand(1);
6308 if (!isa<ConstantSDNode>(EltNo))
6309 return false;
6310
6311 // If the bit convert changed the number of elements, it is unsafe
6312 // to examine the mask.
6313 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006314 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006315 EVT SrcVT = V.getOperand(0).getValueType();
6316 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6317 return false;
6318 V = V.getOperand(0);
6319 HasShuffleIntoBitcast = true;
6320 }
6321
6322 // Select the input vector, guarding against out of range extract vector.
6323 unsigned NumElems = VT.getVectorNumElements();
6324 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6325 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6326 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6327
6328 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006329 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006330 V = V.getOperand(0);
6331
6332 if (ISD::isNormalLoad(V.getNode())) {
6333 // Is the original load suitable?
6334 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6335
6336 // FIXME: avoid the multi-use bug that is preventing lots of
6337 // of foldings to be detected, this is still wrong of course, but
6338 // give the temporary desired behavior, and if it happens that
6339 // the load has real more uses, during isel it will not fold, and
6340 // will generate poor code.
6341 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6342 return false;
6343
6344 if (!HasShuffleIntoBitcast)
6345 return true;
6346
6347 // If there's a bitcast before the shuffle, check if the load type and
6348 // alignment is valid.
6349 unsigned Align = LN0->getAlignment();
6350 unsigned NewAlign =
6351 TLI.getTargetData()->getABITypeAlignment(
6352 VT.getTypeForEVT(*DAG.getContext()));
6353
6354 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6355 return false;
6356 }
6357
6358 return true;
6359}
6360
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006361static
Evan Cheng835580f2010-10-07 20:50:20 +00006362SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6363 EVT VT = Op.getValueType();
6364
6365 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006366 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6367 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006368 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6369 V1, DAG));
6370}
6371
6372static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006373SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006374 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006375 SDValue V1 = Op.getOperand(0);
6376 SDValue V2 = Op.getOperand(1);
6377 EVT VT = Op.getValueType();
6378
6379 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6380
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006381 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006382 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6383
Evan Cheng0899f5c2011-08-31 02:05:24 +00006384 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6385 return DAG.getNode(ISD::BITCAST, dl, VT,
6386 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6387 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6388 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006389}
6390
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006391static
6392SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6393 SDValue V1 = Op.getOperand(0);
6394 SDValue V2 = Op.getOperand(1);
6395 EVT VT = Op.getValueType();
6396
6397 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6398 "unsupported shuffle type");
6399
6400 if (V2.getOpcode() == ISD::UNDEF)
6401 V2 = V1;
6402
6403 // v4i32 or v4f32
6404 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6405}
6406
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006407static inline unsigned getSHUFPOpcode(EVT VT) {
6408 switch(VT.getSimpleVT().SimpleTy) {
6409 case MVT::v8i32: // Use fp unit for int unpack.
6410 case MVT::v8f32:
6411 case MVT::v4i32: // Use fp unit for int unpack.
6412 case MVT::v4f32: return X86ISD::SHUFPS;
6413 case MVT::v4i64: // Use fp unit for int unpack.
6414 case MVT::v4f64:
6415 case MVT::v2i64: // Use fp unit for int unpack.
6416 case MVT::v2f64: return X86ISD::SHUFPD;
6417 default:
6418 llvm_unreachable("Unknown type for shufp*");
6419 }
6420 return 0;
6421}
6422
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006423static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006424SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006425 SDValue V1 = Op.getOperand(0);
6426 SDValue V2 = Op.getOperand(1);
6427 EVT VT = Op.getValueType();
6428 unsigned NumElems = VT.getVectorNumElements();
6429
6430 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6431 // operand of these instructions is only memory, so check if there's a
6432 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6433 // same masks.
6434 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006435
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006436 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006437 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006438 CanFoldLoad = true;
6439
6440 // When V1 is a load, it can be folded later into a store in isel, example:
6441 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6442 // turns into:
6443 // (MOVLPSmr addr:$src1, VR128:$src2)
6444 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006445 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006446 CanFoldLoad = true;
6447
Dan Gohman65fd6562011-11-03 21:49:52 +00006448 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006449 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006450 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006451 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6452
6453 if (NumElems == 4)
Dan Gohman65fd6562011-11-03 21:49:52 +00006454 // If we don't care about the second element, procede to use movss.
6455 if (SVOp->getMaskElt(1) != -1)
6456 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006457 }
6458
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006459 // movl and movlp will both match v2i64, but v2i64 is never matched by
6460 // movl earlier because we make it strict to avoid messing with the movlp load
6461 // folding logic (see the code above getMOVLP call). Match it here then,
6462 // this is horrible, but will stay like this until we move all shuffle
6463 // matching to x86 specific nodes. Note that for the 1st condition all
6464 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006465 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006466 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6467 // as to remove this logic from here, as much as possible
6468 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006469 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006470 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006471 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006472
6473 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6474
6475 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006476 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006477 X86::getShuffleSHUFImmediate(SVOp), DAG);
6478}
6479
Craig Topper6347e862011-11-21 06:57:39 +00006480static inline unsigned getUNPCKLOpcode(EVT VT, bool HasAVX2) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006481 switch(VT.getSimpleVT().SimpleTy) {
Craig Topper06cb6802011-11-26 20:47:44 +00006482 case MVT::v32i8:
6483 case MVT::v16i8:
6484 case MVT::v16i16:
6485 case MVT::v8i16:
6486 case MVT::v4i32:
6487 case MVT::v2i64: return X86ISD::PUNPCKL;
Craig Topper6347e862011-11-21 06:57:39 +00006488 case MVT::v8i32:
Craig Topper06cb6802011-11-26 20:47:44 +00006489 case MVT::v4i64:
6490 if (HasAVX2) return X86ISD::PUNPCKL;
Craig Topper6347e862011-11-21 06:57:39 +00006491 // else use fp unit for int unpack.
Craig Topper705f2432011-11-24 22:57:10 +00006492 case MVT::v8f32:
Craig Topper06cb6802011-11-26 20:47:44 +00006493 case MVT::v4f32:
Craig Topper705f2432011-11-24 22:57:10 +00006494 case MVT::v4f64:
Craig Topper06cb6802011-11-26 20:47:44 +00006495 case MVT::v2f64: return X86ISD::UNPCKLP;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006496 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006497 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006498 }
6499 return 0;
6500}
6501
Craig Topper6347e862011-11-21 06:57:39 +00006502static inline unsigned getUNPCKHOpcode(EVT VT, bool HasAVX2) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006503 switch(VT.getSimpleVT().SimpleTy) {
Craig Topper06cb6802011-11-26 20:47:44 +00006504 case MVT::v32i8:
6505 case MVT::v16i8:
6506 case MVT::v16i16:
6507 case MVT::v8i16:
6508 case MVT::v4i32:
6509 case MVT::v2i64: return X86ISD::PUNPCKH;
6510 case MVT::v4i64:
Craig Topper6347e862011-11-21 06:57:39 +00006511 case MVT::v8i32:
Craig Topper06cb6802011-11-26 20:47:44 +00006512 if (HasAVX2) return X86ISD::PUNPCKH;
Craig Topper6347e862011-11-21 06:57:39 +00006513 // else use fp unit for int unpack.
Craig Topper705f2432011-11-24 22:57:10 +00006514 case MVT::v8f32:
Craig Topper06cb6802011-11-26 20:47:44 +00006515 case MVT::v4f32:
Craig Topper705f2432011-11-24 22:57:10 +00006516 case MVT::v4f64:
Craig Topper06cb6802011-11-26 20:47:44 +00006517 case MVT::v2f64: return X86ISD::UNPCKHP;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006518 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006519 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006520 }
6521 return 0;
6522}
6523
Craig Topper70b883b2011-11-28 10:14:51 +00006524static inline unsigned getVPERM2X128Opcode(EVT VT, bool HasAVX2) {
6525 switch(VT.getSimpleVT().SimpleTy) {
6526 case MVT::v32i8:
6527 case MVT::v16i16:
6528 case MVT::v8i32:
6529 case MVT::v4i64:
6530 if (HasAVX2) return X86ISD::VPERM2I128;
6531 // else use fp unit for int vperm
6532 case MVT::v8f32:
6533 case MVT::v4f64: return X86ISD::VPERM2F128;
6534 default:
6535 llvm_unreachable("Unknown type for vpermil");
6536 }
6537 return 0;
6538}
6539
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006540static
6541SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006542 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006543 const X86Subtarget *Subtarget) {
6544 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6545 EVT VT = Op.getValueType();
6546 DebugLoc dl = Op.getDebugLoc();
6547 SDValue V1 = Op.getOperand(0);
6548 SDValue V2 = Op.getOperand(1);
6549
6550 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006551 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006552
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006553 // Handle splat operations
6554 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006555 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006556 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006557 // Special case, this is the only place now where it's allowed to return
6558 // a vector_shuffle operation without using a target specific node, because
6559 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6560 // this be moved to DAGCombine instead?
6561 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006562 return Op;
6563
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006564 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00006565 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00006566 if (Subtarget->hasAVX() && LD.getNode())
6567 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006568
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006569 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006570 if ((Size == 128 && NumElem <= 4) ||
6571 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006572 return SDValue();
6573
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006574 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006575 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006576 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006577
6578 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6579 // do it!
6580 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6581 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6582 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006583 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006584 } else if ((VT == MVT::v4i32 ||
6585 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006586 // FIXME: Figure out a cleaner way to do this.
6587 // Try to make use of movq to zero out the top part.
6588 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6589 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6590 if (NewOp.getNode()) {
6591 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6592 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6593 DAG, Subtarget, dl);
6594 }
6595 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6596 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6597 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6598 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6599 DAG, Subtarget, dl);
6600 }
6601 }
6602 return SDValue();
6603}
6604
Dan Gohman475871a2008-07-27 21:46:04 +00006605SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006606X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006607 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006608 SDValue V1 = Op.getOperand(0);
6609 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006610 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006611 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006612 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006613 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006614 bool V1IsSplat = false;
6615 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006616 bool HasXMMInt = Subtarget->hasXMMInt();
Craig Topper6347e862011-11-21 06:57:39 +00006617 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006618 MachineFunction &MF = DAG.getMachineFunction();
6619 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006620
Craig Topper3426a3e2011-11-14 06:46:21 +00006621 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006622
Craig Topper38034c52011-11-26 22:55:48 +00006623 assert(V1.getOpcode() != ISD::UNDEF && "Op 1 of shuffle should not be undef");
6624
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006625 // Vector shuffle lowering takes 3 steps:
6626 //
6627 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6628 // narrowing and commutation of operands should be handled.
6629 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6630 // shuffle nodes.
6631 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6632 // so the shuffle can be broken into other shuffles and the legalizer can
6633 // try the lowering again.
6634 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006635 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006636 // be matched during isel, all of them must be converted to a target specific
6637 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006638
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006639 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6640 // narrowing and commutation of operands should be handled. The actual code
6641 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006642 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006643 if (NewOp.getNode())
6644 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006645
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006646 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6647 // unpckh_undef). Only use pshufd if speed is more important than size.
6648 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006649 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V1,
6650 DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006651 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006652 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6653 DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006654
Craig Topperc0d82852011-11-22 00:44:41 +00006655 if (X86::isMOVDDUPMask(SVOp) && Subtarget->hasSSE3orAVX() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006656 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006657 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006658
Dale Johannesen0488fb62010-09-30 23:57:10 +00006659 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006660 return getMOVHighToLow(Op, dl, DAG);
6661
6662 // Use to match splats
Craig Topperc0d82852011-11-22 00:44:41 +00006663 if (HasXMMInt && X86::isUNPCKHMask(SVOp, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006664 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper6347e862011-11-21 06:57:39 +00006665 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6666 DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006667
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006668 if (X86::isPSHUFDMask(SVOp)) {
6669 // The actual implementation will match the mask in the if above and then
6670 // during isel it can match several different instructions, not only pshufd
6671 // as its name says, sad but true, emulate the behavior for now...
6672 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6673 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6674
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006675 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6676
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006677 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006678 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6679
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006680 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6681 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006682 }
Eric Christopherfd179292009-08-27 18:07:15 +00006683
Evan Chengf26ffe92008-05-29 08:22:04 +00006684 // Check if this can be converted into a logical shift.
6685 bool isLeft = false;
6686 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006687 SDValue ShVal;
Craig Topperc0d82852011-11-22 00:44:41 +00006688 bool isShift = HasXMMInt && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006689 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006690 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006691 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006692 EVT EltVT = VT.getVectorElementType();
6693 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006694 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006695 }
Eric Christopherfd179292009-08-27 18:07:15 +00006696
Nate Begeman9008ca62009-04-27 18:41:29 +00006697 if (X86::isMOVLMask(SVOp)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006698 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006699 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006700 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006701 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006702 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6703
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006704 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006705 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6706 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006707 }
Eric Christopherfd179292009-08-27 18:07:15 +00006708
Nate Begeman9008ca62009-04-27 18:41:29 +00006709 // FIXME: fold these into legal mask.
Craig Topperc0d82852011-11-22 00:44:41 +00006710 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp, HasAVX2))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006711 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006712
Dale Johannesen0488fb62010-09-30 23:57:10 +00006713 if (X86::isMOVHLPSMask(SVOp))
6714 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006715
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006716 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006717 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006718
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006719 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006720 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006721
Dale Johannesen0488fb62010-09-30 23:57:10 +00006722 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006723 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006724
Nate Begeman9008ca62009-04-27 18:41:29 +00006725 if (ShouldXformToMOVHLPS(SVOp) ||
6726 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6727 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006728
Evan Chengf26ffe92008-05-29 08:22:04 +00006729 if (isShift) {
6730 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006731 EVT EltVT = VT.getVectorElementType();
6732 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006733 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006734 }
Eric Christopherfd179292009-08-27 18:07:15 +00006735
Evan Cheng9eca5e82006-10-25 21:49:50 +00006736 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006737 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6738 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006739 V1IsSplat = isSplatVector(V1.getNode());
6740 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006741
Chris Lattner8a594482007-11-25 00:24:49 +00006742 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper38034c52011-11-26 22:55:48 +00006743 if (V1IsSplat && !V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006744 Op = CommuteVectorShuffle(SVOp, DAG);
6745 SVOp = cast<ShuffleVectorSDNode>(Op);
6746 V1 = SVOp->getOperand(0);
6747 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006748 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006749 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006750 }
6751
Nate Begeman9008ca62009-04-27 18:41:29 +00006752 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6753 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006754 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006755 return V1;
6756 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6757 // the instruction selector will not match, so get a canonical MOVL with
6758 // swapped operands to undo the commute.
6759 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006760 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006761
Craig Topperc0d82852011-11-22 00:44:41 +00006762 if (X86::isUNPCKLMask(SVOp, HasAVX2))
Craig Topper6347e862011-11-21 06:57:39 +00006763 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V2,
6764 DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006765
Craig Topperc0d82852011-11-22 00:44:41 +00006766 if (X86::isUNPCKHMask(SVOp, HasAVX2))
Craig Topper6347e862011-11-21 06:57:39 +00006767 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V2,
6768 DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006769
Evan Cheng9bbbb982006-10-25 20:48:19 +00006770 if (V2IsSplat) {
6771 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006772 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006773 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006774 SDValue NewMask = NormalizeMask(SVOp, DAG);
6775 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6776 if (NSVOp != SVOp) {
Craig Topperc0d82852011-11-22 00:44:41 +00006777 if (X86::isUNPCKLMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006778 return NewMask;
Craig Topperc0d82852011-11-22 00:44:41 +00006779 } else if (X86::isUNPCKHMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006780 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006781 }
6782 }
6783 }
6784
Evan Cheng9eca5e82006-10-25 21:49:50 +00006785 if (Commuted) {
6786 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006787 // FIXME: this seems wrong.
6788 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6789 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006790
Craig Topperc0d82852011-11-22 00:44:41 +00006791 if (X86::isUNPCKLMask(NewSVOp, HasAVX2))
Craig Topper6347e862011-11-21 06:57:39 +00006792 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V2, V1,
6793 DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006794
Craig Topperc0d82852011-11-22 00:44:41 +00006795 if (X86::isUNPCKHMask(NewSVOp, HasAVX2))
Craig Topper6347e862011-11-21 06:57:39 +00006796 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V2, V1,
6797 DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006798 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006799
Nate Begeman9008ca62009-04-27 18:41:29 +00006800 // Normalize the node to match x86 shuffle ops if needed
Craig Topper71c4c122011-11-28 01:14:24 +00006801 if (!V2IsUndef && (isCommutedSHUFP(SVOp) ||
6802 isCommutedVSHUFPY(SVOp, Subtarget->hasAVX())))
Nate Begeman9008ca62009-04-27 18:41:29 +00006803 return CommuteVectorShuffle(SVOp, DAG);
6804
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006805 // The checks below are all present in isShuffleMaskLegal, but they are
6806 // inlined here right now to enable us to directly emit target specific
6807 // nodes, and remove one by one until they don't return Op anymore.
6808 SmallVector<int, 16> M;
6809 SVOp->getMask(M);
6810
Craig Topperc0d82852011-11-22 00:44:41 +00006811 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006812 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6813 X86::getShufflePALIGNRImmediate(SVOp),
6814 DAG);
6815
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006816 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6817 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006818 if (VT == MVT::v2f64)
Craig Topper06cb6802011-11-26 20:47:44 +00006819 return getTargetShuffleNode(X86ISD::UNPCKLP, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006820 if (VT == MVT::v2i64)
Craig Topper06cb6802011-11-26 20:47:44 +00006821 return getTargetShuffleNode(X86ISD::PUNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006822 }
6823
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006824 if (isPSHUFHWMask(M, VT))
6825 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6826 X86::getShufflePSHUFHWImmediate(SVOp),
6827 DAG);
6828
6829 if (isPSHUFLWMask(M, VT))
6830 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6831 X86::getShufflePSHUFLWImmediate(SVOp),
6832 DAG);
6833
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006834 if (isSHUFPMask(M, VT))
6835 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6836 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006837
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006838 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006839 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V1,
6840 DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006841 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006842 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6843 DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006844
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006845 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006846 // Generate target specific nodes for 128 or 256-bit shuffles only
6847 // supported in the AVX instruction set.
6848 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006849
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006850 // Handle VMOVDDUPY permutations
6851 if (isMOVDDUPYMask(SVOp, Subtarget))
6852 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6853
Craig Topper70b883b2011-11-28 10:14:51 +00006854 // Handle VPERMILPS/D* permutations
6855 if (isVPERMILPMask(M, VT, Subtarget->hasAVX()))
Craig Topper316cd2a2011-11-30 06:25:25 +00006856 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006857 getShuffleVPERMILPImmediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006858
Craig Topper70b883b2011-11-28 10:14:51 +00006859 // Handle VPERM2F128/VPERM2I128 permutations
6860 if (isVPERM2X128Mask(M, VT, Subtarget->hasAVX()))
6861 return getTargetShuffleNode(getVPERM2X128Opcode(VT, HasAVX2), dl, VT, V1,
6862 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006863
Craig Topper70b883b2011-11-28 10:14:51 +00006864 // Handle VSHUFPS/DY permutations
Craig Topper71c4c122011-11-28 01:14:24 +00006865 if (isVSHUFPYMask(M, VT, Subtarget->hasAVX()))
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006866 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
Craig Topper9d7025b2011-11-27 21:41:12 +00006867 getShuffleVSHUFPYImmediate(SVOp), DAG);
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006868
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006869 //===--------------------------------------------------------------------===//
6870 // Since no target specific shuffle was selected for this generic one,
6871 // lower it into other known shuffles. FIXME: this isn't true yet, but
6872 // this is the plan.
6873 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006874
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006875 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6876 if (VT == MVT::v8i16) {
6877 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6878 if (NewOp.getNode())
6879 return NewOp;
6880 }
6881
6882 if (VT == MVT::v16i8) {
6883 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6884 if (NewOp.getNode())
6885 return NewOp;
6886 }
6887
6888 // Handle all 128-bit wide vectors with 4 elements, and match them with
6889 // several different shuffle types.
6890 if (NumElems == 4 && VT.getSizeInBits() == 128)
6891 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6892
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006893 // Handle general 256-bit shuffles
6894 if (VT.is256BitVector())
6895 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6896
Dan Gohman475871a2008-07-27 21:46:04 +00006897 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006898}
6899
Dan Gohman475871a2008-07-27 21:46:04 +00006900SDValue
6901X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006902 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006903 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006904 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006905
6906 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6907 return SDValue();
6908
Duncan Sands83ec4b62008-06-06 12:08:01 +00006909 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006910 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006911 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006912 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006913 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006914 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006915 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006916 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6917 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6918 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006919 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6920 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006921 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006922 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006923 Op.getOperand(0)),
6924 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006925 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006926 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006927 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006928 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006929 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006930 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006931 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6932 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006933 // result has a single use which is a store or a bitcast to i32. And in
6934 // the case of a store, it's not worth it if the index is a constant 0,
6935 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006936 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006937 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006938 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006939 if ((User->getOpcode() != ISD::STORE ||
6940 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6941 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006942 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006943 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006944 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006945 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006946 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006947 Op.getOperand(0)),
6948 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006949 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Pete Coopera77214a2011-11-14 19:38:42 +00006950 } else if (VT == MVT::i32 || VT == MVT::i64) {
6951 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006952 if (isa<ConstantSDNode>(Op.getOperand(1)))
6953 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006954 }
Dan Gohman475871a2008-07-27 21:46:04 +00006955 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006956}
6957
6958
Dan Gohman475871a2008-07-27 21:46:04 +00006959SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006960X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6961 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006962 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006963 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006964
David Greene74a579d2011-02-10 16:57:36 +00006965 SDValue Vec = Op.getOperand(0);
6966 EVT VecVT = Vec.getValueType();
6967
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006968 // If this is a 256-bit vector result, first extract the 128-bit vector and
6969 // then extract the element from the 128-bit vector.
6970 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006971 DebugLoc dl = Op.getNode()->getDebugLoc();
6972 unsigned NumElems = VecVT.getVectorNumElements();
6973 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006974 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6975
6976 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006977 bool Upper = IdxVal >= NumElems/2;
6978 Vec = Extract128BitVector(Vec,
6979 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006980
David Greene74a579d2011-02-10 16:57:36 +00006981 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006982 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006983 }
6984
6985 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6986
Craig Topperc0d82852011-11-22 00:44:41 +00006987 if (Subtarget->hasSSE41orAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006988 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006989 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006990 return Res;
6991 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006992
Owen Andersone50ed302009-08-10 22:56:29 +00006993 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006994 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006995 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006996 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006997 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006998 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006999 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007000 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7001 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007002 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007003 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007004 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007005 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007006 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007007 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007008 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007009 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007010 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007011 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007012 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007013 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007014 if (Idx == 0)
7015 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007016
Evan Cheng0db9fe62006-04-25 20:13:52 +00007017 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007018 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007019 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007020 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007021 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007022 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007023 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00007024 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007025 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7026 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7027 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007028 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007029 if (Idx == 0)
7030 return Op;
7031
7032 // UNPCKHPD the element to the lowest double word, then movsd.
7033 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7034 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007035 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007036 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007037 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007038 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007039 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007040 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007041 }
7042
Dan Gohman475871a2008-07-27 21:46:04 +00007043 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007044}
7045
Dan Gohman475871a2008-07-27 21:46:04 +00007046SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007047X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7048 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007049 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007050 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007051 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007052
Dan Gohman475871a2008-07-27 21:46:04 +00007053 SDValue N0 = Op.getOperand(0);
7054 SDValue N1 = Op.getOperand(1);
7055 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007056
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007057 if (VT.getSizeInBits() == 256)
7058 return SDValue();
7059
Dan Gohman8a55ce42009-09-23 21:02:20 +00007060 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007061 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007062 unsigned Opc;
7063 if (VT == MVT::v8i16)
7064 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007065 else if (VT == MVT::v16i8)
7066 Opc = X86ISD::PINSRB;
7067 else
7068 Opc = X86ISD::PINSRB;
7069
Nate Begeman14d12ca2008-02-11 04:19:36 +00007070 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7071 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007072 if (N1.getValueType() != MVT::i32)
7073 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7074 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007075 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007076 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007077 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007078 // Bits [7:6] of the constant are the source select. This will always be
7079 // zero here. The DAG Combiner may combine an extract_elt index into these
7080 // bits. For example (insert (extract, 3), 2) could be matched by putting
7081 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007082 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007083 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007084 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007085 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007086 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007087 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007088 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007089 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Pete Coopera77214a2011-11-14 19:38:42 +00007090 } else if ((EltVT == MVT::i32 || EltVT == MVT::i64) &&
7091 isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007092 // PINSR* works with constant index.
7093 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007094 }
Dan Gohman475871a2008-07-27 21:46:04 +00007095 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007096}
7097
Dan Gohman475871a2008-07-27 21:46:04 +00007098SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007099X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007100 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007101 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007102
David Greene6b381262011-02-09 15:32:06 +00007103 DebugLoc dl = Op.getDebugLoc();
7104 SDValue N0 = Op.getOperand(0);
7105 SDValue N1 = Op.getOperand(1);
7106 SDValue N2 = Op.getOperand(2);
7107
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007108 // If this is a 256-bit vector result, first extract the 128-bit vector,
7109 // insert the element into the extracted half and then place it back.
7110 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007111 if (!isa<ConstantSDNode>(N2))
7112 return SDValue();
7113
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007114 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007115 unsigned NumElems = VT.getVectorNumElements();
7116 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007117 bool Upper = IdxVal >= NumElems/2;
7118 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7119 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007120
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007121 // Insert the element into the desired half.
7122 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7123 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007124
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007125 // Insert the changed part back to the 256-bit vector
7126 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007127 }
7128
Craig Topperc0d82852011-11-22 00:44:41 +00007129 if (Subtarget->hasSSE41orAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007130 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7131
Dan Gohman8a55ce42009-09-23 21:02:20 +00007132 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007133 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007134
Dan Gohman8a55ce42009-09-23 21:02:20 +00007135 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007136 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7137 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007138 if (N1.getValueType() != MVT::i32)
7139 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7140 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007141 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007142 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007143 }
Dan Gohman475871a2008-07-27 21:46:04 +00007144 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007145}
7146
Dan Gohman475871a2008-07-27 21:46:04 +00007147SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007148X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007149 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007150 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007151 EVT OpVT = Op.getValueType();
7152
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007153 // If this is a 256-bit vector result, first insert into a 128-bit
7154 // vector and then insert into the 256-bit vector.
7155 if (OpVT.getSizeInBits() > 128) {
7156 // Insert into a 128-bit vector.
7157 EVT VT128 = EVT::getVectorVT(*Context,
7158 OpVT.getVectorElementType(),
7159 OpVT.getVectorNumElements() / 2);
7160
7161 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7162
7163 // Insert the 128-bit vector.
7164 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7165 DAG.getConstant(0, MVT::i32),
7166 DAG, dl);
7167 }
7168
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007169 if (Op.getValueType() == MVT::v1i64 &&
7170 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007171 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007172
Owen Anderson825b72b2009-08-11 20:47:22 +00007173 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007174 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7175 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007176 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007177 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007178}
7179
David Greene91585092011-01-26 15:38:49 +00007180// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7181// a simple subregister reference or explicit instructions to grab
7182// upper bits of a vector.
7183SDValue
7184X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7185 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007186 DebugLoc dl = Op.getNode()->getDebugLoc();
7187 SDValue Vec = Op.getNode()->getOperand(0);
7188 SDValue Idx = Op.getNode()->getOperand(1);
7189
7190 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7191 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7192 return Extract128BitVector(Vec, Idx, DAG, dl);
7193 }
David Greene91585092011-01-26 15:38:49 +00007194 }
7195 return SDValue();
7196}
7197
David Greenecfe33c42011-01-26 19:13:22 +00007198// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7199// simple superregister reference or explicit instructions to insert
7200// the upper bits of a vector.
7201SDValue
7202X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7203 if (Subtarget->hasAVX()) {
7204 DebugLoc dl = Op.getNode()->getDebugLoc();
7205 SDValue Vec = Op.getNode()->getOperand(0);
7206 SDValue SubVec = Op.getNode()->getOperand(1);
7207 SDValue Idx = Op.getNode()->getOperand(2);
7208
7209 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7210 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007211 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007212 }
7213 }
7214 return SDValue();
7215}
7216
Bill Wendling056292f2008-09-16 21:48:12 +00007217// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7218// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7219// one of the above mentioned nodes. It has to be wrapped because otherwise
7220// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7221// be used to form addressing mode. These wrapped nodes will be selected
7222// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007223SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007224X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007225 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007226
Chris Lattner41621a22009-06-26 19:22:52 +00007227 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7228 // global base reg.
7229 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007230 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007231 CodeModel::Model M = getTargetMachine().getCodeModel();
7232
Chris Lattner4f066492009-07-11 20:29:19 +00007233 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007234 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007235 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007236 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007237 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007238 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007239 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007240
Evan Cheng1606e8e2009-03-13 07:51:59 +00007241 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007242 CP->getAlignment(),
7243 CP->getOffset(), OpFlag);
7244 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007245 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007246 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007247 if (OpFlag) {
7248 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007249 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007250 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007251 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007252 }
7253
7254 return Result;
7255}
7256
Dan Gohmand858e902010-04-17 15:26:15 +00007257SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007258 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007259
Chris Lattner18c59872009-06-27 04:16:01 +00007260 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7261 // global base reg.
7262 unsigned char OpFlag = 0;
7263 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007264 CodeModel::Model M = getTargetMachine().getCodeModel();
7265
Chris Lattner4f066492009-07-11 20:29:19 +00007266 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007267 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007268 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007269 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007270 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007271 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007272 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007273
Chris Lattner18c59872009-06-27 04:16:01 +00007274 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7275 OpFlag);
7276 DebugLoc DL = JT->getDebugLoc();
7277 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007278
Chris Lattner18c59872009-06-27 04:16:01 +00007279 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007280 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007281 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7282 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007283 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007284 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007285
Chris Lattner18c59872009-06-27 04:16:01 +00007286 return Result;
7287}
7288
7289SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007290X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007291 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007292
Chris Lattner18c59872009-06-27 04:16:01 +00007293 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7294 // global base reg.
7295 unsigned char OpFlag = 0;
7296 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007297 CodeModel::Model M = getTargetMachine().getCodeModel();
7298
Chris Lattner4f066492009-07-11 20:29:19 +00007299 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007300 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7301 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7302 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007303 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007304 } else if (Subtarget->isPICStyleGOT()) {
7305 OpFlag = X86II::MO_GOT;
7306 } else if (Subtarget->isPICStyleStubPIC()) {
7307 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7308 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7309 OpFlag = X86II::MO_DARWIN_NONLAZY;
7310 }
Eric Christopherfd179292009-08-27 18:07:15 +00007311
Chris Lattner18c59872009-06-27 04:16:01 +00007312 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007313
Chris Lattner18c59872009-06-27 04:16:01 +00007314 DebugLoc DL = Op.getDebugLoc();
7315 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007316
7317
Chris Lattner18c59872009-06-27 04:16:01 +00007318 // With PIC, the address is actually $g + Offset.
7319 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007320 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007321 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7322 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007323 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007324 Result);
7325 }
Eric Christopherfd179292009-08-27 18:07:15 +00007326
Eli Friedman586272d2011-08-11 01:48:05 +00007327 // For symbols that require a load from a stub to get the address, emit the
7328 // load.
7329 if (isGlobalStubReference(OpFlag))
7330 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007331 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007332
Chris Lattner18c59872009-06-27 04:16:01 +00007333 return Result;
7334}
7335
Dan Gohman475871a2008-07-27 21:46:04 +00007336SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007337X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007338 // Create the TargetBlockAddressAddress node.
7339 unsigned char OpFlags =
7340 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007341 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007342 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007343 DebugLoc dl = Op.getDebugLoc();
7344 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7345 /*isTarget=*/true, OpFlags);
7346
Dan Gohmanf705adb2009-10-30 01:28:02 +00007347 if (Subtarget->isPICStyleRIPRel() &&
7348 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007349 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7350 else
7351 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007352
Dan Gohman29cbade2009-11-20 23:18:13 +00007353 // With PIC, the address is actually $g + Offset.
7354 if (isGlobalRelativeToPICBase(OpFlags)) {
7355 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7356 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7357 Result);
7358 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007359
7360 return Result;
7361}
7362
7363SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007364X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007365 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007366 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007367 // Create the TargetGlobalAddress node, folding in the constant
7368 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007369 unsigned char OpFlags =
7370 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007371 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007372 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007373 if (OpFlags == X86II::MO_NO_FLAG &&
7374 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007375 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007376 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007377 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007378 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007379 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007380 }
Eric Christopherfd179292009-08-27 18:07:15 +00007381
Chris Lattner4f066492009-07-11 20:29:19 +00007382 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007383 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007384 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7385 else
7386 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007387
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007388 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007389 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007390 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7391 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007392 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007393 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007394
Chris Lattner36c25012009-07-10 07:34:39 +00007395 // For globals that require a load from a stub to get the address, emit the
7396 // load.
7397 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007398 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007399 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007400
Dan Gohman6520e202008-10-18 02:06:02 +00007401 // If there was a non-zero offset that we didn't fold, create an explicit
7402 // addition for it.
7403 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007404 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007405 DAG.getConstant(Offset, getPointerTy()));
7406
Evan Cheng0db9fe62006-04-25 20:13:52 +00007407 return Result;
7408}
7409
Evan Chengda43bcf2008-09-24 00:05:32 +00007410SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007411X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007412 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007413 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007414 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007415}
7416
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007417static SDValue
7418GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007419 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007420 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007421 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007422 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007423 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007424 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007425 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007426 GA->getOffset(),
7427 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007428 if (InFlag) {
7429 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007430 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007431 } else {
7432 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007433 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007434 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007435
7436 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007437 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007438
Rafael Espindola15f1b662009-04-24 12:59:40 +00007439 SDValue Flag = Chain.getValue(1);
7440 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007441}
7442
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007443// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007444static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007445LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007446 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007447 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007448 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7449 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007450 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007451 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007452 InFlag = Chain.getValue(1);
7453
Chris Lattnerb903bed2009-06-26 21:20:29 +00007454 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007455}
7456
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007457// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007458static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007459LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007460 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007461 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7462 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007463}
7464
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007465// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7466// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007467static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007468 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007469 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007470 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007471
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007472 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7473 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7474 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007475
Michael J. Spencerec38de22010-10-10 22:04:20 +00007476 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007477 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007478 MachinePointerInfo(Ptr),
7479 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007480
Chris Lattnerb903bed2009-06-26 21:20:29 +00007481 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007482 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7483 // initialexec.
7484 unsigned WrapperKind = X86ISD::Wrapper;
7485 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007486 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007487 } else if (is64Bit) {
7488 assert(model == TLSModel::InitialExec);
7489 OperandFlags = X86II::MO_GOTTPOFF;
7490 WrapperKind = X86ISD::WrapperRIP;
7491 } else {
7492 assert(model == TLSModel::InitialExec);
7493 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007494 }
Eric Christopherfd179292009-08-27 18:07:15 +00007495
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007496 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7497 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007498 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007499 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007500 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007501 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007502
Rafael Espindola9a580232009-02-27 13:37:18 +00007503 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007504 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007505 MachinePointerInfo::getGOT(), false, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007506
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007507 // The address of the thread local variable is the add of the thread
7508 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007509 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007510}
7511
Dan Gohman475871a2008-07-27 21:46:04 +00007512SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007513X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007514
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007515 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007516 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007517
Eric Christopher30ef0e52010-06-03 04:07:48 +00007518 if (Subtarget->isTargetELF()) {
7519 // TODO: implement the "local dynamic" model
7520 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007521
Eric Christopher30ef0e52010-06-03 04:07:48 +00007522 // If GV is an alias then use the aliasee for determining
7523 // thread-localness.
7524 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7525 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007526
7527 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007528 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007529
Eric Christopher30ef0e52010-06-03 04:07:48 +00007530 switch (model) {
7531 case TLSModel::GeneralDynamic:
7532 case TLSModel::LocalDynamic: // not implemented
7533 if (Subtarget->is64Bit())
7534 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7535 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007536
Eric Christopher30ef0e52010-06-03 04:07:48 +00007537 case TLSModel::InitialExec:
7538 case TLSModel::LocalExec:
7539 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7540 Subtarget->is64Bit());
7541 }
7542 } else if (Subtarget->isTargetDarwin()) {
7543 // Darwin only has one model of TLS. Lower to that.
7544 unsigned char OpFlag = 0;
7545 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7546 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007547
Eric Christopher30ef0e52010-06-03 04:07:48 +00007548 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7549 // global base reg.
7550 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7551 !Subtarget->is64Bit();
7552 if (PIC32)
7553 OpFlag = X86II::MO_TLVP_PIC_BASE;
7554 else
7555 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007556 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007557 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007558 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007559 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007560 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007561
Eric Christopher30ef0e52010-06-03 04:07:48 +00007562 // With PIC32, the address is actually $g + Offset.
7563 if (PIC32)
7564 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7565 DAG.getNode(X86ISD::GlobalBaseReg,
7566 DebugLoc(), getPointerTy()),
7567 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007568
Eric Christopher30ef0e52010-06-03 04:07:48 +00007569 // Lowering the machine isd will make sure everything is in the right
7570 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007571 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007572 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007573 SDValue Args[] = { Chain, Offset };
7574 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007575
Eric Christopher30ef0e52010-06-03 04:07:48 +00007576 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7577 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7578 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007579
Eric Christopher30ef0e52010-06-03 04:07:48 +00007580 // And our return value (tls address) is in the standard call return value
7581 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007582 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007583 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7584 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007585 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007586
Eric Christopher30ef0e52010-06-03 04:07:48 +00007587 assert(false &&
7588 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007589
Torok Edwinc23197a2009-07-14 16:55:14 +00007590 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007591 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007592}
7593
Evan Cheng0db9fe62006-04-25 20:13:52 +00007594
Nadav Rotem43012222011-05-11 08:12:09 +00007595/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007596/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007597SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007598 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007599 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007600 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007601 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007602 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007603 SDValue ShOpLo = Op.getOperand(0);
7604 SDValue ShOpHi = Op.getOperand(1);
7605 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007606 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007607 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007608 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007609
Dan Gohman475871a2008-07-27 21:46:04 +00007610 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007611 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007612 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7613 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007614 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007615 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7616 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007617 }
Evan Chenge3413162006-01-09 18:33:28 +00007618
Owen Anderson825b72b2009-08-11 20:47:22 +00007619 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7620 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007621 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007622 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007623
Dan Gohman475871a2008-07-27 21:46:04 +00007624 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007625 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007626 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7627 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007628
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007629 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007630 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7631 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007632 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007633 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7634 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007635 }
7636
Dan Gohman475871a2008-07-27 21:46:04 +00007637 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007638 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007639}
Evan Chenga3195e82006-01-12 22:54:21 +00007640
Dan Gohmand858e902010-04-17 15:26:15 +00007641SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7642 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007643 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007644
Dale Johannesen0488fb62010-09-30 23:57:10 +00007645 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007646 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007647
Owen Anderson825b72b2009-08-11 20:47:22 +00007648 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007649 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007650
Eli Friedman36df4992009-05-27 00:47:34 +00007651 // These are really Legal; return the operand so the caller accepts it as
7652 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007653 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007654 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007655 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007656 Subtarget->is64Bit()) {
7657 return Op;
7658 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007659
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007660 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007661 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007662 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007663 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007664 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007665 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007666 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007667 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007668 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007669 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7670}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007671
Owen Andersone50ed302009-08-10 22:56:29 +00007672SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007673 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007674 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007675 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007676 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007677 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007678 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007679 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007680 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007681 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007682 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007683
Chris Lattner492a43e2010-09-22 01:28:21 +00007684 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007685
Stuart Hastings84be9582011-06-02 15:57:11 +00007686 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7687 MachineMemOperand *MMO;
7688 if (FI) {
7689 int SSFI = FI->getIndex();
7690 MMO =
7691 DAG.getMachineFunction()
7692 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7693 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7694 } else {
7695 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7696 StackSlot = StackSlot.getOperand(1);
7697 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007698 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007699 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7700 X86ISD::FILD, DL,
7701 Tys, Ops, array_lengthof(Ops),
7702 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007703
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007704 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007705 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007706 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007707
7708 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7709 // shouldn't be necessary except that RFP cannot be live across
7710 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007711 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007712 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7713 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007714 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007715 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007716 SDValue Ops[] = {
7717 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7718 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007719 MachineMemOperand *MMO =
7720 DAG.getMachineFunction()
7721 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007722 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007723
Chris Lattner492a43e2010-09-22 01:28:21 +00007724 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7725 Ops, array_lengthof(Ops),
7726 Op.getValueType(), MMO);
7727 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007728 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007729 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007730 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007731
Evan Cheng0db9fe62006-04-25 20:13:52 +00007732 return Result;
7733}
7734
Bill Wendling8b8a6362009-01-17 03:56:04 +00007735// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007736SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7737 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007738 // This algorithm is not obvious. Here it is in C code, more or less:
7739 /*
7740 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7741 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7742 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007743
Bill Wendling8b8a6362009-01-17 03:56:04 +00007744 // Copy ints to xmm registers.
7745 __m128i xh = _mm_cvtsi32_si128( hi );
7746 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007747
Bill Wendling8b8a6362009-01-17 03:56:04 +00007748 // Combine into low half of a single xmm register.
7749 __m128i x = _mm_unpacklo_epi32( xh, xl );
7750 __m128d d;
7751 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007752
Bill Wendling8b8a6362009-01-17 03:56:04 +00007753 // Merge in appropriate exponents to give the integer bits the right
7754 // magnitude.
7755 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007756
Bill Wendling8b8a6362009-01-17 03:56:04 +00007757 // Subtract away the biases to deal with the IEEE-754 double precision
7758 // implicit 1.
7759 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007760
Bill Wendling8b8a6362009-01-17 03:56:04 +00007761 // All conversions up to here are exact. The correctly rounded result is
7762 // calculated using the current rounding mode using the following
7763 // horizontal add.
7764 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7765 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7766 // store doesn't really need to be here (except
7767 // maybe to zero the other double)
7768 return sd;
7769 }
7770 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007771
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007772 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007773 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007774
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007775 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007776 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007777 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7778 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7779 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7780 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007781 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007782 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007783
Bill Wendling8b8a6362009-01-17 03:56:04 +00007784 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007785 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007786 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007787 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007788 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007789 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007790 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007791
Owen Anderson825b72b2009-08-11 20:47:22 +00007792 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7793 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007794 Op.getOperand(0),
7795 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007796 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7797 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007798 Op.getOperand(0),
7799 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007800 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7801 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007802 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007803 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007804 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007805 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007806 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007807 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007808 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007809 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007810
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007811 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007812 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007813 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7814 DAG.getUNDEF(MVT::v2f64), ShufMask);
7815 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7816 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007817 DAG.getIntPtrConstant(0));
7818}
7819
Bill Wendling8b8a6362009-01-17 03:56:04 +00007820// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007821SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7822 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007823 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007824 // FP constant to bias correct the final result.
7825 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007826 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007827
7828 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007829 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007830 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007831
Eli Friedmanf3704762011-08-29 21:15:46 +00007832 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007833 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7834 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007835
Owen Anderson825b72b2009-08-11 20:47:22 +00007836 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007837 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007838 DAG.getIntPtrConstant(0));
7839
7840 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007841 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007842 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007843 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007844 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007845 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007846 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007847 MVT::v2f64, Bias)));
7848 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007849 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007850 DAG.getIntPtrConstant(0));
7851
7852 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007853 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007854
7855 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007856 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007857
Owen Anderson825b72b2009-08-11 20:47:22 +00007858 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007859 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007860 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007861 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007862 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007863 }
7864
7865 // Handle final rounding.
7866 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007867}
7868
Dan Gohmand858e902010-04-17 15:26:15 +00007869SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7870 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007871 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007872 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007873
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007874 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007875 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7876 // the optimization here.
7877 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007878 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007879
Owen Andersone50ed302009-08-10 22:56:29 +00007880 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007881 EVT DstVT = Op.getValueType();
7882 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007883 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007884 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007885 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007886
7887 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007888 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007889 if (SrcVT == MVT::i32) {
7890 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7891 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7892 getPointerTy(), StackSlot, WordOff);
7893 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007894 StackSlot, MachinePointerInfo(),
7895 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007896 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007897 OffsetSlot, MachinePointerInfo(),
7898 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007899 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7900 return Fild;
7901 }
7902
7903 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7904 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007905 StackSlot, MachinePointerInfo(),
7906 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007907 // For i64 source, we need to add the appropriate power of 2 if the input
7908 // was negative. This is the same as the optimization in
7909 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7910 // we must be careful to do the computation in x87 extended precision, not
7911 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007912 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7913 MachineMemOperand *MMO =
7914 DAG.getMachineFunction()
7915 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7916 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007917
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007918 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7919 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007920 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7921 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007922
7923 APInt FF(32, 0x5F800000ULL);
7924
7925 // Check whether the sign bit is set.
7926 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7927 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7928 ISD::SETLT);
7929
7930 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7931 SDValue FudgePtr = DAG.getConstantPool(
7932 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7933 getPointerTy());
7934
7935 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7936 SDValue Zero = DAG.getIntPtrConstant(0);
7937 SDValue Four = DAG.getIntPtrConstant(4);
7938 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7939 Zero, Four);
7940 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7941
7942 // Load the value out, extending it from f32 to f80.
7943 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007944 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007945 FudgePtr, MachinePointerInfo::getConstantPool(),
7946 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007947 // Extend everything to 80 bits to force it to be done on x87.
7948 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7949 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007950}
7951
Dan Gohman475871a2008-07-27 21:46:04 +00007952std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007953FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007954 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007955
Owen Andersone50ed302009-08-10 22:56:29 +00007956 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007957
7958 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007959 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7960 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007961 }
7962
Owen Anderson825b72b2009-08-11 20:47:22 +00007963 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7964 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007965 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007966
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007967 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007968 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007969 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007970 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007971 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007972 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007973 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007974 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007975
Evan Cheng87c89352007-10-15 20:11:21 +00007976 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7977 // stack slot.
7978 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007979 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007980 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007981 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007982
Michael J. Spencerec38de22010-10-10 22:04:20 +00007983
7984
Evan Cheng0db9fe62006-04-25 20:13:52 +00007985 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007986 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007987 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007988 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7989 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7990 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007991 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007992
Dan Gohman475871a2008-07-27 21:46:04 +00007993 SDValue Chain = DAG.getEntryNode();
7994 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007995 EVT TheVT = Op.getOperand(0).getValueType();
7996 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007997 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007998 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007999 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008000 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008001 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008002 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008003 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008004 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008005
Chris Lattner492a43e2010-09-22 01:28:21 +00008006 MachineMemOperand *MMO =
8007 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8008 MachineMemOperand::MOLoad, MemSize, MemSize);
8009 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8010 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008011 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008012 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008013 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8014 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008015
Chris Lattner07290932010-09-22 01:05:16 +00008016 MachineMemOperand *MMO =
8017 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8018 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008019
Evan Cheng0db9fe62006-04-25 20:13:52 +00008020 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00008021 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00008022 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8023 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00008024
Chris Lattner27a6c732007-11-24 07:07:01 +00008025 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008026}
8027
Dan Gohmand858e902010-04-17 15:26:15 +00008028SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8029 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008030 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008031 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008032
Eli Friedman948e95a2009-05-23 09:59:16 +00008033 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00008034 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008035 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8036 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008037
Chris Lattner27a6c732007-11-24 07:07:01 +00008038 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008039 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008040 FIST, StackSlot, MachinePointerInfo(),
8041 false, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00008042}
8043
Dan Gohmand858e902010-04-17 15:26:15 +00008044SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8045 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00008046 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
8047 SDValue FIST = Vals.first, StackSlot = Vals.second;
8048 assert(FIST.getNode() && "Unexpected failure");
8049
8050 // Load the result.
8051 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008052 FIST, StackSlot, MachinePointerInfo(),
8053 false, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00008054}
8055
Dan Gohmand858e902010-04-17 15:26:15 +00008056SDValue X86TargetLowering::LowerFABS(SDValue Op,
8057 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008058 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008059 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008060 EVT VT = Op.getValueType();
8061 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008062 if (VT.isVector())
8063 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008064 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008065 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008066 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00008067 CV.push_back(C);
8068 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008069 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008070 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00008071 CV.push_back(C);
8072 CV.push_back(C);
8073 CV.push_back(C);
8074 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008075 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008076 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008077 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008078 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008079 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008080 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008081 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008082}
8083
Dan Gohmand858e902010-04-17 15:26:15 +00008084SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008085 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008086 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008087 EVT VT = Op.getValueType();
8088 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008089 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008090 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008091 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008092 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008093 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008094 CV.push_back(C);
8095 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008096 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008097 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008098 CV.push_back(C);
8099 CV.push_back(C);
8100 CV.push_back(C);
8101 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008102 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008103 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008104 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008105 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008106 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008107 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008108 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008109 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008110 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008111 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008112 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008113 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008114 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008115 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008116 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008117}
8118
Dan Gohmand858e902010-04-17 15:26:15 +00008119SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008120 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008121 SDValue Op0 = Op.getOperand(0);
8122 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008123 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008124 EVT VT = Op.getValueType();
8125 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008126
8127 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008128 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008129 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008130 SrcVT = VT;
8131 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008132 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008133 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008134 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008135 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008136 }
8137
8138 // At this point the operands and the result should have the same
8139 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008140
Evan Cheng68c47cb2007-01-05 07:55:56 +00008141 // First get the sign bit of second operand.
8142 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008143 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008144 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8145 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008146 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008147 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8148 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8149 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8150 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008151 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008152 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008153 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008154 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008155 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008156 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008157 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008158
8159 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008160 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008161 // Op0 is MVT::f32, Op1 is MVT::f64.
8162 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8163 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8164 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008165 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008166 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008167 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008168 }
8169
Evan Cheng73d6cf12007-01-05 21:37:56 +00008170 // Clear first operand sign bit.
8171 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008172 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008173 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8174 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008175 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008176 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8177 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8178 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8179 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008180 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008181 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008182 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008183 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008184 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008185 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008186 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008187
8188 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008189 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008190}
8191
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008192SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8193 SDValue N0 = Op.getOperand(0);
8194 DebugLoc dl = Op.getDebugLoc();
8195 EVT VT = Op.getValueType();
8196
8197 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8198 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8199 DAG.getConstant(1, VT));
8200 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8201}
8202
Dan Gohman076aee32009-03-04 19:44:21 +00008203/// Emit nodes that will be selected as "test Op0,Op0", or something
8204/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008205SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008206 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008207 DebugLoc dl = Op.getDebugLoc();
8208
Dan Gohman31125812009-03-07 01:58:32 +00008209 // CF and OF aren't always set the way we want. Determine which
8210 // of these we need.
8211 bool NeedCF = false;
8212 bool NeedOF = false;
8213 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008214 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008215 case X86::COND_A: case X86::COND_AE:
8216 case X86::COND_B: case X86::COND_BE:
8217 NeedCF = true;
8218 break;
8219 case X86::COND_G: case X86::COND_GE:
8220 case X86::COND_L: case X86::COND_LE:
8221 case X86::COND_O: case X86::COND_NO:
8222 NeedOF = true;
8223 break;
Dan Gohman31125812009-03-07 01:58:32 +00008224 }
8225
Dan Gohman076aee32009-03-04 19:44:21 +00008226 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008227 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8228 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008229 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8230 // Emit a CMP with 0, which is the TEST pattern.
8231 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8232 DAG.getConstant(0, Op.getValueType()));
8233
8234 unsigned Opcode = 0;
8235 unsigned NumOperands = 0;
8236 switch (Op.getNode()->getOpcode()) {
8237 case ISD::ADD:
8238 // Due to an isel shortcoming, be conservative if this add is likely to be
8239 // selected as part of a load-modify-store instruction. When the root node
8240 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8241 // uses of other nodes in the match, such as the ADD in this case. This
8242 // leads to the ADD being left around and reselected, with the result being
8243 // two adds in the output. Alas, even if none our users are stores, that
8244 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8245 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8246 // climbing the DAG back to the root, and it doesn't seem to be worth the
8247 // effort.
8248 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008249 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8250 if (UI->getOpcode() != ISD::CopyToReg &&
8251 UI->getOpcode() != ISD::SETCC &&
8252 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008253 goto default_case;
8254
8255 if (ConstantSDNode *C =
8256 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8257 // An add of one will be selected as an INC.
8258 if (C->getAPIntValue() == 1) {
8259 Opcode = X86ISD::INC;
8260 NumOperands = 1;
8261 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008262 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008263
8264 // An add of negative one (subtract of one) will be selected as a DEC.
8265 if (C->getAPIntValue().isAllOnesValue()) {
8266 Opcode = X86ISD::DEC;
8267 NumOperands = 1;
8268 break;
8269 }
Dan Gohman076aee32009-03-04 19:44:21 +00008270 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008271
8272 // Otherwise use a regular EFLAGS-setting add.
8273 Opcode = X86ISD::ADD;
8274 NumOperands = 2;
8275 break;
8276 case ISD::AND: {
8277 // If the primary and result isn't used, don't bother using X86ISD::AND,
8278 // because a TEST instruction will be better.
8279 bool NonFlagUse = false;
8280 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8281 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8282 SDNode *User = *UI;
8283 unsigned UOpNo = UI.getOperandNo();
8284 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8285 // Look pass truncate.
8286 UOpNo = User->use_begin().getOperandNo();
8287 User = *User->use_begin();
8288 }
8289
8290 if (User->getOpcode() != ISD::BRCOND &&
8291 User->getOpcode() != ISD::SETCC &&
8292 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8293 NonFlagUse = true;
8294 break;
8295 }
Dan Gohman076aee32009-03-04 19:44:21 +00008296 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008297
8298 if (!NonFlagUse)
8299 break;
8300 }
8301 // FALL THROUGH
8302 case ISD::SUB:
8303 case ISD::OR:
8304 case ISD::XOR:
8305 // Due to the ISEL shortcoming noted above, be conservative if this op is
8306 // likely to be selected as part of a load-modify-store instruction.
8307 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8308 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8309 if (UI->getOpcode() == ISD::STORE)
8310 goto default_case;
8311
8312 // Otherwise use a regular EFLAGS-setting instruction.
8313 switch (Op.getNode()->getOpcode()) {
8314 default: llvm_unreachable("unexpected operator!");
8315 case ISD::SUB: Opcode = X86ISD::SUB; break;
8316 case ISD::OR: Opcode = X86ISD::OR; break;
8317 case ISD::XOR: Opcode = X86ISD::XOR; break;
8318 case ISD::AND: Opcode = X86ISD::AND; break;
8319 }
8320
8321 NumOperands = 2;
8322 break;
8323 case X86ISD::ADD:
8324 case X86ISD::SUB:
8325 case X86ISD::INC:
8326 case X86ISD::DEC:
8327 case X86ISD::OR:
8328 case X86ISD::XOR:
8329 case X86ISD::AND:
8330 return SDValue(Op.getNode(), 1);
8331 default:
8332 default_case:
8333 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008334 }
8335
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008336 if (Opcode == 0)
8337 // Emit a CMP with 0, which is the TEST pattern.
8338 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8339 DAG.getConstant(0, Op.getValueType()));
8340
8341 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8342 SmallVector<SDValue, 4> Ops;
8343 for (unsigned i = 0; i != NumOperands; ++i)
8344 Ops.push_back(Op.getOperand(i));
8345
8346 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8347 DAG.ReplaceAllUsesWith(Op, New);
8348 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008349}
8350
8351/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8352/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008353SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008354 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008355 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8356 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008357 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008358
8359 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008360 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008361}
8362
Evan Chengd40d03e2010-01-06 19:38:29 +00008363/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8364/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008365SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8366 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008367 SDValue Op0 = And.getOperand(0);
8368 SDValue Op1 = And.getOperand(1);
8369 if (Op0.getOpcode() == ISD::TRUNCATE)
8370 Op0 = Op0.getOperand(0);
8371 if (Op1.getOpcode() == ISD::TRUNCATE)
8372 Op1 = Op1.getOperand(0);
8373
Evan Chengd40d03e2010-01-06 19:38:29 +00008374 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008375 if (Op1.getOpcode() == ISD::SHL)
8376 std::swap(Op0, Op1);
8377 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008378 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8379 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008380 // If we looked past a truncate, check that it's only truncating away
8381 // known zeros.
8382 unsigned BitWidth = Op0.getValueSizeInBits();
8383 unsigned AndBitWidth = And.getValueSizeInBits();
8384 if (BitWidth > AndBitWidth) {
8385 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8386 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8387 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8388 return SDValue();
8389 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008390 LHS = Op1;
8391 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008392 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008393 } else if (Op1.getOpcode() == ISD::Constant) {
8394 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008395 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008396 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008397
8398 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008399 LHS = AndLHS.getOperand(0);
8400 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008401 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008402
8403 // Use BT if the immediate can't be encoded in a TEST instruction.
8404 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8405 LHS = AndLHS;
8406 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8407 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008408 }
Evan Cheng0488db92007-09-25 01:57:46 +00008409
Evan Chengd40d03e2010-01-06 19:38:29 +00008410 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008411 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008412 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008413 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008414 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008415 // Also promote i16 to i32 for performance / code size reason.
8416 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008417 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008418 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008419
Evan Chengd40d03e2010-01-06 19:38:29 +00008420 // If the operand types disagree, extend the shift amount to match. Since
8421 // BT ignores high bits (like shifts) we can use anyextend.
8422 if (LHS.getValueType() != RHS.getValueType())
8423 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008424
Evan Chengd40d03e2010-01-06 19:38:29 +00008425 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8426 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8427 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8428 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008429 }
8430
Evan Cheng54de3ea2010-01-05 06:52:31 +00008431 return SDValue();
8432}
8433
Dan Gohmand858e902010-04-17 15:26:15 +00008434SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008435
8436 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8437
Evan Cheng54de3ea2010-01-05 06:52:31 +00008438 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8439 SDValue Op0 = Op.getOperand(0);
8440 SDValue Op1 = Op.getOperand(1);
8441 DebugLoc dl = Op.getDebugLoc();
8442 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8443
8444 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008445 // Lower (X & (1 << N)) == 0 to BT(X, N).
8446 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8447 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008448 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008449 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008450 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008451 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8452 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8453 if (NewSetCC.getNode())
8454 return NewSetCC;
8455 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008456
Chris Lattner481eebc2010-12-19 21:23:48 +00008457 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8458 // these.
8459 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008460 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008461 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8462 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008463
Chris Lattner481eebc2010-12-19 21:23:48 +00008464 // If the input is a setcc, then reuse the input setcc or use a new one with
8465 // the inverted condition.
8466 if (Op0.getOpcode() == X86ISD::SETCC) {
8467 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8468 bool Invert = (CC == ISD::SETNE) ^
8469 cast<ConstantSDNode>(Op1)->isNullValue();
8470 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008471
Evan Cheng2c755ba2010-02-27 07:36:59 +00008472 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008473 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8474 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8475 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008476 }
8477
Evan Chenge5b51ac2010-04-17 06:13:15 +00008478 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008479 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008480 if (X86CC == X86::COND_INVALID)
8481 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008482
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008483 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008484 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008485 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008486}
8487
Craig Topper89af15e2011-09-18 08:03:58 +00008488// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008489// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008490static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008491 EVT VT = Op.getValueType();
8492
Duncan Sands28b77e92011-09-06 19:07:46 +00008493 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008494 "Unsupported value type for operation");
8495
8496 int NumElems = VT.getVectorNumElements();
8497 DebugLoc dl = Op.getDebugLoc();
8498 SDValue CC = Op.getOperand(2);
8499 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8500 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8501
8502 // Extract the LHS vectors
8503 SDValue LHS = Op.getOperand(0);
8504 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8505 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8506
8507 // Extract the RHS vectors
8508 SDValue RHS = Op.getOperand(1);
8509 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8510 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8511
8512 // Issue the operation on the smaller types and concatenate the result back
8513 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8514 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8515 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8516 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8517 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8518}
8519
8520
Dan Gohmand858e902010-04-17 15:26:15 +00008521SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008522 SDValue Cond;
8523 SDValue Op0 = Op.getOperand(0);
8524 SDValue Op1 = Op.getOperand(1);
8525 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008526 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008527 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8528 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008529 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008530
8531 if (isFP) {
8532 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008533 EVT EltVT = Op0.getValueType().getVectorElementType();
8534 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8535
8536 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008537 bool Swap = false;
8538
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008539 // SSE Condition code mapping:
8540 // 0 - EQ
8541 // 1 - LT
8542 // 2 - LE
8543 // 3 - UNORD
8544 // 4 - NEQ
8545 // 5 - NLT
8546 // 6 - NLE
8547 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008548 switch (SetCCOpcode) {
8549 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008550 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008551 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008552 case ISD::SETOGT:
8553 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008554 case ISD::SETLT:
8555 case ISD::SETOLT: SSECC = 1; break;
8556 case ISD::SETOGE:
8557 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008558 case ISD::SETLE:
8559 case ISD::SETOLE: SSECC = 2; break;
8560 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008561 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008562 case ISD::SETNE: SSECC = 4; break;
8563 case ISD::SETULE: Swap = true;
8564 case ISD::SETUGE: SSECC = 5; break;
8565 case ISD::SETULT: Swap = true;
8566 case ISD::SETUGT: SSECC = 6; break;
8567 case ISD::SETO: SSECC = 7; break;
8568 }
8569 if (Swap)
8570 std::swap(Op0, Op1);
8571
Nate Begemanfb8ead02008-07-25 19:05:58 +00008572 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008573 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008574 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008575 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008576 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8577 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008578 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Craig Topper0a150352011-11-09 08:06:13 +00008579 } else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008580 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008581 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8582 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008583 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008584 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008585 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008586 }
8587 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008588 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008589 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008590
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008591 // Break 256-bit integer vector compare into smaller ones.
Craig Topper0a150352011-11-09 08:06:13 +00008592 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008593 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008594
Nate Begeman30a0de92008-07-17 16:51:19 +00008595 // We are handling one of the integer comparisons here. Since SSE only has
8596 // GT and EQ comparisons for integer, swapping operands and multiple
8597 // operations may be required for some comparisons.
8598 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8599 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008600
Craig Topper0a150352011-11-09 08:06:13 +00008601 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008602 default: break;
Craig Topper0a150352011-11-09 08:06:13 +00008603 case MVT::i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
8604 case MVT::i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
8605 case MVT::i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8606 case MVT::i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008607 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008608
Nate Begeman30a0de92008-07-17 16:51:19 +00008609 switch (SetCCOpcode) {
8610 default: break;
8611 case ISD::SETNE: Invert = true;
8612 case ISD::SETEQ: Opc = EQOpc; break;
8613 case ISD::SETLT: Swap = true;
8614 case ISD::SETGT: Opc = GTOpc; break;
8615 case ISD::SETGE: Swap = true;
8616 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8617 case ISD::SETULT: Swap = true;
8618 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8619 case ISD::SETUGE: Swap = true;
8620 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8621 }
8622 if (Swap)
8623 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008624
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008625 // Check that the operation in question is available (most are plain SSE2,
8626 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topperc0d82852011-11-22 00:44:41 +00008627 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42orAVX())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008628 return SDValue();
Craig Topperc0d82852011-11-22 00:44:41 +00008629 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41orAVX())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008630 return SDValue();
8631
Nate Begeman30a0de92008-07-17 16:51:19 +00008632 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8633 // bits of the inputs before performing those operations.
8634 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008635 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008636 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8637 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008638 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008639 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8640 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008641 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8642 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008643 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008644
Dale Johannesenace16102009-02-03 19:33:06 +00008645 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008646
8647 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008648 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008649 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008650
Nate Begeman30a0de92008-07-17 16:51:19 +00008651 return Result;
8652}
Evan Cheng0488db92007-09-25 01:57:46 +00008653
Evan Cheng370e5342008-12-03 08:38:43 +00008654// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008655static bool isX86LogicalCmp(SDValue Op) {
8656 unsigned Opc = Op.getNode()->getOpcode();
8657 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8658 return true;
8659 if (Op.getResNo() == 1 &&
8660 (Opc == X86ISD::ADD ||
8661 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008662 Opc == X86ISD::ADC ||
8663 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008664 Opc == X86ISD::SMUL ||
8665 Opc == X86ISD::UMUL ||
8666 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008667 Opc == X86ISD::DEC ||
8668 Opc == X86ISD::OR ||
8669 Opc == X86ISD::XOR ||
8670 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008671 return true;
8672
Chris Lattner9637d5b2010-12-05 07:49:54 +00008673 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8674 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008675
Dan Gohman076aee32009-03-04 19:44:21 +00008676 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008677}
8678
Chris Lattnera2b56002010-12-05 01:23:24 +00008679static bool isZero(SDValue V) {
8680 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8681 return C && C->isNullValue();
8682}
8683
Chris Lattner96908b12010-12-05 02:00:51 +00008684static bool isAllOnes(SDValue V) {
8685 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8686 return C && C->isAllOnesValue();
8687}
8688
Dan Gohmand858e902010-04-17 15:26:15 +00008689SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008690 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008691 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008692 SDValue Op1 = Op.getOperand(1);
8693 SDValue Op2 = Op.getOperand(2);
8694 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008695 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008696
Dan Gohman1a492952009-10-20 16:22:37 +00008697 if (Cond.getOpcode() == ISD::SETCC) {
8698 SDValue NewCond = LowerSETCC(Cond, DAG);
8699 if (NewCond.getNode())
8700 Cond = NewCond;
8701 }
Evan Cheng734503b2006-09-11 02:19:56 +00008702
Chris Lattnera2b56002010-12-05 01:23:24 +00008703 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008704 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008705 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008706 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008707 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008708 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8709 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008710 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008711
Chris Lattnera2b56002010-12-05 01:23:24 +00008712 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008713
8714 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008715 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8716 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008717
8718 SDValue CmpOp0 = Cmp.getOperand(0);
8719 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8720 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008721
Chris Lattner96908b12010-12-05 02:00:51 +00008722 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008723 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8724 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008725
Chris Lattner96908b12010-12-05 02:00:51 +00008726 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8727 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008728
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008729 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008730 if (N2C == 0 || !N2C->isNullValue())
8731 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8732 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008733 }
8734 }
8735
Chris Lattnera2b56002010-12-05 01:23:24 +00008736 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008737 if (Cond.getOpcode() == ISD::AND &&
8738 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8739 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008740 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008741 Cond = Cond.getOperand(0);
8742 }
8743
Evan Cheng3f41d662007-10-08 22:16:29 +00008744 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8745 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008746 unsigned CondOpcode = Cond.getOpcode();
8747 if (CondOpcode == X86ISD::SETCC ||
8748 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008749 CC = Cond.getOperand(0);
8750
Dan Gohman475871a2008-07-27 21:46:04 +00008751 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008752 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008753 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008754
Evan Cheng3f41d662007-10-08 22:16:29 +00008755 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008756 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008757 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008758 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008759
Chris Lattnerd1980a52009-03-12 06:52:53 +00008760 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8761 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008762 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008763 addTest = false;
8764 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008765 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8766 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8767 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8768 Cond.getOperand(0).getValueType() != MVT::i8)) {
8769 SDValue LHS = Cond.getOperand(0);
8770 SDValue RHS = Cond.getOperand(1);
8771 unsigned X86Opcode;
8772 unsigned X86Cond;
8773 SDVTList VTs;
8774 switch (CondOpcode) {
8775 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8776 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8777 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8778 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8779 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8780 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8781 default: llvm_unreachable("unexpected overflowing operator");
8782 }
8783 if (CondOpcode == ISD::UMULO)
8784 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8785 MVT::i32);
8786 else
8787 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8788
8789 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8790
8791 if (CondOpcode == ISD::UMULO)
8792 Cond = X86Op.getValue(2);
8793 else
8794 Cond = X86Op.getValue(1);
8795
8796 CC = DAG.getConstant(X86Cond, MVT::i8);
8797 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008798 }
8799
8800 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008801 // Look pass the truncate.
8802 if (Cond.getOpcode() == ISD::TRUNCATE)
8803 Cond = Cond.getOperand(0);
8804
8805 // We know the result of AND is compared against zero. Try to match
8806 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008807 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008808 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008809 if (NewSetCC.getNode()) {
8810 CC = NewSetCC.getOperand(0);
8811 Cond = NewSetCC.getOperand(1);
8812 addTest = false;
8813 }
8814 }
8815 }
8816
8817 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008818 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008819 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008820 }
8821
Benjamin Kramere915ff32010-12-22 23:09:28 +00008822 // a < b ? -1 : 0 -> RES = ~setcc_carry
8823 // a < b ? 0 : -1 -> RES = setcc_carry
8824 // a >= b ? -1 : 0 -> RES = setcc_carry
8825 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8826 if (Cond.getOpcode() == X86ISD::CMP) {
8827 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8828
8829 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8830 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8831 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8832 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8833 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8834 return DAG.getNOT(DL, Res, Res.getValueType());
8835 return Res;
8836 }
8837 }
8838
Evan Cheng0488db92007-09-25 01:57:46 +00008839 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8840 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008841 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008842 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008843 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008844}
8845
Evan Cheng370e5342008-12-03 08:38:43 +00008846// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8847// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8848// from the AND / OR.
8849static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8850 Opc = Op.getOpcode();
8851 if (Opc != ISD::OR && Opc != ISD::AND)
8852 return false;
8853 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8854 Op.getOperand(0).hasOneUse() &&
8855 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8856 Op.getOperand(1).hasOneUse());
8857}
8858
Evan Cheng961d6d42009-02-02 08:19:07 +00008859// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8860// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008861static bool isXor1OfSetCC(SDValue Op) {
8862 if (Op.getOpcode() != ISD::XOR)
8863 return false;
8864 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8865 if (N1C && N1C->getAPIntValue() == 1) {
8866 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8867 Op.getOperand(0).hasOneUse();
8868 }
8869 return false;
8870}
8871
Dan Gohmand858e902010-04-17 15:26:15 +00008872SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008873 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008874 SDValue Chain = Op.getOperand(0);
8875 SDValue Cond = Op.getOperand(1);
8876 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008877 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008878 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008879 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008880
Dan Gohman1a492952009-10-20 16:22:37 +00008881 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008882 // Check for setcc([su]{add,sub,mul}o == 0).
8883 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8884 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8885 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8886 Cond.getOperand(0).getResNo() == 1 &&
8887 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
8888 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
8889 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
8890 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
8891 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
8892 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
8893 Inverted = true;
8894 Cond = Cond.getOperand(0);
8895 } else {
8896 SDValue NewCond = LowerSETCC(Cond, DAG);
8897 if (NewCond.getNode())
8898 Cond = NewCond;
8899 }
Dan Gohman1a492952009-10-20 16:22:37 +00008900 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008901#if 0
8902 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008903 else if (Cond.getOpcode() == X86ISD::ADD ||
8904 Cond.getOpcode() == X86ISD::SUB ||
8905 Cond.getOpcode() == X86ISD::SMUL ||
8906 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008907 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008908#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008909
Evan Chengad9c0a32009-12-15 00:53:42 +00008910 // Look pass (and (setcc_carry (cmp ...)), 1).
8911 if (Cond.getOpcode() == ISD::AND &&
8912 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8913 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008914 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008915 Cond = Cond.getOperand(0);
8916 }
8917
Evan Cheng3f41d662007-10-08 22:16:29 +00008918 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8919 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008920 unsigned CondOpcode = Cond.getOpcode();
8921 if (CondOpcode == X86ISD::SETCC ||
8922 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008923 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008924
Dan Gohman475871a2008-07-27 21:46:04 +00008925 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008926 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008927 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008928 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008929 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008930 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008931 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008932 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008933 default: break;
8934 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008935 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008936 // These can only come from an arithmetic instruction with overflow,
8937 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008938 Cond = Cond.getNode()->getOperand(1);
8939 addTest = false;
8940 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008941 }
Evan Cheng0488db92007-09-25 01:57:46 +00008942 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008943 }
8944 CondOpcode = Cond.getOpcode();
8945 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8946 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8947 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8948 Cond.getOperand(0).getValueType() != MVT::i8)) {
8949 SDValue LHS = Cond.getOperand(0);
8950 SDValue RHS = Cond.getOperand(1);
8951 unsigned X86Opcode;
8952 unsigned X86Cond;
8953 SDVTList VTs;
8954 switch (CondOpcode) {
8955 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8956 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8957 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8958 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8959 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8960 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8961 default: llvm_unreachable("unexpected overflowing operator");
8962 }
8963 if (Inverted)
8964 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
8965 if (CondOpcode == ISD::UMULO)
8966 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8967 MVT::i32);
8968 else
8969 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8970
8971 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
8972
8973 if (CondOpcode == ISD::UMULO)
8974 Cond = X86Op.getValue(2);
8975 else
8976 Cond = X86Op.getValue(1);
8977
8978 CC = DAG.getConstant(X86Cond, MVT::i8);
8979 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00008980 } else {
8981 unsigned CondOpc;
8982 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8983 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008984 if (CondOpc == ISD::OR) {
8985 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8986 // two branches instead of an explicit OR instruction with a
8987 // separate test.
8988 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008989 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008990 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008991 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008992 Chain, Dest, CC, Cmp);
8993 CC = Cond.getOperand(1).getOperand(0);
8994 Cond = Cmp;
8995 addTest = false;
8996 }
8997 } else { // ISD::AND
8998 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8999 // two branches instead of an explicit AND instruction with a
9000 // separate test. However, we only do this if this block doesn't
9001 // have a fall-through edge, because this requires an explicit
9002 // jmp when the condition is false.
9003 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009004 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009005 Op.getNode()->hasOneUse()) {
9006 X86::CondCode CCode =
9007 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9008 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009009 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009010 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009011 // Look for an unconditional branch following this conditional branch.
9012 // We need this because we need to reverse the successors in order
9013 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009014 if (User->getOpcode() == ISD::BR) {
9015 SDValue FalseBB = User->getOperand(1);
9016 SDNode *NewBR =
9017 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009018 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009019 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009020 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009021
Dale Johannesene4d209d2009-02-03 20:21:25 +00009022 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009023 Chain, Dest, CC, Cmp);
9024 X86::CondCode CCode =
9025 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9026 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009027 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009028 Cond = Cmp;
9029 addTest = false;
9030 }
9031 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009032 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009033 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9034 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9035 // It should be transformed during dag combiner except when the condition
9036 // is set by a arithmetics with overflow node.
9037 X86::CondCode CCode =
9038 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9039 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009040 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009041 Cond = Cond.getOperand(0).getOperand(1);
9042 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009043 } else if (Cond.getOpcode() == ISD::SETCC &&
9044 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9045 // For FCMP_OEQ, we can emit
9046 // two branches instead of an explicit AND instruction with a
9047 // separate test. However, we only do this if this block doesn't
9048 // have a fall-through edge, because this requires an explicit
9049 // jmp when the condition is false.
9050 if (Op.getNode()->hasOneUse()) {
9051 SDNode *User = *Op.getNode()->use_begin();
9052 // Look for an unconditional branch following this conditional branch.
9053 // We need this because we need to reverse the successors in order
9054 // to implement FCMP_OEQ.
9055 if (User->getOpcode() == ISD::BR) {
9056 SDValue FalseBB = User->getOperand(1);
9057 SDNode *NewBR =
9058 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9059 assert(NewBR == User);
9060 (void)NewBR;
9061 Dest = FalseBB;
9062
9063 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9064 Cond.getOperand(0), Cond.getOperand(1));
9065 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9066 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9067 Chain, Dest, CC, Cmp);
9068 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9069 Cond = Cmp;
9070 addTest = false;
9071 }
9072 }
9073 } else if (Cond.getOpcode() == ISD::SETCC &&
9074 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9075 // For FCMP_UNE, we can emit
9076 // two branches instead of an explicit AND instruction with a
9077 // separate test. However, we only do this if this block doesn't
9078 // have a fall-through edge, because this requires an explicit
9079 // jmp when the condition is false.
9080 if (Op.getNode()->hasOneUse()) {
9081 SDNode *User = *Op.getNode()->use_begin();
9082 // Look for an unconditional branch following this conditional branch.
9083 // We need this because we need to reverse the successors in order
9084 // to implement FCMP_UNE.
9085 if (User->getOpcode() == ISD::BR) {
9086 SDValue FalseBB = User->getOperand(1);
9087 SDNode *NewBR =
9088 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9089 assert(NewBR == User);
9090 (void)NewBR;
9091
9092 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9093 Cond.getOperand(0), Cond.getOperand(1));
9094 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9095 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9096 Chain, Dest, CC, Cmp);
9097 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9098 Cond = Cmp;
9099 addTest = false;
9100 Dest = FalseBB;
9101 }
9102 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009103 }
Evan Cheng0488db92007-09-25 01:57:46 +00009104 }
9105
9106 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009107 // Look pass the truncate.
9108 if (Cond.getOpcode() == ISD::TRUNCATE)
9109 Cond = Cond.getOperand(0);
9110
9111 // We know the result of AND is compared against zero. Try to match
9112 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009113 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009114 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9115 if (NewSetCC.getNode()) {
9116 CC = NewSetCC.getOperand(0);
9117 Cond = NewSetCC.getOperand(1);
9118 addTest = false;
9119 }
9120 }
9121 }
9122
9123 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009124 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009125 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009126 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00009127 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009128 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009129}
9130
Anton Korobeynikove060b532007-04-17 19:34:00 +00009131
9132// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9133// Calls to _alloca is needed to probe the stack when allocating more than 4k
9134// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9135// that the guard pages used by the OS virtual memory manager are allocated in
9136// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009137SDValue
9138X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009139 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009140 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
9141 EnableSegmentedStacks) &&
9142 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009143 "are being used");
9144 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009145 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009146
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009147 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009148 SDValue Chain = Op.getOperand(0);
9149 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009150 // FIXME: Ensure alignment here
9151
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009152 bool Is64Bit = Subtarget->is64Bit();
9153 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009154
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009155 if (EnableSegmentedStacks) {
9156 MachineFunction &MF = DAG.getMachineFunction();
9157 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009158
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009159 if (Is64Bit) {
9160 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009161 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009162 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009163
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009164 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
9165 I != E; I++)
9166 if (I->hasNestAttr())
9167 report_fatal_error("Cannot use segmented stacks with functions that "
9168 "have nested arguments.");
9169 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009170
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009171 const TargetRegisterClass *AddrRegClass =
9172 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9173 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9174 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9175 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9176 DAG.getRegister(Vreg, SPTy));
9177 SDValue Ops1[2] = { Value, Chain };
9178 return DAG.getMergeValues(Ops1, 2, dl);
9179 } else {
9180 SDValue Flag;
9181 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009182
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009183 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9184 Flag = Chain.getValue(1);
9185 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009186
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009187 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9188 Flag = Chain.getValue(1);
9189
9190 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9191
9192 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9193 return DAG.getMergeValues(Ops1, 2, dl);
9194 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009195}
9196
Dan Gohmand858e902010-04-17 15:26:15 +00009197SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009198 MachineFunction &MF = DAG.getMachineFunction();
9199 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9200
Dan Gohman69de1932008-02-06 22:27:42 +00009201 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009202 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009203
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009204 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009205 // vastart just stores the address of the VarArgsFrameIndex slot into the
9206 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009207 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9208 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009209 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9210 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009211 }
9212
9213 // __va_list_tag:
9214 // gp_offset (0 - 6 * 8)
9215 // fp_offset (48 - 48 + 8 * 16)
9216 // overflow_arg_area (point to parameters coming in memory).
9217 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009218 SmallVector<SDValue, 8> MemOps;
9219 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009220 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009221 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009222 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9223 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009224 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009225 MemOps.push_back(Store);
9226
9227 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009228 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009229 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009230 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009231 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9232 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009233 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009234 MemOps.push_back(Store);
9235
9236 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009237 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009238 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009239 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9240 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009241 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9242 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009243 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009244 MemOps.push_back(Store);
9245
9246 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009247 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009248 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009249 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9250 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009251 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9252 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009253 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009254 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009255 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009256}
9257
Dan Gohmand858e902010-04-17 15:26:15 +00009258SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009259 assert(Subtarget->is64Bit() &&
9260 "LowerVAARG only handles 64-bit va_arg!");
9261 assert((Subtarget->isTargetLinux() ||
9262 Subtarget->isTargetDarwin()) &&
9263 "Unhandled target in LowerVAARG");
9264 assert(Op.getNode()->getNumOperands() == 4);
9265 SDValue Chain = Op.getOperand(0);
9266 SDValue SrcPtr = Op.getOperand(1);
9267 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9268 unsigned Align = Op.getConstantOperandVal(3);
9269 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009270
Dan Gohman320afb82010-10-12 18:00:49 +00009271 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009272 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009273 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9274 uint8_t ArgMode;
9275
9276 // Decide which area this value should be read from.
9277 // TODO: Implement the AMD64 ABI in its entirety. This simple
9278 // selection mechanism works only for the basic types.
9279 if (ArgVT == MVT::f80) {
9280 llvm_unreachable("va_arg for f80 not yet implemented");
9281 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9282 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9283 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9284 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9285 } else {
9286 llvm_unreachable("Unhandled argument type in LowerVAARG");
9287 }
9288
9289 if (ArgMode == 2) {
9290 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009291 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009292 !(DAG.getMachineFunction()
9293 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009294 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009295 }
9296
9297 // Insert VAARG_64 node into the DAG
9298 // VAARG_64 returns two values: Variable Argument Address, Chain
9299 SmallVector<SDValue, 11> InstOps;
9300 InstOps.push_back(Chain);
9301 InstOps.push_back(SrcPtr);
9302 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9303 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9304 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9305 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9306 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9307 VTs, &InstOps[0], InstOps.size(),
9308 MVT::i64,
9309 MachinePointerInfo(SV),
9310 /*Align=*/0,
9311 /*Volatile=*/false,
9312 /*ReadMem=*/true,
9313 /*WriteMem=*/true);
9314 Chain = VAARG.getValue(1);
9315
9316 // Load the next argument and return it
9317 return DAG.getLoad(ArgVT, dl,
9318 Chain,
9319 VAARG,
9320 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009321 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009322}
9323
Dan Gohmand858e902010-04-17 15:26:15 +00009324SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009325 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009326 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009327 SDValue Chain = Op.getOperand(0);
9328 SDValue DstPtr = Op.getOperand(1);
9329 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009330 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9331 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009332 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009333
Chris Lattnere72f2022010-09-21 05:40:29 +00009334 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009335 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009336 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009337 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009338}
9339
Dan Gohman475871a2008-07-27 21:46:04 +00009340SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009341X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009342 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009343 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009344 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009345 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009346 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009347 case Intrinsic::x86_sse_comieq_ss:
9348 case Intrinsic::x86_sse_comilt_ss:
9349 case Intrinsic::x86_sse_comile_ss:
9350 case Intrinsic::x86_sse_comigt_ss:
9351 case Intrinsic::x86_sse_comige_ss:
9352 case Intrinsic::x86_sse_comineq_ss:
9353 case Intrinsic::x86_sse_ucomieq_ss:
9354 case Intrinsic::x86_sse_ucomilt_ss:
9355 case Intrinsic::x86_sse_ucomile_ss:
9356 case Intrinsic::x86_sse_ucomigt_ss:
9357 case Intrinsic::x86_sse_ucomige_ss:
9358 case Intrinsic::x86_sse_ucomineq_ss:
9359 case Intrinsic::x86_sse2_comieq_sd:
9360 case Intrinsic::x86_sse2_comilt_sd:
9361 case Intrinsic::x86_sse2_comile_sd:
9362 case Intrinsic::x86_sse2_comigt_sd:
9363 case Intrinsic::x86_sse2_comige_sd:
9364 case Intrinsic::x86_sse2_comineq_sd:
9365 case Intrinsic::x86_sse2_ucomieq_sd:
9366 case Intrinsic::x86_sse2_ucomilt_sd:
9367 case Intrinsic::x86_sse2_ucomile_sd:
9368 case Intrinsic::x86_sse2_ucomigt_sd:
9369 case Intrinsic::x86_sse2_ucomige_sd:
9370 case Intrinsic::x86_sse2_ucomineq_sd: {
9371 unsigned Opc = 0;
9372 ISD::CondCode CC = ISD::SETCC_INVALID;
9373 switch (IntNo) {
9374 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009375 case Intrinsic::x86_sse_comieq_ss:
9376 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009377 Opc = X86ISD::COMI;
9378 CC = ISD::SETEQ;
9379 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009380 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009381 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009382 Opc = X86ISD::COMI;
9383 CC = ISD::SETLT;
9384 break;
9385 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009386 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009387 Opc = X86ISD::COMI;
9388 CC = ISD::SETLE;
9389 break;
9390 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009391 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009392 Opc = X86ISD::COMI;
9393 CC = ISD::SETGT;
9394 break;
9395 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009396 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009397 Opc = X86ISD::COMI;
9398 CC = ISD::SETGE;
9399 break;
9400 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009401 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009402 Opc = X86ISD::COMI;
9403 CC = ISD::SETNE;
9404 break;
9405 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009406 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009407 Opc = X86ISD::UCOMI;
9408 CC = ISD::SETEQ;
9409 break;
9410 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009411 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009412 Opc = X86ISD::UCOMI;
9413 CC = ISD::SETLT;
9414 break;
9415 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009416 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009417 Opc = X86ISD::UCOMI;
9418 CC = ISD::SETLE;
9419 break;
9420 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009421 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009422 Opc = X86ISD::UCOMI;
9423 CC = ISD::SETGT;
9424 break;
9425 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009426 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009427 Opc = X86ISD::UCOMI;
9428 CC = ISD::SETGE;
9429 break;
9430 case Intrinsic::x86_sse_ucomineq_ss:
9431 case Intrinsic::x86_sse2_ucomineq_sd:
9432 Opc = X86ISD::UCOMI;
9433 CC = ISD::SETNE;
9434 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009435 }
Evan Cheng734503b2006-09-11 02:19:56 +00009436
Dan Gohman475871a2008-07-27 21:46:04 +00009437 SDValue LHS = Op.getOperand(1);
9438 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009439 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009440 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009441 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9442 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9443 DAG.getConstant(X86CC, MVT::i8), Cond);
9444 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009445 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009446 // Arithmetic intrinsics.
9447 case Intrinsic::x86_sse3_hadd_ps:
9448 case Intrinsic::x86_sse3_hadd_pd:
9449 case Intrinsic::x86_avx_hadd_ps_256:
9450 case Intrinsic::x86_avx_hadd_pd_256:
9451 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9452 Op.getOperand(1), Op.getOperand(2));
9453 case Intrinsic::x86_sse3_hsub_ps:
9454 case Intrinsic::x86_sse3_hsub_pd:
9455 case Intrinsic::x86_avx_hsub_ps_256:
9456 case Intrinsic::x86_avx_hsub_pd_256:
9457 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9458 Op.getOperand(1), Op.getOperand(2));
Craig Topper98fc7292011-11-19 17:46:46 +00009459 case Intrinsic::x86_avx2_psllv_d:
9460 case Intrinsic::x86_avx2_psllv_q:
9461 case Intrinsic::x86_avx2_psllv_d_256:
9462 case Intrinsic::x86_avx2_psllv_q_256:
9463 return DAG.getNode(ISD::SHL, dl, Op.getValueType(),
9464 Op.getOperand(1), Op.getOperand(2));
9465 case Intrinsic::x86_avx2_psrlv_d:
9466 case Intrinsic::x86_avx2_psrlv_q:
9467 case Intrinsic::x86_avx2_psrlv_d_256:
9468 case Intrinsic::x86_avx2_psrlv_q_256:
9469 return DAG.getNode(ISD::SRL, dl, Op.getValueType(),
9470 Op.getOperand(1), Op.getOperand(2));
9471 case Intrinsic::x86_avx2_psrav_d:
9472 case Intrinsic::x86_avx2_psrav_d_256:
9473 return DAG.getNode(ISD::SRA, dl, Op.getValueType(),
9474 Op.getOperand(1), Op.getOperand(2));
9475
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009476 // ptest and testp intrinsics. The intrinsic these come from are designed to
9477 // return an integer value, not just an instruction so lower it to the ptest
9478 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009479 case Intrinsic::x86_sse41_ptestz:
9480 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009481 case Intrinsic::x86_sse41_ptestnzc:
9482 case Intrinsic::x86_avx_ptestz_256:
9483 case Intrinsic::x86_avx_ptestc_256:
9484 case Intrinsic::x86_avx_ptestnzc_256:
9485 case Intrinsic::x86_avx_vtestz_ps:
9486 case Intrinsic::x86_avx_vtestc_ps:
9487 case Intrinsic::x86_avx_vtestnzc_ps:
9488 case Intrinsic::x86_avx_vtestz_pd:
9489 case Intrinsic::x86_avx_vtestc_pd:
9490 case Intrinsic::x86_avx_vtestnzc_pd:
9491 case Intrinsic::x86_avx_vtestz_ps_256:
9492 case Intrinsic::x86_avx_vtestc_ps_256:
9493 case Intrinsic::x86_avx_vtestnzc_ps_256:
9494 case Intrinsic::x86_avx_vtestz_pd_256:
9495 case Intrinsic::x86_avx_vtestc_pd_256:
9496 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9497 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009498 unsigned X86CC = 0;
9499 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009500 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009501 case Intrinsic::x86_avx_vtestz_ps:
9502 case Intrinsic::x86_avx_vtestz_pd:
9503 case Intrinsic::x86_avx_vtestz_ps_256:
9504 case Intrinsic::x86_avx_vtestz_pd_256:
9505 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009506 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009507 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009508 // ZF = 1
9509 X86CC = X86::COND_E;
9510 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009511 case Intrinsic::x86_avx_vtestc_ps:
9512 case Intrinsic::x86_avx_vtestc_pd:
9513 case Intrinsic::x86_avx_vtestc_ps_256:
9514 case Intrinsic::x86_avx_vtestc_pd_256:
9515 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009516 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009517 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009518 // CF = 1
9519 X86CC = X86::COND_B;
9520 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009521 case Intrinsic::x86_avx_vtestnzc_ps:
9522 case Intrinsic::x86_avx_vtestnzc_pd:
9523 case Intrinsic::x86_avx_vtestnzc_ps_256:
9524 case Intrinsic::x86_avx_vtestnzc_pd_256:
9525 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009526 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009527 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009528 // ZF and CF = 0
9529 X86CC = X86::COND_A;
9530 break;
9531 }
Eric Christopherfd179292009-08-27 18:07:15 +00009532
Eric Christopher71c67532009-07-29 00:28:05 +00009533 SDValue LHS = Op.getOperand(1);
9534 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009535 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9536 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009537 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9538 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9539 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009540 }
Evan Cheng5759f972008-05-04 09:15:50 +00009541
9542 // Fix vector shift instructions where the last operand is a non-immediate
9543 // i32 value.
Craig Topper7be5dfd2011-11-12 09:58:49 +00009544 case Intrinsic::x86_avx2_pslli_w:
9545 case Intrinsic::x86_avx2_pslli_d:
9546 case Intrinsic::x86_avx2_pslli_q:
9547 case Intrinsic::x86_avx2_psrli_w:
9548 case Intrinsic::x86_avx2_psrli_d:
9549 case Intrinsic::x86_avx2_psrli_q:
9550 case Intrinsic::x86_avx2_psrai_w:
9551 case Intrinsic::x86_avx2_psrai_d:
Evan Cheng5759f972008-05-04 09:15:50 +00009552 case Intrinsic::x86_sse2_pslli_w:
9553 case Intrinsic::x86_sse2_pslli_d:
9554 case Intrinsic::x86_sse2_pslli_q:
9555 case Intrinsic::x86_sse2_psrli_w:
9556 case Intrinsic::x86_sse2_psrli_d:
9557 case Intrinsic::x86_sse2_psrli_q:
9558 case Intrinsic::x86_sse2_psrai_w:
9559 case Intrinsic::x86_sse2_psrai_d:
9560 case Intrinsic::x86_mmx_pslli_w:
9561 case Intrinsic::x86_mmx_pslli_d:
9562 case Intrinsic::x86_mmx_pslli_q:
9563 case Intrinsic::x86_mmx_psrli_w:
9564 case Intrinsic::x86_mmx_psrli_d:
9565 case Intrinsic::x86_mmx_psrli_q:
9566 case Intrinsic::x86_mmx_psrai_w:
9567 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009568 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009569 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009570 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009571
9572 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009573 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009574 switch (IntNo) {
9575 case Intrinsic::x86_sse2_pslli_w:
9576 NewIntNo = Intrinsic::x86_sse2_psll_w;
9577 break;
9578 case Intrinsic::x86_sse2_pslli_d:
9579 NewIntNo = Intrinsic::x86_sse2_psll_d;
9580 break;
9581 case Intrinsic::x86_sse2_pslli_q:
9582 NewIntNo = Intrinsic::x86_sse2_psll_q;
9583 break;
9584 case Intrinsic::x86_sse2_psrli_w:
9585 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9586 break;
9587 case Intrinsic::x86_sse2_psrli_d:
9588 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9589 break;
9590 case Intrinsic::x86_sse2_psrli_q:
9591 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9592 break;
9593 case Intrinsic::x86_sse2_psrai_w:
9594 NewIntNo = Intrinsic::x86_sse2_psra_w;
9595 break;
9596 case Intrinsic::x86_sse2_psrai_d:
9597 NewIntNo = Intrinsic::x86_sse2_psra_d;
9598 break;
Craig Topper7be5dfd2011-11-12 09:58:49 +00009599 case Intrinsic::x86_avx2_pslli_w:
9600 NewIntNo = Intrinsic::x86_avx2_psll_w;
9601 break;
9602 case Intrinsic::x86_avx2_pslli_d:
9603 NewIntNo = Intrinsic::x86_avx2_psll_d;
9604 break;
9605 case Intrinsic::x86_avx2_pslli_q:
9606 NewIntNo = Intrinsic::x86_avx2_psll_q;
9607 break;
9608 case Intrinsic::x86_avx2_psrli_w:
9609 NewIntNo = Intrinsic::x86_avx2_psrl_w;
9610 break;
9611 case Intrinsic::x86_avx2_psrli_d:
9612 NewIntNo = Intrinsic::x86_avx2_psrl_d;
9613 break;
9614 case Intrinsic::x86_avx2_psrli_q:
9615 NewIntNo = Intrinsic::x86_avx2_psrl_q;
9616 break;
9617 case Intrinsic::x86_avx2_psrai_w:
9618 NewIntNo = Intrinsic::x86_avx2_psra_w;
9619 break;
9620 case Intrinsic::x86_avx2_psrai_d:
9621 NewIntNo = Intrinsic::x86_avx2_psra_d;
9622 break;
Evan Cheng5759f972008-05-04 09:15:50 +00009623 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009624 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009625 switch (IntNo) {
9626 case Intrinsic::x86_mmx_pslli_w:
9627 NewIntNo = Intrinsic::x86_mmx_psll_w;
9628 break;
9629 case Intrinsic::x86_mmx_pslli_d:
9630 NewIntNo = Intrinsic::x86_mmx_psll_d;
9631 break;
9632 case Intrinsic::x86_mmx_pslli_q:
9633 NewIntNo = Intrinsic::x86_mmx_psll_q;
9634 break;
9635 case Intrinsic::x86_mmx_psrli_w:
9636 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9637 break;
9638 case Intrinsic::x86_mmx_psrli_d:
9639 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9640 break;
9641 case Intrinsic::x86_mmx_psrli_q:
9642 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9643 break;
9644 case Intrinsic::x86_mmx_psrai_w:
9645 NewIntNo = Intrinsic::x86_mmx_psra_w;
9646 break;
9647 case Intrinsic::x86_mmx_psrai_d:
9648 NewIntNo = Intrinsic::x86_mmx_psra_d;
9649 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009650 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009651 }
9652 break;
9653 }
9654 }
Mon P Wangefa42202009-09-03 19:56:25 +00009655
9656 // The vector shift intrinsics with scalars uses 32b shift amounts but
9657 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9658 // to be zero.
9659 SDValue ShOps[4];
9660 ShOps[0] = ShAmt;
9661 ShOps[1] = DAG.getConstant(0, MVT::i32);
9662 if (ShAmtVT == MVT::v4i32) {
9663 ShOps[2] = DAG.getUNDEF(MVT::i32);
9664 ShOps[3] = DAG.getUNDEF(MVT::i32);
9665 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9666 } else {
9667 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009668// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009669 }
9670
Owen Andersone50ed302009-08-10 22:56:29 +00009671 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009672 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009673 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009674 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009675 Op.getOperand(1), ShAmt);
9676 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009677 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009678}
Evan Cheng72261582005-12-20 06:22:03 +00009679
Dan Gohmand858e902010-04-17 15:26:15 +00009680SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9681 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009682 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9683 MFI->setReturnAddressIsTaken(true);
9684
Bill Wendling64e87322009-01-16 19:25:27 +00009685 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009686 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009687
9688 if (Depth > 0) {
9689 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9690 SDValue Offset =
9691 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009692 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009693 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009694 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009695 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009696 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009697 }
9698
9699 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009700 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009701 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009702 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009703}
9704
Dan Gohmand858e902010-04-17 15:26:15 +00009705SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009706 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9707 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009708
Owen Andersone50ed302009-08-10 22:56:29 +00009709 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009710 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009711 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9712 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009713 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009714 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009715 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9716 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009717 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009718 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009719}
9720
Dan Gohman475871a2008-07-27 21:46:04 +00009721SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009722 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009723 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009724}
9725
Dan Gohmand858e902010-04-17 15:26:15 +00009726SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009727 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009728 SDValue Chain = Op.getOperand(0);
9729 SDValue Offset = Op.getOperand(1);
9730 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009731 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009732
Dan Gohmand8816272010-08-11 18:14:00 +00009733 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9734 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9735 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009736 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009737
Dan Gohmand8816272010-08-11 18:14:00 +00009738 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9739 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009740 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009741 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9742 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009743 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009744 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009745
Dale Johannesene4d209d2009-02-03 20:21:25 +00009746 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009747 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009748 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009749}
9750
Duncan Sands4a544a72011-09-06 13:37:06 +00009751SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9752 SelectionDAG &DAG) const {
9753 return Op.getOperand(0);
9754}
9755
9756SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9757 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009758 SDValue Root = Op.getOperand(0);
9759 SDValue Trmp = Op.getOperand(1); // trampoline
9760 SDValue FPtr = Op.getOperand(2); // nested function
9761 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009762 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009763
Dan Gohman69de1932008-02-06 22:27:42 +00009764 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009765
9766 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009767 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009768
9769 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009770 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9771 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009772
Evan Cheng0e6a0522011-07-18 20:57:22 +00009773 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9774 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009775
9776 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9777
9778 // Load the pointer to the nested function into R11.
9779 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009780 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009781 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009782 Addr, MachinePointerInfo(TrmpAddr),
9783 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009784
Owen Anderson825b72b2009-08-11 20:47:22 +00009785 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9786 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009787 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9788 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009789 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009790
9791 // Load the 'nest' parameter value into R10.
9792 // R10 is specified in X86CallingConv.td
9793 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009794 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9795 DAG.getConstant(10, MVT::i64));
9796 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009797 Addr, MachinePointerInfo(TrmpAddr, 10),
9798 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009799
Owen Anderson825b72b2009-08-11 20:47:22 +00009800 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9801 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009802 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9803 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009804 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009805
9806 // Jump to the nested function.
9807 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009808 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9809 DAG.getConstant(20, MVT::i64));
9810 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009811 Addr, MachinePointerInfo(TrmpAddr, 20),
9812 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009813
9814 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009815 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9816 DAG.getConstant(22, MVT::i64));
9817 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009818 MachinePointerInfo(TrmpAddr, 22),
9819 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009820
Duncan Sands4a544a72011-09-06 13:37:06 +00009821 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009822 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009823 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009824 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009825 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009826 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009827
9828 switch (CC) {
9829 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009830 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009831 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009832 case CallingConv::X86_StdCall: {
9833 // Pass 'nest' parameter in ECX.
9834 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009835 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009836
9837 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009838 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009839 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009840
Chris Lattner58d74912008-03-12 17:45:29 +00009841 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009842 unsigned InRegCount = 0;
9843 unsigned Idx = 1;
9844
9845 for (FunctionType::param_iterator I = FTy->param_begin(),
9846 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009847 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009848 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009849 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009850
9851 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009852 report_fatal_error("Nest register in use - reduce number of inreg"
9853 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009854 }
9855 }
9856 break;
9857 }
9858 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009859 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009860 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009861 // Pass 'nest' parameter in EAX.
9862 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009863 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009864 break;
9865 }
9866
Dan Gohman475871a2008-07-27 21:46:04 +00009867 SDValue OutChains[4];
9868 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009869
Owen Anderson825b72b2009-08-11 20:47:22 +00009870 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9871 DAG.getConstant(10, MVT::i32));
9872 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009873
Chris Lattnera62fe662010-02-05 19:20:30 +00009874 // This is storing the opcode for MOV32ri.
9875 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009876 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009877 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009878 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009879 Trmp, MachinePointerInfo(TrmpAddr),
9880 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009881
Owen Anderson825b72b2009-08-11 20:47:22 +00009882 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9883 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009884 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9885 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009886 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009887
Chris Lattnera62fe662010-02-05 19:20:30 +00009888 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009889 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9890 DAG.getConstant(5, MVT::i32));
9891 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009892 MachinePointerInfo(TrmpAddr, 5),
9893 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009894
Owen Anderson825b72b2009-08-11 20:47:22 +00009895 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9896 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009897 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9898 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009899 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009900
Duncan Sands4a544a72011-09-06 13:37:06 +00009901 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009902 }
9903}
9904
Dan Gohmand858e902010-04-17 15:26:15 +00009905SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9906 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009907 /*
9908 The rounding mode is in bits 11:10 of FPSR, and has the following
9909 settings:
9910 00 Round to nearest
9911 01 Round to -inf
9912 10 Round to +inf
9913 11 Round to 0
9914
9915 FLT_ROUNDS, on the other hand, expects the following:
9916 -1 Undefined
9917 0 Round to 0
9918 1 Round to nearest
9919 2 Round to +inf
9920 3 Round to -inf
9921
9922 To perform the conversion, we do:
9923 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9924 */
9925
9926 MachineFunction &MF = DAG.getMachineFunction();
9927 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009928 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009929 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009930 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009931 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009932
9933 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009934 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009935 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009936
Michael J. Spencerec38de22010-10-10 22:04:20 +00009937
Chris Lattner2156b792010-09-22 01:11:26 +00009938 MachineMemOperand *MMO =
9939 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9940 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009941
Chris Lattner2156b792010-09-22 01:11:26 +00009942 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9943 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9944 DAG.getVTList(MVT::Other),
9945 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009946
9947 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009948 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009949 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009950
9951 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009952 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009953 DAG.getNode(ISD::SRL, DL, MVT::i16,
9954 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009955 CWD, DAG.getConstant(0x800, MVT::i16)),
9956 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009957 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009958 DAG.getNode(ISD::SRL, DL, MVT::i16,
9959 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009960 CWD, DAG.getConstant(0x400, MVT::i16)),
9961 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009962
Dan Gohman475871a2008-07-27 21:46:04 +00009963 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009964 DAG.getNode(ISD::AND, DL, MVT::i16,
9965 DAG.getNode(ISD::ADD, DL, MVT::i16,
9966 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009967 DAG.getConstant(1, MVT::i16)),
9968 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009969
9970
Duncan Sands83ec4b62008-06-06 12:08:01 +00009971 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009972 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009973}
9974
Dan Gohmand858e902010-04-17 15:26:15 +00009975SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009976 EVT VT = Op.getValueType();
9977 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009978 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009979 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009980
9981 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009982 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009983 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009984 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009985 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009986 }
Evan Cheng18efe262007-12-14 02:13:44 +00009987
Evan Cheng152804e2007-12-14 08:30:15 +00009988 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009989 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009990 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009991
9992 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009993 SDValue Ops[] = {
9994 Op,
9995 DAG.getConstant(NumBits+NumBits-1, OpVT),
9996 DAG.getConstant(X86::COND_E, MVT::i8),
9997 Op.getValue(1)
9998 };
9999 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010000
10001 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010002 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010003
Owen Anderson825b72b2009-08-11 20:47:22 +000010004 if (VT == MVT::i8)
10005 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010006 return Op;
10007}
10008
Dan Gohmand858e902010-04-17 15:26:15 +000010009SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010010 EVT VT = Op.getValueType();
10011 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010012 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010013 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010014
10015 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010016 if (VT == MVT::i8) {
10017 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010018 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010019 }
Evan Cheng152804e2007-12-14 08:30:15 +000010020
10021 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010022 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010023 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010024
10025 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010026 SDValue Ops[] = {
10027 Op,
10028 DAG.getConstant(NumBits, OpVT),
10029 DAG.getConstant(X86::COND_E, MVT::i8),
10030 Op.getValue(1)
10031 };
10032 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010033
Owen Anderson825b72b2009-08-11 20:47:22 +000010034 if (VT == MVT::i8)
10035 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010036 return Op;
10037}
10038
Craig Topper13894fa2011-08-24 06:14:18 +000010039// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10040// ones, and then concatenate the result back.
10041static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010042 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010043
10044 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
10045 "Unsupported value type for operation");
10046
10047 int NumElems = VT.getVectorNumElements();
10048 DebugLoc dl = Op.getDebugLoc();
10049 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10050 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10051
10052 // Extract the LHS vectors
10053 SDValue LHS = Op.getOperand(0);
10054 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10055 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10056
10057 // Extract the RHS vectors
10058 SDValue RHS = Op.getOperand(1);
10059 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
10060 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
10061
10062 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10063 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10064
10065 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10066 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10067 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10068}
10069
10070SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
10071 assert(Op.getValueType().getSizeInBits() == 256 &&
10072 Op.getValueType().isInteger() &&
10073 "Only handle AVX 256-bit vector integer operation");
10074 return Lower256IntArith(Op, DAG);
10075}
10076
10077SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
10078 assert(Op.getValueType().getSizeInBits() == 256 &&
10079 Op.getValueType().isInteger() &&
10080 "Only handle AVX 256-bit vector integer operation");
10081 return Lower256IntArith(Op, DAG);
10082}
10083
10084SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10085 EVT VT = Op.getValueType();
10086
10087 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +000010088 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010089 return Lower256IntArith(Op, DAG);
10090
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010091 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010092
Craig Topperaaa643c2011-11-09 07:28:55 +000010093 SDValue A = Op.getOperand(0);
10094 SDValue B = Op.getOperand(1);
10095
10096 if (VT == MVT::v4i64) {
10097 assert(Subtarget->hasAVX2() && "Lowering v4i64 multiply requires AVX2");
10098
10099 // ulong2 Ahi = __builtin_ia32_psrlqi256( a, 32);
10100 // ulong2 Bhi = __builtin_ia32_psrlqi256( b, 32);
10101 // ulong2 AloBlo = __builtin_ia32_pmuludq256( a, b );
10102 // ulong2 AloBhi = __builtin_ia32_pmuludq256( a, Bhi );
10103 // ulong2 AhiBlo = __builtin_ia32_pmuludq256( Ahi, b );
10104 //
10105 // AloBhi = __builtin_ia32_psllqi256( AloBhi, 32 );
10106 // AhiBlo = __builtin_ia32_psllqi256( AhiBlo, 32 );
10107 // return AloBlo + AloBhi + AhiBlo;
10108
10109 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10110 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10111 A, DAG.getConstant(32, MVT::i32));
10112 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10113 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10114 B, DAG.getConstant(32, MVT::i32));
10115 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10116 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10117 A, B);
10118 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10119 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10120 A, Bhi);
10121 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10122 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10123 Ahi, B);
10124 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10125 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10126 AloBhi, DAG.getConstant(32, MVT::i32));
10127 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10128 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10129 AhiBlo, DAG.getConstant(32, MVT::i32));
10130 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10131 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
10132 return Res;
10133 }
10134
10135 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
10136
Mon P Wangaf9b9522008-12-18 21:42:19 +000010137 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
10138 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
10139 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
10140 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
10141 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
10142 //
10143 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
10144 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
10145 // return AloBlo + AloBhi + AhiBlo;
10146
Dale Johannesene4d209d2009-02-03 20:21:25 +000010147 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010148 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10149 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010150 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010151 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10152 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010153 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010154 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010155 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010156 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010157 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010158 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010159 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010160 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010161 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010162 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010163 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10164 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010165 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010166 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10167 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010168 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10169 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010170 return Res;
10171}
10172
Nadav Rotem43012222011-05-11 08:12:09 +000010173SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10174
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010175 EVT VT = Op.getValueType();
10176 DebugLoc dl = Op.getDebugLoc();
10177 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010178 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010179 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010180
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010181 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010182 return SDValue();
10183
Nadav Rotem43012222011-05-11 08:12:09 +000010184 // Optimize shl/srl/sra with constant shift amount.
10185 if (isSplatVector(Amt.getNode())) {
10186 SDValue SclrAmt = Amt->getOperand(0);
10187 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10188 uint64_t ShiftAmt = C->getZExtValue();
10189
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010190 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SHL) {
10191 // Make a large shift.
10192 SDValue SHL =
10193 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10194 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10195 R, DAG.getConstant(ShiftAmt, MVT::i32));
10196 // Zero out the rightmost bits.
10197 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10198 MVT::i8));
10199 return DAG.getNode(ISD::AND, dl, VT, SHL,
10200 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10201 }
10202
Nadav Rotem43012222011-05-11 08:12:09 +000010203 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
10204 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10205 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10206 R, DAG.getConstant(ShiftAmt, MVT::i32));
10207
10208 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
10209 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10210 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10211 R, DAG.getConstant(ShiftAmt, MVT::i32));
10212
10213 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
10214 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10215 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10216 R, DAG.getConstant(ShiftAmt, MVT::i32));
10217
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010218 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRL) {
10219 // Make a large shift.
10220 SDValue SRL =
10221 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10222 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10223 R, DAG.getConstant(ShiftAmt, MVT::i32));
10224 // Zero out the leftmost bits.
10225 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10226 MVT::i8));
10227 return DAG.getNode(ISD::AND, dl, VT, SRL,
10228 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10229 }
10230
Nadav Rotem43012222011-05-11 08:12:09 +000010231 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
10232 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10233 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10234 R, DAG.getConstant(ShiftAmt, MVT::i32));
10235
10236 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
10237 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10238 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
10239 R, DAG.getConstant(ShiftAmt, MVT::i32));
10240
10241 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
10242 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10243 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10244 R, DAG.getConstant(ShiftAmt, MVT::i32));
10245
10246 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
10247 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10248 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
10249 R, DAG.getConstant(ShiftAmt, MVT::i32));
10250
10251 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
10252 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10253 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
10254 R, DAG.getConstant(ShiftAmt, MVT::i32));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010255
10256 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRA) {
10257 if (ShiftAmt == 7) {
10258 // R s>> 7 === R s< 0
10259 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10260 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10261 }
10262
10263 // R s>> a === ((R u>> a) ^ m) - m
10264 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10265 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10266 MVT::i8));
10267 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10268 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10269 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10270 return Res;
10271 }
Craig Topper46154eb2011-11-11 07:39:23 +000010272
Craig Topper0d86d462011-11-20 00:12:05 +000010273 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10274 if (Op.getOpcode() == ISD::SHL) {
10275 // Make a large shift.
10276 SDValue SHL =
10277 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10278 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
10279 R, DAG.getConstant(ShiftAmt, MVT::i32));
10280 // Zero out the rightmost bits.
10281 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10282 MVT::i8));
10283 return DAG.getNode(ISD::AND, dl, VT, SHL,
10284 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010285 }
Craig Topper0d86d462011-11-20 00:12:05 +000010286 if (Op.getOpcode() == ISD::SRL) {
10287 // Make a large shift.
10288 SDValue SRL =
10289 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10290 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
10291 R, DAG.getConstant(ShiftAmt, MVT::i32));
10292 // Zero out the leftmost bits.
10293 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10294 MVT::i8));
10295 return DAG.getNode(ISD::AND, dl, VT, SRL,
10296 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10297 }
10298 if (Op.getOpcode() == ISD::SRA) {
10299 if (ShiftAmt == 7) {
10300 // R s>> 7 === R s< 0
10301 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10302 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10303 }
10304
10305 // R s>> a === ((R u>> a) ^ m) - m
10306 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10307 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10308 MVT::i8));
10309 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10310 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10311 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10312 return Res;
10313 }
10314 }
Nadav Rotem43012222011-05-11 08:12:09 +000010315 }
10316 }
10317
10318 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010319 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010320 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10321 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10322 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
10323
10324 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010325
Nate Begeman51409212010-07-28 00:21:48 +000010326 std::vector<Constant*> CV(4, CI);
10327 Constant *C = ConstantVector::get(CV);
10328 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10329 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010330 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010331 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010332
10333 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010334 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010335 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10336 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10337 }
Nadav Rotem43012222011-05-11 08:12:09 +000010338 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010339 // a = a << 5;
10340 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10341 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10342 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
10343
10344 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
10345 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
10346
10347 std::vector<Constant*> CVM1(16, CM1);
10348 std::vector<Constant*> CVM2(16, CM2);
10349 Constant *C = ConstantVector::get(CVM1);
10350 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10351 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010352 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010353 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010354
10355 // r = pblendv(r, psllw(r & (char16)15, 4), a);
10356 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10357 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10358 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10359 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010360 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010361 // a += a
10362 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010363
Nate Begeman51409212010-07-28 00:21:48 +000010364 C = ConstantVector::get(CVM2);
10365 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10366 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010367 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010368 false, false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010369
Nate Begeman51409212010-07-28 00:21:48 +000010370 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10371 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10372 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10373 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10374 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010375 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010376 // a += a
10377 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010378
Nate Begeman51409212010-07-28 00:21:48 +000010379 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010380 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10381 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010382 return R;
10383 }
Craig Topper46154eb2011-11-11 07:39:23 +000010384
10385 // Decompose 256-bit shifts into smaller 128-bit shifts.
10386 if (VT.getSizeInBits() == 256) {
10387 int NumElems = VT.getVectorNumElements();
10388 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10389 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10390
10391 // Extract the two vectors
10392 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10393 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10394 DAG, dl);
10395
10396 // Recreate the shift amount vectors
10397 SDValue Amt1, Amt2;
10398 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10399 // Constant shift amount
10400 SmallVector<SDValue, 4> Amt1Csts;
10401 SmallVector<SDValue, 4> Amt2Csts;
10402 for (int i = 0; i < NumElems/2; ++i)
10403 Amt1Csts.push_back(Amt->getOperand(i));
10404 for (int i = NumElems/2; i < NumElems; ++i)
10405 Amt2Csts.push_back(Amt->getOperand(i));
10406
10407 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10408 &Amt1Csts[0], NumElems/2);
10409 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10410 &Amt2Csts[0], NumElems/2);
10411 } else {
10412 // Variable shift amount
10413 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10414 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10415 DAG, dl);
10416 }
10417
10418 // Issue new vector shifts for the smaller types
10419 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10420 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10421
10422 // Concatenate the result back
10423 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10424 }
10425
Nate Begeman51409212010-07-28 00:21:48 +000010426 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010427}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010428
Dan Gohmand858e902010-04-17 15:26:15 +000010429SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010430 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10431 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010432 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10433 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010434 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010435 SDValue LHS = N->getOperand(0);
10436 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010437 unsigned BaseOp = 0;
10438 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010439 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010440 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010441 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010442 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010443 // A subtract of one will be selected as a INC. Note that INC doesn't
10444 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010445 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10446 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010447 BaseOp = X86ISD::INC;
10448 Cond = X86::COND_O;
10449 break;
10450 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010451 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010452 Cond = X86::COND_O;
10453 break;
10454 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010455 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010456 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010457 break;
10458 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010459 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10460 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010461 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10462 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010463 BaseOp = X86ISD::DEC;
10464 Cond = X86::COND_O;
10465 break;
10466 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010467 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010468 Cond = X86::COND_O;
10469 break;
10470 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010471 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010472 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010473 break;
10474 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010475 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010476 Cond = X86::COND_O;
10477 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010478 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10479 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10480 MVT::i32);
10481 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010482
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010483 SDValue SetCC =
10484 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10485 DAG.getConstant(X86::COND_O, MVT::i32),
10486 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010487
Dan Gohman6e5fda22011-07-22 18:45:15 +000010488 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010489 }
Bill Wendling74c37652008-12-09 22:08:41 +000010490 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010491
Bill Wendling61edeb52008-12-02 01:06:39 +000010492 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010493 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010494 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010495
Bill Wendling61edeb52008-12-02 01:06:39 +000010496 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010497 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10498 DAG.getConstant(Cond, MVT::i32),
10499 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010500
Dan Gohman6e5fda22011-07-22 18:45:15 +000010501 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010502}
10503
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010504SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10505 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010506 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10507 EVT VT = Op.getValueType();
10508
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010509 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010510 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10511 ExtraVT.getScalarType().getSizeInBits();
10512 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10513
10514 unsigned SHLIntrinsicsID = 0;
10515 unsigned SRAIntrinsicsID = 0;
10516 switch (VT.getSimpleVT().SimpleTy) {
10517 default:
10518 return SDValue();
Craig Toppera124f942011-11-21 01:12:36 +000010519 case MVT::v4i32:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010520 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10521 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10522 break;
Craig Toppera124f942011-11-21 01:12:36 +000010523 case MVT::v8i16:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010524 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10525 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10526 break;
Craig Toppera124f942011-11-21 01:12:36 +000010527 case MVT::v8i32:
10528 case MVT::v16i16:
10529 if (!Subtarget->hasAVX())
10530 return SDValue();
10531 if (!Subtarget->hasAVX2()) {
10532 // needs to be split
10533 int NumElems = VT.getVectorNumElements();
10534 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10535 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10536
10537 // Extract the LHS vectors
10538 SDValue LHS = Op.getOperand(0);
10539 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10540 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10541
10542 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10543 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10544
10545 EVT ExtraEltVT = ExtraVT.getVectorElementType();
10546 int ExtraNumElems = ExtraVT.getVectorNumElements();
10547 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10548 ExtraNumElems/2);
10549 SDValue Extra = DAG.getValueType(ExtraVT);
10550
10551 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10552 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
10553
10554 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10555 }
10556 if (VT == MVT::v8i32) {
10557 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_d;
10558 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_d;
10559 } else {
10560 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_w;
10561 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_w;
10562 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010563 }
10564
10565 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10566 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
Craig Toppera124f942011-11-21 01:12:36 +000010567 Op.getOperand(0), ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010568
Nadav Rotema7934dd2011-10-10 19:31:45 +000010569 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10570 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10571 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010572 }
10573
10574 return SDValue();
10575}
10576
10577
Eric Christopher9a9d2752010-07-22 02:48:34 +000010578SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10579 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010580
Eric Christopher77ed1352011-07-08 00:04:56 +000010581 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10582 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010583 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010584 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010585 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010586 SDValue Ops[] = {
10587 DAG.getRegister(X86::ESP, MVT::i32), // Base
10588 DAG.getTargetConstant(1, MVT::i8), // Scale
10589 DAG.getRegister(0, MVT::i32), // Index
10590 DAG.getTargetConstant(0, MVT::i32), // Disp
10591 DAG.getRegister(0, MVT::i32), // Segment.
10592 Zero,
10593 Chain
10594 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010595 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010596 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10597 array_lengthof(Ops));
10598 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010599 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010600
Eric Christopher9a9d2752010-07-22 02:48:34 +000010601 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010602 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010603 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010604
Chris Lattner132929a2010-08-14 17:26:09 +000010605 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10606 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10607 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10608 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010609
Chris Lattner132929a2010-08-14 17:26:09 +000010610 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10611 if (!Op1 && !Op2 && !Op3 && Op4)
10612 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010613
Chris Lattner132929a2010-08-14 17:26:09 +000010614 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10615 if (Op1 && !Op2 && !Op3 && !Op4)
10616 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010617
10618 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010619 // (MFENCE)>;
10620 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010621}
10622
Eli Friedman14648462011-07-27 22:21:52 +000010623SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10624 SelectionDAG &DAG) const {
10625 DebugLoc dl = Op.getDebugLoc();
10626 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10627 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10628 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10629 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10630
10631 // The only fence that needs an instruction is a sequentially-consistent
10632 // cross-thread fence.
10633 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10634 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10635 // no-sse2). There isn't any reason to disable it if the target processor
10636 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010637 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010638 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10639
10640 SDValue Chain = Op.getOperand(0);
10641 SDValue Zero = DAG.getConstant(0, MVT::i32);
10642 SDValue Ops[] = {
10643 DAG.getRegister(X86::ESP, MVT::i32), // Base
10644 DAG.getTargetConstant(1, MVT::i8), // Scale
10645 DAG.getRegister(0, MVT::i32), // Index
10646 DAG.getTargetConstant(0, MVT::i32), // Disp
10647 DAG.getRegister(0, MVT::i32), // Segment.
10648 Zero,
10649 Chain
10650 };
10651 SDNode *Res =
10652 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10653 array_lengthof(Ops));
10654 return SDValue(Res, 0);
10655 }
10656
10657 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10658 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10659}
10660
10661
Dan Gohmand858e902010-04-17 15:26:15 +000010662SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010663 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010664 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010665 unsigned Reg = 0;
10666 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010667 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010668 default:
10669 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010670 case MVT::i8: Reg = X86::AL; size = 1; break;
10671 case MVT::i16: Reg = X86::AX; size = 2; break;
10672 case MVT::i32: Reg = X86::EAX; size = 4; break;
10673 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010674 assert(Subtarget->is64Bit() && "Node not type legal!");
10675 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010676 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010677 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010678 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010679 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010680 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010681 Op.getOperand(1),
10682 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010683 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010684 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010685 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010686 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10687 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10688 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010689 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010690 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010691 return cpOut;
10692}
10693
Duncan Sands1607f052008-12-01 11:39:25 +000010694SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010695 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010696 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010697 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010698 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010699 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010700 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010701 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10702 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010703 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010704 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10705 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010706 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010707 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010708 rdx.getValue(1)
10709 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010710 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010711}
10712
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010713SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010714 SelectionDAG &DAG) const {
10715 EVT SrcVT = Op.getOperand(0).getValueType();
10716 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010717 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010718 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010719 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010720 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010721 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010722 // i64 <=> MMX conversions are Legal.
10723 if (SrcVT==MVT::i64 && DstVT.isVector())
10724 return Op;
10725 if (DstVT==MVT::i64 && SrcVT.isVector())
10726 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010727 // MMX <=> MMX conversions are Legal.
10728 if (SrcVT.isVector() && DstVT.isVector())
10729 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010730 // All other conversions need to be expanded.
10731 return SDValue();
10732}
Chris Lattner5b856542010-12-20 00:59:46 +000010733
Dan Gohmand858e902010-04-17 15:26:15 +000010734SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010735 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010736 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010737 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010738 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010739 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010740 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010741 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010742 Node->getOperand(0),
10743 Node->getOperand(1), negOp,
10744 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010745 cast<AtomicSDNode>(Node)->getAlignment(),
10746 cast<AtomicSDNode>(Node)->getOrdering(),
10747 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010748}
10749
Eli Friedman327236c2011-08-24 20:50:09 +000010750static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10751 SDNode *Node = Op.getNode();
10752 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010753 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010754
10755 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010756 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10757 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10758 // (The only way to get a 16-byte store is cmpxchg16b)
10759 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10760 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10761 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010762 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10763 cast<AtomicSDNode>(Node)->getMemoryVT(),
10764 Node->getOperand(0),
10765 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010766 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010767 cast<AtomicSDNode>(Node)->getOrdering(),
10768 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010769 return Swap.getValue(1);
10770 }
10771 // Other atomic stores have a simple pattern.
10772 return Op;
10773}
10774
Chris Lattner5b856542010-12-20 00:59:46 +000010775static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10776 EVT VT = Op.getNode()->getValueType(0);
10777
10778 // Let legalize expand this if it isn't a legal type yet.
10779 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10780 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010781
Chris Lattner5b856542010-12-20 00:59:46 +000010782 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010783
Chris Lattner5b856542010-12-20 00:59:46 +000010784 unsigned Opc;
10785 bool ExtraOp = false;
10786 switch (Op.getOpcode()) {
10787 default: assert(0 && "Invalid code");
10788 case ISD::ADDC: Opc = X86ISD::ADD; break;
10789 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10790 case ISD::SUBC: Opc = X86ISD::SUB; break;
10791 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10792 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010793
Chris Lattner5b856542010-12-20 00:59:46 +000010794 if (!ExtraOp)
10795 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10796 Op.getOperand(1));
10797 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10798 Op.getOperand(1), Op.getOperand(2));
10799}
10800
Evan Cheng0db9fe62006-04-25 20:13:52 +000010801/// LowerOperation - Provide custom lowering hooks for some operations.
10802///
Dan Gohmand858e902010-04-17 15:26:15 +000010803SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010804 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010805 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010806 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010807 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010808 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010809 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10810 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010811 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010812 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010813 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010814 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10815 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10816 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010817 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010818 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010819 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10820 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10821 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010822 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010823 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010824 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010825 case ISD::SHL_PARTS:
10826 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010827 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010828 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010829 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010830 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010831 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010832 case ISD::FABS: return LowerFABS(Op, DAG);
10833 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010834 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010835 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010836 case ISD::SETCC: return LowerSETCC(Op, DAG);
10837 case ISD::SELECT: return LowerSELECT(Op, DAG);
10838 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010839 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010840 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010841 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010842 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010843 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010844 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10845 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010846 case ISD::FRAME_TO_ARGS_OFFSET:
10847 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010848 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010849 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010850 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10851 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010852 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010853 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10854 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010855 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010856 case ISD::SRA:
10857 case ISD::SRL:
10858 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010859 case ISD::SADDO:
10860 case ISD::UADDO:
10861 case ISD::SSUBO:
10862 case ISD::USUBO:
10863 case ISD::SMULO:
10864 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010865 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010866 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010867 case ISD::ADDC:
10868 case ISD::ADDE:
10869 case ISD::SUBC:
10870 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010871 case ISD::ADD: return LowerADD(Op, DAG);
10872 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010873 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010874}
10875
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010876static void ReplaceATOMIC_LOAD(SDNode *Node,
10877 SmallVectorImpl<SDValue> &Results,
10878 SelectionDAG &DAG) {
10879 DebugLoc dl = Node->getDebugLoc();
10880 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10881
10882 // Convert wide load -> cmpxchg8b/cmpxchg16b
10883 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10884 // (The only way to get a 16-byte load is cmpxchg16b)
10885 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010886 SDValue Zero = DAG.getConstant(0, VT);
10887 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010888 Node->getOperand(0),
10889 Node->getOperand(1), Zero, Zero,
10890 cast<AtomicSDNode>(Node)->getMemOperand(),
10891 cast<AtomicSDNode>(Node)->getOrdering(),
10892 cast<AtomicSDNode>(Node)->getSynchScope());
10893 Results.push_back(Swap.getValue(0));
10894 Results.push_back(Swap.getValue(1));
10895}
10896
Duncan Sands1607f052008-12-01 11:39:25 +000010897void X86TargetLowering::
10898ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010899 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010900 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000010901 assert (Node->getValueType(0) == MVT::i64 &&
10902 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010903
10904 SDValue Chain = Node->getOperand(0);
10905 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010906 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010907 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010908 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010909 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010910 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010911 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010912 SDValue Result =
10913 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10914 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010915 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010916 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010917 Results.push_back(Result.getValue(2));
10918}
10919
Duncan Sands126d9072008-07-04 11:47:58 +000010920/// ReplaceNodeResults - Replace a node with an illegal result type
10921/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010922void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10923 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010924 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010925 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010926 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010927 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010928 assert(false && "Do not know how to custom type legalize this operation!");
10929 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010930 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010931 case ISD::ADDC:
10932 case ISD::ADDE:
10933 case ISD::SUBC:
10934 case ISD::SUBE:
10935 // We don't want to expand or promote these.
10936 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010937 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010938 std::pair<SDValue,SDValue> Vals =
10939 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010940 SDValue FIST = Vals.first, StackSlot = Vals.second;
10941 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010942 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010943 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010944 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010945 MachinePointerInfo(),
10946 false, false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010947 }
10948 return;
10949 }
10950 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010951 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010952 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010953 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010954 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010955 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010956 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010957 eax.getValue(2));
10958 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10959 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010960 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010961 Results.push_back(edx.getValue(1));
10962 return;
10963 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010964 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010965 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010966 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010967 bool Regs64bit = T == MVT::i128;
10968 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010969 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010970 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10971 DAG.getConstant(0, HalfT));
10972 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10973 DAG.getConstant(1, HalfT));
10974 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10975 Regs64bit ? X86::RAX : X86::EAX,
10976 cpInL, SDValue());
10977 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10978 Regs64bit ? X86::RDX : X86::EDX,
10979 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010980 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010981 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10982 DAG.getConstant(0, HalfT));
10983 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10984 DAG.getConstant(1, HalfT));
10985 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10986 Regs64bit ? X86::RBX : X86::EBX,
10987 swapInL, cpInH.getValue(1));
10988 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10989 Regs64bit ? X86::RCX : X86::ECX,
10990 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010991 SDValue Ops[] = { swapInH.getValue(0),
10992 N->getOperand(1),
10993 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010994 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010995 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010996 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10997 X86ISD::LCMPXCHG8_DAG;
10998 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010999 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011000 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11001 Regs64bit ? X86::RAX : X86::EAX,
11002 HalfT, Result.getValue(1));
11003 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11004 Regs64bit ? X86::RDX : X86::EDX,
11005 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011006 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011007 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011008 Results.push_back(cpOutH.getValue(1));
11009 return;
11010 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011011 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000011012 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
11013 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011014 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000011015 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
11016 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011017 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000011018 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
11019 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011020 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000011021 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
11022 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011023 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000011024 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
11025 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011026 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000011027 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
11028 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011029 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000011030 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
11031 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011032 case ISD::ATOMIC_LOAD:
11033 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011034 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011035}
11036
Evan Cheng72261582005-12-20 06:22:03 +000011037const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11038 switch (Opcode) {
11039 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011040 case X86ISD::BSF: return "X86ISD::BSF";
11041 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011042 case X86ISD::SHLD: return "X86ISD::SHLD";
11043 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011044 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011045 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011046 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011047 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011048 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011049 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011050 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11051 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11052 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011053 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011054 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011055 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011056 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011057 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011058 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011059 case X86ISD::COMI: return "X86ISD::COMI";
11060 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011061 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011062 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011063 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11064 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011065 case X86ISD::CMOV: return "X86ISD::CMOV";
11066 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011067 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011068 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11069 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011070 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011071 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011072 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011073 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011074 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011075 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11076 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011077 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011078 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011079 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011080 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011081 case X86ISD::BLENDV: return "X86ISD::BLENDV";
11082 case X86ISD::FHADD: return "X86ISD::FHADD";
11083 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011084 case X86ISD::FMAX: return "X86ISD::FMAX";
11085 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000011086 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11087 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011088 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011089 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011090 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011091 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011092 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011093 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11094 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011095 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11096 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11097 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11098 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11099 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11100 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011101 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
11102 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000011103 case X86ISD::VSHL: return "X86ISD::VSHL";
11104 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000011105 case X86ISD::CMPPD: return "X86ISD::CMPPD";
11106 case X86ISD::CMPPS: return "X86ISD::CMPPS";
11107 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
11108 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
11109 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
11110 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
11111 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
11112 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
11113 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
11114 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011115 case X86ISD::ADD: return "X86ISD::ADD";
11116 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011117 case X86ISD::ADC: return "X86ISD::ADC";
11118 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011119 case X86ISD::SMUL: return "X86ISD::SMUL";
11120 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011121 case X86ISD::INC: return "X86ISD::INC";
11122 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011123 case X86ISD::OR: return "X86ISD::OR";
11124 case X86ISD::XOR: return "X86ISD::XOR";
11125 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011126 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011127 case X86ISD::BLSI: return "X86ISD::BLSI";
11128 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11129 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011130 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011131 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011132 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011133 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11134 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11135 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
11136 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
11137 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
11138 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
11139 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
11140 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
11141 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011142 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011143 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011144 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011145 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11146 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011147 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11148 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11149 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
11150 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
11151 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
11152 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11153 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper06cb6802011-11-26 20:47:44 +000011154 case X86ISD::UNPCKLP: return "X86ISD::UNPCKLP";
11155 case X86ISD::UNPCKHP: return "X86ISD::UNPCKHP";
11156 case X86ISD::PUNPCKL: return "X86ISD::PUNPCKL";
11157 case X86ISD::PUNPCKH: return "X86ISD::PUNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011158 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011159 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000011160 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Craig Topper70b883b2011-11-28 10:14:51 +000011161 case X86ISD::VPERM2I128: return "X86ISD::VPERM2I128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011162 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011163 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011164 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011165 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011166 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000011167 }
11168}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011169
Chris Lattnerc9addb72007-03-30 23:15:24 +000011170// isLegalAddressingMode - Return true if the addressing mode represented
11171// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011172bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011173 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011174 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011175 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011176 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011177
Chris Lattnerc9addb72007-03-30 23:15:24 +000011178 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011179 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011180 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011181
Chris Lattnerc9addb72007-03-30 23:15:24 +000011182 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011183 unsigned GVFlags =
11184 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011185
Chris Lattnerdfed4132009-07-10 07:38:24 +000011186 // If a reference to this global requires an extra load, we can't fold it.
11187 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011188 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011189
Chris Lattnerdfed4132009-07-10 07:38:24 +000011190 // If BaseGV requires a register for the PIC base, we cannot also have a
11191 // BaseReg specified.
11192 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011193 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011194
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011195 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011196 if ((M != CodeModel::Small || R != Reloc::Static) &&
11197 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011198 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011199 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011200
Chris Lattnerc9addb72007-03-30 23:15:24 +000011201 switch (AM.Scale) {
11202 case 0:
11203 case 1:
11204 case 2:
11205 case 4:
11206 case 8:
11207 // These scales always work.
11208 break;
11209 case 3:
11210 case 5:
11211 case 9:
11212 // These scales are formed with basereg+scalereg. Only accept if there is
11213 // no basereg yet.
11214 if (AM.HasBaseReg)
11215 return false;
11216 break;
11217 default: // Other stuff never works.
11218 return false;
11219 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011220
Chris Lattnerc9addb72007-03-30 23:15:24 +000011221 return true;
11222}
11223
11224
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011225bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011226 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011227 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011228 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11229 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011230 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011231 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011232 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011233}
11234
Owen Andersone50ed302009-08-10 22:56:29 +000011235bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011236 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011237 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011238 unsigned NumBits1 = VT1.getSizeInBits();
11239 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011240 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011241 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011242 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011243}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011244
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011245bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011246 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011247 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011248}
11249
Owen Andersone50ed302009-08-10 22:56:29 +000011250bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011251 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011252 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011253}
11254
Owen Andersone50ed302009-08-10 22:56:29 +000011255bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011256 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011257 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011258}
11259
Evan Cheng60c07e12006-07-05 22:17:51 +000011260/// isShuffleMaskLegal - Targets can use this to indicate that they only
11261/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11262/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11263/// are assumed to be legal.
11264bool
Eric Christopherfd179292009-08-27 18:07:15 +000011265X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011266 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011267 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011268 if (VT.getSizeInBits() == 64)
Craig Topperc0d82852011-11-22 00:44:41 +000011269 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX());
Nate Begeman9008ca62009-04-27 18:41:29 +000011270
Nate Begemana09008b2009-10-19 02:17:23 +000011271 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011272 return (VT.getVectorNumElements() == 2 ||
11273 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11274 isMOVLMask(M, VT) ||
11275 isSHUFPMask(M, VT) ||
11276 isPSHUFDMask(M, VT) ||
11277 isPSHUFHWMask(M, VT) ||
11278 isPSHUFLWMask(M, VT) ||
Craig Topperc0d82852011-11-22 00:44:41 +000011279 isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX()) ||
Craig Topper6347e862011-11-21 06:57:39 +000011280 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11281 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011282 isUNPCKL_v_undef_Mask(M, VT) ||
11283 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011284}
11285
Dan Gohman7d8143f2008-04-09 20:09:42 +000011286bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011287X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011288 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011289 unsigned NumElts = VT.getVectorNumElements();
11290 // FIXME: This collection of masks seems suspect.
11291 if (NumElts == 2)
11292 return true;
11293 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11294 return (isMOVLMask(Mask, VT) ||
11295 isCommutedMOVLMask(Mask, VT, true) ||
11296 isSHUFPMask(Mask, VT) ||
11297 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011298 }
11299 return false;
11300}
11301
11302//===----------------------------------------------------------------------===//
11303// X86 Scheduler Hooks
11304//===----------------------------------------------------------------------===//
11305
Mon P Wang63307c32008-05-05 19:05:59 +000011306// private utility function
11307MachineBasicBlock *
11308X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11309 MachineBasicBlock *MBB,
11310 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011311 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011312 unsigned LoadOpc,
11313 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011314 unsigned notOpc,
11315 unsigned EAXreg,
11316 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011317 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011318 // For the atomic bitwise operator, we generate
11319 // thisMBB:
11320 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011321 // ld t1 = [bitinstr.addr]
11322 // op t2 = t1, [bitinstr.val]
11323 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011324 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11325 // bz newMBB
11326 // fallthrough -->nextMBB
11327 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11328 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011329 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011330 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011331
Mon P Wang63307c32008-05-05 19:05:59 +000011332 /// First build the CFG
11333 MachineFunction *F = MBB->getParent();
11334 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011335 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11336 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11337 F->insert(MBBIter, newMBB);
11338 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011339
Dan Gohman14152b42010-07-06 20:24:04 +000011340 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11341 nextMBB->splice(nextMBB->begin(), thisMBB,
11342 llvm::next(MachineBasicBlock::iterator(bInstr)),
11343 thisMBB->end());
11344 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011345
Mon P Wang63307c32008-05-05 19:05:59 +000011346 // Update thisMBB to fall through to newMBB
11347 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011348
Mon P Wang63307c32008-05-05 19:05:59 +000011349 // newMBB jumps to itself and fall through to nextMBB
11350 newMBB->addSuccessor(nextMBB);
11351 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011352
Mon P Wang63307c32008-05-05 19:05:59 +000011353 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011354 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011355 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011356 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011357 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011358 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011359 int numArgs = bInstr->getNumOperands() - 1;
11360 for (int i=0; i < numArgs; ++i)
11361 argOpers[i] = &bInstr->getOperand(i+1);
11362
11363 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011364 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011365 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011366
Dale Johannesen140be2d2008-08-19 18:47:28 +000011367 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011368 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011369 for (int i=0; i <= lastAddrIndx; ++i)
11370 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011371
Dale Johannesen140be2d2008-08-19 18:47:28 +000011372 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011373 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011374 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011375 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011376 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011377 tt = t1;
11378
Dale Johannesen140be2d2008-08-19 18:47:28 +000011379 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011380 assert((argOpers[valArgIndx]->isReg() ||
11381 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011382 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011383 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011384 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011385 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011386 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011387 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011388 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011389
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011390 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011391 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011392
Dale Johannesene4d209d2009-02-03 20:21:25 +000011393 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011394 for (int i=0; i <= lastAddrIndx; ++i)
11395 (*MIB).addOperand(*argOpers[i]);
11396 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011397 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011398 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11399 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011400
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011401 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011402 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011403
Mon P Wang63307c32008-05-05 19:05:59 +000011404 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011405 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011406
Dan Gohman14152b42010-07-06 20:24:04 +000011407 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011408 return nextMBB;
11409}
11410
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011411// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011412MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011413X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11414 MachineBasicBlock *MBB,
11415 unsigned regOpcL,
11416 unsigned regOpcH,
11417 unsigned immOpcL,
11418 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011419 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011420 // For the atomic bitwise operator, we generate
11421 // thisMBB (instructions are in pairs, except cmpxchg8b)
11422 // ld t1,t2 = [bitinstr.addr]
11423 // newMBB:
11424 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11425 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011426 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011427 // mov ECX, EBX <- t5, t6
11428 // mov EAX, EDX <- t1, t2
11429 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11430 // mov t3, t4 <- EAX, EDX
11431 // bz newMBB
11432 // result in out1, out2
11433 // fallthrough -->nextMBB
11434
11435 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11436 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011437 const unsigned NotOpc = X86::NOT32r;
11438 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11439 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11440 MachineFunction::iterator MBBIter = MBB;
11441 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011442
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011443 /// First build the CFG
11444 MachineFunction *F = MBB->getParent();
11445 MachineBasicBlock *thisMBB = MBB;
11446 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11447 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11448 F->insert(MBBIter, newMBB);
11449 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011450
Dan Gohman14152b42010-07-06 20:24:04 +000011451 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11452 nextMBB->splice(nextMBB->begin(), thisMBB,
11453 llvm::next(MachineBasicBlock::iterator(bInstr)),
11454 thisMBB->end());
11455 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011456
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011457 // Update thisMBB to fall through to newMBB
11458 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011459
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011460 // newMBB jumps to itself and fall through to nextMBB
11461 newMBB->addSuccessor(nextMBB);
11462 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011463
Dale Johannesene4d209d2009-02-03 20:21:25 +000011464 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011465 // Insert instructions into newMBB based on incoming instruction
11466 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011467 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011468 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011469 MachineOperand& dest1Oper = bInstr->getOperand(0);
11470 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011471 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11472 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011473 argOpers[i] = &bInstr->getOperand(i+2);
11474
Dan Gohman71ea4e52010-05-14 21:01:44 +000011475 // We use some of the operands multiple times, so conservatively just
11476 // clear any kill flags that might be present.
11477 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11478 argOpers[i]->setIsKill(false);
11479 }
11480
Evan Chengad5b52f2010-01-08 19:14:57 +000011481 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011482 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011483
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011484 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011485 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011486 for (int i=0; i <= lastAddrIndx; ++i)
11487 (*MIB).addOperand(*argOpers[i]);
11488 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011489 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011490 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011491 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011492 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011493 MachineOperand newOp3 = *(argOpers[3]);
11494 if (newOp3.isImm())
11495 newOp3.setImm(newOp3.getImm()+4);
11496 else
11497 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011498 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011499 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011500
11501 // t3/4 are defined later, at the bottom of the loop
11502 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11503 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011504 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011505 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011506 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011507 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11508
Evan Cheng306b4ca2010-01-08 23:41:50 +000011509 // The subsequent operations should be using the destination registers of
11510 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011511 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011512 t1 = F->getRegInfo().createVirtualRegister(RC);
11513 t2 = F->getRegInfo().createVirtualRegister(RC);
11514 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11515 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011516 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011517 t1 = dest1Oper.getReg();
11518 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011519 }
11520
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011521 int valArgIndx = lastAddrIndx + 1;
11522 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011523 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011524 "invalid operand");
11525 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11526 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011527 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011528 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011529 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011530 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011531 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011532 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011533 (*MIB).addOperand(*argOpers[valArgIndx]);
11534 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011535 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011536 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011537 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011538 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011539 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011540 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011541 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011542 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011543 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011544 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011545
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011546 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011547 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011548 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011549 MIB.addReg(t2);
11550
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011551 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011552 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011553 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011554 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011555
Dale Johannesene4d209d2009-02-03 20:21:25 +000011556 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011557 for (int i=0; i <= lastAddrIndx; ++i)
11558 (*MIB).addOperand(*argOpers[i]);
11559
11560 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011561 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11562 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011563
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011564 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011565 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011566 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011567 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011568
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011569 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011570 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011571
Dan Gohman14152b42010-07-06 20:24:04 +000011572 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011573 return nextMBB;
11574}
11575
11576// private utility function
11577MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011578X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11579 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011580 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011581 // For the atomic min/max operator, we generate
11582 // thisMBB:
11583 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011584 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011585 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011586 // cmp t1, t2
11587 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011588 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011589 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11590 // bz newMBB
11591 // fallthrough -->nextMBB
11592 //
11593 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11594 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011595 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011596 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011597
Mon P Wang63307c32008-05-05 19:05:59 +000011598 /// First build the CFG
11599 MachineFunction *F = MBB->getParent();
11600 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011601 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11602 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11603 F->insert(MBBIter, newMBB);
11604 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011605
Dan Gohman14152b42010-07-06 20:24:04 +000011606 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11607 nextMBB->splice(nextMBB->begin(), thisMBB,
11608 llvm::next(MachineBasicBlock::iterator(mInstr)),
11609 thisMBB->end());
11610 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011611
Mon P Wang63307c32008-05-05 19:05:59 +000011612 // Update thisMBB to fall through to newMBB
11613 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011614
Mon P Wang63307c32008-05-05 19:05:59 +000011615 // newMBB jumps to newMBB and fall through to nextMBB
11616 newMBB->addSuccessor(nextMBB);
11617 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011618
Dale Johannesene4d209d2009-02-03 20:21:25 +000011619 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011620 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011621 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011622 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011623 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011624 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011625 int numArgs = mInstr->getNumOperands() - 1;
11626 for (int i=0; i < numArgs; ++i)
11627 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011628
Mon P Wang63307c32008-05-05 19:05:59 +000011629 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011630 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011631 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011632
Mon P Wangab3e7472008-05-05 22:56:23 +000011633 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011634 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011635 for (int i=0; i <= lastAddrIndx; ++i)
11636 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011637
Mon P Wang63307c32008-05-05 19:05:59 +000011638 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011639 assert((argOpers[valArgIndx]->isReg() ||
11640 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011641 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011642
11643 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011644 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011645 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011646 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011647 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011648 (*MIB).addOperand(*argOpers[valArgIndx]);
11649
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011650 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011651 MIB.addReg(t1);
11652
Dale Johannesene4d209d2009-02-03 20:21:25 +000011653 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011654 MIB.addReg(t1);
11655 MIB.addReg(t2);
11656
11657 // Generate movc
11658 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011659 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011660 MIB.addReg(t2);
11661 MIB.addReg(t1);
11662
11663 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011664 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011665 for (int i=0; i <= lastAddrIndx; ++i)
11666 (*MIB).addOperand(*argOpers[i]);
11667 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011668 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011669 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11670 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011671
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011672 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011673 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011674
Mon P Wang63307c32008-05-05 19:05:59 +000011675 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011676 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011677
Dan Gohman14152b42010-07-06 20:24:04 +000011678 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011679 return nextMBB;
11680}
11681
Eric Christopherf83a5de2009-08-27 18:08:16 +000011682// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011683// or XMM0_V32I8 in AVX all of this code can be replaced with that
11684// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011685MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011686X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011687 unsigned numArgs, bool memArg) const {
Craig Topperc0d82852011-11-22 00:44:41 +000011688 assert(Subtarget->hasSSE42orAVX() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011689 "Target must have SSE4.2 or AVX features enabled");
11690
Eric Christopherb120ab42009-08-18 22:50:32 +000011691 DebugLoc dl = MI->getDebugLoc();
11692 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011693 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011694 if (!Subtarget->hasAVX()) {
11695 if (memArg)
11696 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11697 else
11698 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11699 } else {
11700 if (memArg)
11701 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11702 else
11703 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11704 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011705
Eric Christopher41c902f2010-11-30 08:20:21 +000011706 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011707 for (unsigned i = 0; i < numArgs; ++i) {
11708 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011709 if (!(Op.isReg() && Op.isImplicit()))
11710 MIB.addOperand(Op);
11711 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011712 BuildMI(*BB, MI, dl,
11713 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11714 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011715 .addReg(X86::XMM0);
11716
Dan Gohman14152b42010-07-06 20:24:04 +000011717 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011718 return BB;
11719}
11720
11721MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011722X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011723 DebugLoc dl = MI->getDebugLoc();
11724 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011725
Eric Christopher228232b2010-11-30 07:20:12 +000011726 // Address into RAX/EAX, other two args into ECX, EDX.
11727 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11728 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11729 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11730 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011731 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011732
Eric Christopher228232b2010-11-30 07:20:12 +000011733 unsigned ValOps = X86::AddrNumOperands;
11734 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11735 .addReg(MI->getOperand(ValOps).getReg());
11736 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11737 .addReg(MI->getOperand(ValOps+1).getReg());
11738
11739 // The instruction doesn't actually take any operands though.
11740 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011741
Eric Christopher228232b2010-11-30 07:20:12 +000011742 MI->eraseFromParent(); // The pseudo is gone now.
11743 return BB;
11744}
11745
11746MachineBasicBlock *
11747X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011748 DebugLoc dl = MI->getDebugLoc();
11749 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011750
Eric Christopher228232b2010-11-30 07:20:12 +000011751 // First arg in ECX, the second in EAX.
11752 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11753 .addReg(MI->getOperand(0).getReg());
11754 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11755 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011756
Eric Christopher228232b2010-11-30 07:20:12 +000011757 // The instruction doesn't actually take any operands though.
11758 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011759
Eric Christopher228232b2010-11-30 07:20:12 +000011760 MI->eraseFromParent(); // The pseudo is gone now.
11761 return BB;
11762}
11763
11764MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011765X86TargetLowering::EmitVAARG64WithCustomInserter(
11766 MachineInstr *MI,
11767 MachineBasicBlock *MBB) const {
11768 // Emit va_arg instruction on X86-64.
11769
11770 // Operands to this pseudo-instruction:
11771 // 0 ) Output : destination address (reg)
11772 // 1-5) Input : va_list address (addr, i64mem)
11773 // 6 ) ArgSize : Size (in bytes) of vararg type
11774 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11775 // 8 ) Align : Alignment of type
11776 // 9 ) EFLAGS (implicit-def)
11777
11778 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11779 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11780
11781 unsigned DestReg = MI->getOperand(0).getReg();
11782 MachineOperand &Base = MI->getOperand(1);
11783 MachineOperand &Scale = MI->getOperand(2);
11784 MachineOperand &Index = MI->getOperand(3);
11785 MachineOperand &Disp = MI->getOperand(4);
11786 MachineOperand &Segment = MI->getOperand(5);
11787 unsigned ArgSize = MI->getOperand(6).getImm();
11788 unsigned ArgMode = MI->getOperand(7).getImm();
11789 unsigned Align = MI->getOperand(8).getImm();
11790
11791 // Memory Reference
11792 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11793 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11794 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11795
11796 // Machine Information
11797 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11798 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11799 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11800 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11801 DebugLoc DL = MI->getDebugLoc();
11802
11803 // struct va_list {
11804 // i32 gp_offset
11805 // i32 fp_offset
11806 // i64 overflow_area (address)
11807 // i64 reg_save_area (address)
11808 // }
11809 // sizeof(va_list) = 24
11810 // alignment(va_list) = 8
11811
11812 unsigned TotalNumIntRegs = 6;
11813 unsigned TotalNumXMMRegs = 8;
11814 bool UseGPOffset = (ArgMode == 1);
11815 bool UseFPOffset = (ArgMode == 2);
11816 unsigned MaxOffset = TotalNumIntRegs * 8 +
11817 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11818
11819 /* Align ArgSize to a multiple of 8 */
11820 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11821 bool NeedsAlign = (Align > 8);
11822
11823 MachineBasicBlock *thisMBB = MBB;
11824 MachineBasicBlock *overflowMBB;
11825 MachineBasicBlock *offsetMBB;
11826 MachineBasicBlock *endMBB;
11827
11828 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11829 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11830 unsigned OffsetReg = 0;
11831
11832 if (!UseGPOffset && !UseFPOffset) {
11833 // If we only pull from the overflow region, we don't create a branch.
11834 // We don't need to alter control flow.
11835 OffsetDestReg = 0; // unused
11836 OverflowDestReg = DestReg;
11837
11838 offsetMBB = NULL;
11839 overflowMBB = thisMBB;
11840 endMBB = thisMBB;
11841 } else {
11842 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11843 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11844 // If not, pull from overflow_area. (branch to overflowMBB)
11845 //
11846 // thisMBB
11847 // | .
11848 // | .
11849 // offsetMBB overflowMBB
11850 // | .
11851 // | .
11852 // endMBB
11853
11854 // Registers for the PHI in endMBB
11855 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11856 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11857
11858 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11859 MachineFunction *MF = MBB->getParent();
11860 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11861 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11862 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11863
11864 MachineFunction::iterator MBBIter = MBB;
11865 ++MBBIter;
11866
11867 // Insert the new basic blocks
11868 MF->insert(MBBIter, offsetMBB);
11869 MF->insert(MBBIter, overflowMBB);
11870 MF->insert(MBBIter, endMBB);
11871
11872 // Transfer the remainder of MBB and its successor edges to endMBB.
11873 endMBB->splice(endMBB->begin(), thisMBB,
11874 llvm::next(MachineBasicBlock::iterator(MI)),
11875 thisMBB->end());
11876 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11877
11878 // Make offsetMBB and overflowMBB successors of thisMBB
11879 thisMBB->addSuccessor(offsetMBB);
11880 thisMBB->addSuccessor(overflowMBB);
11881
11882 // endMBB is a successor of both offsetMBB and overflowMBB
11883 offsetMBB->addSuccessor(endMBB);
11884 overflowMBB->addSuccessor(endMBB);
11885
11886 // Load the offset value into a register
11887 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11888 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11889 .addOperand(Base)
11890 .addOperand(Scale)
11891 .addOperand(Index)
11892 .addDisp(Disp, UseFPOffset ? 4 : 0)
11893 .addOperand(Segment)
11894 .setMemRefs(MMOBegin, MMOEnd);
11895
11896 // Check if there is enough room left to pull this argument.
11897 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11898 .addReg(OffsetReg)
11899 .addImm(MaxOffset + 8 - ArgSizeA8);
11900
11901 // Branch to "overflowMBB" if offset >= max
11902 // Fall through to "offsetMBB" otherwise
11903 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11904 .addMBB(overflowMBB);
11905 }
11906
11907 // In offsetMBB, emit code to use the reg_save_area.
11908 if (offsetMBB) {
11909 assert(OffsetReg != 0);
11910
11911 // Read the reg_save_area address.
11912 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11913 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11914 .addOperand(Base)
11915 .addOperand(Scale)
11916 .addOperand(Index)
11917 .addDisp(Disp, 16)
11918 .addOperand(Segment)
11919 .setMemRefs(MMOBegin, MMOEnd);
11920
11921 // Zero-extend the offset
11922 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11923 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11924 .addImm(0)
11925 .addReg(OffsetReg)
11926 .addImm(X86::sub_32bit);
11927
11928 // Add the offset to the reg_save_area to get the final address.
11929 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11930 .addReg(OffsetReg64)
11931 .addReg(RegSaveReg);
11932
11933 // Compute the offset for the next argument
11934 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11935 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11936 .addReg(OffsetReg)
11937 .addImm(UseFPOffset ? 16 : 8);
11938
11939 // Store it back into the va_list.
11940 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11941 .addOperand(Base)
11942 .addOperand(Scale)
11943 .addOperand(Index)
11944 .addDisp(Disp, UseFPOffset ? 4 : 0)
11945 .addOperand(Segment)
11946 .addReg(NextOffsetReg)
11947 .setMemRefs(MMOBegin, MMOEnd);
11948
11949 // Jump to endMBB
11950 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11951 .addMBB(endMBB);
11952 }
11953
11954 //
11955 // Emit code to use overflow area
11956 //
11957
11958 // Load the overflow_area address into a register.
11959 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11960 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11961 .addOperand(Base)
11962 .addOperand(Scale)
11963 .addOperand(Index)
11964 .addDisp(Disp, 8)
11965 .addOperand(Segment)
11966 .setMemRefs(MMOBegin, MMOEnd);
11967
11968 // If we need to align it, do so. Otherwise, just copy the address
11969 // to OverflowDestReg.
11970 if (NeedsAlign) {
11971 // Align the overflow address
11972 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11973 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11974
11975 // aligned_addr = (addr + (align-1)) & ~(align-1)
11976 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11977 .addReg(OverflowAddrReg)
11978 .addImm(Align-1);
11979
11980 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11981 .addReg(TmpReg)
11982 .addImm(~(uint64_t)(Align-1));
11983 } else {
11984 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11985 .addReg(OverflowAddrReg);
11986 }
11987
11988 // Compute the next overflow address after this argument.
11989 // (the overflow address should be kept 8-byte aligned)
11990 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11991 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11992 .addReg(OverflowDestReg)
11993 .addImm(ArgSizeA8);
11994
11995 // Store the new overflow address.
11996 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11997 .addOperand(Base)
11998 .addOperand(Scale)
11999 .addOperand(Index)
12000 .addDisp(Disp, 8)
12001 .addOperand(Segment)
12002 .addReg(NextAddrReg)
12003 .setMemRefs(MMOBegin, MMOEnd);
12004
12005 // If we branched, emit the PHI to the front of endMBB.
12006 if (offsetMBB) {
12007 BuildMI(*endMBB, endMBB->begin(), DL,
12008 TII->get(X86::PHI), DestReg)
12009 .addReg(OffsetDestReg).addMBB(offsetMBB)
12010 .addReg(OverflowDestReg).addMBB(overflowMBB);
12011 }
12012
12013 // Erase the pseudo instruction
12014 MI->eraseFromParent();
12015
12016 return endMBB;
12017}
12018
12019MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012020X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12021 MachineInstr *MI,
12022 MachineBasicBlock *MBB) const {
12023 // Emit code to save XMM registers to the stack. The ABI says that the
12024 // number of registers to save is given in %al, so it's theoretically
12025 // possible to do an indirect jump trick to avoid saving all of them,
12026 // however this code takes a simpler approach and just executes all
12027 // of the stores if %al is non-zero. It's less code, and it's probably
12028 // easier on the hardware branch predictor, and stores aren't all that
12029 // expensive anyway.
12030
12031 // Create the new basic blocks. One block contains all the XMM stores,
12032 // and one block is the final destination regardless of whether any
12033 // stores were performed.
12034 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12035 MachineFunction *F = MBB->getParent();
12036 MachineFunction::iterator MBBIter = MBB;
12037 ++MBBIter;
12038 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12039 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12040 F->insert(MBBIter, XMMSaveMBB);
12041 F->insert(MBBIter, EndMBB);
12042
Dan Gohman14152b42010-07-06 20:24:04 +000012043 // Transfer the remainder of MBB and its successor edges to EndMBB.
12044 EndMBB->splice(EndMBB->begin(), MBB,
12045 llvm::next(MachineBasicBlock::iterator(MI)),
12046 MBB->end());
12047 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12048
Dan Gohmand6708ea2009-08-15 01:38:56 +000012049 // The original block will now fall through to the XMM save block.
12050 MBB->addSuccessor(XMMSaveMBB);
12051 // The XMMSaveMBB will fall through to the end block.
12052 XMMSaveMBB->addSuccessor(EndMBB);
12053
12054 // Now add the instructions.
12055 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12056 DebugLoc DL = MI->getDebugLoc();
12057
12058 unsigned CountReg = MI->getOperand(0).getReg();
12059 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12060 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12061
12062 if (!Subtarget->isTargetWin64()) {
12063 // If %al is 0, branch around the XMM save block.
12064 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012065 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012066 MBB->addSuccessor(EndMBB);
12067 }
12068
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012069 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012070 // In the XMM save block, save all the XMM argument registers.
12071 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12072 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012073 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012074 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012075 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012076 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012077 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012078 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012079 .addFrameIndex(RegSaveFrameIndex)
12080 .addImm(/*Scale=*/1)
12081 .addReg(/*IndexReg=*/0)
12082 .addImm(/*Disp=*/Offset)
12083 .addReg(/*Segment=*/0)
12084 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012085 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012086 }
12087
Dan Gohman14152b42010-07-06 20:24:04 +000012088 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012089
12090 return EndMBB;
12091}
Mon P Wang63307c32008-05-05 19:05:59 +000012092
Evan Cheng60c07e12006-07-05 22:17:51 +000012093MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012094X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012095 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012096 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12097 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012098
Chris Lattner52600972009-09-02 05:57:00 +000012099 // To "insert" a SELECT_CC instruction, we actually have to insert the
12100 // diamond control-flow pattern. The incoming instruction knows the
12101 // destination vreg to set, the condition code register to branch on, the
12102 // true/false values to select between, and a branch opcode to use.
12103 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12104 MachineFunction::iterator It = BB;
12105 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012106
Chris Lattner52600972009-09-02 05:57:00 +000012107 // thisMBB:
12108 // ...
12109 // TrueVal = ...
12110 // cmpTY ccX, r1, r2
12111 // bCC copy1MBB
12112 // fallthrough --> copy0MBB
12113 MachineBasicBlock *thisMBB = BB;
12114 MachineFunction *F = BB->getParent();
12115 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12116 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012117 F->insert(It, copy0MBB);
12118 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012119
Bill Wendling730c07e2010-06-25 20:48:10 +000012120 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12121 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000012122 if (!MI->killsRegister(X86::EFLAGS)) {
12123 copy0MBB->addLiveIn(X86::EFLAGS);
12124 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012125 }
12126
Dan Gohman14152b42010-07-06 20:24:04 +000012127 // Transfer the remainder of BB and its successor edges to sinkMBB.
12128 sinkMBB->splice(sinkMBB->begin(), BB,
12129 llvm::next(MachineBasicBlock::iterator(MI)),
12130 BB->end());
12131 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12132
12133 // Add the true and fallthrough blocks as its successors.
12134 BB->addSuccessor(copy0MBB);
12135 BB->addSuccessor(sinkMBB);
12136
12137 // Create the conditional branch instruction.
12138 unsigned Opc =
12139 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12140 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12141
Chris Lattner52600972009-09-02 05:57:00 +000012142 // copy0MBB:
12143 // %FalseValue = ...
12144 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012145 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012146
Chris Lattner52600972009-09-02 05:57:00 +000012147 // sinkMBB:
12148 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12149 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012150 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12151 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012152 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12153 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12154
Dan Gohman14152b42010-07-06 20:24:04 +000012155 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012156 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012157}
12158
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012159MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012160X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12161 bool Is64Bit) const {
12162 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12163 DebugLoc DL = MI->getDebugLoc();
12164 MachineFunction *MF = BB->getParent();
12165 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12166
12167 assert(EnableSegmentedStacks);
12168
12169 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12170 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12171
12172 // BB:
12173 // ... [Till the alloca]
12174 // If stacklet is not large enough, jump to mallocMBB
12175 //
12176 // bumpMBB:
12177 // Allocate by subtracting from RSP
12178 // Jump to continueMBB
12179 //
12180 // mallocMBB:
12181 // Allocate by call to runtime
12182 //
12183 // continueMBB:
12184 // ...
12185 // [rest of original BB]
12186 //
12187
12188 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12189 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12190 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12191
12192 MachineRegisterInfo &MRI = MF->getRegInfo();
12193 const TargetRegisterClass *AddrRegClass =
12194 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12195
12196 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12197 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12198 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012199 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012200 sizeVReg = MI->getOperand(1).getReg(),
12201 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12202
12203 MachineFunction::iterator MBBIter = BB;
12204 ++MBBIter;
12205
12206 MF->insert(MBBIter, bumpMBB);
12207 MF->insert(MBBIter, mallocMBB);
12208 MF->insert(MBBIter, continueMBB);
12209
12210 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12211 (MachineBasicBlock::iterator(MI)), BB->end());
12212 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12213
12214 // Add code to the main basic block to check if the stack limit has been hit,
12215 // and if so, jump to mallocMBB otherwise to bumpMBB.
12216 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012217 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012218 .addReg(tmpSPVReg).addReg(sizeVReg);
12219 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
12220 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012221 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012222 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12223
12224 // bumpMBB simply decreases the stack pointer, since we know the current
12225 // stacklet has enough space.
12226 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012227 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012228 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012229 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012230 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12231
12232 // Calls into a routine in libgcc to allocate more space from the heap.
12233 if (Is64Bit) {
12234 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12235 .addReg(sizeVReg);
12236 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12237 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12238 } else {
12239 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12240 .addImm(12);
12241 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12242 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12243 .addExternalSymbol("__morestack_allocate_stack_space");
12244 }
12245
12246 if (!Is64Bit)
12247 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12248 .addImm(16);
12249
12250 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12251 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12252 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12253
12254 // Set up the CFG correctly.
12255 BB->addSuccessor(bumpMBB);
12256 BB->addSuccessor(mallocMBB);
12257 mallocMBB->addSuccessor(continueMBB);
12258 bumpMBB->addSuccessor(continueMBB);
12259
12260 // Take care of the PHI nodes.
12261 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12262 MI->getOperand(0).getReg())
12263 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12264 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12265
12266 // Delete the original pseudo instruction.
12267 MI->eraseFromParent();
12268
12269 // And we're done.
12270 return continueMBB;
12271}
12272
12273MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012274X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012275 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012276 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12277 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012278
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012279 assert(!Subtarget->isTargetEnvMacho());
12280
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012281 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12282 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012283
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012284 if (Subtarget->isTargetWin64()) {
12285 if (Subtarget->isTargetCygMing()) {
12286 // ___chkstk(Mingw64):
12287 // Clobbers R10, R11, RAX and EFLAGS.
12288 // Updates RSP.
12289 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12290 .addExternalSymbol("___chkstk")
12291 .addReg(X86::RAX, RegState::Implicit)
12292 .addReg(X86::RSP, RegState::Implicit)
12293 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12294 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12295 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12296 } else {
12297 // __chkstk(MSVCRT): does not update stack pointer.
12298 // Clobbers R10, R11 and EFLAGS.
12299 // FIXME: RAX(allocated size) might be reused and not killed.
12300 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12301 .addExternalSymbol("__chkstk")
12302 .addReg(X86::RAX, RegState::Implicit)
12303 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12304 // RAX has the offset to subtracted from RSP.
12305 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12306 .addReg(X86::RSP)
12307 .addReg(X86::RAX);
12308 }
12309 } else {
12310 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012311 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12312
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012313 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12314 .addExternalSymbol(StackProbeSymbol)
12315 .addReg(X86::EAX, RegState::Implicit)
12316 .addReg(X86::ESP, RegState::Implicit)
12317 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12318 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12319 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12320 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012321
Dan Gohman14152b42010-07-06 20:24:04 +000012322 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012323 return BB;
12324}
Chris Lattner52600972009-09-02 05:57:00 +000012325
12326MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012327X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12328 MachineBasicBlock *BB) const {
12329 // This is pretty easy. We're taking the value that we received from
12330 // our load from the relocation, sticking it in either RDI (x86-64)
12331 // or EAX and doing an indirect call. The return value will then
12332 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012333 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012334 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012335 DebugLoc DL = MI->getDebugLoc();
12336 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012337
12338 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012339 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012340
Eric Christopher30ef0e52010-06-03 04:07:48 +000012341 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012342 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12343 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012344 .addReg(X86::RIP)
12345 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012346 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012347 MI->getOperand(3).getTargetFlags())
12348 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012349 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012350 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012351 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012352 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12353 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012354 .addReg(0)
12355 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012356 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012357 MI->getOperand(3).getTargetFlags())
12358 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012359 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012360 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012361 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012362 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12363 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012364 .addReg(TII->getGlobalBaseReg(F))
12365 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012366 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012367 MI->getOperand(3).getTargetFlags())
12368 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012369 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012370 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012371 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012372
Dan Gohman14152b42010-07-06 20:24:04 +000012373 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012374 return BB;
12375}
12376
12377MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012378X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012379 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012380 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000012381 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012382 case X86::TAILJMPd64:
12383 case X86::TAILJMPr64:
12384 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000012385 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012386 case X86::TCRETURNdi64:
12387 case X86::TCRETURNri64:
12388 case X86::TCRETURNmi64:
12389 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12390 // On AMD64, additional defs should be added before register allocation.
12391 if (!Subtarget->isTargetWin64()) {
12392 MI->addRegisterDefined(X86::RSI);
12393 MI->addRegisterDefined(X86::RDI);
12394 MI->addRegisterDefined(X86::XMM6);
12395 MI->addRegisterDefined(X86::XMM7);
12396 MI->addRegisterDefined(X86::XMM8);
12397 MI->addRegisterDefined(X86::XMM9);
12398 MI->addRegisterDefined(X86::XMM10);
12399 MI->addRegisterDefined(X86::XMM11);
12400 MI->addRegisterDefined(X86::XMM12);
12401 MI->addRegisterDefined(X86::XMM13);
12402 MI->addRegisterDefined(X86::XMM14);
12403 MI->addRegisterDefined(X86::XMM15);
12404 }
12405 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012406 case X86::WIN_ALLOCA:
12407 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012408 case X86::SEG_ALLOCA_32:
12409 return EmitLoweredSegAlloca(MI, BB, false);
12410 case X86::SEG_ALLOCA_64:
12411 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012412 case X86::TLSCall_32:
12413 case X86::TLSCall_64:
12414 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012415 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012416 case X86::CMOV_FR32:
12417 case X86::CMOV_FR64:
12418 case X86::CMOV_V4F32:
12419 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012420 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012421 case X86::CMOV_V8F32:
12422 case X86::CMOV_V4F64:
12423 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012424 case X86::CMOV_GR16:
12425 case X86::CMOV_GR32:
12426 case X86::CMOV_RFP32:
12427 case X86::CMOV_RFP64:
12428 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012429 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012430
Dale Johannesen849f2142007-07-03 00:53:03 +000012431 case X86::FP32_TO_INT16_IN_MEM:
12432 case X86::FP32_TO_INT32_IN_MEM:
12433 case X86::FP32_TO_INT64_IN_MEM:
12434 case X86::FP64_TO_INT16_IN_MEM:
12435 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012436 case X86::FP64_TO_INT64_IN_MEM:
12437 case X86::FP80_TO_INT16_IN_MEM:
12438 case X86::FP80_TO_INT32_IN_MEM:
12439 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012440 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12441 DebugLoc DL = MI->getDebugLoc();
12442
Evan Cheng60c07e12006-07-05 22:17:51 +000012443 // Change the floating point control register to use "round towards zero"
12444 // mode when truncating to an integer value.
12445 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012446 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012447 addFrameReference(BuildMI(*BB, MI, DL,
12448 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012449
12450 // Load the old value of the high byte of the control word...
12451 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012452 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012453 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012454 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012455
12456 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012457 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012458 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012459
12460 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012461 addFrameReference(BuildMI(*BB, MI, DL,
12462 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012463
12464 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012465 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012466 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012467
12468 // Get the X86 opcode to use.
12469 unsigned Opc;
12470 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012471 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012472 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12473 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12474 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12475 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12476 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12477 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012478 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12479 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12480 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012481 }
12482
12483 X86AddressMode AM;
12484 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012485 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012486 AM.BaseType = X86AddressMode::RegBase;
12487 AM.Base.Reg = Op.getReg();
12488 } else {
12489 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012490 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012491 }
12492 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012493 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012494 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012495 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012496 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012497 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012498 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012499 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012500 AM.GV = Op.getGlobal();
12501 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012502 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012503 }
Dan Gohman14152b42010-07-06 20:24:04 +000012504 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012505 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012506
12507 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012508 addFrameReference(BuildMI(*BB, MI, DL,
12509 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012510
Dan Gohman14152b42010-07-06 20:24:04 +000012511 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012512 return BB;
12513 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012514 // String/text processing lowering.
12515 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012516 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012517 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12518 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012519 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012520 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12521 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012522 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012523 return EmitPCMP(MI, BB, 5, false /* in mem */);
12524 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012525 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012526 return EmitPCMP(MI, BB, 5, true /* in mem */);
12527
Eric Christopher228232b2010-11-30 07:20:12 +000012528 // Thread synchronization.
12529 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012530 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012531 case X86::MWAIT:
12532 return EmitMwait(MI, BB);
12533
Eric Christopherb120ab42009-08-18 22:50:32 +000012534 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012535 case X86::ATOMAND32:
12536 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012537 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012538 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012539 X86::NOT32r, X86::EAX,
12540 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012541 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012542 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12543 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012544 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012545 X86::NOT32r, X86::EAX,
12546 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012547 case X86::ATOMXOR32:
12548 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012549 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012550 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012551 X86::NOT32r, X86::EAX,
12552 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012553 case X86::ATOMNAND32:
12554 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012555 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012556 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012557 X86::NOT32r, X86::EAX,
12558 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012559 case X86::ATOMMIN32:
12560 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12561 case X86::ATOMMAX32:
12562 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12563 case X86::ATOMUMIN32:
12564 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12565 case X86::ATOMUMAX32:
12566 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012567
12568 case X86::ATOMAND16:
12569 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12570 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012571 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012572 X86::NOT16r, X86::AX,
12573 X86::GR16RegisterClass);
12574 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012575 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012576 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012577 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012578 X86::NOT16r, X86::AX,
12579 X86::GR16RegisterClass);
12580 case X86::ATOMXOR16:
12581 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12582 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012583 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012584 X86::NOT16r, X86::AX,
12585 X86::GR16RegisterClass);
12586 case X86::ATOMNAND16:
12587 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12588 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012589 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012590 X86::NOT16r, X86::AX,
12591 X86::GR16RegisterClass, true);
12592 case X86::ATOMMIN16:
12593 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12594 case X86::ATOMMAX16:
12595 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12596 case X86::ATOMUMIN16:
12597 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12598 case X86::ATOMUMAX16:
12599 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12600
12601 case X86::ATOMAND8:
12602 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12603 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012604 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012605 X86::NOT8r, X86::AL,
12606 X86::GR8RegisterClass);
12607 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012608 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012609 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012610 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012611 X86::NOT8r, X86::AL,
12612 X86::GR8RegisterClass);
12613 case X86::ATOMXOR8:
12614 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12615 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012616 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012617 X86::NOT8r, X86::AL,
12618 X86::GR8RegisterClass);
12619 case X86::ATOMNAND8:
12620 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12621 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012622 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012623 X86::NOT8r, X86::AL,
12624 X86::GR8RegisterClass, true);
12625 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012626 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012627 case X86::ATOMAND64:
12628 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012629 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012630 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012631 X86::NOT64r, X86::RAX,
12632 X86::GR64RegisterClass);
12633 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012634 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12635 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012636 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012637 X86::NOT64r, X86::RAX,
12638 X86::GR64RegisterClass);
12639 case X86::ATOMXOR64:
12640 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012641 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012642 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012643 X86::NOT64r, X86::RAX,
12644 X86::GR64RegisterClass);
12645 case X86::ATOMNAND64:
12646 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12647 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012648 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012649 X86::NOT64r, X86::RAX,
12650 X86::GR64RegisterClass, true);
12651 case X86::ATOMMIN64:
12652 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12653 case X86::ATOMMAX64:
12654 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12655 case X86::ATOMUMIN64:
12656 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12657 case X86::ATOMUMAX64:
12658 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012659
12660 // This group does 64-bit operations on a 32-bit host.
12661 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012662 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012663 X86::AND32rr, X86::AND32rr,
12664 X86::AND32ri, X86::AND32ri,
12665 false);
12666 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012667 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012668 X86::OR32rr, X86::OR32rr,
12669 X86::OR32ri, X86::OR32ri,
12670 false);
12671 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012672 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012673 X86::XOR32rr, X86::XOR32rr,
12674 X86::XOR32ri, X86::XOR32ri,
12675 false);
12676 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012677 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012678 X86::AND32rr, X86::AND32rr,
12679 X86::AND32ri, X86::AND32ri,
12680 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012681 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012682 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012683 X86::ADD32rr, X86::ADC32rr,
12684 X86::ADD32ri, X86::ADC32ri,
12685 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012686 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012687 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012688 X86::SUB32rr, X86::SBB32rr,
12689 X86::SUB32ri, X86::SBB32ri,
12690 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012691 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012692 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012693 X86::MOV32rr, X86::MOV32rr,
12694 X86::MOV32ri, X86::MOV32ri,
12695 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012696 case X86::VASTART_SAVE_XMM_REGS:
12697 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012698
12699 case X86::VAARG_64:
12700 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012701 }
12702}
12703
12704//===----------------------------------------------------------------------===//
12705// X86 Optimization Hooks
12706//===----------------------------------------------------------------------===//
12707
Dan Gohman475871a2008-07-27 21:46:04 +000012708void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012709 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012710 APInt &KnownZero,
12711 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012712 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012713 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012714 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012715 assert((Opc >= ISD::BUILTIN_OP_END ||
12716 Opc == ISD::INTRINSIC_WO_CHAIN ||
12717 Opc == ISD::INTRINSIC_W_CHAIN ||
12718 Opc == ISD::INTRINSIC_VOID) &&
12719 "Should use MaskedValueIsZero if you don't know whether Op"
12720 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012721
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012722 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012723 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012724 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012725 case X86ISD::ADD:
12726 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012727 case X86ISD::ADC:
12728 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012729 case X86ISD::SMUL:
12730 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012731 case X86ISD::INC:
12732 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012733 case X86ISD::OR:
12734 case X86ISD::XOR:
12735 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012736 // These nodes' second result is a boolean.
12737 if (Op.getResNo() == 0)
12738 break;
12739 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012740 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012741 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12742 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012743 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012744 case ISD::INTRINSIC_WO_CHAIN: {
12745 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12746 unsigned NumLoBits = 0;
12747 switch (IntId) {
12748 default: break;
12749 case Intrinsic::x86_sse_movmsk_ps:
12750 case Intrinsic::x86_avx_movmsk_ps_256:
12751 case Intrinsic::x86_sse2_movmsk_pd:
12752 case Intrinsic::x86_avx_movmsk_pd_256:
12753 case Intrinsic::x86_mmx_pmovmskb:
12754 case Intrinsic::x86_sse2_pmovmskb_128: {
12755 // High bits of movmskp{s|d}, pmovmskb are known zero.
12756 switch (IntId) {
12757 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12758 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12759 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12760 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12761 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12762 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
12763 }
12764 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12765 Mask.getBitWidth() - NumLoBits);
12766 break;
12767 }
12768 }
12769 break;
12770 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012771 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012772}
Chris Lattner259e97c2006-01-31 19:43:35 +000012773
Owen Andersonbc146b02010-09-21 20:42:50 +000012774unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12775 unsigned Depth) const {
12776 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12777 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12778 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012779
Owen Andersonbc146b02010-09-21 20:42:50 +000012780 // Fallback case.
12781 return 1;
12782}
12783
Evan Cheng206ee9d2006-07-07 08:33:52 +000012784/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012785/// node is a GlobalAddress + offset.
12786bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012787 const GlobalValue* &GA,
12788 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012789 if (N->getOpcode() == X86ISD::Wrapper) {
12790 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012791 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012792 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012793 return true;
12794 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012795 }
Evan Chengad4196b2008-05-12 19:56:52 +000012796 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012797}
12798
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012799/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12800/// same as extracting the high 128-bit part of 256-bit vector and then
12801/// inserting the result into the low part of a new 256-bit vector
12802static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12803 EVT VT = SVOp->getValueType(0);
12804 int NumElems = VT.getVectorNumElements();
12805
12806 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12807 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12808 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12809 SVOp->getMaskElt(j) >= 0)
12810 return false;
12811
12812 return true;
12813}
12814
12815/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12816/// same as extracting the low 128-bit part of 256-bit vector and then
12817/// inserting the result into the high part of a new 256-bit vector
12818static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12819 EVT VT = SVOp->getValueType(0);
12820 int NumElems = VT.getVectorNumElements();
12821
12822 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12823 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12824 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12825 SVOp->getMaskElt(j) >= 0)
12826 return false;
12827
12828 return true;
12829}
12830
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012831/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12832static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12833 TargetLowering::DAGCombinerInfo &DCI) {
12834 DebugLoc dl = N->getDebugLoc();
12835 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12836 SDValue V1 = SVOp->getOperand(0);
12837 SDValue V2 = SVOp->getOperand(1);
12838 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012839 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012840
12841 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12842 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12843 //
12844 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012845 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012846 // V UNDEF BUILD_VECTOR UNDEF
12847 // \ / \ /
12848 // CONCAT_VECTOR CONCAT_VECTOR
12849 // \ /
12850 // \ /
12851 // RESULT: V + zero extended
12852 //
12853 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12854 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12855 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12856 return SDValue();
12857
12858 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12859 return SDValue();
12860
12861 // To match the shuffle mask, the first half of the mask should
12862 // be exactly the first vector, and all the rest a splat with the
12863 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012864 for (int i = 0; i < NumElems/2; ++i)
12865 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12866 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12867 return SDValue();
12868
12869 // Emit a zeroed vector and insert the desired subvector on its
12870 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012871 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012872 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12873 DAG.getConstant(0, MVT::i32), DAG, dl);
12874 return DCI.CombineTo(N, InsV);
12875 }
12876
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012877 //===--------------------------------------------------------------------===//
12878 // Combine some shuffles into subvector extracts and inserts:
12879 //
12880
12881 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12882 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12883 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12884 DAG, dl);
12885 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12886 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12887 return DCI.CombineTo(N, InsV);
12888 }
12889
12890 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12891 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12892 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12893 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12894 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12895 return DCI.CombineTo(N, InsV);
12896 }
12897
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012898 return SDValue();
12899}
12900
12901/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012902static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012903 TargetLowering::DAGCombinerInfo &DCI,
12904 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012905 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012906 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012907
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012908 // Don't create instructions with illegal types after legalize types has run.
12909 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12910 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12911 return SDValue();
12912
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012913 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12914 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12915 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012916 return PerformShuffleCombine256(N, DAG, DCI);
12917
12918 // Only handle 128 wide vector from here on.
12919 if (VT.getSizeInBits() != 128)
12920 return SDValue();
12921
12922 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12923 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12924 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012925 SmallVector<SDValue, 16> Elts;
12926 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012927 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012928
Nate Begemanfdea31a2010-03-24 20:49:50 +000012929 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012930}
Evan Chengd880b972008-05-09 21:53:03 +000012931
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012932/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12933/// generation and convert it from being a bunch of shuffles and extracts
12934/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012935static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12936 const TargetLowering &TLI) {
12937 SDValue InputVector = N->getOperand(0);
12938
12939 // Only operate on vectors of 4 elements, where the alternative shuffling
12940 // gets to be more expensive.
12941 if (InputVector.getValueType() != MVT::v4i32)
12942 return SDValue();
12943
12944 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12945 // single use which is a sign-extend or zero-extend, and all elements are
12946 // used.
12947 SmallVector<SDNode *, 4> Uses;
12948 unsigned ExtractedElements = 0;
12949 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12950 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12951 if (UI.getUse().getResNo() != InputVector.getResNo())
12952 return SDValue();
12953
12954 SDNode *Extract = *UI;
12955 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12956 return SDValue();
12957
12958 if (Extract->getValueType(0) != MVT::i32)
12959 return SDValue();
12960 if (!Extract->hasOneUse())
12961 return SDValue();
12962 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12963 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12964 return SDValue();
12965 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12966 return SDValue();
12967
12968 // Record which element was extracted.
12969 ExtractedElements |=
12970 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12971
12972 Uses.push_back(Extract);
12973 }
12974
12975 // If not all the elements were used, this may not be worthwhile.
12976 if (ExtractedElements != 15)
12977 return SDValue();
12978
12979 // Ok, we've now decided to do the transformation.
12980 DebugLoc dl = InputVector.getDebugLoc();
12981
12982 // Store the value to a temporary stack slot.
12983 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012984 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12985 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012986
12987 // Replace each use (extract) with a load of the appropriate element.
12988 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12989 UE = Uses.end(); UI != UE; ++UI) {
12990 SDNode *Extract = *UI;
12991
Nadav Rotem86694292011-05-17 08:31:57 +000012992 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012993 SDValue Idx = Extract->getOperand(1);
12994 unsigned EltSize =
12995 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12996 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12997 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12998
Nadav Rotem86694292011-05-17 08:31:57 +000012999 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013000 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013001
13002 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013003 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013004 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013005 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013006
13007 // Replace the exact with the load.
13008 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13009 }
13010
13011 // The replacement was made in place; don't return anything.
13012 return SDValue();
13013}
13014
Duncan Sands6bcd2192011-09-17 16:49:39 +000013015/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13016/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013017static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013018 const X86Subtarget *Subtarget) {
13019 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013020 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013021 // Get the LHS/RHS of the select.
13022 SDValue LHS = N->getOperand(1);
13023 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013024 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013025
Dan Gohman670e5392009-09-21 18:03:22 +000013026 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013027 // instructions match the semantics of the common C idiom x<y?x:y but not
13028 // x<=y?x:y, because of how they handle negative zero (which can be
13029 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013030 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13031 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
13032 (Subtarget->hasXMMInt() ||
13033 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013034 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013035
Chris Lattner47b4ce82009-03-11 05:48:52 +000013036 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013037 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013038 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13039 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013040 switch (CC) {
13041 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013042 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013043 // Converting this to a min would handle NaNs incorrectly, and swapping
13044 // the operands would cause it to handle comparisons between positive
13045 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013046 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000013047 if (!UnsafeFPMath &&
13048 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13049 break;
13050 std::swap(LHS, RHS);
13051 }
Dan Gohman670e5392009-09-21 18:03:22 +000013052 Opcode = X86ISD::FMIN;
13053 break;
13054 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013055 // Converting this to a min would handle comparisons between positive
13056 // and negative zero incorrectly.
13057 if (!UnsafeFPMath &&
13058 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13059 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013060 Opcode = X86ISD::FMIN;
13061 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013062 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013063 // Converting this to a min would handle both negative zeros and NaNs
13064 // incorrectly, but we can swap the operands to fix both.
13065 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013066 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013067 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013068 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013069 Opcode = X86ISD::FMIN;
13070 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013071
Dan Gohman670e5392009-09-21 18:03:22 +000013072 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013073 // Converting this to a max would handle comparisons between positive
13074 // and negative zero incorrectly.
13075 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013076 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013077 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013078 Opcode = X86ISD::FMAX;
13079 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013080 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013081 // Converting this to a max would handle NaNs incorrectly, and swapping
13082 // the operands would cause it to handle comparisons between positive
13083 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013084 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000013085 if (!UnsafeFPMath &&
13086 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13087 break;
13088 std::swap(LHS, RHS);
13089 }
Dan Gohman670e5392009-09-21 18:03:22 +000013090 Opcode = X86ISD::FMAX;
13091 break;
13092 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013093 // Converting this to a max would handle both negative zeros and NaNs
13094 // incorrectly, but we can swap the operands to fix both.
13095 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013096 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013097 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013098 case ISD::SETGE:
13099 Opcode = X86ISD::FMAX;
13100 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013101 }
Dan Gohman670e5392009-09-21 18:03:22 +000013102 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013103 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13104 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013105 switch (CC) {
13106 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013107 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013108 // Converting this to a min would handle comparisons between positive
13109 // and negative zero incorrectly, and swapping the operands would
13110 // cause it to handle NaNs incorrectly.
13111 if (!UnsafeFPMath &&
13112 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013113 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013114 break;
13115 std::swap(LHS, RHS);
13116 }
Dan Gohman670e5392009-09-21 18:03:22 +000013117 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013118 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013119 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013120 // Converting this to a min would handle NaNs incorrectly.
13121 if (!UnsafeFPMath &&
13122 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13123 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013124 Opcode = X86ISD::FMIN;
13125 break;
13126 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013127 // Converting this to a min would handle both negative zeros and NaNs
13128 // incorrectly, but we can swap the operands to fix both.
13129 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013130 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013131 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013132 case ISD::SETGE:
13133 Opcode = X86ISD::FMIN;
13134 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013135
Dan Gohman670e5392009-09-21 18:03:22 +000013136 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013137 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013138 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013139 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013140 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013141 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013142 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013143 // Converting this to a max would handle comparisons between positive
13144 // and negative zero incorrectly, and swapping the operands would
13145 // cause it to handle NaNs incorrectly.
13146 if (!UnsafeFPMath &&
13147 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013148 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013149 break;
13150 std::swap(LHS, RHS);
13151 }
Dan Gohman670e5392009-09-21 18:03:22 +000013152 Opcode = X86ISD::FMAX;
13153 break;
13154 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013155 // Converting this to a max would handle both negative zeros and NaNs
13156 // incorrectly, but we can swap the operands to fix both.
13157 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013158 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013159 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013160 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013161 Opcode = X86ISD::FMAX;
13162 break;
13163 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013164 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013165
Chris Lattner47b4ce82009-03-11 05:48:52 +000013166 if (Opcode)
13167 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013168 }
Eric Christopherfd179292009-08-27 18:07:15 +000013169
Chris Lattnerd1980a52009-03-12 06:52:53 +000013170 // If this is a select between two integer constants, try to do some
13171 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013172 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13173 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013174 // Don't do this for crazy integer types.
13175 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13176 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013177 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013178 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013179
Chris Lattnercee56e72009-03-13 05:53:31 +000013180 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013181 // Efficiently invertible.
13182 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13183 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13184 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13185 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013186 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013187 }
Eric Christopherfd179292009-08-27 18:07:15 +000013188
Chris Lattnerd1980a52009-03-12 06:52:53 +000013189 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013190 if (FalseC->getAPIntValue() == 0 &&
13191 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013192 if (NeedsCondInvert) // Invert the condition if needed.
13193 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13194 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013195
Chris Lattnerd1980a52009-03-12 06:52:53 +000013196 // Zero extend the condition if needed.
13197 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013198
Chris Lattnercee56e72009-03-13 05:53:31 +000013199 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013200 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013201 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013202 }
Eric Christopherfd179292009-08-27 18:07:15 +000013203
Chris Lattner97a29a52009-03-13 05:22:11 +000013204 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013205 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013206 if (NeedsCondInvert) // Invert the condition if needed.
13207 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13208 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013209
Chris Lattner97a29a52009-03-13 05:22:11 +000013210 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013211 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13212 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013213 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013214 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013215 }
Eric Christopherfd179292009-08-27 18:07:15 +000013216
Chris Lattnercee56e72009-03-13 05:53:31 +000013217 // Optimize cases that will turn into an LEA instruction. This requires
13218 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013219 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013220 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013221 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013222
Chris Lattnercee56e72009-03-13 05:53:31 +000013223 bool isFastMultiplier = false;
13224 if (Diff < 10) {
13225 switch ((unsigned char)Diff) {
13226 default: break;
13227 case 1: // result = add base, cond
13228 case 2: // result = lea base( , cond*2)
13229 case 3: // result = lea base(cond, cond*2)
13230 case 4: // result = lea base( , cond*4)
13231 case 5: // result = lea base(cond, cond*4)
13232 case 8: // result = lea base( , cond*8)
13233 case 9: // result = lea base(cond, cond*8)
13234 isFastMultiplier = true;
13235 break;
13236 }
13237 }
Eric Christopherfd179292009-08-27 18:07:15 +000013238
Chris Lattnercee56e72009-03-13 05:53:31 +000013239 if (isFastMultiplier) {
13240 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13241 if (NeedsCondInvert) // Invert the condition if needed.
13242 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13243 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013244
Chris Lattnercee56e72009-03-13 05:53:31 +000013245 // Zero extend the condition if needed.
13246 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13247 Cond);
13248 // Scale the condition by the difference.
13249 if (Diff != 1)
13250 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13251 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013252
Chris Lattnercee56e72009-03-13 05:53:31 +000013253 // Add the base if non-zero.
13254 if (FalseC->getAPIntValue() != 0)
13255 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13256 SDValue(FalseC, 0));
13257 return Cond;
13258 }
Eric Christopherfd179292009-08-27 18:07:15 +000013259 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013260 }
13261 }
Eric Christopherfd179292009-08-27 18:07:15 +000013262
Dan Gohman475871a2008-07-27 21:46:04 +000013263 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013264}
13265
Chris Lattnerd1980a52009-03-12 06:52:53 +000013266/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13267static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13268 TargetLowering::DAGCombinerInfo &DCI) {
13269 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013270
Chris Lattnerd1980a52009-03-12 06:52:53 +000013271 // If the flag operand isn't dead, don't touch this CMOV.
13272 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13273 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013274
Evan Chengb5a55d92011-05-24 01:48:22 +000013275 SDValue FalseOp = N->getOperand(0);
13276 SDValue TrueOp = N->getOperand(1);
13277 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13278 SDValue Cond = N->getOperand(3);
13279 if (CC == X86::COND_E || CC == X86::COND_NE) {
13280 switch (Cond.getOpcode()) {
13281 default: break;
13282 case X86ISD::BSR:
13283 case X86ISD::BSF:
13284 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13285 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13286 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13287 }
13288 }
13289
Chris Lattnerd1980a52009-03-12 06:52:53 +000013290 // If this is a select between two integer constants, try to do some
13291 // optimizations. Note that the operands are ordered the opposite of SELECT
13292 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013293 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13294 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013295 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13296 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013297 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13298 CC = X86::GetOppositeBranchCondition(CC);
13299 std::swap(TrueC, FalseC);
13300 }
Eric Christopherfd179292009-08-27 18:07:15 +000013301
Chris Lattnerd1980a52009-03-12 06:52:53 +000013302 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013303 // This is efficient for any integer data type (including i8/i16) and
13304 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013305 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013306 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13307 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013308
Chris Lattnerd1980a52009-03-12 06:52:53 +000013309 // Zero extend the condition if needed.
13310 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013311
Chris Lattnerd1980a52009-03-12 06:52:53 +000013312 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13313 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013314 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013315 if (N->getNumValues() == 2) // Dead flag value?
13316 return DCI.CombineTo(N, Cond, SDValue());
13317 return Cond;
13318 }
Eric Christopherfd179292009-08-27 18:07:15 +000013319
Chris Lattnercee56e72009-03-13 05:53:31 +000013320 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13321 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013322 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013323 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13324 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013325
Chris Lattner97a29a52009-03-13 05:22:11 +000013326 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013327 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13328 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013329 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13330 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013331
Chris Lattner97a29a52009-03-13 05:22:11 +000013332 if (N->getNumValues() == 2) // Dead flag value?
13333 return DCI.CombineTo(N, Cond, SDValue());
13334 return Cond;
13335 }
Eric Christopherfd179292009-08-27 18:07:15 +000013336
Chris Lattnercee56e72009-03-13 05:53:31 +000013337 // Optimize cases that will turn into an LEA instruction. This requires
13338 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013339 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013340 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013341 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013342
Chris Lattnercee56e72009-03-13 05:53:31 +000013343 bool isFastMultiplier = false;
13344 if (Diff < 10) {
13345 switch ((unsigned char)Diff) {
13346 default: break;
13347 case 1: // result = add base, cond
13348 case 2: // result = lea base( , cond*2)
13349 case 3: // result = lea base(cond, cond*2)
13350 case 4: // result = lea base( , cond*4)
13351 case 5: // result = lea base(cond, cond*4)
13352 case 8: // result = lea base( , cond*8)
13353 case 9: // result = lea base(cond, cond*8)
13354 isFastMultiplier = true;
13355 break;
13356 }
13357 }
Eric Christopherfd179292009-08-27 18:07:15 +000013358
Chris Lattnercee56e72009-03-13 05:53:31 +000013359 if (isFastMultiplier) {
13360 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013361 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13362 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013363 // Zero extend the condition if needed.
13364 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13365 Cond);
13366 // Scale the condition by the difference.
13367 if (Diff != 1)
13368 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13369 DAG.getConstant(Diff, Cond.getValueType()));
13370
13371 // Add the base if non-zero.
13372 if (FalseC->getAPIntValue() != 0)
13373 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13374 SDValue(FalseC, 0));
13375 if (N->getNumValues() == 2) // Dead flag value?
13376 return DCI.CombineTo(N, Cond, SDValue());
13377 return Cond;
13378 }
Eric Christopherfd179292009-08-27 18:07:15 +000013379 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013380 }
13381 }
13382 return SDValue();
13383}
13384
13385
Evan Cheng0b0cd912009-03-28 05:57:29 +000013386/// PerformMulCombine - Optimize a single multiply with constant into two
13387/// in order to implement it with two cheaper instructions, e.g.
13388/// LEA + SHL, LEA + LEA.
13389static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13390 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013391 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13392 return SDValue();
13393
Owen Andersone50ed302009-08-10 22:56:29 +000013394 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013395 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013396 return SDValue();
13397
13398 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13399 if (!C)
13400 return SDValue();
13401 uint64_t MulAmt = C->getZExtValue();
13402 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13403 return SDValue();
13404
13405 uint64_t MulAmt1 = 0;
13406 uint64_t MulAmt2 = 0;
13407 if ((MulAmt % 9) == 0) {
13408 MulAmt1 = 9;
13409 MulAmt2 = MulAmt / 9;
13410 } else if ((MulAmt % 5) == 0) {
13411 MulAmt1 = 5;
13412 MulAmt2 = MulAmt / 5;
13413 } else if ((MulAmt % 3) == 0) {
13414 MulAmt1 = 3;
13415 MulAmt2 = MulAmt / 3;
13416 }
13417 if (MulAmt2 &&
13418 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13419 DebugLoc DL = N->getDebugLoc();
13420
13421 if (isPowerOf2_64(MulAmt2) &&
13422 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13423 // If second multiplifer is pow2, issue it first. We want the multiply by
13424 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13425 // is an add.
13426 std::swap(MulAmt1, MulAmt2);
13427
13428 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013429 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013430 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013431 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013432 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013433 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013434 DAG.getConstant(MulAmt1, VT));
13435
Eric Christopherfd179292009-08-27 18:07:15 +000013436 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013437 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013438 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013439 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013440 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013441 DAG.getConstant(MulAmt2, VT));
13442
13443 // Do not add new nodes to DAG combiner worklist.
13444 DCI.CombineTo(N, NewMul, false);
13445 }
13446 return SDValue();
13447}
13448
Evan Chengad9c0a32009-12-15 00:53:42 +000013449static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13450 SDValue N0 = N->getOperand(0);
13451 SDValue N1 = N->getOperand(1);
13452 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13453 EVT VT = N0.getValueType();
13454
13455 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13456 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013457 if (VT.isInteger() && !VT.isVector() &&
13458 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013459 N0.getOperand(1).getOpcode() == ISD::Constant) {
13460 SDValue N00 = N0.getOperand(0);
13461 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13462 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13463 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13464 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13465 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13466 APInt ShAmt = N1C->getAPIntValue();
13467 Mask = Mask.shl(ShAmt);
13468 if (Mask != 0)
13469 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13470 N00, DAG.getConstant(Mask, VT));
13471 }
13472 }
13473
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013474
13475 // Hardware support for vector shifts is sparse which makes us scalarize the
13476 // vector operations in many cases. Also, on sandybridge ADD is faster than
13477 // shl.
13478 // (shl V, 1) -> add V,V
13479 if (isSplatVector(N1.getNode())) {
13480 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13481 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13482 // We shift all of the values by one. In many cases we do not have
13483 // hardware support for this operation. This is better expressed as an ADD
13484 // of two values.
13485 if (N1C && (1 == N1C->getZExtValue())) {
13486 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13487 }
13488 }
13489
Evan Chengad9c0a32009-12-15 00:53:42 +000013490 return SDValue();
13491}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013492
Nate Begeman740ab032009-01-26 00:52:55 +000013493/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13494/// when possible.
13495static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13496 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013497 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013498 if (N->getOpcode() == ISD::SHL) {
13499 SDValue V = PerformSHLCombine(N, DAG);
13500 if (V.getNode()) return V;
13501 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013502
Nate Begeman740ab032009-01-26 00:52:55 +000013503 // On X86 with SSE2 support, we can transform this to a vector shift if
13504 // all elements are shifted by the same amount. We can't do this in legalize
13505 // because the a constant vector is typically transformed to a constant pool
13506 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013507 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013508 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013509
Craig Topper7be5dfd2011-11-12 09:58:49 +000013510 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
13511 (!Subtarget->hasAVX2() ||
13512 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013513 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013514
Mon P Wang3becd092009-01-28 08:12:05 +000013515 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013516 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013517 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013518 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013519 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13520 unsigned NumElts = VT.getVectorNumElements();
13521 unsigned i = 0;
13522 for (; i != NumElts; ++i) {
13523 SDValue Arg = ShAmtOp.getOperand(i);
13524 if (Arg.getOpcode() == ISD::UNDEF) continue;
13525 BaseShAmt = Arg;
13526 break;
13527 }
13528 for (; i != NumElts; ++i) {
13529 SDValue Arg = ShAmtOp.getOperand(i);
13530 if (Arg.getOpcode() == ISD::UNDEF) continue;
13531 if (Arg != BaseShAmt) {
13532 return SDValue();
13533 }
13534 }
13535 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013536 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013537 SDValue InVec = ShAmtOp.getOperand(0);
13538 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13539 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13540 unsigned i = 0;
13541 for (; i != NumElts; ++i) {
13542 SDValue Arg = InVec.getOperand(i);
13543 if (Arg.getOpcode() == ISD::UNDEF) continue;
13544 BaseShAmt = Arg;
13545 break;
13546 }
13547 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13548 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013549 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013550 if (C->getZExtValue() == SplatIdx)
13551 BaseShAmt = InVec.getOperand(1);
13552 }
13553 }
13554 if (BaseShAmt.getNode() == 0)
13555 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13556 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013557 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013558 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013559
Mon P Wangefa42202009-09-03 19:56:25 +000013560 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013561 if (EltVT.bitsGT(MVT::i32))
13562 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13563 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013564 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013565
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013566 // The shift amount is identical so we can do a vector shift.
13567 SDValue ValOp = N->getOperand(0);
13568 switch (N->getOpcode()) {
13569 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013570 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013571 break;
13572 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013573 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013574 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013575 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013576 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013577 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013578 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013579 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013580 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013581 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013582 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013583 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013584 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013585 if (VT == MVT::v4i64)
13586 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13587 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
13588 ValOp, BaseShAmt);
13589 if (VT == MVT::v8i32)
13590 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13591 DAG.getConstant(Intrinsic::x86_avx2_pslli_d, MVT::i32),
13592 ValOp, BaseShAmt);
13593 if (VT == MVT::v16i16)
13594 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13595 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
13596 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013597 break;
13598 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013599 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013600 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013601 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013602 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013603 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013604 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013605 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013606 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013607 if (VT == MVT::v8i32)
13608 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13609 DAG.getConstant(Intrinsic::x86_avx2_psrai_d, MVT::i32),
13610 ValOp, BaseShAmt);
13611 if (VT == MVT::v16i16)
13612 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13613 DAG.getConstant(Intrinsic::x86_avx2_psrai_w, MVT::i32),
13614 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013615 break;
13616 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013617 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013618 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013619 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013620 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013621 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013622 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013623 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013624 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013625 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013626 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013627 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013628 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013629 if (VT == MVT::v4i64)
13630 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13631 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
13632 ValOp, BaseShAmt);
13633 if (VT == MVT::v8i32)
13634 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13635 DAG.getConstant(Intrinsic::x86_avx2_psrli_d, MVT::i32),
13636 ValOp, BaseShAmt);
13637 if (VT == MVT::v16i16)
13638 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13639 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
13640 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013641 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013642 }
13643 return SDValue();
13644}
13645
Nate Begemanb65c1752010-12-17 22:55:37 +000013646
Stuart Hastings865f0932011-06-03 23:53:54 +000013647// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13648// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13649// and friends. Likewise for OR -> CMPNEQSS.
13650static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13651 TargetLowering::DAGCombinerInfo &DCI,
13652 const X86Subtarget *Subtarget) {
13653 unsigned opcode;
13654
13655 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13656 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013657 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013658 SDValue N0 = N->getOperand(0);
13659 SDValue N1 = N->getOperand(1);
13660 SDValue CMP0 = N0->getOperand(1);
13661 SDValue CMP1 = N1->getOperand(1);
13662 DebugLoc DL = N->getDebugLoc();
13663
13664 // The SETCCs should both refer to the same CMP.
13665 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13666 return SDValue();
13667
13668 SDValue CMP00 = CMP0->getOperand(0);
13669 SDValue CMP01 = CMP0->getOperand(1);
13670 EVT VT = CMP00.getValueType();
13671
13672 if (VT == MVT::f32 || VT == MVT::f64) {
13673 bool ExpectingFlags = false;
13674 // Check for any users that want flags:
13675 for (SDNode::use_iterator UI = N->use_begin(),
13676 UE = N->use_end();
13677 !ExpectingFlags && UI != UE; ++UI)
13678 switch (UI->getOpcode()) {
13679 default:
13680 case ISD::BR_CC:
13681 case ISD::BRCOND:
13682 case ISD::SELECT:
13683 ExpectingFlags = true;
13684 break;
13685 case ISD::CopyToReg:
13686 case ISD::SIGN_EXTEND:
13687 case ISD::ZERO_EXTEND:
13688 case ISD::ANY_EXTEND:
13689 break;
13690 }
13691
13692 if (!ExpectingFlags) {
13693 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13694 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13695
13696 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13697 X86::CondCode tmp = cc0;
13698 cc0 = cc1;
13699 cc1 = tmp;
13700 }
13701
13702 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13703 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13704 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13705 X86ISD::NodeType NTOperator = is64BitFP ?
13706 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13707 // FIXME: need symbolic constants for these magic numbers.
13708 // See X86ATTInstPrinter.cpp:printSSECC().
13709 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13710 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13711 DAG.getConstant(x86cc, MVT::i8));
13712 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13713 OnesOrZeroesF);
13714 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13715 DAG.getConstant(1, MVT::i32));
13716 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13717 return OneBitOfTruth;
13718 }
13719 }
13720 }
13721 }
13722 return SDValue();
13723}
13724
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013725/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13726/// so it can be folded inside ANDNP.
13727static bool CanFoldXORWithAllOnes(const SDNode *N) {
13728 EVT VT = N->getValueType(0);
13729
13730 // Match direct AllOnes for 128 and 256-bit vectors
13731 if (ISD::isBuildVectorAllOnes(N))
13732 return true;
13733
13734 // Look through a bit convert.
13735 if (N->getOpcode() == ISD::BITCAST)
13736 N = N->getOperand(0).getNode();
13737
13738 // Sometimes the operand may come from a insert_subvector building a 256-bit
13739 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013740 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013741 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13742 SDValue V1 = N->getOperand(0);
13743 SDValue V2 = N->getOperand(1);
13744
13745 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13746 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13747 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13748 ISD::isBuildVectorAllOnes(V2.getNode()))
13749 return true;
13750 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013751
13752 return false;
13753}
13754
Nate Begemanb65c1752010-12-17 22:55:37 +000013755static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13756 TargetLowering::DAGCombinerInfo &DCI,
13757 const X86Subtarget *Subtarget) {
13758 if (DCI.isBeforeLegalizeOps())
13759 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013760
Stuart Hastings865f0932011-06-03 23:53:54 +000013761 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13762 if (R.getNode())
13763 return R;
13764
Craig Topper54a11172011-10-14 07:06:56 +000013765 EVT VT = N->getValueType(0);
13766
Craig Topperb4c94572011-10-21 06:55:01 +000013767 // Create ANDN, BLSI, and BLSR instructions
13768 // BLSI is X & (-X)
13769 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013770 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13771 SDValue N0 = N->getOperand(0);
13772 SDValue N1 = N->getOperand(1);
13773 DebugLoc DL = N->getDebugLoc();
13774
13775 // Check LHS for not
13776 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13777 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13778 // Check RHS for not
13779 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13780 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13781
Craig Topperb4c94572011-10-21 06:55:01 +000013782 // Check LHS for neg
13783 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13784 isZero(N0.getOperand(0)))
13785 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13786
13787 // Check RHS for neg
13788 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13789 isZero(N1.getOperand(0)))
13790 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13791
13792 // Check LHS for X-1
13793 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13794 isAllOnes(N0.getOperand(1)))
13795 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13796
13797 // Check RHS for X-1
13798 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13799 isAllOnes(N1.getOperand(1)))
13800 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13801
Craig Topper54a11172011-10-14 07:06:56 +000013802 return SDValue();
13803 }
13804
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013805 // Want to form ANDNP nodes:
13806 // 1) In the hopes of then easily combining them with OR and AND nodes
13807 // to form PBLEND/PSIGN.
13808 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013809 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013810 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013811
Nate Begemanb65c1752010-12-17 22:55:37 +000013812 SDValue N0 = N->getOperand(0);
13813 SDValue N1 = N->getOperand(1);
13814 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013815
Nate Begemanb65c1752010-12-17 22:55:37 +000013816 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013817 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013818 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13819 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013820 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013821
13822 // Check RHS for vnot
13823 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013824 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13825 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013826 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013827
Nate Begemanb65c1752010-12-17 22:55:37 +000013828 return SDValue();
13829}
13830
Evan Cheng760d1942010-01-04 21:22:48 +000013831static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013832 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013833 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013834 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013835 return SDValue();
13836
Stuart Hastings865f0932011-06-03 23:53:54 +000013837 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13838 if (R.getNode())
13839 return R;
13840
Evan Cheng760d1942010-01-04 21:22:48 +000013841 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000013842
Evan Cheng760d1942010-01-04 21:22:48 +000013843 SDValue N0 = N->getOperand(0);
13844 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013845
Nate Begemanb65c1752010-12-17 22:55:37 +000013846 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000013847 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperc0d82852011-11-22 00:44:41 +000013848 if (!Subtarget->hasSSSE3orAVX() ||
Craig Topper1666cb62011-11-19 07:07:26 +000013849 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
13850 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013851
Craig Topper1666cb62011-11-19 07:07:26 +000013852 // Canonicalize pandn to RHS
13853 if (N0.getOpcode() == X86ISD::ANDNP)
13854 std::swap(N0, N1);
13855 // or (and (m, x), (pandn m, y))
13856 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
13857 SDValue Mask = N1.getOperand(0);
13858 SDValue X = N1.getOperand(1);
13859 SDValue Y;
13860 if (N0.getOperand(0) == Mask)
13861 Y = N0.getOperand(1);
13862 if (N0.getOperand(1) == Mask)
13863 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013864
Craig Topper1666cb62011-11-19 07:07:26 +000013865 // Check to see if the mask appeared in both the AND and ANDNP and
13866 if (!Y.getNode())
13867 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013868
Craig Topper1666cb62011-11-19 07:07:26 +000013869 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13870 if (Mask.getOpcode() != ISD::BITCAST ||
13871 X.getOpcode() != ISD::BITCAST ||
13872 Y.getOpcode() != ISD::BITCAST)
13873 return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013874
Craig Topper1666cb62011-11-19 07:07:26 +000013875 // Look through mask bitcast.
13876 Mask = Mask.getOperand(0);
13877 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013878
Craig Topper1666cb62011-11-19 07:07:26 +000013879 // Validate that the Mask operand is a vector sra node. The sra node
13880 // will be an intrinsic.
13881 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13882 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013883
Craig Topper1666cb62011-11-19 07:07:26 +000013884 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13885 // there is no psrai.b
13886 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13887 case Intrinsic::x86_sse2_psrai_w:
13888 case Intrinsic::x86_sse2_psrai_d:
13889 case Intrinsic::x86_avx2_psrai_w:
13890 case Intrinsic::x86_avx2_psrai_d:
13891 break;
13892 default: return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013893 }
Craig Topper1666cb62011-11-19 07:07:26 +000013894
13895 // Check that the SRA is all signbits.
13896 SDValue SraC = Mask.getOperand(2);
13897 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13898 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13899 if ((SraAmt + 1) != EltBits)
13900 return SDValue();
13901
13902 DebugLoc DL = N->getDebugLoc();
13903
13904 // Now we know we at least have a plendvb with the mask val. See if
13905 // we can form a psignb/w/d.
13906 // psign = x.type == y.type == mask.type && y = sub(0, x);
13907 X = X.getOperand(0);
13908 Y = Y.getOperand(0);
13909 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13910 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Topper31133842011-11-19 07:33:10 +000013911 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType() &&
13912 (EltBits == 8 || EltBits == 16 || EltBits == 32)) {
13913 SDValue Sign = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X,
13914 Mask.getOperand(1));
13915 return DAG.getNode(ISD::BITCAST, DL, VT, Sign);
Craig Topper1666cb62011-11-19 07:07:26 +000013916 }
13917 // PBLENDVB only available on SSE 4.1
Craig Topperc0d82852011-11-22 00:44:41 +000013918 if (!Subtarget->hasSSE41orAVX())
Craig Topper1666cb62011-11-19 07:07:26 +000013919 return SDValue();
13920
13921 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
13922
13923 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
13924 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
13925 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
13926 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, X, Y);
13927 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000013928 }
13929 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013930
Craig Topper1666cb62011-11-19 07:07:26 +000013931 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
13932 return SDValue();
13933
Nate Begemanb65c1752010-12-17 22:55:37 +000013934 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013935 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13936 std::swap(N0, N1);
13937 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13938 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013939 if (!N0.hasOneUse() || !N1.hasOneUse())
13940 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013941
13942 SDValue ShAmt0 = N0.getOperand(1);
13943 if (ShAmt0.getValueType() != MVT::i8)
13944 return SDValue();
13945 SDValue ShAmt1 = N1.getOperand(1);
13946 if (ShAmt1.getValueType() != MVT::i8)
13947 return SDValue();
13948 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13949 ShAmt0 = ShAmt0.getOperand(0);
13950 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13951 ShAmt1 = ShAmt1.getOperand(0);
13952
13953 DebugLoc DL = N->getDebugLoc();
13954 unsigned Opc = X86ISD::SHLD;
13955 SDValue Op0 = N0.getOperand(0);
13956 SDValue Op1 = N1.getOperand(0);
13957 if (ShAmt0.getOpcode() == ISD::SUB) {
13958 Opc = X86ISD::SHRD;
13959 std::swap(Op0, Op1);
13960 std::swap(ShAmt0, ShAmt1);
13961 }
13962
Evan Cheng8b1190a2010-04-28 01:18:01 +000013963 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013964 if (ShAmt1.getOpcode() == ISD::SUB) {
13965 SDValue Sum = ShAmt1.getOperand(0);
13966 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013967 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13968 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13969 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13970 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013971 return DAG.getNode(Opc, DL, VT,
13972 Op0, Op1,
13973 DAG.getNode(ISD::TRUNCATE, DL,
13974 MVT::i8, ShAmt0));
13975 }
13976 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13977 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13978 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013979 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013980 return DAG.getNode(Opc, DL, VT,
13981 N0.getOperand(0), N1.getOperand(0),
13982 DAG.getNode(ISD::TRUNCATE, DL,
13983 MVT::i8, ShAmt0));
13984 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013985
Evan Cheng760d1942010-01-04 21:22:48 +000013986 return SDValue();
13987}
13988
Craig Topperb4c94572011-10-21 06:55:01 +000013989static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
13990 TargetLowering::DAGCombinerInfo &DCI,
13991 const X86Subtarget *Subtarget) {
13992 if (DCI.isBeforeLegalizeOps())
13993 return SDValue();
13994
13995 EVT VT = N->getValueType(0);
13996
13997 if (VT != MVT::i32 && VT != MVT::i64)
13998 return SDValue();
13999
14000 // Create BLSMSK instructions by finding X ^ (X-1)
14001 SDValue N0 = N->getOperand(0);
14002 SDValue N1 = N->getOperand(1);
14003 DebugLoc DL = N->getDebugLoc();
14004
14005 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14006 isAllOnes(N0.getOperand(1)))
14007 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
14008
14009 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14010 isAllOnes(N1.getOperand(1)))
14011 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
14012
14013 return SDValue();
14014}
14015
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014016/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
14017static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
14018 const X86Subtarget *Subtarget) {
14019 LoadSDNode *Ld = cast<LoadSDNode>(N);
14020 EVT RegVT = Ld->getValueType(0);
14021 EVT MemVT = Ld->getMemoryVT();
14022 DebugLoc dl = Ld->getDebugLoc();
14023 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14024
14025 ISD::LoadExtType Ext = Ld->getExtensionType();
14026
Nadav Rotemca6f2962011-09-18 19:00:23 +000014027 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014028 // shuffle. We need SSE4 for the shuffles.
14029 // TODO: It is possible to support ZExt by zeroing the undef values
14030 // during the shuffle phase or after the shuffle.
14031 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
14032 assert(MemVT != RegVT && "Cannot extend to the same type");
14033 assert(MemVT.isVector() && "Must load a vector from memory");
14034
14035 unsigned NumElems = RegVT.getVectorNumElements();
14036 unsigned RegSz = RegVT.getSizeInBits();
14037 unsigned MemSz = MemVT.getSizeInBits();
14038 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000014039 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014040 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
14041
14042 // Attempt to load the original value using a single load op.
14043 // Find a scalar type which is equal to the loaded word size.
14044 MVT SclrLoadTy = MVT::i8;
14045 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14046 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14047 MVT Tp = (MVT::SimpleValueType)tp;
14048 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
14049 SclrLoadTy = Tp;
14050 break;
14051 }
14052 }
14053
14054 // Proceed if a load word is found.
14055 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
14056
14057 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
14058 RegSz/SclrLoadTy.getSizeInBits());
14059
14060 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
14061 RegSz/MemVT.getScalarType().getSizeInBits());
14062 // Can't shuffle using an illegal type.
14063 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14064
14065 // Perform a single load.
14066 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
14067 Ld->getBasePtr(),
14068 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014069 Ld->isNonTemporal(), Ld->isInvariant(),
14070 Ld->getAlignment());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014071
14072 // Insert the word loaded into a vector.
14073 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
14074 LoadUnitVecVT, ScalarLoad);
14075
14076 // Bitcast the loaded value to a vector of the original element type, in
14077 // the size of the target vector type.
14078 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
14079 unsigned SizeRatio = RegSz/MemSz;
14080
14081 // Redistribute the loaded elements into the different locations.
14082 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14083 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
14084
14085 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
14086 DAG.getUNDEF(SlicedVec.getValueType()),
14087 ShuffleVec.data());
14088
14089 // Bitcast to the requested type.
14090 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
14091 // Replace the original load with the new sequence
14092 // and return the new chain.
14093 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
14094 return SDValue(ScalarLoad.getNode(), 1);
14095 }
14096
14097 return SDValue();
14098}
14099
Chris Lattner149a4e52008-02-22 02:09:43 +000014100/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014101static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000014102 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000014103 StoreSDNode *St = cast<StoreSDNode>(N);
14104 EVT VT = St->getValue().getValueType();
14105 EVT StVT = St->getMemoryVT();
14106 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000014107 SDValue StoredVal = St->getOperand(1);
14108 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14109
14110 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000014111 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
14112 // 128-bit ones. If in the future the cost becomes only one memory access the
14113 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000014114 if (VT.getSizeInBits() == 256 &&
14115 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
14116 StoredVal.getNumOperands() == 2) {
14117
14118 SDValue Value0 = StoredVal.getOperand(0);
14119 SDValue Value1 = StoredVal.getOperand(1);
14120
14121 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
14122 SDValue Ptr0 = St->getBasePtr();
14123 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
14124
14125 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
14126 St->getPointerInfo(), St->isVolatile(),
14127 St->isNonTemporal(), St->getAlignment());
14128 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
14129 St->getPointerInfo(), St->isVolatile(),
14130 St->isNonTemporal(), St->getAlignment());
14131 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
14132 }
Nadav Rotem614061b2011-08-10 19:30:14 +000014133
14134 // Optimize trunc store (of multiple scalars) to shuffle and store.
14135 // First, pack all of the elements in one place. Next, store to memory
14136 // in fewer chunks.
14137 if (St->isTruncatingStore() && VT.isVector()) {
14138 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14139 unsigned NumElems = VT.getVectorNumElements();
14140 assert(StVT != VT && "Cannot truncate to the same type");
14141 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
14142 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
14143
14144 // From, To sizes and ElemCount must be pow of two
14145 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014146 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000014147 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014148 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014149
Nadav Rotem614061b2011-08-10 19:30:14 +000014150 unsigned SizeRatio = FromSz / ToSz;
14151
14152 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
14153
14154 // Create a type on which we perform the shuffle
14155 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
14156 StVT.getScalarType(), NumElems*SizeRatio);
14157
14158 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
14159
14160 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
14161 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14162 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
14163
14164 // Can't shuffle using an illegal type
14165 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14166
14167 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
14168 DAG.getUNDEF(WideVec.getValueType()),
14169 ShuffleVec.data());
14170 // At this point all of the data is stored at the bottom of the
14171 // register. We now need to save it to mem.
14172
14173 // Find the largest store unit
14174 MVT StoreType = MVT::i8;
14175 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14176 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14177 MVT Tp = (MVT::SimpleValueType)tp;
14178 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
14179 StoreType = Tp;
14180 }
14181
14182 // Bitcast the original vector into a vector of store-size units
14183 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
14184 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
14185 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
14186 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
14187 SmallVector<SDValue, 8> Chains;
14188 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
14189 TLI.getPointerTy());
14190 SDValue Ptr = St->getBasePtr();
14191
14192 // Perform one or more big stores into memory.
14193 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
14194 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14195 StoreType, ShuffWide,
14196 DAG.getIntPtrConstant(i));
14197 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14198 St->getPointerInfo(), St->isVolatile(),
14199 St->isNonTemporal(), St->getAlignment());
14200 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14201 Chains.push_back(Ch);
14202 }
14203
14204 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14205 Chains.size());
14206 }
14207
14208
Chris Lattner149a4e52008-02-22 02:09:43 +000014209 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14210 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014211 // A preferable solution to the general problem is to figure out the right
14212 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014213
14214 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014215 if (VT.getSizeInBits() != 64)
14216 return SDValue();
14217
Devang Patel578efa92009-06-05 21:57:13 +000014218 const Function *F = DAG.getMachineFunction().getFunction();
14219 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000014220 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000014221 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000014222 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014223 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014224 isa<LoadSDNode>(St->getValue()) &&
14225 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14226 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014227 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014228 LoadSDNode *Ld = 0;
14229 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014230 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014231 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014232 // Must be a store of a load. We currently handle two cases: the load
14233 // is a direct child, and it's under an intervening TokenFactor. It is
14234 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014235 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014236 Ld = cast<LoadSDNode>(St->getChain());
14237 else if (St->getValue().hasOneUse() &&
14238 ChainVal->getOpcode() == ISD::TokenFactor) {
14239 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014240 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014241 TokenFactorIndex = i;
14242 Ld = cast<LoadSDNode>(St->getValue());
14243 } else
14244 Ops.push_back(ChainVal->getOperand(i));
14245 }
14246 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014247
Evan Cheng536e6672009-03-12 05:59:15 +000014248 if (!Ld || !ISD::isNormalLoad(Ld))
14249 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014250
Evan Cheng536e6672009-03-12 05:59:15 +000014251 // If this is not the MMX case, i.e. we are just turning i64 load/store
14252 // into f64 load/store, avoid the transformation if there are multiple
14253 // uses of the loaded value.
14254 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14255 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014256
Evan Cheng536e6672009-03-12 05:59:15 +000014257 DebugLoc LdDL = Ld->getDebugLoc();
14258 DebugLoc StDL = N->getDebugLoc();
14259 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14260 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14261 // pair instead.
14262 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014263 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014264 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14265 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014266 Ld->isNonTemporal(), Ld->isInvariant(),
14267 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014268 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014269 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014270 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014271 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014272 Ops.size());
14273 }
Evan Cheng536e6672009-03-12 05:59:15 +000014274 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014275 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014276 St->isVolatile(), St->isNonTemporal(),
14277 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014278 }
Evan Cheng536e6672009-03-12 05:59:15 +000014279
14280 // Otherwise, lower to two pairs of 32-bit loads / stores.
14281 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014282 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14283 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014284
Owen Anderson825b72b2009-08-11 20:47:22 +000014285 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014286 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014287 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014288 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014289 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014290 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014291 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014292 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014293 MinAlign(Ld->getAlignment(), 4));
14294
14295 SDValue NewChain = LoLd.getValue(1);
14296 if (TokenFactorIndex != -1) {
14297 Ops.push_back(LoLd);
14298 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014299 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014300 Ops.size());
14301 }
14302
14303 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014304 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14305 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014306
14307 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014308 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014309 St->isVolatile(), St->isNonTemporal(),
14310 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014311 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014312 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014313 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014314 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014315 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014316 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014317 }
Dan Gohman475871a2008-07-27 21:46:04 +000014318 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014319}
14320
Duncan Sands17470be2011-09-22 20:15:48 +000014321/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14322/// and return the operands for the horizontal operation in LHS and RHS. A
14323/// horizontal operation performs the binary operation on successive elements
14324/// of its first operand, then on successive elements of its second operand,
14325/// returning the resulting values in a vector. For example, if
14326/// A = < float a0, float a1, float a2, float a3 >
14327/// and
14328/// B = < float b0, float b1, float b2, float b3 >
14329/// then the result of doing a horizontal operation on A and B is
14330/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14331/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14332/// A horizontal-op B, for some already available A and B, and if so then LHS is
14333/// set to A, RHS to B, and the routine returns 'true'.
14334/// Note that the binary operation should have the property that if one of the
14335/// operands is UNDEF then the result is UNDEF.
14336static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool isCommutative) {
14337 // Look for the following pattern: if
14338 // A = < float a0, float a1, float a2, float a3 >
14339 // B = < float b0, float b1, float b2, float b3 >
14340 // and
14341 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14342 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14343 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14344 // which is A horizontal-op B.
14345
14346 // At least one of the operands should be a vector shuffle.
14347 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14348 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14349 return false;
14350
14351 EVT VT = LHS.getValueType();
14352 unsigned N = VT.getVectorNumElements();
14353
14354 // View LHS in the form
14355 // LHS = VECTOR_SHUFFLE A, B, LMask
14356 // If LHS is not a shuffle then pretend it is the shuffle
14357 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14358 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14359 // type VT.
14360 SDValue A, B;
14361 SmallVector<int, 8> LMask(N);
14362 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14363 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14364 A = LHS.getOperand(0);
14365 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14366 B = LHS.getOperand(1);
14367 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
14368 } else {
14369 if (LHS.getOpcode() != ISD::UNDEF)
14370 A = LHS;
14371 for (unsigned i = 0; i != N; ++i)
14372 LMask[i] = i;
14373 }
14374
14375 // Likewise, view RHS in the form
14376 // RHS = VECTOR_SHUFFLE C, D, RMask
14377 SDValue C, D;
14378 SmallVector<int, 8> RMask(N);
14379 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14380 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14381 C = RHS.getOperand(0);
14382 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14383 D = RHS.getOperand(1);
14384 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
14385 } else {
14386 if (RHS.getOpcode() != ISD::UNDEF)
14387 C = RHS;
14388 for (unsigned i = 0; i != N; ++i)
14389 RMask[i] = i;
14390 }
14391
14392 // Check that the shuffles are both shuffling the same vectors.
14393 if (!(A == C && B == D) && !(A == D && B == C))
14394 return false;
14395
14396 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14397 if (!A.getNode() && !B.getNode())
14398 return false;
14399
14400 // If A and B occur in reverse order in RHS, then "swap" them (which means
14401 // rewriting the mask).
14402 if (A != C)
14403 for (unsigned i = 0; i != N; ++i) {
14404 unsigned Idx = RMask[i];
14405 if (Idx < N)
14406 RMask[i] += N;
14407 else if (Idx < 2*N)
14408 RMask[i] -= N;
14409 }
14410
14411 // At this point LHS and RHS are equivalent to
14412 // LHS = VECTOR_SHUFFLE A, B, LMask
14413 // RHS = VECTOR_SHUFFLE A, B, RMask
14414 // Check that the masks correspond to performing a horizontal operation.
14415 for (unsigned i = 0; i != N; ++i) {
14416 unsigned LIdx = LMask[i], RIdx = RMask[i];
14417
14418 // Ignore any UNDEF components.
14419 if (LIdx >= 2*N || RIdx >= 2*N || (!A.getNode() && (LIdx < N || RIdx < N))
14420 || (!B.getNode() && (LIdx >= N || RIdx >= N)))
14421 continue;
14422
14423 // Check that successive elements are being operated on. If not, this is
14424 // not a horizontal operation.
14425 if (!(LIdx == 2*i && RIdx == 2*i + 1) &&
14426 !(isCommutative && LIdx == 2*i + 1 && RIdx == 2*i))
14427 return false;
14428 }
14429
14430 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14431 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14432 return true;
14433}
14434
14435/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14436static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14437 const X86Subtarget *Subtarget) {
14438 EVT VT = N->getValueType(0);
14439 SDValue LHS = N->getOperand(0);
14440 SDValue RHS = N->getOperand(1);
14441
14442 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperc0d82852011-11-22 00:44:41 +000014443 if (Subtarget->hasSSE3orAVX() && (VT == MVT::v4f32 || VT == MVT::v2f64) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014444 isHorizontalBinOp(LHS, RHS, true))
14445 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14446 return SDValue();
14447}
14448
14449/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14450static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14451 const X86Subtarget *Subtarget) {
14452 EVT VT = N->getValueType(0);
14453 SDValue LHS = N->getOperand(0);
14454 SDValue RHS = N->getOperand(1);
14455
14456 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperc0d82852011-11-22 00:44:41 +000014457 if (Subtarget->hasSSE3orAVX() && (VT == MVT::v4f32 || VT == MVT::v2f64) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014458 isHorizontalBinOp(LHS, RHS, false))
14459 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14460 return SDValue();
14461}
14462
Chris Lattner6cf73262008-01-25 06:14:17 +000014463/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14464/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014465static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014466 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14467 // F[X]OR(0.0, x) -> x
14468 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014469 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14470 if (C->getValueAPF().isPosZero())
14471 return N->getOperand(1);
14472 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14473 if (C->getValueAPF().isPosZero())
14474 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014475 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014476}
14477
14478/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014479static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014480 // FAND(0.0, x) -> 0.0
14481 // FAND(x, 0.0) -> 0.0
14482 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14483 if (C->getValueAPF().isPosZero())
14484 return N->getOperand(0);
14485 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14486 if (C->getValueAPF().isPosZero())
14487 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014488 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014489}
14490
Dan Gohmane5af2d32009-01-29 01:59:02 +000014491static SDValue PerformBTCombine(SDNode *N,
14492 SelectionDAG &DAG,
14493 TargetLowering::DAGCombinerInfo &DCI) {
14494 // BT ignores high bits in the bit index operand.
14495 SDValue Op1 = N->getOperand(1);
14496 if (Op1.hasOneUse()) {
14497 unsigned BitWidth = Op1.getValueSizeInBits();
14498 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14499 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014500 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14501 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014502 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014503 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14504 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14505 DCI.CommitTargetLoweringOpt(TLO);
14506 }
14507 return SDValue();
14508}
Chris Lattner83e6c992006-10-04 06:57:07 +000014509
Eli Friedman7a5e5552009-06-07 06:52:44 +000014510static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14511 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014512 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014513 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014514 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014515 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014516 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014517 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014518 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014519 }
14520 return SDValue();
14521}
14522
Evan Cheng2e489c42009-12-16 00:53:11 +000014523static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14524 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14525 // (and (i32 x86isd::setcc_carry), 1)
14526 // This eliminates the zext. This transformation is necessary because
14527 // ISD::SETCC is always legalized to i8.
14528 DebugLoc dl = N->getDebugLoc();
14529 SDValue N0 = N->getOperand(0);
14530 EVT VT = N->getValueType(0);
14531 if (N0.getOpcode() == ISD::AND &&
14532 N0.hasOneUse() &&
14533 N0.getOperand(0).hasOneUse()) {
14534 SDValue N00 = N0.getOperand(0);
14535 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14536 return SDValue();
14537 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14538 if (!C || C->getZExtValue() != 1)
14539 return SDValue();
14540 return DAG.getNode(ISD::AND, dl, VT,
14541 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14542 N00.getOperand(0), N00.getOperand(1)),
14543 DAG.getConstant(1, VT));
14544 }
14545
14546 return SDValue();
14547}
14548
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014549// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14550static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14551 unsigned X86CC = N->getConstantOperandVal(0);
14552 SDValue EFLAG = N->getOperand(1);
14553 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014554
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014555 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14556 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14557 // cases.
14558 if (X86CC == X86::COND_B)
14559 return DAG.getNode(ISD::AND, DL, MVT::i8,
14560 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14561 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14562 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014563
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014564 return SDValue();
14565}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014566
Benjamin Kramer1396c402011-06-18 11:09:41 +000014567static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14568 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014569 SDValue Op0 = N->getOperand(0);
14570 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14571 // a 32-bit target where SSE doesn't support i64->FP operations.
14572 if (Op0.getOpcode() == ISD::LOAD) {
14573 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14574 EVT VT = Ld->getValueType(0);
14575 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14576 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14577 !XTLI->getSubtarget()->is64Bit() &&
14578 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014579 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14580 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014581 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14582 return FILDChain;
14583 }
14584 }
14585 return SDValue();
14586}
14587
Chris Lattner23a01992010-12-20 01:37:09 +000014588// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14589static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14590 X86TargetLowering::DAGCombinerInfo &DCI) {
14591 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14592 // the result is either zero or one (depending on the input carry bit).
14593 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14594 if (X86::isZeroNode(N->getOperand(0)) &&
14595 X86::isZeroNode(N->getOperand(1)) &&
14596 // We don't have a good way to replace an EFLAGS use, so only do this when
14597 // dead right now.
14598 SDValue(N, 1).use_empty()) {
14599 DebugLoc DL = N->getDebugLoc();
14600 EVT VT = N->getValueType(0);
14601 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14602 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14603 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14604 DAG.getConstant(X86::COND_B,MVT::i8),
14605 N->getOperand(2)),
14606 DAG.getConstant(1, VT));
14607 return DCI.CombineTo(N, Res1, CarryOut);
14608 }
14609
14610 return SDValue();
14611}
14612
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014613// fold (add Y, (sete X, 0)) -> adc 0, Y
14614// (add Y, (setne X, 0)) -> sbb -1, Y
14615// (sub (sete X, 0), Y) -> sbb 0, Y
14616// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014617static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014618 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014619
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014620 // Look through ZExts.
14621 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14622 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14623 return SDValue();
14624
14625 SDValue SetCC = Ext.getOperand(0);
14626 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14627 return SDValue();
14628
14629 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14630 if (CC != X86::COND_E && CC != X86::COND_NE)
14631 return SDValue();
14632
14633 SDValue Cmp = SetCC.getOperand(1);
14634 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014635 !X86::isZeroNode(Cmp.getOperand(1)) ||
14636 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014637 return SDValue();
14638
14639 SDValue CmpOp0 = Cmp.getOperand(0);
14640 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14641 DAG.getConstant(1, CmpOp0.getValueType()));
14642
14643 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14644 if (CC == X86::COND_NE)
14645 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14646 DL, OtherVal.getValueType(), OtherVal,
14647 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14648 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14649 DL, OtherVal.getValueType(), OtherVal,
14650 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14651}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014652
Craig Topper54f952a2011-11-19 09:02:40 +000014653/// PerformADDCombine - Do target-specific dag combines on integer adds.
14654static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
14655 const X86Subtarget *Subtarget) {
14656 EVT VT = N->getValueType(0);
14657 SDValue Op0 = N->getOperand(0);
14658 SDValue Op1 = N->getOperand(1);
14659
14660 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperc0d82852011-11-22 00:44:41 +000014661 if ((Subtarget->hasSSSE3orAVX()) && (VT == MVT::v8i16 || VT == MVT::v4i32) &&
Craig Topper54f952a2011-11-19 09:02:40 +000014662 isHorizontalBinOp(Op0, Op1, true))
14663 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
14664
14665 return OptimizeConditionalInDecrement(N, DAG);
14666}
14667
14668static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
14669 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014670 SDValue Op0 = N->getOperand(0);
14671 SDValue Op1 = N->getOperand(1);
14672
14673 // X86 can't encode an immediate LHS of a sub. See if we can push the
14674 // negation into a preceding instruction.
14675 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014676 // If the RHS of the sub is a XOR with one use and a constant, invert the
14677 // immediate. Then add one to the LHS of the sub so we can turn
14678 // X-Y -> X+~Y+1, saving one register.
14679 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14680 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014681 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014682 EVT VT = Op0.getValueType();
14683 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14684 Op1.getOperand(0),
14685 DAG.getConstant(~XorC, VT));
14686 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014687 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014688 }
14689 }
14690
Craig Topper54f952a2011-11-19 09:02:40 +000014691 // Try to synthesize horizontal adds from adds of shuffles.
14692 EVT VT = N->getValueType(0);
Craig Topperc0d82852011-11-22 00:44:41 +000014693 if ((Subtarget->hasSSSE3orAVX()) && (VT == MVT::v8i16 || VT == MVT::v4i32) &&
Craig Topper54f952a2011-11-19 09:02:40 +000014694 isHorizontalBinOp(Op0, Op1, false))
14695 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
14696
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014697 return OptimizeConditionalInDecrement(N, DAG);
14698}
14699
Dan Gohman475871a2008-07-27 21:46:04 +000014700SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014701 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014702 SelectionDAG &DAG = DCI.DAG;
14703 switch (N->getOpcode()) {
14704 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014705 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014706 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014707 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014708 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014709 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Craig Topper54f952a2011-11-19 09:02:40 +000014710 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
14711 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000014712 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014713 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014714 case ISD::SHL:
14715 case ISD::SRA:
14716 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014717 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014718 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014719 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014720 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014721 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014722 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014723 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14724 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014725 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014726 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14727 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014728 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014729 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014730 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014731 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014732 case X86ISD::SHUFPS: // Handle all target specific shuffles
14733 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014734 case X86ISD::PALIGN:
Craig Topper06cb6802011-11-26 20:47:44 +000014735 case X86ISD::PUNPCKH:
14736 case X86ISD::UNPCKHP:
14737 case X86ISD::PUNPCKL:
14738 case X86ISD::UNPCKLP:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014739 case X86ISD::MOVHLPS:
14740 case X86ISD::MOVLHPS:
14741 case X86ISD::PSHUFD:
14742 case X86ISD::PSHUFHW:
14743 case X86ISD::PSHUFLW:
14744 case X86ISD::MOVSS:
14745 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000014746 case X86ISD::VPERMILP:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000014747 case X86ISD::VPERM2F128:
Craig Topper70b883b2011-11-28 10:14:51 +000014748 case X86ISD::VPERM2I128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014749 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014750 }
14751
Dan Gohman475871a2008-07-27 21:46:04 +000014752 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014753}
14754
Evan Chenge5b51ac2010-04-17 06:13:15 +000014755/// isTypeDesirableForOp - Return true if the target has native support for
14756/// the specified value type and it is 'desirable' to use the type for the
14757/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14758/// instruction encodings are longer and some i16 instructions are slow.
14759bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14760 if (!isTypeLegal(VT))
14761 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014762 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014763 return true;
14764
14765 switch (Opc) {
14766 default:
14767 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014768 case ISD::LOAD:
14769 case ISD::SIGN_EXTEND:
14770 case ISD::ZERO_EXTEND:
14771 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014772 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014773 case ISD::SRL:
14774 case ISD::SUB:
14775 case ISD::ADD:
14776 case ISD::MUL:
14777 case ISD::AND:
14778 case ISD::OR:
14779 case ISD::XOR:
14780 return false;
14781 }
14782}
14783
14784/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014785/// beneficial for dag combiner to promote the specified node. If true, it
14786/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014787bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014788 EVT VT = Op.getValueType();
14789 if (VT != MVT::i16)
14790 return false;
14791
Evan Cheng4c26e932010-04-19 19:29:22 +000014792 bool Promote = false;
14793 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014794 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014795 default: break;
14796 case ISD::LOAD: {
14797 LoadSDNode *LD = cast<LoadSDNode>(Op);
14798 // If the non-extending load has a single use and it's not live out, then it
14799 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014800 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14801 Op.hasOneUse()*/) {
14802 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14803 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14804 // The only case where we'd want to promote LOAD (rather then it being
14805 // promoted as an operand is when it's only use is liveout.
14806 if (UI->getOpcode() != ISD::CopyToReg)
14807 return false;
14808 }
14809 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014810 Promote = true;
14811 break;
14812 }
14813 case ISD::SIGN_EXTEND:
14814 case ISD::ZERO_EXTEND:
14815 case ISD::ANY_EXTEND:
14816 Promote = true;
14817 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014818 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014819 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014820 SDValue N0 = Op.getOperand(0);
14821 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014822 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014823 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014824 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014825 break;
14826 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014827 case ISD::ADD:
14828 case ISD::MUL:
14829 case ISD::AND:
14830 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014831 case ISD::XOR:
14832 Commute = true;
14833 // fallthrough
14834 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014835 SDValue N0 = Op.getOperand(0);
14836 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014837 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014838 return false;
14839 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014840 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014841 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014842 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014843 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014844 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014845 }
14846 }
14847
14848 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014849 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014850}
14851
Evan Cheng60c07e12006-07-05 22:17:51 +000014852//===----------------------------------------------------------------------===//
14853// X86 Inline Assembly Support
14854//===----------------------------------------------------------------------===//
14855
Chris Lattnerb8105652009-07-20 17:51:36 +000014856bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14857 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014858
14859 std::string AsmStr = IA->getAsmString();
14860
14861 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014862 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014863 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014864
14865 switch (AsmPieces.size()) {
14866 default: return false;
14867 case 1:
14868 AsmStr = AsmPieces[0];
14869 AsmPieces.clear();
14870 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14871
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014872 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014873 // we will turn this bswap into something that will be lowered to logical ops
14874 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14875 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014876 // bswap $0
14877 if (AsmPieces.size() == 2 &&
14878 (AsmPieces[0] == "bswap" ||
14879 AsmPieces[0] == "bswapq" ||
14880 AsmPieces[0] == "bswapl") &&
14881 (AsmPieces[1] == "$0" ||
14882 AsmPieces[1] == "${0:q}")) {
14883 // No need to check constraints, nothing other than the equivalent of
14884 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014885 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014886 if (!Ty || Ty->getBitWidth() % 16 != 0)
14887 return false;
14888 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014889 }
14890 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014891 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014892 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014893 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014894 AsmPieces[1] == "$$8," &&
14895 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014896 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14897 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014898 const std::string &ConstraintsStr = IA->getConstraintString();
14899 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014900 std::sort(AsmPieces.begin(), AsmPieces.end());
14901 if (AsmPieces.size() == 4 &&
14902 AsmPieces[0] == "~{cc}" &&
14903 AsmPieces[1] == "~{dirflag}" &&
14904 AsmPieces[2] == "~{flags}" &&
14905 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014906 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014907 if (!Ty || Ty->getBitWidth() % 16 != 0)
14908 return false;
14909 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014910 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014911 }
14912 break;
14913 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014914 if (CI->getType()->isIntegerTy(32) &&
14915 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14916 SmallVector<StringRef, 4> Words;
14917 SplitString(AsmPieces[0], Words, " \t,");
14918 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14919 Words[2] == "${0:w}") {
14920 Words.clear();
14921 SplitString(AsmPieces[1], Words, " \t,");
14922 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14923 Words[2] == "$0") {
14924 Words.clear();
14925 SplitString(AsmPieces[2], Words, " \t,");
14926 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14927 Words[2] == "${0:w}") {
14928 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014929 const std::string &ConstraintsStr = IA->getConstraintString();
14930 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014931 std::sort(AsmPieces.begin(), AsmPieces.end());
14932 if (AsmPieces.size() == 4 &&
14933 AsmPieces[0] == "~{cc}" &&
14934 AsmPieces[1] == "~{dirflag}" &&
14935 AsmPieces[2] == "~{flags}" &&
14936 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014937 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014938 if (!Ty || Ty->getBitWidth() % 16 != 0)
14939 return false;
14940 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014941 }
14942 }
14943 }
14944 }
14945 }
Evan Cheng55d42002011-01-08 01:24:27 +000014946
14947 if (CI->getType()->isIntegerTy(64)) {
14948 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14949 if (Constraints.size() >= 2 &&
14950 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14951 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14952 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14953 SmallVector<StringRef, 4> Words;
14954 SplitString(AsmPieces[0], Words, " \t");
14955 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014956 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014957 SplitString(AsmPieces[1], Words, " \t");
14958 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14959 Words.clear();
14960 SplitString(AsmPieces[2], Words, " \t,");
14961 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14962 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014963 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014964 if (!Ty || Ty->getBitWidth() % 16 != 0)
14965 return false;
14966 return IntrinsicLowering::LowerToByteSwap(CI);
14967 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014968 }
14969 }
14970 }
14971 }
14972 break;
14973 }
14974 return false;
14975}
14976
14977
14978
Chris Lattnerf4dff842006-07-11 02:54:03 +000014979/// getConstraintType - Given a constraint letter, return the type of
14980/// constraint it is for this target.
14981X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014982X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14983 if (Constraint.size() == 1) {
14984 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014985 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014986 case 'q':
14987 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014988 case 'f':
14989 case 't':
14990 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014991 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014992 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014993 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014994 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014995 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014996 case 'a':
14997 case 'b':
14998 case 'c':
14999 case 'd':
15000 case 'S':
15001 case 'D':
15002 case 'A':
15003 return C_Register;
15004 case 'I':
15005 case 'J':
15006 case 'K':
15007 case 'L':
15008 case 'M':
15009 case 'N':
15010 case 'G':
15011 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000015012 case 'e':
15013 case 'Z':
15014 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000015015 default:
15016 break;
15017 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000015018 }
Chris Lattner4234f572007-03-25 02:14:49 +000015019 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000015020}
15021
John Thompson44ab89e2010-10-29 17:29:13 +000015022/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000015023/// This object must already have been set up with the operand type
15024/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000015025TargetLowering::ConstraintWeight
15026 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000015027 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000015028 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015029 Value *CallOperandVal = info.CallOperandVal;
15030 // If we don't have a value, we can't do a match,
15031 // but allow it at the lowest weight.
15032 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000015033 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015034 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000015035 // Look at the constraint type.
15036 switch (*constraint) {
15037 default:
John Thompson44ab89e2010-10-29 17:29:13 +000015038 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
15039 case 'R':
15040 case 'q':
15041 case 'Q':
15042 case 'a':
15043 case 'b':
15044 case 'c':
15045 case 'd':
15046 case 'S':
15047 case 'D':
15048 case 'A':
15049 if (CallOperandVal->getType()->isIntegerTy())
15050 weight = CW_SpecificReg;
15051 break;
15052 case 'f':
15053 case 't':
15054 case 'u':
15055 if (type->isFloatingPointTy())
15056 weight = CW_SpecificReg;
15057 break;
15058 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000015059 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000015060 weight = CW_SpecificReg;
15061 break;
15062 case 'x':
15063 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015064 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000015065 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015066 break;
15067 case 'I':
15068 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
15069 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000015070 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015071 }
15072 break;
John Thompson44ab89e2010-10-29 17:29:13 +000015073 case 'J':
15074 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15075 if (C->getZExtValue() <= 63)
15076 weight = CW_Constant;
15077 }
15078 break;
15079 case 'K':
15080 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15081 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
15082 weight = CW_Constant;
15083 }
15084 break;
15085 case 'L':
15086 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15087 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
15088 weight = CW_Constant;
15089 }
15090 break;
15091 case 'M':
15092 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15093 if (C->getZExtValue() <= 3)
15094 weight = CW_Constant;
15095 }
15096 break;
15097 case 'N':
15098 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15099 if (C->getZExtValue() <= 0xff)
15100 weight = CW_Constant;
15101 }
15102 break;
15103 case 'G':
15104 case 'C':
15105 if (dyn_cast<ConstantFP>(CallOperandVal)) {
15106 weight = CW_Constant;
15107 }
15108 break;
15109 case 'e':
15110 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15111 if ((C->getSExtValue() >= -0x80000000LL) &&
15112 (C->getSExtValue() <= 0x7fffffffLL))
15113 weight = CW_Constant;
15114 }
15115 break;
15116 case 'Z':
15117 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15118 if (C->getZExtValue() <= 0xffffffff)
15119 weight = CW_Constant;
15120 }
15121 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015122 }
15123 return weight;
15124}
15125
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015126/// LowerXConstraint - try to replace an X constraint, which matches anything,
15127/// with another that has more specific requirements based on the type of the
15128/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000015129const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000015130LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000015131 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
15132 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000015133 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015134 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000015135 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015136 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000015137 return "x";
15138 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015139
Chris Lattner5e764232008-04-26 23:02:14 +000015140 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015141}
15142
Chris Lattner48884cd2007-08-25 00:47:38 +000015143/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
15144/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000015145void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000015146 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000015147 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000015148 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000015149 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000015150
Eric Christopher100c8332011-06-02 23:16:42 +000015151 // Only support length 1 constraints for now.
15152 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000015153
Eric Christopher100c8332011-06-02 23:16:42 +000015154 char ConstraintLetter = Constraint[0];
15155 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015156 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000015157 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000015158 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015159 if (C->getZExtValue() <= 31) {
15160 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015161 break;
15162 }
Devang Patel84f7fd22007-03-17 00:13:28 +000015163 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015164 return;
Evan Cheng364091e2008-09-22 23:57:37 +000015165 case 'J':
15166 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015167 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000015168 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15169 break;
15170 }
15171 }
15172 return;
15173 case 'K':
15174 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015175 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000015176 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15177 break;
15178 }
15179 }
15180 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000015181 case 'N':
15182 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015183 if (C->getZExtValue() <= 255) {
15184 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015185 break;
15186 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000015187 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015188 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000015189 case 'e': {
15190 // 32-bit signed value
15191 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015192 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15193 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015194 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015195 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000015196 break;
15197 }
15198 // FIXME gcc accepts some relocatable values here too, but only in certain
15199 // memory models; it's complicated.
15200 }
15201 return;
15202 }
15203 case 'Z': {
15204 // 32-bit unsigned value
15205 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015206 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15207 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015208 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15209 break;
15210 }
15211 }
15212 // FIXME gcc accepts some relocatable values here too, but only in certain
15213 // memory models; it's complicated.
15214 return;
15215 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015216 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015217 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000015218 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015219 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015220 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000015221 break;
15222 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015223
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015224 // In any sort of PIC mode addresses need to be computed at runtime by
15225 // adding in a register or some sort of table lookup. These can't
15226 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000015227 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015228 return;
15229
Chris Lattnerdc43a882007-05-03 16:52:29 +000015230 // If we are in non-pic codegen mode, we allow the address of a global (with
15231 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000015232 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015233 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000015234
Chris Lattner49921962009-05-08 18:23:14 +000015235 // Match either (GA), (GA+C), (GA+C1+C2), etc.
15236 while (1) {
15237 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15238 Offset += GA->getOffset();
15239 break;
15240 } else if (Op.getOpcode() == ISD::ADD) {
15241 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15242 Offset += C->getZExtValue();
15243 Op = Op.getOperand(0);
15244 continue;
15245 }
15246 } else if (Op.getOpcode() == ISD::SUB) {
15247 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15248 Offset += -C->getZExtValue();
15249 Op = Op.getOperand(0);
15250 continue;
15251 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015252 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015253
Chris Lattner49921962009-05-08 18:23:14 +000015254 // Otherwise, this isn't something we can handle, reject it.
15255 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015256 }
Eric Christopherfd179292009-08-27 18:07:15 +000015257
Dan Gohman46510a72010-04-15 01:51:59 +000015258 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015259 // If we require an extra load to get this address, as in PIC mode, we
15260 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015261 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15262 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015263 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015264
Devang Patel0d881da2010-07-06 22:08:15 +000015265 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15266 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015267 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015268 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015269 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015270
Gabor Greifba36cb52008-08-28 21:40:38 +000015271 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015272 Ops.push_back(Result);
15273 return;
15274 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015275 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015276}
15277
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015278std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015279X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015280 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015281 // First, see if this is a constraint that directly corresponds to an LLVM
15282 // register class.
15283 if (Constraint.size() == 1) {
15284 // GCC Constraint Letters
15285 switch (Constraint[0]) {
15286 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015287 // TODO: Slight differences here in allocation order and leaving
15288 // RIP in the class. Do they matter any more here than they do
15289 // in the normal allocation?
15290 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15291 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015292 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015293 return std::make_pair(0U, X86::GR32RegisterClass);
15294 else if (VT == MVT::i16)
15295 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015296 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015297 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015298 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015299 return std::make_pair(0U, X86::GR64RegisterClass);
15300 break;
15301 }
15302 // 32-bit fallthrough
15303 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015304 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015305 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
15306 else if (VT == MVT::i16)
15307 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015308 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015309 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
15310 else if (VT == MVT::i64)
15311 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15312 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015313 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015314 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015315 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015316 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015317 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015318 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015319 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015320 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015321 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015322 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015323 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015324 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15325 if (VT == MVT::i16)
15326 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15327 if (VT == MVT::i32 || !Subtarget->is64Bit())
15328 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15329 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015330 case 'f': // FP Stack registers.
15331 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15332 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015333 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015334 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015335 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015336 return std::make_pair(0U, X86::RFP64RegisterClass);
15337 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015338 case 'y': // MMX_REGS if MMX allowed.
15339 if (!Subtarget->hasMMX()) break;
15340 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015341 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015342 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015343 // FALL THROUGH.
15344 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015345 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015346
Owen Anderson825b72b2009-08-11 20:47:22 +000015347 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015348 default: break;
15349 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015350 case MVT::f32:
15351 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015352 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015353 case MVT::f64:
15354 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015355 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015356 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015357 case MVT::v16i8:
15358 case MVT::v8i16:
15359 case MVT::v4i32:
15360 case MVT::v2i64:
15361 case MVT::v4f32:
15362 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015363 return std::make_pair(0U, X86::VR128RegisterClass);
15364 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015365 break;
15366 }
15367 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015368
Chris Lattnerf76d1802006-07-31 23:26:50 +000015369 // Use the default implementation in TargetLowering to convert the register
15370 // constraint into a member of a register class.
15371 std::pair<unsigned, const TargetRegisterClass*> Res;
15372 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015373
15374 // Not found as a standard register?
15375 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015376 // Map st(0) -> st(7) -> ST0
15377 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15378 tolower(Constraint[1]) == 's' &&
15379 tolower(Constraint[2]) == 't' &&
15380 Constraint[3] == '(' &&
15381 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15382 Constraint[5] == ')' &&
15383 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015384
Chris Lattner56d77c72009-09-13 22:41:48 +000015385 Res.first = X86::ST0+Constraint[4]-'0';
15386 Res.second = X86::RFP80RegisterClass;
15387 return Res;
15388 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015389
Chris Lattner56d77c72009-09-13 22:41:48 +000015390 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015391 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015392 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015393 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015394 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015395 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015396
15397 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015398 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015399 Res.first = X86::EFLAGS;
15400 Res.second = X86::CCRRegisterClass;
15401 return Res;
15402 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015403
Dale Johannesen330169f2008-11-13 21:52:36 +000015404 // 'A' means EAX + EDX.
15405 if (Constraint == "A") {
15406 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015407 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015408 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015409 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015410 return Res;
15411 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015412
Chris Lattnerf76d1802006-07-31 23:26:50 +000015413 // Otherwise, check to see if this is a register class of the wrong value
15414 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15415 // turn into {ax},{dx}.
15416 if (Res.second->hasType(VT))
15417 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015418
Chris Lattnerf76d1802006-07-31 23:26:50 +000015419 // All of the single-register GCC register classes map their values onto
15420 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15421 // really want an 8-bit or 32-bit register, map to the appropriate register
15422 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015423 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015424 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015425 unsigned DestReg = 0;
15426 switch (Res.first) {
15427 default: break;
15428 case X86::AX: DestReg = X86::AL; break;
15429 case X86::DX: DestReg = X86::DL; break;
15430 case X86::CX: DestReg = X86::CL; break;
15431 case X86::BX: DestReg = X86::BL; break;
15432 }
15433 if (DestReg) {
15434 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015435 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015436 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015437 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015438 unsigned DestReg = 0;
15439 switch (Res.first) {
15440 default: break;
15441 case X86::AX: DestReg = X86::EAX; break;
15442 case X86::DX: DestReg = X86::EDX; break;
15443 case X86::CX: DestReg = X86::ECX; break;
15444 case X86::BX: DestReg = X86::EBX; break;
15445 case X86::SI: DestReg = X86::ESI; break;
15446 case X86::DI: DestReg = X86::EDI; break;
15447 case X86::BP: DestReg = X86::EBP; break;
15448 case X86::SP: DestReg = X86::ESP; break;
15449 }
15450 if (DestReg) {
15451 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015452 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015453 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015454 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015455 unsigned DestReg = 0;
15456 switch (Res.first) {
15457 default: break;
15458 case X86::AX: DestReg = X86::RAX; break;
15459 case X86::DX: DestReg = X86::RDX; break;
15460 case X86::CX: DestReg = X86::RCX; break;
15461 case X86::BX: DestReg = X86::RBX; break;
15462 case X86::SI: DestReg = X86::RSI; break;
15463 case X86::DI: DestReg = X86::RDI; break;
15464 case X86::BP: DestReg = X86::RBP; break;
15465 case X86::SP: DestReg = X86::RSP; break;
15466 }
15467 if (DestReg) {
15468 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015469 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015470 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015471 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015472 } else if (Res.second == X86::FR32RegisterClass ||
15473 Res.second == X86::FR64RegisterClass ||
15474 Res.second == X86::VR128RegisterClass) {
15475 // Handle references to XMM physical registers that got mapped into the
15476 // wrong class. This can happen with constraints like {xmm0} where the
15477 // target independent register mapper will just pick the first match it can
15478 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015479 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015480 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015481 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015482 Res.second = X86::FR64RegisterClass;
15483 else if (X86::VR128RegisterClass->hasType(VT))
15484 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015485 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015486
Chris Lattnerf76d1802006-07-31 23:26:50 +000015487 return Res;
15488}