blob: a214c06c3b524f7d8dd3d5e85c0348ce8ef2d282 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000043#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000044#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000045#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000047#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000048#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000049#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000050#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000053#include "llvm/Target/TargetOptions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000055using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000056
Evan Chengb1712452010-01-27 06:25:16 +000057STATISTIC(NumTailCalls, "Number of tail calls");
58
Evan Cheng10e86422008-04-25 19:11:04 +000059// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000060static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000061 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000062
David Greenea5f26012011-02-07 19:36:54 +000063static SDValue Insert128BitVector(SDValue Result,
64 SDValue Vec,
65 SDValue Idx,
66 SelectionDAG &DAG,
67 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000068
David Greenea5f26012011-02-07 19:36:54 +000069static SDValue Extract128BitVector(SDValue Vec,
70 SDValue Idx,
71 SelectionDAG &DAG,
72 DebugLoc dl);
73
74/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
75/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000076/// simple subregister reference. Idx is an index in the 128 bits we
77/// want. It need not be aligned to a 128-bit bounday. That makes
78/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000079static SDValue Extract128BitVector(SDValue Vec,
80 SDValue Idx,
81 SelectionDAG &DAG,
82 DebugLoc dl) {
83 EVT VT = Vec.getValueType();
84 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000085 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000086 int Factor = VT.getSizeInBits()/128;
87 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
88 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000089
90 // Extract from UNDEF is UNDEF.
91 if (Vec.getOpcode() == ISD::UNDEF)
92 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
93
94 if (isa<ConstantSDNode>(Idx)) {
95 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
96
97 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
98 // we can match to VEXTRACTF128.
99 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
100
101 // This is the index of the first element of the 128-bit chunk
102 // we want.
103 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
104 * ElemsPerChunk);
105
106 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000107 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
108 VecIdx);
109
110 return Result;
111 }
112
113 return SDValue();
114}
115
116/// Generate a DAG to put 128-bits into a vector > 128 bits. This
117/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000118/// simple superregister reference. Idx is an index in the 128 bits
119/// we want. It need not be aligned to a 128-bit bounday. That makes
120/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000121static SDValue Insert128BitVector(SDValue Result,
122 SDValue Vec,
123 SDValue Idx,
124 SelectionDAG &DAG,
125 DebugLoc dl) {
126 if (isa<ConstantSDNode>(Idx)) {
127 EVT VT = Vec.getValueType();
128 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
129
130 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000131 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000132 EVT ResultVT = Result.getValueType();
133
134 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000135 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000136
137 // This is the index of the first element of the 128-bit chunk
138 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000139 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000140 * ElemsPerChunk);
141
142 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000143 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
144 VecIdx);
145 return Result;
146 }
147
148 return SDValue();
149}
150
Chris Lattnerf0144122009-07-28 03:13:23 +0000151static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000152 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
153 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000154
Evan Cheng2bffee22011-02-01 01:14:13 +0000155 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000156 if (is64Bit)
157 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000158 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000159 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000160
Evan Cheng203576a2011-07-20 19:50:42 +0000161 if (Subtarget->isTargetELF())
162 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000163 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000164 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000165 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000166}
167
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000168X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000169 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000170 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000171 X86ScalarSSEf64 = Subtarget->hasXMMInt();
172 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000173 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000174
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000175 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000176 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000177
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000178 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000179 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000180
181 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000182 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000183 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
184 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000185
Eric Christopherde5e1012011-03-11 01:05:58 +0000186 // For 64-bit since we have so many registers use the ILP scheduler, for
187 // 32-bit code use the register pressure specific scheduling.
188 if (Subtarget->is64Bit())
189 setSchedulingPreference(Sched::ILP);
190 else
191 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000192 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000193
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000194 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000195 // Setup Windows compiler runtime calls.
196 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000197 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000198 setLibcallName(RTLIB::SREM_I64, "_allrem");
199 setLibcallName(RTLIB::UREM_I64, "_aullrem");
200 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000201 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000202 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000203 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000204 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000205 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
206 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000208 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
209 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000210 }
211
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000212 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000213 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000214 setUseUnderscoreSetJmp(false);
215 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000216 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000217 // MS runtime is weird: it exports _setjmp, but longjmp!
218 setUseUnderscoreSetJmp(true);
219 setUseUnderscoreLongJmp(false);
220 } else {
221 setUseUnderscoreSetJmp(true);
222 setUseUnderscoreLongJmp(true);
223 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000224
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000227 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000231
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000233
Scott Michelfdc40a02009-02-17 22:15:04 +0000234 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000236 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000238 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
240 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000241
242 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
244 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
246 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000249
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000250 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
251 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
253 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000255
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
258 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000259 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000260 // We have an algorithm for SSE2->double, and we turn this into a
261 // 64-bit FILD followed by conditional FADD for other targets.
262 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000263 // We have an algorithm for SSE2, and we turn this into a 64-bit
264 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000265 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000266 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000267
268 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
269 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
271 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000272
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000273 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000274 // SSE has no i16 to fp conversion, only i32
275 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000277 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000279 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
281 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000282 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000283 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
285 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000286 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000287
Dale Johannesen73328d12007-09-19 23:55:34 +0000288 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
289 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
291 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000292
Evan Cheng02568ff2006-01-30 22:13:22 +0000293 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
294 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
296 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000297
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000298 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000300 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000302 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
304 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000305 }
306
307 // Handle FP_TO_UINT by promoting the destination to a larger signed
308 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
310 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000312
Evan Cheng25ab6902006-09-08 06:48:29 +0000313 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
315 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000316 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000317 // Since AVX is a superset of SSE3, only check for SSE here.
318 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000319 // Expand FP_TO_UINT into a select.
320 // FIXME: We would like to use a Custom expander here eventually to do
321 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000323 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000324 // With SSE3 we can use fisttpll to convert to a signed i64; without
325 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000327 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000328
Chris Lattner399610a2006-12-05 18:22:22 +0000329 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000330 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000331 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
332 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000333 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000335 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000336 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000337 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000338 }
Chris Lattner21f66852005-12-23 05:15:23 +0000339
Dan Gohmanb00ee212008-02-18 19:34:53 +0000340 // Scalar integer divide and remainder are lowered to use operations that
341 // produce two results, to match the available instructions. This exposes
342 // the two-result form to trivial CSE, which is able to combine x/y and x%y
343 // into a single instruction.
344 //
345 // Scalar integer multiply-high is also lowered to use two-result
346 // operations, to match the available instructions. However, plain multiply
347 // (low) operations are left as Legal, as there are single-result
348 // instructions for this in x86. Using the two-result multiply instructions
349 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000350 for (unsigned i = 0, e = 4; i != e; ++i) {
351 MVT VT = IntVTs[i];
352 setOperationAction(ISD::MULHS, VT, Expand);
353 setOperationAction(ISD::MULHU, VT, Expand);
354 setOperationAction(ISD::SDIV, VT, Expand);
355 setOperationAction(ISD::UDIV, VT, Expand);
356 setOperationAction(ISD::SREM, VT, Expand);
357 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000358
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000359 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000360 setOperationAction(ISD::ADDC, VT, Custom);
361 setOperationAction(ISD::ADDE, VT, Custom);
362 setOperationAction(ISD::SUBC, VT, Custom);
363 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000364 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000365
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
367 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
368 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
369 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000370 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
375 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
376 setOperationAction(ISD::FREM , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f64 , Expand);
378 setOperationAction(ISD::FREM , MVT::f80 , Expand);
379 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000380
Craig Topper909652f2011-10-14 03:21:46 +0000381 if (Subtarget->hasBMI()) {
382 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
383 } else {
384 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
385 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
386 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
387 if (Subtarget->is64Bit())
388 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
389 }
Craig Topper37f21672011-10-11 06:44:02 +0000390
391 if (Subtarget->hasLZCNT()) {
392 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
393 } else {
394 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
395 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
396 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
397 if (Subtarget->is64Bit())
398 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000399 }
400
Benjamin Kramer1292c222010-12-04 20:32:23 +0000401 if (Subtarget->hasPOPCNT()) {
402 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
403 } else {
404 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
405 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
406 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
407 if (Subtarget->is64Bit())
408 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
409 }
410
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
412 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000413
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000414 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000415 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000416 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000417 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000418 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
420 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
421 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
422 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
423 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000424 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
426 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
427 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
428 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000429 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000431 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000432 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000434
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000435 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
437 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
438 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
439 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000440 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
442 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000443 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000444 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
446 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
447 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
448 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000449 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000450 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000451 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
453 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
454 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000455 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
457 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
458 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000459 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000460
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000461 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000463
Eric Christopher9a9d2752010-07-22 02:48:34 +0000464 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000465 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000466
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000467 // On X86 and X86-64, atomic operations are lowered to locked instructions.
468 // Locked instructions, in turn, have implicit fence semantics (all memory
469 // operations are flushed before issuing the locked instruction, and they
470 // are not buffered), so we can fold away the common pattern of
471 // fence-atomic-fence.
472 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000473
Mon P Wang63307c32008-05-05 19:05:59 +0000474 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000475 for (unsigned i = 0, e = 4; i != e; ++i) {
476 MVT VT = IntVTs[i];
477 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
478 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000479 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000480 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000481
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000482 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000483 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
485 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
486 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
487 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
488 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
489 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
490 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000491 }
492
Eli Friedman43f51ae2011-08-26 21:21:21 +0000493 if (Subtarget->hasCmpxchg16b()) {
494 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
495 }
496
Evan Cheng3c992d22006-03-07 02:02:57 +0000497 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000498 if (!Subtarget->isTargetDarwin() &&
499 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000500 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000502 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000503
Owen Anderson825b72b2009-08-11 20:47:22 +0000504 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
505 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
506 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
507 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000508 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000509 setExceptionPointerRegister(X86::RAX);
510 setExceptionSelectorRegister(X86::RDX);
511 } else {
512 setExceptionPointerRegister(X86::EAX);
513 setExceptionSelectorRegister(X86::EDX);
514 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
516 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000517
Duncan Sands4a544a72011-09-06 13:37:06 +0000518 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
519 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000520
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000522
Nate Begemanacc398c2006-01-25 18:21:52 +0000523 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000524 setOperationAction(ISD::VASTART , MVT::Other, Custom);
525 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000526 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 setOperationAction(ISD::VAARG , MVT::Other, Custom);
528 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000529 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::VAARG , MVT::Other, Expand);
531 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000532 }
Evan Chengae642192007-03-02 23:16:35 +0000533
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
535 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000536
537 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
538 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
539 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000540 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000541 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
542 MVT::i64 : MVT::i32, Custom);
543 else
544 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
545 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000546
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000547 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000548 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000549 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
551 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000552
Evan Cheng223547a2006-01-31 22:28:30 +0000553 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 setOperationAction(ISD::FABS , MVT::f64, Custom);
555 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000556
557 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 setOperationAction(ISD::FNEG , MVT::f64, Custom);
559 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000560
Evan Cheng68c47cb2007-01-05 07:55:56 +0000561 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
563 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000564
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000565 // Lower this to FGETSIGNx86 plus an AND.
566 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
567 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
568
Evan Chengd25e9e82006-02-02 00:28:23 +0000569 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setOperationAction(ISD::FSIN , MVT::f64, Expand);
571 setOperationAction(ISD::FCOS , MVT::f64, Expand);
572 setOperationAction(ISD::FSIN , MVT::f32, Expand);
573 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000574
Chris Lattnera54aa942006-01-29 06:26:08 +0000575 // Expand FP immediates into loads from the stack, except for the special
576 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000577 addLegalFPImmediate(APFloat(+0.0)); // xorpd
578 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000579 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000580 // Use SSE for f32, x87 for f64.
581 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
583 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000584
585 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000587
588 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000590
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
593 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
595 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000596
597 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::FSIN , MVT::f32, Expand);
599 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600
Nate Begemane1795842008-02-14 08:57:00 +0000601 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602 addLegalFPImmediate(APFloat(+0.0f)); // xorps
603 addLegalFPImmediate(APFloat(+0.0)); // FLD0
604 addLegalFPImmediate(APFloat(+1.0)); // FLD1
605 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
606 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
607
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000608 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
610 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000611 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000612 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000613 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
616 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000617
Owen Anderson825b72b2009-08-11 20:47:22 +0000618 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
619 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
620 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
621 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000622
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000623 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
625 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000627 addLegalFPImmediate(APFloat(+0.0)); // FLD0
628 addLegalFPImmediate(APFloat(+1.0)); // FLD1
629 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
630 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000631 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
632 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
633 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
634 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000635 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000636
Cameron Zwarich33390842011-07-08 21:39:21 +0000637 // We don't support FMA.
638 setOperationAction(ISD::FMA, MVT::f64, Expand);
639 setOperationAction(ISD::FMA, MVT::f32, Expand);
640
Dale Johannesen59a58732007-08-05 18:49:15 +0000641 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000642 if (!TM.Options.UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
644 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
645 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000646 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000647 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000648 addLegalFPImmediate(TmpFlt); // FLD0
649 TmpFlt.changeSign();
650 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000651
652 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000653 APFloat TmpFlt2(+1.0);
654 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
655 &ignored);
656 addLegalFPImmediate(TmpFlt2); // FLD1
657 TmpFlt2.changeSign();
658 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
659 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000660
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000661 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
663 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000664 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000665
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.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000751 if (!TM.Options.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
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000788 if (!TM.Options.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
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000805 if (!TM.Options.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
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000986 if (!TM.Options.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;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001214 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001215 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001216
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001217 setPrefFunctionAlignment(4); // 2^4 bytes.
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.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001712static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1713 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001714 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001715}
1716
Dan Gohman98ca4f22009-08-05 01:29:28 +00001717SDValue
1718X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001719 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001720 const SmallVectorImpl<ISD::InputArg> &Ins,
1721 DebugLoc dl, SelectionDAG &DAG,
1722 const CCValAssign &VA,
1723 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001724 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001725 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001726 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001727 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1728 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001729 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001730 EVT ValVT;
1731
1732 // If value is passed by pointer we have address passed instead of the value
1733 // itself.
1734 if (VA.getLocInfo() == CCValAssign::Indirect)
1735 ValVT = VA.getLocVT();
1736 else
1737 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001738
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001739 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001740 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001741 // In case of tail call optimization mark all arguments mutable. Since they
1742 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001743 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001744 unsigned Bytes = Flags.getByValSize();
1745 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1746 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001747 return DAG.getFrameIndex(FI, getPointerTy());
1748 } else {
1749 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001750 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001751 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1752 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001753 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001754 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001755 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001756}
1757
Dan Gohman475871a2008-07-27 21:46:04 +00001758SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001759X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001760 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001761 bool isVarArg,
1762 const SmallVectorImpl<ISD::InputArg> &Ins,
1763 DebugLoc dl,
1764 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001765 SmallVectorImpl<SDValue> &InVals)
1766 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001767 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001768 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001769
Gordon Henriksen86737662008-01-05 16:56:59 +00001770 const Function* Fn = MF.getFunction();
1771 if (Fn->hasExternalLinkage() &&
1772 Subtarget->isTargetCygMing() &&
1773 Fn->getName() == "main")
1774 FuncInfo->setForceFramePointer(true);
1775
Evan Cheng1bc78042006-04-26 01:20:17 +00001776 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001777 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001778 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001779
Chris Lattner29689432010-03-11 00:22:57 +00001780 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1781 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001782
Chris Lattner638402b2007-02-28 07:00:42 +00001783 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001784 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001785 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001786 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001787
1788 // Allocate shadow area for Win64
1789 if (IsWin64) {
1790 CCInfo.AllocateStack(32, 8);
1791 }
1792
Duncan Sands45907662010-10-31 13:21:44 +00001793 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001794
Chris Lattnerf39f7712007-02-28 05:46:49 +00001795 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001796 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001797 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1798 CCValAssign &VA = ArgLocs[i];
1799 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1800 // places.
1801 assert(VA.getValNo() != LastVal &&
1802 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001803 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001804 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001805
Chris Lattnerf39f7712007-02-28 05:46:49 +00001806 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001807 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001808 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001809 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001810 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001811 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001812 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001813 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001814 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001815 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001816 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001817 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1818 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001819 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001820 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001821 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001822 RC = X86::VR64RegisterClass;
1823 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001824 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001825
Devang Patel68e6bee2011-02-21 23:21:26 +00001826 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001827 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001828
Chris Lattnerf39f7712007-02-28 05:46:49 +00001829 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1830 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1831 // right size.
1832 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001833 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001834 DAG.getValueType(VA.getValVT()));
1835 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001836 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001837 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001838 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001839 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001840
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001841 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001842 // Handle MMX values passed in XMM regs.
1843 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001844 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1845 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001846 } else
1847 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001848 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001849 } else {
1850 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001851 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001852 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001853
1854 // If value is passed via pointer - do a load.
1855 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001856 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001857 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001858
Dan Gohman98ca4f22009-08-05 01:29:28 +00001859 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001860 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001861
Dan Gohman61a92132008-04-21 23:59:07 +00001862 // The x86-64 ABI for returning structs by value requires that we copy
1863 // the sret argument into %rax for the return. Save the argument into
1864 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001865 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001866 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1867 unsigned Reg = FuncInfo->getSRetReturnReg();
1868 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001869 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001870 FuncInfo->setSRetReturnReg(Reg);
1871 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001872 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001873 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001874 }
1875
Chris Lattnerf39f7712007-02-28 05:46:49 +00001876 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001877 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001878 if (FuncIsMadeTailCallSafe(CallConv,
1879 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001880 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001881
Evan Cheng1bc78042006-04-26 01:20:17 +00001882 // If the function takes variable number of arguments, make a frame index for
1883 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001884 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001885 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1886 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001887 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001888 }
1889 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001890 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1891
1892 // FIXME: We should really autogenerate these arrays
1893 static const unsigned GPR64ArgRegsWin64[] = {
1894 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001895 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001896 static const unsigned GPR64ArgRegs64Bit[] = {
1897 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1898 };
1899 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001900 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1901 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1902 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001903 const unsigned *GPR64ArgRegs;
1904 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001905
1906 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001907 // The XMM registers which might contain var arg parameters are shadowed
1908 // in their paired GPR. So we only need to save the GPR to their home
1909 // slots.
1910 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001911 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001912 } else {
1913 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1914 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001915
1916 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001917 }
1918 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1919 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001920
Devang Patel578efa92009-06-05 21:57:13 +00001921 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001922 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001923 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001924 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
1925 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001926 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001927 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
1928 !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001929 // Kernel mode asks for SSE to be disabled, so don't push them
1930 // on the stack.
1931 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001932
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001933 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001934 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001935 // Get to the caller-allocated home save location. Add 8 to account
1936 // for the return address.
1937 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001938 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001939 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001940 // Fixup to set vararg frame on shadow area (4 x i64).
1941 if (NumIntRegs < 4)
1942 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001943 } else {
1944 // For X86-64, if there are vararg parameters that are passed via
1945 // registers, then we must store them to their spots on the stack so they
1946 // may be loaded by deferencing the result of va_next.
1947 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1948 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1949 FuncInfo->setRegSaveFrameIndex(
1950 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001951 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001952 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001953
Gordon Henriksen86737662008-01-05 16:56:59 +00001954 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001955 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001956 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1957 getPointerTy());
1958 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001959 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001960 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1961 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001962 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001963 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001964 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001965 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001966 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001967 MachinePointerInfo::getFixedStack(
1968 FuncInfo->getRegSaveFrameIndex(), Offset),
1969 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001970 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001971 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001972 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001973
Dan Gohmanface41a2009-08-16 21:24:25 +00001974 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1975 // Now store the XMM (fp + vector) parameter registers.
1976 SmallVector<SDValue, 11> SaveXMMOps;
1977 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001978
Devang Patel68e6bee2011-02-21 23:21:26 +00001979 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001980 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1981 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001982
Dan Gohman1e93df62010-04-17 14:41:14 +00001983 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1984 FuncInfo->getRegSaveFrameIndex()));
1985 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1986 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001987
Dan Gohmanface41a2009-08-16 21:24:25 +00001988 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001989 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001990 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001991 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1992 SaveXMMOps.push_back(Val);
1993 }
1994 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1995 MVT::Other,
1996 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001997 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001998
1999 if (!MemOps.empty())
2000 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2001 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002002 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002003 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002004
Gordon Henriksen86737662008-01-05 16:56:59 +00002005 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002006 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2007 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002008 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002009 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002010 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002011 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00002012 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00002013 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002014 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002015
Gordon Henriksen86737662008-01-05 16:56:59 +00002016 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002017 // RegSaveFrameIndex is X86-64 only.
2018 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002019 if (CallConv == CallingConv::X86_FastCall ||
2020 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002021 // fastcc functions can't have varargs.
2022 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002023 }
Evan Cheng25caf632006-05-23 21:06:34 +00002024
Rafael Espindola76927d752011-08-30 19:39:58 +00002025 FuncInfo->setArgumentStackSize(StackSize);
2026
Dan Gohman98ca4f22009-08-05 01:29:28 +00002027 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002028}
2029
Dan Gohman475871a2008-07-27 21:46:04 +00002030SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002031X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2032 SDValue StackPtr, SDValue Arg,
2033 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002034 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002035 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002036 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002037 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002038 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002039 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002040 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002041
2042 return DAG.getStore(Chain, dl, Arg, PtrOff,
2043 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002044 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002045}
2046
Bill Wendling64e87322009-01-16 19:25:27 +00002047/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002048/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002049SDValue
2050X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002051 SDValue &OutRetAddr, SDValue Chain,
2052 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002053 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002054 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002055 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002056 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002057
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002058 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002059 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002060 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002061 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002062}
2063
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002064/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002065/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002066static SDValue
2067EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002068 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002069 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002070 // Store the return address to the appropriate stack slot.
2071 if (!FPDiff) return Chain;
2072 // Calculate the new stack slot for the return address.
2073 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002074 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002075 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002076 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002077 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002078 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002079 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002080 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002081 return Chain;
2082}
2083
Dan Gohman98ca4f22009-08-05 01:29:28 +00002084SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002085X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002086 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002087 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002088 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002089 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002090 const SmallVectorImpl<ISD::InputArg> &Ins,
2091 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002092 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002093 MachineFunction &MF = DAG.getMachineFunction();
2094 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002095 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002096 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002097 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002098
Evan Cheng5f941932010-02-05 02:21:12 +00002099 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002100 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002101 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2102 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002103 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002104
2105 // Sibcalls are automatically detected tailcalls which do not require
2106 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002107 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002108 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002109
2110 if (isTailCall)
2111 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002112 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002113
Chris Lattner29689432010-03-11 00:22:57 +00002114 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2115 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002116
Chris Lattner638402b2007-02-28 07:00:42 +00002117 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002118 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002119 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002120 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002121
2122 // Allocate shadow area for Win64
2123 if (IsWin64) {
2124 CCInfo.AllocateStack(32, 8);
2125 }
2126
Duncan Sands45907662010-10-31 13:21:44 +00002127 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002128
Chris Lattner423c5f42007-02-28 05:31:48 +00002129 // Get a count of how many bytes are to be pushed on the stack.
2130 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002131 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002132 // This is a sibcall. The memory operands are available in caller's
2133 // own caller's stack.
2134 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002135 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2136 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002137 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002138
Gordon Henriksen86737662008-01-05 16:56:59 +00002139 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002140 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002141 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002142 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002143 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2144 FPDiff = NumBytesCallerPushed - NumBytes;
2145
2146 // Set the delta of movement of the returnaddr stackslot.
2147 // But only set if delta is greater than previous delta.
2148 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2149 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2150 }
2151
Evan Chengf22f9b32010-02-06 03:28:46 +00002152 if (!IsSibcall)
2153 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002154
Dan Gohman475871a2008-07-27 21:46:04 +00002155 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002156 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002157 if (isTailCall && FPDiff)
2158 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2159 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002160
Dan Gohman475871a2008-07-27 21:46:04 +00002161 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2162 SmallVector<SDValue, 8> MemOpChains;
2163 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002164
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002165 // Walk the register/memloc assignments, inserting copies/loads. In the case
2166 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002167 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2168 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002169 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002170 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002171 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002172 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002173
Chris Lattner423c5f42007-02-28 05:31:48 +00002174 // Promote the value if needed.
2175 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002176 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002177 case CCValAssign::Full: break;
2178 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002179 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002180 break;
2181 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002182 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002183 break;
2184 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002185 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2186 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002187 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002188 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2189 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002190 } else
2191 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2192 break;
2193 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002194 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002195 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002196 case CCValAssign::Indirect: {
2197 // Store the argument.
2198 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002199 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002200 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002201 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002202 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002203 Arg = SpillSlot;
2204 break;
2205 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002206 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002207
Chris Lattner423c5f42007-02-28 05:31:48 +00002208 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002209 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2210 if (isVarArg && IsWin64) {
2211 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2212 // shadow reg if callee is a varargs function.
2213 unsigned ShadowReg = 0;
2214 switch (VA.getLocReg()) {
2215 case X86::XMM0: ShadowReg = X86::RCX; break;
2216 case X86::XMM1: ShadowReg = X86::RDX; break;
2217 case X86::XMM2: ShadowReg = X86::R8; break;
2218 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002219 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002220 if (ShadowReg)
2221 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002222 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002223 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002224 assert(VA.isMemLoc());
2225 if (StackPtr.getNode() == 0)
2226 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2227 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2228 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002229 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002230 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002231
Evan Cheng32fe1032006-05-25 00:59:30 +00002232 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002233 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002234 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002235
Evan Cheng347d5f72006-04-28 21:29:37 +00002236 // Build a sequence of copy-to-reg nodes chained together with token chain
2237 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002238 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002239 // Tail call byval lowering might overwrite argument registers so in case of
2240 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002241 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002242 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002243 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002244 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002245 InFlag = Chain.getValue(1);
2246 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002247
Chris Lattner88e1fd52009-07-09 04:24:46 +00002248 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002249 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2250 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002251 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002252 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2253 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002254 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002255 InFlag);
2256 InFlag = Chain.getValue(1);
2257 } else {
2258 // If we are tail calling and generating PIC/GOT style code load the
2259 // address of the callee into ECX. The value in ecx is used as target of
2260 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2261 // for tail calls on PIC/GOT architectures. Normally we would just put the
2262 // address of GOT into ebx and then call target@PLT. But for tail calls
2263 // ebx would be restored (since ebx is callee saved) before jumping to the
2264 // target@PLT.
2265
2266 // Note: The actual moving to ECX is done further down.
2267 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2268 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2269 !G->getGlobal()->hasProtectedVisibility())
2270 Callee = LowerGlobalAddress(Callee, DAG);
2271 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002272 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002273 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002274 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002275
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002276 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002277 // From AMD64 ABI document:
2278 // For calls that may call functions that use varargs or stdargs
2279 // (prototype-less calls or calls to functions containing ellipsis (...) in
2280 // the declaration) %al is used as hidden argument to specify the number
2281 // of SSE registers used. The contents of %al do not need to match exactly
2282 // the number of registers, but must be an ubound on the number of SSE
2283 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002284
Gordon Henriksen86737662008-01-05 16:56:59 +00002285 // Count the number of XMM registers allocated.
2286 static const unsigned XMMArgRegs[] = {
2287 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2288 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2289 };
2290 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002291 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002292 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002293
Dale Johannesendd64c412009-02-04 00:33:20 +00002294 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002295 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002296 InFlag = Chain.getValue(1);
2297 }
2298
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002299
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002300 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002301 if (isTailCall) {
2302 // Force all the incoming stack arguments to be loaded from the stack
2303 // before any new outgoing arguments are stored to the stack, because the
2304 // outgoing stack slots may alias the incoming argument stack slots, and
2305 // the alias isn't otherwise explicit. This is slightly more conservative
2306 // than necessary, because it means that each store effectively depends
2307 // on every argument instead of just those arguments it would clobber.
2308 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2309
Dan Gohman475871a2008-07-27 21:46:04 +00002310 SmallVector<SDValue, 8> MemOpChains2;
2311 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002312 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002313 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002314 InFlag = SDValue();
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002315 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002316 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2317 CCValAssign &VA = ArgLocs[i];
2318 if (VA.isRegLoc())
2319 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002320 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002321 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002322 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002323 // Create frame index.
2324 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002325 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002326 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002327 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002328
Duncan Sands276dcbd2008-03-21 09:14:45 +00002329 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002330 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002331 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002332 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002333 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002334 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002335 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002336
Dan Gohman98ca4f22009-08-05 01:29:28 +00002337 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2338 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002339 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002340 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002341 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002342 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002343 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002344 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002345 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002346 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002347 }
2348 }
2349
2350 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002351 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002352 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002353
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002354 // Copy arguments to their registers.
2355 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002356 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002357 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002358 InFlag = Chain.getValue(1);
2359 }
Dan Gohman475871a2008-07-27 21:46:04 +00002360 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002361
Gordon Henriksen86737662008-01-05 16:56:59 +00002362 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002363 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002364 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002365 }
2366
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002367 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2368 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2369 // In the 64-bit large code model, we have to make all calls
2370 // through a register, since the call instruction's 32-bit
2371 // pc-relative offset may not be large enough to hold the whole
2372 // address.
2373 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002374 // If the callee is a GlobalAddress node (quite common, every direct call
2375 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2376 // it.
2377
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002378 // We should use extra load for direct calls to dllimported functions in
2379 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002380 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002381 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002382 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002383 bool ExtraLoad = false;
2384 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002385
Chris Lattner48a7d022009-07-09 05:02:21 +00002386 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2387 // external symbols most go through the PLT in PIC mode. If the symbol
2388 // has hidden or protected visibility, or if it is static or local, then
2389 // we don't need to use the PLT - we can directly call it.
2390 if (Subtarget->isTargetELF() &&
2391 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002392 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002393 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002394 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002395 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002396 (!Subtarget->getTargetTriple().isMacOSX() ||
2397 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002398 // PC-relative references to external symbols should go through $stub,
2399 // unless we're building with the leopard linker or later, which
2400 // automatically synthesizes these stubs.
2401 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002402 } else if (Subtarget->isPICStyleRIPRel() &&
2403 isa<Function>(GV) &&
2404 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2405 // If the function is marked as non-lazy, generate an indirect call
2406 // which loads from the GOT directly. This avoids runtime overhead
2407 // at the cost of eager binding (and one extra byte of encoding).
2408 OpFlags = X86II::MO_GOTPCREL;
2409 WrapperKind = X86ISD::WrapperRIP;
2410 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002411 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002412
Devang Patel0d881da2010-07-06 22:08:15 +00002413 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002414 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002415
2416 // Add a wrapper if needed.
2417 if (WrapperKind != ISD::DELETED_NODE)
2418 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2419 // Add extra indirection if needed.
2420 if (ExtraLoad)
2421 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2422 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002423 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002424 }
Bill Wendling056292f2008-09-16 21:48:12 +00002425 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002426 unsigned char OpFlags = 0;
2427
Evan Cheng1bf891a2010-12-01 22:59:46 +00002428 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2429 // external symbols should go through the PLT.
2430 if (Subtarget->isTargetELF() &&
2431 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2432 OpFlags = X86II::MO_PLT;
2433 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002434 (!Subtarget->getTargetTriple().isMacOSX() ||
2435 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002436 // PC-relative references to external symbols should go through $stub,
2437 // unless we're building with the leopard linker or later, which
2438 // automatically synthesizes these stubs.
2439 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002440 }
Eric Christopherfd179292009-08-27 18:07:15 +00002441
Chris Lattner48a7d022009-07-09 05:02:21 +00002442 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2443 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002444 }
2445
Chris Lattnerd96d0722007-02-25 06:40:16 +00002446 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002447 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002448 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002449
Evan Chengf22f9b32010-02-06 03:28:46 +00002450 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002451 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2452 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002453 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002454 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002455
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002456 Ops.push_back(Chain);
2457 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002458
Dan Gohman98ca4f22009-08-05 01:29:28 +00002459 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002460 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002461
Gordon Henriksen86737662008-01-05 16:56:59 +00002462 // Add argument registers to the end of the list so that they are known live
2463 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002464 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2465 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2466 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002467
Evan Cheng586ccac2008-03-18 23:36:35 +00002468 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002469 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002470 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2471
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002472 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002473 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002474 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002475
Gabor Greifba36cb52008-08-28 21:40:38 +00002476 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002477 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002478
Dan Gohman98ca4f22009-08-05 01:29:28 +00002479 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002480 // We used to do:
2481 //// If this is the first return lowered for this function, add the regs
2482 //// to the liveout set for the function.
2483 // This isn't right, although it's probably harmless on x86; liveouts
2484 // should be computed from returns not tail calls. Consider a void
2485 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002486 return DAG.getNode(X86ISD::TC_RETURN, dl,
2487 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002488 }
2489
Dale Johannesenace16102009-02-03 19:33:06 +00002490 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002491 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002492
Chris Lattner2d297092006-05-23 18:50:38 +00002493 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002494 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002495 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2496 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002497 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002498 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002499 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002500 // pops the hidden struct pointer, so we have to push it back.
2501 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002502 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002503 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002504 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002505
Gordon Henriksenae636f82008-01-03 16:47:34 +00002506 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002507 if (!IsSibcall) {
2508 Chain = DAG.getCALLSEQ_END(Chain,
2509 DAG.getIntPtrConstant(NumBytes, true),
2510 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2511 true),
2512 InFlag);
2513 InFlag = Chain.getValue(1);
2514 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002515
Chris Lattner3085e152007-02-25 08:59:22 +00002516 // Handle result values, copying them out of physregs into vregs that we
2517 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002518 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2519 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002520}
2521
Evan Cheng25ab6902006-09-08 06:48:29 +00002522
2523//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002524// Fast Calling Convention (tail call) implementation
2525//===----------------------------------------------------------------------===//
2526
2527// Like std call, callee cleans arguments, convention except that ECX is
2528// reserved for storing the tail called function address. Only 2 registers are
2529// free for argument passing (inreg). Tail call optimization is performed
2530// provided:
2531// * tailcallopt is enabled
2532// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002533// On X86_64 architecture with GOT-style position independent code only local
2534// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002535// To keep the stack aligned according to platform abi the function
2536// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2537// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002538// If a tail called function callee has more arguments than the caller the
2539// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002540// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002541// original REtADDR, but before the saved framepointer or the spilled registers
2542// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2543// stack layout:
2544// arg1
2545// arg2
2546// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002547// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002548// move area ]
2549// (possible EBP)
2550// ESI
2551// EDI
2552// local1 ..
2553
2554/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2555/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002556unsigned
2557X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2558 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002559 MachineFunction &MF = DAG.getMachineFunction();
2560 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002561 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002562 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002563 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002564 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002565 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002566 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2567 // Number smaller than 12 so just add the difference.
2568 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2569 } else {
2570 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002571 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002572 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002573 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002574 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002575}
2576
Evan Cheng5f941932010-02-05 02:21:12 +00002577/// MatchingStackOffset - Return true if the given stack call argument is
2578/// already available in the same position (relatively) of the caller's
2579/// incoming argument stack.
2580static
2581bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2582 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2583 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002584 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2585 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002586 if (Arg.getOpcode() == ISD::CopyFromReg) {
2587 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002588 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002589 return false;
2590 MachineInstr *Def = MRI->getVRegDef(VR);
2591 if (!Def)
2592 return false;
2593 if (!Flags.isByVal()) {
2594 if (!TII->isLoadFromStackSlot(Def, FI))
2595 return false;
2596 } else {
2597 unsigned Opcode = Def->getOpcode();
2598 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2599 Def->getOperand(1).isFI()) {
2600 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002601 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002602 } else
2603 return false;
2604 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002605 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2606 if (Flags.isByVal())
2607 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002608 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002609 // define @foo(%struct.X* %A) {
2610 // tail call @bar(%struct.X* byval %A)
2611 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002612 return false;
2613 SDValue Ptr = Ld->getBasePtr();
2614 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2615 if (!FINode)
2616 return false;
2617 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002618 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002619 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002620 FI = FINode->getIndex();
2621 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002622 } else
2623 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002624
Evan Cheng4cae1332010-03-05 08:38:04 +00002625 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002626 if (!MFI->isFixedObjectIndex(FI))
2627 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002628 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002629}
2630
Dan Gohman98ca4f22009-08-05 01:29:28 +00002631/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2632/// for tail call optimization. Targets which want to do tail call
2633/// optimization should implement this function.
2634bool
2635X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002636 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002637 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002638 bool isCalleeStructRet,
2639 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002640 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002641 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002642 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002643 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002644 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002645 CalleeCC != CallingConv::C)
2646 return false;
2647
Evan Cheng7096ae42010-01-29 06:45:59 +00002648 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002649 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002650 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002651 CallingConv::ID CallerCC = CallerF->getCallingConv();
2652 bool CCMatch = CallerCC == CalleeCC;
2653
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002654 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002655 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002656 return true;
2657 return false;
2658 }
2659
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002660 // Look for obvious safe cases to perform tail call optimization that do not
2661 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002662
Evan Cheng2c12cb42010-03-26 16:26:03 +00002663 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2664 // emit a special epilogue.
2665 if (RegInfo->needsStackRealignment(MF))
2666 return false;
2667
Evan Chenga375d472010-03-15 18:54:48 +00002668 // Also avoid sibcall optimization if either caller or callee uses struct
2669 // return semantics.
2670 if (isCalleeStructRet || isCallerStructRet)
2671 return false;
2672
Chad Rosier2416da32011-06-24 21:15:36 +00002673 // An stdcall caller is expected to clean up its arguments; the callee
2674 // isn't going to do that.
2675 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2676 return false;
2677
Chad Rosier871f6642011-05-18 19:59:50 +00002678 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002679 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002680 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002681
2682 // Optimizing for varargs on Win64 is unlikely to be safe without
2683 // additional testing.
2684 if (Subtarget->isTargetWin64())
2685 return false;
2686
Chad Rosier871f6642011-05-18 19:59:50 +00002687 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002688 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2689 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002690
Chad Rosier871f6642011-05-18 19:59:50 +00002691 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2692 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2693 if (!ArgLocs[i].isRegLoc())
2694 return false;
2695 }
2696
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002697 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2698 // Therefore if it's not used by the call it is not safe to optimize this into
2699 // a sibcall.
2700 bool Unused = false;
2701 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2702 if (!Ins[i].Used) {
2703 Unused = true;
2704 break;
2705 }
2706 }
2707 if (Unused) {
2708 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002709 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2710 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002711 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002712 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002713 CCValAssign &VA = RVLocs[i];
2714 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2715 return false;
2716 }
2717 }
2718
Evan Cheng13617962010-04-30 01:12:32 +00002719 // If the calling conventions do not match, then we'd better make sure the
2720 // results are returned in the same way as what the caller expects.
2721 if (!CCMatch) {
2722 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002723 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2724 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002725 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2726
2727 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002728 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2729 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002730 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2731
2732 if (RVLocs1.size() != RVLocs2.size())
2733 return false;
2734 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2735 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2736 return false;
2737 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2738 return false;
2739 if (RVLocs1[i].isRegLoc()) {
2740 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2741 return false;
2742 } else {
2743 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2744 return false;
2745 }
2746 }
2747 }
2748
Evan Chenga6bff982010-01-30 01:22:00 +00002749 // If the callee takes no arguments then go on to check the results of the
2750 // call.
2751 if (!Outs.empty()) {
2752 // Check if stack adjustment is needed. For now, do not do this if any
2753 // argument is passed on the stack.
2754 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002755 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2756 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002757
2758 // Allocate shadow area for Win64
2759 if (Subtarget->isTargetWin64()) {
2760 CCInfo.AllocateStack(32, 8);
2761 }
2762
Duncan Sands45907662010-10-31 13:21:44 +00002763 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002764 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002765 MachineFunction &MF = DAG.getMachineFunction();
2766 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2767 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002768
2769 // Check if the arguments are already laid out in the right way as
2770 // the caller's fixed stack objects.
2771 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002772 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2773 const X86InstrInfo *TII =
2774 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002775 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2776 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002777 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002778 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002779 if (VA.getLocInfo() == CCValAssign::Indirect)
2780 return false;
2781 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002782 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2783 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002784 return false;
2785 }
2786 }
2787 }
Evan Cheng9c044672010-05-29 01:35:22 +00002788
2789 // If the tailcall address may be in a register, then make sure it's
2790 // possible to register allocate for it. In 32-bit, the call address can
2791 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002792 // callee-saved registers are restored. These happen to be the same
2793 // registers used to pass 'inreg' arguments so watch out for those.
2794 if (!Subtarget->is64Bit() &&
2795 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002796 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002797 unsigned NumInRegs = 0;
2798 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2799 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002800 if (!VA.isRegLoc())
2801 continue;
2802 unsigned Reg = VA.getLocReg();
2803 switch (Reg) {
2804 default: break;
2805 case X86::EAX: case X86::EDX: case X86::ECX:
2806 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002807 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002808 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002809 }
2810 }
2811 }
Evan Chenga6bff982010-01-30 01:22:00 +00002812 }
Evan Chengb1712452010-01-27 06:25:16 +00002813
Evan Cheng86809cc2010-02-03 03:28:02 +00002814 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002815}
2816
Dan Gohman3df24e62008-09-03 23:12:08 +00002817FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002818X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2819 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002820}
2821
2822
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002823//===----------------------------------------------------------------------===//
2824// Other Lowering Hooks
2825//===----------------------------------------------------------------------===//
2826
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002827static bool MayFoldLoad(SDValue Op) {
2828 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2829}
2830
2831static bool MayFoldIntoStore(SDValue Op) {
2832 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2833}
2834
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002835static bool isTargetShuffle(unsigned Opcode) {
2836 switch(Opcode) {
2837 default: return false;
2838 case X86ISD::PSHUFD:
2839 case X86ISD::PSHUFHW:
2840 case X86ISD::PSHUFLW:
2841 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002842 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002843 case X86ISD::SHUFPS:
2844 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002845 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002846 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002847 case X86ISD::MOVLPS:
2848 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002849 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002850 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002851 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002852 case X86ISD::MOVSS:
2853 case X86ISD::MOVSD:
Craig Topper06cb6802011-11-26 20:47:44 +00002854 case X86ISD::UNPCKLP:
2855 case X86ISD::PUNPCKL:
2856 case X86ISD::UNPCKHP:
2857 case X86ISD::PUNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002858 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002859 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002860 return true;
2861 }
2862 return false;
2863}
2864
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002865static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002866 SDValue V1, SelectionDAG &DAG) {
2867 switch(Opc) {
2868 default: llvm_unreachable("Unknown x86 shuffle node");
2869 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002870 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002871 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002872 return DAG.getNode(Opc, dl, VT, V1);
2873 }
2874
2875 return SDValue();
2876}
2877
2878static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002879 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002880 switch(Opc) {
2881 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002882 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002883 case X86ISD::PSHUFHW:
2884 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002885 case X86ISD::VPERMILP:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002886 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2887 }
2888
2889 return SDValue();
2890}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002891
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002892static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2893 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2894 switch(Opc) {
2895 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002896 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002897 case X86ISD::SHUFPD:
2898 case X86ISD::SHUFPS:
Craig Topperec24e612011-11-30 07:47:51 +00002899 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002900 return DAG.getNode(Opc, dl, VT, V1, V2,
2901 DAG.getConstant(TargetMask, MVT::i8));
2902 }
2903 return SDValue();
2904}
2905
2906static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2907 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2908 switch(Opc) {
2909 default: llvm_unreachable("Unknown x86 shuffle node");
2910 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002911 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002912 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002913 case X86ISD::MOVLPS:
2914 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002915 case X86ISD::MOVSS:
2916 case X86ISD::MOVSD:
Craig Topper06cb6802011-11-26 20:47:44 +00002917 case X86ISD::UNPCKLP:
2918 case X86ISD::PUNPCKL:
2919 case X86ISD::UNPCKHP:
2920 case X86ISD::PUNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002921 return DAG.getNode(Opc, dl, VT, V1, V2);
2922 }
2923 return SDValue();
2924}
2925
Dan Gohmand858e902010-04-17 15:26:15 +00002926SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002927 MachineFunction &MF = DAG.getMachineFunction();
2928 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2929 int ReturnAddrIndex = FuncInfo->getRAIndex();
2930
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002931 if (ReturnAddrIndex == 0) {
2932 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002933 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002934 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002935 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002936 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002937 }
2938
Evan Cheng25ab6902006-09-08 06:48:29 +00002939 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002940}
2941
2942
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002943bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2944 bool hasSymbolicDisplacement) {
2945 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002946 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002947 return false;
2948
2949 // If we don't have a symbolic displacement - we don't have any extra
2950 // restrictions.
2951 if (!hasSymbolicDisplacement)
2952 return true;
2953
2954 // FIXME: Some tweaks might be needed for medium code model.
2955 if (M != CodeModel::Small && M != CodeModel::Kernel)
2956 return false;
2957
2958 // For small code model we assume that latest object is 16MB before end of 31
2959 // bits boundary. We may also accept pretty large negative constants knowing
2960 // that all objects are in the positive half of address space.
2961 if (M == CodeModel::Small && Offset < 16*1024*1024)
2962 return true;
2963
2964 // For kernel code model we know that all object resist in the negative half
2965 // of 32bits address space. We may not accept negative offsets, since they may
2966 // be just off and we may accept pretty large positive ones.
2967 if (M == CodeModel::Kernel && Offset > 0)
2968 return true;
2969
2970 return false;
2971}
2972
Evan Chengef41ff62011-06-23 17:54:54 +00002973/// isCalleePop - Determines whether the callee is required to pop its
2974/// own arguments. Callee pop is necessary to support tail calls.
2975bool X86::isCalleePop(CallingConv::ID CallingConv,
2976 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2977 if (IsVarArg)
2978 return false;
2979
2980 switch (CallingConv) {
2981 default:
2982 return false;
2983 case CallingConv::X86_StdCall:
2984 return !is64Bit;
2985 case CallingConv::X86_FastCall:
2986 return !is64Bit;
2987 case CallingConv::X86_ThisCall:
2988 return !is64Bit;
2989 case CallingConv::Fast:
2990 return TailCallOpt;
2991 case CallingConv::GHC:
2992 return TailCallOpt;
2993 }
2994}
2995
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002996/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2997/// specific condition code, returning the condition code and the LHS/RHS of the
2998/// comparison to make.
2999static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3000 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003001 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003002 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3003 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3004 // X > -1 -> X == 0, jump !sign.
3005 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003006 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003007 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
3008 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003009 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00003010 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003011 // X < 1 -> X <= 0
3012 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003013 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003014 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003015 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003016
Evan Chengd9558e02006-01-06 00:43:03 +00003017 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003018 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003019 case ISD::SETEQ: return X86::COND_E;
3020 case ISD::SETGT: return X86::COND_G;
3021 case ISD::SETGE: return X86::COND_GE;
3022 case ISD::SETLT: return X86::COND_L;
3023 case ISD::SETLE: return X86::COND_LE;
3024 case ISD::SETNE: return X86::COND_NE;
3025 case ISD::SETULT: return X86::COND_B;
3026 case ISD::SETUGT: return X86::COND_A;
3027 case ISD::SETULE: return X86::COND_BE;
3028 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003029 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003030 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003031
Chris Lattner4c78e022008-12-23 23:42:27 +00003032 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003033
Chris Lattner4c78e022008-12-23 23:42:27 +00003034 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003035 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3036 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003037 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3038 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003039 }
3040
Chris Lattner4c78e022008-12-23 23:42:27 +00003041 switch (SetCCOpcode) {
3042 default: break;
3043 case ISD::SETOLT:
3044 case ISD::SETOLE:
3045 case ISD::SETUGT:
3046 case ISD::SETUGE:
3047 std::swap(LHS, RHS);
3048 break;
3049 }
3050
3051 // On a floating point condition, the flags are set as follows:
3052 // ZF PF CF op
3053 // 0 | 0 | 0 | X > Y
3054 // 0 | 0 | 1 | X < Y
3055 // 1 | 0 | 0 | X == Y
3056 // 1 | 1 | 1 | unordered
3057 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003058 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003059 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003060 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003061 case ISD::SETOLT: // flipped
3062 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003063 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003064 case ISD::SETOLE: // flipped
3065 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003066 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003067 case ISD::SETUGT: // flipped
3068 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003069 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003070 case ISD::SETUGE: // flipped
3071 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003072 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003073 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003074 case ISD::SETNE: return X86::COND_NE;
3075 case ISD::SETUO: return X86::COND_P;
3076 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003077 case ISD::SETOEQ:
3078 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003079 }
Evan Chengd9558e02006-01-06 00:43:03 +00003080}
3081
Evan Cheng4a460802006-01-11 00:33:36 +00003082/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3083/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003084/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003085static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003086 switch (X86CC) {
3087 default:
3088 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003089 case X86::COND_B:
3090 case X86::COND_BE:
3091 case X86::COND_E:
3092 case X86::COND_P:
3093 case X86::COND_A:
3094 case X86::COND_AE:
3095 case X86::COND_NE:
3096 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003097 return true;
3098 }
3099}
3100
Evan Chengeb2f9692009-10-27 19:56:55 +00003101/// isFPImmLegal - Returns true if the target can instruction select the
3102/// specified FP immediate natively. If false, the legalizer will
3103/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003104bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003105 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3106 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3107 return true;
3108 }
3109 return false;
3110}
3111
Nate Begeman9008ca62009-04-27 18:41:29 +00003112/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3113/// the specified range (L, H].
3114static bool isUndefOrInRange(int Val, int Low, int Hi) {
3115 return (Val < 0) || (Val >= Low && Val < Hi);
3116}
3117
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003118/// isUndefOrInRange - Return true if every element in Mask, begining
3119/// from position Pos and ending in Pos+Size, falls within the specified
3120/// range (L, L+Pos]. or is undef.
3121static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3122 int Pos, int Size, int Low, int Hi) {
3123 for (int i = Pos, e = Pos+Size; i != e; ++i)
3124 if (!isUndefOrInRange(Mask[i], Low, Hi))
3125 return false;
3126 return true;
3127}
3128
Nate Begeman9008ca62009-04-27 18:41:29 +00003129/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3130/// specified value.
3131static bool isUndefOrEqual(int Val, int CmpVal) {
3132 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003133 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003134 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003135}
3136
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003137/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3138/// from position Pos and ending in Pos+Size, falls within the specified
3139/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003140static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3141 int Pos, int Size, int Low) {
3142 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3143 if (!isUndefOrEqual(Mask[i], Low))
3144 return false;
3145 return true;
3146}
3147
Nate Begeman9008ca62009-04-27 18:41:29 +00003148/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3149/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3150/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003151static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003152 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003153 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003154 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003155 return (Mask[0] < 2 && Mask[1] < 2);
3156 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003157}
3158
Nate Begeman9008ca62009-04-27 18:41:29 +00003159bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003160 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003161 N->getMask(M);
3162 return ::isPSHUFDMask(M, N->getValueType(0));
3163}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003164
Nate Begeman9008ca62009-04-27 18:41:29 +00003165/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3166/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003167static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003168 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003169 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003170
Nate Begeman9008ca62009-04-27 18:41:29 +00003171 // Lower quadword copied in order or undef.
3172 for (int i = 0; i != 4; ++i)
3173 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003174 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003175
Evan Cheng506d3df2006-03-29 23:07:14 +00003176 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003177 for (int i = 4; i != 8; ++i)
3178 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003179 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003180
Evan Cheng506d3df2006-03-29 23:07:14 +00003181 return true;
3182}
3183
Nate Begeman9008ca62009-04-27 18:41:29 +00003184bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003185 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003186 N->getMask(M);
3187 return ::isPSHUFHWMask(M, N->getValueType(0));
3188}
Evan Cheng506d3df2006-03-29 23:07:14 +00003189
Nate Begeman9008ca62009-04-27 18:41:29 +00003190/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3191/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003192static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003193 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003194 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003195
Rafael Espindola15684b22009-04-24 12:40:33 +00003196 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 for (int i = 4; i != 8; ++i)
3198 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003199 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003200
Rafael Espindola15684b22009-04-24 12:40:33 +00003201 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003202 for (int i = 0; i != 4; ++i)
3203 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003204 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003205
Rafael Espindola15684b22009-04-24 12:40:33 +00003206 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003207}
3208
Nate Begeman9008ca62009-04-27 18:41:29 +00003209bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003210 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003211 N->getMask(M);
3212 return ::isPSHUFLWMask(M, N->getValueType(0));
3213}
3214
Nate Begemana09008b2009-10-19 02:17:23 +00003215/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3216/// is suitable for input to PALIGNR.
3217static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003218 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003219 int i, e = VT.getVectorNumElements();
Craig Topper1dc0fbc2011-12-05 07:27:14 +00003220 if (VT.getSizeInBits() != 128)
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003221 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003222
Nate Begemana09008b2009-10-19 02:17:23 +00003223 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003224 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003225 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003226
Nate Begemana09008b2009-10-19 02:17:23 +00003227 for (i = 0; i != e; ++i)
3228 if (Mask[i] >= 0)
3229 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003230
Nate Begemana09008b2009-10-19 02:17:23 +00003231 // All undef, not a palignr.
3232 if (i == e)
3233 return false;
3234
Eli Friedman63f8dde2011-07-25 21:36:45 +00003235 // Make sure we're shifting in the right direction.
3236 if (Mask[i] <= i)
3237 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003238
3239 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003240
Nate Begemana09008b2009-10-19 02:17:23 +00003241 // Check the rest of the elements to see if they are consecutive.
3242 for (++i; i != e; ++i) {
3243 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003244 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003245 return false;
3246 }
3247 return true;
3248}
3249
Craig Topper9d7025b2011-11-27 21:41:12 +00003250/// isVSHUFPYMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003251/// specifies a shuffle of elements that is suitable for input to 256-bit
3252/// VSHUFPSY.
Craig Topper9d7025b2011-11-27 21:41:12 +00003253static bool isVSHUFPYMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper71c4c122011-11-28 01:14:24 +00003254 bool HasAVX) {
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003255 int NumElems = VT.getVectorNumElements();
3256
Craig Topper71c4c122011-11-28 01:14:24 +00003257 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003258 return false;
3259
Craig Topper9d7025b2011-11-27 21:41:12 +00003260 if (NumElems != 4 && NumElems != 8)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003261 return false;
3262
3263 // VSHUFPSY divides the resulting vector into 4 chunks.
3264 // The sources are also splitted into 4 chunks, and each destination
3265 // chunk must come from a different source chunk.
3266 //
3267 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3268 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3269 //
3270 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3271 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3272 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003273 // VSHUFPDY divides the resulting vector into 4 chunks.
3274 // The sources are also splitted into 4 chunks, and each destination
3275 // chunk must come from a different source chunk.
3276 //
3277 // SRC1 => X3 X2 X1 X0
3278 // SRC2 => Y3 Y2 Y1 Y0
3279 //
3280 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3281 //
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003282 int QuarterSize = NumElems/4;
3283 int HalfSize = QuarterSize*2;
3284 for (int i = 0; i < QuarterSize; ++i)
3285 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3286 return false;
3287 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3288 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3289 return false;
3290
Craig Topper9d7025b2011-11-27 21:41:12 +00003291 // For VSHUFPSY, the mask of the second half must be the same as the first
Craig Topper70b883b2011-11-28 10:14:51 +00003292 // but with the appropriate offsets. This works in the same way as
3293 // VPERMILPS works with masks.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003294 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3295 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3296 return false;
Craig Topper9d7025b2011-11-27 21:41:12 +00003297 if (NumElems == 4)
3298 continue;
3299 // VSHUFPSY handling
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003300 int FstHalfIdx = i-HalfSize;
3301 if (Mask[FstHalfIdx] < 0)
3302 continue;
3303 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3304 return false;
3305 }
3306 for (int i = QuarterSize*3; i < NumElems; ++i) {
3307 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3308 return false;
3309 int FstHalfIdx = i-HalfSize;
Craig Topper9d7025b2011-11-27 21:41:12 +00003310 if (NumElems == 4)
3311 continue;
3312 // VSHUFPSY handling
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003313 if (Mask[FstHalfIdx] < 0)
3314 continue;
3315 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3316 return false;
Craig Topper71c4c122011-11-28 01:14:24 +00003317 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003318
Craig Topper71c4c122011-11-28 01:14:24 +00003319 return true;
3320}
3321
3322/// isCommutedVSHUFP() - Returns true if the shuffle mask is exactly
3323/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3324/// half elements to come from vector 1 (which would equal the dest.) and
3325/// the upper half to come from vector 2.
Craig Topperbeabc6c2011-12-05 06:56:46 +00003326static bool isCommutedVSHUFPYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3327 bool HasAVX) {
Craig Topper71c4c122011-11-28 01:14:24 +00003328 int NumElems = VT.getVectorNumElements();
Craig Topper71c4c122011-11-28 01:14:24 +00003329
3330 if (!HasAVX || VT.getSizeInBits() != 256)
3331 return false;
3332
3333 if (NumElems != 4 && NumElems != 8)
3334 return false;
3335
3336 // VSHUFPSY divides the resulting vector into 4 chunks.
3337 // The sources are also splitted into 4 chunks, and each destination
3338 // chunk must come from a different source chunk.
3339 //
3340 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3341 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3342 //
3343 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3344 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3345 //
3346 // VSHUFPDY divides the resulting vector into 4 chunks.
3347 // The sources are also splitted into 4 chunks, and each destination
3348 // chunk must come from a different source chunk.
3349 //
3350 // SRC1 => X3 X2 X1 X0
3351 // SRC2 => Y3 Y2 Y1 Y0
3352 //
3353 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3354 //
3355 int QuarterSize = NumElems/4;
3356 int HalfSize = QuarterSize*2;
3357 for (int i = 0; i < QuarterSize; ++i)
3358 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3359 return false;
3360 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3361 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3362 return false;
3363
3364 // For VSHUFPSY, the mask of the second half must be the same as the first
Craig Topper70b883b2011-11-28 10:14:51 +00003365 // but with the appropriate offsets. This works in the same way as
3366 // VPERMILPS works with masks.
Craig Topper71c4c122011-11-28 01:14:24 +00003367 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3368 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3369 return false;
3370 if (NumElems == 4)
3371 continue;
3372 // VSHUFPSY handling
3373 int FstHalfIdx = i-HalfSize;
3374 if (Mask[FstHalfIdx] < 0)
3375 continue;
3376 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3377 return false;
3378 }
3379 for (int i = QuarterSize*3; i < NumElems; ++i) {
3380 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3381 return false;
3382 if (NumElems == 4)
3383 continue;
3384 // VSHUFPSY handling
3385 int FstHalfIdx = i-HalfSize;
3386 if (Mask[FstHalfIdx] < 0)
3387 continue;
3388 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3389 return false;
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003390 }
3391
3392 return true;
3393}
3394
Craig Topper9d7025b2011-11-27 21:41:12 +00003395/// getShuffleVSHUFPYImmediate - Return the appropriate immediate to shuffle
3396/// the specified VECTOR_MASK mask with VSHUFPSY/VSHUFPDY instructions.
3397static unsigned getShuffleVSHUFPYImmediate(SDNode *N) {
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003398 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3399 EVT VT = SVOp->getValueType(0);
3400 int NumElems = VT.getVectorNumElements();
3401
Craig Topper9d7025b2011-11-27 21:41:12 +00003402 assert(VT.getSizeInBits() == 256 && "Only supports 256-bit types");
3403 assert((NumElems == 4 || NumElems == 8) && "Only supports v4 and v8 types");
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003404
3405 int HalfSize = NumElems/2;
Craig Topper9d7025b2011-11-27 21:41:12 +00003406 unsigned Mul = (NumElems == 8) ? 2 : 1;
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003407 unsigned Mask = 0;
Craig Topper71c4c122011-11-28 01:14:24 +00003408 for (int i = 0; i != NumElems; ++i) {
Craig Topper9d7025b2011-11-27 21:41:12 +00003409 int Elt = SVOp->getMaskElt(i);
3410 if (Elt < 0)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003411 continue;
Craig Topper9d7025b2011-11-27 21:41:12 +00003412 Elt %= HalfSize;
3413 unsigned Shamt = i;
3414 // For VSHUFPSY, the mask of the first half must be equal to the second one.
3415 if (NumElems == 8) Shamt %= HalfSize;
3416 Mask |= Elt << (Shamt*Mul);
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003417 }
3418
3419 return Mask;
3420}
3421
Elena Demikhovsky52a35a82011-11-23 10:23:16 +00003422/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3423/// the two vector operands have swapped position.
Craig Topperbeabc6c2011-12-05 06:56:46 +00003424static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3425 unsigned NumElems) {
Elena Demikhovsky52a35a82011-11-23 10:23:16 +00003426 for (unsigned i = 0; i != NumElems; ++i) {
3427 int idx = Mask[i];
3428 if (idx < 0)
3429 continue;
3430 else if (idx < (int)NumElems)
3431 Mask[i] = idx + NumElems;
3432 else
3433 Mask[i] = idx - NumElems;
3434 }
3435}
3436
Evan Cheng14aed5e2006-03-24 01:18:28 +00003437/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003438/// specifies a shuffle of elements that is suitable for input to 128-bit
3439/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003440static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003441 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003442
3443 if (VT.getSizeInBits() != 128)
3444 return false;
3445
Nate Begeman9008ca62009-04-27 18:41:29 +00003446 if (NumElems != 2 && NumElems != 4)
3447 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003448
Nate Begeman9008ca62009-04-27 18:41:29 +00003449 int Half = NumElems / 2;
3450 for (int i = 0; i < Half; ++i)
3451 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003452 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003453 for (int i = Half; i < NumElems; ++i)
3454 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003455 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003456
Evan Cheng14aed5e2006-03-24 01:18:28 +00003457 return true;
3458}
3459
Nate Begeman9008ca62009-04-27 18:41:29 +00003460bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3461 SmallVector<int, 8> M;
3462 N->getMask(M);
3463 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003464}
3465
Craig Topper71c4c122011-11-28 01:14:24 +00003466/// isCommutedSHUFPMask - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003467/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3468/// half elements to come from vector 1 (which would equal the dest.) and
3469/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003470static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003471 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003472
3473 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003474 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003475
Nate Begeman9008ca62009-04-27 18:41:29 +00003476 int Half = NumElems / 2;
3477 for (int i = 0; i < Half; ++i)
3478 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003479 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003480 for (int i = Half; i < NumElems; ++i)
3481 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003482 return false;
3483 return true;
3484}
3485
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003486/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3487/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003488bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003489 EVT VT = N->getValueType(0);
3490 unsigned NumElems = VT.getVectorNumElements();
3491
3492 if (VT.getSizeInBits() != 128)
3493 return false;
3494
3495 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003496 return false;
3497
Evan Cheng2064a2b2006-03-28 06:50:32 +00003498 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003499 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3500 isUndefOrEqual(N->getMaskElt(1), 7) &&
3501 isUndefOrEqual(N->getMaskElt(2), 2) &&
3502 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003503}
3504
Nate Begeman0b10b912009-11-07 23:17:15 +00003505/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3506/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3507/// <2, 3, 2, 3>
3508bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003509 EVT VT = N->getValueType(0);
3510 unsigned NumElems = VT.getVectorNumElements();
3511
3512 if (VT.getSizeInBits() != 128)
3513 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003514
Nate Begeman0b10b912009-11-07 23:17:15 +00003515 if (NumElems != 4)
3516 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003517
Nate Begeman0b10b912009-11-07 23:17:15 +00003518 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003519 isUndefOrEqual(N->getMaskElt(1), 3) &&
3520 isUndefOrEqual(N->getMaskElt(2), 2) &&
3521 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003522}
3523
Evan Cheng5ced1d82006-04-06 23:23:56 +00003524/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3525/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003526bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3527 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003528
Evan Cheng5ced1d82006-04-06 23:23:56 +00003529 if (NumElems != 2 && NumElems != 4)
3530 return false;
3531
Evan Chengc5cdff22006-04-07 21:53:05 +00003532 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003533 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003534 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003535
Evan Chengc5cdff22006-04-07 21:53:05 +00003536 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003537 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003538 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003539
3540 return true;
3541}
3542
Nate Begeman0b10b912009-11-07 23:17:15 +00003543/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3544/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3545bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003546 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003547
David Greenea20244d2011-03-02 17:23:43 +00003548 if ((NumElems != 2 && NumElems != 4)
3549 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003550 return false;
3551
Evan Chengc5cdff22006-04-07 21:53:05 +00003552 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003553 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003554 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003555
Nate Begeman9008ca62009-04-27 18:41:29 +00003556 for (unsigned i = 0; i < NumElems/2; ++i)
3557 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003558 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003559
3560 return true;
3561}
3562
Evan Cheng0038e592006-03-28 00:39:58 +00003563/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3564/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003565static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003566 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003567 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003568
3569 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3570 "Unsupported vector type for unpckh");
3571
Craig Topper6347e862011-11-21 06:57:39 +00003572 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003573 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003574 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003575
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003576 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3577 // independently on 128-bit lanes.
3578 unsigned NumLanes = VT.getSizeInBits()/128;
3579 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003580
3581 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003582 unsigned End = NumLaneElts;
3583 for (unsigned s = 0; s < NumLanes; ++s) {
3584 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003585 i != End;
3586 i += 2, ++j) {
3587 int BitI = Mask[i];
3588 int BitI1 = Mask[i+1];
3589 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003590 return false;
David Greenea20244d2011-03-02 17:23:43 +00003591 if (V2IsSplat) {
3592 if (!isUndefOrEqual(BitI1, NumElts))
3593 return false;
3594 } else {
3595 if (!isUndefOrEqual(BitI1, j + NumElts))
3596 return false;
3597 }
Evan Cheng39623da2006-04-20 08:58:49 +00003598 }
David Greenea20244d2011-03-02 17:23:43 +00003599 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003600 Start += NumLaneElts;
3601 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003602 }
David Greenea20244d2011-03-02 17:23:43 +00003603
Evan Cheng0038e592006-03-28 00:39:58 +00003604 return true;
3605}
3606
Craig Topper6347e862011-11-21 06:57:39 +00003607bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003608 SmallVector<int, 8> M;
3609 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003610 return ::isUNPCKLMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003611}
3612
Evan Cheng4fcb9222006-03-28 02:43:26 +00003613/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3614/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003615static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003616 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003617 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003618
3619 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3620 "Unsupported vector type for unpckh");
3621
Craig Topper6347e862011-11-21 06:57:39 +00003622 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003623 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003624 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003625
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003626 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3627 // independently on 128-bit lanes.
3628 unsigned NumLanes = VT.getSizeInBits()/128;
3629 unsigned NumLaneElts = NumElts/NumLanes;
3630
3631 unsigned Start = 0;
3632 unsigned End = NumLaneElts;
3633 for (unsigned l = 0; l != NumLanes; ++l) {
3634 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3635 i != End; i += 2, ++j) {
3636 int BitI = Mask[i];
3637 int BitI1 = Mask[i+1];
3638 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003639 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003640 if (V2IsSplat) {
3641 if (isUndefOrEqual(BitI1, NumElts))
3642 return false;
3643 } else {
3644 if (!isUndefOrEqual(BitI1, j+NumElts))
3645 return false;
3646 }
Evan Cheng39623da2006-04-20 08:58:49 +00003647 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003648 // Process the next 128 bits.
3649 Start += NumLaneElts;
3650 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003651 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003652 return true;
3653}
3654
Craig Topper6347e862011-11-21 06:57:39 +00003655bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003656 SmallVector<int, 8> M;
3657 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003658 return ::isUNPCKHMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003659}
3660
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003661/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3662/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3663/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003664static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003665 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003666 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003667 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003668
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003669 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3670 // FIXME: Need a better way to get rid of this, there's no latency difference
3671 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3672 // the former later. We should also remove the "_undef" special mask.
3673 if (NumElems == 4 && VT.getSizeInBits() == 256)
3674 return false;
3675
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003676 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3677 // independently on 128-bit lanes.
3678 unsigned NumLanes = VT.getSizeInBits() / 128;
3679 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003680
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003681 for (unsigned s = 0; s < NumLanes; ++s) {
3682 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3683 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003684 i += 2, ++j) {
3685 int BitI = Mask[i];
3686 int BitI1 = Mask[i+1];
3687
3688 if (!isUndefOrEqual(BitI, j))
3689 return false;
3690 if (!isUndefOrEqual(BitI1, j))
3691 return false;
3692 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003693 }
David Greenea20244d2011-03-02 17:23:43 +00003694
Rafael Espindola15684b22009-04-24 12:40:33 +00003695 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003696}
3697
Nate Begeman9008ca62009-04-27 18:41:29 +00003698bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3699 SmallVector<int, 8> M;
3700 N->getMask(M);
3701 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3702}
3703
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003704/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3705/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3706/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003707static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003708 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003709 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3710 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003711
Nate Begeman9008ca62009-04-27 18:41:29 +00003712 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3713 int BitI = Mask[i];
3714 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003715 if (!isUndefOrEqual(BitI, j))
3716 return false;
3717 if (!isUndefOrEqual(BitI1, j))
3718 return false;
3719 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003720 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003721}
3722
Nate Begeman9008ca62009-04-27 18:41:29 +00003723bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3724 SmallVector<int, 8> M;
3725 N->getMask(M);
3726 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3727}
3728
Evan Cheng017dcc62006-04-21 01:05:10 +00003729/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3730/// specifies a shuffle of elements that is suitable for input to MOVSS,
3731/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003732static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003733 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003734 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003735
3736 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003737
Nate Begeman9008ca62009-04-27 18:41:29 +00003738 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003739 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003740
Nate Begeman9008ca62009-04-27 18:41:29 +00003741 for (int i = 1; i < NumElts; ++i)
3742 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003743 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003744
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003745 return true;
3746}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003747
Nate Begeman9008ca62009-04-27 18:41:29 +00003748bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3749 SmallVector<int, 8> M;
3750 N->getMask(M);
3751 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003752}
3753
Craig Topper70b883b2011-11-28 10:14:51 +00003754/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003755/// as permutations between 128-bit chunks or halves. As an example: this
3756/// shuffle bellow:
3757/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3758/// The first half comes from the second half of V1 and the second half from the
3759/// the second half of V2.
Craig Topper70b883b2011-11-28 10:14:51 +00003760static bool isVPERM2X128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3761 bool HasAVX) {
3762 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003763 return false;
3764
3765 // The shuffle result is divided into half A and half B. In total the two
3766 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3767 // B must come from C, D, E or F.
3768 int HalfSize = VT.getVectorNumElements()/2;
3769 bool MatchA = false, MatchB = false;
3770
3771 // Check if A comes from one of C, D, E, F.
3772 for (int Half = 0; Half < 4; ++Half) {
3773 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3774 MatchA = true;
3775 break;
3776 }
3777 }
3778
3779 // Check if B comes from one of C, D, E, F.
3780 for (int Half = 0; Half < 4; ++Half) {
3781 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3782 MatchB = true;
3783 break;
3784 }
3785 }
3786
3787 return MatchA && MatchB;
3788}
3789
Craig Topper70b883b2011-11-28 10:14:51 +00003790/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3791/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
3792static unsigned getShuffleVPERM2X128Immediate(SDNode *N) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003793 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3794 EVT VT = SVOp->getValueType(0);
3795
3796 int HalfSize = VT.getVectorNumElements()/2;
3797
3798 int FstHalf = 0, SndHalf = 0;
3799 for (int i = 0; i < HalfSize; ++i) {
3800 if (SVOp->getMaskElt(i) > 0) {
3801 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3802 break;
3803 }
3804 }
3805 for (int i = HalfSize; i < HalfSize*2; ++i) {
3806 if (SVOp->getMaskElt(i) > 0) {
3807 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3808 break;
3809 }
3810 }
3811
3812 return (FstHalf | (SndHalf << 4));
3813}
3814
Craig Topper70b883b2011-11-28 10:14:51 +00003815/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003816/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3817/// Note that VPERMIL mask matching is different depending whether theunderlying
3818/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3819/// to the same elements of the low, but to the higher half of the source.
3820/// In VPERMILPD the two lanes could be shuffled independently of each other
3821/// with the same restriction that lanes can't be crossed.
Craig Topper70b883b2011-11-28 10:14:51 +00003822static bool isVPERMILPMask(const SmallVectorImpl<int> &Mask, EVT VT,
3823 bool HasAVX) {
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003824 int NumElts = VT.getVectorNumElements();
3825 int NumLanes = VT.getSizeInBits()/128;
3826
Craig Topper70b883b2011-11-28 10:14:51 +00003827 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003828 return false;
3829
Craig Topper70b883b2011-11-28 10:14:51 +00003830 // Only match 256-bit with 32/64-bit types
3831 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003832 return false;
3833
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003834 int LaneSize = NumElts/NumLanes;
Craig Topper70b883b2011-11-28 10:14:51 +00003835 for (int l = 0; l != NumLanes; ++l) {
3836 int LaneStart = l*LaneSize;
3837 for (int i = 0; i != LaneSize; ++i) {
3838 if (!isUndefOrInRange(Mask[i+LaneStart], LaneStart, LaneStart+LaneSize))
3839 return false;
3840 if (NumElts == 4 || l == 0)
3841 continue;
3842 // VPERMILPS handling
3843 if (Mask[i] < 0)
3844 continue;
3845 if (!isUndefOrEqual(Mask[i+LaneStart], Mask[i]+LaneSize))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003846 return false;
3847 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003848 }
3849
3850 return true;
3851}
3852
Craig Topper70b883b2011-11-28 10:14:51 +00003853/// getShuffleVPERMILPImmediate - Return the appropriate immediate to shuffle
3854/// the specified VECTOR_MASK mask with VPERMILPS/D* instructions.
3855static unsigned getShuffleVPERMILPImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003856 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3857 EVT VT = SVOp->getValueType(0);
3858
3859 int NumElts = VT.getVectorNumElements();
3860 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003861 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003862
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003863 // Although the mask is equal for both lanes do it twice to get the cases
3864 // where a mask will match because the same mask element is undef on the
3865 // first half but valid on the second. This would get pathological cases
3866 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Craig Topper70b883b2011-11-28 10:14:51 +00003867 unsigned Shift = (LaneSize == 4) ? 2 : 1;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003868 unsigned Mask = 0;
Craig Topper70b883b2011-11-28 10:14:51 +00003869 for (int i = 0; i != NumElts; ++i) {
3870 int MaskElt = SVOp->getMaskElt(i);
3871 if (MaskElt < 0)
3872 continue;
3873 MaskElt %= LaneSize;
3874 unsigned Shamt = i;
3875 // VPERMILPSY, the mask of the first half must be equal to the second one
3876 if (NumElts == 8) Shamt %= LaneSize;
3877 Mask |= MaskElt << (Shamt*Shift);
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003878 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003879
3880 return Mask;
3881}
3882
Evan Cheng017dcc62006-04-21 01:05:10 +00003883/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3884/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003885/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003886static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003887 bool V2IsSplat = false, bool V2IsUndef = false) {
3888 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003889 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003890 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003891
Nate Begeman9008ca62009-04-27 18:41:29 +00003892 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003893 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003894
Nate Begeman9008ca62009-04-27 18:41:29 +00003895 for (int i = 1; i < NumOps; ++i)
3896 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3897 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3898 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003899 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003900
Evan Cheng39623da2006-04-20 08:58:49 +00003901 return true;
3902}
3903
Nate Begeman9008ca62009-04-27 18:41:29 +00003904static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003905 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003906 SmallVector<int, 8> M;
3907 N->getMask(M);
3908 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003909}
3910
Evan Chengd9539472006-04-14 21:59:03 +00003911/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3912/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003913/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3914bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3915 const X86Subtarget *Subtarget) {
Craig Topperc0d82852011-11-22 00:44:41 +00003916 if (!Subtarget->hasSSE3orAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003917 return false;
3918
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003919 // The second vector must be undef
3920 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3921 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003922
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003923 EVT VT = N->getValueType(0);
3924 unsigned NumElems = VT.getVectorNumElements();
3925
3926 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3927 (VT.getSizeInBits() == 256 && NumElems != 8))
3928 return false;
3929
3930 // "i+1" is the value the indexed mask element must have
3931 for (unsigned i = 0; i < NumElems; i += 2)
3932 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3933 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003934 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003935
3936 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003937}
3938
3939/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3940/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003941/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3942bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3943 const X86Subtarget *Subtarget) {
Craig Topperc0d82852011-11-22 00:44:41 +00003944 if (!Subtarget->hasSSE3orAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003945 return false;
3946
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003947 // The second vector must be undef
3948 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3949 return false;
3950
3951 EVT VT = N->getValueType(0);
3952 unsigned NumElems = VT.getVectorNumElements();
3953
3954 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3955 (VT.getSizeInBits() == 256 && NumElems != 8))
3956 return false;
3957
3958 // "i" is the value the indexed mask element must have
3959 for (unsigned i = 0; i < NumElems; i += 2)
3960 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3961 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003962 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003963
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003964 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003965}
3966
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003967/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3968/// specifies a shuffle of elements that is suitable for input to 256-bit
3969/// version of MOVDDUP.
Craig Topperbeabc6c2011-12-05 06:56:46 +00003970static bool isMOVDDUPYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3971 bool HasAVX) {
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003972 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003973
Craig Topperbeabc6c2011-12-05 06:56:46 +00003974 if (!HasAVX || VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003975 return false;
3976
3977 for (int i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003978 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003979 return false;
3980 for (int i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003981 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003982 return false;
3983 return true;
3984}
3985
Evan Cheng0b457f02008-09-25 20:50:48 +00003986/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003987/// specifies a shuffle of elements that is suitable for input to 128-bit
3988/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003989bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003990 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003991
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003992 if (VT.getSizeInBits() != 128)
3993 return false;
3994
3995 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003996 for (int i = 0; i < e; ++i)
3997 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003998 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003999 for (int i = 0; i < e; ++i)
4000 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004001 return false;
4002 return true;
4003}
4004
David Greenec38a03e2011-02-03 15:50:00 +00004005/// isVEXTRACTF128Index - Return true if the specified
4006/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4007/// suitable for input to VEXTRACTF128.
4008bool X86::isVEXTRACTF128Index(SDNode *N) {
4009 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4010 return false;
4011
4012 // The index should be aligned on a 128-bit boundary.
4013 uint64_t Index =
4014 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4015
4016 unsigned VL = N->getValueType(0).getVectorNumElements();
4017 unsigned VBits = N->getValueType(0).getSizeInBits();
4018 unsigned ElSize = VBits / VL;
4019 bool Result = (Index * ElSize) % 128 == 0;
4020
4021 return Result;
4022}
4023
David Greeneccacdc12011-02-04 16:08:29 +00004024/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4025/// operand specifies a subvector insert that is suitable for input to
4026/// VINSERTF128.
4027bool X86::isVINSERTF128Index(SDNode *N) {
4028 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4029 return false;
4030
4031 // The index should be aligned on a 128-bit boundary.
4032 uint64_t Index =
4033 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4034
4035 unsigned VL = N->getValueType(0).getVectorNumElements();
4036 unsigned VBits = N->getValueType(0).getSizeInBits();
4037 unsigned ElSize = VBits / VL;
4038 bool Result = (Index * ElSize) % 128 == 0;
4039
4040 return Result;
4041}
4042
Evan Cheng63d33002006-03-22 08:01:21 +00004043/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004044/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004045unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004046 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4047 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4048
Evan Chengb9df0ca2006-03-22 02:53:00 +00004049 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4050 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004051 for (int i = 0; i < NumOperands; ++i) {
4052 int Val = SVOp->getMaskElt(NumOperands-i-1);
4053 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004054 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004055 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004056 if (i != NumOperands - 1)
4057 Mask <<= Shift;
4058 }
Evan Cheng63d33002006-03-22 08:01:21 +00004059 return Mask;
4060}
4061
Evan Cheng506d3df2006-03-29 23:07:14 +00004062/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004063/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004064unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004065 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004066 unsigned Mask = 0;
4067 // 8 nodes, but we only care about the last 4.
4068 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004069 int Val = SVOp->getMaskElt(i);
4070 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004071 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004072 if (i != 4)
4073 Mask <<= 2;
4074 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004075 return Mask;
4076}
4077
4078/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004079/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004080unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004081 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004082 unsigned Mask = 0;
4083 // 8 nodes, but we only care about the first 4.
4084 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004085 int Val = SVOp->getMaskElt(i);
4086 if (Val >= 0)
4087 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004088 if (i != 0)
4089 Mask <<= 2;
4090 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004091 return Mask;
4092}
4093
Nate Begemana09008b2009-10-19 02:17:23 +00004094/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4095/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4096unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4097 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4098 EVT VVT = N->getValueType(0);
4099 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4100 int Val = 0;
4101
4102 unsigned i, e;
4103 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4104 Val = SVOp->getMaskElt(i);
4105 if (Val >= 0)
4106 break;
4107 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004108 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004109 return (Val - i) * EltSize;
4110}
4111
David Greenec38a03e2011-02-03 15:50:00 +00004112/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4113/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4114/// instructions.
4115unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4116 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4117 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4118
4119 uint64_t Index =
4120 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4121
4122 EVT VecVT = N->getOperand(0).getValueType();
4123 EVT ElVT = VecVT.getVectorElementType();
4124
4125 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004126 return Index / NumElemsPerChunk;
4127}
4128
David Greeneccacdc12011-02-04 16:08:29 +00004129/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4130/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4131/// instructions.
4132unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4133 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4134 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4135
4136 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004137 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004138
4139 EVT VecVT = N->getValueType(0);
4140 EVT ElVT = VecVT.getVectorElementType();
4141
4142 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004143 return Index / NumElemsPerChunk;
4144}
4145
Evan Cheng37b73872009-07-30 08:33:02 +00004146/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4147/// constant +0.0.
4148bool X86::isZeroNode(SDValue Elt) {
4149 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004150 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004151 (isa<ConstantFPSDNode>(Elt) &&
4152 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4153}
4154
Nate Begeman9008ca62009-04-27 18:41:29 +00004155/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4156/// their permute mask.
4157static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4158 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004159 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004160 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004161 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004162
Nate Begeman5a5ca152009-04-29 05:20:52 +00004163 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004164 int idx = SVOp->getMaskElt(i);
4165 if (idx < 0)
4166 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004167 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004168 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004169 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004170 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004171 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004172 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4173 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004174}
4175
Evan Cheng533a0aa2006-04-19 20:35:22 +00004176/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4177/// match movhlps. The lower half elements should come from upper half of
4178/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004179/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004180static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004181 EVT VT = Op->getValueType(0);
4182 if (VT.getSizeInBits() != 128)
4183 return false;
4184 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004185 return false;
4186 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004187 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004188 return false;
4189 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004190 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004191 return false;
4192 return true;
4193}
4194
Evan Cheng5ced1d82006-04-06 23:23:56 +00004195/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004196/// is promoted to a vector. It also returns the LoadSDNode by reference if
4197/// required.
4198static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004199 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4200 return false;
4201 N = N->getOperand(0).getNode();
4202 if (!ISD::isNON_EXTLoad(N))
4203 return false;
4204 if (LD)
4205 *LD = cast<LoadSDNode>(N);
4206 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004207}
4208
Dan Gohman65fd6562011-11-03 21:49:52 +00004209// Test whether the given value is a vector value which will be legalized
4210// into a load.
4211static bool WillBeConstantPoolLoad(SDNode *N) {
4212 if (N->getOpcode() != ISD::BUILD_VECTOR)
4213 return false;
4214
4215 // Check for any non-constant elements.
4216 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4217 switch (N->getOperand(i).getNode()->getOpcode()) {
4218 case ISD::UNDEF:
4219 case ISD::ConstantFP:
4220 case ISD::Constant:
4221 break;
4222 default:
4223 return false;
4224 }
4225
4226 // Vectors of all-zeros and all-ones are materialized with special
4227 // instructions rather than being loaded.
4228 return !ISD::isBuildVectorAllZeros(N) &&
4229 !ISD::isBuildVectorAllOnes(N);
4230}
4231
Evan Cheng533a0aa2006-04-19 20:35:22 +00004232/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4233/// match movlp{s|d}. The lower half elements should come from lower half of
4234/// V1 (and in order), and the upper half elements should come from the upper
4235/// half of V2 (and in order). And since V1 will become the source of the
4236/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004237static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4238 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004239 EVT VT = Op->getValueType(0);
4240 if (VT.getSizeInBits() != 128)
4241 return false;
4242
Evan Cheng466685d2006-10-09 20:57:25 +00004243 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004244 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004245 // Is V2 is a vector load, don't do this transformation. We will try to use
4246 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004247 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004248 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004249
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004250 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004251
Evan Cheng533a0aa2006-04-19 20:35:22 +00004252 if (NumElems != 2 && NumElems != 4)
4253 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004254 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004255 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004256 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004257 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004258 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004259 return false;
4260 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004261}
4262
Evan Cheng39623da2006-04-20 08:58:49 +00004263/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4264/// all the same.
4265static bool isSplatVector(SDNode *N) {
4266 if (N->getOpcode() != ISD::BUILD_VECTOR)
4267 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004268
Dan Gohman475871a2008-07-27 21:46:04 +00004269 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004270 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4271 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004272 return false;
4273 return true;
4274}
4275
Evan Cheng213d2cf2007-05-17 18:45:50 +00004276/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004277/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004278/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004279static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004280 SDValue V1 = N->getOperand(0);
4281 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004282 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4283 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004284 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004285 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004286 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004287 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4288 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004289 if (Opc != ISD::BUILD_VECTOR ||
4290 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004291 return false;
4292 } else if (Idx >= 0) {
4293 unsigned Opc = V1.getOpcode();
4294 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4295 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004296 if (Opc != ISD::BUILD_VECTOR ||
4297 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004298 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004299 }
4300 }
4301 return true;
4302}
4303
4304/// getZeroVector - Returns a vector of specified type with all zero elements.
4305///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004306static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004307 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004308 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004309
Dale Johannesen0488fb62010-09-30 23:57:10 +00004310 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004311 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004312 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004313 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004314 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004315 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4316 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4317 } else { // SSE1
4318 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4319 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4320 }
4321 } else if (VT.getSizeInBits() == 256) { // AVX
4322 // 256-bit logic and arithmetic instructions in AVX are
4323 // all floating-point, no support for integer ops. Default
4324 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004325 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004326 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4327 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004328 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004329 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004330}
4331
Chris Lattner8a594482007-11-25 00:24:49 +00004332/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004333/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4334/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4335/// Then bitcast to their original type, ensuring they get CSE'd.
4336static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4337 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004338 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004339 assert((VT.is128BitVector() || VT.is256BitVector())
4340 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004341
Owen Anderson825b72b2009-08-11 20:47:22 +00004342 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004343 SDValue Vec;
4344 if (VT.getSizeInBits() == 256) {
4345 if (HasAVX2) { // AVX2
4346 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4347 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4348 } else { // AVX
4349 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4350 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4351 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4352 Vec = Insert128BitVector(InsV, Vec,
4353 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4354 }
4355 } else {
4356 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004357 }
4358
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004359 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004360}
4361
Evan Cheng39623da2006-04-20 08:58:49 +00004362/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4363/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004364static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004365 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004366 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004367
Evan Cheng39623da2006-04-20 08:58:49 +00004368 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004369 SmallVector<int, 8> MaskVec;
4370 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004371
Nate Begeman5a5ca152009-04-29 05:20:52 +00004372 for (unsigned i = 0; i != NumElems; ++i) {
4373 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004374 MaskVec[i] = NumElems;
4375 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004376 }
Evan Cheng39623da2006-04-20 08:58:49 +00004377 }
Evan Cheng39623da2006-04-20 08:58:49 +00004378 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004379 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4380 SVOp->getOperand(1), &MaskVec[0]);
4381 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004382}
4383
Evan Cheng017dcc62006-04-21 01:05:10 +00004384/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4385/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004386static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004387 SDValue V2) {
4388 unsigned NumElems = VT.getVectorNumElements();
4389 SmallVector<int, 8> Mask;
4390 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004391 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004392 Mask.push_back(i);
4393 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004394}
4395
Nate Begeman9008ca62009-04-27 18:41:29 +00004396/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004397static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004398 SDValue V2) {
4399 unsigned NumElems = VT.getVectorNumElements();
4400 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004401 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004402 Mask.push_back(i);
4403 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004404 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004405 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004406}
4407
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004408/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004409static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004410 SDValue V2) {
4411 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004412 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004413 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004414 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004415 Mask.push_back(i + Half);
4416 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004417 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004418 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004419}
4420
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004421// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004422// a generic shuffle instruction because the target has no such instructions.
4423// Generate shuffles which repeat i16 and i8 several times until they can be
4424// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004425static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004426 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004427 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004428 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004429
Nate Begeman9008ca62009-04-27 18:41:29 +00004430 while (NumElems > 4) {
4431 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004432 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004433 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004434 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004435 EltNo -= NumElems/2;
4436 }
4437 NumElems >>= 1;
4438 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004439 return V;
4440}
Eric Christopherfd179292009-08-27 18:07:15 +00004441
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004442/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4443static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4444 EVT VT = V.getValueType();
4445 DebugLoc dl = V.getDebugLoc();
4446 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4447 && "Vector size not supported");
4448
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004449 if (VT.getSizeInBits() == 128) {
4450 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004451 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004452 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4453 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004454 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004455 // To use VPERMILPS to splat scalars, the second half of indicies must
4456 // refer to the higher part, which is a duplication of the lower one,
4457 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004458 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4459 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004460
4461 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4462 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4463 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004464 }
4465
4466 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4467}
4468
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004469/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004470static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4471 EVT SrcVT = SV->getValueType(0);
4472 SDValue V1 = SV->getOperand(0);
4473 DebugLoc dl = SV->getDebugLoc();
4474
4475 int EltNo = SV->getSplatIndex();
4476 int NumElems = SrcVT.getVectorNumElements();
4477 unsigned Size = SrcVT.getSizeInBits();
4478
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004479 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4480 "Unknown how to promote splat for type");
4481
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004482 // Extract the 128-bit part containing the splat element and update
4483 // the splat element index when it refers to the higher register.
4484 if (Size == 256) {
4485 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4486 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4487 if (Idx > 0)
4488 EltNo -= NumElems/2;
4489 }
4490
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004491 // All i16 and i8 vector types can't be used directly by a generic shuffle
4492 // instruction because the target has no such instruction. Generate shuffles
4493 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004494 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004495 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004496 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004497 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004498
4499 // Recreate the 256-bit vector and place the same 128-bit vector
4500 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004501 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004502 if (Size == 256) {
4503 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4504 DAG.getConstant(0, MVT::i32), DAG, dl);
4505 V1 = Insert128BitVector(InsV, V1,
4506 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4507 }
4508
4509 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004510}
4511
Evan Chengba05f722006-04-21 23:03:30 +00004512/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004513/// vector of zero or undef vector. This produces a shuffle where the low
4514/// element of V2 is swizzled into the zero/undef vector, landing at element
4515/// 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 +00004516static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004517 bool isZero, bool HasXMMInt,
4518 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004519 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004520 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004521 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004522 unsigned NumElems = VT.getVectorNumElements();
4523 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004524 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004525 // If this is the insertion idx, put the low elt of V2 here.
4526 MaskVec.push_back(i == Idx ? NumElems : i);
4527 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004528}
4529
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004530/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4531/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004532static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4533 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004534 if (Depth == 6)
4535 return SDValue(); // Limit search depth.
4536
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004537 SDValue V = SDValue(N, 0);
4538 EVT VT = V.getValueType();
4539 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004540
4541 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4542 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4543 Index = SV->getMaskElt(Index);
4544
4545 if (Index < 0)
4546 return DAG.getUNDEF(VT.getVectorElementType());
4547
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004548 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004549 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004550 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004551 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004552
4553 // Recurse into target specific vector shuffles to find scalars.
4554 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004555 int NumElems = VT.getVectorNumElements();
4556 SmallVector<unsigned, 16> ShuffleMask;
4557 SDValue ImmN;
4558
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004559 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004560 case X86ISD::SHUFPS:
4561 case X86ISD::SHUFPD:
4562 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper36e36ac2011-11-29 07:49:05 +00004563 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4564 ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004565 break;
Craig Topper06cb6802011-11-26 20:47:44 +00004566 case X86ISD::PUNPCKH:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004567 DecodePUNPCKHMask(NumElems, ShuffleMask);
4568 break;
Craig Topper06cb6802011-11-26 20:47:44 +00004569 case X86ISD::UNPCKHP:
Craig Topperf7de5772011-11-22 01:57:35 +00004570 DecodeUNPCKHPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004571 break;
Craig Topper06cb6802011-11-26 20:47:44 +00004572 case X86ISD::PUNPCKL:
David Greenec4db4e52011-02-28 19:06:56 +00004573 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004574 break;
Craig Topper06cb6802011-11-26 20:47:44 +00004575 case X86ISD::UNPCKLP:
David Greenec4db4e52011-02-28 19:06:56 +00004576 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004577 break;
4578 case X86ISD::MOVHLPS:
4579 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4580 break;
4581 case X86ISD::MOVLHPS:
4582 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4583 break;
4584 case X86ISD::PSHUFD:
4585 ImmN = N->getOperand(N->getNumOperands()-1);
4586 DecodePSHUFMask(NumElems,
4587 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4588 ShuffleMask);
4589 break;
4590 case X86ISD::PSHUFHW:
4591 ImmN = N->getOperand(N->getNumOperands()-1);
4592 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4593 ShuffleMask);
4594 break;
4595 case X86ISD::PSHUFLW:
4596 ImmN = N->getOperand(N->getNumOperands()-1);
4597 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4598 ShuffleMask);
4599 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004600 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004601 case X86ISD::MOVSD: {
4602 // The index 0 always comes from the first element of the second source,
4603 // this is why MOVSS and MOVSD are used in the first place. The other
4604 // elements come from the other positions of the first source vector.
4605 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004606 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4607 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004608 }
Craig Topper316cd2a2011-11-30 06:25:25 +00004609 case X86ISD::VPERMILP:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004610 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper316cd2a2011-11-30 06:25:25 +00004611 DecodeVPERMILPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004612 ShuffleMask);
4613 break;
Craig Topperec24e612011-11-30 07:47:51 +00004614 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004615 ImmN = N->getOperand(N->getNumOperands()-1);
4616 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4617 ShuffleMask);
4618 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004619 case X86ISD::MOVDDUP:
4620 case X86ISD::MOVLHPD:
4621 case X86ISD::MOVLPD:
4622 case X86ISD::MOVLPS:
4623 case X86ISD::MOVSHDUP:
4624 case X86ISD::MOVSLDUP:
4625 case X86ISD::PALIGN:
4626 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004627 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004628 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004629 return SDValue();
4630 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004631
4632 Index = ShuffleMask[Index];
4633 if (Index < 0)
4634 return DAG.getUNDEF(VT.getVectorElementType());
4635
4636 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4637 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4638 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004639 }
4640
4641 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004642 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004643 V = V.getOperand(0);
4644 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004645 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004646
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004647 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004648 return SDValue();
4649 }
4650
4651 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4652 return (Index == 0) ? V.getOperand(0)
4653 : DAG.getUNDEF(VT.getVectorElementType());
4654
4655 if (V.getOpcode() == ISD::BUILD_VECTOR)
4656 return V.getOperand(Index);
4657
4658 return SDValue();
4659}
4660
4661/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4662/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004663/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004664static
4665unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4666 bool ZerosFromLeft, SelectionDAG &DAG) {
4667 int i = 0;
4668
4669 while (i < NumElems) {
4670 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004671 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004672 if (!(Elt.getNode() &&
4673 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4674 break;
4675 ++i;
4676 }
4677
4678 return i;
4679}
4680
4681/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4682/// MaskE correspond consecutively to elements from one of the vector operands,
4683/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4684static
4685bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4686 int OpIdx, int NumElems, unsigned &OpNum) {
4687 bool SeenV1 = false;
4688 bool SeenV2 = false;
4689
4690 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4691 int Idx = SVOp->getMaskElt(i);
4692 // Ignore undef indicies
4693 if (Idx < 0)
4694 continue;
4695
4696 if (Idx < NumElems)
4697 SeenV1 = true;
4698 else
4699 SeenV2 = true;
4700
4701 // Only accept consecutive elements from the same vector
4702 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4703 return false;
4704 }
4705
4706 OpNum = SeenV1 ? 0 : 1;
4707 return true;
4708}
4709
4710/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4711/// logical left shift of a vector.
4712static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4713 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4714 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4715 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4716 false /* check zeros from right */, DAG);
4717 unsigned OpSrc;
4718
4719 if (!NumZeros)
4720 return false;
4721
4722 // Considering the elements in the mask that are not consecutive zeros,
4723 // check if they consecutively come from only one of the source vectors.
4724 //
4725 // V1 = {X, A, B, C} 0
4726 // \ \ \ /
4727 // vector_shuffle V1, V2 <1, 2, 3, X>
4728 //
4729 if (!isShuffleMaskConsecutive(SVOp,
4730 0, // Mask Start Index
4731 NumElems-NumZeros-1, // Mask End Index
4732 NumZeros, // Where to start looking in the src vector
4733 NumElems, // Number of elements in vector
4734 OpSrc)) // Which source operand ?
4735 return false;
4736
4737 isLeft = false;
4738 ShAmt = NumZeros;
4739 ShVal = SVOp->getOperand(OpSrc);
4740 return true;
4741}
4742
4743/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4744/// logical left shift of a vector.
4745static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4746 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4747 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4748 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4749 true /* check zeros from left */, DAG);
4750 unsigned OpSrc;
4751
4752 if (!NumZeros)
4753 return false;
4754
4755 // Considering the elements in the mask that are not consecutive zeros,
4756 // check if they consecutively come from only one of the source vectors.
4757 //
4758 // 0 { A, B, X, X } = V2
4759 // / \ / /
4760 // vector_shuffle V1, V2 <X, X, 4, 5>
4761 //
4762 if (!isShuffleMaskConsecutive(SVOp,
4763 NumZeros, // Mask Start Index
4764 NumElems-1, // Mask End Index
4765 0, // Where to start looking in the src vector
4766 NumElems, // Number of elements in vector
4767 OpSrc)) // Which source operand ?
4768 return false;
4769
4770 isLeft = true;
4771 ShAmt = NumZeros;
4772 ShVal = SVOp->getOperand(OpSrc);
4773 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004774}
4775
4776/// isVectorShift - Returns true if the shuffle can be implemented as a
4777/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004778static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004779 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004780 // Although the logic below support any bitwidth size, there are no
4781 // shift instructions which handle more than 128-bit vectors.
4782 if (SVOp->getValueType(0).getSizeInBits() > 128)
4783 return false;
4784
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004785 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4786 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4787 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004788
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004789 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004790}
4791
Evan Chengc78d3b42006-04-24 18:01:45 +00004792/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4793///
Dan Gohman475871a2008-07-27 21:46:04 +00004794static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004795 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004796 SelectionDAG &DAG,
4797 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004798 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004799 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004800
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004801 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004802 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004803 bool First = true;
4804 for (unsigned i = 0; i < 16; ++i) {
4805 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4806 if (ThisIsNonZero && First) {
4807 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004808 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004809 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004810 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004811 First = false;
4812 }
4813
4814 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004815 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004816 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4817 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004818 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004819 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004820 }
4821 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004822 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4823 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4824 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004825 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004826 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004827 } else
4828 ThisElt = LastElt;
4829
Gabor Greifba36cb52008-08-28 21:40:38 +00004830 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004831 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004832 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004833 }
4834 }
4835
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004836 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004837}
4838
Bill Wendlinga348c562007-03-22 18:42:45 +00004839/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004840///
Dan Gohman475871a2008-07-27 21:46:04 +00004841static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004842 unsigned NumNonZero, unsigned NumZero,
4843 SelectionDAG &DAG,
4844 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004845 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004846 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004847
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004848 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004849 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004850 bool First = true;
4851 for (unsigned i = 0; i < 8; ++i) {
4852 bool isNonZero = (NonZeros & (1 << i)) != 0;
4853 if (isNonZero) {
4854 if (First) {
4855 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004856 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004857 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004858 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004859 First = false;
4860 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004861 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004862 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004863 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004864 }
4865 }
4866
4867 return V;
4868}
4869
Evan Chengf26ffe92008-05-29 08:22:04 +00004870/// getVShift - Return a vector logical shift node.
4871///
Owen Andersone50ed302009-08-10 22:56:29 +00004872static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004873 unsigned NumBits, SelectionDAG &DAG,
4874 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004875 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004876 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004877 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004878 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4879 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004880 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004881 DAG.getConstant(NumBits,
4882 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004883}
4884
Dan Gohman475871a2008-07-27 21:46:04 +00004885SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004886X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004887 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004888
Evan Chengc3630942009-12-09 21:00:30 +00004889 // Check if the scalar load can be widened into a vector load. And if
4890 // the address is "base + cst" see if the cst can be "absorbed" into
4891 // the shuffle mask.
4892 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4893 SDValue Ptr = LD->getBasePtr();
4894 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4895 return SDValue();
4896 EVT PVT = LD->getValueType(0);
4897 if (PVT != MVT::i32 && PVT != MVT::f32)
4898 return SDValue();
4899
4900 int FI = -1;
4901 int64_t Offset = 0;
4902 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4903 FI = FINode->getIndex();
4904 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004905 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004906 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4907 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4908 Offset = Ptr.getConstantOperandVal(1);
4909 Ptr = Ptr.getOperand(0);
4910 } else {
4911 return SDValue();
4912 }
4913
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004914 // FIXME: 256-bit vector instructions don't require a strict alignment,
4915 // improve this code to support it better.
4916 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004917 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004918 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004919 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004920 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004921 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004922 // Can't change the alignment. FIXME: It's possible to compute
4923 // the exact stack offset and reference FI + adjust offset instead.
4924 // If someone *really* cares about this. That's the way to implement it.
4925 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004926 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004927 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004928 }
4929 }
4930
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004931 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004932 // Ptr + (Offset & ~15).
4933 if (Offset < 0)
4934 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004935 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004936 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004937 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004938 if (StartOffset)
4939 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4940 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4941
4942 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004943 int NumElems = VT.getVectorNumElements();
4944
4945 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4946 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4947 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004948 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004949 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004950
4951 // Canonicalize it to a v4i32 or v8i32 shuffle.
4952 SmallVector<int, 8> Mask;
4953 for (int i = 0; i < NumElems; ++i)
4954 Mask.push_back(EltNo);
4955
4956 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4957 return DAG.getNode(ISD::BITCAST, dl, NVT,
4958 DAG.getVectorShuffle(CanonVT, dl, V1,
4959 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004960 }
4961
4962 return SDValue();
4963}
4964
Michael J. Spencerec38de22010-10-10 22:04:20 +00004965/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4966/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004967/// load which has the same value as a build_vector whose operands are 'elts'.
4968///
4969/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004970///
Nate Begeman1449f292010-03-24 22:19:06 +00004971/// FIXME: we'd also like to handle the case where the last elements are zero
4972/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4973/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004974static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004975 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004976 EVT EltVT = VT.getVectorElementType();
4977 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004978
Nate Begemanfdea31a2010-03-24 20:49:50 +00004979 LoadSDNode *LDBase = NULL;
4980 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004981
Nate Begeman1449f292010-03-24 22:19:06 +00004982 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004983 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004984 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004985 for (unsigned i = 0; i < NumElems; ++i) {
4986 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004987
Nate Begemanfdea31a2010-03-24 20:49:50 +00004988 if (!Elt.getNode() ||
4989 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4990 return SDValue();
4991 if (!LDBase) {
4992 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4993 return SDValue();
4994 LDBase = cast<LoadSDNode>(Elt.getNode());
4995 LastLoadedElt = i;
4996 continue;
4997 }
4998 if (Elt.getOpcode() == ISD::UNDEF)
4999 continue;
5000
5001 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5002 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5003 return SDValue();
5004 LastLoadedElt = i;
5005 }
Nate Begeman1449f292010-03-24 22:19:06 +00005006
5007 // If we have found an entire vector of loads and undefs, then return a large
5008 // load of the entire vector width starting at the base pointer. If we found
5009 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005010 if (LastLoadedElt == NumElems - 1) {
5011 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005012 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005013 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005014 LDBase->isVolatile(), LDBase->isNonTemporal(),
5015 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005016 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005017 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005018 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005019 LDBase->isInvariant(), LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005020 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5021 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005022 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5023 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005024 SDValue ResNode =
5025 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5026 LDBase->getPointerInfo(),
5027 LDBase->getAlignment(),
5028 false/*isVolatile*/, true/*ReadMem*/,
5029 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005030 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005031 }
5032 return SDValue();
5033}
5034
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005035/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
5036/// a vbroadcast node. We support two patterns:
5037/// 1. A splat BUILD_VECTOR which uses a single scalar load.
5038/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
5039/// a scalar load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005040/// The scalar load node is returned when a pattern is found,
5041/// or SDValue() otherwise.
5042static SDValue isVectorBroadcast(SDValue &Op, bool hasAVX2) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005043 EVT VT = Op.getValueType();
5044 SDValue V = Op;
5045
5046 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
5047 V = V.getOperand(0);
5048
5049 //A suspected load to be broadcasted.
5050 SDValue Ld;
5051
5052 switch (V.getOpcode()) {
5053 default:
5054 // Unknown pattern found.
5055 return SDValue();
5056
5057 case ISD::BUILD_VECTOR: {
5058 // The BUILD_VECTOR node must be a splat.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005059 if (!isSplatVector(V.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005060 return SDValue();
5061
5062 Ld = V.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005063
5064 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005065 // of its users are from the BUILD_VECTOR node.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005066 if (!Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005067 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005068 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005069 }
5070
5071 case ISD::VECTOR_SHUFFLE: {
5072 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5073
5074 // Shuffles must have a splat mask where the first element is
5075 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005076 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005077 return SDValue();
5078
5079 SDValue Sc = Op.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005080 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005081 return SDValue();
5082
5083 Ld = Sc.getOperand(0);
5084
5085 // The scalar_to_vector node and the suspected
5086 // load node must have exactly one user.
5087 if (!Sc.hasOneUse() || !Ld.hasOneUse())
5088 return SDValue();
5089 break;
5090 }
5091 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005092
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005093 // The scalar source must be a normal load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005094 if (!ISD::isNormalLoad(Ld.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005095 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005096
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005097 bool Is256 = VT.getSizeInBits() == 256;
5098 bool Is128 = VT.getSizeInBits() == 128;
5099 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5100
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005101 if (hasAVX2) {
5102 // VBroadcast to YMM
5103 if (Is256 && (ScalarSize == 8 || ScalarSize == 16 ||
5104 ScalarSize == 32 || ScalarSize == 64 ))
5105 return Ld;
5106
5107 // VBroadcast to XMM
5108 if (Is128 && (ScalarSize == 8 || ScalarSize == 32 ||
5109 ScalarSize == 16 || ScalarSize == 64 ))
5110 return Ld;
5111 }
5112
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005113 // VBroadcast to YMM
5114 if (Is256 && (ScalarSize == 32 || ScalarSize == 64))
5115 return Ld;
5116
5117 // VBroadcast to XMM
5118 if (Is128 && (ScalarSize == 32))
5119 return Ld;
5120
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005121
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005122 // Unsupported broadcast.
5123 return SDValue();
5124}
5125
Evan Chengc3630942009-12-09 21:00:30 +00005126SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005127X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005128 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005129
David Greenef125a292011-02-08 19:04:41 +00005130 EVT VT = Op.getValueType();
5131 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005132 unsigned NumElems = Op.getNumOperands();
5133
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005134 // Vectors containing all zeros can be matched by pxor and xorps later
5135 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5136 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5137 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005138 if (Op.getValueType() == MVT::v4i32 ||
5139 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005140 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005141
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005142 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005143 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005144
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005145 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005146 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5147 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005148 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper745a86b2011-11-19 22:34:59 +00005149 if (Op.getValueType() == MVT::v4i32 ||
5150 (Op.getValueType() == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005151 return Op;
5152
Craig Topper745a86b2011-11-19 22:34:59 +00005153 return getOnesVector(Op.getValueType(), Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005154 }
5155
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005156 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005157 if (Subtarget->hasAVX() && LD.getNode())
5158 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
5159
Owen Andersone50ed302009-08-10 22:56:29 +00005160 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005161
Evan Cheng0db9fe62006-04-25 20:13:52 +00005162 unsigned NumZero = 0;
5163 unsigned NumNonZero = 0;
5164 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005165 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005166 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005167 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005168 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005169 if (Elt.getOpcode() == ISD::UNDEF)
5170 continue;
5171 Values.insert(Elt);
5172 if (Elt.getOpcode() != ISD::Constant &&
5173 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005174 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005175 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005176 NumZero++;
5177 else {
5178 NonZeros |= (1 << i);
5179 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005180 }
5181 }
5182
Chris Lattner97a2a562010-08-26 05:24:29 +00005183 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5184 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005185 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005186
Chris Lattner67f453a2008-03-09 05:42:06 +00005187 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005188 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005189 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005190 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005191
Chris Lattner62098042008-03-09 01:05:04 +00005192 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5193 // the value are obviously zero, truncate the value to i32 and do the
5194 // insertion that way. Only do this if the value is non-constant or if the
5195 // value is a constant being inserted into element 0. It is cheaper to do
5196 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005197 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005198 (!IsAllConstants || Idx == 0)) {
5199 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005200 // Handle SSE only.
5201 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5202 EVT VecVT = MVT::v4i32;
5203 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005204
Chris Lattner62098042008-03-09 01:05:04 +00005205 // Truncate the value (which may itself be a constant) to i32, and
5206 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005207 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005208 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005209 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005210 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005211
Chris Lattner62098042008-03-09 01:05:04 +00005212 // Now we have our 32-bit value zero extended in the low element of
5213 // a vector. If Idx != 0, swizzle it into place.
5214 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005215 SmallVector<int, 4> Mask;
5216 Mask.push_back(Idx);
5217 for (unsigned i = 1; i != VecElts; ++i)
5218 Mask.push_back(i);
5219 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005220 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005221 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005222 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005223 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005224 }
5225 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005226
Chris Lattner19f79692008-03-08 22:59:52 +00005227 // If we have a constant or non-constant insertion into the low element of
5228 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5229 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005230 // depending on what the source datatype is.
5231 if (Idx == 0) {
5232 if (NumZero == 0) {
5233 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005234 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5235 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005236 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5237 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005238 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005239 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005240 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5241 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005242 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5243 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005244 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5245 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005246 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005247 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005248 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005249 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005250
5251 // Is it a vector logical left shift?
5252 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005253 X86::isZeroNode(Op.getOperand(0)) &&
5254 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005255 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005256 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005257 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005258 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005259 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005260 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005261
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005262 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005263 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005264
Chris Lattner19f79692008-03-08 22:59:52 +00005265 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5266 // is a non-constant being inserted into an element other than the low one,
5267 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5268 // movd/movss) to move this into the low element, then shuffle it into
5269 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005270 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005271 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005272
Evan Cheng0db9fe62006-04-25 20:13:52 +00005273 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005274 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005275 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005276 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005277 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005278 MaskVec.push_back(i == Idx ? 0 : 1);
5279 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005280 }
5281 }
5282
Chris Lattner67f453a2008-03-09 05:42:06 +00005283 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005284 if (Values.size() == 1) {
5285 if (EVTBits == 32) {
5286 // Instead of a shuffle like this:
5287 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5288 // Check if it's possible to issue this instead.
5289 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5290 unsigned Idx = CountTrailingZeros_32(NonZeros);
5291 SDValue Item = Op.getOperand(Idx);
5292 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5293 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5294 }
Dan Gohman475871a2008-07-27 21:46:04 +00005295 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005296 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005297
Dan Gohmana3941172007-07-24 22:55:08 +00005298 // A vector full of immediates; various special cases are already
5299 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005300 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005301 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005302
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005303 // For AVX-length vectors, build the individual 128-bit pieces and use
5304 // shuffles to put them in place.
5305 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5306 SmallVector<SDValue, 32> V;
5307 for (unsigned i = 0; i < NumElems; ++i)
5308 V.push_back(Op.getOperand(i));
5309
5310 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5311
5312 // Build both the lower and upper subvector.
5313 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5314 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5315 NumElems/2);
5316
5317 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005318 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5319 DAG.getConstant(0, MVT::i32), DAG, dl);
5320 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005321 DAG, dl);
5322 }
5323
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005324 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005325 if (EVTBits == 64) {
5326 if (NumNonZero == 1) {
5327 // One half is zero or undef.
5328 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005329 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005330 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005331 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005332 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005333 }
Dan Gohman475871a2008-07-27 21:46:04 +00005334 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005335 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005336
5337 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005338 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005339 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005340 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005341 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005342 }
5343
Bill Wendling826f36f2007-03-28 00:57:11 +00005344 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005345 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24: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
5350 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005351 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005352 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005353 if (NumElems == 4 && NumZero > 0) {
5354 for (unsigned i = 0; i < 4; ++i) {
5355 bool isZero = !(NonZeros & (1 << i));
5356 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005357 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005358 else
Dale Johannesenace16102009-02-03 19:33:06 +00005359 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005360 }
5361
5362 for (unsigned i = 0; i < 2; ++i) {
5363 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5364 default: break;
5365 case 0:
5366 V[i] = V[i*2]; // Must be a zero vector.
5367 break;
5368 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005369 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005370 break;
5371 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005372 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005373 break;
5374 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005375 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005376 break;
5377 }
5378 }
5379
Nate Begeman9008ca62009-04-27 18:41:29 +00005380 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005381 bool Reverse = (NonZeros & 0x3) == 2;
5382 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005383 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005384 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5385 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005386 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5387 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005388 }
5389
Nate Begemanfdea31a2010-03-24 20:49:50 +00005390 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5391 // Check for a build vector of consecutive loads.
5392 for (unsigned i = 0; i < NumElems; ++i)
5393 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005394
Nate Begemanfdea31a2010-03-24 20:49:50 +00005395 // Check for elements which are consecutive loads.
5396 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5397 if (LD.getNode())
5398 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005399
5400 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperc0d82852011-11-22 00:44:41 +00005401 if (getSubtarget()->hasSSE41orAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005402 SDValue Result;
5403 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5404 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5405 else
5406 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005407
Chris Lattner24faf612010-08-28 17:59:08 +00005408 for (unsigned i = 1; i < NumElems; ++i) {
5409 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5410 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005411 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005412 }
5413 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005414 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005415
Chris Lattner6e80e442010-08-28 17:15:43 +00005416 // Otherwise, expand into a number of unpckl*, start by extending each of
5417 // our (non-undef) elements to the full vector width with the element in the
5418 // bottom slot of the vector (which generates no code for SSE).
5419 for (unsigned i = 0; i < NumElems; ++i) {
5420 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5421 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5422 else
5423 V[i] = DAG.getUNDEF(VT);
5424 }
5425
5426 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005427 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5428 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5429 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005430 unsigned EltStride = NumElems >> 1;
5431 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005432 for (unsigned i = 0; i < EltStride; ++i) {
5433 // If V[i+EltStride] is undef and this is the first round of mixing,
5434 // then it is safe to just drop this shuffle: V[i] is already in the
5435 // right place, the one element (since it's the first round) being
5436 // inserted as undef can be dropped. This isn't safe for successive
5437 // rounds because they will permute elements within both vectors.
5438 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5439 EltStride == NumElems/2)
5440 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005441
Chris Lattner6e80e442010-08-28 17:15:43 +00005442 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005443 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005444 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005445 }
5446 return V[0];
5447 }
Dan Gohman475871a2008-07-27 21:46:04 +00005448 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005449}
5450
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005451// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5452// them in a MMX register. This is better than doing a stack convert.
5453static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005454 DebugLoc dl = Op.getDebugLoc();
5455 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005456
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005457 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5458 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5459 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005460 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005461 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5462 InVec = Op.getOperand(1);
5463 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5464 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005465 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005466 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5467 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5468 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005469 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005470 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5471 Mask[0] = 0; Mask[1] = 2;
5472 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5473 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005474 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005475}
5476
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005477// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5478// to create 256-bit vectors from two other 128-bit ones.
5479static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5480 DebugLoc dl = Op.getDebugLoc();
5481 EVT ResVT = Op.getValueType();
5482
5483 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5484
5485 SDValue V1 = Op.getOperand(0);
5486 SDValue V2 = Op.getOperand(1);
5487 unsigned NumElems = ResVT.getVectorNumElements();
5488
5489 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5490 DAG.getConstant(0, MVT::i32), DAG, dl);
5491 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5492 DAG, dl);
5493}
5494
5495SDValue
5496X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005497 EVT ResVT = Op.getValueType();
5498
5499 assert(Op.getNumOperands() == 2);
5500 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5501 "Unsupported CONCAT_VECTORS for value type");
5502
5503 // We support concatenate two MMX registers and place them in a MMX register.
5504 // This is better than doing a stack convert.
5505 if (ResVT.is128BitVector())
5506 return LowerMMXCONCAT_VECTORS(Op, DAG);
5507
5508 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5509 // from two other 128-bit ones.
5510 return LowerAVXCONCAT_VECTORS(Op, DAG);
5511}
5512
Nate Begemanb9a47b82009-02-23 08:49:38 +00005513// v8i16 shuffles - Prefer shuffles in the following order:
5514// 1. [all] pshuflw, pshufhw, optional move
5515// 2. [ssse3] 1 x pshufb
5516// 3. [ssse3] 2 x pshufb + 1 x por
5517// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005518SDValue
5519X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5520 SelectionDAG &DAG) const {
5521 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005522 SDValue V1 = SVOp->getOperand(0);
5523 SDValue V2 = SVOp->getOperand(1);
5524 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005525 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005526
Nate Begemanb9a47b82009-02-23 08:49:38 +00005527 // Determine if more than 1 of the words in each of the low and high quadwords
5528 // of the result come from the same quadword of one of the two inputs. Undef
5529 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005530 unsigned LoQuad[] = { 0, 0, 0, 0 };
5531 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005532 BitVector InputQuads(4);
5533 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005534 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005535 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005536 MaskVals.push_back(EltIdx);
5537 if (EltIdx < 0) {
5538 ++Quad[0];
5539 ++Quad[1];
5540 ++Quad[2];
5541 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005542 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005543 }
5544 ++Quad[EltIdx / 4];
5545 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005546 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005547
Nate Begemanb9a47b82009-02-23 08:49:38 +00005548 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005549 unsigned MaxQuad = 1;
5550 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005551 if (LoQuad[i] > MaxQuad) {
5552 BestLoQuad = i;
5553 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005554 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005555 }
5556
Nate Begemanb9a47b82009-02-23 08:49:38 +00005557 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005558 MaxQuad = 1;
5559 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005560 if (HiQuad[i] > MaxQuad) {
5561 BestHiQuad = i;
5562 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005563 }
5564 }
5565
Nate Begemanb9a47b82009-02-23 08:49:38 +00005566 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005567 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005568 // single pshufb instruction is necessary. If There are more than 2 input
5569 // quads, disable the next transformation since it does not help SSSE3.
5570 bool V1Used = InputQuads[0] || InputQuads[1];
5571 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperc0d82852011-11-22 00:44:41 +00005572 if (Subtarget->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005573 if (InputQuads.count() == 2 && V1Used && V2Used) {
5574 BestLoQuad = InputQuads.find_first();
5575 BestHiQuad = InputQuads.find_next(BestLoQuad);
5576 }
5577 if (InputQuads.count() > 2) {
5578 BestLoQuad = -1;
5579 BestHiQuad = -1;
5580 }
5581 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005582
Nate Begemanb9a47b82009-02-23 08:49:38 +00005583 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5584 // the shuffle mask. If a quad is scored as -1, that means that it contains
5585 // words from all 4 input quadwords.
5586 SDValue NewV;
5587 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005588 SmallVector<int, 8> MaskV;
5589 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5590 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005591 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005592 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5593 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5594 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005595
Nate Begemanb9a47b82009-02-23 08:49:38 +00005596 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5597 // source words for the shuffle, to aid later transformations.
5598 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005599 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005600 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005601 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005602 if (idx != (int)i)
5603 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005604 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005605 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005606 AllWordsInNewV = false;
5607 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005608 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005609
Nate Begemanb9a47b82009-02-23 08:49:38 +00005610 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5611 if (AllWordsInNewV) {
5612 for (int i = 0; i != 8; ++i) {
5613 int idx = MaskVals[i];
5614 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005615 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005616 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005617 if ((idx != i) && idx < 4)
5618 pshufhw = false;
5619 if ((idx != i) && idx > 3)
5620 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005621 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005622 V1 = NewV;
5623 V2Used = false;
5624 BestLoQuad = 0;
5625 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005626 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005627
Nate Begemanb9a47b82009-02-23 08:49:38 +00005628 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5629 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005630 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005631 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5632 unsigned TargetMask = 0;
5633 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005634 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005635 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5636 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5637 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005638 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005639 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005640 }
Eric Christopherfd179292009-08-27 18:07:15 +00005641
Nate Begemanb9a47b82009-02-23 08:49:38 +00005642 // If we have SSSE3, and all words of the result are from 1 input vector,
5643 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5644 // is present, fall back to case 4.
Craig Topperc0d82852011-11-22 00:44:41 +00005645 if (Subtarget->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005646 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005647
Nate Begemanb9a47b82009-02-23 08:49:38 +00005648 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005649 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005650 // mask, and elements that come from V1 in the V2 mask, so that the two
5651 // results can be OR'd together.
5652 bool TwoInputs = V1Used && V2Used;
5653 for (unsigned i = 0; i != 8; ++i) {
5654 int EltIdx = MaskVals[i] * 2;
5655 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005656 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5657 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005658 continue;
5659 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005660 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5661 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005662 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005663 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005664 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005665 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005666 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005667 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005668 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005669
Nate Begemanb9a47b82009-02-23 08:49:38 +00005670 // Calculate the shuffle mask for the second input, shuffle it, and
5671 // OR it with the first shuffled input.
5672 pshufbMask.clear();
5673 for (unsigned i = 0; i != 8; ++i) {
5674 int EltIdx = MaskVals[i] * 2;
5675 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005676 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5677 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005678 continue;
5679 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005680 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5681 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005682 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005683 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005684 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005685 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005686 MVT::v16i8, &pshufbMask[0], 16));
5687 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005688 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005689 }
5690
5691 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5692 // and update MaskVals with new element order.
5693 BitVector InOrder(8);
5694 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005695 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005696 for (int i = 0; i != 4; ++i) {
5697 int idx = MaskVals[i];
5698 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005699 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005700 InOrder.set(i);
5701 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005702 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005703 InOrder.set(i);
5704 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005705 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005706 }
5707 }
5708 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005709 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005710 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005711 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005712
Craig Topperc0d82852011-11-22 00:44:41 +00005713 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3orAVX())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005714 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5715 NewV.getOperand(0),
5716 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5717 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005718 }
Eric Christopherfd179292009-08-27 18:07:15 +00005719
Nate Begemanb9a47b82009-02-23 08:49:38 +00005720 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5721 // and update MaskVals with the new element order.
5722 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005723 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005724 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005725 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005726 for (unsigned i = 4; i != 8; ++i) {
5727 int idx = MaskVals[i];
5728 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005729 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005730 InOrder.set(i);
5731 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005732 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005733 InOrder.set(i);
5734 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005735 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005736 }
5737 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005738 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005739 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005740
Craig Topperc0d82852011-11-22 00:44:41 +00005741 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3orAVX())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005742 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5743 NewV.getOperand(0),
5744 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5745 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005746 }
Eric Christopherfd179292009-08-27 18:07:15 +00005747
Nate Begemanb9a47b82009-02-23 08:49:38 +00005748 // In case BestHi & BestLo were both -1, which means each quadword has a word
5749 // from each of the four input quadwords, calculate the InOrder bitvector now
5750 // before falling through to the insert/extract cleanup.
5751 if (BestLoQuad == -1 && BestHiQuad == -1) {
5752 NewV = V1;
5753 for (int i = 0; i != 8; ++i)
5754 if (MaskVals[i] < 0 || MaskVals[i] == i)
5755 InOrder.set(i);
5756 }
Eric Christopherfd179292009-08-27 18:07:15 +00005757
Nate Begemanb9a47b82009-02-23 08:49:38 +00005758 // The other elements are put in the right place using pextrw and pinsrw.
5759 for (unsigned i = 0; i != 8; ++i) {
5760 if (InOrder[i])
5761 continue;
5762 int EltIdx = MaskVals[i];
5763 if (EltIdx < 0)
5764 continue;
5765 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005766 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005767 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005768 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005769 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005770 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005771 DAG.getIntPtrConstant(i));
5772 }
5773 return NewV;
5774}
5775
5776// v16i8 shuffles - Prefer shuffles in the following order:
5777// 1. [ssse3] 1 x pshufb
5778// 2. [ssse3] 2 x pshufb + 1 x por
5779// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5780static
Nate Begeman9008ca62009-04-27 18:41:29 +00005781SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005782 SelectionDAG &DAG,
5783 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005784 SDValue V1 = SVOp->getOperand(0);
5785 SDValue V2 = SVOp->getOperand(1);
5786 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005787 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005788 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005789
Nate Begemanb9a47b82009-02-23 08:49:38 +00005790 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005791 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005792 // present, fall back to case 3.
5793 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5794 bool V1Only = true;
5795 bool V2Only = true;
5796 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005797 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005798 if (EltIdx < 0)
5799 continue;
5800 if (EltIdx < 16)
5801 V2Only = false;
5802 else
5803 V1Only = false;
5804 }
Eric Christopherfd179292009-08-27 18:07:15 +00005805
Nate Begemanb9a47b82009-02-23 08:49:38 +00005806 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperc0d82852011-11-22 00:44:41 +00005807 if (TLI.getSubtarget()->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005808 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005809
Nate Begemanb9a47b82009-02-23 08:49:38 +00005810 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005811 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005812 //
5813 // Otherwise, we have elements from both input vectors, and must zero out
5814 // elements that come from V2 in the first mask, and V1 in the second mask
5815 // so that we can OR them together.
5816 bool TwoInputs = !(V1Only || V2Only);
5817 for (unsigned i = 0; i != 16; ++i) {
5818 int EltIdx = MaskVals[i];
5819 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005820 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005821 continue;
5822 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005823 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005824 }
5825 // If all the elements are from V2, assign it to V1 and return after
5826 // building the first pshufb.
5827 if (V2Only)
5828 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005829 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005830 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005831 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005832 if (!TwoInputs)
5833 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005834
Nate Begemanb9a47b82009-02-23 08:49:38 +00005835 // Calculate the shuffle mask for the second input, shuffle it, and
5836 // OR it with the first shuffled input.
5837 pshufbMask.clear();
5838 for (unsigned i = 0; i != 16; ++i) {
5839 int EltIdx = MaskVals[i];
5840 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005841 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005842 continue;
5843 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005844 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005845 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005846 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005847 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005848 MVT::v16i8, &pshufbMask[0], 16));
5849 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005850 }
Eric Christopherfd179292009-08-27 18:07:15 +00005851
Nate Begemanb9a47b82009-02-23 08:49:38 +00005852 // No SSSE3 - Calculate in place words and then fix all out of place words
5853 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5854 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005855 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5856 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005857 SDValue NewV = V2Only ? V2 : V1;
5858 for (int i = 0; i != 8; ++i) {
5859 int Elt0 = MaskVals[i*2];
5860 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005861
Nate Begemanb9a47b82009-02-23 08:49:38 +00005862 // This word of the result is all undef, skip it.
5863 if (Elt0 < 0 && Elt1 < 0)
5864 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005865
Nate Begemanb9a47b82009-02-23 08:49:38 +00005866 // This word of the result is already in the correct place, skip it.
5867 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5868 continue;
5869 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5870 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005871
Nate Begemanb9a47b82009-02-23 08:49:38 +00005872 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5873 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5874 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005875
5876 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5877 // using a single extract together, load it and store it.
5878 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005879 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005880 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005881 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005882 DAG.getIntPtrConstant(i));
5883 continue;
5884 }
5885
Nate Begemanb9a47b82009-02-23 08:49:38 +00005886 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005887 // source byte is not also odd, shift the extracted word left 8 bits
5888 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005889 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005890 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005891 DAG.getIntPtrConstant(Elt1 / 2));
5892 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005893 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005894 DAG.getConstant(8,
5895 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005896 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005897 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5898 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005899 }
5900 // If Elt0 is defined, extract it from the appropriate source. If the
5901 // source byte is not also even, shift the extracted word right 8 bits. If
5902 // Elt1 was also defined, OR the extracted values together before
5903 // inserting them in the result.
5904 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005905 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005906 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5907 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005908 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005909 DAG.getConstant(8,
5910 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005911 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005912 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5913 DAG.getConstant(0x00FF, MVT::i16));
5914 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005915 : InsElt0;
5916 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005917 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005918 DAG.getIntPtrConstant(i));
5919 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005920 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005921}
5922
Evan Cheng7a831ce2007-12-15 03:00:47 +00005923/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005924/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005925/// done when every pair / quad of shuffle mask elements point to elements in
5926/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005927/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005928static
Nate Begeman9008ca62009-04-27 18:41:29 +00005929SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005930 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005931 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005932 SDValue V1 = SVOp->getOperand(0);
5933 SDValue V2 = SVOp->getOperand(1);
5934 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005935 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005936 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005937 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005938 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005939 case MVT::v4f32: NewVT = MVT::v2f64; break;
5940 case MVT::v4i32: NewVT = MVT::v2i64; break;
5941 case MVT::v8i16: NewVT = MVT::v4i32; break;
5942 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005943 }
5944
Nate Begeman9008ca62009-04-27 18:41:29 +00005945 int Scale = NumElems / NewWidth;
5946 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005947 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005948 int StartIdx = -1;
5949 for (int j = 0; j < Scale; ++j) {
5950 int EltIdx = SVOp->getMaskElt(i+j);
5951 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005952 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005953 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005954 StartIdx = EltIdx - (EltIdx % Scale);
5955 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005956 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005957 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005958 if (StartIdx == -1)
5959 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005960 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005961 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005962 }
5963
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005964 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5965 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005966 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005967}
5968
Evan Chengd880b972008-05-09 21:53:03 +00005969/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005970///
Owen Andersone50ed302009-08-10 22:56:29 +00005971static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005972 SDValue SrcOp, SelectionDAG &DAG,
5973 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005974 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005975 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005976 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005977 LD = dyn_cast<LoadSDNode>(SrcOp);
5978 if (!LD) {
5979 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5980 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005981 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005982 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005983 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005984 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005985 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005986 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005987 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005988 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005989 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5990 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5991 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005992 SrcOp.getOperand(0)
5993 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005994 }
5995 }
5996 }
5997
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005998 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005999 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006000 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006001 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006002}
6003
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006004/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
6005/// shuffle node referes to only one lane in the sources.
6006static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
6007 EVT VT = SVOp->getValueType(0);
6008 int NumElems = VT.getVectorNumElements();
6009 int HalfSize = NumElems/2;
6010 SmallVector<int, 16> M;
6011 SVOp->getMask(M);
6012 bool MatchA = false, MatchB = false;
6013
6014 for (int l = 0; l < NumElems*2; l += HalfSize) {
6015 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
6016 MatchA = true;
6017 break;
6018 }
6019 }
6020
6021 for (int l = 0; l < NumElems*2; l += HalfSize) {
6022 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
6023 MatchB = true;
6024 break;
6025 }
6026 }
6027
6028 return MatchA && MatchB;
6029}
6030
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006031/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6032/// which could not be matched by any known target speficic shuffle
6033static SDValue
6034LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006035 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
6036 // If each half of a vector shuffle node referes to only one lane in the
6037 // source vectors, extract each used 128-bit lane and shuffle them using
6038 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
6039 // the work to the legalizer.
6040 DebugLoc dl = SVOp->getDebugLoc();
6041 EVT VT = SVOp->getValueType(0);
6042 int NumElems = VT.getVectorNumElements();
6043 int HalfSize = NumElems/2;
6044
6045 // Extract the reference for each half
6046 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
6047 int FstVecOpNum = 0, SndVecOpNum = 0;
6048 for (int i = 0; i < HalfSize; ++i) {
6049 int Elt = SVOp->getMaskElt(i);
6050 if (SVOp->getMaskElt(i) < 0)
6051 continue;
6052 FstVecOpNum = Elt/NumElems;
6053 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6054 break;
6055 }
6056 for (int i = HalfSize; i < NumElems; ++i) {
6057 int Elt = SVOp->getMaskElt(i);
6058 if (SVOp->getMaskElt(i) < 0)
6059 continue;
6060 SndVecOpNum = Elt/NumElems;
6061 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6062 break;
6063 }
6064
6065 // Extract the subvectors
6066 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
6067 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
6068 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
6069 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
6070
6071 // Generate 128-bit shuffles
6072 SmallVector<int, 16> MaskV1, MaskV2;
6073 for (int i = 0; i < HalfSize; ++i) {
6074 int Elt = SVOp->getMaskElt(i);
6075 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6076 }
6077 for (int i = HalfSize; i < NumElems; ++i) {
6078 int Elt = SVOp->getMaskElt(i);
6079 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6080 }
6081
6082 EVT NVT = V1.getValueType();
6083 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
6084 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
6085
6086 // Concatenate the result back
6087 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
6088 DAG.getConstant(0, MVT::i32), DAG, dl);
6089 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
6090 DAG, dl);
6091 }
6092
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006093 return SDValue();
6094}
6095
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006096/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6097/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006098static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006099LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006100 SDValue V1 = SVOp->getOperand(0);
6101 SDValue V2 = SVOp->getOperand(1);
6102 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006103 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006104
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006105 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6106
Evan Chengace3c172008-07-22 21:13:36 +00006107 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006108 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006109 SmallVector<int, 8> Mask1(4U, -1);
6110 SmallVector<int, 8> PermMask;
6111 SVOp->getMask(PermMask);
6112
Evan Chengace3c172008-07-22 21:13:36 +00006113 unsigned NumHi = 0;
6114 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006115 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006116 int Idx = PermMask[i];
6117 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006118 Locs[i] = std::make_pair(-1, -1);
6119 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006120 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6121 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006122 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006123 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006124 NumLo++;
6125 } else {
6126 Locs[i] = std::make_pair(1, NumHi);
6127 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006128 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006129 NumHi++;
6130 }
6131 }
6132 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006133
Evan Chengace3c172008-07-22 21:13:36 +00006134 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006135 // If no more than two elements come from either vector. This can be
6136 // implemented with two shuffles. First shuffle gather the elements.
6137 // The second shuffle, which takes the first shuffle as both of its
6138 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006139 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006140
Nate Begeman9008ca62009-04-27 18:41:29 +00006141 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006142
Evan Chengace3c172008-07-22 21:13:36 +00006143 for (unsigned i = 0; i != 4; ++i) {
6144 if (Locs[i].first == -1)
6145 continue;
6146 else {
6147 unsigned Idx = (i < 2) ? 0 : 4;
6148 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006149 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006150 }
6151 }
6152
Nate Begeman9008ca62009-04-27 18:41:29 +00006153 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006154 } else if (NumLo == 3 || NumHi == 3) {
6155 // Otherwise, we must have three elements from one vector, call it X, and
6156 // one element from the other, call it Y. First, use a shufps to build an
6157 // intermediate vector with the one element from Y and the element from X
6158 // that will be in the same half in the final destination (the indexes don't
6159 // matter). Then, use a shufps to build the final vector, taking the half
6160 // containing the element from Y from the intermediate, and the other half
6161 // from X.
6162 if (NumHi == 3) {
6163 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006164 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006165 std::swap(V1, V2);
6166 }
6167
6168 // Find the element from V2.
6169 unsigned HiIndex;
6170 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006171 int Val = PermMask[HiIndex];
6172 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006173 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006174 if (Val >= 4)
6175 break;
6176 }
6177
Nate Begeman9008ca62009-04-27 18:41:29 +00006178 Mask1[0] = PermMask[HiIndex];
6179 Mask1[1] = -1;
6180 Mask1[2] = PermMask[HiIndex^1];
6181 Mask1[3] = -1;
6182 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006183
6184 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006185 Mask1[0] = PermMask[0];
6186 Mask1[1] = PermMask[1];
6187 Mask1[2] = HiIndex & 1 ? 6 : 4;
6188 Mask1[3] = HiIndex & 1 ? 4 : 6;
6189 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006190 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006191 Mask1[0] = HiIndex & 1 ? 2 : 0;
6192 Mask1[1] = HiIndex & 1 ? 0 : 2;
6193 Mask1[2] = PermMask[2];
6194 Mask1[3] = PermMask[3];
6195 if (Mask1[2] >= 0)
6196 Mask1[2] += 4;
6197 if (Mask1[3] >= 0)
6198 Mask1[3] += 4;
6199 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006200 }
Evan Chengace3c172008-07-22 21:13:36 +00006201 }
6202
6203 // Break it into (shuffle shuffle_hi, shuffle_lo).
6204 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006205 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006206 SmallVector<int,8> LoMask(4U, -1);
6207 SmallVector<int,8> HiMask(4U, -1);
6208
6209 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006210 unsigned MaskIdx = 0;
6211 unsigned LoIdx = 0;
6212 unsigned HiIdx = 2;
6213 for (unsigned i = 0; i != 4; ++i) {
6214 if (i == 2) {
6215 MaskPtr = &HiMask;
6216 MaskIdx = 1;
6217 LoIdx = 0;
6218 HiIdx = 2;
6219 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006220 int Idx = PermMask[i];
6221 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006222 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006223 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006224 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006225 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006226 LoIdx++;
6227 } else {
6228 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006229 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006230 HiIdx++;
6231 }
6232 }
6233
Nate Begeman9008ca62009-04-27 18:41:29 +00006234 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6235 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6236 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006237 for (unsigned i = 0; i != 4; ++i) {
6238 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006239 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006240 } else {
6241 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006242 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006243 }
6244 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006245 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006246}
6247
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006248static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006249 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006250 V = V.getOperand(0);
6251 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6252 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006253 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6254 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6255 // BUILD_VECTOR (load), undef
6256 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006257 if (MayFoldLoad(V))
6258 return true;
6259 return false;
6260}
6261
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006262// FIXME: the version above should always be used. Since there's
6263// a bug where several vector shuffles can't be folded because the
6264// DAG is not updated during lowering and a node claims to have two
6265// uses while it only has one, use this version, and let isel match
6266// another instruction if the load really happens to have more than
6267// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006268// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006269static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006270 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006271 V = V.getOperand(0);
6272 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6273 V = V.getOperand(0);
6274 if (ISD::isNormalLoad(V.getNode()))
6275 return true;
6276 return false;
6277}
6278
6279/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6280/// a vector extract, and if both can be later optimized into a single load.
6281/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6282/// here because otherwise a target specific shuffle node is going to be
6283/// emitted for this shuffle, and the optimization not done.
6284/// FIXME: This is probably not the best approach, but fix the problem
6285/// until the right path is decided.
6286static
6287bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6288 const TargetLowering &TLI) {
6289 EVT VT = V.getValueType();
6290 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6291
6292 // Be sure that the vector shuffle is present in a pattern like this:
6293 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6294 if (!V.hasOneUse())
6295 return false;
6296
6297 SDNode *N = *V.getNode()->use_begin();
6298 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6299 return false;
6300
6301 SDValue EltNo = N->getOperand(1);
6302 if (!isa<ConstantSDNode>(EltNo))
6303 return false;
6304
6305 // If the bit convert changed the number of elements, it is unsafe
6306 // to examine the mask.
6307 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006308 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006309 EVT SrcVT = V.getOperand(0).getValueType();
6310 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6311 return false;
6312 V = V.getOperand(0);
6313 HasShuffleIntoBitcast = true;
6314 }
6315
6316 // Select the input vector, guarding against out of range extract vector.
6317 unsigned NumElems = VT.getVectorNumElements();
6318 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6319 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6320 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6321
6322 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006323 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006324 V = V.getOperand(0);
6325
6326 if (ISD::isNormalLoad(V.getNode())) {
6327 // Is the original load suitable?
6328 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6329
6330 // FIXME: avoid the multi-use bug that is preventing lots of
6331 // of foldings to be detected, this is still wrong of course, but
6332 // give the temporary desired behavior, and if it happens that
6333 // the load has real more uses, during isel it will not fold, and
6334 // will generate poor code.
6335 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6336 return false;
6337
6338 if (!HasShuffleIntoBitcast)
6339 return true;
6340
6341 // If there's a bitcast before the shuffle, check if the load type and
6342 // alignment is valid.
6343 unsigned Align = LN0->getAlignment();
6344 unsigned NewAlign =
6345 TLI.getTargetData()->getABITypeAlignment(
6346 VT.getTypeForEVT(*DAG.getContext()));
6347
6348 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6349 return false;
6350 }
6351
6352 return true;
6353}
6354
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006355static
Evan Cheng835580f2010-10-07 20:50:20 +00006356SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6357 EVT VT = Op.getValueType();
6358
6359 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006360 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6361 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006362 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6363 V1, DAG));
6364}
6365
6366static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006367SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006368 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006369 SDValue V1 = Op.getOperand(0);
6370 SDValue V2 = Op.getOperand(1);
6371 EVT VT = Op.getValueType();
6372
6373 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6374
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006375 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006376 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6377
Evan Cheng0899f5c2011-08-31 02:05:24 +00006378 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6379 return DAG.getNode(ISD::BITCAST, dl, VT,
6380 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6381 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6382 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006383}
6384
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006385static
6386SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6387 SDValue V1 = Op.getOperand(0);
6388 SDValue V2 = Op.getOperand(1);
6389 EVT VT = Op.getValueType();
6390
6391 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6392 "unsupported shuffle type");
6393
6394 if (V2.getOpcode() == ISD::UNDEF)
6395 V2 = V1;
6396
6397 // v4i32 or v4f32
6398 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6399}
6400
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006401static inline unsigned getSHUFPOpcode(EVT VT) {
6402 switch(VT.getSimpleVT().SimpleTy) {
6403 case MVT::v8i32: // Use fp unit for int unpack.
6404 case MVT::v8f32:
6405 case MVT::v4i32: // Use fp unit for int unpack.
6406 case MVT::v4f32: return X86ISD::SHUFPS;
6407 case MVT::v4i64: // Use fp unit for int unpack.
6408 case MVT::v4f64:
6409 case MVT::v2i64: // Use fp unit for int unpack.
6410 case MVT::v2f64: return X86ISD::SHUFPD;
6411 default:
6412 llvm_unreachable("Unknown type for shufp*");
6413 }
6414 return 0;
6415}
6416
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006417static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006418SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006419 SDValue V1 = Op.getOperand(0);
6420 SDValue V2 = Op.getOperand(1);
6421 EVT VT = Op.getValueType();
6422 unsigned NumElems = VT.getVectorNumElements();
6423
6424 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6425 // operand of these instructions is only memory, so check if there's a
6426 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6427 // same masks.
6428 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006429
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006430 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006431 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006432 CanFoldLoad = true;
6433
6434 // When V1 is a load, it can be folded later into a store in isel, example:
6435 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6436 // turns into:
6437 // (MOVLPSmr addr:$src1, VR128:$src2)
6438 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006439 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006440 CanFoldLoad = true;
6441
Dan Gohman65fd6562011-11-03 21:49:52 +00006442 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006443 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006444 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006445 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6446
6447 if (NumElems == 4)
Dan Gohman65fd6562011-11-03 21:49:52 +00006448 // If we don't care about the second element, procede to use movss.
6449 if (SVOp->getMaskElt(1) != -1)
6450 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006451 }
6452
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006453 // movl and movlp will both match v2i64, but v2i64 is never matched by
6454 // movl earlier because we make it strict to avoid messing with the movlp load
6455 // folding logic (see the code above getMOVLP call). Match it here then,
6456 // this is horrible, but will stay like this until we move all shuffle
6457 // matching to x86 specific nodes. Note that for the 1st condition all
6458 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006459 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006460 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6461 // as to remove this logic from here, as much as possible
6462 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006463 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006464 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006465 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006466
6467 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6468
6469 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006470 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006471 X86::getShuffleSHUFImmediate(SVOp), DAG);
6472}
6473
Craig Topper6347e862011-11-21 06:57:39 +00006474static inline unsigned getUNPCKLOpcode(EVT VT, bool HasAVX2) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006475 switch(VT.getSimpleVT().SimpleTy) {
Craig Topper06cb6802011-11-26 20:47:44 +00006476 case MVT::v32i8:
6477 case MVT::v16i8:
6478 case MVT::v16i16:
6479 case MVT::v8i16:
6480 case MVT::v4i32:
6481 case MVT::v2i64: return X86ISD::PUNPCKL;
Craig Topper6347e862011-11-21 06:57:39 +00006482 case MVT::v8i32:
Craig Topper06cb6802011-11-26 20:47:44 +00006483 case MVT::v4i64:
6484 if (HasAVX2) return X86ISD::PUNPCKL;
Craig Topper6347e862011-11-21 06:57:39 +00006485 // else use fp unit for int unpack.
Craig Topper705f2432011-11-24 22:57:10 +00006486 case MVT::v8f32:
Craig Topper06cb6802011-11-26 20:47:44 +00006487 case MVT::v4f32:
Craig Topper705f2432011-11-24 22:57:10 +00006488 case MVT::v4f64:
Craig Topper06cb6802011-11-26 20:47:44 +00006489 case MVT::v2f64: return X86ISD::UNPCKLP;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006490 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006491 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006492 }
6493 return 0;
6494}
6495
Craig Topper6347e862011-11-21 06:57:39 +00006496static inline unsigned getUNPCKHOpcode(EVT VT, bool HasAVX2) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006497 switch(VT.getSimpleVT().SimpleTy) {
Craig Topper06cb6802011-11-26 20:47:44 +00006498 case MVT::v32i8:
6499 case MVT::v16i8:
6500 case MVT::v16i16:
6501 case MVT::v8i16:
6502 case MVT::v4i32:
6503 case MVT::v2i64: return X86ISD::PUNPCKH;
6504 case MVT::v4i64:
Craig Topper6347e862011-11-21 06:57:39 +00006505 case MVT::v8i32:
Craig Topper06cb6802011-11-26 20:47:44 +00006506 if (HasAVX2) return X86ISD::PUNPCKH;
Craig Topper6347e862011-11-21 06:57:39 +00006507 // else use fp unit for int unpack.
Craig Topper705f2432011-11-24 22:57:10 +00006508 case MVT::v8f32:
Craig Topper06cb6802011-11-26 20:47:44 +00006509 case MVT::v4f32:
Craig Topper705f2432011-11-24 22:57:10 +00006510 case MVT::v4f64:
Craig Topper06cb6802011-11-26 20:47:44 +00006511 case MVT::v2f64: return X86ISD::UNPCKHP;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006512 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006513 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006514 }
6515 return 0;
6516}
6517
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006518static
6519SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006520 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006521 const X86Subtarget *Subtarget) {
6522 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6523 EVT VT = Op.getValueType();
6524 DebugLoc dl = Op.getDebugLoc();
6525 SDValue V1 = Op.getOperand(0);
6526 SDValue V2 = Op.getOperand(1);
6527
6528 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006529 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006530
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006531 // Handle splat operations
6532 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006533 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006534 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006535 // Special case, this is the only place now where it's allowed to return
6536 // a vector_shuffle operation without using a target specific node, because
6537 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6538 // this be moved to DAGCombine instead?
6539 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006540 return Op;
6541
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006542 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00006543 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00006544 if (Subtarget->hasAVX() && LD.getNode())
6545 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006546
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006547 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006548 if ((Size == 128 && NumElem <= 4) ||
6549 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006550 return SDValue();
6551
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006552 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006553 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006554 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006555
6556 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6557 // do it!
6558 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6559 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6560 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006561 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006562 } else if ((VT == MVT::v4i32 ||
6563 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006564 // FIXME: Figure out a cleaner way to do this.
6565 // Try to make use of movq to zero out the top part.
6566 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6567 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6568 if (NewOp.getNode()) {
6569 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6570 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6571 DAG, Subtarget, dl);
6572 }
6573 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6574 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6575 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6576 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6577 DAG, Subtarget, dl);
6578 }
6579 }
6580 return SDValue();
6581}
6582
Dan Gohman475871a2008-07-27 21:46:04 +00006583SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006584X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006585 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006586 SDValue V1 = Op.getOperand(0);
6587 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006588 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006589 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006590 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006591 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006592 bool V1IsSplat = false;
6593 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006594 bool HasXMMInt = Subtarget->hasXMMInt();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006595 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006596 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006597 MachineFunction &MF = DAG.getMachineFunction();
6598 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006599
Craig Topper3426a3e2011-11-14 06:46:21 +00006600 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006601
Craig Topper38034c52011-11-26 22:55:48 +00006602 assert(V1.getOpcode() != ISD::UNDEF && "Op 1 of shuffle should not be undef");
6603
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006604 // Vector shuffle lowering takes 3 steps:
6605 //
6606 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6607 // narrowing and commutation of operands should be handled.
6608 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6609 // shuffle nodes.
6610 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6611 // so the shuffle can be broken into other shuffles and the legalizer can
6612 // try the lowering again.
6613 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006614 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006615 // be matched during isel, all of them must be converted to a target specific
6616 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006617
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006618 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6619 // narrowing and commutation of operands should be handled. The actual code
6620 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006621 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006622 if (NewOp.getNode())
6623 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006624
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006625 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6626 // unpckh_undef). Only use pshufd if speed is more important than size.
6627 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006628 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V1,
6629 DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006630 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006631 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6632 DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006633
Craig Topperc0d82852011-11-22 00:44:41 +00006634 if (X86::isMOVDDUPMask(SVOp) && Subtarget->hasSSE3orAVX() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006635 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006636 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006637
Dale Johannesen0488fb62010-09-30 23:57:10 +00006638 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006639 return getMOVHighToLow(Op, dl, DAG);
6640
6641 // Use to match splats
Craig Topperc0d82852011-11-22 00:44:41 +00006642 if (HasXMMInt && X86::isUNPCKHMask(SVOp, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006643 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper6347e862011-11-21 06:57:39 +00006644 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6645 DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006646
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006647 if (X86::isPSHUFDMask(SVOp)) {
6648 // The actual implementation will match the mask in the if above and then
6649 // during isel it can match several different instructions, not only pshufd
6650 // as its name says, sad but true, emulate the behavior for now...
6651 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6652 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6653
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006654 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6655
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006656 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006657 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6658
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006659 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6660 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006661 }
Eric Christopherfd179292009-08-27 18:07:15 +00006662
Evan Chengf26ffe92008-05-29 08:22:04 +00006663 // Check if this can be converted into a logical shift.
6664 bool isLeft = false;
6665 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006666 SDValue ShVal;
Craig Topperc0d82852011-11-22 00:44:41 +00006667 bool isShift = HasXMMInt && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006668 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006669 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006670 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006671 EVT EltVT = VT.getVectorElementType();
6672 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006673 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006674 }
Eric Christopherfd179292009-08-27 18:07:15 +00006675
Nate Begeman9008ca62009-04-27 18:41:29 +00006676 if (X86::isMOVLMask(SVOp)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006677 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006678 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006679 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006680 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006681 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6682
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006683 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006684 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6685 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006686 }
Eric Christopherfd179292009-08-27 18:07:15 +00006687
Nate Begeman9008ca62009-04-27 18:41:29 +00006688 // FIXME: fold these into legal mask.
Craig Topperc0d82852011-11-22 00:44:41 +00006689 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp, HasAVX2))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006690 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006691
Dale Johannesen0488fb62010-09-30 23:57:10 +00006692 if (X86::isMOVHLPSMask(SVOp))
6693 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006694
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006695 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006696 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006697
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006698 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006699 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006700
Dale Johannesen0488fb62010-09-30 23:57:10 +00006701 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006702 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006703
Nate Begeman9008ca62009-04-27 18:41:29 +00006704 if (ShouldXformToMOVHLPS(SVOp) ||
6705 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6706 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006707
Evan Chengf26ffe92008-05-29 08:22:04 +00006708 if (isShift) {
6709 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006710 EVT EltVT = VT.getVectorElementType();
6711 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006712 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006713 }
Eric Christopherfd179292009-08-27 18:07:15 +00006714
Evan Cheng9eca5e82006-10-25 21:49:50 +00006715 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006716 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6717 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006718 V1IsSplat = isSplatVector(V1.getNode());
6719 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006720
Chris Lattner8a594482007-11-25 00:24:49 +00006721 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper38034c52011-11-26 22:55:48 +00006722 if (V1IsSplat && !V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006723 Op = CommuteVectorShuffle(SVOp, DAG);
6724 SVOp = cast<ShuffleVectorSDNode>(Op);
6725 V1 = SVOp->getOperand(0);
6726 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006727 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006728 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006729 }
6730
Craig Topperbeabc6c2011-12-05 06:56:46 +00006731 SmallVector<int, 32> M;
6732 SVOp->getMask(M);
6733
6734 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006735 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006736 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006737 return V1;
6738 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6739 // the instruction selector will not match, so get a canonical MOVL with
6740 // swapped operands to undo the commute.
6741 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006742 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006743
Craig Topperbeabc6c2011-12-05 06:56:46 +00006744 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper6347e862011-11-21 06:57:39 +00006745 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V2,
6746 DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006747
Craig Topperbeabc6c2011-12-05 06:56:46 +00006748 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper6347e862011-11-21 06:57:39 +00006749 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V2,
6750 DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006751
Evan Cheng9bbbb982006-10-25 20:48:19 +00006752 if (V2IsSplat) {
6753 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006754 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006755 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006756 SDValue NewMask = NormalizeMask(SVOp, DAG);
6757 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6758 if (NSVOp != SVOp) {
Craig Topperc0d82852011-11-22 00:44:41 +00006759 if (X86::isUNPCKLMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006760 return NewMask;
Craig Topperc0d82852011-11-22 00:44:41 +00006761 } else if (X86::isUNPCKHMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006762 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006763 }
6764 }
6765 }
6766
Evan Cheng9eca5e82006-10-25 21:49:50 +00006767 if (Commuted) {
6768 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006769 // FIXME: this seems wrong.
6770 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6771 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006772
Craig Topperc0d82852011-11-22 00:44:41 +00006773 if (X86::isUNPCKLMask(NewSVOp, HasAVX2))
Craig Topper6347e862011-11-21 06:57:39 +00006774 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V2, V1,
6775 DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006776
Craig Topperc0d82852011-11-22 00:44:41 +00006777 if (X86::isUNPCKHMask(NewSVOp, HasAVX2))
Craig Topper6347e862011-11-21 06:57:39 +00006778 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V2, V1,
6779 DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006780 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006781
Nate Begeman9008ca62009-04-27 18:41:29 +00006782 // Normalize the node to match x86 shuffle ops if needed
Craig Topperbeabc6c2011-12-05 06:56:46 +00006783 if (!V2IsUndef && (isCommutedSHUFPMask(M, VT) ||
6784 isCommutedVSHUFPYMask(M, VT, HasAVX)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006785 return CommuteVectorShuffle(SVOp, DAG);
6786
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006787 // The checks below are all present in isShuffleMaskLegal, but they are
6788 // inlined here right now to enable us to directly emit target specific
6789 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006790
Craig Topperc0d82852011-11-22 00:44:41 +00006791 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006792 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6793 X86::getShufflePALIGNRImmediate(SVOp),
6794 DAG);
6795
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006796 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6797 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006798 if (VT == MVT::v2f64 || VT == MVT::v2i64)
6799 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V1,
6800 DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006801 }
6802
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006803 if (isPSHUFHWMask(M, VT))
6804 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6805 X86::getShufflePSHUFHWImmediate(SVOp),
6806 DAG);
6807
6808 if (isPSHUFLWMask(M, VT))
6809 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6810 X86::getShufflePSHUFLWImmediate(SVOp),
6811 DAG);
6812
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006813 if (isSHUFPMask(M, VT))
6814 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6815 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006816
Craig Topperbeabc6c2011-12-05 06:56:46 +00006817 if (isUNPCKL_v_undef_Mask(M, VT))
Craig Topper6347e862011-11-21 06:57:39 +00006818 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V1,
6819 DAG);
Craig Topperbeabc6c2011-12-05 06:56:46 +00006820 if (isUNPCKH_v_undef_Mask(M, VT))
Craig Topper6347e862011-11-21 06:57:39 +00006821 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6822 DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006823
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006824 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006825 // Generate target specific nodes for 128 or 256-bit shuffles only
6826 // supported in the AVX instruction set.
6827 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006828
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006829 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006830 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006831 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6832
Craig Topper70b883b2011-11-28 10:14:51 +00006833 // Handle VPERMILPS/D* permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006834 if (isVPERMILPMask(M, VT, HasAVX))
Craig Topper316cd2a2011-11-30 06:25:25 +00006835 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006836 getShuffleVPERMILPImmediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006837
Craig Topper70b883b2011-11-28 10:14:51 +00006838 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006839 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006840 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006841 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006842
Craig Topper70b883b2011-11-28 10:14:51 +00006843 // Handle VSHUFPS/DY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006844 if (isVSHUFPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006845 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
Craig Topper9d7025b2011-11-27 21:41:12 +00006846 getShuffleVSHUFPYImmediate(SVOp), DAG);
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006847
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006848 //===--------------------------------------------------------------------===//
6849 // Since no target specific shuffle was selected for this generic one,
6850 // lower it into other known shuffles. FIXME: this isn't true yet, but
6851 // this is the plan.
6852 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006853
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006854 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6855 if (VT == MVT::v8i16) {
6856 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6857 if (NewOp.getNode())
6858 return NewOp;
6859 }
6860
6861 if (VT == MVT::v16i8) {
6862 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6863 if (NewOp.getNode())
6864 return NewOp;
6865 }
6866
6867 // Handle all 128-bit wide vectors with 4 elements, and match them with
6868 // several different shuffle types.
6869 if (NumElems == 4 && VT.getSizeInBits() == 128)
6870 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6871
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006872 // Handle general 256-bit shuffles
6873 if (VT.is256BitVector())
6874 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6875
Dan Gohman475871a2008-07-27 21:46:04 +00006876 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006877}
6878
Dan Gohman475871a2008-07-27 21:46:04 +00006879SDValue
6880X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006881 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006882 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006883 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006884
6885 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6886 return SDValue();
6887
Duncan Sands83ec4b62008-06-06 12:08:01 +00006888 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006889 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006890 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006891 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006892 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006893 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006894 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006895 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6896 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6897 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006898 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6899 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006900 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006901 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006902 Op.getOperand(0)),
6903 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006904 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006905 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006906 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006907 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006908 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006909 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006910 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6911 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006912 // result has a single use which is a store or a bitcast to i32. And in
6913 // the case of a store, it's not worth it if the index is a constant 0,
6914 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006915 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006916 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006917 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006918 if ((User->getOpcode() != ISD::STORE ||
6919 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6920 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006921 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006922 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006923 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006924 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006925 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006926 Op.getOperand(0)),
6927 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006928 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Pete Coopera77214a2011-11-14 19:38:42 +00006929 } else if (VT == MVT::i32 || VT == MVT::i64) {
6930 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006931 if (isa<ConstantSDNode>(Op.getOperand(1)))
6932 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006933 }
Dan Gohman475871a2008-07-27 21:46:04 +00006934 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006935}
6936
6937
Dan Gohman475871a2008-07-27 21:46:04 +00006938SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006939X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6940 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006941 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006942 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006943
David Greene74a579d2011-02-10 16:57:36 +00006944 SDValue Vec = Op.getOperand(0);
6945 EVT VecVT = Vec.getValueType();
6946
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006947 // If this is a 256-bit vector result, first extract the 128-bit vector and
6948 // then extract the element from the 128-bit vector.
6949 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006950 DebugLoc dl = Op.getNode()->getDebugLoc();
6951 unsigned NumElems = VecVT.getVectorNumElements();
6952 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006953 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6954
6955 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006956 bool Upper = IdxVal >= NumElems/2;
6957 Vec = Extract128BitVector(Vec,
6958 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006959
David Greene74a579d2011-02-10 16:57:36 +00006960 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006961 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006962 }
6963
6964 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6965
Craig Topperc0d82852011-11-22 00:44:41 +00006966 if (Subtarget->hasSSE41orAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006967 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006968 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006969 return Res;
6970 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006971
Owen Andersone50ed302009-08-10 22:56:29 +00006972 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006973 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006974 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006975 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006976 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006977 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006978 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006979 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6980 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006981 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006982 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006983 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006984 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006985 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006986 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006987 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006988 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006989 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006990 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006991 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006992 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006993 if (Idx == 0)
6994 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006995
Evan Cheng0db9fe62006-04-25 20:13:52 +00006996 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006997 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006998 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006999 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007000 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007001 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007002 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00007003 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007004 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7005 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7006 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007007 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007008 if (Idx == 0)
7009 return Op;
7010
7011 // UNPCKHPD the element to the lowest double word, then movsd.
7012 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7013 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007014 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007015 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007016 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007017 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007018 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007019 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007020 }
7021
Dan Gohman475871a2008-07-27 21:46:04 +00007022 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007023}
7024
Dan Gohman475871a2008-07-27 21:46:04 +00007025SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007026X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7027 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007028 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007029 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007030 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007031
Dan Gohman475871a2008-07-27 21:46:04 +00007032 SDValue N0 = Op.getOperand(0);
7033 SDValue N1 = Op.getOperand(1);
7034 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007035
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007036 if (VT.getSizeInBits() == 256)
7037 return SDValue();
7038
Dan Gohman8a55ce42009-09-23 21:02:20 +00007039 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007040 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007041 unsigned Opc;
7042 if (VT == MVT::v8i16)
7043 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007044 else if (VT == MVT::v16i8)
7045 Opc = X86ISD::PINSRB;
7046 else
7047 Opc = X86ISD::PINSRB;
7048
Nate Begeman14d12ca2008-02-11 04:19:36 +00007049 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7050 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007051 if (N1.getValueType() != MVT::i32)
7052 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7053 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007054 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007055 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007056 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007057 // Bits [7:6] of the constant are the source select. This will always be
7058 // zero here. The DAG Combiner may combine an extract_elt index into these
7059 // bits. For example (insert (extract, 3), 2) could be matched by putting
7060 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007061 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007062 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007063 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007064 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007065 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007066 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007067 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007068 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Pete Coopera77214a2011-11-14 19:38:42 +00007069 } else if ((EltVT == MVT::i32 || EltVT == MVT::i64) &&
7070 isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007071 // PINSR* works with constant index.
7072 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007073 }
Dan Gohman475871a2008-07-27 21:46:04 +00007074 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007075}
7076
Dan Gohman475871a2008-07-27 21:46:04 +00007077SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007078X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007079 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007080 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007081
David Greene6b381262011-02-09 15:32:06 +00007082 DebugLoc dl = Op.getDebugLoc();
7083 SDValue N0 = Op.getOperand(0);
7084 SDValue N1 = Op.getOperand(1);
7085 SDValue N2 = Op.getOperand(2);
7086
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007087 // If this is a 256-bit vector result, first extract the 128-bit vector,
7088 // insert the element into the extracted half and then place it back.
7089 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007090 if (!isa<ConstantSDNode>(N2))
7091 return SDValue();
7092
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007093 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007094 unsigned NumElems = VT.getVectorNumElements();
7095 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007096 bool Upper = IdxVal >= NumElems/2;
7097 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7098 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007099
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007100 // Insert the element into the desired half.
7101 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7102 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007103
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007104 // Insert the changed part back to the 256-bit vector
7105 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007106 }
7107
Craig Topperc0d82852011-11-22 00:44:41 +00007108 if (Subtarget->hasSSE41orAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007109 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7110
Dan Gohman8a55ce42009-09-23 21:02:20 +00007111 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007112 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007113
Dan Gohman8a55ce42009-09-23 21:02:20 +00007114 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007115 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7116 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007117 if (N1.getValueType() != MVT::i32)
7118 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7119 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007120 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007121 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007122 }
Dan Gohman475871a2008-07-27 21:46:04 +00007123 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007124}
7125
Dan Gohman475871a2008-07-27 21:46:04 +00007126SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007127X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007128 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007129 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007130 EVT OpVT = Op.getValueType();
7131
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007132 // If this is a 256-bit vector result, first insert into a 128-bit
7133 // vector and then insert into the 256-bit vector.
7134 if (OpVT.getSizeInBits() > 128) {
7135 // Insert into a 128-bit vector.
7136 EVT VT128 = EVT::getVectorVT(*Context,
7137 OpVT.getVectorElementType(),
7138 OpVT.getVectorNumElements() / 2);
7139
7140 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7141
7142 // Insert the 128-bit vector.
7143 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7144 DAG.getConstant(0, MVT::i32),
7145 DAG, dl);
7146 }
7147
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007148 if (Op.getValueType() == MVT::v1i64 &&
7149 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007150 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007151
Owen Anderson825b72b2009-08-11 20:47:22 +00007152 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007153 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7154 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007155 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007156 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007157}
7158
David Greene91585092011-01-26 15:38:49 +00007159// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7160// a simple subregister reference or explicit instructions to grab
7161// upper bits of a vector.
7162SDValue
7163X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7164 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007165 DebugLoc dl = Op.getNode()->getDebugLoc();
7166 SDValue Vec = Op.getNode()->getOperand(0);
7167 SDValue Idx = Op.getNode()->getOperand(1);
7168
7169 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7170 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7171 return Extract128BitVector(Vec, Idx, DAG, dl);
7172 }
David Greene91585092011-01-26 15:38:49 +00007173 }
7174 return SDValue();
7175}
7176
David Greenecfe33c42011-01-26 19:13:22 +00007177// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7178// simple superregister reference or explicit instructions to insert
7179// the upper bits of a vector.
7180SDValue
7181X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7182 if (Subtarget->hasAVX()) {
7183 DebugLoc dl = Op.getNode()->getDebugLoc();
7184 SDValue Vec = Op.getNode()->getOperand(0);
7185 SDValue SubVec = Op.getNode()->getOperand(1);
7186 SDValue Idx = Op.getNode()->getOperand(2);
7187
7188 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7189 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007190 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007191 }
7192 }
7193 return SDValue();
7194}
7195
Bill Wendling056292f2008-09-16 21:48:12 +00007196// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7197// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7198// one of the above mentioned nodes. It has to be wrapped because otherwise
7199// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7200// be used to form addressing mode. These wrapped nodes will be selected
7201// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007202SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007203X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007204 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007205
Chris Lattner41621a22009-06-26 19:22:52 +00007206 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7207 // global base reg.
7208 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007209 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007210 CodeModel::Model M = getTargetMachine().getCodeModel();
7211
Chris Lattner4f066492009-07-11 20:29:19 +00007212 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007213 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007214 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007215 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007216 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007217 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007218 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007219
Evan Cheng1606e8e2009-03-13 07:51:59 +00007220 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007221 CP->getAlignment(),
7222 CP->getOffset(), OpFlag);
7223 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007224 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007225 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007226 if (OpFlag) {
7227 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007228 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007229 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007230 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007231 }
7232
7233 return Result;
7234}
7235
Dan Gohmand858e902010-04-17 15:26:15 +00007236SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007237 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007238
Chris Lattner18c59872009-06-27 04:16:01 +00007239 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7240 // global base reg.
7241 unsigned char OpFlag = 0;
7242 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007243 CodeModel::Model M = getTargetMachine().getCodeModel();
7244
Chris Lattner4f066492009-07-11 20:29:19 +00007245 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007246 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007247 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007248 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007249 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007250 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007251 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007252
Chris Lattner18c59872009-06-27 04:16:01 +00007253 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7254 OpFlag);
7255 DebugLoc DL = JT->getDebugLoc();
7256 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007257
Chris Lattner18c59872009-06-27 04:16:01 +00007258 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007259 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007260 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7261 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007262 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007263 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007264
Chris Lattner18c59872009-06-27 04:16:01 +00007265 return Result;
7266}
7267
7268SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007269X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007270 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007271
Chris Lattner18c59872009-06-27 04:16:01 +00007272 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7273 // global base reg.
7274 unsigned char OpFlag = 0;
7275 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007276 CodeModel::Model M = getTargetMachine().getCodeModel();
7277
Chris Lattner4f066492009-07-11 20:29:19 +00007278 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007279 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7280 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7281 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007282 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007283 } else if (Subtarget->isPICStyleGOT()) {
7284 OpFlag = X86II::MO_GOT;
7285 } else if (Subtarget->isPICStyleStubPIC()) {
7286 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7287 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7288 OpFlag = X86II::MO_DARWIN_NONLAZY;
7289 }
Eric Christopherfd179292009-08-27 18:07:15 +00007290
Chris Lattner18c59872009-06-27 04:16:01 +00007291 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007292
Chris Lattner18c59872009-06-27 04:16:01 +00007293 DebugLoc DL = Op.getDebugLoc();
7294 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007295
7296
Chris Lattner18c59872009-06-27 04:16:01 +00007297 // With PIC, the address is actually $g + Offset.
7298 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007299 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007300 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7301 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007302 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007303 Result);
7304 }
Eric Christopherfd179292009-08-27 18:07:15 +00007305
Eli Friedman586272d2011-08-11 01:48:05 +00007306 // For symbols that require a load from a stub to get the address, emit the
7307 // load.
7308 if (isGlobalStubReference(OpFlag))
7309 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007310 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007311
Chris Lattner18c59872009-06-27 04:16:01 +00007312 return Result;
7313}
7314
Dan Gohman475871a2008-07-27 21:46:04 +00007315SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007316X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007317 // Create the TargetBlockAddressAddress node.
7318 unsigned char OpFlags =
7319 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007320 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007321 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007322 DebugLoc dl = Op.getDebugLoc();
7323 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7324 /*isTarget=*/true, OpFlags);
7325
Dan Gohmanf705adb2009-10-30 01:28:02 +00007326 if (Subtarget->isPICStyleRIPRel() &&
7327 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007328 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7329 else
7330 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007331
Dan Gohman29cbade2009-11-20 23:18:13 +00007332 // With PIC, the address is actually $g + Offset.
7333 if (isGlobalRelativeToPICBase(OpFlags)) {
7334 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7335 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7336 Result);
7337 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007338
7339 return Result;
7340}
7341
7342SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007343X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007344 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007345 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007346 // Create the TargetGlobalAddress node, folding in the constant
7347 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007348 unsigned char OpFlags =
7349 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007350 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007351 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007352 if (OpFlags == X86II::MO_NO_FLAG &&
7353 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007354 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007355 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007356 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007357 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007358 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007359 }
Eric Christopherfd179292009-08-27 18:07:15 +00007360
Chris Lattner4f066492009-07-11 20:29:19 +00007361 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007362 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007363 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7364 else
7365 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007366
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007367 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007368 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007369 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7370 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007371 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007372 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007373
Chris Lattner36c25012009-07-10 07:34:39 +00007374 // For globals that require a load from a stub to get the address, emit the
7375 // load.
7376 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007377 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007378 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007379
Dan Gohman6520e202008-10-18 02:06:02 +00007380 // If there was a non-zero offset that we didn't fold, create an explicit
7381 // addition for it.
7382 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007383 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007384 DAG.getConstant(Offset, getPointerTy()));
7385
Evan Cheng0db9fe62006-04-25 20:13:52 +00007386 return Result;
7387}
7388
Evan Chengda43bcf2008-09-24 00:05:32 +00007389SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007390X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007391 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007392 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007393 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007394}
7395
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007396static SDValue
7397GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007398 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007399 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007400 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007401 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007402 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007403 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007404 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007405 GA->getOffset(),
7406 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007407 if (InFlag) {
7408 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007409 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007410 } else {
7411 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007412 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007413 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007414
7415 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007416 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007417
Rafael Espindola15f1b662009-04-24 12:59:40 +00007418 SDValue Flag = Chain.getValue(1);
7419 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007420}
7421
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007422// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007423static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007424LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007425 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007426 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007427 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7428 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007429 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007430 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007431 InFlag = Chain.getValue(1);
7432
Chris Lattnerb903bed2009-06-26 21:20:29 +00007433 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007434}
7435
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007436// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007437static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007438LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007439 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007440 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7441 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007442}
7443
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007444// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7445// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007446static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007447 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007448 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007449 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007450
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007451 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7452 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7453 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007454
Michael J. Spencerec38de22010-10-10 22:04:20 +00007455 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007456 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007457 MachinePointerInfo(Ptr),
7458 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007459
Chris Lattnerb903bed2009-06-26 21:20:29 +00007460 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007461 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7462 // initialexec.
7463 unsigned WrapperKind = X86ISD::Wrapper;
7464 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007465 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007466 } else if (is64Bit) {
7467 assert(model == TLSModel::InitialExec);
7468 OperandFlags = X86II::MO_GOTTPOFF;
7469 WrapperKind = X86ISD::WrapperRIP;
7470 } else {
7471 assert(model == TLSModel::InitialExec);
7472 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007473 }
Eric Christopherfd179292009-08-27 18:07:15 +00007474
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007475 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7476 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007477 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007478 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007479 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007480 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007481
Rafael Espindola9a580232009-02-27 13:37:18 +00007482 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007483 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007484 MachinePointerInfo::getGOT(), false, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007485
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007486 // The address of the thread local variable is the add of the thread
7487 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007488 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007489}
7490
Dan Gohman475871a2008-07-27 21:46:04 +00007491SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007492X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007493
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007494 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007495 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007496
Eric Christopher30ef0e52010-06-03 04:07:48 +00007497 if (Subtarget->isTargetELF()) {
7498 // TODO: implement the "local dynamic" model
7499 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007500
Eric Christopher30ef0e52010-06-03 04:07:48 +00007501 // If GV is an alias then use the aliasee for determining
7502 // thread-localness.
7503 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7504 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007505
7506 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007507 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007508
Eric Christopher30ef0e52010-06-03 04:07:48 +00007509 switch (model) {
7510 case TLSModel::GeneralDynamic:
7511 case TLSModel::LocalDynamic: // not implemented
7512 if (Subtarget->is64Bit())
7513 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7514 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007515
Eric Christopher30ef0e52010-06-03 04:07:48 +00007516 case TLSModel::InitialExec:
7517 case TLSModel::LocalExec:
7518 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7519 Subtarget->is64Bit());
7520 }
7521 } else if (Subtarget->isTargetDarwin()) {
7522 // Darwin only has one model of TLS. Lower to that.
7523 unsigned char OpFlag = 0;
7524 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7525 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007526
Eric Christopher30ef0e52010-06-03 04:07:48 +00007527 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7528 // global base reg.
7529 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7530 !Subtarget->is64Bit();
7531 if (PIC32)
7532 OpFlag = X86II::MO_TLVP_PIC_BASE;
7533 else
7534 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007535 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007536 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007537 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007538 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007539 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007540
Eric Christopher30ef0e52010-06-03 04:07:48 +00007541 // With PIC32, the address is actually $g + Offset.
7542 if (PIC32)
7543 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7544 DAG.getNode(X86ISD::GlobalBaseReg,
7545 DebugLoc(), getPointerTy()),
7546 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007547
Eric Christopher30ef0e52010-06-03 04:07:48 +00007548 // Lowering the machine isd will make sure everything is in the right
7549 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007550 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007551 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007552 SDValue Args[] = { Chain, Offset };
7553 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007554
Eric Christopher30ef0e52010-06-03 04:07:48 +00007555 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7556 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7557 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007558
Eric Christopher30ef0e52010-06-03 04:07:48 +00007559 // And our return value (tls address) is in the standard call return value
7560 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007561 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007562 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7563 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007564 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007565
Eric Christopher30ef0e52010-06-03 04:07:48 +00007566 assert(false &&
7567 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007568
Torok Edwinc23197a2009-07-14 16:55:14 +00007569 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007570 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007571}
7572
Evan Cheng0db9fe62006-04-25 20:13:52 +00007573
Nadav Rotem43012222011-05-11 08:12:09 +00007574/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007575/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007576SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007577 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007578 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007579 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007580 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007581 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007582 SDValue ShOpLo = Op.getOperand(0);
7583 SDValue ShOpHi = Op.getOperand(1);
7584 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007585 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007586 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007587 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007588
Dan Gohman475871a2008-07-27 21:46:04 +00007589 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007590 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007591 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7592 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007593 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007594 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7595 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007596 }
Evan Chenge3413162006-01-09 18:33:28 +00007597
Owen Anderson825b72b2009-08-11 20:47:22 +00007598 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7599 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007600 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007601 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007602
Dan Gohman475871a2008-07-27 21:46:04 +00007603 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007604 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007605 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7606 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007607
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007608 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007609 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7610 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007611 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007612 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7613 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007614 }
7615
Dan Gohman475871a2008-07-27 21:46:04 +00007616 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007617 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007618}
Evan Chenga3195e82006-01-12 22:54:21 +00007619
Dan Gohmand858e902010-04-17 15:26:15 +00007620SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7621 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007622 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007623
Dale Johannesen0488fb62010-09-30 23:57:10 +00007624 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007625 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007626
Owen Anderson825b72b2009-08-11 20:47:22 +00007627 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007628 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007629
Eli Friedman36df4992009-05-27 00:47:34 +00007630 // These are really Legal; return the operand so the caller accepts it as
7631 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007632 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007633 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007634 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007635 Subtarget->is64Bit()) {
7636 return Op;
7637 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007638
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007639 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007640 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007641 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007642 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007643 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007644 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007645 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007646 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007647 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007648 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7649}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007650
Owen Andersone50ed302009-08-10 22:56:29 +00007651SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007652 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007653 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007654 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007655 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007656 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007657 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007658 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007659 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007660 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007661 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007662
Chris Lattner492a43e2010-09-22 01:28:21 +00007663 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007664
Stuart Hastings84be9582011-06-02 15:57:11 +00007665 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7666 MachineMemOperand *MMO;
7667 if (FI) {
7668 int SSFI = FI->getIndex();
7669 MMO =
7670 DAG.getMachineFunction()
7671 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7672 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7673 } else {
7674 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7675 StackSlot = StackSlot.getOperand(1);
7676 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007677 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007678 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7679 X86ISD::FILD, DL,
7680 Tys, Ops, array_lengthof(Ops),
7681 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007682
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007683 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007684 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007685 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007686
7687 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7688 // shouldn't be necessary except that RFP cannot be live across
7689 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007690 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007691 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7692 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007693 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007694 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007695 SDValue Ops[] = {
7696 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7697 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007698 MachineMemOperand *MMO =
7699 DAG.getMachineFunction()
7700 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007701 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007702
Chris Lattner492a43e2010-09-22 01:28:21 +00007703 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7704 Ops, array_lengthof(Ops),
7705 Op.getValueType(), MMO);
7706 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007707 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007708 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007709 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007710
Evan Cheng0db9fe62006-04-25 20:13:52 +00007711 return Result;
7712}
7713
Bill Wendling8b8a6362009-01-17 03:56:04 +00007714// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007715SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7716 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007717 // This algorithm is not obvious. Here it is in C code, more or less:
7718 /*
7719 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7720 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7721 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007722
Bill Wendling8b8a6362009-01-17 03:56:04 +00007723 // Copy ints to xmm registers.
7724 __m128i xh = _mm_cvtsi32_si128( hi );
7725 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007726
Bill Wendling8b8a6362009-01-17 03:56:04 +00007727 // Combine into low half of a single xmm register.
7728 __m128i x = _mm_unpacklo_epi32( xh, xl );
7729 __m128d d;
7730 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007731
Bill Wendling8b8a6362009-01-17 03:56:04 +00007732 // Merge in appropriate exponents to give the integer bits the right
7733 // magnitude.
7734 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007735
Bill Wendling8b8a6362009-01-17 03:56:04 +00007736 // Subtract away the biases to deal with the IEEE-754 double precision
7737 // implicit 1.
7738 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007739
Bill Wendling8b8a6362009-01-17 03:56:04 +00007740 // All conversions up to here are exact. The correctly rounded result is
7741 // calculated using the current rounding mode using the following
7742 // horizontal add.
7743 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7744 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7745 // store doesn't really need to be here (except
7746 // maybe to zero the other double)
7747 return sd;
7748 }
7749 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007750
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007751 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007752 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007753
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007754 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007755 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007756 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7757 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7758 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7759 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007760 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007761 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007762
Bill Wendling8b8a6362009-01-17 03:56:04 +00007763 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007764 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007765 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007766 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007767 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007768 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007769 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007770
Owen Anderson825b72b2009-08-11 20:47:22 +00007771 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7772 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007773 Op.getOperand(0),
7774 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007775 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7776 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007777 Op.getOperand(0),
7778 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007779 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7780 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007781 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007782 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007783 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007784 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007785 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007786 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007787 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007788 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007789
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007790 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007791 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007792 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7793 DAG.getUNDEF(MVT::v2f64), ShufMask);
7794 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7795 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007796 DAG.getIntPtrConstant(0));
7797}
7798
Bill Wendling8b8a6362009-01-17 03:56:04 +00007799// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007800SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7801 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007802 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007803 // FP constant to bias correct the final result.
7804 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007805 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007806
7807 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007808 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007809 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007810
Eli Friedmanf3704762011-08-29 21:15:46 +00007811 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007812 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7813 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007814
Owen Anderson825b72b2009-08-11 20:47:22 +00007815 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007816 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007817 DAG.getIntPtrConstant(0));
7818
7819 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007820 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007821 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007822 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007823 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007824 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007825 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007826 MVT::v2f64, Bias)));
7827 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007828 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007829 DAG.getIntPtrConstant(0));
7830
7831 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007832 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007833
7834 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007835 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007836
Owen Anderson825b72b2009-08-11 20:47:22 +00007837 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007838 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007839 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007840 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007841 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007842 }
7843
7844 // Handle final rounding.
7845 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007846}
7847
Dan Gohmand858e902010-04-17 15:26:15 +00007848SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7849 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007850 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007851 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007852
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007853 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007854 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7855 // the optimization here.
7856 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007857 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007858
Owen Andersone50ed302009-08-10 22:56:29 +00007859 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007860 EVT DstVT = Op.getValueType();
7861 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007862 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007863 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007864 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007865
7866 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007867 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007868 if (SrcVT == MVT::i32) {
7869 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7870 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7871 getPointerTy(), StackSlot, WordOff);
7872 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007873 StackSlot, MachinePointerInfo(),
7874 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007875 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007876 OffsetSlot, MachinePointerInfo(),
7877 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007878 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7879 return Fild;
7880 }
7881
7882 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7883 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007884 StackSlot, MachinePointerInfo(),
7885 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007886 // For i64 source, we need to add the appropriate power of 2 if the input
7887 // was negative. This is the same as the optimization in
7888 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7889 // we must be careful to do the computation in x87 extended precision, not
7890 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007891 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7892 MachineMemOperand *MMO =
7893 DAG.getMachineFunction()
7894 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7895 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007896
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007897 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7898 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007899 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7900 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007901
7902 APInt FF(32, 0x5F800000ULL);
7903
7904 // Check whether the sign bit is set.
7905 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7906 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7907 ISD::SETLT);
7908
7909 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7910 SDValue FudgePtr = DAG.getConstantPool(
7911 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7912 getPointerTy());
7913
7914 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7915 SDValue Zero = DAG.getIntPtrConstant(0);
7916 SDValue Four = DAG.getIntPtrConstant(4);
7917 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7918 Zero, Four);
7919 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7920
7921 // Load the value out, extending it from f32 to f80.
7922 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007923 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007924 FudgePtr, MachinePointerInfo::getConstantPool(),
7925 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007926 // Extend everything to 80 bits to force it to be done on x87.
7927 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7928 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007929}
7930
Dan Gohman475871a2008-07-27 21:46:04 +00007931std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007932FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007933 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007934
Owen Andersone50ed302009-08-10 22:56:29 +00007935 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007936
7937 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007938 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7939 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007940 }
7941
Owen Anderson825b72b2009-08-11 20:47:22 +00007942 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7943 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007944 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007945
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007946 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007947 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007948 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007949 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007950 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007951 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007952 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007953 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007954
Evan Cheng87c89352007-10-15 20:11:21 +00007955 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7956 // stack slot.
7957 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007958 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007959 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007960 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007961
Michael J. Spencerec38de22010-10-10 22:04:20 +00007962
7963
Evan Cheng0db9fe62006-04-25 20:13:52 +00007964 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007965 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007966 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007967 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7968 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7969 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007970 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007971
Dan Gohman475871a2008-07-27 21:46:04 +00007972 SDValue Chain = DAG.getEntryNode();
7973 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007974 EVT TheVT = Op.getOperand(0).getValueType();
7975 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007976 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007977 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007978 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007979 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007980 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007981 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007982 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007983 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007984
Chris Lattner492a43e2010-09-22 01:28:21 +00007985 MachineMemOperand *MMO =
7986 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7987 MachineMemOperand::MOLoad, MemSize, MemSize);
7988 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7989 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007990 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007991 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007992 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7993 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007994
Chris Lattner07290932010-09-22 01:05:16 +00007995 MachineMemOperand *MMO =
7996 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7997 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007998
Evan Cheng0db9fe62006-04-25 20:13:52 +00007999 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00008000 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00008001 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8002 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00008003
Chris Lattner27a6c732007-11-24 07:07:01 +00008004 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008005}
8006
Dan Gohmand858e902010-04-17 15:26:15 +00008007SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8008 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008009 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008010 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008011
Eli Friedman948e95a2009-05-23 09:59:16 +00008012 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00008013 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008014 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8015 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008016
Chris Lattner27a6c732007-11-24 07:07:01 +00008017 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008018 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008019 FIST, StackSlot, MachinePointerInfo(),
8020 false, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00008021}
8022
Dan Gohmand858e902010-04-17 15:26:15 +00008023SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8024 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00008025 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
8026 SDValue FIST = Vals.first, StackSlot = Vals.second;
8027 assert(FIST.getNode() && "Unexpected failure");
8028
8029 // Load the result.
8030 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008031 FIST, StackSlot, MachinePointerInfo(),
8032 false, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00008033}
8034
Dan Gohmand858e902010-04-17 15:26:15 +00008035SDValue X86TargetLowering::LowerFABS(SDValue Op,
8036 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008037 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008038 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008039 EVT VT = Op.getValueType();
8040 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008041 if (VT.isVector())
8042 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008043 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008044 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008045 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00008046 CV.push_back(C);
8047 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008048 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008049 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00008050 CV.push_back(C);
8051 CV.push_back(C);
8052 CV.push_back(C);
8053 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008054 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008055 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008056 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008057 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008058 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008059 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008060 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008061}
8062
Dan Gohmand858e902010-04-17 15:26:15 +00008063SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008064 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008065 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008066 EVT VT = Op.getValueType();
8067 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008068 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008069 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008070 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008071 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008072 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008073 CV.push_back(C);
8074 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008075 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008076 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008077 CV.push_back(C);
8078 CV.push_back(C);
8079 CV.push_back(C);
8080 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008081 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008082 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008083 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008084 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008085 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008086 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008087 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008088 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008089 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008090 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008091 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008092 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008093 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008094 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008095 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008096}
8097
Dan Gohmand858e902010-04-17 15:26:15 +00008098SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008099 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008100 SDValue Op0 = Op.getOperand(0);
8101 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008102 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008103 EVT VT = Op.getValueType();
8104 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008105
8106 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008107 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008108 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008109 SrcVT = VT;
8110 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008111 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008112 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008113 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008114 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008115 }
8116
8117 // At this point the operands and the result should have the same
8118 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008119
Evan Cheng68c47cb2007-01-05 07:55:56 +00008120 // First get the sign bit of second operand.
8121 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008122 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008123 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8124 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008125 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008126 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8127 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8128 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8129 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008130 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008131 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008132 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008133 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008134 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008135 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008136 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008137
8138 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008139 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008140 // Op0 is MVT::f32, Op1 is MVT::f64.
8141 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8142 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8143 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008144 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008145 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008146 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008147 }
8148
Evan Cheng73d6cf12007-01-05 21:37:56 +00008149 // Clear first operand sign bit.
8150 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008151 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008152 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8153 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008154 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008155 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8156 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8157 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8158 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008159 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008160 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008161 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008162 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008163 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008164 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008165 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008166
8167 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008168 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008169}
8170
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008171SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8172 SDValue N0 = Op.getOperand(0);
8173 DebugLoc dl = Op.getDebugLoc();
8174 EVT VT = Op.getValueType();
8175
8176 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8177 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8178 DAG.getConstant(1, VT));
8179 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8180}
8181
Dan Gohman076aee32009-03-04 19:44:21 +00008182/// Emit nodes that will be selected as "test Op0,Op0", or something
8183/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008184SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008185 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008186 DebugLoc dl = Op.getDebugLoc();
8187
Dan Gohman31125812009-03-07 01:58:32 +00008188 // CF and OF aren't always set the way we want. Determine which
8189 // of these we need.
8190 bool NeedCF = false;
8191 bool NeedOF = false;
8192 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008193 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008194 case X86::COND_A: case X86::COND_AE:
8195 case X86::COND_B: case X86::COND_BE:
8196 NeedCF = true;
8197 break;
8198 case X86::COND_G: case X86::COND_GE:
8199 case X86::COND_L: case X86::COND_LE:
8200 case X86::COND_O: case X86::COND_NO:
8201 NeedOF = true;
8202 break;
Dan Gohman31125812009-03-07 01:58:32 +00008203 }
8204
Dan Gohman076aee32009-03-04 19:44:21 +00008205 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008206 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8207 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008208 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8209 // Emit a CMP with 0, which is the TEST pattern.
8210 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8211 DAG.getConstant(0, Op.getValueType()));
8212
8213 unsigned Opcode = 0;
8214 unsigned NumOperands = 0;
8215 switch (Op.getNode()->getOpcode()) {
8216 case ISD::ADD:
8217 // Due to an isel shortcoming, be conservative if this add is likely to be
8218 // selected as part of a load-modify-store instruction. When the root node
8219 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8220 // uses of other nodes in the match, such as the ADD in this case. This
8221 // leads to the ADD being left around and reselected, with the result being
8222 // two adds in the output. Alas, even if none our users are stores, that
8223 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8224 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8225 // climbing the DAG back to the root, and it doesn't seem to be worth the
8226 // effort.
8227 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008228 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8229 if (UI->getOpcode() != ISD::CopyToReg &&
8230 UI->getOpcode() != ISD::SETCC &&
8231 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008232 goto default_case;
8233
8234 if (ConstantSDNode *C =
8235 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8236 // An add of one will be selected as an INC.
8237 if (C->getAPIntValue() == 1) {
8238 Opcode = X86ISD::INC;
8239 NumOperands = 1;
8240 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008241 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008242
8243 // An add of negative one (subtract of one) will be selected as a DEC.
8244 if (C->getAPIntValue().isAllOnesValue()) {
8245 Opcode = X86ISD::DEC;
8246 NumOperands = 1;
8247 break;
8248 }
Dan Gohman076aee32009-03-04 19:44:21 +00008249 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008250
8251 // Otherwise use a regular EFLAGS-setting add.
8252 Opcode = X86ISD::ADD;
8253 NumOperands = 2;
8254 break;
8255 case ISD::AND: {
8256 // If the primary and result isn't used, don't bother using X86ISD::AND,
8257 // because a TEST instruction will be better.
8258 bool NonFlagUse = false;
8259 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8260 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8261 SDNode *User = *UI;
8262 unsigned UOpNo = UI.getOperandNo();
8263 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8264 // Look pass truncate.
8265 UOpNo = User->use_begin().getOperandNo();
8266 User = *User->use_begin();
8267 }
8268
8269 if (User->getOpcode() != ISD::BRCOND &&
8270 User->getOpcode() != ISD::SETCC &&
8271 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8272 NonFlagUse = true;
8273 break;
8274 }
Dan Gohman076aee32009-03-04 19:44:21 +00008275 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008276
8277 if (!NonFlagUse)
8278 break;
8279 }
8280 // FALL THROUGH
8281 case ISD::SUB:
8282 case ISD::OR:
8283 case ISD::XOR:
8284 // Due to the ISEL shortcoming noted above, be conservative if this op is
8285 // likely to be selected as part of a load-modify-store instruction.
8286 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8287 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8288 if (UI->getOpcode() == ISD::STORE)
8289 goto default_case;
8290
8291 // Otherwise use a regular EFLAGS-setting instruction.
8292 switch (Op.getNode()->getOpcode()) {
8293 default: llvm_unreachable("unexpected operator!");
8294 case ISD::SUB: Opcode = X86ISD::SUB; break;
8295 case ISD::OR: Opcode = X86ISD::OR; break;
8296 case ISD::XOR: Opcode = X86ISD::XOR; break;
8297 case ISD::AND: Opcode = X86ISD::AND; break;
8298 }
8299
8300 NumOperands = 2;
8301 break;
8302 case X86ISD::ADD:
8303 case X86ISD::SUB:
8304 case X86ISD::INC:
8305 case X86ISD::DEC:
8306 case X86ISD::OR:
8307 case X86ISD::XOR:
8308 case X86ISD::AND:
8309 return SDValue(Op.getNode(), 1);
8310 default:
8311 default_case:
8312 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008313 }
8314
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008315 if (Opcode == 0)
8316 // Emit a CMP with 0, which is the TEST pattern.
8317 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8318 DAG.getConstant(0, Op.getValueType()));
8319
8320 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8321 SmallVector<SDValue, 4> Ops;
8322 for (unsigned i = 0; i != NumOperands; ++i)
8323 Ops.push_back(Op.getOperand(i));
8324
8325 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8326 DAG.ReplaceAllUsesWith(Op, New);
8327 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008328}
8329
8330/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8331/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008332SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008333 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008334 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8335 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008336 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008337
8338 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008339 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008340}
8341
Evan Chengd40d03e2010-01-06 19:38:29 +00008342/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8343/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008344SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8345 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008346 SDValue Op0 = And.getOperand(0);
8347 SDValue Op1 = And.getOperand(1);
8348 if (Op0.getOpcode() == ISD::TRUNCATE)
8349 Op0 = Op0.getOperand(0);
8350 if (Op1.getOpcode() == ISD::TRUNCATE)
8351 Op1 = Op1.getOperand(0);
8352
Evan Chengd40d03e2010-01-06 19:38:29 +00008353 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008354 if (Op1.getOpcode() == ISD::SHL)
8355 std::swap(Op0, Op1);
8356 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008357 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8358 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008359 // If we looked past a truncate, check that it's only truncating away
8360 // known zeros.
8361 unsigned BitWidth = Op0.getValueSizeInBits();
8362 unsigned AndBitWidth = And.getValueSizeInBits();
8363 if (BitWidth > AndBitWidth) {
8364 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8365 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8366 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8367 return SDValue();
8368 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008369 LHS = Op1;
8370 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008371 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008372 } else if (Op1.getOpcode() == ISD::Constant) {
8373 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008374 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008375 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008376
8377 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008378 LHS = AndLHS.getOperand(0);
8379 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008380 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008381
8382 // Use BT if the immediate can't be encoded in a TEST instruction.
8383 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8384 LHS = AndLHS;
8385 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8386 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008387 }
Evan Cheng0488db92007-09-25 01:57:46 +00008388
Evan Chengd40d03e2010-01-06 19:38:29 +00008389 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008390 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008391 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008392 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008393 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008394 // Also promote i16 to i32 for performance / code size reason.
8395 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008396 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008397 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008398
Evan Chengd40d03e2010-01-06 19:38:29 +00008399 // If the operand types disagree, extend the shift amount to match. Since
8400 // BT ignores high bits (like shifts) we can use anyextend.
8401 if (LHS.getValueType() != RHS.getValueType())
8402 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008403
Evan Chengd40d03e2010-01-06 19:38:29 +00008404 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8405 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8406 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8407 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008408 }
8409
Evan Cheng54de3ea2010-01-05 06:52:31 +00008410 return SDValue();
8411}
8412
Dan Gohmand858e902010-04-17 15:26:15 +00008413SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008414
8415 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8416
Evan Cheng54de3ea2010-01-05 06:52:31 +00008417 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8418 SDValue Op0 = Op.getOperand(0);
8419 SDValue Op1 = Op.getOperand(1);
8420 DebugLoc dl = Op.getDebugLoc();
8421 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8422
8423 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008424 // Lower (X & (1 << N)) == 0 to BT(X, N).
8425 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8426 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008427 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008428 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008429 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008430 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8431 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8432 if (NewSetCC.getNode())
8433 return NewSetCC;
8434 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008435
Chris Lattner481eebc2010-12-19 21:23:48 +00008436 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8437 // these.
8438 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008439 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008440 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8441 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008442
Chris Lattner481eebc2010-12-19 21:23:48 +00008443 // If the input is a setcc, then reuse the input setcc or use a new one with
8444 // the inverted condition.
8445 if (Op0.getOpcode() == X86ISD::SETCC) {
8446 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8447 bool Invert = (CC == ISD::SETNE) ^
8448 cast<ConstantSDNode>(Op1)->isNullValue();
8449 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008450
Evan Cheng2c755ba2010-02-27 07:36:59 +00008451 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008452 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8453 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8454 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008455 }
8456
Evan Chenge5b51ac2010-04-17 06:13:15 +00008457 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008458 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008459 if (X86CC == X86::COND_INVALID)
8460 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008461
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008462 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008463 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008464 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008465}
8466
Craig Topper89af15e2011-09-18 08:03:58 +00008467// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008468// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008469static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008470 EVT VT = Op.getValueType();
8471
Duncan Sands28b77e92011-09-06 19:07:46 +00008472 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008473 "Unsupported value type for operation");
8474
8475 int NumElems = VT.getVectorNumElements();
8476 DebugLoc dl = Op.getDebugLoc();
8477 SDValue CC = Op.getOperand(2);
8478 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8479 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8480
8481 // Extract the LHS vectors
8482 SDValue LHS = Op.getOperand(0);
8483 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8484 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8485
8486 // Extract the RHS vectors
8487 SDValue RHS = Op.getOperand(1);
8488 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8489 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8490
8491 // Issue the operation on the smaller types and concatenate the result back
8492 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8493 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8494 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8495 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8496 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8497}
8498
8499
Dan Gohmand858e902010-04-17 15:26:15 +00008500SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008501 SDValue Cond;
8502 SDValue Op0 = Op.getOperand(0);
8503 SDValue Op1 = Op.getOperand(1);
8504 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008505 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008506 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8507 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008508 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008509
8510 if (isFP) {
8511 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008512 EVT EltVT = Op0.getValueType().getVectorElementType();
8513 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8514
8515 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008516 bool Swap = false;
8517
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008518 // SSE Condition code mapping:
8519 // 0 - EQ
8520 // 1 - LT
8521 // 2 - LE
8522 // 3 - UNORD
8523 // 4 - NEQ
8524 // 5 - NLT
8525 // 6 - NLE
8526 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008527 switch (SetCCOpcode) {
8528 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008529 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008530 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008531 case ISD::SETOGT:
8532 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008533 case ISD::SETLT:
8534 case ISD::SETOLT: SSECC = 1; break;
8535 case ISD::SETOGE:
8536 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008537 case ISD::SETLE:
8538 case ISD::SETOLE: SSECC = 2; break;
8539 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008540 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008541 case ISD::SETNE: SSECC = 4; break;
8542 case ISD::SETULE: Swap = true;
8543 case ISD::SETUGE: SSECC = 5; break;
8544 case ISD::SETULT: Swap = true;
8545 case ISD::SETUGT: SSECC = 6; break;
8546 case ISD::SETO: SSECC = 7; break;
8547 }
8548 if (Swap)
8549 std::swap(Op0, Op1);
8550
Nate Begemanfb8ead02008-07-25 19:05:58 +00008551 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008552 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008553 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008554 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008555 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8556 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008557 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Craig Topper0a150352011-11-09 08:06:13 +00008558 } else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008559 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008560 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8561 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008562 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008563 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008564 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008565 }
8566 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008567 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008568 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008569
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008570 // Break 256-bit integer vector compare into smaller ones.
Craig Topper0a150352011-11-09 08:06:13 +00008571 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008572 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008573
Nate Begeman30a0de92008-07-17 16:51:19 +00008574 // We are handling one of the integer comparisons here. Since SSE only has
8575 // GT and EQ comparisons for integer, swapping operands and multiple
8576 // operations may be required for some comparisons.
8577 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8578 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008579
Craig Topper0a150352011-11-09 08:06:13 +00008580 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008581 default: break;
Craig Topper0a150352011-11-09 08:06:13 +00008582 case MVT::i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
8583 case MVT::i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
8584 case MVT::i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8585 case MVT::i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008586 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008587
Nate Begeman30a0de92008-07-17 16:51:19 +00008588 switch (SetCCOpcode) {
8589 default: break;
8590 case ISD::SETNE: Invert = true;
8591 case ISD::SETEQ: Opc = EQOpc; break;
8592 case ISD::SETLT: Swap = true;
8593 case ISD::SETGT: Opc = GTOpc; break;
8594 case ISD::SETGE: Swap = true;
8595 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8596 case ISD::SETULT: Swap = true;
8597 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8598 case ISD::SETUGE: Swap = true;
8599 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8600 }
8601 if (Swap)
8602 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008603
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008604 // Check that the operation in question is available (most are plain SSE2,
8605 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topperc0d82852011-11-22 00:44:41 +00008606 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42orAVX())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008607 return SDValue();
Craig Topperc0d82852011-11-22 00:44:41 +00008608 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41orAVX())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008609 return SDValue();
8610
Nate Begeman30a0de92008-07-17 16:51:19 +00008611 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8612 // bits of the inputs before performing those operations.
8613 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008614 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008615 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8616 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008617 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008618 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8619 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008620 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8621 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008622 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008623
Dale Johannesenace16102009-02-03 19:33:06 +00008624 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008625
8626 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008627 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008628 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008629
Nate Begeman30a0de92008-07-17 16:51:19 +00008630 return Result;
8631}
Evan Cheng0488db92007-09-25 01:57:46 +00008632
Evan Cheng370e5342008-12-03 08:38:43 +00008633// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008634static bool isX86LogicalCmp(SDValue Op) {
8635 unsigned Opc = Op.getNode()->getOpcode();
8636 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8637 return true;
8638 if (Op.getResNo() == 1 &&
8639 (Opc == X86ISD::ADD ||
8640 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008641 Opc == X86ISD::ADC ||
8642 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008643 Opc == X86ISD::SMUL ||
8644 Opc == X86ISD::UMUL ||
8645 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008646 Opc == X86ISD::DEC ||
8647 Opc == X86ISD::OR ||
8648 Opc == X86ISD::XOR ||
8649 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008650 return true;
8651
Chris Lattner9637d5b2010-12-05 07:49:54 +00008652 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8653 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008654
Dan Gohman076aee32009-03-04 19:44:21 +00008655 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008656}
8657
Chris Lattnera2b56002010-12-05 01:23:24 +00008658static bool isZero(SDValue V) {
8659 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8660 return C && C->isNullValue();
8661}
8662
Chris Lattner96908b12010-12-05 02:00:51 +00008663static bool isAllOnes(SDValue V) {
8664 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8665 return C && C->isAllOnesValue();
8666}
8667
Dan Gohmand858e902010-04-17 15:26:15 +00008668SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008669 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008670 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008671 SDValue Op1 = Op.getOperand(1);
8672 SDValue Op2 = Op.getOperand(2);
8673 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008674 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008675
Dan Gohman1a492952009-10-20 16:22:37 +00008676 if (Cond.getOpcode() == ISD::SETCC) {
8677 SDValue NewCond = LowerSETCC(Cond, DAG);
8678 if (NewCond.getNode())
8679 Cond = NewCond;
8680 }
Evan Cheng734503b2006-09-11 02:19:56 +00008681
Chris Lattnera2b56002010-12-05 01:23:24 +00008682 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008683 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008684 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008685 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008686 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008687 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8688 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008689 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008690
Chris Lattnera2b56002010-12-05 01:23:24 +00008691 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008692
8693 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008694 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8695 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008696
8697 SDValue CmpOp0 = Cmp.getOperand(0);
8698 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8699 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008700
Chris Lattner96908b12010-12-05 02:00:51 +00008701 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008702 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8703 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008704
Chris Lattner96908b12010-12-05 02:00:51 +00008705 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8706 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008707
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008708 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008709 if (N2C == 0 || !N2C->isNullValue())
8710 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8711 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008712 }
8713 }
8714
Chris Lattnera2b56002010-12-05 01:23:24 +00008715 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008716 if (Cond.getOpcode() == ISD::AND &&
8717 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8718 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008719 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008720 Cond = Cond.getOperand(0);
8721 }
8722
Evan Cheng3f41d662007-10-08 22:16:29 +00008723 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8724 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008725 unsigned CondOpcode = Cond.getOpcode();
8726 if (CondOpcode == X86ISD::SETCC ||
8727 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008728 CC = Cond.getOperand(0);
8729
Dan Gohman475871a2008-07-27 21:46:04 +00008730 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008731 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008732 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008733
Evan Cheng3f41d662007-10-08 22:16:29 +00008734 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008735 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008736 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008737 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008738
Chris Lattnerd1980a52009-03-12 06:52:53 +00008739 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8740 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008741 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008742 addTest = false;
8743 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008744 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8745 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8746 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8747 Cond.getOperand(0).getValueType() != MVT::i8)) {
8748 SDValue LHS = Cond.getOperand(0);
8749 SDValue RHS = Cond.getOperand(1);
8750 unsigned X86Opcode;
8751 unsigned X86Cond;
8752 SDVTList VTs;
8753 switch (CondOpcode) {
8754 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8755 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8756 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8757 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8758 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8759 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8760 default: llvm_unreachable("unexpected overflowing operator");
8761 }
8762 if (CondOpcode == ISD::UMULO)
8763 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8764 MVT::i32);
8765 else
8766 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8767
8768 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8769
8770 if (CondOpcode == ISD::UMULO)
8771 Cond = X86Op.getValue(2);
8772 else
8773 Cond = X86Op.getValue(1);
8774
8775 CC = DAG.getConstant(X86Cond, MVT::i8);
8776 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008777 }
8778
8779 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008780 // Look pass the truncate.
8781 if (Cond.getOpcode() == ISD::TRUNCATE)
8782 Cond = Cond.getOperand(0);
8783
8784 // We know the result of AND is compared against zero. Try to match
8785 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008786 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008787 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008788 if (NewSetCC.getNode()) {
8789 CC = NewSetCC.getOperand(0);
8790 Cond = NewSetCC.getOperand(1);
8791 addTest = false;
8792 }
8793 }
8794 }
8795
8796 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008797 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008798 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008799 }
8800
Benjamin Kramere915ff32010-12-22 23:09:28 +00008801 // a < b ? -1 : 0 -> RES = ~setcc_carry
8802 // a < b ? 0 : -1 -> RES = setcc_carry
8803 // a >= b ? -1 : 0 -> RES = setcc_carry
8804 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8805 if (Cond.getOpcode() == X86ISD::CMP) {
8806 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8807
8808 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8809 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8810 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8811 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8812 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8813 return DAG.getNOT(DL, Res, Res.getValueType());
8814 return Res;
8815 }
8816 }
8817
Evan Cheng0488db92007-09-25 01:57:46 +00008818 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8819 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008820 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008821 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008822 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008823}
8824
Evan Cheng370e5342008-12-03 08:38:43 +00008825// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8826// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8827// from the AND / OR.
8828static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8829 Opc = Op.getOpcode();
8830 if (Opc != ISD::OR && Opc != ISD::AND)
8831 return false;
8832 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8833 Op.getOperand(0).hasOneUse() &&
8834 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8835 Op.getOperand(1).hasOneUse());
8836}
8837
Evan Cheng961d6d42009-02-02 08:19:07 +00008838// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8839// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008840static bool isXor1OfSetCC(SDValue Op) {
8841 if (Op.getOpcode() != ISD::XOR)
8842 return false;
8843 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8844 if (N1C && N1C->getAPIntValue() == 1) {
8845 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8846 Op.getOperand(0).hasOneUse();
8847 }
8848 return false;
8849}
8850
Dan Gohmand858e902010-04-17 15:26:15 +00008851SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008852 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008853 SDValue Chain = Op.getOperand(0);
8854 SDValue Cond = Op.getOperand(1);
8855 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008856 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008857 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008858 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008859
Dan Gohman1a492952009-10-20 16:22:37 +00008860 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008861 // Check for setcc([su]{add,sub,mul}o == 0).
8862 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8863 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8864 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8865 Cond.getOperand(0).getResNo() == 1 &&
8866 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
8867 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
8868 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
8869 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
8870 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
8871 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
8872 Inverted = true;
8873 Cond = Cond.getOperand(0);
8874 } else {
8875 SDValue NewCond = LowerSETCC(Cond, DAG);
8876 if (NewCond.getNode())
8877 Cond = NewCond;
8878 }
Dan Gohman1a492952009-10-20 16:22:37 +00008879 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008880#if 0
8881 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008882 else if (Cond.getOpcode() == X86ISD::ADD ||
8883 Cond.getOpcode() == X86ISD::SUB ||
8884 Cond.getOpcode() == X86ISD::SMUL ||
8885 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008886 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008887#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008888
Evan Chengad9c0a32009-12-15 00:53:42 +00008889 // Look pass (and (setcc_carry (cmp ...)), 1).
8890 if (Cond.getOpcode() == ISD::AND &&
8891 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8892 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008893 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008894 Cond = Cond.getOperand(0);
8895 }
8896
Evan Cheng3f41d662007-10-08 22:16:29 +00008897 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8898 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008899 unsigned CondOpcode = Cond.getOpcode();
8900 if (CondOpcode == X86ISD::SETCC ||
8901 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008902 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008903
Dan Gohman475871a2008-07-27 21:46:04 +00008904 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008905 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008906 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008907 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008908 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008909 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008910 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008911 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008912 default: break;
8913 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008914 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008915 // These can only come from an arithmetic instruction with overflow,
8916 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008917 Cond = Cond.getNode()->getOperand(1);
8918 addTest = false;
8919 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008920 }
Evan Cheng0488db92007-09-25 01:57:46 +00008921 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008922 }
8923 CondOpcode = Cond.getOpcode();
8924 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8925 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8926 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8927 Cond.getOperand(0).getValueType() != MVT::i8)) {
8928 SDValue LHS = Cond.getOperand(0);
8929 SDValue RHS = Cond.getOperand(1);
8930 unsigned X86Opcode;
8931 unsigned X86Cond;
8932 SDVTList VTs;
8933 switch (CondOpcode) {
8934 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8935 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8936 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8937 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8938 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8939 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8940 default: llvm_unreachable("unexpected overflowing operator");
8941 }
8942 if (Inverted)
8943 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
8944 if (CondOpcode == ISD::UMULO)
8945 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8946 MVT::i32);
8947 else
8948 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8949
8950 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
8951
8952 if (CondOpcode == ISD::UMULO)
8953 Cond = X86Op.getValue(2);
8954 else
8955 Cond = X86Op.getValue(1);
8956
8957 CC = DAG.getConstant(X86Cond, MVT::i8);
8958 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00008959 } else {
8960 unsigned CondOpc;
8961 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8962 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008963 if (CondOpc == ISD::OR) {
8964 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8965 // two branches instead of an explicit OR instruction with a
8966 // separate test.
8967 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008968 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008969 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008970 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008971 Chain, Dest, CC, Cmp);
8972 CC = Cond.getOperand(1).getOperand(0);
8973 Cond = Cmp;
8974 addTest = false;
8975 }
8976 } else { // ISD::AND
8977 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8978 // two branches instead of an explicit AND instruction with a
8979 // separate test. However, we only do this if this block doesn't
8980 // have a fall-through edge, because this requires an explicit
8981 // jmp when the condition is false.
8982 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008983 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008984 Op.getNode()->hasOneUse()) {
8985 X86::CondCode CCode =
8986 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8987 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008988 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008989 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008990 // Look for an unconditional branch following this conditional branch.
8991 // We need this because we need to reverse the successors in order
8992 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008993 if (User->getOpcode() == ISD::BR) {
8994 SDValue FalseBB = User->getOperand(1);
8995 SDNode *NewBR =
8996 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008997 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008998 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008999 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009000
Dale Johannesene4d209d2009-02-03 20:21:25 +00009001 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009002 Chain, Dest, CC, Cmp);
9003 X86::CondCode CCode =
9004 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9005 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009006 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009007 Cond = Cmp;
9008 addTest = false;
9009 }
9010 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009011 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009012 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9013 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9014 // It should be transformed during dag combiner except when the condition
9015 // is set by a arithmetics with overflow node.
9016 X86::CondCode CCode =
9017 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9018 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009019 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009020 Cond = Cond.getOperand(0).getOperand(1);
9021 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009022 } else if (Cond.getOpcode() == ISD::SETCC &&
9023 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9024 // For FCMP_OEQ, we can emit
9025 // two branches instead of an explicit AND instruction with a
9026 // separate test. However, we only do this if this block doesn't
9027 // have a fall-through edge, because this requires an explicit
9028 // jmp when the condition is false.
9029 if (Op.getNode()->hasOneUse()) {
9030 SDNode *User = *Op.getNode()->use_begin();
9031 // Look for an unconditional branch following this conditional branch.
9032 // We need this because we need to reverse the successors in order
9033 // to implement FCMP_OEQ.
9034 if (User->getOpcode() == ISD::BR) {
9035 SDValue FalseBB = User->getOperand(1);
9036 SDNode *NewBR =
9037 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9038 assert(NewBR == User);
9039 (void)NewBR;
9040 Dest = FalseBB;
9041
9042 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9043 Cond.getOperand(0), Cond.getOperand(1));
9044 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9045 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9046 Chain, Dest, CC, Cmp);
9047 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9048 Cond = Cmp;
9049 addTest = false;
9050 }
9051 }
9052 } else if (Cond.getOpcode() == ISD::SETCC &&
9053 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9054 // For FCMP_UNE, we can emit
9055 // two branches instead of an explicit AND instruction with a
9056 // separate test. However, we only do this if this block doesn't
9057 // have a fall-through edge, because this requires an explicit
9058 // jmp when the condition is false.
9059 if (Op.getNode()->hasOneUse()) {
9060 SDNode *User = *Op.getNode()->use_begin();
9061 // Look for an unconditional branch following this conditional branch.
9062 // We need this because we need to reverse the successors in order
9063 // to implement FCMP_UNE.
9064 if (User->getOpcode() == ISD::BR) {
9065 SDValue FalseBB = User->getOperand(1);
9066 SDNode *NewBR =
9067 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9068 assert(NewBR == User);
9069 (void)NewBR;
9070
9071 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9072 Cond.getOperand(0), Cond.getOperand(1));
9073 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9074 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9075 Chain, Dest, CC, Cmp);
9076 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9077 Cond = Cmp;
9078 addTest = false;
9079 Dest = FalseBB;
9080 }
9081 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009082 }
Evan Cheng0488db92007-09-25 01:57:46 +00009083 }
9084
9085 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009086 // Look pass the truncate.
9087 if (Cond.getOpcode() == ISD::TRUNCATE)
9088 Cond = Cond.getOperand(0);
9089
9090 // We know the result of AND is compared against zero. Try to match
9091 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009092 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009093 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9094 if (NewSetCC.getNode()) {
9095 CC = NewSetCC.getOperand(0);
9096 Cond = NewSetCC.getOperand(1);
9097 addTest = false;
9098 }
9099 }
9100 }
9101
9102 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009103 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009104 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009105 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00009106 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009107 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009108}
9109
Anton Korobeynikove060b532007-04-17 19:34:00 +00009110
9111// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9112// Calls to _alloca is needed to probe the stack when allocating more than 4k
9113// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9114// that the guard pages used by the OS virtual memory manager are allocated in
9115// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009116SDValue
9117X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009118 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009119 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009120 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009121 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009122 "are being used");
9123 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009124 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009125
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009126 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009127 SDValue Chain = Op.getOperand(0);
9128 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009129 // FIXME: Ensure alignment here
9130
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009131 bool Is64Bit = Subtarget->is64Bit();
9132 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009133
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009134 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009135 MachineFunction &MF = DAG.getMachineFunction();
9136 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009137
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009138 if (Is64Bit) {
9139 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009140 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009141 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009142
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009143 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
9144 I != E; I++)
9145 if (I->hasNestAttr())
9146 report_fatal_error("Cannot use segmented stacks with functions that "
9147 "have nested arguments.");
9148 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009149
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009150 const TargetRegisterClass *AddrRegClass =
9151 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9152 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9153 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9154 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9155 DAG.getRegister(Vreg, SPTy));
9156 SDValue Ops1[2] = { Value, Chain };
9157 return DAG.getMergeValues(Ops1, 2, dl);
9158 } else {
9159 SDValue Flag;
9160 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009161
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009162 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9163 Flag = Chain.getValue(1);
9164 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009165
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009166 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9167 Flag = Chain.getValue(1);
9168
9169 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9170
9171 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9172 return DAG.getMergeValues(Ops1, 2, dl);
9173 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009174}
9175
Dan Gohmand858e902010-04-17 15:26:15 +00009176SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009177 MachineFunction &MF = DAG.getMachineFunction();
9178 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9179
Dan Gohman69de1932008-02-06 22:27:42 +00009180 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009181 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009182
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009183 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009184 // vastart just stores the address of the VarArgsFrameIndex slot into the
9185 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009186 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9187 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009188 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9189 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009190 }
9191
9192 // __va_list_tag:
9193 // gp_offset (0 - 6 * 8)
9194 // fp_offset (48 - 48 + 8 * 16)
9195 // overflow_arg_area (point to parameters coming in memory).
9196 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009197 SmallVector<SDValue, 8> MemOps;
9198 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009199 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009200 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009201 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9202 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009203 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009204 MemOps.push_back(Store);
9205
9206 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009207 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009208 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009209 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009210 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9211 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009212 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009213 MemOps.push_back(Store);
9214
9215 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009216 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009217 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009218 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9219 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009220 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9221 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009222 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009223 MemOps.push_back(Store);
9224
9225 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009226 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009227 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009228 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9229 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009230 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9231 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009232 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009233 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009234 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009235}
9236
Dan Gohmand858e902010-04-17 15:26:15 +00009237SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009238 assert(Subtarget->is64Bit() &&
9239 "LowerVAARG only handles 64-bit va_arg!");
9240 assert((Subtarget->isTargetLinux() ||
9241 Subtarget->isTargetDarwin()) &&
9242 "Unhandled target in LowerVAARG");
9243 assert(Op.getNode()->getNumOperands() == 4);
9244 SDValue Chain = Op.getOperand(0);
9245 SDValue SrcPtr = Op.getOperand(1);
9246 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9247 unsigned Align = Op.getConstantOperandVal(3);
9248 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009249
Dan Gohman320afb82010-10-12 18:00:49 +00009250 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009251 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009252 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9253 uint8_t ArgMode;
9254
9255 // Decide which area this value should be read from.
9256 // TODO: Implement the AMD64 ABI in its entirety. This simple
9257 // selection mechanism works only for the basic types.
9258 if (ArgVT == MVT::f80) {
9259 llvm_unreachable("va_arg for f80 not yet implemented");
9260 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9261 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9262 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9263 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9264 } else {
9265 llvm_unreachable("Unhandled argument type in LowerVAARG");
9266 }
9267
9268 if (ArgMode == 2) {
9269 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009270 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009271 !(DAG.getMachineFunction()
9272 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009273 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009274 }
9275
9276 // Insert VAARG_64 node into the DAG
9277 // VAARG_64 returns two values: Variable Argument Address, Chain
9278 SmallVector<SDValue, 11> InstOps;
9279 InstOps.push_back(Chain);
9280 InstOps.push_back(SrcPtr);
9281 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9282 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9283 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9284 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9285 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9286 VTs, &InstOps[0], InstOps.size(),
9287 MVT::i64,
9288 MachinePointerInfo(SV),
9289 /*Align=*/0,
9290 /*Volatile=*/false,
9291 /*ReadMem=*/true,
9292 /*WriteMem=*/true);
9293 Chain = VAARG.getValue(1);
9294
9295 // Load the next argument and return it
9296 return DAG.getLoad(ArgVT, dl,
9297 Chain,
9298 VAARG,
9299 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009300 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009301}
9302
Dan Gohmand858e902010-04-17 15:26:15 +00009303SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009304 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009305 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009306 SDValue Chain = Op.getOperand(0);
9307 SDValue DstPtr = Op.getOperand(1);
9308 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009309 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9310 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009311 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009312
Chris Lattnere72f2022010-09-21 05:40:29 +00009313 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009314 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009315 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009316 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009317}
9318
Dan Gohman475871a2008-07-27 21:46:04 +00009319SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009320X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009321 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009322 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009323 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009324 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009325 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009326 case Intrinsic::x86_sse_comieq_ss:
9327 case Intrinsic::x86_sse_comilt_ss:
9328 case Intrinsic::x86_sse_comile_ss:
9329 case Intrinsic::x86_sse_comigt_ss:
9330 case Intrinsic::x86_sse_comige_ss:
9331 case Intrinsic::x86_sse_comineq_ss:
9332 case Intrinsic::x86_sse_ucomieq_ss:
9333 case Intrinsic::x86_sse_ucomilt_ss:
9334 case Intrinsic::x86_sse_ucomile_ss:
9335 case Intrinsic::x86_sse_ucomigt_ss:
9336 case Intrinsic::x86_sse_ucomige_ss:
9337 case Intrinsic::x86_sse_ucomineq_ss:
9338 case Intrinsic::x86_sse2_comieq_sd:
9339 case Intrinsic::x86_sse2_comilt_sd:
9340 case Intrinsic::x86_sse2_comile_sd:
9341 case Intrinsic::x86_sse2_comigt_sd:
9342 case Intrinsic::x86_sse2_comige_sd:
9343 case Intrinsic::x86_sse2_comineq_sd:
9344 case Intrinsic::x86_sse2_ucomieq_sd:
9345 case Intrinsic::x86_sse2_ucomilt_sd:
9346 case Intrinsic::x86_sse2_ucomile_sd:
9347 case Intrinsic::x86_sse2_ucomigt_sd:
9348 case Intrinsic::x86_sse2_ucomige_sd:
9349 case Intrinsic::x86_sse2_ucomineq_sd: {
9350 unsigned Opc = 0;
9351 ISD::CondCode CC = ISD::SETCC_INVALID;
9352 switch (IntNo) {
9353 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009354 case Intrinsic::x86_sse_comieq_ss:
9355 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009356 Opc = X86ISD::COMI;
9357 CC = ISD::SETEQ;
9358 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009359 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009360 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009361 Opc = X86ISD::COMI;
9362 CC = ISD::SETLT;
9363 break;
9364 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009365 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009366 Opc = X86ISD::COMI;
9367 CC = ISD::SETLE;
9368 break;
9369 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009370 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009371 Opc = X86ISD::COMI;
9372 CC = ISD::SETGT;
9373 break;
9374 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009375 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009376 Opc = X86ISD::COMI;
9377 CC = ISD::SETGE;
9378 break;
9379 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009380 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009381 Opc = X86ISD::COMI;
9382 CC = ISD::SETNE;
9383 break;
9384 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009385 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009386 Opc = X86ISD::UCOMI;
9387 CC = ISD::SETEQ;
9388 break;
9389 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009390 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009391 Opc = X86ISD::UCOMI;
9392 CC = ISD::SETLT;
9393 break;
9394 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009395 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009396 Opc = X86ISD::UCOMI;
9397 CC = ISD::SETLE;
9398 break;
9399 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009400 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009401 Opc = X86ISD::UCOMI;
9402 CC = ISD::SETGT;
9403 break;
9404 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009405 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009406 Opc = X86ISD::UCOMI;
9407 CC = ISD::SETGE;
9408 break;
9409 case Intrinsic::x86_sse_ucomineq_ss:
9410 case Intrinsic::x86_sse2_ucomineq_sd:
9411 Opc = X86ISD::UCOMI;
9412 CC = ISD::SETNE;
9413 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009414 }
Evan Cheng734503b2006-09-11 02:19:56 +00009415
Dan Gohman475871a2008-07-27 21:46:04 +00009416 SDValue LHS = Op.getOperand(1);
9417 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009418 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009419 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009420 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9421 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9422 DAG.getConstant(X86CC, MVT::i8), Cond);
9423 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009424 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009425 // Arithmetic intrinsics.
9426 case Intrinsic::x86_sse3_hadd_ps:
9427 case Intrinsic::x86_sse3_hadd_pd:
9428 case Intrinsic::x86_avx_hadd_ps_256:
9429 case Intrinsic::x86_avx_hadd_pd_256:
9430 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9431 Op.getOperand(1), Op.getOperand(2));
9432 case Intrinsic::x86_sse3_hsub_ps:
9433 case Intrinsic::x86_sse3_hsub_pd:
9434 case Intrinsic::x86_avx_hsub_ps_256:
9435 case Intrinsic::x86_avx_hsub_pd_256:
9436 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9437 Op.getOperand(1), Op.getOperand(2));
Craig Topper98fc7292011-11-19 17:46:46 +00009438 case Intrinsic::x86_avx2_psllv_d:
9439 case Intrinsic::x86_avx2_psllv_q:
9440 case Intrinsic::x86_avx2_psllv_d_256:
9441 case Intrinsic::x86_avx2_psllv_q_256:
9442 return DAG.getNode(ISD::SHL, dl, Op.getValueType(),
9443 Op.getOperand(1), Op.getOperand(2));
9444 case Intrinsic::x86_avx2_psrlv_d:
9445 case Intrinsic::x86_avx2_psrlv_q:
9446 case Intrinsic::x86_avx2_psrlv_d_256:
9447 case Intrinsic::x86_avx2_psrlv_q_256:
9448 return DAG.getNode(ISD::SRL, dl, Op.getValueType(),
9449 Op.getOperand(1), Op.getOperand(2));
9450 case Intrinsic::x86_avx2_psrav_d:
9451 case Intrinsic::x86_avx2_psrav_d_256:
9452 return DAG.getNode(ISD::SRA, dl, Op.getValueType(),
9453 Op.getOperand(1), Op.getOperand(2));
9454
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009455 // ptest and testp intrinsics. The intrinsic these come from are designed to
9456 // return an integer value, not just an instruction so lower it to the ptest
9457 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009458 case Intrinsic::x86_sse41_ptestz:
9459 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009460 case Intrinsic::x86_sse41_ptestnzc:
9461 case Intrinsic::x86_avx_ptestz_256:
9462 case Intrinsic::x86_avx_ptestc_256:
9463 case Intrinsic::x86_avx_ptestnzc_256:
9464 case Intrinsic::x86_avx_vtestz_ps:
9465 case Intrinsic::x86_avx_vtestc_ps:
9466 case Intrinsic::x86_avx_vtestnzc_ps:
9467 case Intrinsic::x86_avx_vtestz_pd:
9468 case Intrinsic::x86_avx_vtestc_pd:
9469 case Intrinsic::x86_avx_vtestnzc_pd:
9470 case Intrinsic::x86_avx_vtestz_ps_256:
9471 case Intrinsic::x86_avx_vtestc_ps_256:
9472 case Intrinsic::x86_avx_vtestnzc_ps_256:
9473 case Intrinsic::x86_avx_vtestz_pd_256:
9474 case Intrinsic::x86_avx_vtestc_pd_256:
9475 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9476 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009477 unsigned X86CC = 0;
9478 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009479 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009480 case Intrinsic::x86_avx_vtestz_ps:
9481 case Intrinsic::x86_avx_vtestz_pd:
9482 case Intrinsic::x86_avx_vtestz_ps_256:
9483 case Intrinsic::x86_avx_vtestz_pd_256:
9484 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009485 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009486 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009487 // ZF = 1
9488 X86CC = X86::COND_E;
9489 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009490 case Intrinsic::x86_avx_vtestc_ps:
9491 case Intrinsic::x86_avx_vtestc_pd:
9492 case Intrinsic::x86_avx_vtestc_ps_256:
9493 case Intrinsic::x86_avx_vtestc_pd_256:
9494 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009495 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009496 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009497 // CF = 1
9498 X86CC = X86::COND_B;
9499 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009500 case Intrinsic::x86_avx_vtestnzc_ps:
9501 case Intrinsic::x86_avx_vtestnzc_pd:
9502 case Intrinsic::x86_avx_vtestnzc_ps_256:
9503 case Intrinsic::x86_avx_vtestnzc_pd_256:
9504 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009505 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009506 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009507 // ZF and CF = 0
9508 X86CC = X86::COND_A;
9509 break;
9510 }
Eric Christopherfd179292009-08-27 18:07:15 +00009511
Eric Christopher71c67532009-07-29 00:28:05 +00009512 SDValue LHS = Op.getOperand(1);
9513 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009514 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9515 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009516 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9517 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9518 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009519 }
Evan Cheng5759f972008-05-04 09:15:50 +00009520
9521 // Fix vector shift instructions where the last operand is a non-immediate
9522 // i32 value.
Craig Topper7be5dfd2011-11-12 09:58:49 +00009523 case Intrinsic::x86_avx2_pslli_w:
9524 case Intrinsic::x86_avx2_pslli_d:
9525 case Intrinsic::x86_avx2_pslli_q:
9526 case Intrinsic::x86_avx2_psrli_w:
9527 case Intrinsic::x86_avx2_psrli_d:
9528 case Intrinsic::x86_avx2_psrli_q:
9529 case Intrinsic::x86_avx2_psrai_w:
9530 case Intrinsic::x86_avx2_psrai_d:
Evan Cheng5759f972008-05-04 09:15:50 +00009531 case Intrinsic::x86_sse2_pslli_w:
9532 case Intrinsic::x86_sse2_pslli_d:
9533 case Intrinsic::x86_sse2_pslli_q:
9534 case Intrinsic::x86_sse2_psrli_w:
9535 case Intrinsic::x86_sse2_psrli_d:
9536 case Intrinsic::x86_sse2_psrli_q:
9537 case Intrinsic::x86_sse2_psrai_w:
9538 case Intrinsic::x86_sse2_psrai_d:
9539 case Intrinsic::x86_mmx_pslli_w:
9540 case Intrinsic::x86_mmx_pslli_d:
9541 case Intrinsic::x86_mmx_pslli_q:
9542 case Intrinsic::x86_mmx_psrli_w:
9543 case Intrinsic::x86_mmx_psrli_d:
9544 case Intrinsic::x86_mmx_psrli_q:
9545 case Intrinsic::x86_mmx_psrai_w:
9546 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009547 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009548 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009549 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009550
9551 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009552 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009553 switch (IntNo) {
9554 case Intrinsic::x86_sse2_pslli_w:
9555 NewIntNo = Intrinsic::x86_sse2_psll_w;
9556 break;
9557 case Intrinsic::x86_sse2_pslli_d:
9558 NewIntNo = Intrinsic::x86_sse2_psll_d;
9559 break;
9560 case Intrinsic::x86_sse2_pslli_q:
9561 NewIntNo = Intrinsic::x86_sse2_psll_q;
9562 break;
9563 case Intrinsic::x86_sse2_psrli_w:
9564 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9565 break;
9566 case Intrinsic::x86_sse2_psrli_d:
9567 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9568 break;
9569 case Intrinsic::x86_sse2_psrli_q:
9570 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9571 break;
9572 case Intrinsic::x86_sse2_psrai_w:
9573 NewIntNo = Intrinsic::x86_sse2_psra_w;
9574 break;
9575 case Intrinsic::x86_sse2_psrai_d:
9576 NewIntNo = Intrinsic::x86_sse2_psra_d;
9577 break;
Craig Topper7be5dfd2011-11-12 09:58:49 +00009578 case Intrinsic::x86_avx2_pslli_w:
9579 NewIntNo = Intrinsic::x86_avx2_psll_w;
9580 break;
9581 case Intrinsic::x86_avx2_pslli_d:
9582 NewIntNo = Intrinsic::x86_avx2_psll_d;
9583 break;
9584 case Intrinsic::x86_avx2_pslli_q:
9585 NewIntNo = Intrinsic::x86_avx2_psll_q;
9586 break;
9587 case Intrinsic::x86_avx2_psrli_w:
9588 NewIntNo = Intrinsic::x86_avx2_psrl_w;
9589 break;
9590 case Intrinsic::x86_avx2_psrli_d:
9591 NewIntNo = Intrinsic::x86_avx2_psrl_d;
9592 break;
9593 case Intrinsic::x86_avx2_psrli_q:
9594 NewIntNo = Intrinsic::x86_avx2_psrl_q;
9595 break;
9596 case Intrinsic::x86_avx2_psrai_w:
9597 NewIntNo = Intrinsic::x86_avx2_psra_w;
9598 break;
9599 case Intrinsic::x86_avx2_psrai_d:
9600 NewIntNo = Intrinsic::x86_avx2_psra_d;
9601 break;
Evan Cheng5759f972008-05-04 09:15:50 +00009602 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009603 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009604 switch (IntNo) {
9605 case Intrinsic::x86_mmx_pslli_w:
9606 NewIntNo = Intrinsic::x86_mmx_psll_w;
9607 break;
9608 case Intrinsic::x86_mmx_pslli_d:
9609 NewIntNo = Intrinsic::x86_mmx_psll_d;
9610 break;
9611 case Intrinsic::x86_mmx_pslli_q:
9612 NewIntNo = Intrinsic::x86_mmx_psll_q;
9613 break;
9614 case Intrinsic::x86_mmx_psrli_w:
9615 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9616 break;
9617 case Intrinsic::x86_mmx_psrli_d:
9618 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9619 break;
9620 case Intrinsic::x86_mmx_psrli_q:
9621 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9622 break;
9623 case Intrinsic::x86_mmx_psrai_w:
9624 NewIntNo = Intrinsic::x86_mmx_psra_w;
9625 break;
9626 case Intrinsic::x86_mmx_psrai_d:
9627 NewIntNo = Intrinsic::x86_mmx_psra_d;
9628 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009629 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009630 }
9631 break;
9632 }
9633 }
Mon P Wangefa42202009-09-03 19:56:25 +00009634
9635 // The vector shift intrinsics with scalars uses 32b shift amounts but
9636 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9637 // to be zero.
9638 SDValue ShOps[4];
9639 ShOps[0] = ShAmt;
9640 ShOps[1] = DAG.getConstant(0, MVT::i32);
9641 if (ShAmtVT == MVT::v4i32) {
9642 ShOps[2] = DAG.getUNDEF(MVT::i32);
9643 ShOps[3] = DAG.getUNDEF(MVT::i32);
9644 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9645 } else {
9646 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009647// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009648 }
9649
Owen Andersone50ed302009-08-10 22:56:29 +00009650 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009651 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009652 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009653 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009654 Op.getOperand(1), ShAmt);
9655 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009656 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009657}
Evan Cheng72261582005-12-20 06:22:03 +00009658
Dan Gohmand858e902010-04-17 15:26:15 +00009659SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9660 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009661 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9662 MFI->setReturnAddressIsTaken(true);
9663
Bill Wendling64e87322009-01-16 19:25:27 +00009664 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009665 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009666
9667 if (Depth > 0) {
9668 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9669 SDValue Offset =
9670 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009671 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009672 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009673 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009674 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009675 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009676 }
9677
9678 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009679 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009680 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009681 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009682}
9683
Dan Gohmand858e902010-04-17 15:26:15 +00009684SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009685 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9686 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009687
Owen Andersone50ed302009-08-10 22:56:29 +00009688 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009689 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009690 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9691 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009692 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009693 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009694 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9695 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009696 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009697 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009698}
9699
Dan Gohman475871a2008-07-27 21:46:04 +00009700SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009701 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009702 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009703}
9704
Dan Gohmand858e902010-04-17 15:26:15 +00009705SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009706 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009707 SDValue Chain = Op.getOperand(0);
9708 SDValue Offset = Op.getOperand(1);
9709 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009710 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009711
Dan Gohmand8816272010-08-11 18:14:00 +00009712 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9713 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9714 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009715 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009716
Dan Gohmand8816272010-08-11 18:14:00 +00009717 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9718 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009719 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009720 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9721 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009722 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009723 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009724
Dale Johannesene4d209d2009-02-03 20:21:25 +00009725 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009726 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009727 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009728}
9729
Duncan Sands4a544a72011-09-06 13:37:06 +00009730SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9731 SelectionDAG &DAG) const {
9732 return Op.getOperand(0);
9733}
9734
9735SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9736 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009737 SDValue Root = Op.getOperand(0);
9738 SDValue Trmp = Op.getOperand(1); // trampoline
9739 SDValue FPtr = Op.getOperand(2); // nested function
9740 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009741 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009742
Dan Gohman69de1932008-02-06 22:27:42 +00009743 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009744
9745 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009746 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009747
9748 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009749 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9750 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009751
Evan Cheng0e6a0522011-07-18 20:57:22 +00009752 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9753 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009754
9755 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9756
9757 // Load the pointer to the nested function into R11.
9758 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009759 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009760 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009761 Addr, MachinePointerInfo(TrmpAddr),
9762 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009763
Owen Anderson825b72b2009-08-11 20:47:22 +00009764 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9765 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009766 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9767 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009768 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009769
9770 // Load the 'nest' parameter value into R10.
9771 // R10 is specified in X86CallingConv.td
9772 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009773 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9774 DAG.getConstant(10, MVT::i64));
9775 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009776 Addr, MachinePointerInfo(TrmpAddr, 10),
9777 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009778
Owen Anderson825b72b2009-08-11 20:47:22 +00009779 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9780 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009781 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9782 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009783 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009784
9785 // Jump to the nested function.
9786 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009787 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9788 DAG.getConstant(20, MVT::i64));
9789 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009790 Addr, MachinePointerInfo(TrmpAddr, 20),
9791 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009792
9793 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009794 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9795 DAG.getConstant(22, MVT::i64));
9796 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009797 MachinePointerInfo(TrmpAddr, 22),
9798 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009799
Duncan Sands4a544a72011-09-06 13:37:06 +00009800 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009801 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009802 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009803 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009804 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009805 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009806
9807 switch (CC) {
9808 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009809 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009810 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009811 case CallingConv::X86_StdCall: {
9812 // Pass 'nest' parameter in ECX.
9813 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009814 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009815
9816 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009817 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009818 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009819
Chris Lattner58d74912008-03-12 17:45:29 +00009820 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009821 unsigned InRegCount = 0;
9822 unsigned Idx = 1;
9823
9824 for (FunctionType::param_iterator I = FTy->param_begin(),
9825 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009826 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009827 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009828 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009829
9830 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009831 report_fatal_error("Nest register in use - reduce number of inreg"
9832 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009833 }
9834 }
9835 break;
9836 }
9837 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009838 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009839 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009840 // Pass 'nest' parameter in EAX.
9841 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009842 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009843 break;
9844 }
9845
Dan Gohman475871a2008-07-27 21:46:04 +00009846 SDValue OutChains[4];
9847 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009848
Owen Anderson825b72b2009-08-11 20:47:22 +00009849 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9850 DAG.getConstant(10, MVT::i32));
9851 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009852
Chris Lattnera62fe662010-02-05 19:20:30 +00009853 // This is storing the opcode for MOV32ri.
9854 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009855 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009856 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009857 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009858 Trmp, MachinePointerInfo(TrmpAddr),
9859 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009860
Owen Anderson825b72b2009-08-11 20:47:22 +00009861 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9862 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009863 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9864 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009865 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009866
Chris Lattnera62fe662010-02-05 19:20:30 +00009867 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009868 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9869 DAG.getConstant(5, MVT::i32));
9870 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009871 MachinePointerInfo(TrmpAddr, 5),
9872 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009873
Owen Anderson825b72b2009-08-11 20:47:22 +00009874 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9875 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009876 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9877 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009878 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009879
Duncan Sands4a544a72011-09-06 13:37:06 +00009880 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009881 }
9882}
9883
Dan Gohmand858e902010-04-17 15:26:15 +00009884SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9885 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009886 /*
9887 The rounding mode is in bits 11:10 of FPSR, and has the following
9888 settings:
9889 00 Round to nearest
9890 01 Round to -inf
9891 10 Round to +inf
9892 11 Round to 0
9893
9894 FLT_ROUNDS, on the other hand, expects the following:
9895 -1 Undefined
9896 0 Round to 0
9897 1 Round to nearest
9898 2 Round to +inf
9899 3 Round to -inf
9900
9901 To perform the conversion, we do:
9902 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9903 */
9904
9905 MachineFunction &MF = DAG.getMachineFunction();
9906 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009907 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009908 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009909 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009910 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009911
9912 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009913 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009914 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009915
Michael J. Spencerec38de22010-10-10 22:04:20 +00009916
Chris Lattner2156b792010-09-22 01:11:26 +00009917 MachineMemOperand *MMO =
9918 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9919 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009920
Chris Lattner2156b792010-09-22 01:11:26 +00009921 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9922 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9923 DAG.getVTList(MVT::Other),
9924 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009925
9926 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009927 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009928 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009929
9930 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009931 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009932 DAG.getNode(ISD::SRL, DL, MVT::i16,
9933 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009934 CWD, DAG.getConstant(0x800, MVT::i16)),
9935 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009936 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009937 DAG.getNode(ISD::SRL, DL, MVT::i16,
9938 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009939 CWD, DAG.getConstant(0x400, MVT::i16)),
9940 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009941
Dan Gohman475871a2008-07-27 21:46:04 +00009942 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009943 DAG.getNode(ISD::AND, DL, MVT::i16,
9944 DAG.getNode(ISD::ADD, DL, MVT::i16,
9945 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009946 DAG.getConstant(1, MVT::i16)),
9947 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009948
9949
Duncan Sands83ec4b62008-06-06 12:08:01 +00009950 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009951 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009952}
9953
Dan Gohmand858e902010-04-17 15:26:15 +00009954SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009955 EVT VT = Op.getValueType();
9956 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009957 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009958 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009959
9960 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009961 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009962 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009963 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009964 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009965 }
Evan Cheng18efe262007-12-14 02:13:44 +00009966
Evan Cheng152804e2007-12-14 08:30:15 +00009967 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009968 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009969 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009970
9971 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009972 SDValue Ops[] = {
9973 Op,
9974 DAG.getConstant(NumBits+NumBits-1, OpVT),
9975 DAG.getConstant(X86::COND_E, MVT::i8),
9976 Op.getValue(1)
9977 };
9978 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009979
9980 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009981 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009982
Owen Anderson825b72b2009-08-11 20:47:22 +00009983 if (VT == MVT::i8)
9984 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009985 return Op;
9986}
9987
Dan Gohmand858e902010-04-17 15:26:15 +00009988SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009989 EVT VT = Op.getValueType();
9990 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009991 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009992 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009993
9994 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009995 if (VT == MVT::i8) {
9996 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009997 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009998 }
Evan Cheng152804e2007-12-14 08:30:15 +00009999
10000 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010001 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010002 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010003
10004 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010005 SDValue Ops[] = {
10006 Op,
10007 DAG.getConstant(NumBits, OpVT),
10008 DAG.getConstant(X86::COND_E, MVT::i8),
10009 Op.getValue(1)
10010 };
10011 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010012
Owen Anderson825b72b2009-08-11 20:47:22 +000010013 if (VT == MVT::i8)
10014 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010015 return Op;
10016}
10017
Craig Topper13894fa2011-08-24 06:14:18 +000010018// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10019// ones, and then concatenate the result back.
10020static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010021 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010022
10023 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
10024 "Unsupported value type for operation");
10025
10026 int NumElems = VT.getVectorNumElements();
10027 DebugLoc dl = Op.getDebugLoc();
10028 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10029 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10030
10031 // Extract the LHS vectors
10032 SDValue LHS = Op.getOperand(0);
10033 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10034 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10035
10036 // Extract the RHS vectors
10037 SDValue RHS = Op.getOperand(1);
10038 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
10039 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
10040
10041 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10042 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10043
10044 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10045 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10046 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10047}
10048
10049SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
10050 assert(Op.getValueType().getSizeInBits() == 256 &&
10051 Op.getValueType().isInteger() &&
10052 "Only handle AVX 256-bit vector integer operation");
10053 return Lower256IntArith(Op, DAG);
10054}
10055
10056SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
10057 assert(Op.getValueType().getSizeInBits() == 256 &&
10058 Op.getValueType().isInteger() &&
10059 "Only handle AVX 256-bit vector integer operation");
10060 return Lower256IntArith(Op, DAG);
10061}
10062
10063SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10064 EVT VT = Op.getValueType();
10065
10066 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +000010067 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010068 return Lower256IntArith(Op, DAG);
10069
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010070 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010071
Craig Topperaaa643c2011-11-09 07:28:55 +000010072 SDValue A = Op.getOperand(0);
10073 SDValue B = Op.getOperand(1);
10074
10075 if (VT == MVT::v4i64) {
10076 assert(Subtarget->hasAVX2() && "Lowering v4i64 multiply requires AVX2");
10077
10078 // ulong2 Ahi = __builtin_ia32_psrlqi256( a, 32);
10079 // ulong2 Bhi = __builtin_ia32_psrlqi256( b, 32);
10080 // ulong2 AloBlo = __builtin_ia32_pmuludq256( a, b );
10081 // ulong2 AloBhi = __builtin_ia32_pmuludq256( a, Bhi );
10082 // ulong2 AhiBlo = __builtin_ia32_pmuludq256( Ahi, b );
10083 //
10084 // AloBhi = __builtin_ia32_psllqi256( AloBhi, 32 );
10085 // AhiBlo = __builtin_ia32_psllqi256( AhiBlo, 32 );
10086 // return AloBlo + AloBhi + AhiBlo;
10087
10088 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10089 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10090 A, DAG.getConstant(32, MVT::i32));
10091 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10092 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10093 B, DAG.getConstant(32, MVT::i32));
10094 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10095 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10096 A, B);
10097 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10098 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10099 A, Bhi);
10100 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10101 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10102 Ahi, B);
10103 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10104 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10105 AloBhi, DAG.getConstant(32, MVT::i32));
10106 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10107 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10108 AhiBlo, DAG.getConstant(32, MVT::i32));
10109 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10110 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
10111 return Res;
10112 }
10113
10114 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
10115
Mon P Wangaf9b9522008-12-18 21:42:19 +000010116 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
10117 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
10118 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
10119 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
10120 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
10121 //
10122 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
10123 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
10124 // return AloBlo + AloBhi + AhiBlo;
10125
Dale Johannesene4d209d2009-02-03 20:21:25 +000010126 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010127 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10128 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010129 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010130 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10131 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010132 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010133 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010134 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010135 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010136 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010137 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010138 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010139 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010140 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010141 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010142 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10143 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010144 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010145 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10146 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010147 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10148 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010149 return Res;
10150}
10151
Nadav Rotem43012222011-05-11 08:12:09 +000010152SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10153
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010154 EVT VT = Op.getValueType();
10155 DebugLoc dl = Op.getDebugLoc();
10156 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010157 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010158 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010159
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010160 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010161 return SDValue();
10162
Nadav Rotem43012222011-05-11 08:12:09 +000010163 // Optimize shl/srl/sra with constant shift amount.
10164 if (isSplatVector(Amt.getNode())) {
10165 SDValue SclrAmt = Amt->getOperand(0);
10166 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10167 uint64_t ShiftAmt = C->getZExtValue();
10168
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010169 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SHL) {
10170 // Make a large shift.
10171 SDValue SHL =
10172 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10173 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10174 R, DAG.getConstant(ShiftAmt, MVT::i32));
10175 // Zero out the rightmost bits.
10176 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10177 MVT::i8));
10178 return DAG.getNode(ISD::AND, dl, VT, SHL,
10179 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10180 }
10181
Nadav Rotem43012222011-05-11 08:12:09 +000010182 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
10183 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10184 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10185 R, DAG.getConstant(ShiftAmt, MVT::i32));
10186
10187 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
10188 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10189 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10190 R, DAG.getConstant(ShiftAmt, MVT::i32));
10191
10192 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
10193 return 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
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010197 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRL) {
10198 // Make a large shift.
10199 SDValue SRL =
10200 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10201 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10202 R, DAG.getConstant(ShiftAmt, MVT::i32));
10203 // Zero out the leftmost bits.
10204 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10205 MVT::i8));
10206 return DAG.getNode(ISD::AND, dl, VT, SRL,
10207 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10208 }
10209
Nadav Rotem43012222011-05-11 08:12:09 +000010210 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
10211 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10212 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10213 R, DAG.getConstant(ShiftAmt, MVT::i32));
10214
10215 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
10216 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10217 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
10218 R, DAG.getConstant(ShiftAmt, MVT::i32));
10219
10220 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
10221 return 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
10225 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
10226 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10227 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
10228 R, DAG.getConstant(ShiftAmt, MVT::i32));
10229
10230 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
10231 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10232 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
10233 R, DAG.getConstant(ShiftAmt, MVT::i32));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010234
10235 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRA) {
10236 if (ShiftAmt == 7) {
10237 // R s>> 7 === R s< 0
10238 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10239 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10240 }
10241
10242 // R s>> a === ((R u>> a) ^ m) - m
10243 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10244 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10245 MVT::i8));
10246 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10247 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10248 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10249 return Res;
10250 }
Craig Topper46154eb2011-11-11 07:39:23 +000010251
Craig Topper0d86d462011-11-20 00:12:05 +000010252 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10253 if (Op.getOpcode() == ISD::SHL) {
10254 // Make a large shift.
10255 SDValue SHL =
10256 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10257 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
10258 R, DAG.getConstant(ShiftAmt, MVT::i32));
10259 // Zero out the rightmost bits.
10260 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10261 MVT::i8));
10262 return DAG.getNode(ISD::AND, dl, VT, SHL,
10263 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010264 }
Craig Topper0d86d462011-11-20 00:12:05 +000010265 if (Op.getOpcode() == ISD::SRL) {
10266 // Make a large shift.
10267 SDValue SRL =
10268 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10269 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
10270 R, DAG.getConstant(ShiftAmt, MVT::i32));
10271 // Zero out the leftmost bits.
10272 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10273 MVT::i8));
10274 return DAG.getNode(ISD::AND, dl, VT, SRL,
10275 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10276 }
10277 if (Op.getOpcode() == ISD::SRA) {
10278 if (ShiftAmt == 7) {
10279 // R s>> 7 === R s< 0
10280 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10281 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10282 }
10283
10284 // R s>> a === ((R u>> a) ^ m) - m
10285 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10286 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10287 MVT::i8));
10288 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10289 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10290 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10291 return Res;
10292 }
10293 }
Nadav Rotem43012222011-05-11 08:12:09 +000010294 }
10295 }
10296
10297 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010298 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010299 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10300 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10301 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
10302
10303 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010304
Nate Begeman51409212010-07-28 00:21:48 +000010305 std::vector<Constant*> CV(4, CI);
10306 Constant *C = ConstantVector::get(CV);
10307 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10308 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010309 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010310 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010311
10312 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010313 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010314 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10315 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10316 }
Nadav Rotem43012222011-05-11 08:12:09 +000010317 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010318 // a = a << 5;
10319 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10320 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10321 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
10322
10323 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
10324 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
10325
10326 std::vector<Constant*> CVM1(16, CM1);
10327 std::vector<Constant*> CVM2(16, CM2);
10328 Constant *C = ConstantVector::get(CVM1);
10329 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10330 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010331 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010332 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010333
10334 // r = pblendv(r, psllw(r & (char16)15, 4), a);
10335 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10336 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10337 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10338 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010339 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010340 // a += a
10341 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010342
Nate Begeman51409212010-07-28 00:21:48 +000010343 C = ConstantVector::get(CVM2);
10344 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10345 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010346 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010347 false, false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010348
Nate Begeman51409212010-07-28 00:21:48 +000010349 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10350 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10351 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10352 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10353 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010354 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010355 // a += a
10356 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010357
Nate Begeman51409212010-07-28 00:21:48 +000010358 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010359 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10360 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010361 return R;
10362 }
Craig Topper46154eb2011-11-11 07:39:23 +000010363
10364 // Decompose 256-bit shifts into smaller 128-bit shifts.
10365 if (VT.getSizeInBits() == 256) {
10366 int NumElems = VT.getVectorNumElements();
10367 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10368 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10369
10370 // Extract the two vectors
10371 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10372 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10373 DAG, dl);
10374
10375 // Recreate the shift amount vectors
10376 SDValue Amt1, Amt2;
10377 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10378 // Constant shift amount
10379 SmallVector<SDValue, 4> Amt1Csts;
10380 SmallVector<SDValue, 4> Amt2Csts;
10381 for (int i = 0; i < NumElems/2; ++i)
10382 Amt1Csts.push_back(Amt->getOperand(i));
10383 for (int i = NumElems/2; i < NumElems; ++i)
10384 Amt2Csts.push_back(Amt->getOperand(i));
10385
10386 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10387 &Amt1Csts[0], NumElems/2);
10388 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10389 &Amt2Csts[0], NumElems/2);
10390 } else {
10391 // Variable shift amount
10392 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10393 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10394 DAG, dl);
10395 }
10396
10397 // Issue new vector shifts for the smaller types
10398 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10399 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10400
10401 // Concatenate the result back
10402 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10403 }
10404
Nate Begeman51409212010-07-28 00:21:48 +000010405 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010406}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010407
Dan Gohmand858e902010-04-17 15:26:15 +000010408SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010409 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10410 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010411 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10412 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010413 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010414 SDValue LHS = N->getOperand(0);
10415 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010416 unsigned BaseOp = 0;
10417 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010418 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010419 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010420 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010421 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010422 // A subtract of one will be selected as a INC. Note that INC doesn't
10423 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010424 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10425 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010426 BaseOp = X86ISD::INC;
10427 Cond = X86::COND_O;
10428 break;
10429 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010430 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010431 Cond = X86::COND_O;
10432 break;
10433 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010434 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010435 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010436 break;
10437 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010438 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10439 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010440 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10441 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010442 BaseOp = X86ISD::DEC;
10443 Cond = X86::COND_O;
10444 break;
10445 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010446 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010447 Cond = X86::COND_O;
10448 break;
10449 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010450 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010451 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010452 break;
10453 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010454 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010455 Cond = X86::COND_O;
10456 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010457 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10458 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10459 MVT::i32);
10460 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010461
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010462 SDValue SetCC =
10463 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10464 DAG.getConstant(X86::COND_O, MVT::i32),
10465 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010466
Dan Gohman6e5fda22011-07-22 18:45:15 +000010467 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010468 }
Bill Wendling74c37652008-12-09 22:08:41 +000010469 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010470
Bill Wendling61edeb52008-12-02 01:06:39 +000010471 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010472 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010473 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010474
Bill Wendling61edeb52008-12-02 01:06:39 +000010475 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010476 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10477 DAG.getConstant(Cond, MVT::i32),
10478 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010479
Dan Gohman6e5fda22011-07-22 18:45:15 +000010480 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010481}
10482
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010483SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10484 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010485 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10486 EVT VT = Op.getValueType();
10487
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010488 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010489 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10490 ExtraVT.getScalarType().getSizeInBits();
10491 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10492
10493 unsigned SHLIntrinsicsID = 0;
10494 unsigned SRAIntrinsicsID = 0;
10495 switch (VT.getSimpleVT().SimpleTy) {
10496 default:
10497 return SDValue();
Craig Toppera124f942011-11-21 01:12:36 +000010498 case MVT::v4i32:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010499 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10500 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10501 break;
Craig Toppera124f942011-11-21 01:12:36 +000010502 case MVT::v8i16:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010503 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10504 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10505 break;
Craig Toppera124f942011-11-21 01:12:36 +000010506 case MVT::v8i32:
10507 case MVT::v16i16:
10508 if (!Subtarget->hasAVX())
10509 return SDValue();
10510 if (!Subtarget->hasAVX2()) {
10511 // needs to be split
10512 int NumElems = VT.getVectorNumElements();
10513 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10514 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10515
10516 // Extract the LHS vectors
10517 SDValue LHS = Op.getOperand(0);
10518 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10519 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10520
10521 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10522 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10523
10524 EVT ExtraEltVT = ExtraVT.getVectorElementType();
10525 int ExtraNumElems = ExtraVT.getVectorNumElements();
10526 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10527 ExtraNumElems/2);
10528 SDValue Extra = DAG.getValueType(ExtraVT);
10529
10530 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10531 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
10532
10533 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10534 }
10535 if (VT == MVT::v8i32) {
10536 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_d;
10537 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_d;
10538 } else {
10539 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_w;
10540 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_w;
10541 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010542 }
10543
10544 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10545 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
Craig Toppera124f942011-11-21 01:12:36 +000010546 Op.getOperand(0), ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010547
Nadav Rotema7934dd2011-10-10 19:31:45 +000010548 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10549 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10550 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010551 }
10552
10553 return SDValue();
10554}
10555
10556
Eric Christopher9a9d2752010-07-22 02:48:34 +000010557SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10558 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010559
Eric Christopher77ed1352011-07-08 00:04:56 +000010560 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10561 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010562 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010563 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010564 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010565 SDValue Ops[] = {
10566 DAG.getRegister(X86::ESP, MVT::i32), // Base
10567 DAG.getTargetConstant(1, MVT::i8), // Scale
10568 DAG.getRegister(0, MVT::i32), // Index
10569 DAG.getTargetConstant(0, MVT::i32), // Disp
10570 DAG.getRegister(0, MVT::i32), // Segment.
10571 Zero,
10572 Chain
10573 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010574 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010575 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10576 array_lengthof(Ops));
10577 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010578 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010579
Eric Christopher9a9d2752010-07-22 02:48:34 +000010580 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010581 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010582 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010583
Chris Lattner132929a2010-08-14 17:26:09 +000010584 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10585 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10586 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10587 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010588
Chris Lattner132929a2010-08-14 17:26:09 +000010589 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10590 if (!Op1 && !Op2 && !Op3 && Op4)
10591 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010592
Chris Lattner132929a2010-08-14 17:26:09 +000010593 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10594 if (Op1 && !Op2 && !Op3 && !Op4)
10595 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010596
10597 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010598 // (MFENCE)>;
10599 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010600}
10601
Eli Friedman14648462011-07-27 22:21:52 +000010602SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10603 SelectionDAG &DAG) const {
10604 DebugLoc dl = Op.getDebugLoc();
10605 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10606 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10607 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10608 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10609
10610 // The only fence that needs an instruction is a sequentially-consistent
10611 // cross-thread fence.
10612 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10613 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10614 // no-sse2). There isn't any reason to disable it if the target processor
10615 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010616 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010617 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10618
10619 SDValue Chain = Op.getOperand(0);
10620 SDValue Zero = DAG.getConstant(0, MVT::i32);
10621 SDValue Ops[] = {
10622 DAG.getRegister(X86::ESP, MVT::i32), // Base
10623 DAG.getTargetConstant(1, MVT::i8), // Scale
10624 DAG.getRegister(0, MVT::i32), // Index
10625 DAG.getTargetConstant(0, MVT::i32), // Disp
10626 DAG.getRegister(0, MVT::i32), // Segment.
10627 Zero,
10628 Chain
10629 };
10630 SDNode *Res =
10631 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10632 array_lengthof(Ops));
10633 return SDValue(Res, 0);
10634 }
10635
10636 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10637 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10638}
10639
10640
Dan Gohmand858e902010-04-17 15:26:15 +000010641SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010642 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010643 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010644 unsigned Reg = 0;
10645 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010646 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010647 default:
10648 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010649 case MVT::i8: Reg = X86::AL; size = 1; break;
10650 case MVT::i16: Reg = X86::AX; size = 2; break;
10651 case MVT::i32: Reg = X86::EAX; size = 4; break;
10652 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010653 assert(Subtarget->is64Bit() && "Node not type legal!");
10654 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010655 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010656 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010657 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010658 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010659 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010660 Op.getOperand(1),
10661 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010662 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010663 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010664 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010665 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10666 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10667 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010668 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010669 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010670 return cpOut;
10671}
10672
Duncan Sands1607f052008-12-01 11:39:25 +000010673SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010674 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010675 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010676 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010677 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010678 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010679 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010680 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10681 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010682 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010683 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10684 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010685 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010686 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010687 rdx.getValue(1)
10688 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010689 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010690}
10691
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010692SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010693 SelectionDAG &DAG) const {
10694 EVT SrcVT = Op.getOperand(0).getValueType();
10695 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010696 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010697 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010698 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010699 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010700 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010701 // i64 <=> MMX conversions are Legal.
10702 if (SrcVT==MVT::i64 && DstVT.isVector())
10703 return Op;
10704 if (DstVT==MVT::i64 && SrcVT.isVector())
10705 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010706 // MMX <=> MMX conversions are Legal.
10707 if (SrcVT.isVector() && DstVT.isVector())
10708 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010709 // All other conversions need to be expanded.
10710 return SDValue();
10711}
Chris Lattner5b856542010-12-20 00:59:46 +000010712
Dan Gohmand858e902010-04-17 15:26:15 +000010713SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010714 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010715 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010716 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010717 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010718 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010719 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010720 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010721 Node->getOperand(0),
10722 Node->getOperand(1), negOp,
10723 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010724 cast<AtomicSDNode>(Node)->getAlignment(),
10725 cast<AtomicSDNode>(Node)->getOrdering(),
10726 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010727}
10728
Eli Friedman327236c2011-08-24 20:50:09 +000010729static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10730 SDNode *Node = Op.getNode();
10731 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010732 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010733
10734 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010735 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10736 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10737 // (The only way to get a 16-byte store is cmpxchg16b)
10738 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10739 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10740 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010741 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10742 cast<AtomicSDNode>(Node)->getMemoryVT(),
10743 Node->getOperand(0),
10744 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010745 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010746 cast<AtomicSDNode>(Node)->getOrdering(),
10747 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010748 return Swap.getValue(1);
10749 }
10750 // Other atomic stores have a simple pattern.
10751 return Op;
10752}
10753
Chris Lattner5b856542010-12-20 00:59:46 +000010754static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10755 EVT VT = Op.getNode()->getValueType(0);
10756
10757 // Let legalize expand this if it isn't a legal type yet.
10758 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10759 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010760
Chris Lattner5b856542010-12-20 00:59:46 +000010761 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010762
Chris Lattner5b856542010-12-20 00:59:46 +000010763 unsigned Opc;
10764 bool ExtraOp = false;
10765 switch (Op.getOpcode()) {
10766 default: assert(0 && "Invalid code");
10767 case ISD::ADDC: Opc = X86ISD::ADD; break;
10768 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10769 case ISD::SUBC: Opc = X86ISD::SUB; break;
10770 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10771 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010772
Chris Lattner5b856542010-12-20 00:59:46 +000010773 if (!ExtraOp)
10774 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10775 Op.getOperand(1));
10776 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10777 Op.getOperand(1), Op.getOperand(2));
10778}
10779
Evan Cheng0db9fe62006-04-25 20:13:52 +000010780/// LowerOperation - Provide custom lowering hooks for some operations.
10781///
Dan Gohmand858e902010-04-17 15:26:15 +000010782SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010783 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010784 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010785 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010786 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010787 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010788 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10789 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010790 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010791 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010792 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010793 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10794 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10795 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010796 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010797 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010798 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10799 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10800 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010801 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010802 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010803 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010804 case ISD::SHL_PARTS:
10805 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010806 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010807 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010808 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010809 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010810 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010811 case ISD::FABS: return LowerFABS(Op, DAG);
10812 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010813 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010814 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010815 case ISD::SETCC: return LowerSETCC(Op, DAG);
10816 case ISD::SELECT: return LowerSELECT(Op, DAG);
10817 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010818 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010819 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010820 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010821 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010822 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010823 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10824 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010825 case ISD::FRAME_TO_ARGS_OFFSET:
10826 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010827 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010828 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010829 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10830 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010831 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010832 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10833 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010834 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010835 case ISD::SRA:
10836 case ISD::SRL:
10837 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010838 case ISD::SADDO:
10839 case ISD::UADDO:
10840 case ISD::SSUBO:
10841 case ISD::USUBO:
10842 case ISD::SMULO:
10843 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010844 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010845 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010846 case ISD::ADDC:
10847 case ISD::ADDE:
10848 case ISD::SUBC:
10849 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010850 case ISD::ADD: return LowerADD(Op, DAG);
10851 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010852 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010853}
10854
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010855static void ReplaceATOMIC_LOAD(SDNode *Node,
10856 SmallVectorImpl<SDValue> &Results,
10857 SelectionDAG &DAG) {
10858 DebugLoc dl = Node->getDebugLoc();
10859 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10860
10861 // Convert wide load -> cmpxchg8b/cmpxchg16b
10862 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10863 // (The only way to get a 16-byte load is cmpxchg16b)
10864 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010865 SDValue Zero = DAG.getConstant(0, VT);
10866 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010867 Node->getOperand(0),
10868 Node->getOperand(1), Zero, Zero,
10869 cast<AtomicSDNode>(Node)->getMemOperand(),
10870 cast<AtomicSDNode>(Node)->getOrdering(),
10871 cast<AtomicSDNode>(Node)->getSynchScope());
10872 Results.push_back(Swap.getValue(0));
10873 Results.push_back(Swap.getValue(1));
10874}
10875
Duncan Sands1607f052008-12-01 11:39:25 +000010876void X86TargetLowering::
10877ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010878 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010879 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000010880 assert (Node->getValueType(0) == MVT::i64 &&
10881 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010882
10883 SDValue Chain = Node->getOperand(0);
10884 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010885 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010886 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010887 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010888 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010889 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010890 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010891 SDValue Result =
10892 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10893 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010894 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010895 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010896 Results.push_back(Result.getValue(2));
10897}
10898
Duncan Sands126d9072008-07-04 11:47:58 +000010899/// ReplaceNodeResults - Replace a node with an illegal result type
10900/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010901void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10902 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010903 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010904 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010905 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010906 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010907 assert(false && "Do not know how to custom type legalize this operation!");
10908 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010909 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010910 case ISD::ADDC:
10911 case ISD::ADDE:
10912 case ISD::SUBC:
10913 case ISD::SUBE:
10914 // We don't want to expand or promote these.
10915 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010916 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010917 std::pair<SDValue,SDValue> Vals =
10918 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010919 SDValue FIST = Vals.first, StackSlot = Vals.second;
10920 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010921 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010922 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010923 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010924 MachinePointerInfo(),
10925 false, false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010926 }
10927 return;
10928 }
10929 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010930 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010931 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010932 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010933 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010934 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010935 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010936 eax.getValue(2));
10937 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10938 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010939 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010940 Results.push_back(edx.getValue(1));
10941 return;
10942 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010943 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010944 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010945 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010946 bool Regs64bit = T == MVT::i128;
10947 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010948 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010949 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10950 DAG.getConstant(0, HalfT));
10951 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10952 DAG.getConstant(1, HalfT));
10953 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10954 Regs64bit ? X86::RAX : X86::EAX,
10955 cpInL, SDValue());
10956 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10957 Regs64bit ? X86::RDX : X86::EDX,
10958 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010959 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010960 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10961 DAG.getConstant(0, HalfT));
10962 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10963 DAG.getConstant(1, HalfT));
10964 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10965 Regs64bit ? X86::RBX : X86::EBX,
10966 swapInL, cpInH.getValue(1));
10967 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10968 Regs64bit ? X86::RCX : X86::ECX,
10969 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010970 SDValue Ops[] = { swapInH.getValue(0),
10971 N->getOperand(1),
10972 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010973 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010974 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010975 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10976 X86ISD::LCMPXCHG8_DAG;
10977 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010978 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010979 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10980 Regs64bit ? X86::RAX : X86::EAX,
10981 HalfT, Result.getValue(1));
10982 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10983 Regs64bit ? X86::RDX : X86::EDX,
10984 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010985 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010986 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010987 Results.push_back(cpOutH.getValue(1));
10988 return;
10989 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010990 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010991 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10992 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010993 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010994 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10995 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010996 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010997 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10998 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010999 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000011000 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
11001 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011002 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000011003 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
11004 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011005 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000011006 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
11007 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011008 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000011009 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
11010 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011011 case ISD::ATOMIC_LOAD:
11012 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011013 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011014}
11015
Evan Cheng72261582005-12-20 06:22:03 +000011016const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11017 switch (Opcode) {
11018 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011019 case X86ISD::BSF: return "X86ISD::BSF";
11020 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011021 case X86ISD::SHLD: return "X86ISD::SHLD";
11022 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011023 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011024 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011025 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011026 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011027 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011028 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011029 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11030 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11031 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011032 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011033 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011034 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011035 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011036 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011037 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011038 case X86ISD::COMI: return "X86ISD::COMI";
11039 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011040 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011041 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011042 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11043 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011044 case X86ISD::CMOV: return "X86ISD::CMOV";
11045 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011046 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011047 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11048 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011049 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011050 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011051 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011052 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011053 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011054 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11055 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011056 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011057 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011058 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011059 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011060 case X86ISD::BLENDV: return "X86ISD::BLENDV";
11061 case X86ISD::FHADD: return "X86ISD::FHADD";
11062 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011063 case X86ISD::FMAX: return "X86ISD::FMAX";
11064 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000011065 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11066 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011067 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011068 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011069 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011070 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011071 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011072 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11073 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011074 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11075 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11076 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11077 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11078 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11079 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011080 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
11081 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000011082 case X86ISD::VSHL: return "X86ISD::VSHL";
11083 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000011084 case X86ISD::CMPPD: return "X86ISD::CMPPD";
11085 case X86ISD::CMPPS: return "X86ISD::CMPPS";
11086 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
11087 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
11088 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
11089 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
11090 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
11091 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
11092 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
11093 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011094 case X86ISD::ADD: return "X86ISD::ADD";
11095 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011096 case X86ISD::ADC: return "X86ISD::ADC";
11097 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011098 case X86ISD::SMUL: return "X86ISD::SMUL";
11099 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011100 case X86ISD::INC: return "X86ISD::INC";
11101 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011102 case X86ISD::OR: return "X86ISD::OR";
11103 case X86ISD::XOR: return "X86ISD::XOR";
11104 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011105 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011106 case X86ISD::BLSI: return "X86ISD::BLSI";
11107 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11108 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011109 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011110 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011111 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011112 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11113 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11114 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
11115 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
11116 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
11117 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
11118 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
11119 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
11120 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011121 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011122 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011123 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011124 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11125 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011126 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11127 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11128 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
11129 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
11130 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
11131 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11132 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper06cb6802011-11-26 20:47:44 +000011133 case X86ISD::UNPCKLP: return "X86ISD::UNPCKLP";
11134 case X86ISD::UNPCKHP: return "X86ISD::UNPCKHP";
11135 case X86ISD::PUNPCKL: return "X86ISD::PUNPCKL";
11136 case X86ISD::PUNPCKH: return "X86ISD::PUNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011137 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011138 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011139 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011140 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011141 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011142 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011143 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011144 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000011145 }
11146}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011147
Chris Lattnerc9addb72007-03-30 23:15:24 +000011148// isLegalAddressingMode - Return true if the addressing mode represented
11149// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011150bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011151 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011152 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011153 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011154 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011155
Chris Lattnerc9addb72007-03-30 23:15:24 +000011156 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011157 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011158 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011159
Chris Lattnerc9addb72007-03-30 23:15:24 +000011160 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011161 unsigned GVFlags =
11162 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011163
Chris Lattnerdfed4132009-07-10 07:38:24 +000011164 // If a reference to this global requires an extra load, we can't fold it.
11165 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011166 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011167
Chris Lattnerdfed4132009-07-10 07:38:24 +000011168 // If BaseGV requires a register for the PIC base, we cannot also have a
11169 // BaseReg specified.
11170 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011171 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011172
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011173 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011174 if ((M != CodeModel::Small || R != Reloc::Static) &&
11175 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011176 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011177 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011178
Chris Lattnerc9addb72007-03-30 23:15:24 +000011179 switch (AM.Scale) {
11180 case 0:
11181 case 1:
11182 case 2:
11183 case 4:
11184 case 8:
11185 // These scales always work.
11186 break;
11187 case 3:
11188 case 5:
11189 case 9:
11190 // These scales are formed with basereg+scalereg. Only accept if there is
11191 // no basereg yet.
11192 if (AM.HasBaseReg)
11193 return false;
11194 break;
11195 default: // Other stuff never works.
11196 return false;
11197 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011198
Chris Lattnerc9addb72007-03-30 23:15:24 +000011199 return true;
11200}
11201
11202
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011203bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011204 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011205 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011206 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11207 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011208 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011209 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011210 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011211}
11212
Owen Andersone50ed302009-08-10 22:56:29 +000011213bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011214 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011215 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011216 unsigned NumBits1 = VT1.getSizeInBits();
11217 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011218 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011219 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011220 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011221}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011222
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011223bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011224 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011225 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011226}
11227
Owen Andersone50ed302009-08-10 22:56:29 +000011228bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011229 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011230 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011231}
11232
Owen Andersone50ed302009-08-10 22:56:29 +000011233bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011234 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011235 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011236}
11237
Evan Cheng60c07e12006-07-05 22:17:51 +000011238/// isShuffleMaskLegal - Targets can use this to indicate that they only
11239/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11240/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11241/// are assumed to be legal.
11242bool
Eric Christopherfd179292009-08-27 18:07:15 +000011243X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011244 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011245 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011246 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011247 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011248
Nate Begemana09008b2009-10-19 02:17:23 +000011249 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011250 return (VT.getVectorNumElements() == 2 ||
11251 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11252 isMOVLMask(M, VT) ||
11253 isSHUFPMask(M, VT) ||
11254 isPSHUFDMask(M, VT) ||
11255 isPSHUFHWMask(M, VT) ||
11256 isPSHUFLWMask(M, VT) ||
Craig Topperc0d82852011-11-22 00:44:41 +000011257 isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX()) ||
Craig Topper6347e862011-11-21 06:57:39 +000011258 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11259 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011260 isUNPCKL_v_undef_Mask(M, VT) ||
11261 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011262}
11263
Dan Gohman7d8143f2008-04-09 20:09:42 +000011264bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011265X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011266 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011267 unsigned NumElts = VT.getVectorNumElements();
11268 // FIXME: This collection of masks seems suspect.
11269 if (NumElts == 2)
11270 return true;
11271 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11272 return (isMOVLMask(Mask, VT) ||
11273 isCommutedMOVLMask(Mask, VT, true) ||
11274 isSHUFPMask(Mask, VT) ||
11275 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011276 }
11277 return false;
11278}
11279
11280//===----------------------------------------------------------------------===//
11281// X86 Scheduler Hooks
11282//===----------------------------------------------------------------------===//
11283
Mon P Wang63307c32008-05-05 19:05:59 +000011284// private utility function
11285MachineBasicBlock *
11286X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11287 MachineBasicBlock *MBB,
11288 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011289 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011290 unsigned LoadOpc,
11291 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011292 unsigned notOpc,
11293 unsigned EAXreg,
11294 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011295 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011296 // For the atomic bitwise operator, we generate
11297 // thisMBB:
11298 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011299 // ld t1 = [bitinstr.addr]
11300 // op t2 = t1, [bitinstr.val]
11301 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011302 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11303 // bz newMBB
11304 // fallthrough -->nextMBB
11305 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11306 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011307 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011308 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011309
Mon P Wang63307c32008-05-05 19:05:59 +000011310 /// First build the CFG
11311 MachineFunction *F = MBB->getParent();
11312 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011313 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11314 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11315 F->insert(MBBIter, newMBB);
11316 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011317
Dan Gohman14152b42010-07-06 20:24:04 +000011318 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11319 nextMBB->splice(nextMBB->begin(), thisMBB,
11320 llvm::next(MachineBasicBlock::iterator(bInstr)),
11321 thisMBB->end());
11322 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011323
Mon P Wang63307c32008-05-05 19:05:59 +000011324 // Update thisMBB to fall through to newMBB
11325 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011326
Mon P Wang63307c32008-05-05 19:05:59 +000011327 // newMBB jumps to itself and fall through to nextMBB
11328 newMBB->addSuccessor(nextMBB);
11329 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011330
Mon P Wang63307c32008-05-05 19:05:59 +000011331 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011332 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011333 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011334 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011335 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011336 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011337 int numArgs = bInstr->getNumOperands() - 1;
11338 for (int i=0; i < numArgs; ++i)
11339 argOpers[i] = &bInstr->getOperand(i+1);
11340
11341 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011342 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011343 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011344
Dale Johannesen140be2d2008-08-19 18:47:28 +000011345 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011346 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011347 for (int i=0; i <= lastAddrIndx; ++i)
11348 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011349
Dale Johannesen140be2d2008-08-19 18:47:28 +000011350 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011351 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011352 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011353 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011354 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011355 tt = t1;
11356
Dale Johannesen140be2d2008-08-19 18:47:28 +000011357 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011358 assert((argOpers[valArgIndx]->isReg() ||
11359 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011360 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011361 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011362 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011363 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011364 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011365 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011366 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011367
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011368 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011369 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011370
Dale Johannesene4d209d2009-02-03 20:21:25 +000011371 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011372 for (int i=0; i <= lastAddrIndx; ++i)
11373 (*MIB).addOperand(*argOpers[i]);
11374 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011375 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011376 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11377 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011378
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011379 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011380 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011381
Mon P Wang63307c32008-05-05 19:05:59 +000011382 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011383 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011384
Dan Gohman14152b42010-07-06 20:24:04 +000011385 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011386 return nextMBB;
11387}
11388
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011389// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011390MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011391X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11392 MachineBasicBlock *MBB,
11393 unsigned regOpcL,
11394 unsigned regOpcH,
11395 unsigned immOpcL,
11396 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011397 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011398 // For the atomic bitwise operator, we generate
11399 // thisMBB (instructions are in pairs, except cmpxchg8b)
11400 // ld t1,t2 = [bitinstr.addr]
11401 // newMBB:
11402 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11403 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011404 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011405 // mov ECX, EBX <- t5, t6
11406 // mov EAX, EDX <- t1, t2
11407 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11408 // mov t3, t4 <- EAX, EDX
11409 // bz newMBB
11410 // result in out1, out2
11411 // fallthrough -->nextMBB
11412
11413 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11414 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011415 const unsigned NotOpc = X86::NOT32r;
11416 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11417 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11418 MachineFunction::iterator MBBIter = MBB;
11419 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011420
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011421 /// First build the CFG
11422 MachineFunction *F = MBB->getParent();
11423 MachineBasicBlock *thisMBB = MBB;
11424 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11425 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11426 F->insert(MBBIter, newMBB);
11427 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011428
Dan Gohman14152b42010-07-06 20:24:04 +000011429 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11430 nextMBB->splice(nextMBB->begin(), thisMBB,
11431 llvm::next(MachineBasicBlock::iterator(bInstr)),
11432 thisMBB->end());
11433 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011434
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011435 // Update thisMBB to fall through to newMBB
11436 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011437
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011438 // newMBB jumps to itself and fall through to nextMBB
11439 newMBB->addSuccessor(nextMBB);
11440 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011441
Dale Johannesene4d209d2009-02-03 20:21:25 +000011442 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011443 // Insert instructions into newMBB based on incoming instruction
11444 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011445 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011446 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011447 MachineOperand& dest1Oper = bInstr->getOperand(0);
11448 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011449 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11450 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011451 argOpers[i] = &bInstr->getOperand(i+2);
11452
Dan Gohman71ea4e52010-05-14 21:01:44 +000011453 // We use some of the operands multiple times, so conservatively just
11454 // clear any kill flags that might be present.
11455 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11456 argOpers[i]->setIsKill(false);
11457 }
11458
Evan Chengad5b52f2010-01-08 19:14:57 +000011459 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011460 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011461
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011462 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011463 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011464 for (int i=0; i <= lastAddrIndx; ++i)
11465 (*MIB).addOperand(*argOpers[i]);
11466 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011467 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011468 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011469 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011470 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011471 MachineOperand newOp3 = *(argOpers[3]);
11472 if (newOp3.isImm())
11473 newOp3.setImm(newOp3.getImm()+4);
11474 else
11475 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011476 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011477 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011478
11479 // t3/4 are defined later, at the bottom of the loop
11480 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11481 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011482 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011483 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011484 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011485 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11486
Evan Cheng306b4ca2010-01-08 23:41:50 +000011487 // The subsequent operations should be using the destination registers of
11488 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011489 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011490 t1 = F->getRegInfo().createVirtualRegister(RC);
11491 t2 = F->getRegInfo().createVirtualRegister(RC);
11492 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11493 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011494 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011495 t1 = dest1Oper.getReg();
11496 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011497 }
11498
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011499 int valArgIndx = lastAddrIndx + 1;
11500 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011501 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011502 "invalid operand");
11503 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11504 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011505 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011506 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011507 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011508 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011509 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011510 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011511 (*MIB).addOperand(*argOpers[valArgIndx]);
11512 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011513 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011514 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011515 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011516 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011517 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011518 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011519 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011520 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011521 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011522 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011523
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011524 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011525 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011526 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011527 MIB.addReg(t2);
11528
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011529 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011530 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011531 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011532 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011533
Dale Johannesene4d209d2009-02-03 20:21:25 +000011534 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011535 for (int i=0; i <= lastAddrIndx; ++i)
11536 (*MIB).addOperand(*argOpers[i]);
11537
11538 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011539 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11540 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011541
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011542 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011543 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011544 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011545 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011546
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011547 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011548 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011549
Dan Gohman14152b42010-07-06 20:24:04 +000011550 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011551 return nextMBB;
11552}
11553
11554// private utility function
11555MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011556X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11557 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011558 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011559 // For the atomic min/max operator, we generate
11560 // thisMBB:
11561 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011562 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011563 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011564 // cmp t1, t2
11565 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011566 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011567 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11568 // bz newMBB
11569 // fallthrough -->nextMBB
11570 //
11571 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11572 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011573 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011574 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011575
Mon P Wang63307c32008-05-05 19:05:59 +000011576 /// First build the CFG
11577 MachineFunction *F = MBB->getParent();
11578 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011579 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11580 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11581 F->insert(MBBIter, newMBB);
11582 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011583
Dan Gohman14152b42010-07-06 20:24:04 +000011584 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11585 nextMBB->splice(nextMBB->begin(), thisMBB,
11586 llvm::next(MachineBasicBlock::iterator(mInstr)),
11587 thisMBB->end());
11588 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011589
Mon P Wang63307c32008-05-05 19:05:59 +000011590 // Update thisMBB to fall through to newMBB
11591 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011592
Mon P Wang63307c32008-05-05 19:05:59 +000011593 // newMBB jumps to newMBB and fall through to nextMBB
11594 newMBB->addSuccessor(nextMBB);
11595 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011596
Dale Johannesene4d209d2009-02-03 20:21:25 +000011597 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011598 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011599 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011600 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011601 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011602 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011603 int numArgs = mInstr->getNumOperands() - 1;
11604 for (int i=0; i < numArgs; ++i)
11605 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011606
Mon P Wang63307c32008-05-05 19:05:59 +000011607 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011608 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011609 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011610
Mon P Wangab3e7472008-05-05 22:56:23 +000011611 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011612 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011613 for (int i=0; i <= lastAddrIndx; ++i)
11614 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011615
Mon P Wang63307c32008-05-05 19:05:59 +000011616 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011617 assert((argOpers[valArgIndx]->isReg() ||
11618 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011619 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011620
11621 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011622 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011623 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011624 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011625 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011626 (*MIB).addOperand(*argOpers[valArgIndx]);
11627
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011628 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011629 MIB.addReg(t1);
11630
Dale Johannesene4d209d2009-02-03 20:21:25 +000011631 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011632 MIB.addReg(t1);
11633 MIB.addReg(t2);
11634
11635 // Generate movc
11636 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011637 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011638 MIB.addReg(t2);
11639 MIB.addReg(t1);
11640
11641 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011642 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011643 for (int i=0; i <= lastAddrIndx; ++i)
11644 (*MIB).addOperand(*argOpers[i]);
11645 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011646 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011647 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11648 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011649
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011650 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011651 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011652
Mon P Wang63307c32008-05-05 19:05:59 +000011653 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011654 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011655
Dan Gohman14152b42010-07-06 20:24:04 +000011656 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011657 return nextMBB;
11658}
11659
Eric Christopherf83a5de2009-08-27 18:08:16 +000011660// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011661// or XMM0_V32I8 in AVX all of this code can be replaced with that
11662// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011663MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011664X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011665 unsigned numArgs, bool memArg) const {
Craig Topperc0d82852011-11-22 00:44:41 +000011666 assert(Subtarget->hasSSE42orAVX() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011667 "Target must have SSE4.2 or AVX features enabled");
11668
Eric Christopherb120ab42009-08-18 22:50:32 +000011669 DebugLoc dl = MI->getDebugLoc();
11670 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011671 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011672 if (!Subtarget->hasAVX()) {
11673 if (memArg)
11674 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11675 else
11676 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11677 } else {
11678 if (memArg)
11679 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11680 else
11681 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11682 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011683
Eric Christopher41c902f2010-11-30 08:20:21 +000011684 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011685 for (unsigned i = 0; i < numArgs; ++i) {
11686 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011687 if (!(Op.isReg() && Op.isImplicit()))
11688 MIB.addOperand(Op);
11689 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011690 BuildMI(*BB, MI, dl,
11691 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11692 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011693 .addReg(X86::XMM0);
11694
Dan Gohman14152b42010-07-06 20:24:04 +000011695 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011696 return BB;
11697}
11698
11699MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011700X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011701 DebugLoc dl = MI->getDebugLoc();
11702 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011703
Eric Christopher228232b2010-11-30 07:20:12 +000011704 // Address into RAX/EAX, other two args into ECX, EDX.
11705 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11706 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11707 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11708 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011709 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011710
Eric Christopher228232b2010-11-30 07:20:12 +000011711 unsigned ValOps = X86::AddrNumOperands;
11712 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11713 .addReg(MI->getOperand(ValOps).getReg());
11714 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11715 .addReg(MI->getOperand(ValOps+1).getReg());
11716
11717 // The instruction doesn't actually take any operands though.
11718 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011719
Eric Christopher228232b2010-11-30 07:20:12 +000011720 MI->eraseFromParent(); // The pseudo is gone now.
11721 return BB;
11722}
11723
11724MachineBasicBlock *
11725X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011726 DebugLoc dl = MI->getDebugLoc();
11727 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011728
Eric Christopher228232b2010-11-30 07:20:12 +000011729 // First arg in ECX, the second in EAX.
11730 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11731 .addReg(MI->getOperand(0).getReg());
11732 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11733 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011734
Eric Christopher228232b2010-11-30 07:20:12 +000011735 // The instruction doesn't actually take any operands though.
11736 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011737
Eric Christopher228232b2010-11-30 07:20:12 +000011738 MI->eraseFromParent(); // The pseudo is gone now.
11739 return BB;
11740}
11741
11742MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011743X86TargetLowering::EmitVAARG64WithCustomInserter(
11744 MachineInstr *MI,
11745 MachineBasicBlock *MBB) const {
11746 // Emit va_arg instruction on X86-64.
11747
11748 // Operands to this pseudo-instruction:
11749 // 0 ) Output : destination address (reg)
11750 // 1-5) Input : va_list address (addr, i64mem)
11751 // 6 ) ArgSize : Size (in bytes) of vararg type
11752 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11753 // 8 ) Align : Alignment of type
11754 // 9 ) EFLAGS (implicit-def)
11755
11756 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11757 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11758
11759 unsigned DestReg = MI->getOperand(0).getReg();
11760 MachineOperand &Base = MI->getOperand(1);
11761 MachineOperand &Scale = MI->getOperand(2);
11762 MachineOperand &Index = MI->getOperand(3);
11763 MachineOperand &Disp = MI->getOperand(4);
11764 MachineOperand &Segment = MI->getOperand(5);
11765 unsigned ArgSize = MI->getOperand(6).getImm();
11766 unsigned ArgMode = MI->getOperand(7).getImm();
11767 unsigned Align = MI->getOperand(8).getImm();
11768
11769 // Memory Reference
11770 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11771 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11772 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11773
11774 // Machine Information
11775 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11776 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11777 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11778 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11779 DebugLoc DL = MI->getDebugLoc();
11780
11781 // struct va_list {
11782 // i32 gp_offset
11783 // i32 fp_offset
11784 // i64 overflow_area (address)
11785 // i64 reg_save_area (address)
11786 // }
11787 // sizeof(va_list) = 24
11788 // alignment(va_list) = 8
11789
11790 unsigned TotalNumIntRegs = 6;
11791 unsigned TotalNumXMMRegs = 8;
11792 bool UseGPOffset = (ArgMode == 1);
11793 bool UseFPOffset = (ArgMode == 2);
11794 unsigned MaxOffset = TotalNumIntRegs * 8 +
11795 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11796
11797 /* Align ArgSize to a multiple of 8 */
11798 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11799 bool NeedsAlign = (Align > 8);
11800
11801 MachineBasicBlock *thisMBB = MBB;
11802 MachineBasicBlock *overflowMBB;
11803 MachineBasicBlock *offsetMBB;
11804 MachineBasicBlock *endMBB;
11805
11806 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11807 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11808 unsigned OffsetReg = 0;
11809
11810 if (!UseGPOffset && !UseFPOffset) {
11811 // If we only pull from the overflow region, we don't create a branch.
11812 // We don't need to alter control flow.
11813 OffsetDestReg = 0; // unused
11814 OverflowDestReg = DestReg;
11815
11816 offsetMBB = NULL;
11817 overflowMBB = thisMBB;
11818 endMBB = thisMBB;
11819 } else {
11820 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11821 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11822 // If not, pull from overflow_area. (branch to overflowMBB)
11823 //
11824 // thisMBB
11825 // | .
11826 // | .
11827 // offsetMBB overflowMBB
11828 // | .
11829 // | .
11830 // endMBB
11831
11832 // Registers for the PHI in endMBB
11833 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11834 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11835
11836 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11837 MachineFunction *MF = MBB->getParent();
11838 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11839 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11840 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11841
11842 MachineFunction::iterator MBBIter = MBB;
11843 ++MBBIter;
11844
11845 // Insert the new basic blocks
11846 MF->insert(MBBIter, offsetMBB);
11847 MF->insert(MBBIter, overflowMBB);
11848 MF->insert(MBBIter, endMBB);
11849
11850 // Transfer the remainder of MBB and its successor edges to endMBB.
11851 endMBB->splice(endMBB->begin(), thisMBB,
11852 llvm::next(MachineBasicBlock::iterator(MI)),
11853 thisMBB->end());
11854 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11855
11856 // Make offsetMBB and overflowMBB successors of thisMBB
11857 thisMBB->addSuccessor(offsetMBB);
11858 thisMBB->addSuccessor(overflowMBB);
11859
11860 // endMBB is a successor of both offsetMBB and overflowMBB
11861 offsetMBB->addSuccessor(endMBB);
11862 overflowMBB->addSuccessor(endMBB);
11863
11864 // Load the offset value into a register
11865 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11866 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11867 .addOperand(Base)
11868 .addOperand(Scale)
11869 .addOperand(Index)
11870 .addDisp(Disp, UseFPOffset ? 4 : 0)
11871 .addOperand(Segment)
11872 .setMemRefs(MMOBegin, MMOEnd);
11873
11874 // Check if there is enough room left to pull this argument.
11875 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11876 .addReg(OffsetReg)
11877 .addImm(MaxOffset + 8 - ArgSizeA8);
11878
11879 // Branch to "overflowMBB" if offset >= max
11880 // Fall through to "offsetMBB" otherwise
11881 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11882 .addMBB(overflowMBB);
11883 }
11884
11885 // In offsetMBB, emit code to use the reg_save_area.
11886 if (offsetMBB) {
11887 assert(OffsetReg != 0);
11888
11889 // Read the reg_save_area address.
11890 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11891 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11892 .addOperand(Base)
11893 .addOperand(Scale)
11894 .addOperand(Index)
11895 .addDisp(Disp, 16)
11896 .addOperand(Segment)
11897 .setMemRefs(MMOBegin, MMOEnd);
11898
11899 // Zero-extend the offset
11900 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11901 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11902 .addImm(0)
11903 .addReg(OffsetReg)
11904 .addImm(X86::sub_32bit);
11905
11906 // Add the offset to the reg_save_area to get the final address.
11907 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11908 .addReg(OffsetReg64)
11909 .addReg(RegSaveReg);
11910
11911 // Compute the offset for the next argument
11912 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11913 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11914 .addReg(OffsetReg)
11915 .addImm(UseFPOffset ? 16 : 8);
11916
11917 // Store it back into the va_list.
11918 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11919 .addOperand(Base)
11920 .addOperand(Scale)
11921 .addOperand(Index)
11922 .addDisp(Disp, UseFPOffset ? 4 : 0)
11923 .addOperand(Segment)
11924 .addReg(NextOffsetReg)
11925 .setMemRefs(MMOBegin, MMOEnd);
11926
11927 // Jump to endMBB
11928 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11929 .addMBB(endMBB);
11930 }
11931
11932 //
11933 // Emit code to use overflow area
11934 //
11935
11936 // Load the overflow_area address into a register.
11937 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11938 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11939 .addOperand(Base)
11940 .addOperand(Scale)
11941 .addOperand(Index)
11942 .addDisp(Disp, 8)
11943 .addOperand(Segment)
11944 .setMemRefs(MMOBegin, MMOEnd);
11945
11946 // If we need to align it, do so. Otherwise, just copy the address
11947 // to OverflowDestReg.
11948 if (NeedsAlign) {
11949 // Align the overflow address
11950 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11951 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11952
11953 // aligned_addr = (addr + (align-1)) & ~(align-1)
11954 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11955 .addReg(OverflowAddrReg)
11956 .addImm(Align-1);
11957
11958 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11959 .addReg(TmpReg)
11960 .addImm(~(uint64_t)(Align-1));
11961 } else {
11962 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11963 .addReg(OverflowAddrReg);
11964 }
11965
11966 // Compute the next overflow address after this argument.
11967 // (the overflow address should be kept 8-byte aligned)
11968 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11969 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11970 .addReg(OverflowDestReg)
11971 .addImm(ArgSizeA8);
11972
11973 // Store the new overflow address.
11974 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11975 .addOperand(Base)
11976 .addOperand(Scale)
11977 .addOperand(Index)
11978 .addDisp(Disp, 8)
11979 .addOperand(Segment)
11980 .addReg(NextAddrReg)
11981 .setMemRefs(MMOBegin, MMOEnd);
11982
11983 // If we branched, emit the PHI to the front of endMBB.
11984 if (offsetMBB) {
11985 BuildMI(*endMBB, endMBB->begin(), DL,
11986 TII->get(X86::PHI), DestReg)
11987 .addReg(OffsetDestReg).addMBB(offsetMBB)
11988 .addReg(OverflowDestReg).addMBB(overflowMBB);
11989 }
11990
11991 // Erase the pseudo instruction
11992 MI->eraseFromParent();
11993
11994 return endMBB;
11995}
11996
11997MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011998X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11999 MachineInstr *MI,
12000 MachineBasicBlock *MBB) const {
12001 // Emit code to save XMM registers to the stack. The ABI says that the
12002 // number of registers to save is given in %al, so it's theoretically
12003 // possible to do an indirect jump trick to avoid saving all of them,
12004 // however this code takes a simpler approach and just executes all
12005 // of the stores if %al is non-zero. It's less code, and it's probably
12006 // easier on the hardware branch predictor, and stores aren't all that
12007 // expensive anyway.
12008
12009 // Create the new basic blocks. One block contains all the XMM stores,
12010 // and one block is the final destination regardless of whether any
12011 // stores were performed.
12012 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12013 MachineFunction *F = MBB->getParent();
12014 MachineFunction::iterator MBBIter = MBB;
12015 ++MBBIter;
12016 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12017 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12018 F->insert(MBBIter, XMMSaveMBB);
12019 F->insert(MBBIter, EndMBB);
12020
Dan Gohman14152b42010-07-06 20:24:04 +000012021 // Transfer the remainder of MBB and its successor edges to EndMBB.
12022 EndMBB->splice(EndMBB->begin(), MBB,
12023 llvm::next(MachineBasicBlock::iterator(MI)),
12024 MBB->end());
12025 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12026
Dan Gohmand6708ea2009-08-15 01:38:56 +000012027 // The original block will now fall through to the XMM save block.
12028 MBB->addSuccessor(XMMSaveMBB);
12029 // The XMMSaveMBB will fall through to the end block.
12030 XMMSaveMBB->addSuccessor(EndMBB);
12031
12032 // Now add the instructions.
12033 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12034 DebugLoc DL = MI->getDebugLoc();
12035
12036 unsigned CountReg = MI->getOperand(0).getReg();
12037 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12038 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12039
12040 if (!Subtarget->isTargetWin64()) {
12041 // If %al is 0, branch around the XMM save block.
12042 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012043 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012044 MBB->addSuccessor(EndMBB);
12045 }
12046
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012047 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012048 // In the XMM save block, save all the XMM argument registers.
12049 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12050 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012051 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012052 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012053 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012054 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012055 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012056 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012057 .addFrameIndex(RegSaveFrameIndex)
12058 .addImm(/*Scale=*/1)
12059 .addReg(/*IndexReg=*/0)
12060 .addImm(/*Disp=*/Offset)
12061 .addReg(/*Segment=*/0)
12062 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012063 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012064 }
12065
Dan Gohman14152b42010-07-06 20:24:04 +000012066 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012067
12068 return EndMBB;
12069}
Mon P Wang63307c32008-05-05 19:05:59 +000012070
Evan Cheng60c07e12006-07-05 22:17:51 +000012071MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012072X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012073 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012074 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12075 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012076
Chris Lattner52600972009-09-02 05:57:00 +000012077 // To "insert" a SELECT_CC instruction, we actually have to insert the
12078 // diamond control-flow pattern. The incoming instruction knows the
12079 // destination vreg to set, the condition code register to branch on, the
12080 // true/false values to select between, and a branch opcode to use.
12081 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12082 MachineFunction::iterator It = BB;
12083 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012084
Chris Lattner52600972009-09-02 05:57:00 +000012085 // thisMBB:
12086 // ...
12087 // TrueVal = ...
12088 // cmpTY ccX, r1, r2
12089 // bCC copy1MBB
12090 // fallthrough --> copy0MBB
12091 MachineBasicBlock *thisMBB = BB;
12092 MachineFunction *F = BB->getParent();
12093 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12094 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012095 F->insert(It, copy0MBB);
12096 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012097
Bill Wendling730c07e2010-06-25 20:48:10 +000012098 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12099 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000012100 if (!MI->killsRegister(X86::EFLAGS)) {
12101 copy0MBB->addLiveIn(X86::EFLAGS);
12102 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012103 }
12104
Dan Gohman14152b42010-07-06 20:24:04 +000012105 // Transfer the remainder of BB and its successor edges to sinkMBB.
12106 sinkMBB->splice(sinkMBB->begin(), BB,
12107 llvm::next(MachineBasicBlock::iterator(MI)),
12108 BB->end());
12109 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12110
12111 // Add the true and fallthrough blocks as its successors.
12112 BB->addSuccessor(copy0MBB);
12113 BB->addSuccessor(sinkMBB);
12114
12115 // Create the conditional branch instruction.
12116 unsigned Opc =
12117 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12118 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12119
Chris Lattner52600972009-09-02 05:57:00 +000012120 // copy0MBB:
12121 // %FalseValue = ...
12122 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012123 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012124
Chris Lattner52600972009-09-02 05:57:00 +000012125 // sinkMBB:
12126 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12127 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012128 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12129 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012130 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12131 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12132
Dan Gohman14152b42010-07-06 20:24:04 +000012133 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012134 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012135}
12136
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012137MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012138X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12139 bool Is64Bit) const {
12140 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12141 DebugLoc DL = MI->getDebugLoc();
12142 MachineFunction *MF = BB->getParent();
12143 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12144
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012145 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012146
12147 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12148 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12149
12150 // BB:
12151 // ... [Till the alloca]
12152 // If stacklet is not large enough, jump to mallocMBB
12153 //
12154 // bumpMBB:
12155 // Allocate by subtracting from RSP
12156 // Jump to continueMBB
12157 //
12158 // mallocMBB:
12159 // Allocate by call to runtime
12160 //
12161 // continueMBB:
12162 // ...
12163 // [rest of original BB]
12164 //
12165
12166 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12167 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12168 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12169
12170 MachineRegisterInfo &MRI = MF->getRegInfo();
12171 const TargetRegisterClass *AddrRegClass =
12172 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12173
12174 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12175 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12176 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012177 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012178 sizeVReg = MI->getOperand(1).getReg(),
12179 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12180
12181 MachineFunction::iterator MBBIter = BB;
12182 ++MBBIter;
12183
12184 MF->insert(MBBIter, bumpMBB);
12185 MF->insert(MBBIter, mallocMBB);
12186 MF->insert(MBBIter, continueMBB);
12187
12188 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12189 (MachineBasicBlock::iterator(MI)), BB->end());
12190 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12191
12192 // Add code to the main basic block to check if the stack limit has been hit,
12193 // and if so, jump to mallocMBB otherwise to bumpMBB.
12194 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012195 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012196 .addReg(tmpSPVReg).addReg(sizeVReg);
12197 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
12198 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012199 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012200 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12201
12202 // bumpMBB simply decreases the stack pointer, since we know the current
12203 // stacklet has enough space.
12204 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012205 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012206 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012207 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012208 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12209
12210 // Calls into a routine in libgcc to allocate more space from the heap.
12211 if (Is64Bit) {
12212 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12213 .addReg(sizeVReg);
12214 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12215 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12216 } else {
12217 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12218 .addImm(12);
12219 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12220 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12221 .addExternalSymbol("__morestack_allocate_stack_space");
12222 }
12223
12224 if (!Is64Bit)
12225 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12226 .addImm(16);
12227
12228 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12229 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12230 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12231
12232 // Set up the CFG correctly.
12233 BB->addSuccessor(bumpMBB);
12234 BB->addSuccessor(mallocMBB);
12235 mallocMBB->addSuccessor(continueMBB);
12236 bumpMBB->addSuccessor(continueMBB);
12237
12238 // Take care of the PHI nodes.
12239 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12240 MI->getOperand(0).getReg())
12241 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12242 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12243
12244 // Delete the original pseudo instruction.
12245 MI->eraseFromParent();
12246
12247 // And we're done.
12248 return continueMBB;
12249}
12250
12251MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012252X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012253 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012254 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12255 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012256
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012257 assert(!Subtarget->isTargetEnvMacho());
12258
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012259 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12260 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012261
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012262 if (Subtarget->isTargetWin64()) {
12263 if (Subtarget->isTargetCygMing()) {
12264 // ___chkstk(Mingw64):
12265 // Clobbers R10, R11, RAX and EFLAGS.
12266 // Updates RSP.
12267 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12268 .addExternalSymbol("___chkstk")
12269 .addReg(X86::RAX, RegState::Implicit)
12270 .addReg(X86::RSP, RegState::Implicit)
12271 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12272 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12273 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12274 } else {
12275 // __chkstk(MSVCRT): does not update stack pointer.
12276 // Clobbers R10, R11 and EFLAGS.
12277 // FIXME: RAX(allocated size) might be reused and not killed.
12278 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12279 .addExternalSymbol("__chkstk")
12280 .addReg(X86::RAX, RegState::Implicit)
12281 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12282 // RAX has the offset to subtracted from RSP.
12283 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12284 .addReg(X86::RSP)
12285 .addReg(X86::RAX);
12286 }
12287 } else {
12288 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012289 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12290
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012291 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12292 .addExternalSymbol(StackProbeSymbol)
12293 .addReg(X86::EAX, RegState::Implicit)
12294 .addReg(X86::ESP, RegState::Implicit)
12295 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12296 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12297 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12298 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012299
Dan Gohman14152b42010-07-06 20:24:04 +000012300 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012301 return BB;
12302}
Chris Lattner52600972009-09-02 05:57:00 +000012303
12304MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012305X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12306 MachineBasicBlock *BB) const {
12307 // This is pretty easy. We're taking the value that we received from
12308 // our load from the relocation, sticking it in either RDI (x86-64)
12309 // or EAX and doing an indirect call. The return value will then
12310 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012311 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012312 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012313 DebugLoc DL = MI->getDebugLoc();
12314 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012315
12316 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012317 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012318
Eric Christopher30ef0e52010-06-03 04:07:48 +000012319 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012320 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12321 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012322 .addReg(X86::RIP)
12323 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012324 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012325 MI->getOperand(3).getTargetFlags())
12326 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012327 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012328 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012329 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012330 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12331 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012332 .addReg(0)
12333 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012334 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012335 MI->getOperand(3).getTargetFlags())
12336 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012337 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012338 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012339 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012340 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12341 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012342 .addReg(TII->getGlobalBaseReg(F))
12343 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012344 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012345 MI->getOperand(3).getTargetFlags())
12346 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012347 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012348 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012349 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012350
Dan Gohman14152b42010-07-06 20:24:04 +000012351 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012352 return BB;
12353}
12354
12355MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012356X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012357 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012358 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000012359 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012360 case X86::TAILJMPd64:
12361 case X86::TAILJMPr64:
12362 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000012363 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012364 case X86::TCRETURNdi64:
12365 case X86::TCRETURNri64:
12366 case X86::TCRETURNmi64:
12367 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12368 // On AMD64, additional defs should be added before register allocation.
12369 if (!Subtarget->isTargetWin64()) {
12370 MI->addRegisterDefined(X86::RSI);
12371 MI->addRegisterDefined(X86::RDI);
12372 MI->addRegisterDefined(X86::XMM6);
12373 MI->addRegisterDefined(X86::XMM7);
12374 MI->addRegisterDefined(X86::XMM8);
12375 MI->addRegisterDefined(X86::XMM9);
12376 MI->addRegisterDefined(X86::XMM10);
12377 MI->addRegisterDefined(X86::XMM11);
12378 MI->addRegisterDefined(X86::XMM12);
12379 MI->addRegisterDefined(X86::XMM13);
12380 MI->addRegisterDefined(X86::XMM14);
12381 MI->addRegisterDefined(X86::XMM15);
12382 }
12383 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012384 case X86::WIN_ALLOCA:
12385 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012386 case X86::SEG_ALLOCA_32:
12387 return EmitLoweredSegAlloca(MI, BB, false);
12388 case X86::SEG_ALLOCA_64:
12389 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012390 case X86::TLSCall_32:
12391 case X86::TLSCall_64:
12392 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012393 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012394 case X86::CMOV_FR32:
12395 case X86::CMOV_FR64:
12396 case X86::CMOV_V4F32:
12397 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012398 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012399 case X86::CMOV_V8F32:
12400 case X86::CMOV_V4F64:
12401 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012402 case X86::CMOV_GR16:
12403 case X86::CMOV_GR32:
12404 case X86::CMOV_RFP32:
12405 case X86::CMOV_RFP64:
12406 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012407 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012408
Dale Johannesen849f2142007-07-03 00:53:03 +000012409 case X86::FP32_TO_INT16_IN_MEM:
12410 case X86::FP32_TO_INT32_IN_MEM:
12411 case X86::FP32_TO_INT64_IN_MEM:
12412 case X86::FP64_TO_INT16_IN_MEM:
12413 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012414 case X86::FP64_TO_INT64_IN_MEM:
12415 case X86::FP80_TO_INT16_IN_MEM:
12416 case X86::FP80_TO_INT32_IN_MEM:
12417 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012418 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12419 DebugLoc DL = MI->getDebugLoc();
12420
Evan Cheng60c07e12006-07-05 22:17:51 +000012421 // Change the floating point control register to use "round towards zero"
12422 // mode when truncating to an integer value.
12423 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012424 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012425 addFrameReference(BuildMI(*BB, MI, DL,
12426 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012427
12428 // Load the old value of the high byte of the control word...
12429 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012430 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012431 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012432 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012433
12434 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012435 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012436 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012437
12438 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012439 addFrameReference(BuildMI(*BB, MI, DL,
12440 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012441
12442 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012443 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012444 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012445
12446 // Get the X86 opcode to use.
12447 unsigned Opc;
12448 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012449 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012450 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12451 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12452 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12453 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12454 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12455 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012456 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12457 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12458 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012459 }
12460
12461 X86AddressMode AM;
12462 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012463 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012464 AM.BaseType = X86AddressMode::RegBase;
12465 AM.Base.Reg = Op.getReg();
12466 } else {
12467 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012468 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012469 }
12470 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012471 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012472 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012473 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012474 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012475 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012476 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012477 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012478 AM.GV = Op.getGlobal();
12479 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012480 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012481 }
Dan Gohman14152b42010-07-06 20:24:04 +000012482 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012483 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012484
12485 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012486 addFrameReference(BuildMI(*BB, MI, DL,
12487 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012488
Dan Gohman14152b42010-07-06 20:24:04 +000012489 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012490 return BB;
12491 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012492 // String/text processing lowering.
12493 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012494 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012495 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12496 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012497 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012498 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12499 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012500 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012501 return EmitPCMP(MI, BB, 5, false /* in mem */);
12502 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012503 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012504 return EmitPCMP(MI, BB, 5, true /* in mem */);
12505
Eric Christopher228232b2010-11-30 07:20:12 +000012506 // Thread synchronization.
12507 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012508 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012509 case X86::MWAIT:
12510 return EmitMwait(MI, BB);
12511
Eric Christopherb120ab42009-08-18 22:50:32 +000012512 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012513 case X86::ATOMAND32:
12514 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012515 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012516 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012517 X86::NOT32r, X86::EAX,
12518 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012519 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012520 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12521 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012522 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012523 X86::NOT32r, X86::EAX,
12524 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012525 case X86::ATOMXOR32:
12526 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012527 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012528 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012529 X86::NOT32r, X86::EAX,
12530 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012531 case X86::ATOMNAND32:
12532 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012533 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012534 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012535 X86::NOT32r, X86::EAX,
12536 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012537 case X86::ATOMMIN32:
12538 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12539 case X86::ATOMMAX32:
12540 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12541 case X86::ATOMUMIN32:
12542 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12543 case X86::ATOMUMAX32:
12544 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012545
12546 case X86::ATOMAND16:
12547 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12548 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012549 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012550 X86::NOT16r, X86::AX,
12551 X86::GR16RegisterClass);
12552 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012553 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012554 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012555 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012556 X86::NOT16r, X86::AX,
12557 X86::GR16RegisterClass);
12558 case X86::ATOMXOR16:
12559 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12560 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012561 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012562 X86::NOT16r, X86::AX,
12563 X86::GR16RegisterClass);
12564 case X86::ATOMNAND16:
12565 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12566 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012567 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012568 X86::NOT16r, X86::AX,
12569 X86::GR16RegisterClass, true);
12570 case X86::ATOMMIN16:
12571 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12572 case X86::ATOMMAX16:
12573 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12574 case X86::ATOMUMIN16:
12575 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12576 case X86::ATOMUMAX16:
12577 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12578
12579 case X86::ATOMAND8:
12580 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12581 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012582 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012583 X86::NOT8r, X86::AL,
12584 X86::GR8RegisterClass);
12585 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012586 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012587 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012588 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012589 X86::NOT8r, X86::AL,
12590 X86::GR8RegisterClass);
12591 case X86::ATOMXOR8:
12592 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12593 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012594 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012595 X86::NOT8r, X86::AL,
12596 X86::GR8RegisterClass);
12597 case X86::ATOMNAND8:
12598 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12599 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012600 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012601 X86::NOT8r, X86::AL,
12602 X86::GR8RegisterClass, true);
12603 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012604 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012605 case X86::ATOMAND64:
12606 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012607 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012608 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012609 X86::NOT64r, X86::RAX,
12610 X86::GR64RegisterClass);
12611 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012612 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12613 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012614 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012615 X86::NOT64r, X86::RAX,
12616 X86::GR64RegisterClass);
12617 case X86::ATOMXOR64:
12618 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012619 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012620 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012621 X86::NOT64r, X86::RAX,
12622 X86::GR64RegisterClass);
12623 case X86::ATOMNAND64:
12624 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12625 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012626 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012627 X86::NOT64r, X86::RAX,
12628 X86::GR64RegisterClass, true);
12629 case X86::ATOMMIN64:
12630 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12631 case X86::ATOMMAX64:
12632 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12633 case X86::ATOMUMIN64:
12634 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12635 case X86::ATOMUMAX64:
12636 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012637
12638 // This group does 64-bit operations on a 32-bit host.
12639 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012640 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012641 X86::AND32rr, X86::AND32rr,
12642 X86::AND32ri, X86::AND32ri,
12643 false);
12644 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012645 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012646 X86::OR32rr, X86::OR32rr,
12647 X86::OR32ri, X86::OR32ri,
12648 false);
12649 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012650 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012651 X86::XOR32rr, X86::XOR32rr,
12652 X86::XOR32ri, X86::XOR32ri,
12653 false);
12654 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012655 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012656 X86::AND32rr, X86::AND32rr,
12657 X86::AND32ri, X86::AND32ri,
12658 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012659 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012660 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012661 X86::ADD32rr, X86::ADC32rr,
12662 X86::ADD32ri, X86::ADC32ri,
12663 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012664 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012665 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012666 X86::SUB32rr, X86::SBB32rr,
12667 X86::SUB32ri, X86::SBB32ri,
12668 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012669 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012670 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012671 X86::MOV32rr, X86::MOV32rr,
12672 X86::MOV32ri, X86::MOV32ri,
12673 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012674 case X86::VASTART_SAVE_XMM_REGS:
12675 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012676
12677 case X86::VAARG_64:
12678 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012679 }
12680}
12681
12682//===----------------------------------------------------------------------===//
12683// X86 Optimization Hooks
12684//===----------------------------------------------------------------------===//
12685
Dan Gohman475871a2008-07-27 21:46:04 +000012686void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012687 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012688 APInt &KnownZero,
12689 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012690 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012691 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012692 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012693 assert((Opc >= ISD::BUILTIN_OP_END ||
12694 Opc == ISD::INTRINSIC_WO_CHAIN ||
12695 Opc == ISD::INTRINSIC_W_CHAIN ||
12696 Opc == ISD::INTRINSIC_VOID) &&
12697 "Should use MaskedValueIsZero if you don't know whether Op"
12698 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012699
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012700 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012701 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012702 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012703 case X86ISD::ADD:
12704 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012705 case X86ISD::ADC:
12706 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012707 case X86ISD::SMUL:
12708 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012709 case X86ISD::INC:
12710 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012711 case X86ISD::OR:
12712 case X86ISD::XOR:
12713 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012714 // These nodes' second result is a boolean.
12715 if (Op.getResNo() == 0)
12716 break;
12717 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012718 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012719 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12720 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012721 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012722 case ISD::INTRINSIC_WO_CHAIN: {
12723 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12724 unsigned NumLoBits = 0;
12725 switch (IntId) {
12726 default: break;
12727 case Intrinsic::x86_sse_movmsk_ps:
12728 case Intrinsic::x86_avx_movmsk_ps_256:
12729 case Intrinsic::x86_sse2_movmsk_pd:
12730 case Intrinsic::x86_avx_movmsk_pd_256:
12731 case Intrinsic::x86_mmx_pmovmskb:
12732 case Intrinsic::x86_sse2_pmovmskb_128: {
12733 // High bits of movmskp{s|d}, pmovmskb are known zero.
12734 switch (IntId) {
12735 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12736 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12737 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12738 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12739 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12740 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
12741 }
12742 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12743 Mask.getBitWidth() - NumLoBits);
12744 break;
12745 }
12746 }
12747 break;
12748 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012749 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012750}
Chris Lattner259e97c2006-01-31 19:43:35 +000012751
Owen Andersonbc146b02010-09-21 20:42:50 +000012752unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12753 unsigned Depth) const {
12754 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12755 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12756 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012757
Owen Andersonbc146b02010-09-21 20:42:50 +000012758 // Fallback case.
12759 return 1;
12760}
12761
Evan Cheng206ee9d2006-07-07 08:33:52 +000012762/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012763/// node is a GlobalAddress + offset.
12764bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012765 const GlobalValue* &GA,
12766 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012767 if (N->getOpcode() == X86ISD::Wrapper) {
12768 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012769 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012770 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012771 return true;
12772 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012773 }
Evan Chengad4196b2008-05-12 19:56:52 +000012774 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012775}
12776
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012777/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12778/// same as extracting the high 128-bit part of 256-bit vector and then
12779/// inserting the result into the low part of a new 256-bit vector
12780static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12781 EVT VT = SVOp->getValueType(0);
12782 int NumElems = VT.getVectorNumElements();
12783
12784 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12785 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12786 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12787 SVOp->getMaskElt(j) >= 0)
12788 return false;
12789
12790 return true;
12791}
12792
12793/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12794/// same as extracting the low 128-bit part of 256-bit vector and then
12795/// inserting the result into the high part of a new 256-bit vector
12796static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12797 EVT VT = SVOp->getValueType(0);
12798 int NumElems = VT.getVectorNumElements();
12799
12800 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12801 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12802 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12803 SVOp->getMaskElt(j) >= 0)
12804 return false;
12805
12806 return true;
12807}
12808
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012809/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12810static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12811 TargetLowering::DAGCombinerInfo &DCI) {
12812 DebugLoc dl = N->getDebugLoc();
12813 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12814 SDValue V1 = SVOp->getOperand(0);
12815 SDValue V2 = SVOp->getOperand(1);
12816 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012817 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012818
12819 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12820 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12821 //
12822 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012823 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012824 // V UNDEF BUILD_VECTOR UNDEF
12825 // \ / \ /
12826 // CONCAT_VECTOR CONCAT_VECTOR
12827 // \ /
12828 // \ /
12829 // RESULT: V + zero extended
12830 //
12831 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12832 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12833 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12834 return SDValue();
12835
12836 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12837 return SDValue();
12838
12839 // To match the shuffle mask, the first half of the mask should
12840 // be exactly the first vector, and all the rest a splat with the
12841 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012842 for (int i = 0; i < NumElems/2; ++i)
12843 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12844 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12845 return SDValue();
12846
12847 // Emit a zeroed vector and insert the desired subvector on its
12848 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012849 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012850 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12851 DAG.getConstant(0, MVT::i32), DAG, dl);
12852 return DCI.CombineTo(N, InsV);
12853 }
12854
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012855 //===--------------------------------------------------------------------===//
12856 // Combine some shuffles into subvector extracts and inserts:
12857 //
12858
12859 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12860 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12861 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12862 DAG, dl);
12863 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12864 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12865 return DCI.CombineTo(N, InsV);
12866 }
12867
12868 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12869 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12870 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12871 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12872 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12873 return DCI.CombineTo(N, InsV);
12874 }
12875
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012876 return SDValue();
12877}
12878
12879/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012880static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012881 TargetLowering::DAGCombinerInfo &DCI,
12882 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012883 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012884 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012885
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012886 // Don't create instructions with illegal types after legalize types has run.
12887 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12888 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12889 return SDValue();
12890
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012891 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12892 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12893 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012894 return PerformShuffleCombine256(N, DAG, DCI);
12895
12896 // Only handle 128 wide vector from here on.
12897 if (VT.getSizeInBits() != 128)
12898 return SDValue();
12899
12900 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12901 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12902 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012903 SmallVector<SDValue, 16> Elts;
12904 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012905 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012906
Nate Begemanfdea31a2010-03-24 20:49:50 +000012907 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012908}
Evan Chengd880b972008-05-09 21:53:03 +000012909
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012910/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12911/// generation and convert it from being a bunch of shuffles and extracts
12912/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012913static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12914 const TargetLowering &TLI) {
12915 SDValue InputVector = N->getOperand(0);
12916
12917 // Only operate on vectors of 4 elements, where the alternative shuffling
12918 // gets to be more expensive.
12919 if (InputVector.getValueType() != MVT::v4i32)
12920 return SDValue();
12921
12922 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12923 // single use which is a sign-extend or zero-extend, and all elements are
12924 // used.
12925 SmallVector<SDNode *, 4> Uses;
12926 unsigned ExtractedElements = 0;
12927 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12928 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12929 if (UI.getUse().getResNo() != InputVector.getResNo())
12930 return SDValue();
12931
12932 SDNode *Extract = *UI;
12933 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12934 return SDValue();
12935
12936 if (Extract->getValueType(0) != MVT::i32)
12937 return SDValue();
12938 if (!Extract->hasOneUse())
12939 return SDValue();
12940 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12941 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12942 return SDValue();
12943 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12944 return SDValue();
12945
12946 // Record which element was extracted.
12947 ExtractedElements |=
12948 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12949
12950 Uses.push_back(Extract);
12951 }
12952
12953 // If not all the elements were used, this may not be worthwhile.
12954 if (ExtractedElements != 15)
12955 return SDValue();
12956
12957 // Ok, we've now decided to do the transformation.
12958 DebugLoc dl = InputVector.getDebugLoc();
12959
12960 // Store the value to a temporary stack slot.
12961 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012962 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12963 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012964
12965 // Replace each use (extract) with a load of the appropriate element.
12966 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12967 UE = Uses.end(); UI != UE; ++UI) {
12968 SDNode *Extract = *UI;
12969
Nadav Rotem86694292011-05-17 08:31:57 +000012970 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012971 SDValue Idx = Extract->getOperand(1);
12972 unsigned EltSize =
12973 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12974 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12975 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12976
Nadav Rotem86694292011-05-17 08:31:57 +000012977 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012978 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012979
12980 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012981 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012982 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000012983 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012984
12985 // Replace the exact with the load.
12986 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12987 }
12988
12989 // The replacement was made in place; don't return anything.
12990 return SDValue();
12991}
12992
Duncan Sands6bcd2192011-09-17 16:49:39 +000012993/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
12994/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012995static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012996 const X86Subtarget *Subtarget) {
12997 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012998 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012999 // Get the LHS/RHS of the select.
13000 SDValue LHS = N->getOperand(1);
13001 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013002 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013003
Dan Gohman670e5392009-09-21 18:03:22 +000013004 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013005 // instructions match the semantics of the common C idiom x<y?x:y but not
13006 // x<=y?x:y, because of how they handle negative zero (which can be
13007 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013008 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13009 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
13010 (Subtarget->hasXMMInt() ||
13011 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013012 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013013
Chris Lattner47b4ce82009-03-11 05:48:52 +000013014 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013015 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013016 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13017 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013018 switch (CC) {
13019 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013020 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013021 // Converting this to a min would handle NaNs incorrectly, and swapping
13022 // the operands would cause it to handle comparisons between positive
13023 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013024 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013025 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013026 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13027 break;
13028 std::swap(LHS, RHS);
13029 }
Dan Gohman670e5392009-09-21 18:03:22 +000013030 Opcode = X86ISD::FMIN;
13031 break;
13032 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013033 // Converting this to a min would handle comparisons between positive
13034 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013035 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013036 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13037 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013038 Opcode = X86ISD::FMIN;
13039 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013040 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013041 // Converting this to a min would handle both negative zeros and NaNs
13042 // incorrectly, but we can swap the operands to fix both.
13043 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013044 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013045 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013046 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013047 Opcode = X86ISD::FMIN;
13048 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013049
Dan Gohman670e5392009-09-21 18:03:22 +000013050 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013051 // Converting this to a max would handle comparisons between positive
13052 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013053 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013054 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013055 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013056 Opcode = X86ISD::FMAX;
13057 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013058 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013059 // Converting this to a max would handle NaNs incorrectly, and swapping
13060 // the operands would cause it to handle comparisons between positive
13061 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013062 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013063 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013064 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13065 break;
13066 std::swap(LHS, RHS);
13067 }
Dan Gohman670e5392009-09-21 18:03:22 +000013068 Opcode = X86ISD::FMAX;
13069 break;
13070 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013071 // Converting this to a max would handle both negative zeros and NaNs
13072 // incorrectly, but we can swap the operands to fix both.
13073 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013074 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013075 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013076 case ISD::SETGE:
13077 Opcode = X86ISD::FMAX;
13078 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013079 }
Dan Gohman670e5392009-09-21 18:03:22 +000013080 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013081 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13082 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013083 switch (CC) {
13084 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013085 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013086 // Converting this to a min would handle comparisons between positive
13087 // and negative zero incorrectly, and swapping the operands would
13088 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013089 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013090 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013091 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013092 break;
13093 std::swap(LHS, RHS);
13094 }
Dan Gohman670e5392009-09-21 18:03:22 +000013095 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013096 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013097 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013098 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013099 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013100 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13101 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013102 Opcode = X86ISD::FMIN;
13103 break;
13104 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013105 // Converting this to a min would handle both negative zeros and NaNs
13106 // incorrectly, but we can swap the operands to fix both.
13107 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013108 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013109 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013110 case ISD::SETGE:
13111 Opcode = X86ISD::FMIN;
13112 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013113
Dan Gohman670e5392009-09-21 18:03:22 +000013114 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013115 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013116 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013117 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013118 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013119 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013120 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013121 // Converting this to a max would handle comparisons between positive
13122 // and negative zero incorrectly, and swapping the operands would
13123 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013124 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013125 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013126 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013127 break;
13128 std::swap(LHS, RHS);
13129 }
Dan Gohman670e5392009-09-21 18:03:22 +000013130 Opcode = X86ISD::FMAX;
13131 break;
13132 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013133 // Converting this to a max would handle both negative zeros and NaNs
13134 // incorrectly, but we can swap the operands to fix both.
13135 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013136 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013137 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013138 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013139 Opcode = X86ISD::FMAX;
13140 break;
13141 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013142 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013143
Chris Lattner47b4ce82009-03-11 05:48:52 +000013144 if (Opcode)
13145 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013146 }
Eric Christopherfd179292009-08-27 18:07:15 +000013147
Chris Lattnerd1980a52009-03-12 06:52:53 +000013148 // If this is a select between two integer constants, try to do some
13149 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013150 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13151 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013152 // Don't do this for crazy integer types.
13153 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13154 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013155 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013156 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013157
Chris Lattnercee56e72009-03-13 05:53:31 +000013158 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013159 // Efficiently invertible.
13160 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13161 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13162 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13163 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013164 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013165 }
Eric Christopherfd179292009-08-27 18:07:15 +000013166
Chris Lattnerd1980a52009-03-12 06:52:53 +000013167 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013168 if (FalseC->getAPIntValue() == 0 &&
13169 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013170 if (NeedsCondInvert) // Invert the condition if needed.
13171 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13172 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013173
Chris Lattnerd1980a52009-03-12 06:52:53 +000013174 // Zero extend the condition if needed.
13175 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013176
Chris Lattnercee56e72009-03-13 05:53:31 +000013177 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013178 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013179 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013180 }
Eric Christopherfd179292009-08-27 18:07:15 +000013181
Chris Lattner97a29a52009-03-13 05:22:11 +000013182 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013183 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013184 if (NeedsCondInvert) // Invert the condition if needed.
13185 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13186 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013187
Chris Lattner97a29a52009-03-13 05:22:11 +000013188 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013189 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13190 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013191 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013192 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013193 }
Eric Christopherfd179292009-08-27 18:07:15 +000013194
Chris Lattnercee56e72009-03-13 05:53:31 +000013195 // Optimize cases that will turn into an LEA instruction. This requires
13196 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013197 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013198 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013199 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013200
Chris Lattnercee56e72009-03-13 05:53:31 +000013201 bool isFastMultiplier = false;
13202 if (Diff < 10) {
13203 switch ((unsigned char)Diff) {
13204 default: break;
13205 case 1: // result = add base, cond
13206 case 2: // result = lea base( , cond*2)
13207 case 3: // result = lea base(cond, cond*2)
13208 case 4: // result = lea base( , cond*4)
13209 case 5: // result = lea base(cond, cond*4)
13210 case 8: // result = lea base( , cond*8)
13211 case 9: // result = lea base(cond, cond*8)
13212 isFastMultiplier = true;
13213 break;
13214 }
13215 }
Eric Christopherfd179292009-08-27 18:07:15 +000013216
Chris Lattnercee56e72009-03-13 05:53:31 +000013217 if (isFastMultiplier) {
13218 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13219 if (NeedsCondInvert) // Invert the condition if needed.
13220 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13221 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013222
Chris Lattnercee56e72009-03-13 05:53:31 +000013223 // Zero extend the condition if needed.
13224 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13225 Cond);
13226 // Scale the condition by the difference.
13227 if (Diff != 1)
13228 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13229 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013230
Chris Lattnercee56e72009-03-13 05:53:31 +000013231 // Add the base if non-zero.
13232 if (FalseC->getAPIntValue() != 0)
13233 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13234 SDValue(FalseC, 0));
13235 return Cond;
13236 }
Eric Christopherfd179292009-08-27 18:07:15 +000013237 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013238 }
13239 }
Eric Christopherfd179292009-08-27 18:07:15 +000013240
Dan Gohman475871a2008-07-27 21:46:04 +000013241 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013242}
13243
Chris Lattnerd1980a52009-03-12 06:52:53 +000013244/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13245static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13246 TargetLowering::DAGCombinerInfo &DCI) {
13247 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013248
Chris Lattnerd1980a52009-03-12 06:52:53 +000013249 // If the flag operand isn't dead, don't touch this CMOV.
13250 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13251 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013252
Evan Chengb5a55d92011-05-24 01:48:22 +000013253 SDValue FalseOp = N->getOperand(0);
13254 SDValue TrueOp = N->getOperand(1);
13255 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13256 SDValue Cond = N->getOperand(3);
13257 if (CC == X86::COND_E || CC == X86::COND_NE) {
13258 switch (Cond.getOpcode()) {
13259 default: break;
13260 case X86ISD::BSR:
13261 case X86ISD::BSF:
13262 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13263 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13264 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13265 }
13266 }
13267
Chris Lattnerd1980a52009-03-12 06:52:53 +000013268 // If this is a select between two integer constants, try to do some
13269 // optimizations. Note that the operands are ordered the opposite of SELECT
13270 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013271 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13272 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013273 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13274 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013275 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13276 CC = X86::GetOppositeBranchCondition(CC);
13277 std::swap(TrueC, FalseC);
13278 }
Eric Christopherfd179292009-08-27 18:07:15 +000013279
Chris Lattnerd1980a52009-03-12 06:52:53 +000013280 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013281 // This is efficient for any integer data type (including i8/i16) and
13282 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013283 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013284 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13285 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013286
Chris Lattnerd1980a52009-03-12 06:52:53 +000013287 // Zero extend the condition if needed.
13288 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013289
Chris Lattnerd1980a52009-03-12 06:52:53 +000013290 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13291 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013292 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013293 if (N->getNumValues() == 2) // Dead flag value?
13294 return DCI.CombineTo(N, Cond, SDValue());
13295 return Cond;
13296 }
Eric Christopherfd179292009-08-27 18:07:15 +000013297
Chris Lattnercee56e72009-03-13 05:53:31 +000013298 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13299 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013300 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013301 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13302 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013303
Chris Lattner97a29a52009-03-13 05:22:11 +000013304 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013305 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13306 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013307 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13308 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013309
Chris Lattner97a29a52009-03-13 05:22:11 +000013310 if (N->getNumValues() == 2) // Dead flag value?
13311 return DCI.CombineTo(N, Cond, SDValue());
13312 return Cond;
13313 }
Eric Christopherfd179292009-08-27 18:07:15 +000013314
Chris Lattnercee56e72009-03-13 05:53:31 +000013315 // Optimize cases that will turn into an LEA instruction. This requires
13316 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013317 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013318 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013319 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013320
Chris Lattnercee56e72009-03-13 05:53:31 +000013321 bool isFastMultiplier = false;
13322 if (Diff < 10) {
13323 switch ((unsigned char)Diff) {
13324 default: break;
13325 case 1: // result = add base, cond
13326 case 2: // result = lea base( , cond*2)
13327 case 3: // result = lea base(cond, cond*2)
13328 case 4: // result = lea base( , cond*4)
13329 case 5: // result = lea base(cond, cond*4)
13330 case 8: // result = lea base( , cond*8)
13331 case 9: // result = lea base(cond, cond*8)
13332 isFastMultiplier = true;
13333 break;
13334 }
13335 }
Eric Christopherfd179292009-08-27 18:07:15 +000013336
Chris Lattnercee56e72009-03-13 05:53:31 +000013337 if (isFastMultiplier) {
13338 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013339 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13340 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013341 // Zero extend the condition if needed.
13342 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13343 Cond);
13344 // Scale the condition by the difference.
13345 if (Diff != 1)
13346 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13347 DAG.getConstant(Diff, Cond.getValueType()));
13348
13349 // Add the base if non-zero.
13350 if (FalseC->getAPIntValue() != 0)
13351 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13352 SDValue(FalseC, 0));
13353 if (N->getNumValues() == 2) // Dead flag value?
13354 return DCI.CombineTo(N, Cond, SDValue());
13355 return Cond;
13356 }
Eric Christopherfd179292009-08-27 18:07:15 +000013357 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013358 }
13359 }
13360 return SDValue();
13361}
13362
13363
Evan Cheng0b0cd912009-03-28 05:57:29 +000013364/// PerformMulCombine - Optimize a single multiply with constant into two
13365/// in order to implement it with two cheaper instructions, e.g.
13366/// LEA + SHL, LEA + LEA.
13367static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13368 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013369 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13370 return SDValue();
13371
Owen Andersone50ed302009-08-10 22:56:29 +000013372 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013373 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013374 return SDValue();
13375
13376 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13377 if (!C)
13378 return SDValue();
13379 uint64_t MulAmt = C->getZExtValue();
13380 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13381 return SDValue();
13382
13383 uint64_t MulAmt1 = 0;
13384 uint64_t MulAmt2 = 0;
13385 if ((MulAmt % 9) == 0) {
13386 MulAmt1 = 9;
13387 MulAmt2 = MulAmt / 9;
13388 } else if ((MulAmt % 5) == 0) {
13389 MulAmt1 = 5;
13390 MulAmt2 = MulAmt / 5;
13391 } else if ((MulAmt % 3) == 0) {
13392 MulAmt1 = 3;
13393 MulAmt2 = MulAmt / 3;
13394 }
13395 if (MulAmt2 &&
13396 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13397 DebugLoc DL = N->getDebugLoc();
13398
13399 if (isPowerOf2_64(MulAmt2) &&
13400 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13401 // If second multiplifer is pow2, issue it first. We want the multiply by
13402 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13403 // is an add.
13404 std::swap(MulAmt1, MulAmt2);
13405
13406 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013407 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013408 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013409 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013410 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013411 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013412 DAG.getConstant(MulAmt1, VT));
13413
Eric Christopherfd179292009-08-27 18:07:15 +000013414 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013415 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013416 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013417 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013418 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013419 DAG.getConstant(MulAmt2, VT));
13420
13421 // Do not add new nodes to DAG combiner worklist.
13422 DCI.CombineTo(N, NewMul, false);
13423 }
13424 return SDValue();
13425}
13426
Evan Chengad9c0a32009-12-15 00:53:42 +000013427static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13428 SDValue N0 = N->getOperand(0);
13429 SDValue N1 = N->getOperand(1);
13430 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13431 EVT VT = N0.getValueType();
13432
13433 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13434 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013435 if (VT.isInteger() && !VT.isVector() &&
13436 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013437 N0.getOperand(1).getOpcode() == ISD::Constant) {
13438 SDValue N00 = N0.getOperand(0);
13439 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13440 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13441 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13442 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13443 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13444 APInt ShAmt = N1C->getAPIntValue();
13445 Mask = Mask.shl(ShAmt);
13446 if (Mask != 0)
13447 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13448 N00, DAG.getConstant(Mask, VT));
13449 }
13450 }
13451
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013452
13453 // Hardware support for vector shifts is sparse which makes us scalarize the
13454 // vector operations in many cases. Also, on sandybridge ADD is faster than
13455 // shl.
13456 // (shl V, 1) -> add V,V
13457 if (isSplatVector(N1.getNode())) {
13458 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13459 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13460 // We shift all of the values by one. In many cases we do not have
13461 // hardware support for this operation. This is better expressed as an ADD
13462 // of two values.
13463 if (N1C && (1 == N1C->getZExtValue())) {
13464 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13465 }
13466 }
13467
Evan Chengad9c0a32009-12-15 00:53:42 +000013468 return SDValue();
13469}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013470
Nate Begeman740ab032009-01-26 00:52:55 +000013471/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13472/// when possible.
13473static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13474 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013475 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013476 if (N->getOpcode() == ISD::SHL) {
13477 SDValue V = PerformSHLCombine(N, DAG);
13478 if (V.getNode()) return V;
13479 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013480
Nate Begeman740ab032009-01-26 00:52:55 +000013481 // On X86 with SSE2 support, we can transform this to a vector shift if
13482 // all elements are shifted by the same amount. We can't do this in legalize
13483 // because the a constant vector is typically transformed to a constant pool
13484 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013485 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013486 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013487
Craig Topper7be5dfd2011-11-12 09:58:49 +000013488 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
13489 (!Subtarget->hasAVX2() ||
13490 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013491 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013492
Mon P Wang3becd092009-01-28 08:12:05 +000013493 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013494 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013495 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013496 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013497 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13498 unsigned NumElts = VT.getVectorNumElements();
13499 unsigned i = 0;
13500 for (; i != NumElts; ++i) {
13501 SDValue Arg = ShAmtOp.getOperand(i);
13502 if (Arg.getOpcode() == ISD::UNDEF) continue;
13503 BaseShAmt = Arg;
13504 break;
13505 }
13506 for (; i != NumElts; ++i) {
13507 SDValue Arg = ShAmtOp.getOperand(i);
13508 if (Arg.getOpcode() == ISD::UNDEF) continue;
13509 if (Arg != BaseShAmt) {
13510 return SDValue();
13511 }
13512 }
13513 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013514 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013515 SDValue InVec = ShAmtOp.getOperand(0);
13516 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13517 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13518 unsigned i = 0;
13519 for (; i != NumElts; ++i) {
13520 SDValue Arg = InVec.getOperand(i);
13521 if (Arg.getOpcode() == ISD::UNDEF) continue;
13522 BaseShAmt = Arg;
13523 break;
13524 }
13525 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13526 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013527 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013528 if (C->getZExtValue() == SplatIdx)
13529 BaseShAmt = InVec.getOperand(1);
13530 }
13531 }
13532 if (BaseShAmt.getNode() == 0)
13533 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13534 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013535 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013536 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013537
Mon P Wangefa42202009-09-03 19:56:25 +000013538 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013539 if (EltVT.bitsGT(MVT::i32))
13540 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13541 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013542 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013543
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013544 // The shift amount is identical so we can do a vector shift.
13545 SDValue ValOp = N->getOperand(0);
13546 switch (N->getOpcode()) {
13547 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013548 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013549 break;
13550 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013551 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013552 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013553 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013554 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013555 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013556 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013557 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013558 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013559 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013560 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013561 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013562 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013563 if (VT == MVT::v4i64)
13564 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13565 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
13566 ValOp, BaseShAmt);
13567 if (VT == MVT::v8i32)
13568 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13569 DAG.getConstant(Intrinsic::x86_avx2_pslli_d, MVT::i32),
13570 ValOp, BaseShAmt);
13571 if (VT == MVT::v16i16)
13572 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13573 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
13574 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013575 break;
13576 case ISD::SRA:
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_psrai_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_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013584 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013585 if (VT == MVT::v8i32)
13586 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13587 DAG.getConstant(Intrinsic::x86_avx2_psrai_d, MVT::i32),
13588 ValOp, BaseShAmt);
13589 if (VT == MVT::v16i16)
13590 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13591 DAG.getConstant(Intrinsic::x86_avx2_psrai_w, MVT::i32),
13592 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013593 break;
13594 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013595 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013596 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013597 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013598 ValOp, BaseShAmt);
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_psrli_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_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013606 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013607 if (VT == MVT::v4i64)
13608 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13609 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
13610 ValOp, BaseShAmt);
13611 if (VT == MVT::v8i32)
13612 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13613 DAG.getConstant(Intrinsic::x86_avx2_psrli_d, MVT::i32),
13614 ValOp, BaseShAmt);
13615 if (VT == MVT::v16i16)
13616 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13617 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
13618 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013619 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013620 }
13621 return SDValue();
13622}
13623
Nate Begemanb65c1752010-12-17 22:55:37 +000013624
Stuart Hastings865f0932011-06-03 23:53:54 +000013625// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13626// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13627// and friends. Likewise for OR -> CMPNEQSS.
13628static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13629 TargetLowering::DAGCombinerInfo &DCI,
13630 const X86Subtarget *Subtarget) {
13631 unsigned opcode;
13632
13633 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13634 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013635 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013636 SDValue N0 = N->getOperand(0);
13637 SDValue N1 = N->getOperand(1);
13638 SDValue CMP0 = N0->getOperand(1);
13639 SDValue CMP1 = N1->getOperand(1);
13640 DebugLoc DL = N->getDebugLoc();
13641
13642 // The SETCCs should both refer to the same CMP.
13643 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13644 return SDValue();
13645
13646 SDValue CMP00 = CMP0->getOperand(0);
13647 SDValue CMP01 = CMP0->getOperand(1);
13648 EVT VT = CMP00.getValueType();
13649
13650 if (VT == MVT::f32 || VT == MVT::f64) {
13651 bool ExpectingFlags = false;
13652 // Check for any users that want flags:
13653 for (SDNode::use_iterator UI = N->use_begin(),
13654 UE = N->use_end();
13655 !ExpectingFlags && UI != UE; ++UI)
13656 switch (UI->getOpcode()) {
13657 default:
13658 case ISD::BR_CC:
13659 case ISD::BRCOND:
13660 case ISD::SELECT:
13661 ExpectingFlags = true;
13662 break;
13663 case ISD::CopyToReg:
13664 case ISD::SIGN_EXTEND:
13665 case ISD::ZERO_EXTEND:
13666 case ISD::ANY_EXTEND:
13667 break;
13668 }
13669
13670 if (!ExpectingFlags) {
13671 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13672 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13673
13674 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13675 X86::CondCode tmp = cc0;
13676 cc0 = cc1;
13677 cc1 = tmp;
13678 }
13679
13680 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13681 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13682 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13683 X86ISD::NodeType NTOperator = is64BitFP ?
13684 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13685 // FIXME: need symbolic constants for these magic numbers.
13686 // See X86ATTInstPrinter.cpp:printSSECC().
13687 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13688 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13689 DAG.getConstant(x86cc, MVT::i8));
13690 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13691 OnesOrZeroesF);
13692 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13693 DAG.getConstant(1, MVT::i32));
13694 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13695 return OneBitOfTruth;
13696 }
13697 }
13698 }
13699 }
13700 return SDValue();
13701}
13702
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013703/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13704/// so it can be folded inside ANDNP.
13705static bool CanFoldXORWithAllOnes(const SDNode *N) {
13706 EVT VT = N->getValueType(0);
13707
13708 // Match direct AllOnes for 128 and 256-bit vectors
13709 if (ISD::isBuildVectorAllOnes(N))
13710 return true;
13711
13712 // Look through a bit convert.
13713 if (N->getOpcode() == ISD::BITCAST)
13714 N = N->getOperand(0).getNode();
13715
13716 // Sometimes the operand may come from a insert_subvector building a 256-bit
13717 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013718 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013719 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13720 SDValue V1 = N->getOperand(0);
13721 SDValue V2 = N->getOperand(1);
13722
13723 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13724 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13725 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13726 ISD::isBuildVectorAllOnes(V2.getNode()))
13727 return true;
13728 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013729
13730 return false;
13731}
13732
Nate Begemanb65c1752010-12-17 22:55:37 +000013733static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13734 TargetLowering::DAGCombinerInfo &DCI,
13735 const X86Subtarget *Subtarget) {
13736 if (DCI.isBeforeLegalizeOps())
13737 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013738
Stuart Hastings865f0932011-06-03 23:53:54 +000013739 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13740 if (R.getNode())
13741 return R;
13742
Craig Topper54a11172011-10-14 07:06:56 +000013743 EVT VT = N->getValueType(0);
13744
Craig Topperb4c94572011-10-21 06:55:01 +000013745 // Create ANDN, BLSI, and BLSR instructions
13746 // BLSI is X & (-X)
13747 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013748 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13749 SDValue N0 = N->getOperand(0);
13750 SDValue N1 = N->getOperand(1);
13751 DebugLoc DL = N->getDebugLoc();
13752
13753 // Check LHS for not
13754 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13755 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13756 // Check RHS for not
13757 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13758 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13759
Craig Topperb4c94572011-10-21 06:55:01 +000013760 // Check LHS for neg
13761 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13762 isZero(N0.getOperand(0)))
13763 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13764
13765 // Check RHS for neg
13766 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13767 isZero(N1.getOperand(0)))
13768 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13769
13770 // Check LHS for X-1
13771 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13772 isAllOnes(N0.getOperand(1)))
13773 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13774
13775 // Check RHS for X-1
13776 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13777 isAllOnes(N1.getOperand(1)))
13778 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13779
Craig Topper54a11172011-10-14 07:06:56 +000013780 return SDValue();
13781 }
13782
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013783 // Want to form ANDNP nodes:
13784 // 1) In the hopes of then easily combining them with OR and AND nodes
13785 // to form PBLEND/PSIGN.
13786 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013787 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013788 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013789
Nate Begemanb65c1752010-12-17 22:55:37 +000013790 SDValue N0 = N->getOperand(0);
13791 SDValue N1 = N->getOperand(1);
13792 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013793
Nate Begemanb65c1752010-12-17 22:55:37 +000013794 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013795 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013796 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13797 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013798 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013799
13800 // Check RHS for vnot
13801 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013802 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13803 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013804 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013805
Nate Begemanb65c1752010-12-17 22:55:37 +000013806 return SDValue();
13807}
13808
Evan Cheng760d1942010-01-04 21:22:48 +000013809static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013810 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013811 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013812 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013813 return SDValue();
13814
Stuart Hastings865f0932011-06-03 23:53:54 +000013815 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13816 if (R.getNode())
13817 return R;
13818
Evan Cheng760d1942010-01-04 21:22:48 +000013819 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000013820
Evan Cheng760d1942010-01-04 21:22:48 +000013821 SDValue N0 = N->getOperand(0);
13822 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013823
Nate Begemanb65c1752010-12-17 22:55:37 +000013824 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000013825 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperc0d82852011-11-22 00:44:41 +000013826 if (!Subtarget->hasSSSE3orAVX() ||
Craig Topper1666cb62011-11-19 07:07:26 +000013827 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
13828 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013829
Craig Topper1666cb62011-11-19 07:07:26 +000013830 // Canonicalize pandn to RHS
13831 if (N0.getOpcode() == X86ISD::ANDNP)
13832 std::swap(N0, N1);
13833 // or (and (m, x), (pandn m, y))
13834 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
13835 SDValue Mask = N1.getOperand(0);
13836 SDValue X = N1.getOperand(1);
13837 SDValue Y;
13838 if (N0.getOperand(0) == Mask)
13839 Y = N0.getOperand(1);
13840 if (N0.getOperand(1) == Mask)
13841 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013842
Craig Topper1666cb62011-11-19 07:07:26 +000013843 // Check to see if the mask appeared in both the AND and ANDNP and
13844 if (!Y.getNode())
13845 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013846
Craig Topper1666cb62011-11-19 07:07:26 +000013847 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13848 if (Mask.getOpcode() != ISD::BITCAST ||
13849 X.getOpcode() != ISD::BITCAST ||
13850 Y.getOpcode() != ISD::BITCAST)
13851 return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013852
Craig Topper1666cb62011-11-19 07:07:26 +000013853 // Look through mask bitcast.
13854 Mask = Mask.getOperand(0);
13855 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013856
Craig Topper1666cb62011-11-19 07:07:26 +000013857 // Validate that the Mask operand is a vector sra node. The sra node
13858 // will be an intrinsic.
13859 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13860 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013861
Craig Topper1666cb62011-11-19 07:07:26 +000013862 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13863 // there is no psrai.b
13864 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13865 case Intrinsic::x86_sse2_psrai_w:
13866 case Intrinsic::x86_sse2_psrai_d:
13867 case Intrinsic::x86_avx2_psrai_w:
13868 case Intrinsic::x86_avx2_psrai_d:
13869 break;
13870 default: return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013871 }
Craig Topper1666cb62011-11-19 07:07:26 +000013872
13873 // Check that the SRA is all signbits.
13874 SDValue SraC = Mask.getOperand(2);
13875 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13876 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13877 if ((SraAmt + 1) != EltBits)
13878 return SDValue();
13879
13880 DebugLoc DL = N->getDebugLoc();
13881
13882 // Now we know we at least have a plendvb with the mask val. See if
13883 // we can form a psignb/w/d.
13884 // psign = x.type == y.type == mask.type && y = sub(0, x);
13885 X = X.getOperand(0);
13886 Y = Y.getOperand(0);
13887 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13888 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Topper31133842011-11-19 07:33:10 +000013889 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType() &&
13890 (EltBits == 8 || EltBits == 16 || EltBits == 32)) {
13891 SDValue Sign = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X,
13892 Mask.getOperand(1));
13893 return DAG.getNode(ISD::BITCAST, DL, VT, Sign);
Craig Topper1666cb62011-11-19 07:07:26 +000013894 }
13895 // PBLENDVB only available on SSE 4.1
Craig Topperc0d82852011-11-22 00:44:41 +000013896 if (!Subtarget->hasSSE41orAVX())
Craig Topper1666cb62011-11-19 07:07:26 +000013897 return SDValue();
13898
13899 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
13900
13901 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
13902 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
13903 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000013904 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000013905 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000013906 }
13907 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013908
Craig Topper1666cb62011-11-19 07:07:26 +000013909 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
13910 return SDValue();
13911
Nate Begemanb65c1752010-12-17 22:55:37 +000013912 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013913 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13914 std::swap(N0, N1);
13915 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13916 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013917 if (!N0.hasOneUse() || !N1.hasOneUse())
13918 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013919
13920 SDValue ShAmt0 = N0.getOperand(1);
13921 if (ShAmt0.getValueType() != MVT::i8)
13922 return SDValue();
13923 SDValue ShAmt1 = N1.getOperand(1);
13924 if (ShAmt1.getValueType() != MVT::i8)
13925 return SDValue();
13926 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13927 ShAmt0 = ShAmt0.getOperand(0);
13928 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13929 ShAmt1 = ShAmt1.getOperand(0);
13930
13931 DebugLoc DL = N->getDebugLoc();
13932 unsigned Opc = X86ISD::SHLD;
13933 SDValue Op0 = N0.getOperand(0);
13934 SDValue Op1 = N1.getOperand(0);
13935 if (ShAmt0.getOpcode() == ISD::SUB) {
13936 Opc = X86ISD::SHRD;
13937 std::swap(Op0, Op1);
13938 std::swap(ShAmt0, ShAmt1);
13939 }
13940
Evan Cheng8b1190a2010-04-28 01:18:01 +000013941 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013942 if (ShAmt1.getOpcode() == ISD::SUB) {
13943 SDValue Sum = ShAmt1.getOperand(0);
13944 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013945 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13946 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13947 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13948 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013949 return DAG.getNode(Opc, DL, VT,
13950 Op0, Op1,
13951 DAG.getNode(ISD::TRUNCATE, DL,
13952 MVT::i8, ShAmt0));
13953 }
13954 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13955 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13956 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013957 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013958 return DAG.getNode(Opc, DL, VT,
13959 N0.getOperand(0), N1.getOperand(0),
13960 DAG.getNode(ISD::TRUNCATE, DL,
13961 MVT::i8, ShAmt0));
13962 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013963
Evan Cheng760d1942010-01-04 21:22:48 +000013964 return SDValue();
13965}
13966
Craig Topperb4c94572011-10-21 06:55:01 +000013967static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
13968 TargetLowering::DAGCombinerInfo &DCI,
13969 const X86Subtarget *Subtarget) {
13970 if (DCI.isBeforeLegalizeOps())
13971 return SDValue();
13972
13973 EVT VT = N->getValueType(0);
13974
13975 if (VT != MVT::i32 && VT != MVT::i64)
13976 return SDValue();
13977
13978 // Create BLSMSK instructions by finding X ^ (X-1)
13979 SDValue N0 = N->getOperand(0);
13980 SDValue N1 = N->getOperand(1);
13981 DebugLoc DL = N->getDebugLoc();
13982
13983 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13984 isAllOnes(N0.getOperand(1)))
13985 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
13986
13987 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13988 isAllOnes(N1.getOperand(1)))
13989 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
13990
13991 return SDValue();
13992}
13993
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013994/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
13995static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
13996 const X86Subtarget *Subtarget) {
13997 LoadSDNode *Ld = cast<LoadSDNode>(N);
13998 EVT RegVT = Ld->getValueType(0);
13999 EVT MemVT = Ld->getMemoryVT();
14000 DebugLoc dl = Ld->getDebugLoc();
14001 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14002
14003 ISD::LoadExtType Ext = Ld->getExtensionType();
14004
Nadav Rotemca6f2962011-09-18 19:00:23 +000014005 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014006 // shuffle. We need SSE4 for the shuffles.
14007 // TODO: It is possible to support ZExt by zeroing the undef values
14008 // during the shuffle phase or after the shuffle.
14009 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
14010 assert(MemVT != RegVT && "Cannot extend to the same type");
14011 assert(MemVT.isVector() && "Must load a vector from memory");
14012
14013 unsigned NumElems = RegVT.getVectorNumElements();
14014 unsigned RegSz = RegVT.getSizeInBits();
14015 unsigned MemSz = MemVT.getSizeInBits();
14016 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000014017 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014018 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
14019
14020 // Attempt to load the original value using a single load op.
14021 // Find a scalar type which is equal to the loaded word size.
14022 MVT SclrLoadTy = MVT::i8;
14023 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14024 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14025 MVT Tp = (MVT::SimpleValueType)tp;
14026 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
14027 SclrLoadTy = Tp;
14028 break;
14029 }
14030 }
14031
14032 // Proceed if a load word is found.
14033 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
14034
14035 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
14036 RegSz/SclrLoadTy.getSizeInBits());
14037
14038 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
14039 RegSz/MemVT.getScalarType().getSizeInBits());
14040 // Can't shuffle using an illegal type.
14041 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14042
14043 // Perform a single load.
14044 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
14045 Ld->getBasePtr(),
14046 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014047 Ld->isNonTemporal(), Ld->isInvariant(),
14048 Ld->getAlignment());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014049
14050 // Insert the word loaded into a vector.
14051 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
14052 LoadUnitVecVT, ScalarLoad);
14053
14054 // Bitcast the loaded value to a vector of the original element type, in
14055 // the size of the target vector type.
14056 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
14057 unsigned SizeRatio = RegSz/MemSz;
14058
14059 // Redistribute the loaded elements into the different locations.
14060 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14061 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
14062
14063 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
14064 DAG.getUNDEF(SlicedVec.getValueType()),
14065 ShuffleVec.data());
14066
14067 // Bitcast to the requested type.
14068 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
14069 // Replace the original load with the new sequence
14070 // and return the new chain.
14071 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
14072 return SDValue(ScalarLoad.getNode(), 1);
14073 }
14074
14075 return SDValue();
14076}
14077
Chris Lattner149a4e52008-02-22 02:09:43 +000014078/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014079static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000014080 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000014081 StoreSDNode *St = cast<StoreSDNode>(N);
14082 EVT VT = St->getValue().getValueType();
14083 EVT StVT = St->getMemoryVT();
14084 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000014085 SDValue StoredVal = St->getOperand(1);
14086 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14087
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014088 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000014089 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
14090 // 128-bit ones. If in the future the cost becomes only one memory access the
14091 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000014092 if (VT.getSizeInBits() == 256 &&
14093 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
14094 StoredVal.getNumOperands() == 2) {
14095
14096 SDValue Value0 = StoredVal.getOperand(0);
14097 SDValue Value1 = StoredVal.getOperand(1);
14098
14099 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
14100 SDValue Ptr0 = St->getBasePtr();
14101 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
14102
14103 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
14104 St->getPointerInfo(), St->isVolatile(),
14105 St->isNonTemporal(), St->getAlignment());
14106 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
14107 St->getPointerInfo(), St->isVolatile(),
14108 St->isNonTemporal(), St->getAlignment());
14109 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
14110 }
Nadav Rotem614061b2011-08-10 19:30:14 +000014111
14112 // Optimize trunc store (of multiple scalars) to shuffle and store.
14113 // First, pack all of the elements in one place. Next, store to memory
14114 // in fewer chunks.
14115 if (St->isTruncatingStore() && VT.isVector()) {
14116 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14117 unsigned NumElems = VT.getVectorNumElements();
14118 assert(StVT != VT && "Cannot truncate to the same type");
14119 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
14120 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
14121
14122 // From, To sizes and ElemCount must be pow of two
14123 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014124 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000014125 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014126 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014127
Nadav Rotem614061b2011-08-10 19:30:14 +000014128 unsigned SizeRatio = FromSz / ToSz;
14129
14130 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
14131
14132 // Create a type on which we perform the shuffle
14133 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
14134 StVT.getScalarType(), NumElems*SizeRatio);
14135
14136 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
14137
14138 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
14139 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14140 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
14141
14142 // Can't shuffle using an illegal type
14143 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14144
14145 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
14146 DAG.getUNDEF(WideVec.getValueType()),
14147 ShuffleVec.data());
14148 // At this point all of the data is stored at the bottom of the
14149 // register. We now need to save it to mem.
14150
14151 // Find the largest store unit
14152 MVT StoreType = MVT::i8;
14153 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14154 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14155 MVT Tp = (MVT::SimpleValueType)tp;
14156 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
14157 StoreType = Tp;
14158 }
14159
14160 // Bitcast the original vector into a vector of store-size units
14161 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
14162 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
14163 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
14164 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
14165 SmallVector<SDValue, 8> Chains;
14166 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
14167 TLI.getPointerTy());
14168 SDValue Ptr = St->getBasePtr();
14169
14170 // Perform one or more big stores into memory.
14171 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
14172 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14173 StoreType, ShuffWide,
14174 DAG.getIntPtrConstant(i));
14175 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14176 St->getPointerInfo(), St->isVolatile(),
14177 St->isNonTemporal(), St->getAlignment());
14178 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14179 Chains.push_back(Ch);
14180 }
14181
14182 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14183 Chains.size());
14184 }
14185
14186
Chris Lattner149a4e52008-02-22 02:09:43 +000014187 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14188 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014189 // A preferable solution to the general problem is to figure out the right
14190 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014191
14192 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014193 if (VT.getSizeInBits() != 64)
14194 return SDValue();
14195
Devang Patel578efa92009-06-05 21:57:13 +000014196 const Function *F = DAG.getMachineFunction().getFunction();
14197 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014198 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000014199 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000014200 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014201 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014202 isa<LoadSDNode>(St->getValue()) &&
14203 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14204 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014205 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014206 LoadSDNode *Ld = 0;
14207 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014208 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014209 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014210 // Must be a store of a load. We currently handle two cases: the load
14211 // is a direct child, and it's under an intervening TokenFactor. It is
14212 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014213 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014214 Ld = cast<LoadSDNode>(St->getChain());
14215 else if (St->getValue().hasOneUse() &&
14216 ChainVal->getOpcode() == ISD::TokenFactor) {
14217 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014218 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014219 TokenFactorIndex = i;
14220 Ld = cast<LoadSDNode>(St->getValue());
14221 } else
14222 Ops.push_back(ChainVal->getOperand(i));
14223 }
14224 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014225
Evan Cheng536e6672009-03-12 05:59:15 +000014226 if (!Ld || !ISD::isNormalLoad(Ld))
14227 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014228
Evan Cheng536e6672009-03-12 05:59:15 +000014229 // If this is not the MMX case, i.e. we are just turning i64 load/store
14230 // into f64 load/store, avoid the transformation if there are multiple
14231 // uses of the loaded value.
14232 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14233 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014234
Evan Cheng536e6672009-03-12 05:59:15 +000014235 DebugLoc LdDL = Ld->getDebugLoc();
14236 DebugLoc StDL = N->getDebugLoc();
14237 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14238 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14239 // pair instead.
14240 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014241 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014242 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14243 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014244 Ld->isNonTemporal(), Ld->isInvariant(),
14245 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014246 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014247 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014248 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014249 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014250 Ops.size());
14251 }
Evan Cheng536e6672009-03-12 05:59:15 +000014252 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014253 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014254 St->isVolatile(), St->isNonTemporal(),
14255 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014256 }
Evan Cheng536e6672009-03-12 05:59:15 +000014257
14258 // Otherwise, lower to two pairs of 32-bit loads / stores.
14259 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014260 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14261 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014262
Owen Anderson825b72b2009-08-11 20:47:22 +000014263 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014264 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014265 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014266 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014267 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014268 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014269 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014270 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014271 MinAlign(Ld->getAlignment(), 4));
14272
14273 SDValue NewChain = LoLd.getValue(1);
14274 if (TokenFactorIndex != -1) {
14275 Ops.push_back(LoLd);
14276 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014277 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014278 Ops.size());
14279 }
14280
14281 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014282 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14283 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014284
14285 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014286 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014287 St->isVolatile(), St->isNonTemporal(),
14288 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014289 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014290 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014291 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014292 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014293 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014294 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014295 }
Dan Gohman475871a2008-07-27 21:46:04 +000014296 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014297}
14298
Duncan Sands17470be2011-09-22 20:15:48 +000014299/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14300/// and return the operands for the horizontal operation in LHS and RHS. A
14301/// horizontal operation performs the binary operation on successive elements
14302/// of its first operand, then on successive elements of its second operand,
14303/// returning the resulting values in a vector. For example, if
14304/// A = < float a0, float a1, float a2, float a3 >
14305/// and
14306/// B = < float b0, float b1, float b2, float b3 >
14307/// then the result of doing a horizontal operation on A and B is
14308/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14309/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14310/// A horizontal-op B, for some already available A and B, and if so then LHS is
14311/// set to A, RHS to B, and the routine returns 'true'.
14312/// Note that the binary operation should have the property that if one of the
14313/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014314static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000014315 // Look for the following pattern: if
14316 // A = < float a0, float a1, float a2, float a3 >
14317 // B = < float b0, float b1, float b2, float b3 >
14318 // and
14319 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14320 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14321 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14322 // which is A horizontal-op B.
14323
14324 // At least one of the operands should be a vector shuffle.
14325 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14326 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14327 return false;
14328
14329 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000014330
14331 assert((VT.is128BitVector() || VT.is256BitVector()) &&
14332 "Unsupported vector type for horizontal add/sub");
14333
14334 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
14335 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000014336 unsigned NumElts = VT.getVectorNumElements();
14337 unsigned NumLanes = VT.getSizeInBits()/128;
14338 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000014339 assert((NumLaneElts % 2 == 0) &&
14340 "Vector type should have an even number of elements in each lane");
14341 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000014342
14343 // View LHS in the form
14344 // LHS = VECTOR_SHUFFLE A, B, LMask
14345 // If LHS is not a shuffle then pretend it is the shuffle
14346 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14347 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14348 // type VT.
14349 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000014350 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014351 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14352 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14353 A = LHS.getOperand(0);
14354 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14355 B = LHS.getOperand(1);
14356 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
14357 } else {
14358 if (LHS.getOpcode() != ISD::UNDEF)
14359 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014360 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014361 LMask[i] = i;
14362 }
14363
14364 // Likewise, view RHS in the form
14365 // RHS = VECTOR_SHUFFLE C, D, RMask
14366 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000014367 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014368 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14369 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14370 C = RHS.getOperand(0);
14371 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14372 D = RHS.getOperand(1);
14373 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
14374 } else {
14375 if (RHS.getOpcode() != ISD::UNDEF)
14376 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014377 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014378 RMask[i] = i;
14379 }
14380
14381 // Check that the shuffles are both shuffling the same vectors.
14382 if (!(A == C && B == D) && !(A == D && B == C))
14383 return false;
14384
14385 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14386 if (!A.getNode() && !B.getNode())
14387 return false;
14388
14389 // If A and B occur in reverse order in RHS, then "swap" them (which means
14390 // rewriting the mask).
14391 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000014392 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014393
14394 // At this point LHS and RHS are equivalent to
14395 // LHS = VECTOR_SHUFFLE A, B, LMask
14396 // RHS = VECTOR_SHUFFLE A, B, RMask
14397 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000014398 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000014399 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000014400
Craig Topperf8363302011-12-02 08:18:41 +000014401 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014402 if (LIdx < 0 || RIdx < 0 ||
14403 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
14404 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000014405 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000014406
Craig Topperf8363302011-12-02 08:18:41 +000014407 // Check that successive elements are being operated on. If not, this is
14408 // not a horizontal operation.
14409 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
14410 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000014411 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000014412 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000014413 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000014414 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000014415 }
14416
14417 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14418 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14419 return true;
14420}
14421
14422/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14423static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14424 const X86Subtarget *Subtarget) {
14425 EVT VT = N->getValueType(0);
14426 SDValue LHS = N->getOperand(0);
14427 SDValue RHS = N->getOperand(1);
14428
14429 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topper138a5c62011-12-02 07:16:01 +000014430 if (((Subtarget->hasSSE3orAVX() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
14431 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014432 isHorizontalBinOp(LHS, RHS, true))
14433 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14434 return SDValue();
14435}
14436
14437/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14438static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14439 const X86Subtarget *Subtarget) {
14440 EVT VT = N->getValueType(0);
14441 SDValue LHS = N->getOperand(0);
14442 SDValue RHS = N->getOperand(1);
14443
14444 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topper138a5c62011-12-02 07:16:01 +000014445 if (((Subtarget->hasSSE3orAVX() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
14446 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014447 isHorizontalBinOp(LHS, RHS, false))
14448 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14449 return SDValue();
14450}
14451
Chris Lattner6cf73262008-01-25 06:14:17 +000014452/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14453/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014454static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014455 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14456 // F[X]OR(0.0, x) -> x
14457 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014458 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14459 if (C->getValueAPF().isPosZero())
14460 return N->getOperand(1);
14461 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14462 if (C->getValueAPF().isPosZero())
14463 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014464 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014465}
14466
14467/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014468static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014469 // FAND(0.0, x) -> 0.0
14470 // FAND(x, 0.0) -> 0.0
14471 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14472 if (C->getValueAPF().isPosZero())
14473 return N->getOperand(0);
14474 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14475 if (C->getValueAPF().isPosZero())
14476 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014477 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014478}
14479
Dan Gohmane5af2d32009-01-29 01:59:02 +000014480static SDValue PerformBTCombine(SDNode *N,
14481 SelectionDAG &DAG,
14482 TargetLowering::DAGCombinerInfo &DCI) {
14483 // BT ignores high bits in the bit index operand.
14484 SDValue Op1 = N->getOperand(1);
14485 if (Op1.hasOneUse()) {
14486 unsigned BitWidth = Op1.getValueSizeInBits();
14487 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14488 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014489 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14490 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014491 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014492 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14493 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14494 DCI.CommitTargetLoweringOpt(TLO);
14495 }
14496 return SDValue();
14497}
Chris Lattner83e6c992006-10-04 06:57:07 +000014498
Eli Friedman7a5e5552009-06-07 06:52:44 +000014499static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14500 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014501 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014502 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014503 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014504 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014505 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014506 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014507 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014508 }
14509 return SDValue();
14510}
14511
Evan Cheng2e489c42009-12-16 00:53:11 +000014512static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14513 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14514 // (and (i32 x86isd::setcc_carry), 1)
14515 // This eliminates the zext. This transformation is necessary because
14516 // ISD::SETCC is always legalized to i8.
14517 DebugLoc dl = N->getDebugLoc();
14518 SDValue N0 = N->getOperand(0);
14519 EVT VT = N->getValueType(0);
14520 if (N0.getOpcode() == ISD::AND &&
14521 N0.hasOneUse() &&
14522 N0.getOperand(0).hasOneUse()) {
14523 SDValue N00 = N0.getOperand(0);
14524 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14525 return SDValue();
14526 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14527 if (!C || C->getZExtValue() != 1)
14528 return SDValue();
14529 return DAG.getNode(ISD::AND, dl, VT,
14530 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14531 N00.getOperand(0), N00.getOperand(1)),
14532 DAG.getConstant(1, VT));
14533 }
14534
14535 return SDValue();
14536}
14537
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014538// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14539static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14540 unsigned X86CC = N->getConstantOperandVal(0);
14541 SDValue EFLAG = N->getOperand(1);
14542 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014543
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014544 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14545 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14546 // cases.
14547 if (X86CC == X86::COND_B)
14548 return DAG.getNode(ISD::AND, DL, MVT::i8,
14549 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14550 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14551 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014552
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014553 return SDValue();
14554}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014555
Benjamin Kramer1396c402011-06-18 11:09:41 +000014556static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14557 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014558 SDValue Op0 = N->getOperand(0);
14559 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14560 // a 32-bit target where SSE doesn't support i64->FP operations.
14561 if (Op0.getOpcode() == ISD::LOAD) {
14562 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14563 EVT VT = Ld->getValueType(0);
14564 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14565 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14566 !XTLI->getSubtarget()->is64Bit() &&
14567 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014568 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14569 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014570 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14571 return FILDChain;
14572 }
14573 }
14574 return SDValue();
14575}
14576
Chris Lattner23a01992010-12-20 01:37:09 +000014577// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14578static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14579 X86TargetLowering::DAGCombinerInfo &DCI) {
14580 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14581 // the result is either zero or one (depending on the input carry bit).
14582 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14583 if (X86::isZeroNode(N->getOperand(0)) &&
14584 X86::isZeroNode(N->getOperand(1)) &&
14585 // We don't have a good way to replace an EFLAGS use, so only do this when
14586 // dead right now.
14587 SDValue(N, 1).use_empty()) {
14588 DebugLoc DL = N->getDebugLoc();
14589 EVT VT = N->getValueType(0);
14590 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14591 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14592 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14593 DAG.getConstant(X86::COND_B,MVT::i8),
14594 N->getOperand(2)),
14595 DAG.getConstant(1, VT));
14596 return DCI.CombineTo(N, Res1, CarryOut);
14597 }
14598
14599 return SDValue();
14600}
14601
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014602// fold (add Y, (sete X, 0)) -> adc 0, Y
14603// (add Y, (setne X, 0)) -> sbb -1, Y
14604// (sub (sete X, 0), Y) -> sbb 0, Y
14605// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014606static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014607 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014608
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014609 // Look through ZExts.
14610 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14611 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14612 return SDValue();
14613
14614 SDValue SetCC = Ext.getOperand(0);
14615 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14616 return SDValue();
14617
14618 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14619 if (CC != X86::COND_E && CC != X86::COND_NE)
14620 return SDValue();
14621
14622 SDValue Cmp = SetCC.getOperand(1);
14623 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014624 !X86::isZeroNode(Cmp.getOperand(1)) ||
14625 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014626 return SDValue();
14627
14628 SDValue CmpOp0 = Cmp.getOperand(0);
14629 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14630 DAG.getConstant(1, CmpOp0.getValueType()));
14631
14632 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14633 if (CC == X86::COND_NE)
14634 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14635 DL, OtherVal.getValueType(), OtherVal,
14636 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14637 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14638 DL, OtherVal.getValueType(), OtherVal,
14639 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14640}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014641
Craig Topper54f952a2011-11-19 09:02:40 +000014642/// PerformADDCombine - Do target-specific dag combines on integer adds.
14643static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
14644 const X86Subtarget *Subtarget) {
14645 EVT VT = N->getValueType(0);
14646 SDValue Op0 = N->getOperand(0);
14647 SDValue Op1 = N->getOperand(1);
14648
14649 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperb72039c2011-11-30 09:10:50 +000014650 if (((Subtarget->hasSSSE3orAVX() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
14651 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000014652 isHorizontalBinOp(Op0, Op1, true))
14653 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
14654
14655 return OptimizeConditionalInDecrement(N, DAG);
14656}
14657
14658static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
14659 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014660 SDValue Op0 = N->getOperand(0);
14661 SDValue Op1 = N->getOperand(1);
14662
14663 // X86 can't encode an immediate LHS of a sub. See if we can push the
14664 // negation into a preceding instruction.
14665 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014666 // If the RHS of the sub is a XOR with one use and a constant, invert the
14667 // immediate. Then add one to the LHS of the sub so we can turn
14668 // X-Y -> X+~Y+1, saving one register.
14669 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14670 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014671 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014672 EVT VT = Op0.getValueType();
14673 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14674 Op1.getOperand(0),
14675 DAG.getConstant(~XorC, VT));
14676 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014677 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014678 }
14679 }
14680
Craig Topper54f952a2011-11-19 09:02:40 +000014681 // Try to synthesize horizontal adds from adds of shuffles.
14682 EVT VT = N->getValueType(0);
Craig Topperb72039c2011-11-30 09:10:50 +000014683 if (((Subtarget->hasSSSE3orAVX() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
14684 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
14685 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000014686 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
14687
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014688 return OptimizeConditionalInDecrement(N, DAG);
14689}
14690
Dan Gohman475871a2008-07-27 21:46:04 +000014691SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014692 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014693 SelectionDAG &DAG = DCI.DAG;
14694 switch (N->getOpcode()) {
14695 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014696 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014697 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014698 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014699 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014700 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Craig Topper54f952a2011-11-19 09:02:40 +000014701 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
14702 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000014703 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014704 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014705 case ISD::SHL:
14706 case ISD::SRA:
14707 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014708 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014709 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014710 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014711 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014712 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014713 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014714 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14715 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014716 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014717 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14718 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014719 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014720 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014721 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014722 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014723 case X86ISD::SHUFPS: // Handle all target specific shuffles
14724 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014725 case X86ISD::PALIGN:
Craig Topper06cb6802011-11-26 20:47:44 +000014726 case X86ISD::PUNPCKH:
14727 case X86ISD::UNPCKHP:
14728 case X86ISD::PUNPCKL:
14729 case X86ISD::UNPCKLP:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014730 case X86ISD::MOVHLPS:
14731 case X86ISD::MOVLHPS:
14732 case X86ISD::PSHUFD:
14733 case X86ISD::PSHUFHW:
14734 case X86ISD::PSHUFLW:
14735 case X86ISD::MOVSS:
14736 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000014737 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000014738 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014739 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014740 }
14741
Dan Gohman475871a2008-07-27 21:46:04 +000014742 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014743}
14744
Evan Chenge5b51ac2010-04-17 06:13:15 +000014745/// isTypeDesirableForOp - Return true if the target has native support for
14746/// the specified value type and it is 'desirable' to use the type for the
14747/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14748/// instruction encodings are longer and some i16 instructions are slow.
14749bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14750 if (!isTypeLegal(VT))
14751 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014752 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014753 return true;
14754
14755 switch (Opc) {
14756 default:
14757 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014758 case ISD::LOAD:
14759 case ISD::SIGN_EXTEND:
14760 case ISD::ZERO_EXTEND:
14761 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014762 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014763 case ISD::SRL:
14764 case ISD::SUB:
14765 case ISD::ADD:
14766 case ISD::MUL:
14767 case ISD::AND:
14768 case ISD::OR:
14769 case ISD::XOR:
14770 return false;
14771 }
14772}
14773
14774/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014775/// beneficial for dag combiner to promote the specified node. If true, it
14776/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014777bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014778 EVT VT = Op.getValueType();
14779 if (VT != MVT::i16)
14780 return false;
14781
Evan Cheng4c26e932010-04-19 19:29:22 +000014782 bool Promote = false;
14783 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014784 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014785 default: break;
14786 case ISD::LOAD: {
14787 LoadSDNode *LD = cast<LoadSDNode>(Op);
14788 // If the non-extending load has a single use and it's not live out, then it
14789 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014790 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14791 Op.hasOneUse()*/) {
14792 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14793 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14794 // The only case where we'd want to promote LOAD (rather then it being
14795 // promoted as an operand is when it's only use is liveout.
14796 if (UI->getOpcode() != ISD::CopyToReg)
14797 return false;
14798 }
14799 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014800 Promote = true;
14801 break;
14802 }
14803 case ISD::SIGN_EXTEND:
14804 case ISD::ZERO_EXTEND:
14805 case ISD::ANY_EXTEND:
14806 Promote = true;
14807 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014808 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014809 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014810 SDValue N0 = Op.getOperand(0);
14811 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014812 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014813 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014814 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014815 break;
14816 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014817 case ISD::ADD:
14818 case ISD::MUL:
14819 case ISD::AND:
14820 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014821 case ISD::XOR:
14822 Commute = true;
14823 // fallthrough
14824 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014825 SDValue N0 = Op.getOperand(0);
14826 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014827 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014828 return false;
14829 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014830 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014831 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014832 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014833 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014834 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014835 }
14836 }
14837
14838 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014839 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014840}
14841
Evan Cheng60c07e12006-07-05 22:17:51 +000014842//===----------------------------------------------------------------------===//
14843// X86 Inline Assembly Support
14844//===----------------------------------------------------------------------===//
14845
Chris Lattnerb8105652009-07-20 17:51:36 +000014846bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14847 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014848
14849 std::string AsmStr = IA->getAsmString();
14850
14851 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014852 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014853 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014854
14855 switch (AsmPieces.size()) {
14856 default: return false;
14857 case 1:
14858 AsmStr = AsmPieces[0];
14859 AsmPieces.clear();
14860 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14861
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014862 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014863 // we will turn this bswap into something that will be lowered to logical ops
14864 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14865 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014866 // bswap $0
14867 if (AsmPieces.size() == 2 &&
14868 (AsmPieces[0] == "bswap" ||
14869 AsmPieces[0] == "bswapq" ||
14870 AsmPieces[0] == "bswapl") &&
14871 (AsmPieces[1] == "$0" ||
14872 AsmPieces[1] == "${0:q}")) {
14873 // No need to check constraints, nothing other than the equivalent of
14874 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014875 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014876 if (!Ty || Ty->getBitWidth() % 16 != 0)
14877 return false;
14878 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014879 }
14880 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014881 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014882 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014883 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014884 AsmPieces[1] == "$$8," &&
14885 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014886 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14887 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014888 const std::string &ConstraintsStr = IA->getConstraintString();
14889 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014890 std::sort(AsmPieces.begin(), AsmPieces.end());
14891 if (AsmPieces.size() == 4 &&
14892 AsmPieces[0] == "~{cc}" &&
14893 AsmPieces[1] == "~{dirflag}" &&
14894 AsmPieces[2] == "~{flags}" &&
14895 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014896 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014897 if (!Ty || Ty->getBitWidth() % 16 != 0)
14898 return false;
14899 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014900 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014901 }
14902 break;
14903 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014904 if (CI->getType()->isIntegerTy(32) &&
14905 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14906 SmallVector<StringRef, 4> Words;
14907 SplitString(AsmPieces[0], Words, " \t,");
14908 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14909 Words[2] == "${0:w}") {
14910 Words.clear();
14911 SplitString(AsmPieces[1], Words, " \t,");
14912 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14913 Words[2] == "$0") {
14914 Words.clear();
14915 SplitString(AsmPieces[2], Words, " \t,");
14916 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14917 Words[2] == "${0:w}") {
14918 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014919 const std::string &ConstraintsStr = IA->getConstraintString();
14920 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014921 std::sort(AsmPieces.begin(), AsmPieces.end());
14922 if (AsmPieces.size() == 4 &&
14923 AsmPieces[0] == "~{cc}" &&
14924 AsmPieces[1] == "~{dirflag}" &&
14925 AsmPieces[2] == "~{flags}" &&
14926 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014927 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014928 if (!Ty || Ty->getBitWidth() % 16 != 0)
14929 return false;
14930 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014931 }
14932 }
14933 }
14934 }
14935 }
Evan Cheng55d42002011-01-08 01:24:27 +000014936
14937 if (CI->getType()->isIntegerTy(64)) {
14938 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14939 if (Constraints.size() >= 2 &&
14940 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14941 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14942 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14943 SmallVector<StringRef, 4> Words;
14944 SplitString(AsmPieces[0], Words, " \t");
14945 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014946 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014947 SplitString(AsmPieces[1], Words, " \t");
14948 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14949 Words.clear();
14950 SplitString(AsmPieces[2], Words, " \t,");
14951 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14952 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014953 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014954 if (!Ty || Ty->getBitWidth() % 16 != 0)
14955 return false;
14956 return IntrinsicLowering::LowerToByteSwap(CI);
14957 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014958 }
14959 }
14960 }
14961 }
14962 break;
14963 }
14964 return false;
14965}
14966
14967
14968
Chris Lattnerf4dff842006-07-11 02:54:03 +000014969/// getConstraintType - Given a constraint letter, return the type of
14970/// constraint it is for this target.
14971X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014972X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14973 if (Constraint.size() == 1) {
14974 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014975 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014976 case 'q':
14977 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014978 case 'f':
14979 case 't':
14980 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014981 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014982 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014983 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014984 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014985 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014986 case 'a':
14987 case 'b':
14988 case 'c':
14989 case 'd':
14990 case 'S':
14991 case 'D':
14992 case 'A':
14993 return C_Register;
14994 case 'I':
14995 case 'J':
14996 case 'K':
14997 case 'L':
14998 case 'M':
14999 case 'N':
15000 case 'G':
15001 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000015002 case 'e':
15003 case 'Z':
15004 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000015005 default:
15006 break;
15007 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000015008 }
Chris Lattner4234f572007-03-25 02:14:49 +000015009 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000015010}
15011
John Thompson44ab89e2010-10-29 17:29:13 +000015012/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000015013/// This object must already have been set up with the operand type
15014/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000015015TargetLowering::ConstraintWeight
15016 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000015017 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000015018 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015019 Value *CallOperandVal = info.CallOperandVal;
15020 // If we don't have a value, we can't do a match,
15021 // but allow it at the lowest weight.
15022 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000015023 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015024 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000015025 // Look at the constraint type.
15026 switch (*constraint) {
15027 default:
John Thompson44ab89e2010-10-29 17:29:13 +000015028 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
15029 case 'R':
15030 case 'q':
15031 case 'Q':
15032 case 'a':
15033 case 'b':
15034 case 'c':
15035 case 'd':
15036 case 'S':
15037 case 'D':
15038 case 'A':
15039 if (CallOperandVal->getType()->isIntegerTy())
15040 weight = CW_SpecificReg;
15041 break;
15042 case 'f':
15043 case 't':
15044 case 'u':
15045 if (type->isFloatingPointTy())
15046 weight = CW_SpecificReg;
15047 break;
15048 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000015049 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000015050 weight = CW_SpecificReg;
15051 break;
15052 case 'x':
15053 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015054 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000015055 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015056 break;
15057 case 'I':
15058 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
15059 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000015060 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015061 }
15062 break;
John Thompson44ab89e2010-10-29 17:29:13 +000015063 case 'J':
15064 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15065 if (C->getZExtValue() <= 63)
15066 weight = CW_Constant;
15067 }
15068 break;
15069 case 'K':
15070 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15071 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
15072 weight = CW_Constant;
15073 }
15074 break;
15075 case 'L':
15076 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15077 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
15078 weight = CW_Constant;
15079 }
15080 break;
15081 case 'M':
15082 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15083 if (C->getZExtValue() <= 3)
15084 weight = CW_Constant;
15085 }
15086 break;
15087 case 'N':
15088 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15089 if (C->getZExtValue() <= 0xff)
15090 weight = CW_Constant;
15091 }
15092 break;
15093 case 'G':
15094 case 'C':
15095 if (dyn_cast<ConstantFP>(CallOperandVal)) {
15096 weight = CW_Constant;
15097 }
15098 break;
15099 case 'e':
15100 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15101 if ((C->getSExtValue() >= -0x80000000LL) &&
15102 (C->getSExtValue() <= 0x7fffffffLL))
15103 weight = CW_Constant;
15104 }
15105 break;
15106 case 'Z':
15107 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15108 if (C->getZExtValue() <= 0xffffffff)
15109 weight = CW_Constant;
15110 }
15111 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015112 }
15113 return weight;
15114}
15115
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015116/// LowerXConstraint - try to replace an X constraint, which matches anything,
15117/// with another that has more specific requirements based on the type of the
15118/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000015119const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000015120LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000015121 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
15122 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000015123 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015124 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000015125 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015126 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000015127 return "x";
15128 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015129
Chris Lattner5e764232008-04-26 23:02:14 +000015130 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015131}
15132
Chris Lattner48884cd2007-08-25 00:47:38 +000015133/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
15134/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000015135void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000015136 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000015137 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000015138 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000015139 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000015140
Eric Christopher100c8332011-06-02 23:16:42 +000015141 // Only support length 1 constraints for now.
15142 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000015143
Eric Christopher100c8332011-06-02 23:16:42 +000015144 char ConstraintLetter = Constraint[0];
15145 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015146 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000015147 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000015148 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015149 if (C->getZExtValue() <= 31) {
15150 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015151 break;
15152 }
Devang Patel84f7fd22007-03-17 00:13:28 +000015153 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015154 return;
Evan Cheng364091e2008-09-22 23:57:37 +000015155 case 'J':
15156 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015157 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000015158 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15159 break;
15160 }
15161 }
15162 return;
15163 case 'K':
15164 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015165 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000015166 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15167 break;
15168 }
15169 }
15170 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000015171 case 'N':
15172 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015173 if (C->getZExtValue() <= 255) {
15174 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015175 break;
15176 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000015177 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015178 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000015179 case 'e': {
15180 // 32-bit signed value
15181 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015182 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15183 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015184 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015185 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000015186 break;
15187 }
15188 // FIXME gcc accepts some relocatable values here too, but only in certain
15189 // memory models; it's complicated.
15190 }
15191 return;
15192 }
15193 case 'Z': {
15194 // 32-bit unsigned value
15195 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015196 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15197 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015198 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15199 break;
15200 }
15201 }
15202 // FIXME gcc accepts some relocatable values here too, but only in certain
15203 // memory models; it's complicated.
15204 return;
15205 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015206 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015207 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000015208 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015209 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015210 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000015211 break;
15212 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015213
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015214 // In any sort of PIC mode addresses need to be computed at runtime by
15215 // adding in a register or some sort of table lookup. These can't
15216 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000015217 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015218 return;
15219
Chris Lattnerdc43a882007-05-03 16:52:29 +000015220 // If we are in non-pic codegen mode, we allow the address of a global (with
15221 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000015222 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015223 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000015224
Chris Lattner49921962009-05-08 18:23:14 +000015225 // Match either (GA), (GA+C), (GA+C1+C2), etc.
15226 while (1) {
15227 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15228 Offset += GA->getOffset();
15229 break;
15230 } else if (Op.getOpcode() == ISD::ADD) {
15231 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15232 Offset += C->getZExtValue();
15233 Op = Op.getOperand(0);
15234 continue;
15235 }
15236 } else if (Op.getOpcode() == ISD::SUB) {
15237 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15238 Offset += -C->getZExtValue();
15239 Op = Op.getOperand(0);
15240 continue;
15241 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015242 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015243
Chris Lattner49921962009-05-08 18:23:14 +000015244 // Otherwise, this isn't something we can handle, reject it.
15245 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015246 }
Eric Christopherfd179292009-08-27 18:07:15 +000015247
Dan Gohman46510a72010-04-15 01:51:59 +000015248 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015249 // If we require an extra load to get this address, as in PIC mode, we
15250 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015251 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15252 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015253 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015254
Devang Patel0d881da2010-07-06 22:08:15 +000015255 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15256 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015257 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015258 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015259 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015260
Gabor Greifba36cb52008-08-28 21:40:38 +000015261 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015262 Ops.push_back(Result);
15263 return;
15264 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015265 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015266}
15267
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015268std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015269X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015270 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015271 // First, see if this is a constraint that directly corresponds to an LLVM
15272 // register class.
15273 if (Constraint.size() == 1) {
15274 // GCC Constraint Letters
15275 switch (Constraint[0]) {
15276 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015277 // TODO: Slight differences here in allocation order and leaving
15278 // RIP in the class. Do they matter any more here than they do
15279 // in the normal allocation?
15280 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15281 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015282 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015283 return std::make_pair(0U, X86::GR32RegisterClass);
15284 else if (VT == MVT::i16)
15285 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015286 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015287 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015288 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015289 return std::make_pair(0U, X86::GR64RegisterClass);
15290 break;
15291 }
15292 // 32-bit fallthrough
15293 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015294 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015295 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
15296 else if (VT == MVT::i16)
15297 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015298 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015299 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
15300 else if (VT == MVT::i64)
15301 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15302 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015303 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015304 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015305 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015306 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015307 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015308 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015309 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015310 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015311 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015312 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015313 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015314 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15315 if (VT == MVT::i16)
15316 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15317 if (VT == MVT::i32 || !Subtarget->is64Bit())
15318 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15319 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015320 case 'f': // FP Stack registers.
15321 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15322 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015323 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015324 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015325 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015326 return std::make_pair(0U, X86::RFP64RegisterClass);
15327 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015328 case 'y': // MMX_REGS if MMX allowed.
15329 if (!Subtarget->hasMMX()) break;
15330 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015331 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015332 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015333 // FALL THROUGH.
15334 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015335 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015336
Owen Anderson825b72b2009-08-11 20:47:22 +000015337 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015338 default: break;
15339 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015340 case MVT::f32:
15341 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015342 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015343 case MVT::f64:
15344 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015345 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015346 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015347 case MVT::v16i8:
15348 case MVT::v8i16:
15349 case MVT::v4i32:
15350 case MVT::v2i64:
15351 case MVT::v4f32:
15352 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015353 return std::make_pair(0U, X86::VR128RegisterClass);
15354 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015355 break;
15356 }
15357 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015358
Chris Lattnerf76d1802006-07-31 23:26:50 +000015359 // Use the default implementation in TargetLowering to convert the register
15360 // constraint into a member of a register class.
15361 std::pair<unsigned, const TargetRegisterClass*> Res;
15362 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015363
15364 // Not found as a standard register?
15365 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015366 // Map st(0) -> st(7) -> ST0
15367 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15368 tolower(Constraint[1]) == 's' &&
15369 tolower(Constraint[2]) == 't' &&
15370 Constraint[3] == '(' &&
15371 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15372 Constraint[5] == ')' &&
15373 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015374
Chris Lattner56d77c72009-09-13 22:41:48 +000015375 Res.first = X86::ST0+Constraint[4]-'0';
15376 Res.second = X86::RFP80RegisterClass;
15377 return Res;
15378 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015379
Chris Lattner56d77c72009-09-13 22:41:48 +000015380 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015381 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015382 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015383 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015384 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015385 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015386
15387 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015388 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015389 Res.first = X86::EFLAGS;
15390 Res.second = X86::CCRRegisterClass;
15391 return Res;
15392 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015393
Dale Johannesen330169f2008-11-13 21:52:36 +000015394 // 'A' means EAX + EDX.
15395 if (Constraint == "A") {
15396 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015397 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015398 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015399 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015400 return Res;
15401 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015402
Chris Lattnerf76d1802006-07-31 23:26:50 +000015403 // Otherwise, check to see if this is a register class of the wrong value
15404 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15405 // turn into {ax},{dx}.
15406 if (Res.second->hasType(VT))
15407 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015408
Chris Lattnerf76d1802006-07-31 23:26:50 +000015409 // All of the single-register GCC register classes map their values onto
15410 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15411 // really want an 8-bit or 32-bit register, map to the appropriate register
15412 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015413 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015414 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015415 unsigned DestReg = 0;
15416 switch (Res.first) {
15417 default: break;
15418 case X86::AX: DestReg = X86::AL; break;
15419 case X86::DX: DestReg = X86::DL; break;
15420 case X86::CX: DestReg = X86::CL; break;
15421 case X86::BX: DestReg = X86::BL; break;
15422 }
15423 if (DestReg) {
15424 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015425 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015426 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015427 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015428 unsigned DestReg = 0;
15429 switch (Res.first) {
15430 default: break;
15431 case X86::AX: DestReg = X86::EAX; break;
15432 case X86::DX: DestReg = X86::EDX; break;
15433 case X86::CX: DestReg = X86::ECX; break;
15434 case X86::BX: DestReg = X86::EBX; break;
15435 case X86::SI: DestReg = X86::ESI; break;
15436 case X86::DI: DestReg = X86::EDI; break;
15437 case X86::BP: DestReg = X86::EBP; break;
15438 case X86::SP: DestReg = X86::ESP; break;
15439 }
15440 if (DestReg) {
15441 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015442 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015443 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015444 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015445 unsigned DestReg = 0;
15446 switch (Res.first) {
15447 default: break;
15448 case X86::AX: DestReg = X86::RAX; break;
15449 case X86::DX: DestReg = X86::RDX; break;
15450 case X86::CX: DestReg = X86::RCX; break;
15451 case X86::BX: DestReg = X86::RBX; break;
15452 case X86::SI: DestReg = X86::RSI; break;
15453 case X86::DI: DestReg = X86::RDI; break;
15454 case X86::BP: DestReg = X86::RBP; break;
15455 case X86::SP: DestReg = X86::RSP; break;
15456 }
15457 if (DestReg) {
15458 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015459 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015460 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015461 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015462 } else if (Res.second == X86::FR32RegisterClass ||
15463 Res.second == X86::FR64RegisterClass ||
15464 Res.second == X86::VR128RegisterClass) {
15465 // Handle references to XMM physical registers that got mapped into the
15466 // wrong class. This can happen with constraints like {xmm0} where the
15467 // target independent register mapper will just pick the first match it can
15468 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015469 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015470 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015471 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015472 Res.second = X86::FR64RegisterClass;
15473 else if (X86::VR128RegisterClass->hasType(VT))
15474 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015475 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015476
Chris Lattnerf76d1802006-07-31 23:26:50 +000015477 return Res;
15478}