blob: a7bdb9993361f352207789fe8c687eef76365688 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000043#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000044#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000045#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000047#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000048#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000049#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000050#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000053#include "llvm/Target/TargetOptions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000055using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000056
Evan Chengb1712452010-01-27 06:25:16 +000057STATISTIC(NumTailCalls, "Number of tail calls");
58
Evan Cheng10e86422008-04-25 19:11:04 +000059// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000060static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000061 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000062
David Greenea5f26012011-02-07 19:36:54 +000063static SDValue Insert128BitVector(SDValue Result,
64 SDValue Vec,
65 SDValue Idx,
66 SelectionDAG &DAG,
67 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000068
David Greenea5f26012011-02-07 19:36:54 +000069static SDValue Extract128BitVector(SDValue Vec,
70 SDValue Idx,
71 SelectionDAG &DAG,
72 DebugLoc dl);
73
74/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
75/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000076/// simple subregister reference. Idx is an index in the 128 bits we
77/// want. It need not be aligned to a 128-bit bounday. That makes
78/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000079static SDValue Extract128BitVector(SDValue Vec,
80 SDValue Idx,
81 SelectionDAG &DAG,
82 DebugLoc dl) {
83 EVT VT = Vec.getValueType();
84 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000085 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000086 int Factor = VT.getSizeInBits()/128;
87 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
88 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000089
90 // Extract from UNDEF is UNDEF.
91 if (Vec.getOpcode() == ISD::UNDEF)
92 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
93
94 if (isa<ConstantSDNode>(Idx)) {
95 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
96
97 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
98 // we can match to VEXTRACTF128.
99 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
100
101 // This is the index of the first element of the 128-bit chunk
102 // we want.
103 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
104 * ElemsPerChunk);
105
106 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000107 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
108 VecIdx);
109
110 return Result;
111 }
112
113 return SDValue();
114}
115
116/// Generate a DAG to put 128-bits into a vector > 128 bits. This
117/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000118/// simple superregister reference. Idx is an index in the 128 bits
119/// we want. It need not be aligned to a 128-bit bounday. That makes
120/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000121static SDValue Insert128BitVector(SDValue Result,
122 SDValue Vec,
123 SDValue Idx,
124 SelectionDAG &DAG,
125 DebugLoc dl) {
126 if (isa<ConstantSDNode>(Idx)) {
127 EVT VT = Vec.getValueType();
128 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
129
130 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000131 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000132 EVT ResultVT = Result.getValueType();
133
134 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000135 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000136
137 // This is the index of the first element of the 128-bit chunk
138 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000139 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000140 * ElemsPerChunk);
141
142 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000143 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
144 VecIdx);
145 return Result;
146 }
147
148 return SDValue();
149}
150
Chris Lattnerf0144122009-07-28 03:13:23 +0000151static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000152 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
153 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000154
Evan Cheng2bffee22011-02-01 01:14:13 +0000155 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000156 if (is64Bit)
157 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000158 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000159 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000160
Evan Cheng203576a2011-07-20 19:50:42 +0000161 if (Subtarget->isTargetELF())
162 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000163 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000164 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000165 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000166}
167
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000168X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000169 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000170 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000171 X86ScalarSSEf64 = Subtarget->hasXMMInt();
172 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000173 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000174
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000175 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000176 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000177
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000178 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000179 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000180
181 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000182 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000183 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
184 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000185
Eric Christopherde5e1012011-03-11 01:05:58 +0000186 // For 64-bit since we have so many registers use the ILP scheduler, for
187 // 32-bit code use the register pressure specific scheduling.
188 if (Subtarget->is64Bit())
189 setSchedulingPreference(Sched::ILP);
190 else
191 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000192 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000193
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000194 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000195 // Setup Windows compiler runtime calls.
196 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000197 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000198 setLibcallName(RTLIB::SREM_I64, "_allrem");
199 setLibcallName(RTLIB::UREM_I64, "_aullrem");
200 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000201 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000202 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000203 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000204 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000205 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
206 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000208 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
209 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000210 }
211
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000212 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000213 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000214 setUseUnderscoreSetJmp(false);
215 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000216 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000217 // MS runtime is weird: it exports _setjmp, but longjmp!
218 setUseUnderscoreSetJmp(true);
219 setUseUnderscoreLongJmp(false);
220 } else {
221 setUseUnderscoreSetJmp(true);
222 setUseUnderscoreLongJmp(true);
223 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000224
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000227 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000231
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000233
Scott Michelfdc40a02009-02-17 22:15:04 +0000234 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000236 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000238 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
240 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000241
242 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
244 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
246 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000249
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000250 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
251 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
253 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000255
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
258 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000259 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000260 // We have an algorithm for SSE2->double, and we turn this into a
261 // 64-bit FILD followed by conditional FADD for other targets.
262 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000263 // We have an algorithm for SSE2, and we turn this into a 64-bit
264 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000265 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000266 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000267
268 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
269 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
271 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000272
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000273 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000274 // SSE has no i16 to fp conversion, only i32
275 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000277 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000279 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
281 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000282 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000283 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
285 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000286 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000287
Dale Johannesen73328d12007-09-19 23:55:34 +0000288 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
289 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
291 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000292
Evan Cheng02568ff2006-01-30 22:13:22 +0000293 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
294 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
296 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000297
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000298 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000300 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000302 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
304 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000305 }
306
307 // Handle FP_TO_UINT by promoting the destination to a larger signed
308 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
310 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000312
Evan Cheng25ab6902006-09-08 06:48:29 +0000313 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
315 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000316 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000317 // Since AVX is a superset of SSE3, only check for SSE here.
318 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000319 // Expand FP_TO_UINT into a select.
320 // FIXME: We would like to use a Custom expander here eventually to do
321 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000323 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000324 // With SSE3 we can use fisttpll to convert to a signed i64; without
325 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000327 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000328
Chris Lattner399610a2006-12-05 18:22:22 +0000329 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000330 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000331 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
332 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000333 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000335 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000336 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000337 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000338 }
Chris Lattner21f66852005-12-23 05:15:23 +0000339
Dan Gohmanb00ee212008-02-18 19:34:53 +0000340 // Scalar integer divide and remainder are lowered to use operations that
341 // produce two results, to match the available instructions. This exposes
342 // the two-result form to trivial CSE, which is able to combine x/y and x%y
343 // into a single instruction.
344 //
345 // Scalar integer multiply-high is also lowered to use two-result
346 // operations, to match the available instructions. However, plain multiply
347 // (low) operations are left as Legal, as there are single-result
348 // instructions for this in x86. Using the two-result multiply instructions
349 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000350 for (unsigned i = 0, e = 4; i != e; ++i) {
351 MVT VT = IntVTs[i];
352 setOperationAction(ISD::MULHS, VT, Expand);
353 setOperationAction(ISD::MULHU, VT, Expand);
354 setOperationAction(ISD::SDIV, VT, Expand);
355 setOperationAction(ISD::UDIV, VT, Expand);
356 setOperationAction(ISD::SREM, VT, Expand);
357 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000358
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000359 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000360 setOperationAction(ISD::ADDC, VT, Custom);
361 setOperationAction(ISD::ADDE, VT, Custom);
362 setOperationAction(ISD::SUBC, VT, Custom);
363 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000364 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000365
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
367 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
368 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
369 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000370 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
375 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
376 setOperationAction(ISD::FREM , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f64 , Expand);
378 setOperationAction(ISD::FREM , MVT::f80 , Expand);
379 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000380
Craig Topper909652f2011-10-14 03:21:46 +0000381 if (Subtarget->hasBMI()) {
382 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
383 } else {
384 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
385 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
386 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
387 if (Subtarget->is64Bit())
388 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
389 }
Craig Topper37f21672011-10-11 06:44:02 +0000390
391 if (Subtarget->hasLZCNT()) {
392 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
393 } else {
394 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
395 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
396 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
397 if (Subtarget->is64Bit())
398 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000399 }
400
Benjamin Kramer1292c222010-12-04 20:32:23 +0000401 if (Subtarget->hasPOPCNT()) {
402 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
403 } else {
404 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
405 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
406 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
407 if (Subtarget->is64Bit())
408 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
409 }
410
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
412 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000413
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000414 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000415 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000416 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000417 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000418 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
420 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
421 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
422 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
423 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000424 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
426 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
427 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
428 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000429 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000431 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000432 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000434
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000435 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
437 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
438 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
439 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000440 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
442 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000443 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000444 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
446 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
447 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
448 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000449 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000450 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000451 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
453 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
454 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000455 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
457 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
458 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000459 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000460
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000461 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000463
Eric Christopher9a9d2752010-07-22 02:48:34 +0000464 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000465 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000466
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000467 // On X86 and X86-64, atomic operations are lowered to locked instructions.
468 // Locked instructions, in turn, have implicit fence semantics (all memory
469 // operations are flushed before issuing the locked instruction, and they
470 // are not buffered), so we can fold away the common pattern of
471 // fence-atomic-fence.
472 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000473
Mon P Wang63307c32008-05-05 19:05:59 +0000474 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000475 for (unsigned i = 0, e = 4; i != e; ++i) {
476 MVT VT = IntVTs[i];
477 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
478 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000479 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000480 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000481
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000482 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000483 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
485 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
486 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
487 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
488 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
489 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
490 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000491 }
492
Eli Friedman43f51ae2011-08-26 21:21:21 +0000493 if (Subtarget->hasCmpxchg16b()) {
494 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
495 }
496
Evan Cheng3c992d22006-03-07 02:02:57 +0000497 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000498 if (!Subtarget->isTargetDarwin() &&
499 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000500 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000502 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000503
Owen Anderson825b72b2009-08-11 20:47:22 +0000504 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
505 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
506 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
507 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000508 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000509 setExceptionPointerRegister(X86::RAX);
510 setExceptionSelectorRegister(X86::RDX);
511 } else {
512 setExceptionPointerRegister(X86::EAX);
513 setExceptionSelectorRegister(X86::EDX);
514 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
516 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000517
Duncan Sands4a544a72011-09-06 13:37:06 +0000518 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
519 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000520
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000522
Nate Begemanacc398c2006-01-25 18:21:52 +0000523 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000524 setOperationAction(ISD::VASTART , MVT::Other, Custom);
525 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000526 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 setOperationAction(ISD::VAARG , MVT::Other, Custom);
528 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000529 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::VAARG , MVT::Other, Expand);
531 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000532 }
Evan Chengae642192007-03-02 23:16:35 +0000533
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
535 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000536
537 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
538 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
539 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000540 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000541 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
542 MVT::i64 : MVT::i32, Custom);
543 else
544 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
545 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000546
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000547 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000548 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000549 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
551 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000552
Evan Cheng223547a2006-01-31 22:28:30 +0000553 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 setOperationAction(ISD::FABS , MVT::f64, Custom);
555 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000556
557 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 setOperationAction(ISD::FNEG , MVT::f64, Custom);
559 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000560
Evan Cheng68c47cb2007-01-05 07:55:56 +0000561 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
563 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000564
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000565 // Lower this to FGETSIGNx86 plus an AND.
566 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
567 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
568
Evan Chengd25e9e82006-02-02 00:28:23 +0000569 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setOperationAction(ISD::FSIN , MVT::f64, Expand);
571 setOperationAction(ISD::FCOS , MVT::f64, Expand);
572 setOperationAction(ISD::FSIN , MVT::f32, Expand);
573 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000574
Chris Lattnera54aa942006-01-29 06:26:08 +0000575 // Expand FP immediates into loads from the stack, except for the special
576 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000577 addLegalFPImmediate(APFloat(+0.0)); // xorpd
578 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000579 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000580 // Use SSE for f32, x87 for f64.
581 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
583 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000584
585 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000587
588 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000590
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
593 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
595 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000596
597 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::FSIN , MVT::f32, Expand);
599 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600
Nate Begemane1795842008-02-14 08:57:00 +0000601 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602 addLegalFPImmediate(APFloat(+0.0f)); // xorps
603 addLegalFPImmediate(APFloat(+0.0)); // FLD0
604 addLegalFPImmediate(APFloat(+1.0)); // FLD1
605 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
606 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
607
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000608 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
610 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000611 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000612 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000613 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
616 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000617
Owen Anderson825b72b2009-08-11 20:47:22 +0000618 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
619 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
620 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
621 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000622
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000623 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
625 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000627 addLegalFPImmediate(APFloat(+0.0)); // FLD0
628 addLegalFPImmediate(APFloat(+1.0)); // FLD1
629 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
630 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000631 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
632 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
633 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
634 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000635 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000636
Cameron Zwarich33390842011-07-08 21:39:21 +0000637 // We don't support FMA.
638 setOperationAction(ISD::FMA, MVT::f64, Expand);
639 setOperationAction(ISD::FMA, MVT::f32, Expand);
640
Dale Johannesen59a58732007-08-05 18:49:15 +0000641 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000642 if (!TM.Options.UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
644 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
645 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000646 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000647 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000648 addLegalFPImmediate(TmpFlt); // FLD0
649 TmpFlt.changeSign();
650 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000651
652 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000653 APFloat TmpFlt2(+1.0);
654 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
655 &ignored);
656 addLegalFPImmediate(TmpFlt2); // FLD1
657 TmpFlt2.changeSign();
658 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
659 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000660
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000661 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
663 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000664 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000665
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000666 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
667 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
668 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
669 setOperationAction(ISD::FRINT, MVT::f80, Expand);
670 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000671 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000672 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000673
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000674 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
676 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
677 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000678
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 setOperationAction(ISD::FLOG, MVT::f80, Expand);
680 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
681 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
682 setOperationAction(ISD::FEXP, MVT::f80, Expand);
683 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000684
Mon P Wangf007a8b2008-11-06 05:31:54 +0000685 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000686 // (for widening) or expand (for scalarization). Then we will selectively
687 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000688 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
689 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
690 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000706 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
707 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000708 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000729 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000730 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000739 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000740 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000744 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000745 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
746 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
747 setTruncStoreAction((MVT::SimpleValueType)VT,
748 (MVT::SimpleValueType)InnerVT, Expand);
749 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
750 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
751 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000752 }
753
Evan Chengc7ce29b2009-02-13 22:36:38 +0000754 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
755 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000756 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000757 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000758 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000759 }
760
Dale Johannesen0488fb62010-09-30 23:57:10 +0000761 // MMX-sized vectors (other than x86mmx) are expected to be expanded
762 // into smaller operations.
763 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
764 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
765 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
766 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
767 setOperationAction(ISD::AND, MVT::v8i8, Expand);
768 setOperationAction(ISD::AND, MVT::v4i16, Expand);
769 setOperationAction(ISD::AND, MVT::v2i32, Expand);
770 setOperationAction(ISD::AND, MVT::v1i64, Expand);
771 setOperationAction(ISD::OR, MVT::v8i8, Expand);
772 setOperationAction(ISD::OR, MVT::v4i16, Expand);
773 setOperationAction(ISD::OR, MVT::v2i32, Expand);
774 setOperationAction(ISD::OR, MVT::v1i64, Expand);
775 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
776 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
777 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
778 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
779 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
780 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
781 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
782 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
783 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
784 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
785 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
786 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
787 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000788 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
789 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
790 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
791 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000792
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000793 if (!TM.Options.UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000795
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
797 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
798 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
799 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
800 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
801 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
802 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
803 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
804 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
805 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
806 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000807 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000808 }
809
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000810 if (!TM.Options.UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000812
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000813 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
814 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
816 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
817 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
818 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000819
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
821 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
822 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
823 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
824 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
825 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
826 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
827 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
828 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
829 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
830 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
831 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
832 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
833 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
834 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
835 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000836
Nadav Rotem354efd82011-09-18 14:57:03 +0000837 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000838 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
839 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
840 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000841
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
843 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
844 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
845 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
846 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000847
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000848 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
849 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
850 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
851 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
852 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
853
Evan Cheng2c3ae372006-04-12 21:21:57 +0000854 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000855 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
856 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000857 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000858 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000859 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000860 // Do not attempt to custom lower non-128-bit vectors
861 if (!VT.is128BitVector())
862 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 setOperationAction(ISD::BUILD_VECTOR,
864 VT.getSimpleVT().SimpleTy, Custom);
865 setOperationAction(ISD::VECTOR_SHUFFLE,
866 VT.getSimpleVT().SimpleTy, Custom);
867 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
868 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000869 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000870
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
872 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
873 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
874 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000877
Nate Begemancdd1eec2008-02-12 22:51:28 +0000878 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
880 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000881 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000882
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000883 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
885 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000886 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000887
888 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000889 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000890 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000891
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000894 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000896 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000898 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000900 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000902 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000903
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000905
Evan Cheng2c3ae372006-04-12 21:21:57 +0000906 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
908 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
909 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
910 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000911
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
913 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000914 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000915
Craig Topperc0d82852011-11-22 00:44:41 +0000916 if (Subtarget->hasSSE41orAVX()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000917 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
918 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
919 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
920 setOperationAction(ISD::FRINT, MVT::f32, Legal);
921 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
922 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
923 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
924 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
925 setOperationAction(ISD::FRINT, MVT::f64, Legal);
926 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
927
Nate Begeman14d12ca2008-02-11 04:19:36 +0000928 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000929 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000930
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000931 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
932 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
933 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
934 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
935 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000936
Nate Begeman14d12ca2008-02-11 04:19:36 +0000937 // i8 and i16 vectors are custom , because the source register and source
938 // source memory operand types are not the same width. f32 vectors are
939 // custom since the immediate controlling the insert encodes additional
940 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
942 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
943 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
944 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000945
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
947 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
948 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
949 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000950
Pete Coopera77214a2011-11-14 19:38:42 +0000951 // FIXME: these should be Legal but thats only for the case where
952 // the index is constant. For now custom expand to deal with that
Nate Begeman14d12ca2008-02-11 04:19:36 +0000953 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000954 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
955 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000956 }
957 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000958
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000959 if (Subtarget->hasXMMInt()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000960 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000961 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000962
Nadav Rotem43012222011-05-11 08:12:09 +0000963 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000964 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000965
Nadav Rotem43012222011-05-11 08:12:09 +0000966 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000967 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000968
969 if (Subtarget->hasAVX2()) {
970 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
971 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
972
973 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
974 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
975
976 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
977 } else {
978 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
979 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
980
981 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
982 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
983
984 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
985 }
Nadav Rotem43012222011-05-11 08:12:09 +0000986 }
987
Craig Topperc0d82852011-11-22 00:44:41 +0000988 if (Subtarget->hasSSE42orAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000989 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000990
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000991 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000992 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
993 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
994 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
995 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
996 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
997 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000998
Owen Anderson825b72b2009-08-11 20:47:22 +0000999 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001000 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1001 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001002
Owen Anderson825b72b2009-08-11 20:47:22 +00001003 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1004 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1005 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1006 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1007 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
1008 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001009
Owen Anderson825b72b2009-08-11 20:47:22 +00001010 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1011 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1012 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1013 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1014 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1015 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001016
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001017 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1018 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001019 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001020
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00001021 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
1022 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
1023 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
1024 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
1025 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1026 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1027
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001028 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1029 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1030
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001031 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1032 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1033
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001034 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001035 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001036
Duncan Sands28b77e92011-09-06 19:07:46 +00001037 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1038 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1039 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1040 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001041
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001042 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1043 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1044 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1045
Craig Topperaaa643c2011-11-09 07:28:55 +00001046 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1047 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1048 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1049 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001050
Craig Topperaaa643c2011-11-09 07:28:55 +00001051 if (Subtarget->hasAVX2()) {
1052 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1053 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1054 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1055 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001056
Craig Topperaaa643c2011-11-09 07:28:55 +00001057 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1058 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1059 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1060 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001061
Craig Topperaaa643c2011-11-09 07:28:55 +00001062 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1063 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1064 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001065 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001066
1067 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001068
1069 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1070 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1071
1072 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1073 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1074
1075 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001076 } else {
1077 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1078 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1079 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1080 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1081
1082 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1083 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1084 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1085 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1086
1087 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1088 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1089 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1090 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001091
1092 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1093 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1094
1095 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1096 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1097
1098 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001099 }
Craig Topper13894fa2011-08-24 06:14:18 +00001100
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001101 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001102 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001103 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1104 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1105 EVT VT = SVT;
1106
1107 // Extract subvector is special because the value type
1108 // (result) is 128-bit but the source is 256-bit wide.
1109 if (VT.is128BitVector())
1110 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1111
1112 // Do not attempt to custom lower other non-256-bit vectors
1113 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001114 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001115
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001116 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1117 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1118 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1119 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001120 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001121 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001122 }
1123
David Greene54d8eba2011-01-27 22:38:56 +00001124 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001125 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1126 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1127 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001128
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001129 // Do not attempt to promote non-256-bit vectors
1130 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001131 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001132
1133 setOperationAction(ISD::AND, SVT, Promote);
1134 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1135 setOperationAction(ISD::OR, SVT, Promote);
1136 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1137 setOperationAction(ISD::XOR, SVT, Promote);
1138 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1139 setOperationAction(ISD::LOAD, SVT, Promote);
1140 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1141 setOperationAction(ISD::SELECT, SVT, Promote);
1142 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001143 }
David Greene9b9838d2009-06-29 16:47:10 +00001144 }
1145
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001146 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1147 // of this type with custom code.
1148 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1149 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1150 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1151 }
1152
Evan Cheng6be2c582006-04-05 23:38:46 +00001153 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001154 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001155
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001156
Eli Friedman962f5492010-06-02 19:35:46 +00001157 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1158 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001159 //
Eli Friedman962f5492010-06-02 19:35:46 +00001160 // FIXME: We really should do custom legalization for addition and
1161 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1162 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001163 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1164 // Add/Sub/Mul with overflow operations are custom lowered.
1165 MVT VT = IntVTs[i];
1166 setOperationAction(ISD::SADDO, VT, Custom);
1167 setOperationAction(ISD::UADDO, VT, Custom);
1168 setOperationAction(ISD::SSUBO, VT, Custom);
1169 setOperationAction(ISD::USUBO, VT, Custom);
1170 setOperationAction(ISD::SMULO, VT, Custom);
1171 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001172 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001173
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001174 // There are no 8-bit 3-address imul/mul instructions
1175 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1176 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001177
Evan Chengd54f2d52009-03-31 19:38:51 +00001178 if (!Subtarget->is64Bit()) {
1179 // These libcalls are not available in 32-bit.
1180 setLibcallName(RTLIB::SHL_I128, 0);
1181 setLibcallName(RTLIB::SRL_I128, 0);
1182 setLibcallName(RTLIB::SRA_I128, 0);
1183 }
1184
Evan Cheng206ee9d2006-07-07 08:33:52 +00001185 // We have target-specific dag combine patterns for the following nodes:
1186 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001187 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001188 setTargetDAGCombine(ISD::BUILD_VECTOR);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001189 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001190 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001191 setTargetDAGCombine(ISD::SHL);
1192 setTargetDAGCombine(ISD::SRA);
1193 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001194 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001195 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001196 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001197 setTargetDAGCombine(ISD::FADD);
1198 setTargetDAGCombine(ISD::FSUB);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001199 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001200 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001201 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001202 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001203 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001204 if (Subtarget->is64Bit())
1205 setTargetDAGCombine(ISD::MUL);
Craig Topperb4c94572011-10-21 06:55:01 +00001206 if (Subtarget->hasBMI())
1207 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001208
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001209 computeRegisterProperties();
1210
Evan Cheng05219282011-01-06 06:52:41 +00001211 // On Darwin, -Os means optimize for size without hurting performance,
1212 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001213 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001214 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001215 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001216 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1217 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1218 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001219 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001220 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001221
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001222 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001223}
1224
Scott Michel5b8f82e2008-03-10 15:42:14 +00001225
Duncan Sands28b77e92011-09-06 19:07:46 +00001226EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1227 if (!VT.isVector()) return MVT::i8;
1228 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001229}
1230
1231
Evan Cheng29286502008-01-23 23:17:41 +00001232/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1233/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001234static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001235 if (MaxAlign == 16)
1236 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001237 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001238 if (VTy->getBitWidth() == 128)
1239 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001240 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001241 unsigned EltAlign = 0;
1242 getMaxByValAlign(ATy->getElementType(), EltAlign);
1243 if (EltAlign > MaxAlign)
1244 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001245 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001246 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1247 unsigned EltAlign = 0;
1248 getMaxByValAlign(STy->getElementType(i), EltAlign);
1249 if (EltAlign > MaxAlign)
1250 MaxAlign = EltAlign;
1251 if (MaxAlign == 16)
1252 break;
1253 }
1254 }
1255 return;
1256}
1257
1258/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1259/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001260/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1261/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001262unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001263 if (Subtarget->is64Bit()) {
1264 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001265 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001266 if (TyAlign > 8)
1267 return TyAlign;
1268 return 8;
1269 }
1270
Evan Cheng29286502008-01-23 23:17:41 +00001271 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001272 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001273 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001274 return Align;
1275}
Chris Lattner2b02a442007-02-25 08:29:00 +00001276
Evan Chengf0df0312008-05-15 08:39:06 +00001277/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001278/// and store operations as a result of memset, memcpy, and memmove
1279/// lowering. If DstAlign is zero that means it's safe to destination
1280/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1281/// means there isn't a need to check it against alignment requirement,
1282/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001283/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001284/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1285/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1286/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001287/// It returns EVT::Other if the type should be determined using generic
1288/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001289EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001290X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1291 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001292 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001293 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001294 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001295 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1296 // linux. This is because the stack realignment code can't handle certain
1297 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001298 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001299 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001300 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001301 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001302 (Subtarget->isUnalignedMemAccessFast() ||
1303 ((DstAlign == 0 || DstAlign >= 16) &&
1304 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001305 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001306 if (Subtarget->hasAVX() &&
1307 Subtarget->getStackAlignment() >= 32)
1308 return MVT::v8f32;
1309 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001310 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001311 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001312 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001313 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001314 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001315 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001316 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001317 // Do not use f64 to lower memcpy if source is string constant. It's
1318 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001319 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001320 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001321 }
Evan Chengf0df0312008-05-15 08:39:06 +00001322 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001323 return MVT::i64;
1324 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001325}
1326
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001327/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1328/// current function. The returned value is a member of the
1329/// MachineJumpTableInfo::JTEntryKind enum.
1330unsigned X86TargetLowering::getJumpTableEncoding() const {
1331 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1332 // symbol.
1333 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1334 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001335 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001336
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001337 // Otherwise, use the normal jump table encoding heuristics.
1338 return TargetLowering::getJumpTableEncoding();
1339}
1340
Chris Lattnerc64daab2010-01-26 05:02:42 +00001341const MCExpr *
1342X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1343 const MachineBasicBlock *MBB,
1344 unsigned uid,MCContext &Ctx) const{
1345 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1346 Subtarget->isPICStyleGOT());
1347 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1348 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001349 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1350 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001351}
1352
Evan Chengcc415862007-11-09 01:32:10 +00001353/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1354/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001355SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001356 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001357 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001358 // This doesn't have DebugLoc associated with it, but is not really the
1359 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001360 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001361 return Table;
1362}
1363
Chris Lattner589c6f62010-01-26 06:28:43 +00001364/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1365/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1366/// MCExpr.
1367const MCExpr *X86TargetLowering::
1368getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1369 MCContext &Ctx) const {
1370 // X86-64 uses RIP relative addressing based on the jump table label.
1371 if (Subtarget->isPICStyleRIPRel())
1372 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1373
1374 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001375 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001376}
1377
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001378// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001379std::pair<const TargetRegisterClass*, uint8_t>
1380X86TargetLowering::findRepresentativeClass(EVT VT) const{
1381 const TargetRegisterClass *RRC = 0;
1382 uint8_t Cost = 1;
1383 switch (VT.getSimpleVT().SimpleTy) {
1384 default:
1385 return TargetLowering::findRepresentativeClass(VT);
1386 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1387 RRC = (Subtarget->is64Bit()
1388 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1389 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001390 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001391 RRC = X86::VR64RegisterClass;
1392 break;
1393 case MVT::f32: case MVT::f64:
1394 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1395 case MVT::v4f32: case MVT::v2f64:
1396 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1397 case MVT::v4f64:
1398 RRC = X86::VR128RegisterClass;
1399 break;
1400 }
1401 return std::make_pair(RRC, Cost);
1402}
1403
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001404bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1405 unsigned &Offset) const {
1406 if (!Subtarget->isTargetLinux())
1407 return false;
1408
1409 if (Subtarget->is64Bit()) {
1410 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1411 Offset = 0x28;
1412 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1413 AddressSpace = 256;
1414 else
1415 AddressSpace = 257;
1416 } else {
1417 // %gs:0x14 on i386
1418 Offset = 0x14;
1419 AddressSpace = 256;
1420 }
1421 return true;
1422}
1423
1424
Chris Lattner2b02a442007-02-25 08:29:00 +00001425//===----------------------------------------------------------------------===//
1426// Return Value Calling Convention Implementation
1427//===----------------------------------------------------------------------===//
1428
Chris Lattner59ed56b2007-02-28 04:55:35 +00001429#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001430
Michael J. Spencerec38de22010-10-10 22:04:20 +00001431bool
Eric Christopher471e4222011-06-08 23:55:35 +00001432X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1433 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001434 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001435 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001436 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001437 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001438 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001439 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001440}
1441
Dan Gohman98ca4f22009-08-05 01:29:28 +00001442SDValue
1443X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001444 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001445 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001446 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001447 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001448 MachineFunction &MF = DAG.getMachineFunction();
1449 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001450
Chris Lattner9774c912007-02-27 05:28:59 +00001451 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001452 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001453 RVLocs, *DAG.getContext());
1454 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001455
Evan Chengdcea1632010-02-04 02:40:39 +00001456 // Add the regs to the liveout set for the function.
1457 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1458 for (unsigned i = 0; i != RVLocs.size(); ++i)
1459 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1460 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001461
Dan Gohman475871a2008-07-27 21:46:04 +00001462 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001463
Dan Gohman475871a2008-07-27 21:46:04 +00001464 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001465 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1466 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001467 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1468 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001469
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001470 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001471 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1472 CCValAssign &VA = RVLocs[i];
1473 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001474 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001475 EVT ValVT = ValToCopy.getValueType();
1476
Dale Johannesenc4510512010-09-24 19:05:48 +00001477 // If this is x86-64, and we disabled SSE, we can't return FP values,
1478 // or SSE or MMX vectors.
1479 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1480 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001481 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001482 report_fatal_error("SSE register return with SSE disabled");
1483 }
1484 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1485 // llvm-gcc has never done it right and no one has noticed, so this
1486 // should be OK for now.
1487 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001488 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001489 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001490
Chris Lattner447ff682008-03-11 03:23:40 +00001491 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1492 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001493 if (VA.getLocReg() == X86::ST0 ||
1494 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001495 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1496 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001497 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001498 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001499 RetOps.push_back(ValToCopy);
1500 // Don't emit a copytoreg.
1501 continue;
1502 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001503
Evan Cheng242b38b2009-02-23 09:03:22 +00001504 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1505 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001506 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001507 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001508 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001509 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001510 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1511 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001512 // If we don't have SSE2 available, convert to v4f32 so the generated
1513 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001514 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001515 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001516 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001517 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001518 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001519
Dale Johannesendd64c412009-02-04 00:33:20 +00001520 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001521 Flag = Chain.getValue(1);
1522 }
Dan Gohman61a92132008-04-21 23:59:07 +00001523
1524 // The x86-64 ABI for returning structs by value requires that we copy
1525 // the sret argument into %rax for the return. We saved the argument into
1526 // a virtual register in the entry block, so now we copy the value out
1527 // and into %rax.
1528 if (Subtarget->is64Bit() &&
1529 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1530 MachineFunction &MF = DAG.getMachineFunction();
1531 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1532 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001533 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001534 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001535 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001536
Dale Johannesendd64c412009-02-04 00:33:20 +00001537 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001538 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001539
1540 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001541 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001542 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001543
Chris Lattner447ff682008-03-11 03:23:40 +00001544 RetOps[0] = Chain; // Update chain.
1545
1546 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001547 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001548 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001549
1550 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001551 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001552}
1553
Evan Cheng3d2125c2010-11-30 23:55:39 +00001554bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1555 if (N->getNumValues() != 1)
1556 return false;
1557 if (!N->hasNUsesOfValue(1, 0))
1558 return false;
1559
1560 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001561 if (Copy->getOpcode() != ISD::CopyToReg &&
1562 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001563 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001564
1565 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001566 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001567 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001568 if (UI->getOpcode() != X86ISD::RET_FLAG)
1569 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001570 HasRet = true;
1571 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001572
Evan Cheng1bf891a2010-12-01 22:59:46 +00001573 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001574}
1575
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001576EVT
1577X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001578 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001579 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001580 // TODO: Is this also valid on 32-bit?
1581 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001582 ReturnMVT = MVT::i8;
1583 else
1584 ReturnMVT = MVT::i32;
1585
1586 EVT MinVT = getRegisterType(Context, ReturnMVT);
1587 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001588}
1589
Dan Gohman98ca4f22009-08-05 01:29:28 +00001590/// LowerCallResult - Lower the result values of a call into the
1591/// appropriate copies out of appropriate physical registers.
1592///
1593SDValue
1594X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001595 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001596 const SmallVectorImpl<ISD::InputArg> &Ins,
1597 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001598 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001599
Chris Lattnere32bbf62007-02-28 07:09:55 +00001600 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001601 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001602 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001603 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1604 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001605 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001606
Chris Lattner3085e152007-02-25 08:59:22 +00001607 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001608 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001609 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001610 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001611
Torok Edwin3f142c32009-02-01 18:15:56 +00001612 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001613 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001614 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001615 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001616 }
1617
Evan Cheng79fb3b42009-02-20 20:43:02 +00001618 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001619
1620 // If this is a call to a function that returns an fp value on the floating
1621 // point stack, we must guarantee the the value is popped from the stack, so
1622 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001623 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001624 // instead.
1625 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1626 // If we prefer to use the value in xmm registers, copy it out as f80 and
1627 // use a truncate to move it from fp stack reg to xmm reg.
1628 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001629 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001630 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1631 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001632 Val = Chain.getValue(0);
1633
1634 // Round the f80 to the right size, which also moves it to the appropriate
1635 // xmm register.
1636 if (CopyVT != VA.getValVT())
1637 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1638 // This truncation won't change the value.
1639 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001640 } else {
1641 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1642 CopyVT, InFlag).getValue(1);
1643 Val = Chain.getValue(0);
1644 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001645 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001646 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001647 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001648
Dan Gohman98ca4f22009-08-05 01:29:28 +00001649 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001650}
1651
1652
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001653//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001654// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001655//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001656// StdCall calling convention seems to be standard for many Windows' API
1657// routines and around. It differs from C calling convention just a little:
1658// callee should clean up the stack, not caller. Symbols should be also
1659// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001660// For info on fast calling convention see Fast Calling Convention (tail call)
1661// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001662
Dan Gohman98ca4f22009-08-05 01:29:28 +00001663/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001664/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001665static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1666 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001667 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001668
Dan Gohman98ca4f22009-08-05 01:29:28 +00001669 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001670}
1671
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001672/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001673/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001674static bool
1675ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1676 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001677 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001678
Dan Gohman98ca4f22009-08-05 01:29:28 +00001679 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001680}
1681
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001682/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1683/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001684/// the specific parameter attribute. The copy will be passed as a byval
1685/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001686static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001687CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001688 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1689 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001690 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001691
Dale Johannesendd64c412009-02-04 00:33:20 +00001692 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001693 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001694 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001695}
1696
Chris Lattner29689432010-03-11 00:22:57 +00001697/// IsTailCallConvention - Return true if the calling convention is one that
1698/// supports tail call optimization.
1699static bool IsTailCallConvention(CallingConv::ID CC) {
1700 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1701}
1702
Evan Cheng485fafc2011-03-21 01:19:09 +00001703bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1704 if (!CI->isTailCall())
1705 return false;
1706
1707 CallSite CS(CI);
1708 CallingConv::ID CalleeCC = CS.getCallingConv();
1709 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1710 return false;
1711
1712 return true;
1713}
1714
Evan Cheng0c439eb2010-01-27 00:07:07 +00001715/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1716/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001717static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1718 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001719 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001720}
1721
Dan Gohman98ca4f22009-08-05 01:29:28 +00001722SDValue
1723X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001724 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001725 const SmallVectorImpl<ISD::InputArg> &Ins,
1726 DebugLoc dl, SelectionDAG &DAG,
1727 const CCValAssign &VA,
1728 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001729 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001730 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001731 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001732 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1733 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001734 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001735 EVT ValVT;
1736
1737 // If value is passed by pointer we have address passed instead of the value
1738 // itself.
1739 if (VA.getLocInfo() == CCValAssign::Indirect)
1740 ValVT = VA.getLocVT();
1741 else
1742 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001743
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001744 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001745 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001746 // In case of tail call optimization mark all arguments mutable. Since they
1747 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001748 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001749 unsigned Bytes = Flags.getByValSize();
1750 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1751 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001752 return DAG.getFrameIndex(FI, getPointerTy());
1753 } else {
1754 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001755 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001756 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1757 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001758 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001759 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001760 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001761}
1762
Dan Gohman475871a2008-07-27 21:46:04 +00001763SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001764X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001765 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001766 bool isVarArg,
1767 const SmallVectorImpl<ISD::InputArg> &Ins,
1768 DebugLoc dl,
1769 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001770 SmallVectorImpl<SDValue> &InVals)
1771 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001772 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001773 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001774
Gordon Henriksen86737662008-01-05 16:56:59 +00001775 const Function* Fn = MF.getFunction();
1776 if (Fn->hasExternalLinkage() &&
1777 Subtarget->isTargetCygMing() &&
1778 Fn->getName() == "main")
1779 FuncInfo->setForceFramePointer(true);
1780
Evan Cheng1bc78042006-04-26 01:20:17 +00001781 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001782 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001783 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001784
Chris Lattner29689432010-03-11 00:22:57 +00001785 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1786 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001787
Chris Lattner638402b2007-02-28 07:00:42 +00001788 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001789 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001790 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001791 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001792
1793 // Allocate shadow area for Win64
1794 if (IsWin64) {
1795 CCInfo.AllocateStack(32, 8);
1796 }
1797
Duncan Sands45907662010-10-31 13:21:44 +00001798 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001799
Chris Lattnerf39f7712007-02-28 05:46:49 +00001800 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001801 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001802 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1803 CCValAssign &VA = ArgLocs[i];
1804 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1805 // places.
1806 assert(VA.getValNo() != LastVal &&
1807 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001808 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001809 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001810
Chris Lattnerf39f7712007-02-28 05:46:49 +00001811 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001812 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001813 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001814 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001815 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001816 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001817 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001818 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001819 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001820 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001821 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001822 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1823 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001824 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001825 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001826 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001827 RC = X86::VR64RegisterClass;
1828 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001829 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001830
Devang Patel68e6bee2011-02-21 23:21:26 +00001831 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001832 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001833
Chris Lattnerf39f7712007-02-28 05:46:49 +00001834 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1835 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1836 // right size.
1837 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001838 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001839 DAG.getValueType(VA.getValVT()));
1840 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001841 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001842 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001843 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001844 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001845
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001846 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001847 // Handle MMX values passed in XMM regs.
1848 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001849 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1850 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001851 } else
1852 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001853 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001854 } else {
1855 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001856 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001857 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001858
1859 // If value is passed via pointer - do a load.
1860 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001861 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001862 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001863
Dan Gohman98ca4f22009-08-05 01:29:28 +00001864 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001865 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001866
Dan Gohman61a92132008-04-21 23:59:07 +00001867 // The x86-64 ABI for returning structs by value requires that we copy
1868 // the sret argument into %rax for the return. Save the argument into
1869 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001870 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001871 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1872 unsigned Reg = FuncInfo->getSRetReturnReg();
1873 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001874 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001875 FuncInfo->setSRetReturnReg(Reg);
1876 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001877 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001878 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001879 }
1880
Chris Lattnerf39f7712007-02-28 05:46:49 +00001881 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001882 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001883 if (FuncIsMadeTailCallSafe(CallConv,
1884 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001885 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001886
Evan Cheng1bc78042006-04-26 01:20:17 +00001887 // If the function takes variable number of arguments, make a frame index for
1888 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001889 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001890 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1891 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001892 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001893 }
1894 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001895 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1896
1897 // FIXME: We should really autogenerate these arrays
1898 static const unsigned GPR64ArgRegsWin64[] = {
1899 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001900 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001901 static const unsigned GPR64ArgRegs64Bit[] = {
1902 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1903 };
1904 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001905 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1906 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1907 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001908 const unsigned *GPR64ArgRegs;
1909 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001910
1911 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001912 // The XMM registers which might contain var arg parameters are shadowed
1913 // in their paired GPR. So we only need to save the GPR to their home
1914 // slots.
1915 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001916 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001917 } else {
1918 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1919 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001920
1921 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001922 }
1923 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1924 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001925
Devang Patel578efa92009-06-05 21:57:13 +00001926 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001927 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001928 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001929 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
1930 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001931 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001932 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
1933 !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001934 // Kernel mode asks for SSE to be disabled, so don't push them
1935 // on the stack.
1936 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001937
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001938 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001939 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001940 // Get to the caller-allocated home save location. Add 8 to account
1941 // for the return address.
1942 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001943 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001944 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001945 // Fixup to set vararg frame on shadow area (4 x i64).
1946 if (NumIntRegs < 4)
1947 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001948 } else {
1949 // For X86-64, if there are vararg parameters that are passed via
1950 // registers, then we must store them to their spots on the stack so they
1951 // may be loaded by deferencing the result of va_next.
1952 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1953 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1954 FuncInfo->setRegSaveFrameIndex(
1955 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001956 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001957 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001958
Gordon Henriksen86737662008-01-05 16:56:59 +00001959 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001960 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001961 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1962 getPointerTy());
1963 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001964 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001965 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1966 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001967 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001968 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001969 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001970 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001971 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001972 MachinePointerInfo::getFixedStack(
1973 FuncInfo->getRegSaveFrameIndex(), Offset),
1974 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001975 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001976 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001977 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001978
Dan Gohmanface41a2009-08-16 21:24:25 +00001979 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1980 // Now store the XMM (fp + vector) parameter registers.
1981 SmallVector<SDValue, 11> SaveXMMOps;
1982 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001983
Devang Patel68e6bee2011-02-21 23:21:26 +00001984 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001985 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1986 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001987
Dan Gohman1e93df62010-04-17 14:41:14 +00001988 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1989 FuncInfo->getRegSaveFrameIndex()));
1990 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1991 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001992
Dan Gohmanface41a2009-08-16 21:24:25 +00001993 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001994 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001995 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001996 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1997 SaveXMMOps.push_back(Val);
1998 }
1999 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2000 MVT::Other,
2001 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002002 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002003
2004 if (!MemOps.empty())
2005 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2006 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002007 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002008 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002009
Gordon Henriksen86737662008-01-05 16:56:59 +00002010 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002011 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2012 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002013 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002014 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002015 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002016 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00002017 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00002018 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002019 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002020
Gordon Henriksen86737662008-01-05 16:56:59 +00002021 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002022 // RegSaveFrameIndex is X86-64 only.
2023 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002024 if (CallConv == CallingConv::X86_FastCall ||
2025 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002026 // fastcc functions can't have varargs.
2027 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002028 }
Evan Cheng25caf632006-05-23 21:06:34 +00002029
Rafael Espindola76927d752011-08-30 19:39:58 +00002030 FuncInfo->setArgumentStackSize(StackSize);
2031
Dan Gohman98ca4f22009-08-05 01:29:28 +00002032 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002033}
2034
Dan Gohman475871a2008-07-27 21:46:04 +00002035SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002036X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2037 SDValue StackPtr, SDValue Arg,
2038 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002039 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002040 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002041 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002042 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002043 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002044 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002045 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002046
2047 return DAG.getStore(Chain, dl, Arg, PtrOff,
2048 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002049 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002050}
2051
Bill Wendling64e87322009-01-16 19:25:27 +00002052/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002053/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002054SDValue
2055X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002056 SDValue &OutRetAddr, SDValue Chain,
2057 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002058 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002059 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002060 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002061 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002062
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002063 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002064 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002065 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002066 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002067}
2068
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002069/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002070/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002071static SDValue
2072EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002073 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002074 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002075 // Store the return address to the appropriate stack slot.
2076 if (!FPDiff) return Chain;
2077 // Calculate the new stack slot for the return address.
2078 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002079 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002080 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002081 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002082 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002083 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002084 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002085 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002086 return Chain;
2087}
2088
Dan Gohman98ca4f22009-08-05 01:29:28 +00002089SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002090X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002091 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002092 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002093 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002094 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002095 const SmallVectorImpl<ISD::InputArg> &Ins,
2096 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002097 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002098 MachineFunction &MF = DAG.getMachineFunction();
2099 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002100 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002101 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002102 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002103
Evan Cheng5f941932010-02-05 02:21:12 +00002104 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002105 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002106 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2107 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002108 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002109
2110 // Sibcalls are automatically detected tailcalls which do not require
2111 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002112 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002113 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002114
2115 if (isTailCall)
2116 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002117 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002118
Chris Lattner29689432010-03-11 00:22:57 +00002119 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2120 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002121
Chris Lattner638402b2007-02-28 07:00:42 +00002122 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002123 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002124 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002125 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002126
2127 // Allocate shadow area for Win64
2128 if (IsWin64) {
2129 CCInfo.AllocateStack(32, 8);
2130 }
2131
Duncan Sands45907662010-10-31 13:21:44 +00002132 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002133
Chris Lattner423c5f42007-02-28 05:31:48 +00002134 // Get a count of how many bytes are to be pushed on the stack.
2135 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002136 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002137 // This is a sibcall. The memory operands are available in caller's
2138 // own caller's stack.
2139 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002140 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2141 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002142 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002143
Gordon Henriksen86737662008-01-05 16:56:59 +00002144 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002145 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002146 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002147 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002148 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2149 FPDiff = NumBytesCallerPushed - NumBytes;
2150
2151 // Set the delta of movement of the returnaddr stackslot.
2152 // But only set if delta is greater than previous delta.
2153 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2154 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2155 }
2156
Evan Chengf22f9b32010-02-06 03:28:46 +00002157 if (!IsSibcall)
2158 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002159
Dan Gohman475871a2008-07-27 21:46:04 +00002160 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002161 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002162 if (isTailCall && FPDiff)
2163 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2164 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002165
Dan Gohman475871a2008-07-27 21:46:04 +00002166 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2167 SmallVector<SDValue, 8> MemOpChains;
2168 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002169
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002170 // Walk the register/memloc assignments, inserting copies/loads. In the case
2171 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002172 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2173 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002174 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002175 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002176 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002177 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002178
Chris Lattner423c5f42007-02-28 05:31:48 +00002179 // Promote the value if needed.
2180 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002181 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002182 case CCValAssign::Full: break;
2183 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002184 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002185 break;
2186 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002187 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002188 break;
2189 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002190 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2191 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002192 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002193 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2194 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002195 } else
2196 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2197 break;
2198 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002199 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002200 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002201 case CCValAssign::Indirect: {
2202 // Store the argument.
2203 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002204 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002205 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002206 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002207 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002208 Arg = SpillSlot;
2209 break;
2210 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002211 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002212
Chris Lattner423c5f42007-02-28 05:31:48 +00002213 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002214 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2215 if (isVarArg && IsWin64) {
2216 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2217 // shadow reg if callee is a varargs function.
2218 unsigned ShadowReg = 0;
2219 switch (VA.getLocReg()) {
2220 case X86::XMM0: ShadowReg = X86::RCX; break;
2221 case X86::XMM1: ShadowReg = X86::RDX; break;
2222 case X86::XMM2: ShadowReg = X86::R8; break;
2223 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002224 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002225 if (ShadowReg)
2226 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002227 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002228 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002229 assert(VA.isMemLoc());
2230 if (StackPtr.getNode() == 0)
2231 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2232 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2233 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002234 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002235 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002236
Evan Cheng32fe1032006-05-25 00:59:30 +00002237 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002238 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002239 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002240
Evan Cheng347d5f72006-04-28 21:29:37 +00002241 // Build a sequence of copy-to-reg nodes chained together with token chain
2242 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002243 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002244 // Tail call byval lowering might overwrite argument registers so in case of
2245 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002246 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002247 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002248 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002249 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002250 InFlag = Chain.getValue(1);
2251 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002252
Chris Lattner88e1fd52009-07-09 04:24:46 +00002253 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002254 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2255 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002256 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002257 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2258 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002259 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002260 InFlag);
2261 InFlag = Chain.getValue(1);
2262 } else {
2263 // If we are tail calling and generating PIC/GOT style code load the
2264 // address of the callee into ECX. The value in ecx is used as target of
2265 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2266 // for tail calls on PIC/GOT architectures. Normally we would just put the
2267 // address of GOT into ebx and then call target@PLT. But for tail calls
2268 // ebx would be restored (since ebx is callee saved) before jumping to the
2269 // target@PLT.
2270
2271 // Note: The actual moving to ECX is done further down.
2272 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2273 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2274 !G->getGlobal()->hasProtectedVisibility())
2275 Callee = LowerGlobalAddress(Callee, DAG);
2276 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002277 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002278 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002279 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002280
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002281 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002282 // From AMD64 ABI document:
2283 // For calls that may call functions that use varargs or stdargs
2284 // (prototype-less calls or calls to functions containing ellipsis (...) in
2285 // the declaration) %al is used as hidden argument to specify the number
2286 // of SSE registers used. The contents of %al do not need to match exactly
2287 // the number of registers, but must be an ubound on the number of SSE
2288 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002289
Gordon Henriksen86737662008-01-05 16:56:59 +00002290 // Count the number of XMM registers allocated.
2291 static const unsigned XMMArgRegs[] = {
2292 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2293 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2294 };
2295 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002296 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002297 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002298
Dale Johannesendd64c412009-02-04 00:33:20 +00002299 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002300 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002301 InFlag = Chain.getValue(1);
2302 }
2303
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002304
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002305 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002306 if (isTailCall) {
2307 // Force all the incoming stack arguments to be loaded from the stack
2308 // before any new outgoing arguments are stored to the stack, because the
2309 // outgoing stack slots may alias the incoming argument stack slots, and
2310 // the alias isn't otherwise explicit. This is slightly more conservative
2311 // than necessary, because it means that each store effectively depends
2312 // on every argument instead of just those arguments it would clobber.
2313 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2314
Dan Gohman475871a2008-07-27 21:46:04 +00002315 SmallVector<SDValue, 8> MemOpChains2;
2316 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002317 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002318 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002319 InFlag = SDValue();
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002320 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002321 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2322 CCValAssign &VA = ArgLocs[i];
2323 if (VA.isRegLoc())
2324 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002325 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002326 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002327 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002328 // Create frame index.
2329 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002330 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002331 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002332 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002333
Duncan Sands276dcbd2008-03-21 09:14:45 +00002334 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002335 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002336 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002337 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002338 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002339 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002340 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002341
Dan Gohman98ca4f22009-08-05 01:29:28 +00002342 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2343 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002344 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002345 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002346 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002347 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002348 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002349 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002350 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002351 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002352 }
2353 }
2354
2355 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002356 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002357 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002358
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002359 // Copy arguments to their registers.
2360 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002361 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002362 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002363 InFlag = Chain.getValue(1);
2364 }
Dan Gohman475871a2008-07-27 21:46:04 +00002365 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002366
Gordon Henriksen86737662008-01-05 16:56:59 +00002367 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002368 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002369 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002370 }
2371
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002372 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2373 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2374 // In the 64-bit large code model, we have to make all calls
2375 // through a register, since the call instruction's 32-bit
2376 // pc-relative offset may not be large enough to hold the whole
2377 // address.
2378 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002379 // If the callee is a GlobalAddress node (quite common, every direct call
2380 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2381 // it.
2382
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002383 // We should use extra load for direct calls to dllimported functions in
2384 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002385 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002386 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002387 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002388 bool ExtraLoad = false;
2389 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002390
Chris Lattner48a7d022009-07-09 05:02:21 +00002391 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2392 // external symbols most go through the PLT in PIC mode. If the symbol
2393 // has hidden or protected visibility, or if it is static or local, then
2394 // we don't need to use the PLT - we can directly call it.
2395 if (Subtarget->isTargetELF() &&
2396 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002397 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002398 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002399 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002400 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002401 (!Subtarget->getTargetTriple().isMacOSX() ||
2402 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002403 // PC-relative references to external symbols should go through $stub,
2404 // unless we're building with the leopard linker or later, which
2405 // automatically synthesizes these stubs.
2406 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002407 } else if (Subtarget->isPICStyleRIPRel() &&
2408 isa<Function>(GV) &&
2409 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2410 // If the function is marked as non-lazy, generate an indirect call
2411 // which loads from the GOT directly. This avoids runtime overhead
2412 // at the cost of eager binding (and one extra byte of encoding).
2413 OpFlags = X86II::MO_GOTPCREL;
2414 WrapperKind = X86ISD::WrapperRIP;
2415 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002416 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002417
Devang Patel0d881da2010-07-06 22:08:15 +00002418 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002419 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002420
2421 // Add a wrapper if needed.
2422 if (WrapperKind != ISD::DELETED_NODE)
2423 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2424 // Add extra indirection if needed.
2425 if (ExtraLoad)
2426 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2427 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002428 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002429 }
Bill Wendling056292f2008-09-16 21:48:12 +00002430 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002431 unsigned char OpFlags = 0;
2432
Evan Cheng1bf891a2010-12-01 22:59:46 +00002433 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2434 // external symbols should go through the PLT.
2435 if (Subtarget->isTargetELF() &&
2436 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2437 OpFlags = X86II::MO_PLT;
2438 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002439 (!Subtarget->getTargetTriple().isMacOSX() ||
2440 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002441 // PC-relative references to external symbols should go through $stub,
2442 // unless we're building with the leopard linker or later, which
2443 // automatically synthesizes these stubs.
2444 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002445 }
Eric Christopherfd179292009-08-27 18:07:15 +00002446
Chris Lattner48a7d022009-07-09 05:02:21 +00002447 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2448 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002449 }
2450
Chris Lattnerd96d0722007-02-25 06:40:16 +00002451 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002452 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002453 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002454
Evan Chengf22f9b32010-02-06 03:28:46 +00002455 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002456 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2457 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002458 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002459 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002460
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002461 Ops.push_back(Chain);
2462 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002463
Dan Gohman98ca4f22009-08-05 01:29:28 +00002464 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002465 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002466
Gordon Henriksen86737662008-01-05 16:56:59 +00002467 // Add argument registers to the end of the list so that they are known live
2468 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002469 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2470 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2471 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002472
Evan Cheng586ccac2008-03-18 23:36:35 +00002473 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002474 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002475 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2476
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002477 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002478 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002479 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002480
Gabor Greifba36cb52008-08-28 21:40:38 +00002481 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002482 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002483
Dan Gohman98ca4f22009-08-05 01:29:28 +00002484 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002485 // We used to do:
2486 //// If this is the first return lowered for this function, add the regs
2487 //// to the liveout set for the function.
2488 // This isn't right, although it's probably harmless on x86; liveouts
2489 // should be computed from returns not tail calls. Consider a void
2490 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002491 return DAG.getNode(X86ISD::TC_RETURN, dl,
2492 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002493 }
2494
Dale Johannesenace16102009-02-03 19:33:06 +00002495 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002496 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002497
Chris Lattner2d297092006-05-23 18:50:38 +00002498 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002499 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002500 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2501 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002502 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002503 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002504 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002505 // pops the hidden struct pointer, so we have to push it back.
2506 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002507 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002508 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002509 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002510
Gordon Henriksenae636f82008-01-03 16:47:34 +00002511 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002512 if (!IsSibcall) {
2513 Chain = DAG.getCALLSEQ_END(Chain,
2514 DAG.getIntPtrConstant(NumBytes, true),
2515 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2516 true),
2517 InFlag);
2518 InFlag = Chain.getValue(1);
2519 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002520
Chris Lattner3085e152007-02-25 08:59:22 +00002521 // Handle result values, copying them out of physregs into vregs that we
2522 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002523 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2524 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002525}
2526
Evan Cheng25ab6902006-09-08 06:48:29 +00002527
2528//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002529// Fast Calling Convention (tail call) implementation
2530//===----------------------------------------------------------------------===//
2531
2532// Like std call, callee cleans arguments, convention except that ECX is
2533// reserved for storing the tail called function address. Only 2 registers are
2534// free for argument passing (inreg). Tail call optimization is performed
2535// provided:
2536// * tailcallopt is enabled
2537// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002538// On X86_64 architecture with GOT-style position independent code only local
2539// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002540// To keep the stack aligned according to platform abi the function
2541// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2542// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002543// If a tail called function callee has more arguments than the caller the
2544// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002545// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002546// original REtADDR, but before the saved framepointer or the spilled registers
2547// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2548// stack layout:
2549// arg1
2550// arg2
2551// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002552// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002553// move area ]
2554// (possible EBP)
2555// ESI
2556// EDI
2557// local1 ..
2558
2559/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2560/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002561unsigned
2562X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2563 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002564 MachineFunction &MF = DAG.getMachineFunction();
2565 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002566 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002567 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002568 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002569 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002570 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002571 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2572 // Number smaller than 12 so just add the difference.
2573 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2574 } else {
2575 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002576 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002577 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002578 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002579 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002580}
2581
Evan Cheng5f941932010-02-05 02:21:12 +00002582/// MatchingStackOffset - Return true if the given stack call argument is
2583/// already available in the same position (relatively) of the caller's
2584/// incoming argument stack.
2585static
2586bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2587 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2588 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002589 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2590 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002591 if (Arg.getOpcode() == ISD::CopyFromReg) {
2592 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002593 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002594 return false;
2595 MachineInstr *Def = MRI->getVRegDef(VR);
2596 if (!Def)
2597 return false;
2598 if (!Flags.isByVal()) {
2599 if (!TII->isLoadFromStackSlot(Def, FI))
2600 return false;
2601 } else {
2602 unsigned Opcode = Def->getOpcode();
2603 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2604 Def->getOperand(1).isFI()) {
2605 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002606 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002607 } else
2608 return false;
2609 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002610 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2611 if (Flags.isByVal())
2612 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002613 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002614 // define @foo(%struct.X* %A) {
2615 // tail call @bar(%struct.X* byval %A)
2616 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002617 return false;
2618 SDValue Ptr = Ld->getBasePtr();
2619 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2620 if (!FINode)
2621 return false;
2622 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002623 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002624 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002625 FI = FINode->getIndex();
2626 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002627 } else
2628 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002629
Evan Cheng4cae1332010-03-05 08:38:04 +00002630 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002631 if (!MFI->isFixedObjectIndex(FI))
2632 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002633 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002634}
2635
Dan Gohman98ca4f22009-08-05 01:29:28 +00002636/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2637/// for tail call optimization. Targets which want to do tail call
2638/// optimization should implement this function.
2639bool
2640X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002641 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002642 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002643 bool isCalleeStructRet,
2644 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002645 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002646 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002647 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002648 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002649 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002650 CalleeCC != CallingConv::C)
2651 return false;
2652
Evan Cheng7096ae42010-01-29 06:45:59 +00002653 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002654 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002655 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002656 CallingConv::ID CallerCC = CallerF->getCallingConv();
2657 bool CCMatch = CallerCC == CalleeCC;
2658
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002659 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002660 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002661 return true;
2662 return false;
2663 }
2664
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002665 // Look for obvious safe cases to perform tail call optimization that do not
2666 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002667
Evan Cheng2c12cb42010-03-26 16:26:03 +00002668 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2669 // emit a special epilogue.
2670 if (RegInfo->needsStackRealignment(MF))
2671 return false;
2672
Evan Chenga375d472010-03-15 18:54:48 +00002673 // Also avoid sibcall optimization if either caller or callee uses struct
2674 // return semantics.
2675 if (isCalleeStructRet || isCallerStructRet)
2676 return false;
2677
Chad Rosier2416da32011-06-24 21:15:36 +00002678 // An stdcall caller is expected to clean up its arguments; the callee
2679 // isn't going to do that.
2680 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2681 return false;
2682
Chad Rosier871f6642011-05-18 19:59:50 +00002683 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002684 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002685 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002686
2687 // Optimizing for varargs on Win64 is unlikely to be safe without
2688 // additional testing.
2689 if (Subtarget->isTargetWin64())
2690 return false;
2691
Chad Rosier871f6642011-05-18 19:59:50 +00002692 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002693 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2694 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002695
Chad Rosier871f6642011-05-18 19:59:50 +00002696 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2697 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2698 if (!ArgLocs[i].isRegLoc())
2699 return false;
2700 }
2701
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002702 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2703 // Therefore if it's not used by the call it is not safe to optimize this into
2704 // a sibcall.
2705 bool Unused = false;
2706 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2707 if (!Ins[i].Used) {
2708 Unused = true;
2709 break;
2710 }
2711 }
2712 if (Unused) {
2713 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002714 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2715 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002716 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002717 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002718 CCValAssign &VA = RVLocs[i];
2719 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2720 return false;
2721 }
2722 }
2723
Evan Cheng13617962010-04-30 01:12:32 +00002724 // If the calling conventions do not match, then we'd better make sure the
2725 // results are returned in the same way as what the caller expects.
2726 if (!CCMatch) {
2727 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002728 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2729 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002730 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2731
2732 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002733 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2734 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002735 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2736
2737 if (RVLocs1.size() != RVLocs2.size())
2738 return false;
2739 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2740 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2741 return false;
2742 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2743 return false;
2744 if (RVLocs1[i].isRegLoc()) {
2745 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2746 return false;
2747 } else {
2748 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2749 return false;
2750 }
2751 }
2752 }
2753
Evan Chenga6bff982010-01-30 01:22:00 +00002754 // If the callee takes no arguments then go on to check the results of the
2755 // call.
2756 if (!Outs.empty()) {
2757 // Check if stack adjustment is needed. For now, do not do this if any
2758 // argument is passed on the stack.
2759 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002760 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2761 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002762
2763 // Allocate shadow area for Win64
2764 if (Subtarget->isTargetWin64()) {
2765 CCInfo.AllocateStack(32, 8);
2766 }
2767
Duncan Sands45907662010-10-31 13:21:44 +00002768 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002769 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002770 MachineFunction &MF = DAG.getMachineFunction();
2771 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2772 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002773
2774 // Check if the arguments are already laid out in the right way as
2775 // the caller's fixed stack objects.
2776 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002777 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2778 const X86InstrInfo *TII =
2779 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002780 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2781 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002782 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002783 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002784 if (VA.getLocInfo() == CCValAssign::Indirect)
2785 return false;
2786 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002787 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2788 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002789 return false;
2790 }
2791 }
2792 }
Evan Cheng9c044672010-05-29 01:35:22 +00002793
2794 // If the tailcall address may be in a register, then make sure it's
2795 // possible to register allocate for it. In 32-bit, the call address can
2796 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002797 // callee-saved registers are restored. These happen to be the same
2798 // registers used to pass 'inreg' arguments so watch out for those.
2799 if (!Subtarget->is64Bit() &&
2800 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002801 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002802 unsigned NumInRegs = 0;
2803 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2804 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002805 if (!VA.isRegLoc())
2806 continue;
2807 unsigned Reg = VA.getLocReg();
2808 switch (Reg) {
2809 default: break;
2810 case X86::EAX: case X86::EDX: case X86::ECX:
2811 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002812 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002813 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002814 }
2815 }
2816 }
Evan Chenga6bff982010-01-30 01:22:00 +00002817 }
Evan Chengb1712452010-01-27 06:25:16 +00002818
Evan Cheng86809cc2010-02-03 03:28:02 +00002819 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002820}
2821
Dan Gohman3df24e62008-09-03 23:12:08 +00002822FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002823X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2824 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002825}
2826
2827
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002828//===----------------------------------------------------------------------===//
2829// Other Lowering Hooks
2830//===----------------------------------------------------------------------===//
2831
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002832static bool MayFoldLoad(SDValue Op) {
2833 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2834}
2835
2836static bool MayFoldIntoStore(SDValue Op) {
2837 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2838}
2839
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002840static bool isTargetShuffle(unsigned Opcode) {
2841 switch(Opcode) {
2842 default: return false;
2843 case X86ISD::PSHUFD:
2844 case X86ISD::PSHUFHW:
2845 case X86ISD::PSHUFLW:
2846 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002847 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002848 case X86ISD::SHUFPS:
2849 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002850 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002851 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002852 case X86ISD::MOVLPS:
2853 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002854 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002855 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002856 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002857 case X86ISD::MOVSS:
2858 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002859 case X86ISD::UNPCKL:
2860 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002861 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002862 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002863 return true;
2864 }
2865 return false;
2866}
2867
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002868static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002869 SDValue V1, SelectionDAG &DAG) {
2870 switch(Opc) {
2871 default: llvm_unreachable("Unknown x86 shuffle node");
2872 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002873 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002874 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002875 return DAG.getNode(Opc, dl, VT, V1);
2876 }
2877
2878 return SDValue();
2879}
2880
2881static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002882 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002883 switch(Opc) {
2884 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002885 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002886 case X86ISD::PSHUFHW:
2887 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002888 case X86ISD::VPERMILP:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002889 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2890 }
2891
2892 return SDValue();
2893}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002894
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002895static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2896 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2897 switch(Opc) {
2898 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002899 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002900 case X86ISD::SHUFPD:
2901 case X86ISD::SHUFPS:
Craig Topperec24e612011-11-30 07:47:51 +00002902 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002903 return DAG.getNode(Opc, dl, VT, V1, V2,
2904 DAG.getConstant(TargetMask, MVT::i8));
2905 }
2906 return SDValue();
2907}
2908
2909static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2910 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2911 switch(Opc) {
2912 default: llvm_unreachable("Unknown x86 shuffle node");
2913 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002914 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002915 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002916 case X86ISD::MOVLPS:
2917 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002918 case X86ISD::MOVSS:
2919 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002920 case X86ISD::UNPCKL:
2921 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002922 return DAG.getNode(Opc, dl, VT, V1, V2);
2923 }
2924 return SDValue();
2925}
2926
Dan Gohmand858e902010-04-17 15:26:15 +00002927SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002928 MachineFunction &MF = DAG.getMachineFunction();
2929 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2930 int ReturnAddrIndex = FuncInfo->getRAIndex();
2931
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002932 if (ReturnAddrIndex == 0) {
2933 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002934 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002935 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002936 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002937 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002938 }
2939
Evan Cheng25ab6902006-09-08 06:48:29 +00002940 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002941}
2942
2943
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002944bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2945 bool hasSymbolicDisplacement) {
2946 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002947 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002948 return false;
2949
2950 // If we don't have a symbolic displacement - we don't have any extra
2951 // restrictions.
2952 if (!hasSymbolicDisplacement)
2953 return true;
2954
2955 // FIXME: Some tweaks might be needed for medium code model.
2956 if (M != CodeModel::Small && M != CodeModel::Kernel)
2957 return false;
2958
2959 // For small code model we assume that latest object is 16MB before end of 31
2960 // bits boundary. We may also accept pretty large negative constants knowing
2961 // that all objects are in the positive half of address space.
2962 if (M == CodeModel::Small && Offset < 16*1024*1024)
2963 return true;
2964
2965 // For kernel code model we know that all object resist in the negative half
2966 // of 32bits address space. We may not accept negative offsets, since they may
2967 // be just off and we may accept pretty large positive ones.
2968 if (M == CodeModel::Kernel && Offset > 0)
2969 return true;
2970
2971 return false;
2972}
2973
Evan Chengef41ff62011-06-23 17:54:54 +00002974/// isCalleePop - Determines whether the callee is required to pop its
2975/// own arguments. Callee pop is necessary to support tail calls.
2976bool X86::isCalleePop(CallingConv::ID CallingConv,
2977 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2978 if (IsVarArg)
2979 return false;
2980
2981 switch (CallingConv) {
2982 default:
2983 return false;
2984 case CallingConv::X86_StdCall:
2985 return !is64Bit;
2986 case CallingConv::X86_FastCall:
2987 return !is64Bit;
2988 case CallingConv::X86_ThisCall:
2989 return !is64Bit;
2990 case CallingConv::Fast:
2991 return TailCallOpt;
2992 case CallingConv::GHC:
2993 return TailCallOpt;
2994 }
2995}
2996
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002997/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2998/// specific condition code, returning the condition code and the LHS/RHS of the
2999/// comparison to make.
3000static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3001 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003002 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003003 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3004 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3005 // X > -1 -> X == 0, jump !sign.
3006 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003007 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003008 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
3009 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003010 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00003011 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003012 // X < 1 -> X <= 0
3013 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003014 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003015 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003016 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003017
Evan Chengd9558e02006-01-06 00:43:03 +00003018 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003019 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003020 case ISD::SETEQ: return X86::COND_E;
3021 case ISD::SETGT: return X86::COND_G;
3022 case ISD::SETGE: return X86::COND_GE;
3023 case ISD::SETLT: return X86::COND_L;
3024 case ISD::SETLE: return X86::COND_LE;
3025 case ISD::SETNE: return X86::COND_NE;
3026 case ISD::SETULT: return X86::COND_B;
3027 case ISD::SETUGT: return X86::COND_A;
3028 case ISD::SETULE: return X86::COND_BE;
3029 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003030 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003031 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003032
Chris Lattner4c78e022008-12-23 23:42:27 +00003033 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003034
Chris Lattner4c78e022008-12-23 23:42:27 +00003035 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003036 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3037 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003038 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3039 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003040 }
3041
Chris Lattner4c78e022008-12-23 23:42:27 +00003042 switch (SetCCOpcode) {
3043 default: break;
3044 case ISD::SETOLT:
3045 case ISD::SETOLE:
3046 case ISD::SETUGT:
3047 case ISD::SETUGE:
3048 std::swap(LHS, RHS);
3049 break;
3050 }
3051
3052 // On a floating point condition, the flags are set as follows:
3053 // ZF PF CF op
3054 // 0 | 0 | 0 | X > Y
3055 // 0 | 0 | 1 | X < Y
3056 // 1 | 0 | 0 | X == Y
3057 // 1 | 1 | 1 | unordered
3058 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003059 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003060 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003061 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003062 case ISD::SETOLT: // flipped
3063 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003064 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003065 case ISD::SETOLE: // flipped
3066 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003067 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003068 case ISD::SETUGT: // flipped
3069 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003070 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003071 case ISD::SETUGE: // flipped
3072 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003073 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003074 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003075 case ISD::SETNE: return X86::COND_NE;
3076 case ISD::SETUO: return X86::COND_P;
3077 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003078 case ISD::SETOEQ:
3079 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003080 }
Evan Chengd9558e02006-01-06 00:43:03 +00003081}
3082
Evan Cheng4a460802006-01-11 00:33:36 +00003083/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3084/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003085/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003086static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003087 switch (X86CC) {
3088 default:
3089 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003090 case X86::COND_B:
3091 case X86::COND_BE:
3092 case X86::COND_E:
3093 case X86::COND_P:
3094 case X86::COND_A:
3095 case X86::COND_AE:
3096 case X86::COND_NE:
3097 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003098 return true;
3099 }
3100}
3101
Evan Chengeb2f9692009-10-27 19:56:55 +00003102/// isFPImmLegal - Returns true if the target can instruction select the
3103/// specified FP immediate natively. If false, the legalizer will
3104/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003105bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003106 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3107 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3108 return true;
3109 }
3110 return false;
3111}
3112
Nate Begeman9008ca62009-04-27 18:41:29 +00003113/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3114/// the specified range (L, H].
3115static bool isUndefOrInRange(int Val, int Low, int Hi) {
3116 return (Val < 0) || (Val >= Low && Val < Hi);
3117}
3118
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003119/// isUndefOrInRange - Return true if every element in Mask, begining
3120/// from position Pos and ending in Pos+Size, falls within the specified
3121/// range (L, L+Pos]. or is undef.
3122static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3123 int Pos, int Size, int Low, int Hi) {
3124 for (int i = Pos, e = Pos+Size; i != e; ++i)
3125 if (!isUndefOrInRange(Mask[i], Low, Hi))
3126 return false;
3127 return true;
3128}
3129
Nate Begeman9008ca62009-04-27 18:41:29 +00003130/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3131/// specified value.
3132static bool isUndefOrEqual(int Val, int CmpVal) {
3133 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003134 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003135 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003136}
3137
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003138/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3139/// from position Pos and ending in Pos+Size, falls within the specified
3140/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003141static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3142 int Pos, int Size, int Low) {
3143 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3144 if (!isUndefOrEqual(Mask[i], Low))
3145 return false;
3146 return true;
3147}
3148
Nate Begeman9008ca62009-04-27 18:41:29 +00003149/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3150/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3151/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003152static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003153 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003154 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003156 return (Mask[0] < 2 && Mask[1] < 2);
3157 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003158}
3159
Nate Begeman9008ca62009-04-27 18:41:29 +00003160bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003161 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003162 N->getMask(M);
3163 return ::isPSHUFDMask(M, N->getValueType(0));
3164}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003165
Nate Begeman9008ca62009-04-27 18:41:29 +00003166/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3167/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003168static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003169 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003170 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003171
Nate Begeman9008ca62009-04-27 18:41:29 +00003172 // Lower quadword copied in order or undef.
3173 for (int i = 0; i != 4; ++i)
3174 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003175 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003176
Evan Cheng506d3df2006-03-29 23:07:14 +00003177 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 for (int i = 4; i != 8; ++i)
3179 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003180 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003181
Evan Cheng506d3df2006-03-29 23:07:14 +00003182 return true;
3183}
3184
Nate Begeman9008ca62009-04-27 18:41:29 +00003185bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003186 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003187 N->getMask(M);
3188 return ::isPSHUFHWMask(M, N->getValueType(0));
3189}
Evan Cheng506d3df2006-03-29 23:07:14 +00003190
Nate Begeman9008ca62009-04-27 18:41:29 +00003191/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3192/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003193static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003194 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003195 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003196
Rafael Espindola15684b22009-04-24 12:40:33 +00003197 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003198 for (int i = 4; i != 8; ++i)
3199 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003200 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003201
Rafael Espindola15684b22009-04-24 12:40:33 +00003202 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003203 for (int i = 0; i != 4; ++i)
3204 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003205 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003206
Rafael Espindola15684b22009-04-24 12:40:33 +00003207 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003208}
3209
Nate Begeman9008ca62009-04-27 18:41:29 +00003210bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003211 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003212 N->getMask(M);
3213 return ::isPSHUFLWMask(M, N->getValueType(0));
3214}
3215
Nate Begemana09008b2009-10-19 02:17:23 +00003216/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3217/// is suitable for input to PALIGNR.
3218static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003219 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003220 int i, e = VT.getVectorNumElements();
Craig Topper1dc0fbc2011-12-05 07:27:14 +00003221 if (VT.getSizeInBits() != 128)
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003222 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003223
Nate Begemana09008b2009-10-19 02:17:23 +00003224 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003225 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003226 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003227
Nate Begemana09008b2009-10-19 02:17:23 +00003228 for (i = 0; i != e; ++i)
3229 if (Mask[i] >= 0)
3230 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003231
Nate Begemana09008b2009-10-19 02:17:23 +00003232 // All undef, not a palignr.
3233 if (i == e)
3234 return false;
3235
Eli Friedman63f8dde2011-07-25 21:36:45 +00003236 // Make sure we're shifting in the right direction.
3237 if (Mask[i] <= i)
3238 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003239
3240 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003241
Nate Begemana09008b2009-10-19 02:17:23 +00003242 // Check the rest of the elements to see if they are consecutive.
3243 for (++i; i != e; ++i) {
3244 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003245 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003246 return false;
3247 }
3248 return true;
3249}
3250
Craig Topper9d7025b2011-11-27 21:41:12 +00003251/// isVSHUFPYMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003252/// specifies a shuffle of elements that is suitable for input to 256-bit
3253/// VSHUFPSY.
Craig Topper9d7025b2011-11-27 21:41:12 +00003254static bool isVSHUFPYMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper1ff73d72011-12-06 04:59:07 +00003255 bool HasAVX, bool Commuted = false) {
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003256 int NumElems = VT.getVectorNumElements();
3257
Craig Topper71c4c122011-11-28 01:14:24 +00003258 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003259 return false;
3260
Craig Topper9d7025b2011-11-27 21:41:12 +00003261 if (NumElems != 4 && NumElems != 8)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003262 return false;
3263
3264 // VSHUFPSY divides the resulting vector into 4 chunks.
3265 // The sources are also splitted into 4 chunks, and each destination
3266 // chunk must come from a different source chunk.
3267 //
3268 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3269 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3270 //
3271 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3272 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3273 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003274 // VSHUFPDY divides the resulting vector into 4 chunks.
3275 // The sources are also splitted into 4 chunks, and each destination
3276 // chunk must come from a different source chunk.
3277 //
3278 // SRC1 => X3 X2 X1 X0
3279 // SRC2 => Y3 Y2 Y1 Y0
3280 //
3281 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3282 //
Craig Topper1ff73d72011-12-06 04:59:07 +00003283 unsigned QuarterSize = NumElems/4;
3284 unsigned HalfSize = QuarterSize*2;
3285 for (unsigned l = 0; l != 2; ++l) {
3286 unsigned LaneStart = l*HalfSize;
3287 for (unsigned s = 0; s != 2; ++s) {
3288 unsigned QuarterStart = s*QuarterSize;
3289 unsigned Src = (Commuted) ? (1-s) : s;
3290 unsigned SrcStart = Src*NumElems + LaneStart;
3291 for (unsigned i = 0; i != QuarterSize; ++i) {
3292 int Idx = Mask[i+QuarterStart+LaneStart];
3293 if (!isUndefOrInRange(Idx, SrcStart, SrcStart+HalfSize))
3294 return false;
3295 // For VSHUFPSY, the mask of the second half must be the same as the first
3296 // but with the appropriate offsets. This works in the same way as
3297 // VPERMILPS works with masks.
3298 if (NumElems == 4 || l == 0 || Mask[i+QuarterStart] < 0)
3299 continue;
3300 if (!isUndefOrEqual(Idx, Mask[i+QuarterStart]+HalfSize))
3301 return false;
3302 }
3303 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003304 }
3305
3306 return true;
3307}
3308
Craig Topper9d7025b2011-11-27 21:41:12 +00003309/// getShuffleVSHUFPYImmediate - Return the appropriate immediate to shuffle
3310/// the specified VECTOR_MASK mask with VSHUFPSY/VSHUFPDY instructions.
3311static unsigned getShuffleVSHUFPYImmediate(SDNode *N) {
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003312 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3313 EVT VT = SVOp->getValueType(0);
3314 int NumElems = VT.getVectorNumElements();
3315
Craig Topper9d7025b2011-11-27 21:41:12 +00003316 assert(VT.getSizeInBits() == 256 && "Only supports 256-bit types");
3317 assert((NumElems == 4 || NumElems == 8) && "Only supports v4 and v8 types");
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003318
3319 int HalfSize = NumElems/2;
Craig Topper9d7025b2011-11-27 21:41:12 +00003320 unsigned Mul = (NumElems == 8) ? 2 : 1;
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003321 unsigned Mask = 0;
Craig Topper71c4c122011-11-28 01:14:24 +00003322 for (int i = 0; i != NumElems; ++i) {
Craig Topper9d7025b2011-11-27 21:41:12 +00003323 int Elt = SVOp->getMaskElt(i);
3324 if (Elt < 0)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003325 continue;
Craig Topper9d7025b2011-11-27 21:41:12 +00003326 Elt %= HalfSize;
3327 unsigned Shamt = i;
3328 // For VSHUFPSY, the mask of the first half must be equal to the second one.
3329 if (NumElems == 8) Shamt %= HalfSize;
3330 Mask |= Elt << (Shamt*Mul);
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003331 }
3332
3333 return Mask;
3334}
3335
Elena Demikhovsky52a35a82011-11-23 10:23:16 +00003336/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3337/// the two vector operands have swapped position.
Craig Topperbeabc6c2011-12-05 06:56:46 +00003338static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3339 unsigned NumElems) {
Elena Demikhovsky52a35a82011-11-23 10:23:16 +00003340 for (unsigned i = 0; i != NumElems; ++i) {
3341 int idx = Mask[i];
3342 if (idx < 0)
3343 continue;
3344 else if (idx < (int)NumElems)
3345 Mask[i] = idx + NumElems;
3346 else
3347 Mask[i] = idx - NumElems;
3348 }
3349}
3350
Evan Cheng14aed5e2006-03-24 01:18:28 +00003351/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003352/// specifies a shuffle of elements that is suitable for input to 128-bit
Craig Topper1ff73d72011-12-06 04:59:07 +00003353/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3354/// reverse of what x86 shuffles want.
3355static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT,
3356 bool Commuted = false) {
3357 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003358
3359 if (VT.getSizeInBits() != 128)
3360 return false;
3361
Nate Begeman9008ca62009-04-27 18:41:29 +00003362 if (NumElems != 2 && NumElems != 4)
3363 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003364
Craig Topper1ff73d72011-12-06 04:59:07 +00003365 unsigned Half = NumElems / 2;
3366 unsigned SrcStart = Commuted ? NumElems : 0;
3367 for (unsigned i = 0; i != Half; ++i)
3368 if (!isUndefOrInRange(Mask[i], SrcStart, SrcStart+NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003369 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003370 SrcStart = Commuted ? 0 : NumElems;
3371 for (unsigned i = Half; i != NumElems; ++i)
3372 if (!isUndefOrInRange(Mask[i], SrcStart, SrcStart+NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003373 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003374
Evan Cheng14aed5e2006-03-24 01:18:28 +00003375 return true;
3376}
3377
Nate Begeman9008ca62009-04-27 18:41:29 +00003378bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3379 SmallVector<int, 8> M;
3380 N->getMask(M);
3381 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003382}
3383
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003384/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3385/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003386bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003387 EVT VT = N->getValueType(0);
3388 unsigned NumElems = VT.getVectorNumElements();
3389
3390 if (VT.getSizeInBits() != 128)
3391 return false;
3392
3393 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003394 return false;
3395
Evan Cheng2064a2b2006-03-28 06:50:32 +00003396 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003397 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3398 isUndefOrEqual(N->getMaskElt(1), 7) &&
3399 isUndefOrEqual(N->getMaskElt(2), 2) &&
3400 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003401}
3402
Nate Begeman0b10b912009-11-07 23:17:15 +00003403/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3404/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3405/// <2, 3, 2, 3>
3406bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003407 EVT VT = N->getValueType(0);
3408 unsigned NumElems = VT.getVectorNumElements();
3409
3410 if (VT.getSizeInBits() != 128)
3411 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003412
Nate Begeman0b10b912009-11-07 23:17:15 +00003413 if (NumElems != 4)
3414 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003415
Nate Begeman0b10b912009-11-07 23:17:15 +00003416 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003417 isUndefOrEqual(N->getMaskElt(1), 3) &&
3418 isUndefOrEqual(N->getMaskElt(2), 2) &&
3419 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003420}
3421
Evan Cheng5ced1d82006-04-06 23:23:56 +00003422/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3423/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003424bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3425 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003426
Evan Cheng5ced1d82006-04-06 23:23:56 +00003427 if (NumElems != 2 && NumElems != 4)
3428 return false;
3429
Evan Chengc5cdff22006-04-07 21:53:05 +00003430 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003431 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003432 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003433
Evan Chengc5cdff22006-04-07 21:53:05 +00003434 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003435 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003436 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003437
3438 return true;
3439}
3440
Nate Begeman0b10b912009-11-07 23:17:15 +00003441/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3442/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3443bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003444 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003445
David Greenea20244d2011-03-02 17:23:43 +00003446 if ((NumElems != 2 && NumElems != 4)
3447 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003448 return false;
3449
Evan Chengc5cdff22006-04-07 21:53:05 +00003450 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003451 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003452 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003453
Nate Begeman9008ca62009-04-27 18:41:29 +00003454 for (unsigned i = 0; i < NumElems/2; ++i)
3455 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003456 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003457
3458 return true;
3459}
3460
Evan Cheng0038e592006-03-28 00:39:58 +00003461/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3462/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003463static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003464 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003465 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003466
3467 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3468 "Unsupported vector type for unpckh");
3469
Craig Topper6347e862011-11-21 06:57:39 +00003470 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003471 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003472 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003473
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003474 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3475 // independently on 128-bit lanes.
3476 unsigned NumLanes = VT.getSizeInBits()/128;
3477 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003478
3479 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003480 unsigned End = NumLaneElts;
3481 for (unsigned s = 0; s < NumLanes; ++s) {
3482 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003483 i != End;
3484 i += 2, ++j) {
3485 int BitI = Mask[i];
3486 int BitI1 = Mask[i+1];
3487 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003488 return false;
David Greenea20244d2011-03-02 17:23:43 +00003489 if (V2IsSplat) {
3490 if (!isUndefOrEqual(BitI1, NumElts))
3491 return false;
3492 } else {
3493 if (!isUndefOrEqual(BitI1, j + NumElts))
3494 return false;
3495 }
Evan Cheng39623da2006-04-20 08:58:49 +00003496 }
David Greenea20244d2011-03-02 17:23:43 +00003497 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003498 Start += NumLaneElts;
3499 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003500 }
David Greenea20244d2011-03-02 17:23:43 +00003501
Evan Cheng0038e592006-03-28 00:39:58 +00003502 return true;
3503}
3504
Craig Topper6347e862011-11-21 06:57:39 +00003505bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003506 SmallVector<int, 8> M;
3507 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003508 return ::isUNPCKLMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003509}
3510
Evan Cheng4fcb9222006-03-28 02:43:26 +00003511/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3512/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003513static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003514 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003515 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003516
3517 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3518 "Unsupported vector type for unpckh");
3519
Craig Topper6347e862011-11-21 06:57:39 +00003520 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003521 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003522 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003523
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003524 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3525 // independently on 128-bit lanes.
3526 unsigned NumLanes = VT.getSizeInBits()/128;
3527 unsigned NumLaneElts = NumElts/NumLanes;
3528
3529 unsigned Start = 0;
3530 unsigned End = NumLaneElts;
3531 for (unsigned l = 0; l != NumLanes; ++l) {
3532 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3533 i != End; i += 2, ++j) {
3534 int BitI = Mask[i];
3535 int BitI1 = Mask[i+1];
3536 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003537 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003538 if (V2IsSplat) {
3539 if (isUndefOrEqual(BitI1, NumElts))
3540 return false;
3541 } else {
3542 if (!isUndefOrEqual(BitI1, j+NumElts))
3543 return false;
3544 }
Evan Cheng39623da2006-04-20 08:58:49 +00003545 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003546 // Process the next 128 bits.
3547 Start += NumLaneElts;
3548 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003549 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003550 return true;
3551}
3552
Craig Topper6347e862011-11-21 06:57:39 +00003553bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003554 SmallVector<int, 8> M;
3555 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003556 return ::isUNPCKHMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003557}
3558
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003559/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3560/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3561/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003562static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003563 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003564 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003565 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003566
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003567 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3568 // FIXME: Need a better way to get rid of this, there's no latency difference
3569 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3570 // the former later. We should also remove the "_undef" special mask.
3571 if (NumElems == 4 && VT.getSizeInBits() == 256)
3572 return false;
3573
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003574 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3575 // independently on 128-bit lanes.
3576 unsigned NumLanes = VT.getSizeInBits() / 128;
3577 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003578
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003579 for (unsigned s = 0; s < NumLanes; ++s) {
3580 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3581 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003582 i += 2, ++j) {
3583 int BitI = Mask[i];
3584 int BitI1 = Mask[i+1];
3585
3586 if (!isUndefOrEqual(BitI, j))
3587 return false;
3588 if (!isUndefOrEqual(BitI1, j))
3589 return false;
3590 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003591 }
David Greenea20244d2011-03-02 17:23:43 +00003592
Rafael Espindola15684b22009-04-24 12:40:33 +00003593 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003594}
3595
Nate Begeman9008ca62009-04-27 18:41:29 +00003596bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3597 SmallVector<int, 8> M;
3598 N->getMask(M);
3599 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3600}
3601
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003602/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3603/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3604/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003605static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003606 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003607 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3608 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003609
Nate Begeman9008ca62009-04-27 18:41:29 +00003610 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3611 int BitI = Mask[i];
3612 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003613 if (!isUndefOrEqual(BitI, j))
3614 return false;
3615 if (!isUndefOrEqual(BitI1, j))
3616 return false;
3617 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003618 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003619}
3620
Nate Begeman9008ca62009-04-27 18:41:29 +00003621bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3622 SmallVector<int, 8> M;
3623 N->getMask(M);
3624 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3625}
3626
Evan Cheng017dcc62006-04-21 01:05:10 +00003627/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3628/// specifies a shuffle of elements that is suitable for input to MOVSS,
3629/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003630static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003631 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003632 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003633
3634 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003635
Nate Begeman9008ca62009-04-27 18:41:29 +00003636 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003637 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003638
Nate Begeman9008ca62009-04-27 18:41:29 +00003639 for (int i = 1; i < NumElts; ++i)
3640 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003641 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003642
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003643 return true;
3644}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003645
Nate Begeman9008ca62009-04-27 18:41:29 +00003646bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3647 SmallVector<int, 8> M;
3648 N->getMask(M);
3649 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003650}
3651
Craig Topper70b883b2011-11-28 10:14:51 +00003652/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003653/// as permutations between 128-bit chunks or halves. As an example: this
3654/// shuffle bellow:
3655/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3656/// The first half comes from the second half of V1 and the second half from the
3657/// the second half of V2.
Craig Topper70b883b2011-11-28 10:14:51 +00003658static bool isVPERM2X128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3659 bool HasAVX) {
3660 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003661 return false;
3662
3663 // The shuffle result is divided into half A and half B. In total the two
3664 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3665 // B must come from C, D, E or F.
3666 int HalfSize = VT.getVectorNumElements()/2;
3667 bool MatchA = false, MatchB = false;
3668
3669 // Check if A comes from one of C, D, E, F.
3670 for (int Half = 0; Half < 4; ++Half) {
3671 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3672 MatchA = true;
3673 break;
3674 }
3675 }
3676
3677 // Check if B comes from one of C, D, E, F.
3678 for (int Half = 0; Half < 4; ++Half) {
3679 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3680 MatchB = true;
3681 break;
3682 }
3683 }
3684
3685 return MatchA && MatchB;
3686}
3687
Craig Topper70b883b2011-11-28 10:14:51 +00003688/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3689/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003690static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003691 EVT VT = SVOp->getValueType(0);
3692
3693 int HalfSize = VT.getVectorNumElements()/2;
3694
3695 int FstHalf = 0, SndHalf = 0;
3696 for (int i = 0; i < HalfSize; ++i) {
3697 if (SVOp->getMaskElt(i) > 0) {
3698 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3699 break;
3700 }
3701 }
3702 for (int i = HalfSize; i < HalfSize*2; ++i) {
3703 if (SVOp->getMaskElt(i) > 0) {
3704 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3705 break;
3706 }
3707 }
3708
3709 return (FstHalf | (SndHalf << 4));
3710}
3711
Craig Topper70b883b2011-11-28 10:14:51 +00003712/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003713/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3714/// Note that VPERMIL mask matching is different depending whether theunderlying
3715/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3716/// to the same elements of the low, but to the higher half of the source.
3717/// In VPERMILPD the two lanes could be shuffled independently of each other
3718/// with the same restriction that lanes can't be crossed.
Craig Topper70b883b2011-11-28 10:14:51 +00003719static bool isVPERMILPMask(const SmallVectorImpl<int> &Mask, EVT VT,
3720 bool HasAVX) {
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003721 int NumElts = VT.getVectorNumElements();
3722 int NumLanes = VT.getSizeInBits()/128;
3723
Craig Topper70b883b2011-11-28 10:14:51 +00003724 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003725 return false;
3726
Craig Topper70b883b2011-11-28 10:14:51 +00003727 // Only match 256-bit with 32/64-bit types
3728 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003729 return false;
3730
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003731 int LaneSize = NumElts/NumLanes;
Craig Topper70b883b2011-11-28 10:14:51 +00003732 for (int l = 0; l != NumLanes; ++l) {
3733 int LaneStart = l*LaneSize;
3734 for (int i = 0; i != LaneSize; ++i) {
3735 if (!isUndefOrInRange(Mask[i+LaneStart], LaneStart, LaneStart+LaneSize))
3736 return false;
3737 if (NumElts == 4 || l == 0)
3738 continue;
3739 // VPERMILPS handling
3740 if (Mask[i] < 0)
3741 continue;
3742 if (!isUndefOrEqual(Mask[i+LaneStart], Mask[i]+LaneSize))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003743 return false;
3744 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003745 }
3746
3747 return true;
3748}
3749
Craig Topper70b883b2011-11-28 10:14:51 +00003750/// getShuffleVPERMILPImmediate - Return the appropriate immediate to shuffle
3751/// the specified VECTOR_MASK mask with VPERMILPS/D* instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003752static unsigned getShuffleVPERMILPImmediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003753 EVT VT = SVOp->getValueType(0);
3754
3755 int NumElts = VT.getVectorNumElements();
3756 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003757 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003758
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003759 // Although the mask is equal for both lanes do it twice to get the cases
3760 // where a mask will match because the same mask element is undef on the
3761 // first half but valid on the second. This would get pathological cases
3762 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Craig Topper70b883b2011-11-28 10:14:51 +00003763 unsigned Shift = (LaneSize == 4) ? 2 : 1;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003764 unsigned Mask = 0;
Craig Topper70b883b2011-11-28 10:14:51 +00003765 for (int i = 0; i != NumElts; ++i) {
3766 int MaskElt = SVOp->getMaskElt(i);
3767 if (MaskElt < 0)
3768 continue;
3769 MaskElt %= LaneSize;
3770 unsigned Shamt = i;
3771 // VPERMILPSY, the mask of the first half must be equal to the second one
3772 if (NumElts == 8) Shamt %= LaneSize;
3773 Mask |= MaskElt << (Shamt*Shift);
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003774 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003775
3776 return Mask;
3777}
3778
Evan Cheng017dcc62006-04-21 01:05:10 +00003779/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3780/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003781/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003782static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003783 bool V2IsSplat = false, bool V2IsUndef = false) {
3784 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003785 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003786 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003787
Nate Begeman9008ca62009-04-27 18:41:29 +00003788 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003789 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003790
Nate Begeman9008ca62009-04-27 18:41:29 +00003791 for (int i = 1; i < NumOps; ++i)
3792 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3793 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3794 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003795 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003796
Evan Cheng39623da2006-04-20 08:58:49 +00003797 return true;
3798}
3799
Nate Begeman9008ca62009-04-27 18:41:29 +00003800static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003801 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003802 SmallVector<int, 8> M;
3803 N->getMask(M);
3804 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003805}
3806
Evan Chengd9539472006-04-14 21:59:03 +00003807/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3808/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003809/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3810bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3811 const X86Subtarget *Subtarget) {
Craig Topperc0d82852011-11-22 00:44:41 +00003812 if (!Subtarget->hasSSE3orAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003813 return false;
3814
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003815 // The second vector must be undef
3816 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3817 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003818
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003819 EVT VT = N->getValueType(0);
3820 unsigned NumElems = VT.getVectorNumElements();
3821
3822 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3823 (VT.getSizeInBits() == 256 && NumElems != 8))
3824 return false;
3825
3826 // "i+1" is the value the indexed mask element must have
3827 for (unsigned i = 0; i < NumElems; i += 2)
3828 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3829 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003830 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003831
3832 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003833}
3834
3835/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3836/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003837/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3838bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3839 const X86Subtarget *Subtarget) {
Craig Topperc0d82852011-11-22 00:44:41 +00003840 if (!Subtarget->hasSSE3orAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003841 return false;
3842
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003843 // The second vector must be undef
3844 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3845 return false;
3846
3847 EVT VT = N->getValueType(0);
3848 unsigned NumElems = VT.getVectorNumElements();
3849
3850 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3851 (VT.getSizeInBits() == 256 && NumElems != 8))
3852 return false;
3853
3854 // "i" is the value the indexed mask element must have
3855 for (unsigned i = 0; i < NumElems; i += 2)
3856 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3857 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003858 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003859
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003860 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003861}
3862
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003863/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3864/// specifies a shuffle of elements that is suitable for input to 256-bit
3865/// version of MOVDDUP.
Craig Topperbeabc6c2011-12-05 06:56:46 +00003866static bool isMOVDDUPYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3867 bool HasAVX) {
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003868 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003869
Craig Topperbeabc6c2011-12-05 06:56:46 +00003870 if (!HasAVX || VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003871 return false;
3872
3873 for (int i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003874 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003875 return false;
3876 for (int i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003877 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003878 return false;
3879 return true;
3880}
3881
Evan Cheng0b457f02008-09-25 20:50:48 +00003882/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003883/// specifies a shuffle of elements that is suitable for input to 128-bit
3884/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003885bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003886 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003887
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003888 if (VT.getSizeInBits() != 128)
3889 return false;
3890
3891 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003892 for (int i = 0; i < e; ++i)
3893 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003894 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003895 for (int i = 0; i < e; ++i)
3896 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003897 return false;
3898 return true;
3899}
3900
David Greenec38a03e2011-02-03 15:50:00 +00003901/// isVEXTRACTF128Index - Return true if the specified
3902/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3903/// suitable for input to VEXTRACTF128.
3904bool X86::isVEXTRACTF128Index(SDNode *N) {
3905 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3906 return false;
3907
3908 // The index should be aligned on a 128-bit boundary.
3909 uint64_t Index =
3910 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3911
3912 unsigned VL = N->getValueType(0).getVectorNumElements();
3913 unsigned VBits = N->getValueType(0).getSizeInBits();
3914 unsigned ElSize = VBits / VL;
3915 bool Result = (Index * ElSize) % 128 == 0;
3916
3917 return Result;
3918}
3919
David Greeneccacdc12011-02-04 16:08:29 +00003920/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3921/// operand specifies a subvector insert that is suitable for input to
3922/// VINSERTF128.
3923bool X86::isVINSERTF128Index(SDNode *N) {
3924 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3925 return false;
3926
3927 // The index should be aligned on a 128-bit boundary.
3928 uint64_t Index =
3929 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3930
3931 unsigned VL = N->getValueType(0).getVectorNumElements();
3932 unsigned VBits = N->getValueType(0).getSizeInBits();
3933 unsigned ElSize = VBits / VL;
3934 bool Result = (Index * ElSize) % 128 == 0;
3935
3936 return Result;
3937}
3938
Evan Cheng63d33002006-03-22 08:01:21 +00003939/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003940/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003941unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003942 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3943 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3944
Evan Chengb9df0ca2006-03-22 02:53:00 +00003945 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3946 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003947 for (int i = 0; i < NumOperands; ++i) {
3948 int Val = SVOp->getMaskElt(NumOperands-i-1);
3949 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003950 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003951 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003952 if (i != NumOperands - 1)
3953 Mask <<= Shift;
3954 }
Evan Cheng63d33002006-03-22 08:01:21 +00003955 return Mask;
3956}
3957
Evan Cheng506d3df2006-03-29 23:07:14 +00003958/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003959/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003960unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003961 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003962 unsigned Mask = 0;
3963 // 8 nodes, but we only care about the last 4.
3964 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003965 int Val = SVOp->getMaskElt(i);
3966 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003967 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003968 if (i != 4)
3969 Mask <<= 2;
3970 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003971 return Mask;
3972}
3973
3974/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003975/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003976unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003977 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003978 unsigned Mask = 0;
3979 // 8 nodes, but we only care about the first 4.
3980 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003981 int Val = SVOp->getMaskElt(i);
3982 if (Val >= 0)
3983 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003984 if (i != 0)
3985 Mask <<= 2;
3986 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003987 return Mask;
3988}
3989
Nate Begemana09008b2009-10-19 02:17:23 +00003990/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3991/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00003992static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
3993 EVT VT = SVOp->getValueType(0);
3994 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00003995 int Val = 0;
3996
3997 unsigned i, e;
Craig Topperd93e4c32011-12-11 19:12:35 +00003998 for (i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00003999 Val = SVOp->getMaskElt(i);
4000 if (Val >= 0)
4001 break;
4002 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004003 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004004 return (Val - i) * EltSize;
4005}
4006
David Greenec38a03e2011-02-03 15:50:00 +00004007/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4008/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4009/// instructions.
4010unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4011 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4012 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4013
4014 uint64_t Index =
4015 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4016
4017 EVT VecVT = N->getOperand(0).getValueType();
4018 EVT ElVT = VecVT.getVectorElementType();
4019
4020 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004021 return Index / NumElemsPerChunk;
4022}
4023
David Greeneccacdc12011-02-04 16:08:29 +00004024/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4025/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4026/// instructions.
4027unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4028 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4029 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4030
4031 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004032 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004033
4034 EVT VecVT = N->getValueType(0);
4035 EVT ElVT = VecVT.getVectorElementType();
4036
4037 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004038 return Index / NumElemsPerChunk;
4039}
4040
Evan Cheng37b73872009-07-30 08:33:02 +00004041/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4042/// constant +0.0.
4043bool X86::isZeroNode(SDValue Elt) {
4044 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004045 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004046 (isa<ConstantFPSDNode>(Elt) &&
4047 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4048}
4049
Nate Begeman9008ca62009-04-27 18:41:29 +00004050/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4051/// their permute mask.
4052static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4053 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004054 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004055 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004056 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004057
Nate Begeman5a5ca152009-04-29 05:20:52 +00004058 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004059 int idx = SVOp->getMaskElt(i);
4060 if (idx < 0)
4061 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004062 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004063 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004064 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004065 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004066 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004067 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4068 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004069}
4070
Evan Cheng533a0aa2006-04-19 20:35:22 +00004071/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4072/// match movhlps. The lower half elements should come from upper half of
4073/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004074/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004075static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004076 EVT VT = Op->getValueType(0);
4077 if (VT.getSizeInBits() != 128)
4078 return false;
4079 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004080 return false;
4081 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004082 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004083 return false;
4084 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004085 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004086 return false;
4087 return true;
4088}
4089
Evan Cheng5ced1d82006-04-06 23:23:56 +00004090/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004091/// is promoted to a vector. It also returns the LoadSDNode by reference if
4092/// required.
4093static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004094 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4095 return false;
4096 N = N->getOperand(0).getNode();
4097 if (!ISD::isNON_EXTLoad(N))
4098 return false;
4099 if (LD)
4100 *LD = cast<LoadSDNode>(N);
4101 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004102}
4103
Dan Gohman65fd6562011-11-03 21:49:52 +00004104// Test whether the given value is a vector value which will be legalized
4105// into a load.
4106static bool WillBeConstantPoolLoad(SDNode *N) {
4107 if (N->getOpcode() != ISD::BUILD_VECTOR)
4108 return false;
4109
4110 // Check for any non-constant elements.
4111 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4112 switch (N->getOperand(i).getNode()->getOpcode()) {
4113 case ISD::UNDEF:
4114 case ISD::ConstantFP:
4115 case ISD::Constant:
4116 break;
4117 default:
4118 return false;
4119 }
4120
4121 // Vectors of all-zeros and all-ones are materialized with special
4122 // instructions rather than being loaded.
4123 return !ISD::isBuildVectorAllZeros(N) &&
4124 !ISD::isBuildVectorAllOnes(N);
4125}
4126
Evan Cheng533a0aa2006-04-19 20:35:22 +00004127/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4128/// match movlp{s|d}. The lower half elements should come from lower half of
4129/// V1 (and in order), and the upper half elements should come from the upper
4130/// half of V2 (and in order). And since V1 will become the source of the
4131/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004132static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4133 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004134 EVT VT = Op->getValueType(0);
4135 if (VT.getSizeInBits() != 128)
4136 return false;
4137
Evan Cheng466685d2006-10-09 20:57:25 +00004138 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004139 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004140 // Is V2 is a vector load, don't do this transformation. We will try to use
4141 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004142 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004143 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004144
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004145 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004146
Evan Cheng533a0aa2006-04-19 20:35:22 +00004147 if (NumElems != 2 && NumElems != 4)
4148 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004149 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004150 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004151 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004152 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004153 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004154 return false;
4155 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004156}
4157
Evan Cheng39623da2006-04-20 08:58:49 +00004158/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4159/// all the same.
4160static bool isSplatVector(SDNode *N) {
4161 if (N->getOpcode() != ISD::BUILD_VECTOR)
4162 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004163
Dan Gohman475871a2008-07-27 21:46:04 +00004164 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004165 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4166 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004167 return false;
4168 return true;
4169}
4170
Evan Cheng213d2cf2007-05-17 18:45:50 +00004171/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004172/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004173/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004174static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004175 SDValue V1 = N->getOperand(0);
4176 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004177 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4178 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004179 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004180 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004181 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004182 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4183 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004184 if (Opc != ISD::BUILD_VECTOR ||
4185 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004186 return false;
4187 } else if (Idx >= 0) {
4188 unsigned Opc = V1.getOpcode();
4189 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4190 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004191 if (Opc != ISD::BUILD_VECTOR ||
4192 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004193 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004194 }
4195 }
4196 return true;
4197}
4198
4199/// getZeroVector - Returns a vector of specified type with all zero elements.
4200///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004201static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004202 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004203 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004204
Dale Johannesen0488fb62010-09-30 23:57:10 +00004205 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004206 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004207 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004208 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004209 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004210 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4211 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4212 } else { // SSE1
4213 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4214 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4215 }
4216 } else if (VT.getSizeInBits() == 256) { // AVX
4217 // 256-bit logic and arithmetic instructions in AVX are
4218 // all floating-point, no support for integer ops. Default
4219 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004220 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004221 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4222 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004223 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004224 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004225}
4226
Chris Lattner8a594482007-11-25 00:24:49 +00004227/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004228/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4229/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4230/// Then bitcast to their original type, ensuring they get CSE'd.
4231static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4232 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004233 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004234 assert((VT.is128BitVector() || VT.is256BitVector())
4235 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004236
Owen Anderson825b72b2009-08-11 20:47:22 +00004237 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004238 SDValue Vec;
4239 if (VT.getSizeInBits() == 256) {
4240 if (HasAVX2) { // AVX2
4241 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4242 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4243 } else { // AVX
4244 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4245 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4246 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4247 Vec = Insert128BitVector(InsV, Vec,
4248 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4249 }
4250 } else {
4251 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004252 }
4253
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004254 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004255}
4256
Evan Cheng39623da2006-04-20 08:58:49 +00004257/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4258/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004259static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004260 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004261 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004262
Evan Cheng39623da2006-04-20 08:58:49 +00004263 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004264 SmallVector<int, 8> MaskVec;
4265 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004266
Nate Begeman5a5ca152009-04-29 05:20:52 +00004267 for (unsigned i = 0; i != NumElems; ++i) {
4268 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004269 MaskVec[i] = NumElems;
4270 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004271 }
Evan Cheng39623da2006-04-20 08:58:49 +00004272 }
Evan Cheng39623da2006-04-20 08:58:49 +00004273 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004274 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4275 SVOp->getOperand(1), &MaskVec[0]);
4276 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004277}
4278
Evan Cheng017dcc62006-04-21 01:05:10 +00004279/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4280/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004281static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004282 SDValue V2) {
4283 unsigned NumElems = VT.getVectorNumElements();
4284 SmallVector<int, 8> Mask;
4285 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004286 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004287 Mask.push_back(i);
4288 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004289}
4290
Nate Begeman9008ca62009-04-27 18:41:29 +00004291/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004292static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004293 SDValue V2) {
4294 unsigned NumElems = VT.getVectorNumElements();
4295 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004296 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004297 Mask.push_back(i);
4298 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004299 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004300 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004301}
4302
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004303/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004304static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004305 SDValue V2) {
4306 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004307 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004308 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004309 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004310 Mask.push_back(i + Half);
4311 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004312 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004313 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004314}
4315
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004316// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004317// a generic shuffle instruction because the target has no such instructions.
4318// Generate shuffles which repeat i16 and i8 several times until they can be
4319// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004320static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004321 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004322 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004323 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004324
Nate Begeman9008ca62009-04-27 18:41:29 +00004325 while (NumElems > 4) {
4326 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004327 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004328 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004329 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004330 EltNo -= NumElems/2;
4331 }
4332 NumElems >>= 1;
4333 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004334 return V;
4335}
Eric Christopherfd179292009-08-27 18:07:15 +00004336
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004337/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4338static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4339 EVT VT = V.getValueType();
4340 DebugLoc dl = V.getDebugLoc();
4341 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4342 && "Vector size not supported");
4343
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004344 if (VT.getSizeInBits() == 128) {
4345 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004346 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004347 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4348 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004349 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004350 // To use VPERMILPS to splat scalars, the second half of indicies must
4351 // refer to the higher part, which is a duplication of the lower one,
4352 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004353 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4354 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004355
4356 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4357 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4358 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004359 }
4360
4361 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4362}
4363
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004364/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004365static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4366 EVT SrcVT = SV->getValueType(0);
4367 SDValue V1 = SV->getOperand(0);
4368 DebugLoc dl = SV->getDebugLoc();
4369
4370 int EltNo = SV->getSplatIndex();
4371 int NumElems = SrcVT.getVectorNumElements();
4372 unsigned Size = SrcVT.getSizeInBits();
4373
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004374 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4375 "Unknown how to promote splat for type");
4376
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004377 // Extract the 128-bit part containing the splat element and update
4378 // the splat element index when it refers to the higher register.
4379 if (Size == 256) {
4380 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4381 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4382 if (Idx > 0)
4383 EltNo -= NumElems/2;
4384 }
4385
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004386 // All i16 and i8 vector types can't be used directly by a generic shuffle
4387 // instruction because the target has no such instruction. Generate shuffles
4388 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004389 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004390 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004391 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004392 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004393
4394 // Recreate the 256-bit vector and place the same 128-bit vector
4395 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004396 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004397 if (Size == 256) {
4398 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4399 DAG.getConstant(0, MVT::i32), DAG, dl);
4400 V1 = Insert128BitVector(InsV, V1,
4401 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4402 }
4403
4404 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004405}
4406
Evan Chengba05f722006-04-21 23:03:30 +00004407/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004408/// vector of zero or undef vector. This produces a shuffle where the low
4409/// element of V2 is swizzled into the zero/undef vector, landing at element
4410/// 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 +00004411static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004412 bool isZero, bool HasXMMInt,
4413 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004414 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004415 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004416 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004417 unsigned NumElems = VT.getVectorNumElements();
4418 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004419 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004420 // If this is the insertion idx, put the low elt of V2 here.
4421 MaskVec.push_back(i == Idx ? NumElems : i);
4422 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004423}
4424
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004425/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4426/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004427static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4428 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004429 if (Depth == 6)
4430 return SDValue(); // Limit search depth.
4431
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004432 SDValue V = SDValue(N, 0);
4433 EVT VT = V.getValueType();
4434 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004435
4436 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4437 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4438 Index = SV->getMaskElt(Index);
4439
4440 if (Index < 0)
4441 return DAG.getUNDEF(VT.getVectorElementType());
4442
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004443 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004444 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004445 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004446 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004447
4448 // Recurse into target specific vector shuffles to find scalars.
4449 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004450 int NumElems = VT.getVectorNumElements();
4451 SmallVector<unsigned, 16> ShuffleMask;
4452 SDValue ImmN;
4453
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004454 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004455 case X86ISD::SHUFPS:
4456 case X86ISD::SHUFPD:
4457 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper36e36ac2011-11-29 07:49:05 +00004458 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4459 ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004460 break;
Craig Topper34671b82011-12-06 08:21:25 +00004461 case X86ISD::UNPCKH:
Craig Topper3d8c2ce2011-12-06 05:31:16 +00004462 DecodeUNPCKHMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004463 break;
Craig Topper34671b82011-12-06 08:21:25 +00004464 case X86ISD::UNPCKL:
Craig Topper3d8c2ce2011-12-06 05:31:16 +00004465 DecodeUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004466 break;
4467 case X86ISD::MOVHLPS:
4468 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4469 break;
4470 case X86ISD::MOVLHPS:
4471 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4472 break;
4473 case X86ISD::PSHUFD:
4474 ImmN = N->getOperand(N->getNumOperands()-1);
4475 DecodePSHUFMask(NumElems,
4476 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4477 ShuffleMask);
4478 break;
4479 case X86ISD::PSHUFHW:
4480 ImmN = N->getOperand(N->getNumOperands()-1);
4481 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4482 ShuffleMask);
4483 break;
4484 case X86ISD::PSHUFLW:
4485 ImmN = N->getOperand(N->getNumOperands()-1);
4486 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4487 ShuffleMask);
4488 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004489 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004490 case X86ISD::MOVSD: {
4491 // The index 0 always comes from the first element of the second source,
4492 // this is why MOVSS and MOVSD are used in the first place. The other
4493 // elements come from the other positions of the first source vector.
4494 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004495 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4496 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004497 }
Craig Topper316cd2a2011-11-30 06:25:25 +00004498 case X86ISD::VPERMILP:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004499 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper316cd2a2011-11-30 06:25:25 +00004500 DecodeVPERMILPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004501 ShuffleMask);
4502 break;
Craig Topperec24e612011-11-30 07:47:51 +00004503 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004504 ImmN = N->getOperand(N->getNumOperands()-1);
4505 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4506 ShuffleMask);
4507 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004508 case X86ISD::MOVDDUP:
4509 case X86ISD::MOVLHPD:
4510 case X86ISD::MOVLPD:
4511 case X86ISD::MOVLPS:
4512 case X86ISD::MOVSHDUP:
4513 case X86ISD::MOVSLDUP:
4514 case X86ISD::PALIGN:
4515 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004516 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004517 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004518 return SDValue();
4519 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004520
4521 Index = ShuffleMask[Index];
4522 if (Index < 0)
4523 return DAG.getUNDEF(VT.getVectorElementType());
4524
4525 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4526 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4527 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004528 }
4529
4530 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004531 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004532 V = V.getOperand(0);
4533 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004534 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004535
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004536 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004537 return SDValue();
4538 }
4539
4540 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4541 return (Index == 0) ? V.getOperand(0)
4542 : DAG.getUNDEF(VT.getVectorElementType());
4543
4544 if (V.getOpcode() == ISD::BUILD_VECTOR)
4545 return V.getOperand(Index);
4546
4547 return SDValue();
4548}
4549
4550/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4551/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004552/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004553static
4554unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4555 bool ZerosFromLeft, SelectionDAG &DAG) {
4556 int i = 0;
4557
4558 while (i < NumElems) {
4559 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004560 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004561 if (!(Elt.getNode() &&
4562 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4563 break;
4564 ++i;
4565 }
4566
4567 return i;
4568}
4569
4570/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4571/// MaskE correspond consecutively to elements from one of the vector operands,
4572/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4573static
4574bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4575 int OpIdx, int NumElems, unsigned &OpNum) {
4576 bool SeenV1 = false;
4577 bool SeenV2 = false;
4578
4579 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4580 int Idx = SVOp->getMaskElt(i);
4581 // Ignore undef indicies
4582 if (Idx < 0)
4583 continue;
4584
4585 if (Idx < NumElems)
4586 SeenV1 = true;
4587 else
4588 SeenV2 = true;
4589
4590 // Only accept consecutive elements from the same vector
4591 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4592 return false;
4593 }
4594
4595 OpNum = SeenV1 ? 0 : 1;
4596 return true;
4597}
4598
4599/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4600/// logical left shift of a vector.
4601static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4602 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4603 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4604 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4605 false /* check zeros from right */, DAG);
4606 unsigned OpSrc;
4607
4608 if (!NumZeros)
4609 return false;
4610
4611 // Considering the elements in the mask that are not consecutive zeros,
4612 // check if they consecutively come from only one of the source vectors.
4613 //
4614 // V1 = {X, A, B, C} 0
4615 // \ \ \ /
4616 // vector_shuffle V1, V2 <1, 2, 3, X>
4617 //
4618 if (!isShuffleMaskConsecutive(SVOp,
4619 0, // Mask Start Index
4620 NumElems-NumZeros-1, // Mask End Index
4621 NumZeros, // Where to start looking in the src vector
4622 NumElems, // Number of elements in vector
4623 OpSrc)) // Which source operand ?
4624 return false;
4625
4626 isLeft = false;
4627 ShAmt = NumZeros;
4628 ShVal = SVOp->getOperand(OpSrc);
4629 return true;
4630}
4631
4632/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4633/// logical left shift of a vector.
4634static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4635 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4636 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4637 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4638 true /* check zeros from left */, DAG);
4639 unsigned OpSrc;
4640
4641 if (!NumZeros)
4642 return false;
4643
4644 // Considering the elements in the mask that are not consecutive zeros,
4645 // check if they consecutively come from only one of the source vectors.
4646 //
4647 // 0 { A, B, X, X } = V2
4648 // / \ / /
4649 // vector_shuffle V1, V2 <X, X, 4, 5>
4650 //
4651 if (!isShuffleMaskConsecutive(SVOp,
4652 NumZeros, // Mask Start Index
4653 NumElems-1, // Mask End Index
4654 0, // Where to start looking in the src vector
4655 NumElems, // Number of elements in vector
4656 OpSrc)) // Which source operand ?
4657 return false;
4658
4659 isLeft = true;
4660 ShAmt = NumZeros;
4661 ShVal = SVOp->getOperand(OpSrc);
4662 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004663}
4664
4665/// isVectorShift - Returns true if the shuffle can be implemented as a
4666/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004667static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004668 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004669 // Although the logic below support any bitwidth size, there are no
4670 // shift instructions which handle more than 128-bit vectors.
4671 if (SVOp->getValueType(0).getSizeInBits() > 128)
4672 return false;
4673
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004674 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4675 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4676 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004677
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004678 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004679}
4680
Evan Chengc78d3b42006-04-24 18:01:45 +00004681/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4682///
Dan Gohman475871a2008-07-27 21:46:04 +00004683static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004684 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004685 SelectionDAG &DAG,
4686 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004687 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004688 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004689
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004690 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004691 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004692 bool First = true;
4693 for (unsigned i = 0; i < 16; ++i) {
4694 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4695 if (ThisIsNonZero && First) {
4696 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004697 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004698 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004699 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004700 First = false;
4701 }
4702
4703 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004704 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004705 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4706 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004707 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004708 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004709 }
4710 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004711 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4712 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4713 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004714 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004715 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004716 } else
4717 ThisElt = LastElt;
4718
Gabor Greifba36cb52008-08-28 21:40:38 +00004719 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004720 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004721 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004722 }
4723 }
4724
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004725 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004726}
4727
Bill Wendlinga348c562007-03-22 18:42:45 +00004728/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004729///
Dan Gohman475871a2008-07-27 21:46:04 +00004730static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004731 unsigned NumNonZero, unsigned NumZero,
4732 SelectionDAG &DAG,
4733 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004734 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004735 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004736
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004737 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004738 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004739 bool First = true;
4740 for (unsigned i = 0; i < 8; ++i) {
4741 bool isNonZero = (NonZeros & (1 << i)) != 0;
4742 if (isNonZero) {
4743 if (First) {
4744 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004745 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004746 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004747 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004748 First = false;
4749 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004750 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004751 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004752 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004753 }
4754 }
4755
4756 return V;
4757}
4758
Evan Chengf26ffe92008-05-29 08:22:04 +00004759/// getVShift - Return a vector logical shift node.
4760///
Owen Andersone50ed302009-08-10 22:56:29 +00004761static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004762 unsigned NumBits, SelectionDAG &DAG,
4763 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004764 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004765 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004766 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004767 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4768 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004769 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004770 DAG.getConstant(NumBits,
4771 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004772}
4773
Dan Gohman475871a2008-07-27 21:46:04 +00004774SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004775X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004776 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004777
Evan Chengc3630942009-12-09 21:00:30 +00004778 // Check if the scalar load can be widened into a vector load. And if
4779 // the address is "base + cst" see if the cst can be "absorbed" into
4780 // the shuffle mask.
4781 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4782 SDValue Ptr = LD->getBasePtr();
4783 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4784 return SDValue();
4785 EVT PVT = LD->getValueType(0);
4786 if (PVT != MVT::i32 && PVT != MVT::f32)
4787 return SDValue();
4788
4789 int FI = -1;
4790 int64_t Offset = 0;
4791 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4792 FI = FINode->getIndex();
4793 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004794 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004795 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4796 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4797 Offset = Ptr.getConstantOperandVal(1);
4798 Ptr = Ptr.getOperand(0);
4799 } else {
4800 return SDValue();
4801 }
4802
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004803 // FIXME: 256-bit vector instructions don't require a strict alignment,
4804 // improve this code to support it better.
4805 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004806 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004807 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004808 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004809 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004810 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004811 // Can't change the alignment. FIXME: It's possible to compute
4812 // the exact stack offset and reference FI + adjust offset instead.
4813 // If someone *really* cares about this. That's the way to implement it.
4814 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004815 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004816 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004817 }
4818 }
4819
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004820 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004821 // Ptr + (Offset & ~15).
4822 if (Offset < 0)
4823 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004824 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004825 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004826 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004827 if (StartOffset)
4828 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4829 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4830
4831 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004832 int NumElems = VT.getVectorNumElements();
4833
4834 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4835 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4836 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004837 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004838 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004839
4840 // Canonicalize it to a v4i32 or v8i32 shuffle.
4841 SmallVector<int, 8> Mask;
4842 for (int i = 0; i < NumElems; ++i)
4843 Mask.push_back(EltNo);
4844
4845 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4846 return DAG.getNode(ISD::BITCAST, dl, NVT,
4847 DAG.getVectorShuffle(CanonVT, dl, V1,
4848 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004849 }
4850
4851 return SDValue();
4852}
4853
Michael J. Spencerec38de22010-10-10 22:04:20 +00004854/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4855/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004856/// load which has the same value as a build_vector whose operands are 'elts'.
4857///
4858/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004859///
Nate Begeman1449f292010-03-24 22:19:06 +00004860/// FIXME: we'd also like to handle the case where the last elements are zero
4861/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4862/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004863static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004864 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004865 EVT EltVT = VT.getVectorElementType();
4866 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004867
Nate Begemanfdea31a2010-03-24 20:49:50 +00004868 LoadSDNode *LDBase = NULL;
4869 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004870
Nate Begeman1449f292010-03-24 22:19:06 +00004871 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004872 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004873 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004874 for (unsigned i = 0; i < NumElems; ++i) {
4875 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004876
Nate Begemanfdea31a2010-03-24 20:49:50 +00004877 if (!Elt.getNode() ||
4878 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4879 return SDValue();
4880 if (!LDBase) {
4881 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4882 return SDValue();
4883 LDBase = cast<LoadSDNode>(Elt.getNode());
4884 LastLoadedElt = i;
4885 continue;
4886 }
4887 if (Elt.getOpcode() == ISD::UNDEF)
4888 continue;
4889
4890 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4891 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4892 return SDValue();
4893 LastLoadedElt = i;
4894 }
Nate Begeman1449f292010-03-24 22:19:06 +00004895
4896 // If we have found an entire vector of loads and undefs, then return a large
4897 // load of the entire vector width starting at the base pointer. If we found
4898 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004899 if (LastLoadedElt == NumElems - 1) {
4900 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004901 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004902 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004903 LDBase->isVolatile(), LDBase->isNonTemporal(),
4904 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004905 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004906 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004907 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004908 LDBase->isInvariant(), LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00004909 } else if (NumElems == 4 && LastLoadedElt == 1 &&
4910 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004911 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4912 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00004913 SDValue ResNode =
4914 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
4915 LDBase->getPointerInfo(),
4916 LDBase->getAlignment(),
4917 false/*isVolatile*/, true/*ReadMem*/,
4918 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004919 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004920 }
4921 return SDValue();
4922}
4923
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004924/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
4925/// a vbroadcast node. We support two patterns:
4926/// 1. A splat BUILD_VECTOR which uses a single scalar load.
4927/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
4928/// a scalar load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004929/// The scalar load node is returned when a pattern is found,
4930/// or SDValue() otherwise.
4931static SDValue isVectorBroadcast(SDValue &Op, bool hasAVX2) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004932 EVT VT = Op.getValueType();
4933 SDValue V = Op;
4934
4935 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
4936 V = V.getOperand(0);
4937
4938 //A suspected load to be broadcasted.
4939 SDValue Ld;
4940
4941 switch (V.getOpcode()) {
4942 default:
4943 // Unknown pattern found.
4944 return SDValue();
4945
4946 case ISD::BUILD_VECTOR: {
4947 // The BUILD_VECTOR node must be a splat.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004948 if (!isSplatVector(V.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004949 return SDValue();
4950
4951 Ld = V.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004952
4953 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004954 // of its users are from the BUILD_VECTOR node.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004955 if (!Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004956 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004957 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004958 }
4959
4960 case ISD::VECTOR_SHUFFLE: {
4961 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
4962
4963 // Shuffles must have a splat mask where the first element is
4964 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004965 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004966 return SDValue();
4967
4968 SDValue Sc = Op.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004969 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004970 return SDValue();
4971
4972 Ld = Sc.getOperand(0);
4973
4974 // The scalar_to_vector node and the suspected
4975 // load node must have exactly one user.
4976 if (!Sc.hasOneUse() || !Ld.hasOneUse())
4977 return SDValue();
4978 break;
4979 }
4980 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004981
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004982 // The scalar source must be a normal load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004983 if (!ISD::isNormalLoad(Ld.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004984 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004985
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004986 bool Is256 = VT.getSizeInBits() == 256;
4987 bool Is128 = VT.getSizeInBits() == 128;
4988 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
4989
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004990 if (hasAVX2) {
4991 // VBroadcast to YMM
4992 if (Is256 && (ScalarSize == 8 || ScalarSize == 16 ||
4993 ScalarSize == 32 || ScalarSize == 64 ))
4994 return Ld;
4995
4996 // VBroadcast to XMM
4997 if (Is128 && (ScalarSize == 8 || ScalarSize == 32 ||
4998 ScalarSize == 16 || ScalarSize == 64 ))
4999 return Ld;
5000 }
5001
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005002 // VBroadcast to YMM
5003 if (Is256 && (ScalarSize == 32 || ScalarSize == 64))
5004 return Ld;
5005
5006 // VBroadcast to XMM
5007 if (Is128 && (ScalarSize == 32))
5008 return Ld;
5009
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005010
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005011 // Unsupported broadcast.
5012 return SDValue();
5013}
5014
Evan Chengc3630942009-12-09 21:00:30 +00005015SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005016X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005017 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005018
David Greenef125a292011-02-08 19:04:41 +00005019 EVT VT = Op.getValueType();
5020 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005021 unsigned NumElems = Op.getNumOperands();
5022
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005023 // Vectors containing all zeros can be matched by pxor and xorps later
5024 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5025 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5026 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005027 if (Op.getValueType() == MVT::v4i32 ||
5028 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005029 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005030
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005031 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005032 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005033
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005034 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005035 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5036 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005037 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper745a86b2011-11-19 22:34:59 +00005038 if (Op.getValueType() == MVT::v4i32 ||
5039 (Op.getValueType() == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005040 return Op;
5041
Craig Topper745a86b2011-11-19 22:34:59 +00005042 return getOnesVector(Op.getValueType(), Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005043 }
5044
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005045 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005046 if (Subtarget->hasAVX() && LD.getNode())
5047 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
5048
Owen Andersone50ed302009-08-10 22:56:29 +00005049 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005050
Evan Cheng0db9fe62006-04-25 20:13:52 +00005051 unsigned NumZero = 0;
5052 unsigned NumNonZero = 0;
5053 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005054 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005055 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005056 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005057 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005058 if (Elt.getOpcode() == ISD::UNDEF)
5059 continue;
5060 Values.insert(Elt);
5061 if (Elt.getOpcode() != ISD::Constant &&
5062 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005063 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005064 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005065 NumZero++;
5066 else {
5067 NonZeros |= (1 << i);
5068 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005069 }
5070 }
5071
Chris Lattner97a2a562010-08-26 05:24:29 +00005072 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5073 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005074 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005075
Chris Lattner67f453a2008-03-09 05:42:06 +00005076 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005077 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005078 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005079 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005080
Chris Lattner62098042008-03-09 01:05:04 +00005081 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5082 // the value are obviously zero, truncate the value to i32 and do the
5083 // insertion that way. Only do this if the value is non-constant or if the
5084 // value is a constant being inserted into element 0. It is cheaper to do
5085 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005086 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005087 (!IsAllConstants || Idx == 0)) {
5088 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005089 // Handle SSE only.
5090 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5091 EVT VecVT = MVT::v4i32;
5092 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005093
Chris Lattner62098042008-03-09 01:05:04 +00005094 // Truncate the value (which may itself be a constant) to i32, and
5095 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005096 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005097 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005098 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005099 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005100
Chris Lattner62098042008-03-09 01:05:04 +00005101 // Now we have our 32-bit value zero extended in the low element of
5102 // a vector. If Idx != 0, swizzle it into place.
5103 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005104 SmallVector<int, 4> Mask;
5105 Mask.push_back(Idx);
5106 for (unsigned i = 1; i != VecElts; ++i)
5107 Mask.push_back(i);
5108 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005109 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005110 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005111 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005112 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005113 }
5114 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005115
Chris Lattner19f79692008-03-08 22:59:52 +00005116 // If we have a constant or non-constant insertion into the low element of
5117 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5118 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005119 // depending on what the source datatype is.
5120 if (Idx == 0) {
5121 if (NumZero == 0) {
5122 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005123 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5124 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005125 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5126 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005127 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005128 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005129 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5130 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005131 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5132 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005133 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5134 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005135 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005136 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005137 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005138 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005139
5140 // Is it a vector logical left shift?
5141 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005142 X86::isZeroNode(Op.getOperand(0)) &&
5143 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005144 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005145 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005146 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005147 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005148 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005149 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005150
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005151 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005152 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005153
Chris Lattner19f79692008-03-08 22:59:52 +00005154 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5155 // is a non-constant being inserted into an element other than the low one,
5156 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5157 // movd/movss) to move this into the low element, then shuffle it into
5158 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005159 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005160 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005161
Evan Cheng0db9fe62006-04-25 20:13:52 +00005162 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005163 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005164 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005165 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005166 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005167 MaskVec.push_back(i == Idx ? 0 : 1);
5168 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005169 }
5170 }
5171
Chris Lattner67f453a2008-03-09 05:42:06 +00005172 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005173 if (Values.size() == 1) {
5174 if (EVTBits == 32) {
5175 // Instead of a shuffle like this:
5176 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5177 // Check if it's possible to issue this instead.
5178 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5179 unsigned Idx = CountTrailingZeros_32(NonZeros);
5180 SDValue Item = Op.getOperand(Idx);
5181 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5182 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5183 }
Dan Gohman475871a2008-07-27 21:46:04 +00005184 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005185 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005186
Dan Gohmana3941172007-07-24 22:55:08 +00005187 // A vector full of immediates; various special cases are already
5188 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005189 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005190 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005191
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005192 // For AVX-length vectors, build the individual 128-bit pieces and use
5193 // shuffles to put them in place.
5194 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5195 SmallVector<SDValue, 32> V;
5196 for (unsigned i = 0; i < NumElems; ++i)
5197 V.push_back(Op.getOperand(i));
5198
5199 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5200
5201 // Build both the lower and upper subvector.
5202 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5203 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5204 NumElems/2);
5205
5206 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005207 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5208 DAG.getConstant(0, MVT::i32), DAG, dl);
5209 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005210 DAG, dl);
5211 }
5212
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005213 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005214 if (EVTBits == 64) {
5215 if (NumNonZero == 1) {
5216 // One half is zero or undef.
5217 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005218 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005219 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005220 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005221 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005222 }
Dan Gohman475871a2008-07-27 21:46:04 +00005223 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005224 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005225
5226 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005227 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005228 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005229 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005230 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005231 }
5232
Bill Wendling826f36f2007-03-28 00:57:11 +00005233 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005234 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005235 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005236 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005237 }
5238
5239 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005240 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005241 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005242 if (NumElems == 4 && NumZero > 0) {
5243 for (unsigned i = 0; i < 4; ++i) {
5244 bool isZero = !(NonZeros & (1 << i));
5245 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005246 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005247 else
Dale Johannesenace16102009-02-03 19:33:06 +00005248 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005249 }
5250
5251 for (unsigned i = 0; i < 2; ++i) {
5252 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5253 default: break;
5254 case 0:
5255 V[i] = V[i*2]; // Must be a zero vector.
5256 break;
5257 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005258 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005259 break;
5260 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005261 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005262 break;
5263 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005264 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005265 break;
5266 }
5267 }
5268
Nate Begeman9008ca62009-04-27 18:41:29 +00005269 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005270 bool Reverse = (NonZeros & 0x3) == 2;
5271 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005272 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005273 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5274 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005275 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5276 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005277 }
5278
Nate Begemanfdea31a2010-03-24 20:49:50 +00005279 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5280 // Check for a build vector of consecutive loads.
5281 for (unsigned i = 0; i < NumElems; ++i)
5282 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005283
Nate Begemanfdea31a2010-03-24 20:49:50 +00005284 // Check for elements which are consecutive loads.
5285 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5286 if (LD.getNode())
5287 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005288
5289 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperc0d82852011-11-22 00:44:41 +00005290 if (getSubtarget()->hasSSE41orAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005291 SDValue Result;
5292 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5293 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5294 else
5295 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005296
Chris Lattner24faf612010-08-28 17:59:08 +00005297 for (unsigned i = 1; i < NumElems; ++i) {
5298 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5299 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005300 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005301 }
5302 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005303 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005304
Chris Lattner6e80e442010-08-28 17:15:43 +00005305 // Otherwise, expand into a number of unpckl*, start by extending each of
5306 // our (non-undef) elements to the full vector width with the element in the
5307 // bottom slot of the vector (which generates no code for SSE).
5308 for (unsigned i = 0; i < NumElems; ++i) {
5309 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5310 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5311 else
5312 V[i] = DAG.getUNDEF(VT);
5313 }
5314
5315 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005316 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5317 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5318 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005319 unsigned EltStride = NumElems >> 1;
5320 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005321 for (unsigned i = 0; i < EltStride; ++i) {
5322 // If V[i+EltStride] is undef and this is the first round of mixing,
5323 // then it is safe to just drop this shuffle: V[i] is already in the
5324 // right place, the one element (since it's the first round) being
5325 // inserted as undef can be dropped. This isn't safe for successive
5326 // rounds because they will permute elements within both vectors.
5327 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5328 EltStride == NumElems/2)
5329 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005330
Chris Lattner6e80e442010-08-28 17:15:43 +00005331 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005332 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005333 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005334 }
5335 return V[0];
5336 }
Dan Gohman475871a2008-07-27 21:46:04 +00005337 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005338}
5339
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005340// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5341// them in a MMX register. This is better than doing a stack convert.
5342static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005343 DebugLoc dl = Op.getDebugLoc();
5344 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005345
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005346 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5347 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5348 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005349 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005350 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5351 InVec = Op.getOperand(1);
5352 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5353 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005354 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005355 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5356 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5357 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005358 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005359 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5360 Mask[0] = 0; Mask[1] = 2;
5361 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5362 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005363 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005364}
5365
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005366// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5367// to create 256-bit vectors from two other 128-bit ones.
5368static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5369 DebugLoc dl = Op.getDebugLoc();
5370 EVT ResVT = Op.getValueType();
5371
5372 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5373
5374 SDValue V1 = Op.getOperand(0);
5375 SDValue V2 = Op.getOperand(1);
5376 unsigned NumElems = ResVT.getVectorNumElements();
5377
5378 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5379 DAG.getConstant(0, MVT::i32), DAG, dl);
5380 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5381 DAG, dl);
5382}
5383
5384SDValue
5385X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005386 EVT ResVT = Op.getValueType();
5387
5388 assert(Op.getNumOperands() == 2);
5389 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5390 "Unsupported CONCAT_VECTORS for value type");
5391
5392 // We support concatenate two MMX registers and place them in a MMX register.
5393 // This is better than doing a stack convert.
5394 if (ResVT.is128BitVector())
5395 return LowerMMXCONCAT_VECTORS(Op, DAG);
5396
5397 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5398 // from two other 128-bit ones.
5399 return LowerAVXCONCAT_VECTORS(Op, DAG);
5400}
5401
Nate Begemanb9a47b82009-02-23 08:49:38 +00005402// v8i16 shuffles - Prefer shuffles in the following order:
5403// 1. [all] pshuflw, pshufhw, optional move
5404// 2. [ssse3] 1 x pshufb
5405// 3. [ssse3] 2 x pshufb + 1 x por
5406// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005407SDValue
5408X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5409 SelectionDAG &DAG) const {
5410 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005411 SDValue V1 = SVOp->getOperand(0);
5412 SDValue V2 = SVOp->getOperand(1);
5413 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005414 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005415
Nate Begemanb9a47b82009-02-23 08:49:38 +00005416 // Determine if more than 1 of the words in each of the low and high quadwords
5417 // of the result come from the same quadword of one of the two inputs. Undef
5418 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005419 unsigned LoQuad[] = { 0, 0, 0, 0 };
5420 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005421 BitVector InputQuads(4);
5422 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005423 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005424 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005425 MaskVals.push_back(EltIdx);
5426 if (EltIdx < 0) {
5427 ++Quad[0];
5428 ++Quad[1];
5429 ++Quad[2];
5430 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005431 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005432 }
5433 ++Quad[EltIdx / 4];
5434 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005435 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005436
Nate Begemanb9a47b82009-02-23 08:49:38 +00005437 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005438 unsigned MaxQuad = 1;
5439 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005440 if (LoQuad[i] > MaxQuad) {
5441 BestLoQuad = i;
5442 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005443 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005444 }
5445
Nate Begemanb9a47b82009-02-23 08:49:38 +00005446 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005447 MaxQuad = 1;
5448 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005449 if (HiQuad[i] > MaxQuad) {
5450 BestHiQuad = i;
5451 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005452 }
5453 }
5454
Nate Begemanb9a47b82009-02-23 08:49:38 +00005455 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005456 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005457 // single pshufb instruction is necessary. If There are more than 2 input
5458 // quads, disable the next transformation since it does not help SSSE3.
5459 bool V1Used = InputQuads[0] || InputQuads[1];
5460 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperc0d82852011-11-22 00:44:41 +00005461 if (Subtarget->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005462 if (InputQuads.count() == 2 && V1Used && V2Used) {
5463 BestLoQuad = InputQuads.find_first();
5464 BestHiQuad = InputQuads.find_next(BestLoQuad);
5465 }
5466 if (InputQuads.count() > 2) {
5467 BestLoQuad = -1;
5468 BestHiQuad = -1;
5469 }
5470 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005471
Nate Begemanb9a47b82009-02-23 08:49:38 +00005472 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5473 // the shuffle mask. If a quad is scored as -1, that means that it contains
5474 // words from all 4 input quadwords.
5475 SDValue NewV;
5476 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005477 SmallVector<int, 8> MaskV;
5478 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5479 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005480 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005481 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5482 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5483 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005484
Nate Begemanb9a47b82009-02-23 08:49:38 +00005485 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5486 // source words for the shuffle, to aid later transformations.
5487 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005488 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005489 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005490 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005491 if (idx != (int)i)
5492 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005493 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005494 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005495 AllWordsInNewV = false;
5496 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005497 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005498
Nate Begemanb9a47b82009-02-23 08:49:38 +00005499 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5500 if (AllWordsInNewV) {
5501 for (int i = 0; i != 8; ++i) {
5502 int idx = MaskVals[i];
5503 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005504 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005505 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005506 if ((idx != i) && idx < 4)
5507 pshufhw = false;
5508 if ((idx != i) && idx > 3)
5509 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005510 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005511 V1 = NewV;
5512 V2Used = false;
5513 BestLoQuad = 0;
5514 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005515 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005516
Nate Begemanb9a47b82009-02-23 08:49:38 +00005517 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5518 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005519 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005520 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5521 unsigned TargetMask = 0;
5522 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005523 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005524 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5525 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5526 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005527 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005528 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005529 }
Eric Christopherfd179292009-08-27 18:07:15 +00005530
Nate Begemanb9a47b82009-02-23 08:49:38 +00005531 // If we have SSSE3, and all words of the result are from 1 input vector,
5532 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5533 // is present, fall back to case 4.
Craig Topperc0d82852011-11-22 00:44:41 +00005534 if (Subtarget->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005535 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005536
Nate Begemanb9a47b82009-02-23 08:49:38 +00005537 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005538 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005539 // mask, and elements that come from V1 in the V2 mask, so that the two
5540 // results can be OR'd together.
5541 bool TwoInputs = V1Used && V2Used;
5542 for (unsigned i = 0; i != 8; ++i) {
5543 int EltIdx = MaskVals[i] * 2;
5544 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005545 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5546 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005547 continue;
5548 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005549 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5550 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005551 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005552 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005553 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005554 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005555 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005556 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005557 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005558
Nate Begemanb9a47b82009-02-23 08:49:38 +00005559 // Calculate the shuffle mask for the second input, shuffle it, and
5560 // OR it with the first shuffled input.
5561 pshufbMask.clear();
5562 for (unsigned i = 0; i != 8; ++i) {
5563 int EltIdx = MaskVals[i] * 2;
5564 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005565 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5566 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005567 continue;
5568 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005569 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5570 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005571 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005572 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005573 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005574 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005575 MVT::v16i8, &pshufbMask[0], 16));
5576 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005577 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005578 }
5579
5580 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5581 // and update MaskVals with new element order.
5582 BitVector InOrder(8);
5583 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005584 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005585 for (int i = 0; i != 4; ++i) {
5586 int idx = MaskVals[i];
5587 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005588 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005589 InOrder.set(i);
5590 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005591 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005592 InOrder.set(i);
5593 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005594 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005595 }
5596 }
5597 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005598 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005599 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005600 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005601
Craig Topperc0d82852011-11-22 00:44:41 +00005602 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3orAVX())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005603 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5604 NewV.getOperand(0),
5605 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5606 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005607 }
Eric Christopherfd179292009-08-27 18:07:15 +00005608
Nate Begemanb9a47b82009-02-23 08:49:38 +00005609 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5610 // and update MaskVals with the new element order.
5611 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005612 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005613 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005614 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005615 for (unsigned i = 4; i != 8; ++i) {
5616 int idx = MaskVals[i];
5617 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005618 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005619 InOrder.set(i);
5620 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005621 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005622 InOrder.set(i);
5623 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005624 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005625 }
5626 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005627 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005628 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005629
Craig Topperc0d82852011-11-22 00:44:41 +00005630 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3orAVX())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005631 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5632 NewV.getOperand(0),
5633 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5634 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005635 }
Eric Christopherfd179292009-08-27 18:07:15 +00005636
Nate Begemanb9a47b82009-02-23 08:49:38 +00005637 // In case BestHi & BestLo were both -1, which means each quadword has a word
5638 // from each of the four input quadwords, calculate the InOrder bitvector now
5639 // before falling through to the insert/extract cleanup.
5640 if (BestLoQuad == -1 && BestHiQuad == -1) {
5641 NewV = V1;
5642 for (int i = 0; i != 8; ++i)
5643 if (MaskVals[i] < 0 || MaskVals[i] == i)
5644 InOrder.set(i);
5645 }
Eric Christopherfd179292009-08-27 18:07:15 +00005646
Nate Begemanb9a47b82009-02-23 08:49:38 +00005647 // The other elements are put in the right place using pextrw and pinsrw.
5648 for (unsigned i = 0; i != 8; ++i) {
5649 if (InOrder[i])
5650 continue;
5651 int EltIdx = MaskVals[i];
5652 if (EltIdx < 0)
5653 continue;
5654 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005655 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005656 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005657 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005658 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005659 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005660 DAG.getIntPtrConstant(i));
5661 }
5662 return NewV;
5663}
5664
5665// v16i8 shuffles - Prefer shuffles in the following order:
5666// 1. [ssse3] 1 x pshufb
5667// 2. [ssse3] 2 x pshufb + 1 x por
5668// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5669static
Nate Begeman9008ca62009-04-27 18:41:29 +00005670SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005671 SelectionDAG &DAG,
5672 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005673 SDValue V1 = SVOp->getOperand(0);
5674 SDValue V2 = SVOp->getOperand(1);
5675 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005676 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005677 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005678
Nate Begemanb9a47b82009-02-23 08:49:38 +00005679 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005680 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005681 // present, fall back to case 3.
5682 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5683 bool V1Only = true;
5684 bool V2Only = true;
5685 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005686 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005687 if (EltIdx < 0)
5688 continue;
5689 if (EltIdx < 16)
5690 V2Only = false;
5691 else
5692 V1Only = false;
5693 }
Eric Christopherfd179292009-08-27 18:07:15 +00005694
Nate Begemanb9a47b82009-02-23 08:49:38 +00005695 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperc0d82852011-11-22 00:44:41 +00005696 if (TLI.getSubtarget()->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005697 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005698
Nate Begemanb9a47b82009-02-23 08:49:38 +00005699 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005700 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005701 //
5702 // Otherwise, we have elements from both input vectors, and must zero out
5703 // elements that come from V2 in the first mask, and V1 in the second mask
5704 // so that we can OR them together.
5705 bool TwoInputs = !(V1Only || V2Only);
5706 for (unsigned i = 0; i != 16; ++i) {
5707 int EltIdx = MaskVals[i];
5708 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005709 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005710 continue;
5711 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005712 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005713 }
5714 // If all the elements are from V2, assign it to V1 and return after
5715 // building the first pshufb.
5716 if (V2Only)
5717 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005718 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005719 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005720 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005721 if (!TwoInputs)
5722 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005723
Nate Begemanb9a47b82009-02-23 08:49:38 +00005724 // Calculate the shuffle mask for the second input, shuffle it, and
5725 // OR it with the first shuffled input.
5726 pshufbMask.clear();
5727 for (unsigned i = 0; i != 16; ++i) {
5728 int EltIdx = MaskVals[i];
5729 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005730 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005731 continue;
5732 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005733 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005734 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005735 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005736 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005737 MVT::v16i8, &pshufbMask[0], 16));
5738 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005739 }
Eric Christopherfd179292009-08-27 18:07:15 +00005740
Nate Begemanb9a47b82009-02-23 08:49:38 +00005741 // No SSSE3 - Calculate in place words and then fix all out of place words
5742 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5743 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005744 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5745 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005746 SDValue NewV = V2Only ? V2 : V1;
5747 for (int i = 0; i != 8; ++i) {
5748 int Elt0 = MaskVals[i*2];
5749 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005750
Nate Begemanb9a47b82009-02-23 08:49:38 +00005751 // This word of the result is all undef, skip it.
5752 if (Elt0 < 0 && Elt1 < 0)
5753 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005754
Nate Begemanb9a47b82009-02-23 08:49:38 +00005755 // This word of the result is already in the correct place, skip it.
5756 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5757 continue;
5758 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5759 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005760
Nate Begemanb9a47b82009-02-23 08:49:38 +00005761 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5762 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5763 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005764
5765 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5766 // using a single extract together, load it and store it.
5767 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005768 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005769 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005770 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005771 DAG.getIntPtrConstant(i));
5772 continue;
5773 }
5774
Nate Begemanb9a47b82009-02-23 08:49:38 +00005775 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005776 // source byte is not also odd, shift the extracted word left 8 bits
5777 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005778 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005779 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005780 DAG.getIntPtrConstant(Elt1 / 2));
5781 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005782 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005783 DAG.getConstant(8,
5784 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005785 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005786 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5787 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005788 }
5789 // If Elt0 is defined, extract it from the appropriate source. If the
5790 // source byte is not also even, shift the extracted word right 8 bits. If
5791 // Elt1 was also defined, OR the extracted values together before
5792 // inserting them in the result.
5793 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005794 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5796 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005797 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005798 DAG.getConstant(8,
5799 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005800 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005801 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5802 DAG.getConstant(0x00FF, MVT::i16));
5803 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005804 : InsElt0;
5805 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005806 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005807 DAG.getIntPtrConstant(i));
5808 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005809 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005810}
5811
Evan Cheng7a831ce2007-12-15 03:00:47 +00005812/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005813/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005814/// done when every pair / quad of shuffle mask elements point to elements in
5815/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005816/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005817static
Nate Begeman9008ca62009-04-27 18:41:29 +00005818SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005819 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005820 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005821 SDValue V1 = SVOp->getOperand(0);
5822 SDValue V2 = SVOp->getOperand(1);
5823 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005824 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005825 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005826 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005827 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005828 case MVT::v4f32: NewVT = MVT::v2f64; break;
5829 case MVT::v4i32: NewVT = MVT::v2i64; break;
5830 case MVT::v8i16: NewVT = MVT::v4i32; break;
5831 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005832 }
5833
Nate Begeman9008ca62009-04-27 18:41:29 +00005834 int Scale = NumElems / NewWidth;
5835 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005836 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005837 int StartIdx = -1;
5838 for (int j = 0; j < Scale; ++j) {
5839 int EltIdx = SVOp->getMaskElt(i+j);
5840 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005841 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005842 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005843 StartIdx = EltIdx - (EltIdx % Scale);
5844 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005845 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005846 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005847 if (StartIdx == -1)
5848 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005849 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005850 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005851 }
5852
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005853 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5854 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005855 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005856}
5857
Evan Chengd880b972008-05-09 21:53:03 +00005858/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005859///
Owen Andersone50ed302009-08-10 22:56:29 +00005860static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005861 SDValue SrcOp, SelectionDAG &DAG,
5862 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005863 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005864 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005865 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005866 LD = dyn_cast<LoadSDNode>(SrcOp);
5867 if (!LD) {
5868 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5869 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005870 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005871 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005872 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005873 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005874 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005875 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005876 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005877 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005878 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5879 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5880 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005881 SrcOp.getOperand(0)
5882 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005883 }
5884 }
5885 }
5886
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005887 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005888 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005889 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005890 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005891}
5892
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005893/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5894/// shuffle node referes to only one lane in the sources.
5895static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5896 EVT VT = SVOp->getValueType(0);
5897 int NumElems = VT.getVectorNumElements();
5898 int HalfSize = NumElems/2;
5899 SmallVector<int, 16> M;
5900 SVOp->getMask(M);
5901 bool MatchA = false, MatchB = false;
5902
5903 for (int l = 0; l < NumElems*2; l += HalfSize) {
5904 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5905 MatchA = true;
5906 break;
5907 }
5908 }
5909
5910 for (int l = 0; l < NumElems*2; l += HalfSize) {
5911 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5912 MatchB = true;
5913 break;
5914 }
5915 }
5916
5917 return MatchA && MatchB;
5918}
5919
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005920/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5921/// which could not be matched by any known target speficic shuffle
5922static SDValue
5923LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005924 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5925 // If each half of a vector shuffle node referes to only one lane in the
5926 // source vectors, extract each used 128-bit lane and shuffle them using
5927 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5928 // the work to the legalizer.
5929 DebugLoc dl = SVOp->getDebugLoc();
5930 EVT VT = SVOp->getValueType(0);
5931 int NumElems = VT.getVectorNumElements();
5932 int HalfSize = NumElems/2;
5933
5934 // Extract the reference for each half
5935 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5936 int FstVecOpNum = 0, SndVecOpNum = 0;
5937 for (int i = 0; i < HalfSize; ++i) {
5938 int Elt = SVOp->getMaskElt(i);
5939 if (SVOp->getMaskElt(i) < 0)
5940 continue;
5941 FstVecOpNum = Elt/NumElems;
5942 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5943 break;
5944 }
5945 for (int i = HalfSize; i < NumElems; ++i) {
5946 int Elt = SVOp->getMaskElt(i);
5947 if (SVOp->getMaskElt(i) < 0)
5948 continue;
5949 SndVecOpNum = Elt/NumElems;
5950 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5951 break;
5952 }
5953
5954 // Extract the subvectors
5955 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5956 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5957 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5958 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5959
5960 // Generate 128-bit shuffles
5961 SmallVector<int, 16> MaskV1, MaskV2;
5962 for (int i = 0; i < HalfSize; ++i) {
5963 int Elt = SVOp->getMaskElt(i);
5964 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5965 }
5966 for (int i = HalfSize; i < NumElems; ++i) {
5967 int Elt = SVOp->getMaskElt(i);
5968 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5969 }
5970
5971 EVT NVT = V1.getValueType();
5972 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5973 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5974
5975 // Concatenate the result back
5976 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
5977 DAG.getConstant(0, MVT::i32), DAG, dl);
5978 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5979 DAG, dl);
5980 }
5981
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005982 return SDValue();
5983}
5984
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005985/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
5986/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00005987static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005988LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005989 SDValue V1 = SVOp->getOperand(0);
5990 SDValue V2 = SVOp->getOperand(1);
5991 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005992 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005993
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005994 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
5995
Evan Chengace3c172008-07-22 21:13:36 +00005996 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005997 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005998 SmallVector<int, 8> Mask1(4U, -1);
5999 SmallVector<int, 8> PermMask;
6000 SVOp->getMask(PermMask);
6001
Evan Chengace3c172008-07-22 21:13:36 +00006002 unsigned NumHi = 0;
6003 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006004 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006005 int Idx = PermMask[i];
6006 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006007 Locs[i] = std::make_pair(-1, -1);
6008 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006009 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6010 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006011 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006012 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006013 NumLo++;
6014 } else {
6015 Locs[i] = std::make_pair(1, NumHi);
6016 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006017 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006018 NumHi++;
6019 }
6020 }
6021 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006022
Evan Chengace3c172008-07-22 21:13:36 +00006023 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006024 // If no more than two elements come from either vector. This can be
6025 // implemented with two shuffles. First shuffle gather the elements.
6026 // The second shuffle, which takes the first shuffle as both of its
6027 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006028 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006029
Nate Begeman9008ca62009-04-27 18:41:29 +00006030 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006031
Evan Chengace3c172008-07-22 21:13:36 +00006032 for (unsigned i = 0; i != 4; ++i) {
6033 if (Locs[i].first == -1)
6034 continue;
6035 else {
6036 unsigned Idx = (i < 2) ? 0 : 4;
6037 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006038 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006039 }
6040 }
6041
Nate Begeman9008ca62009-04-27 18:41:29 +00006042 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006043 } else if (NumLo == 3 || NumHi == 3) {
6044 // Otherwise, we must have three elements from one vector, call it X, and
6045 // one element from the other, call it Y. First, use a shufps to build an
6046 // intermediate vector with the one element from Y and the element from X
6047 // that will be in the same half in the final destination (the indexes don't
6048 // matter). Then, use a shufps to build the final vector, taking the half
6049 // containing the element from Y from the intermediate, and the other half
6050 // from X.
6051 if (NumHi == 3) {
6052 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006053 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006054 std::swap(V1, V2);
6055 }
6056
6057 // Find the element from V2.
6058 unsigned HiIndex;
6059 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006060 int Val = PermMask[HiIndex];
6061 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006062 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006063 if (Val >= 4)
6064 break;
6065 }
6066
Nate Begeman9008ca62009-04-27 18:41:29 +00006067 Mask1[0] = PermMask[HiIndex];
6068 Mask1[1] = -1;
6069 Mask1[2] = PermMask[HiIndex^1];
6070 Mask1[3] = -1;
6071 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006072
6073 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006074 Mask1[0] = PermMask[0];
6075 Mask1[1] = PermMask[1];
6076 Mask1[2] = HiIndex & 1 ? 6 : 4;
6077 Mask1[3] = HiIndex & 1 ? 4 : 6;
6078 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006079 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006080 Mask1[0] = HiIndex & 1 ? 2 : 0;
6081 Mask1[1] = HiIndex & 1 ? 0 : 2;
6082 Mask1[2] = PermMask[2];
6083 Mask1[3] = PermMask[3];
6084 if (Mask1[2] >= 0)
6085 Mask1[2] += 4;
6086 if (Mask1[3] >= 0)
6087 Mask1[3] += 4;
6088 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006089 }
Evan Chengace3c172008-07-22 21:13:36 +00006090 }
6091
6092 // Break it into (shuffle shuffle_hi, shuffle_lo).
6093 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006094 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006095 SmallVector<int,8> LoMask(4U, -1);
6096 SmallVector<int,8> HiMask(4U, -1);
6097
6098 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006099 unsigned MaskIdx = 0;
6100 unsigned LoIdx = 0;
6101 unsigned HiIdx = 2;
6102 for (unsigned i = 0; i != 4; ++i) {
6103 if (i == 2) {
6104 MaskPtr = &HiMask;
6105 MaskIdx = 1;
6106 LoIdx = 0;
6107 HiIdx = 2;
6108 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006109 int Idx = PermMask[i];
6110 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006111 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006112 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006113 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006114 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006115 LoIdx++;
6116 } else {
6117 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006118 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006119 HiIdx++;
6120 }
6121 }
6122
Nate Begeman9008ca62009-04-27 18:41:29 +00006123 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6124 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6125 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006126 for (unsigned i = 0; i != 4; ++i) {
6127 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006128 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006129 } else {
6130 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006131 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006132 }
6133 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006134 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006135}
6136
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006137static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006138 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006139 V = V.getOperand(0);
6140 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6141 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006142 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6143 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6144 // BUILD_VECTOR (load), undef
6145 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006146 if (MayFoldLoad(V))
6147 return true;
6148 return false;
6149}
6150
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006151// FIXME: the version above should always be used. Since there's
6152// a bug where several vector shuffles can't be folded because the
6153// DAG is not updated during lowering and a node claims to have two
6154// uses while it only has one, use this version, and let isel match
6155// another instruction if the load really happens to have more than
6156// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006157// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006158static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006159 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006160 V = V.getOperand(0);
6161 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6162 V = V.getOperand(0);
6163 if (ISD::isNormalLoad(V.getNode()))
6164 return true;
6165 return false;
6166}
6167
6168/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6169/// a vector extract, and if both can be later optimized into a single load.
6170/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6171/// here because otherwise a target specific shuffle node is going to be
6172/// emitted for this shuffle, and the optimization not done.
6173/// FIXME: This is probably not the best approach, but fix the problem
6174/// until the right path is decided.
6175static
6176bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6177 const TargetLowering &TLI) {
6178 EVT VT = V.getValueType();
6179 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6180
6181 // Be sure that the vector shuffle is present in a pattern like this:
6182 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6183 if (!V.hasOneUse())
6184 return false;
6185
6186 SDNode *N = *V.getNode()->use_begin();
6187 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6188 return false;
6189
6190 SDValue EltNo = N->getOperand(1);
6191 if (!isa<ConstantSDNode>(EltNo))
6192 return false;
6193
6194 // If the bit convert changed the number of elements, it is unsafe
6195 // to examine the mask.
6196 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006197 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006198 EVT SrcVT = V.getOperand(0).getValueType();
6199 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6200 return false;
6201 V = V.getOperand(0);
6202 HasShuffleIntoBitcast = true;
6203 }
6204
6205 // Select the input vector, guarding against out of range extract vector.
6206 unsigned NumElems = VT.getVectorNumElements();
6207 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6208 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6209 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6210
6211 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006212 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006213 V = V.getOperand(0);
6214
6215 if (ISD::isNormalLoad(V.getNode())) {
6216 // Is the original load suitable?
6217 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6218
6219 // FIXME: avoid the multi-use bug that is preventing lots of
6220 // of foldings to be detected, this is still wrong of course, but
6221 // give the temporary desired behavior, and if it happens that
6222 // the load has real more uses, during isel it will not fold, and
6223 // will generate poor code.
6224 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6225 return false;
6226
6227 if (!HasShuffleIntoBitcast)
6228 return true;
6229
6230 // If there's a bitcast before the shuffle, check if the load type and
6231 // alignment is valid.
6232 unsigned Align = LN0->getAlignment();
6233 unsigned NewAlign =
6234 TLI.getTargetData()->getABITypeAlignment(
6235 VT.getTypeForEVT(*DAG.getContext()));
6236
6237 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6238 return false;
6239 }
6240
6241 return true;
6242}
6243
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006244static
Evan Cheng835580f2010-10-07 20:50:20 +00006245SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6246 EVT VT = Op.getValueType();
6247
6248 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006249 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6250 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006251 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6252 V1, DAG));
6253}
6254
6255static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006256SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006257 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006258 SDValue V1 = Op.getOperand(0);
6259 SDValue V2 = Op.getOperand(1);
6260 EVT VT = Op.getValueType();
6261
6262 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6263
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006264 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006265 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6266
Evan Cheng0899f5c2011-08-31 02:05:24 +00006267 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6268 return DAG.getNode(ISD::BITCAST, dl, VT,
6269 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6270 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6271 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006272}
6273
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006274static
6275SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6276 SDValue V1 = Op.getOperand(0);
6277 SDValue V2 = Op.getOperand(1);
6278 EVT VT = Op.getValueType();
6279
6280 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6281 "unsupported shuffle type");
6282
6283 if (V2.getOpcode() == ISD::UNDEF)
6284 V2 = V1;
6285
6286 // v4i32 or v4f32
6287 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6288}
6289
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006290static inline unsigned getSHUFPOpcode(EVT VT) {
6291 switch(VT.getSimpleVT().SimpleTy) {
6292 case MVT::v8i32: // Use fp unit for int unpack.
6293 case MVT::v8f32:
6294 case MVT::v4i32: // Use fp unit for int unpack.
6295 case MVT::v4f32: return X86ISD::SHUFPS;
6296 case MVT::v4i64: // Use fp unit for int unpack.
6297 case MVT::v4f64:
6298 case MVT::v2i64: // Use fp unit for int unpack.
6299 case MVT::v2f64: return X86ISD::SHUFPD;
6300 default:
6301 llvm_unreachable("Unknown type for shufp*");
6302 }
6303 return 0;
6304}
6305
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006306static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006307SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006308 SDValue V1 = Op.getOperand(0);
6309 SDValue V2 = Op.getOperand(1);
6310 EVT VT = Op.getValueType();
6311 unsigned NumElems = VT.getVectorNumElements();
6312
6313 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6314 // operand of these instructions is only memory, so check if there's a
6315 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6316 // same masks.
6317 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006318
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006319 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006320 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006321 CanFoldLoad = true;
6322
6323 // When V1 is a load, it can be folded later into a store in isel, example:
6324 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6325 // turns into:
6326 // (MOVLPSmr addr:$src1, VR128:$src2)
6327 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006328 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006329 CanFoldLoad = true;
6330
Dan Gohman65fd6562011-11-03 21:49:52 +00006331 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006332 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006333 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006334 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6335
6336 if (NumElems == 4)
Dan Gohman65fd6562011-11-03 21:49:52 +00006337 // If we don't care about the second element, procede to use movss.
6338 if (SVOp->getMaskElt(1) != -1)
6339 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006340 }
6341
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006342 // movl and movlp will both match v2i64, but v2i64 is never matched by
6343 // movl earlier because we make it strict to avoid messing with the movlp load
6344 // folding logic (see the code above getMOVLP call). Match it here then,
6345 // this is horrible, but will stay like this until we move all shuffle
6346 // matching to x86 specific nodes. Note that for the 1st condition all
6347 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006348 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006349 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6350 // as to remove this logic from here, as much as possible
6351 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006352 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006353 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006354 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006355
6356 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6357
6358 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006359 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006360 X86::getShuffleSHUFImmediate(SVOp), DAG);
6361}
6362
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006363static
6364SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006365 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006366 const X86Subtarget *Subtarget) {
6367 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6368 EVT VT = Op.getValueType();
6369 DebugLoc dl = Op.getDebugLoc();
6370 SDValue V1 = Op.getOperand(0);
6371 SDValue V2 = Op.getOperand(1);
6372
6373 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006374 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006375
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006376 // Handle splat operations
6377 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006378 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006379 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006380 // Special case, this is the only place now where it's allowed to return
6381 // a vector_shuffle operation without using a target specific node, because
6382 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6383 // this be moved to DAGCombine instead?
6384 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006385 return Op;
6386
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006387 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00006388 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00006389 if (Subtarget->hasAVX() && LD.getNode())
6390 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006391
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006392 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006393 if ((Size == 128 && NumElem <= 4) ||
6394 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006395 return SDValue();
6396
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006397 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006398 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006399 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006400
6401 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6402 // do it!
6403 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6404 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6405 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006406 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006407 } else if ((VT == MVT::v4i32 ||
6408 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006409 // FIXME: Figure out a cleaner way to do this.
6410 // Try to make use of movq to zero out the top part.
6411 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6412 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6413 if (NewOp.getNode()) {
6414 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6415 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6416 DAG, Subtarget, dl);
6417 }
6418 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6419 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6420 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6421 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6422 DAG, Subtarget, dl);
6423 }
6424 }
6425 return SDValue();
6426}
6427
Dan Gohman475871a2008-07-27 21:46:04 +00006428SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006429X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006430 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006431 SDValue V1 = Op.getOperand(0);
6432 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006433 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006434 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006435 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006436 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006437 bool V1IsSplat = false;
6438 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006439 bool HasXMMInt = Subtarget->hasXMMInt();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006440 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006441 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006442 MachineFunction &MF = DAG.getMachineFunction();
6443 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006444
Craig Topper3426a3e2011-11-14 06:46:21 +00006445 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006446
Craig Topper38034c52011-11-26 22:55:48 +00006447 assert(V1.getOpcode() != ISD::UNDEF && "Op 1 of shuffle should not be undef");
6448
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006449 // Vector shuffle lowering takes 3 steps:
6450 //
6451 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6452 // narrowing and commutation of operands should be handled.
6453 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6454 // shuffle nodes.
6455 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6456 // so the shuffle can be broken into other shuffles and the legalizer can
6457 // try the lowering again.
6458 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006459 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006460 // be matched during isel, all of them must be converted to a target specific
6461 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006462
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006463 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6464 // narrowing and commutation of operands should be handled. The actual code
6465 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006466 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006467 if (NewOp.getNode())
6468 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006469
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006470 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6471 // unpckh_undef). Only use pshufd if speed is more important than size.
6472 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Craig Topper34671b82011-12-06 08:21:25 +00006473 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006474 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Craig Topper34671b82011-12-06 08:21:25 +00006475 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006476
Craig Topperc0d82852011-11-22 00:44:41 +00006477 if (X86::isMOVDDUPMask(SVOp) && Subtarget->hasSSE3orAVX() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006478 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006479 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006480
Dale Johannesen0488fb62010-09-30 23:57:10 +00006481 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006482 return getMOVHighToLow(Op, dl, DAG);
6483
6484 // Use to match splats
Craig Topperc0d82852011-11-22 00:44:41 +00006485 if (HasXMMInt && X86::isUNPCKHMask(SVOp, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006486 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006487 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006488
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006489 if (X86::isPSHUFDMask(SVOp)) {
6490 // The actual implementation will match the mask in the if above and then
6491 // during isel it can match several different instructions, not only pshufd
6492 // as its name says, sad but true, emulate the behavior for now...
6493 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6494 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6495
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006496 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6497
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006498 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006499 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6500
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006501 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6502 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006503 }
Eric Christopherfd179292009-08-27 18:07:15 +00006504
Evan Chengf26ffe92008-05-29 08:22:04 +00006505 // Check if this can be converted into a logical shift.
6506 bool isLeft = false;
6507 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006508 SDValue ShVal;
Craig Topperc0d82852011-11-22 00:44:41 +00006509 bool isShift = HasXMMInt && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006510 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006511 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006512 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006513 EVT EltVT = VT.getVectorElementType();
6514 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006515 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006516 }
Eric Christopherfd179292009-08-27 18:07:15 +00006517
Nate Begeman9008ca62009-04-27 18:41:29 +00006518 if (X86::isMOVLMask(SVOp)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006519 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006520 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006521 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006522 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006523 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6524
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006525 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006526 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6527 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006528 }
Eric Christopherfd179292009-08-27 18:07:15 +00006529
Nate Begeman9008ca62009-04-27 18:41:29 +00006530 // FIXME: fold these into legal mask.
Craig Topperc0d82852011-11-22 00:44:41 +00006531 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp, HasAVX2))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006532 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006533
Dale Johannesen0488fb62010-09-30 23:57:10 +00006534 if (X86::isMOVHLPSMask(SVOp))
6535 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006536
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006537 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006538 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006539
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006540 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006541 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006542
Dale Johannesen0488fb62010-09-30 23:57:10 +00006543 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006544 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006545
Nate Begeman9008ca62009-04-27 18:41:29 +00006546 if (ShouldXformToMOVHLPS(SVOp) ||
6547 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6548 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006549
Evan Chengf26ffe92008-05-29 08:22:04 +00006550 if (isShift) {
6551 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006552 EVT EltVT = VT.getVectorElementType();
6553 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006554 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006555 }
Eric Christopherfd179292009-08-27 18:07:15 +00006556
Evan Cheng9eca5e82006-10-25 21:49:50 +00006557 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006558 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6559 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006560 V1IsSplat = isSplatVector(V1.getNode());
6561 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006562
Chris Lattner8a594482007-11-25 00:24:49 +00006563 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper38034c52011-11-26 22:55:48 +00006564 if (V1IsSplat && !V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006565 Op = CommuteVectorShuffle(SVOp, DAG);
6566 SVOp = cast<ShuffleVectorSDNode>(Op);
6567 V1 = SVOp->getOperand(0);
6568 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006569 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006570 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006571 }
6572
Craig Topperbeabc6c2011-12-05 06:56:46 +00006573 SmallVector<int, 32> M;
6574 SVOp->getMask(M);
6575
6576 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006577 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006578 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006579 return V1;
6580 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6581 // the instruction selector will not match, so get a canonical MOVL with
6582 // swapped operands to undo the commute.
6583 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006584 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006585
Craig Topperbeabc6c2011-12-05 06:56:46 +00006586 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006587 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006588
Craig Topperbeabc6c2011-12-05 06:56:46 +00006589 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006590 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006591
Evan Cheng9bbbb982006-10-25 20:48:19 +00006592 if (V2IsSplat) {
6593 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006594 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006595 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006596 SDValue NewMask = NormalizeMask(SVOp, DAG);
6597 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6598 if (NSVOp != SVOp) {
Craig Topperc0d82852011-11-22 00:44:41 +00006599 if (X86::isUNPCKLMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006600 return NewMask;
Craig Topperc0d82852011-11-22 00:44:41 +00006601 } else if (X86::isUNPCKHMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006602 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006603 }
6604 }
6605 }
6606
Evan Cheng9eca5e82006-10-25 21:49:50 +00006607 if (Commuted) {
6608 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006609 // FIXME: this seems wrong.
6610 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6611 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006612
Craig Topperc0d82852011-11-22 00:44:41 +00006613 if (X86::isUNPCKLMask(NewSVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006614 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006615
Craig Topperc0d82852011-11-22 00:44:41 +00006616 if (X86::isUNPCKHMask(NewSVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006617 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006618 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006619
Nate Begeman9008ca62009-04-27 18:41:29 +00006620 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1ff73d72011-12-06 04:59:07 +00006621 if (!V2IsUndef && (isSHUFPMask(M, VT, /* Commuted */ true) ||
6622 isVSHUFPYMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006623 return CommuteVectorShuffle(SVOp, DAG);
6624
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006625 // The checks below are all present in isShuffleMaskLegal, but they are
6626 // inlined here right now to enable us to directly emit target specific
6627 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006628
Craig Topperc0d82852011-11-22 00:44:41 +00006629 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006630 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006631 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006632 DAG);
6633
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006634 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6635 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006636 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006637 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006638 }
6639
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006640 if (isPSHUFHWMask(M, VT))
6641 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6642 X86::getShufflePSHUFHWImmediate(SVOp),
6643 DAG);
6644
6645 if (isPSHUFLWMask(M, VT))
6646 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6647 X86::getShufflePSHUFLWImmediate(SVOp),
6648 DAG);
6649
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006650 if (isSHUFPMask(M, VT))
6651 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6652 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006653
Craig Topperbeabc6c2011-12-05 06:56:46 +00006654 if (isUNPCKL_v_undef_Mask(M, VT))
Craig Topper34671b82011-12-06 08:21:25 +00006655 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topperbeabc6c2011-12-05 06:56:46 +00006656 if (isUNPCKH_v_undef_Mask(M, VT))
Craig Topper34671b82011-12-06 08:21:25 +00006657 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006658
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006659 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006660 // Generate target specific nodes for 128 or 256-bit shuffles only
6661 // supported in the AVX instruction set.
6662 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006663
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006664 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006665 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006666 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6667
Craig Topper70b883b2011-11-28 10:14:51 +00006668 // Handle VPERMILPS/D* permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006669 if (isVPERMILPMask(M, VT, HasAVX))
Craig Topper316cd2a2011-11-30 06:25:25 +00006670 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006671 getShuffleVPERMILPImmediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006672
Craig Topper70b883b2011-11-28 10:14:51 +00006673 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006674 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006675 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006676 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006677
Craig Topper70b883b2011-11-28 10:14:51 +00006678 // Handle VSHUFPS/DY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006679 if (isVSHUFPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006680 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
Craig Topper9d7025b2011-11-27 21:41:12 +00006681 getShuffleVSHUFPYImmediate(SVOp), DAG);
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006682
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006683 //===--------------------------------------------------------------------===//
6684 // Since no target specific shuffle was selected for this generic one,
6685 // lower it into other known shuffles. FIXME: this isn't true yet, but
6686 // this is the plan.
6687 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006688
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006689 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6690 if (VT == MVT::v8i16) {
6691 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6692 if (NewOp.getNode())
6693 return NewOp;
6694 }
6695
6696 if (VT == MVT::v16i8) {
6697 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6698 if (NewOp.getNode())
6699 return NewOp;
6700 }
6701
6702 // Handle all 128-bit wide vectors with 4 elements, and match them with
6703 // several different shuffle types.
6704 if (NumElems == 4 && VT.getSizeInBits() == 128)
6705 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6706
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006707 // Handle general 256-bit shuffles
6708 if (VT.is256BitVector())
6709 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6710
Dan Gohman475871a2008-07-27 21:46:04 +00006711 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006712}
6713
Dan Gohman475871a2008-07-27 21:46:04 +00006714SDValue
6715X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006716 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006717 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006718 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006719
6720 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6721 return SDValue();
6722
Duncan Sands83ec4b62008-06-06 12:08:01 +00006723 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006724 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006725 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006726 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006727 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006728 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006729 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006730 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6731 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6732 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006733 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6734 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006735 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006736 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006737 Op.getOperand(0)),
6738 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006739 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006740 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006741 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006742 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006743 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006744 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006745 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6746 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006747 // result has a single use which is a store or a bitcast to i32. And in
6748 // the case of a store, it's not worth it if the index is a constant 0,
6749 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006750 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006751 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006752 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006753 if ((User->getOpcode() != ISD::STORE ||
6754 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6755 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006756 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006757 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006758 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006759 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006760 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006761 Op.getOperand(0)),
6762 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006763 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Pete Coopera77214a2011-11-14 19:38:42 +00006764 } else if (VT == MVT::i32 || VT == MVT::i64) {
6765 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006766 if (isa<ConstantSDNode>(Op.getOperand(1)))
6767 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006768 }
Dan Gohman475871a2008-07-27 21:46:04 +00006769 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006770}
6771
6772
Dan Gohman475871a2008-07-27 21:46:04 +00006773SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006774X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6775 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006776 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006777 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006778
David Greene74a579d2011-02-10 16:57:36 +00006779 SDValue Vec = Op.getOperand(0);
6780 EVT VecVT = Vec.getValueType();
6781
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006782 // If this is a 256-bit vector result, first extract the 128-bit vector and
6783 // then extract the element from the 128-bit vector.
6784 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006785 DebugLoc dl = Op.getNode()->getDebugLoc();
6786 unsigned NumElems = VecVT.getVectorNumElements();
6787 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006788 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6789
6790 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006791 bool Upper = IdxVal >= NumElems/2;
6792 Vec = Extract128BitVector(Vec,
6793 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006794
David Greene74a579d2011-02-10 16:57:36 +00006795 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006796 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006797 }
6798
6799 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6800
Craig Topperc0d82852011-11-22 00:44:41 +00006801 if (Subtarget->hasSSE41orAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006802 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006803 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006804 return Res;
6805 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006806
Owen Andersone50ed302009-08-10 22:56:29 +00006807 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006808 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006809 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006810 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006811 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006812 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006813 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006814 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6815 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006816 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006817 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006818 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006819 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006820 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006821 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006822 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006823 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006824 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006825 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006826 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006827 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006828 if (Idx == 0)
6829 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006830
Evan Cheng0db9fe62006-04-25 20:13:52 +00006831 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006832 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006833 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006834 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006835 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006836 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006837 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006838 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006839 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6840 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6841 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006842 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006843 if (Idx == 0)
6844 return Op;
6845
6846 // UNPCKHPD the element to the lowest double word, then movsd.
6847 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6848 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006849 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006850 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006851 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006852 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006853 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006854 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006855 }
6856
Dan Gohman475871a2008-07-27 21:46:04 +00006857 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006858}
6859
Dan Gohman475871a2008-07-27 21:46:04 +00006860SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006861X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6862 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006863 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006864 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006865 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006866
Dan Gohman475871a2008-07-27 21:46:04 +00006867 SDValue N0 = Op.getOperand(0);
6868 SDValue N1 = Op.getOperand(1);
6869 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006870
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006871 if (VT.getSizeInBits() == 256)
6872 return SDValue();
6873
Dan Gohman8a55ce42009-09-23 21:02:20 +00006874 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006875 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006876 unsigned Opc;
6877 if (VT == MVT::v8i16)
6878 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006879 else if (VT == MVT::v16i8)
6880 Opc = X86ISD::PINSRB;
6881 else
6882 Opc = X86ISD::PINSRB;
6883
Nate Begeman14d12ca2008-02-11 04:19:36 +00006884 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6885 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006886 if (N1.getValueType() != MVT::i32)
6887 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6888 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006889 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006890 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006891 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006892 // Bits [7:6] of the constant are the source select. This will always be
6893 // zero here. The DAG Combiner may combine an extract_elt index into these
6894 // bits. For example (insert (extract, 3), 2) could be matched by putting
6895 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006896 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006897 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006898 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006899 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006900 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006901 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006902 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006903 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Pete Coopera77214a2011-11-14 19:38:42 +00006904 } else if ((EltVT == MVT::i32 || EltVT == MVT::i64) &&
6905 isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006906 // PINSR* works with constant index.
6907 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006908 }
Dan Gohman475871a2008-07-27 21:46:04 +00006909 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006910}
6911
Dan Gohman475871a2008-07-27 21:46:04 +00006912SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006913X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006914 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006915 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006916
David Greene6b381262011-02-09 15:32:06 +00006917 DebugLoc dl = Op.getDebugLoc();
6918 SDValue N0 = Op.getOperand(0);
6919 SDValue N1 = Op.getOperand(1);
6920 SDValue N2 = Op.getOperand(2);
6921
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006922 // If this is a 256-bit vector result, first extract the 128-bit vector,
6923 // insert the element into the extracted half and then place it back.
6924 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00006925 if (!isa<ConstantSDNode>(N2))
6926 return SDValue();
6927
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006928 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00006929 unsigned NumElems = VT.getVectorNumElements();
6930 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006931 bool Upper = IdxVal >= NumElems/2;
6932 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
6933 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00006934
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006935 // Insert the element into the desired half.
6936 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
6937 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00006938
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006939 // Insert the changed part back to the 256-bit vector
6940 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00006941 }
6942
Craig Topperc0d82852011-11-22 00:44:41 +00006943 if (Subtarget->hasSSE41orAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00006944 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6945
Dan Gohman8a55ce42009-09-23 21:02:20 +00006946 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006947 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006948
Dan Gohman8a55ce42009-09-23 21:02:20 +00006949 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006950 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6951 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006952 if (N1.getValueType() != MVT::i32)
6953 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6954 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006955 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006956 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006957 }
Dan Gohman475871a2008-07-27 21:46:04 +00006958 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006959}
6960
Dan Gohman475871a2008-07-27 21:46:04 +00006961SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006962X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00006963 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006964 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006965 EVT OpVT = Op.getValueType();
6966
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00006967 // If this is a 256-bit vector result, first insert into a 128-bit
6968 // vector and then insert into the 256-bit vector.
6969 if (OpVT.getSizeInBits() > 128) {
6970 // Insert into a 128-bit vector.
6971 EVT VT128 = EVT::getVectorVT(*Context,
6972 OpVT.getVectorElementType(),
6973 OpVT.getVectorNumElements() / 2);
6974
6975 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6976
6977 // Insert the 128-bit vector.
6978 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6979 DAG.getConstant(0, MVT::i32),
6980 DAG, dl);
6981 }
6982
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006983 if (Op.getValueType() == MVT::v1i64 &&
6984 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006985 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006986
Owen Anderson825b72b2009-08-11 20:47:22 +00006987 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006988 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6989 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006990 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006991 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006992}
6993
David Greene91585092011-01-26 15:38:49 +00006994// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6995// a simple subregister reference or explicit instructions to grab
6996// upper bits of a vector.
6997SDValue
6998X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6999 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007000 DebugLoc dl = Op.getNode()->getDebugLoc();
7001 SDValue Vec = Op.getNode()->getOperand(0);
7002 SDValue Idx = Op.getNode()->getOperand(1);
7003
7004 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7005 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7006 return Extract128BitVector(Vec, Idx, DAG, dl);
7007 }
David Greene91585092011-01-26 15:38:49 +00007008 }
7009 return SDValue();
7010}
7011
David Greenecfe33c42011-01-26 19:13:22 +00007012// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7013// simple superregister reference or explicit instructions to insert
7014// the upper bits of a vector.
7015SDValue
7016X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7017 if (Subtarget->hasAVX()) {
7018 DebugLoc dl = Op.getNode()->getDebugLoc();
7019 SDValue Vec = Op.getNode()->getOperand(0);
7020 SDValue SubVec = Op.getNode()->getOperand(1);
7021 SDValue Idx = Op.getNode()->getOperand(2);
7022
7023 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7024 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007025 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007026 }
7027 }
7028 return SDValue();
7029}
7030
Bill Wendling056292f2008-09-16 21:48:12 +00007031// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7032// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7033// one of the above mentioned nodes. It has to be wrapped because otherwise
7034// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7035// be used to form addressing mode. These wrapped nodes will be selected
7036// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007037SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007038X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007039 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007040
Chris Lattner41621a22009-06-26 19:22:52 +00007041 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7042 // global base reg.
7043 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007044 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007045 CodeModel::Model M = getTargetMachine().getCodeModel();
7046
Chris Lattner4f066492009-07-11 20:29:19 +00007047 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007048 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007049 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007050 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007051 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007052 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007053 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007054
Evan Cheng1606e8e2009-03-13 07:51:59 +00007055 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007056 CP->getAlignment(),
7057 CP->getOffset(), OpFlag);
7058 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007059 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007060 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007061 if (OpFlag) {
7062 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007063 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007064 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007065 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007066 }
7067
7068 return Result;
7069}
7070
Dan Gohmand858e902010-04-17 15:26:15 +00007071SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007072 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007073
Chris Lattner18c59872009-06-27 04:16:01 +00007074 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7075 // global base reg.
7076 unsigned char OpFlag = 0;
7077 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007078 CodeModel::Model M = getTargetMachine().getCodeModel();
7079
Chris Lattner4f066492009-07-11 20:29:19 +00007080 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007081 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007082 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007083 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007084 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007085 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007086 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007087
Chris Lattner18c59872009-06-27 04:16:01 +00007088 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7089 OpFlag);
7090 DebugLoc DL = JT->getDebugLoc();
7091 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007092
Chris Lattner18c59872009-06-27 04:16:01 +00007093 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007094 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007095 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7096 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007097 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007098 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007099
Chris Lattner18c59872009-06-27 04:16:01 +00007100 return Result;
7101}
7102
7103SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007104X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007105 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007106
Chris Lattner18c59872009-06-27 04:16:01 +00007107 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7108 // global base reg.
7109 unsigned char OpFlag = 0;
7110 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007111 CodeModel::Model M = getTargetMachine().getCodeModel();
7112
Chris Lattner4f066492009-07-11 20:29:19 +00007113 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007114 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7115 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7116 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007117 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007118 } else if (Subtarget->isPICStyleGOT()) {
7119 OpFlag = X86II::MO_GOT;
7120 } else if (Subtarget->isPICStyleStubPIC()) {
7121 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7122 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7123 OpFlag = X86II::MO_DARWIN_NONLAZY;
7124 }
Eric Christopherfd179292009-08-27 18:07:15 +00007125
Chris Lattner18c59872009-06-27 04:16:01 +00007126 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007127
Chris Lattner18c59872009-06-27 04:16:01 +00007128 DebugLoc DL = Op.getDebugLoc();
7129 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007130
7131
Chris Lattner18c59872009-06-27 04:16:01 +00007132 // With PIC, the address is actually $g + Offset.
7133 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007134 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007135 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7136 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007137 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007138 Result);
7139 }
Eric Christopherfd179292009-08-27 18:07:15 +00007140
Eli Friedman586272d2011-08-11 01:48:05 +00007141 // For symbols that require a load from a stub to get the address, emit the
7142 // load.
7143 if (isGlobalStubReference(OpFlag))
7144 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007145 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007146
Chris Lattner18c59872009-06-27 04:16:01 +00007147 return Result;
7148}
7149
Dan Gohman475871a2008-07-27 21:46:04 +00007150SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007151X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007152 // Create the TargetBlockAddressAddress node.
7153 unsigned char OpFlags =
7154 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007155 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007156 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007157 DebugLoc dl = Op.getDebugLoc();
7158 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7159 /*isTarget=*/true, OpFlags);
7160
Dan Gohmanf705adb2009-10-30 01:28:02 +00007161 if (Subtarget->isPICStyleRIPRel() &&
7162 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007163 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7164 else
7165 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007166
Dan Gohman29cbade2009-11-20 23:18:13 +00007167 // With PIC, the address is actually $g + Offset.
7168 if (isGlobalRelativeToPICBase(OpFlags)) {
7169 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7170 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7171 Result);
7172 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007173
7174 return Result;
7175}
7176
7177SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007178X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007179 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007180 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007181 // Create the TargetGlobalAddress node, folding in the constant
7182 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007183 unsigned char OpFlags =
7184 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007185 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007186 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007187 if (OpFlags == X86II::MO_NO_FLAG &&
7188 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007189 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007190 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007191 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007192 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007193 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007194 }
Eric Christopherfd179292009-08-27 18:07:15 +00007195
Chris Lattner4f066492009-07-11 20:29:19 +00007196 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007197 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007198 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7199 else
7200 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007201
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007202 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007203 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007204 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7205 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007206 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007207 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007208
Chris Lattner36c25012009-07-10 07:34:39 +00007209 // For globals that require a load from a stub to get the address, emit the
7210 // load.
7211 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007212 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007213 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007214
Dan Gohman6520e202008-10-18 02:06:02 +00007215 // If there was a non-zero offset that we didn't fold, create an explicit
7216 // addition for it.
7217 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007218 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007219 DAG.getConstant(Offset, getPointerTy()));
7220
Evan Cheng0db9fe62006-04-25 20:13:52 +00007221 return Result;
7222}
7223
Evan Chengda43bcf2008-09-24 00:05:32 +00007224SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007225X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007226 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007227 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007228 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007229}
7230
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007231static SDValue
7232GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007233 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007234 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007235 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007236 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007237 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007238 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007239 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007240 GA->getOffset(),
7241 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007242 if (InFlag) {
7243 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007244 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007245 } else {
7246 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007247 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007248 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007249
7250 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007251 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007252
Rafael Espindola15f1b662009-04-24 12:59:40 +00007253 SDValue Flag = Chain.getValue(1);
7254 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007255}
7256
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007257// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007258static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007259LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007260 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007261 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007262 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7263 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007264 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007265 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007266 InFlag = Chain.getValue(1);
7267
Chris Lattnerb903bed2009-06-26 21:20:29 +00007268 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007269}
7270
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007271// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007272static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007273LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007274 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007275 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7276 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007277}
7278
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007279// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7280// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007281static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007282 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007283 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007284 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007285
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007286 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7287 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7288 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007289
Michael J. Spencerec38de22010-10-10 22:04:20 +00007290 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007291 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007292 MachinePointerInfo(Ptr),
7293 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007294
Chris Lattnerb903bed2009-06-26 21:20:29 +00007295 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007296 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7297 // initialexec.
7298 unsigned WrapperKind = X86ISD::Wrapper;
7299 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007300 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007301 } else if (is64Bit) {
7302 assert(model == TLSModel::InitialExec);
7303 OperandFlags = X86II::MO_GOTTPOFF;
7304 WrapperKind = X86ISD::WrapperRIP;
7305 } else {
7306 assert(model == TLSModel::InitialExec);
7307 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007308 }
Eric Christopherfd179292009-08-27 18:07:15 +00007309
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007310 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7311 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007312 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007313 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007314 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007315 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007316
Rafael Espindola9a580232009-02-27 13:37:18 +00007317 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007318 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007319 MachinePointerInfo::getGOT(), false, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007320
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007321 // The address of the thread local variable is the add of the thread
7322 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007323 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007324}
7325
Dan Gohman475871a2008-07-27 21:46:04 +00007326SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007327X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007328
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007329 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007330 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007331
Eric Christopher30ef0e52010-06-03 04:07:48 +00007332 if (Subtarget->isTargetELF()) {
7333 // TODO: implement the "local dynamic" model
7334 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007335
Eric Christopher30ef0e52010-06-03 04:07:48 +00007336 // If GV is an alias then use the aliasee for determining
7337 // thread-localness.
7338 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7339 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007340
7341 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007342 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007343
Eric Christopher30ef0e52010-06-03 04:07:48 +00007344 switch (model) {
7345 case TLSModel::GeneralDynamic:
7346 case TLSModel::LocalDynamic: // not implemented
7347 if (Subtarget->is64Bit())
7348 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7349 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007350
Eric Christopher30ef0e52010-06-03 04:07:48 +00007351 case TLSModel::InitialExec:
7352 case TLSModel::LocalExec:
7353 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7354 Subtarget->is64Bit());
7355 }
7356 } else if (Subtarget->isTargetDarwin()) {
7357 // Darwin only has one model of TLS. Lower to that.
7358 unsigned char OpFlag = 0;
7359 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7360 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007361
Eric Christopher30ef0e52010-06-03 04:07:48 +00007362 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7363 // global base reg.
7364 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7365 !Subtarget->is64Bit();
7366 if (PIC32)
7367 OpFlag = X86II::MO_TLVP_PIC_BASE;
7368 else
7369 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007370 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007371 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007372 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007373 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007374 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007375
Eric Christopher30ef0e52010-06-03 04:07:48 +00007376 // With PIC32, the address is actually $g + Offset.
7377 if (PIC32)
7378 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7379 DAG.getNode(X86ISD::GlobalBaseReg,
7380 DebugLoc(), getPointerTy()),
7381 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007382
Eric Christopher30ef0e52010-06-03 04:07:48 +00007383 // Lowering the machine isd will make sure everything is in the right
7384 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007385 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007386 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007387 SDValue Args[] = { Chain, Offset };
7388 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007389
Eric Christopher30ef0e52010-06-03 04:07:48 +00007390 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7391 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7392 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007393
Eric Christopher30ef0e52010-06-03 04:07:48 +00007394 // And our return value (tls address) is in the standard call return value
7395 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007396 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007397 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7398 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007399 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007400
Eric Christopher30ef0e52010-06-03 04:07:48 +00007401 assert(false &&
7402 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007403
Torok Edwinc23197a2009-07-14 16:55:14 +00007404 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007405 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007406}
7407
Evan Cheng0db9fe62006-04-25 20:13:52 +00007408
Nadav Rotem43012222011-05-11 08:12:09 +00007409/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007410/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007411SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007412 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007413 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007414 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007415 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007416 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007417 SDValue ShOpLo = Op.getOperand(0);
7418 SDValue ShOpHi = Op.getOperand(1);
7419 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007420 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007421 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007422 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007423
Dan Gohman475871a2008-07-27 21:46:04 +00007424 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007425 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007426 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7427 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007428 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007429 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7430 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007431 }
Evan Chenge3413162006-01-09 18:33:28 +00007432
Owen Anderson825b72b2009-08-11 20:47:22 +00007433 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7434 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007435 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007436 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007437
Dan Gohman475871a2008-07-27 21:46:04 +00007438 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007439 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007440 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7441 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007442
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007443 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007444 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7445 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007446 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007447 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7448 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007449 }
7450
Dan Gohman475871a2008-07-27 21:46:04 +00007451 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007452 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007453}
Evan Chenga3195e82006-01-12 22:54:21 +00007454
Dan Gohmand858e902010-04-17 15:26:15 +00007455SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7456 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007457 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007458
Dale Johannesen0488fb62010-09-30 23:57:10 +00007459 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007460 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007461
Owen Anderson825b72b2009-08-11 20:47:22 +00007462 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007463 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007464
Eli Friedman36df4992009-05-27 00:47:34 +00007465 // These are really Legal; return the operand so the caller accepts it as
7466 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007467 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007468 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007469 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007470 Subtarget->is64Bit()) {
7471 return Op;
7472 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007473
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007474 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007475 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007476 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007477 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007478 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007479 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007480 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007481 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007482 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007483 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7484}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007485
Owen Andersone50ed302009-08-10 22:56:29 +00007486SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007487 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007488 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007489 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007490 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007491 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007492 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007493 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007494 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007495 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007496 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007497
Chris Lattner492a43e2010-09-22 01:28:21 +00007498 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007499
Stuart Hastings84be9582011-06-02 15:57:11 +00007500 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7501 MachineMemOperand *MMO;
7502 if (FI) {
7503 int SSFI = FI->getIndex();
7504 MMO =
7505 DAG.getMachineFunction()
7506 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7507 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7508 } else {
7509 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7510 StackSlot = StackSlot.getOperand(1);
7511 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007512 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007513 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7514 X86ISD::FILD, DL,
7515 Tys, Ops, array_lengthof(Ops),
7516 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007517
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007518 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007519 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007520 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007521
7522 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7523 // shouldn't be necessary except that RFP cannot be live across
7524 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007525 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007526 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7527 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007528 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007529 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007530 SDValue Ops[] = {
7531 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7532 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007533 MachineMemOperand *MMO =
7534 DAG.getMachineFunction()
7535 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007536 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007537
Chris Lattner492a43e2010-09-22 01:28:21 +00007538 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7539 Ops, array_lengthof(Ops),
7540 Op.getValueType(), MMO);
7541 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007542 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007543 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007544 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007545
Evan Cheng0db9fe62006-04-25 20:13:52 +00007546 return Result;
7547}
7548
Bill Wendling8b8a6362009-01-17 03:56:04 +00007549// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007550SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7551 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007552 // This algorithm is not obvious. Here it is in C code, more or less:
7553 /*
7554 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7555 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7556 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007557
Bill Wendling8b8a6362009-01-17 03:56:04 +00007558 // Copy ints to xmm registers.
7559 __m128i xh = _mm_cvtsi32_si128( hi );
7560 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007561
Bill Wendling8b8a6362009-01-17 03:56:04 +00007562 // Combine into low half of a single xmm register.
7563 __m128i x = _mm_unpacklo_epi32( xh, xl );
7564 __m128d d;
7565 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007566
Bill Wendling8b8a6362009-01-17 03:56:04 +00007567 // Merge in appropriate exponents to give the integer bits the right
7568 // magnitude.
7569 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007570
Bill Wendling8b8a6362009-01-17 03:56:04 +00007571 // Subtract away the biases to deal with the IEEE-754 double precision
7572 // implicit 1.
7573 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007574
Bill Wendling8b8a6362009-01-17 03:56:04 +00007575 // All conversions up to here are exact. The correctly rounded result is
7576 // calculated using the current rounding mode using the following
7577 // horizontal add.
7578 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7579 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7580 // store doesn't really need to be here (except
7581 // maybe to zero the other double)
7582 return sd;
7583 }
7584 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007585
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007586 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007587 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007588
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007589 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007590 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007591 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7592 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7593 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7594 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007595 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007596 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007597
Bill Wendling8b8a6362009-01-17 03:56:04 +00007598 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007599 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007600 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007601 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007602 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007603 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007604 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007605
Owen Anderson825b72b2009-08-11 20:47:22 +00007606 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7607 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007608 Op.getOperand(0),
7609 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007610 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7611 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007612 Op.getOperand(0),
7613 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007614 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7615 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007616 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007617 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007618 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007619 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007620 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007621 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007622 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007623 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007624
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007625 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007626 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007627 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7628 DAG.getUNDEF(MVT::v2f64), ShufMask);
7629 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7630 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007631 DAG.getIntPtrConstant(0));
7632}
7633
Bill Wendling8b8a6362009-01-17 03:56:04 +00007634// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007635SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7636 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007637 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007638 // FP constant to bias correct the final result.
7639 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007640 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007641
7642 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007643 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007644 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007645
Eli Friedmanf3704762011-08-29 21:15:46 +00007646 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007647 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7648 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007649
Owen Anderson825b72b2009-08-11 20:47:22 +00007650 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007651 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007652 DAG.getIntPtrConstant(0));
7653
7654 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007655 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007656 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007657 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007658 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007659 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007660 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007661 MVT::v2f64, Bias)));
7662 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007663 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007664 DAG.getIntPtrConstant(0));
7665
7666 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007667 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007668
7669 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007670 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007671
Owen Anderson825b72b2009-08-11 20:47:22 +00007672 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007673 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007674 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007675 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007676 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007677 }
7678
7679 // Handle final rounding.
7680 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007681}
7682
Dan Gohmand858e902010-04-17 15:26:15 +00007683SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7684 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007685 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007686 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007687
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007688 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007689 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7690 // the optimization here.
7691 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007692 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007693
Owen Andersone50ed302009-08-10 22:56:29 +00007694 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007695 EVT DstVT = Op.getValueType();
7696 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007697 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007698 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007699 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007700
7701 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007702 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007703 if (SrcVT == MVT::i32) {
7704 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7705 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7706 getPointerTy(), StackSlot, WordOff);
7707 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007708 StackSlot, MachinePointerInfo(),
7709 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007710 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007711 OffsetSlot, MachinePointerInfo(),
7712 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007713 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7714 return Fild;
7715 }
7716
7717 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7718 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007719 StackSlot, MachinePointerInfo(),
7720 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007721 // For i64 source, we need to add the appropriate power of 2 if the input
7722 // was negative. This is the same as the optimization in
7723 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7724 // we must be careful to do the computation in x87 extended precision, not
7725 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007726 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7727 MachineMemOperand *MMO =
7728 DAG.getMachineFunction()
7729 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7730 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007731
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007732 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7733 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007734 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7735 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007736
7737 APInt FF(32, 0x5F800000ULL);
7738
7739 // Check whether the sign bit is set.
7740 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7741 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7742 ISD::SETLT);
7743
7744 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7745 SDValue FudgePtr = DAG.getConstantPool(
7746 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7747 getPointerTy());
7748
7749 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7750 SDValue Zero = DAG.getIntPtrConstant(0);
7751 SDValue Four = DAG.getIntPtrConstant(4);
7752 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7753 Zero, Four);
7754 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7755
7756 // Load the value out, extending it from f32 to f80.
7757 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007758 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007759 FudgePtr, MachinePointerInfo::getConstantPool(),
7760 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007761 // Extend everything to 80 bits to force it to be done on x87.
7762 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7763 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007764}
7765
Dan Gohman475871a2008-07-27 21:46:04 +00007766std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007767FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007768 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007769
Owen Andersone50ed302009-08-10 22:56:29 +00007770 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007771
7772 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007773 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7774 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007775 }
7776
Owen Anderson825b72b2009-08-11 20:47:22 +00007777 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7778 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007779 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007780
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007781 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007782 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007783 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007784 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007785 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007786 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007787 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007788 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007789
Evan Cheng87c89352007-10-15 20:11:21 +00007790 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7791 // stack slot.
7792 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007793 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007794 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007795 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007796
Michael J. Spencerec38de22010-10-10 22:04:20 +00007797
7798
Evan Cheng0db9fe62006-04-25 20:13:52 +00007799 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007800 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007801 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007802 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7803 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7804 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007805 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007806
Dan Gohman475871a2008-07-27 21:46:04 +00007807 SDValue Chain = DAG.getEntryNode();
7808 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007809 EVT TheVT = Op.getOperand(0).getValueType();
7810 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007811 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007812 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007813 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007814 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007815 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007816 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007817 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007818 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007819
Chris Lattner492a43e2010-09-22 01:28:21 +00007820 MachineMemOperand *MMO =
7821 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7822 MachineMemOperand::MOLoad, MemSize, MemSize);
7823 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7824 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007825 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007826 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007827 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7828 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007829
Chris Lattner07290932010-09-22 01:05:16 +00007830 MachineMemOperand *MMO =
7831 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7832 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007833
Evan Cheng0db9fe62006-04-25 20:13:52 +00007834 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007835 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007836 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7837 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007838
Chris Lattner27a6c732007-11-24 07:07:01 +00007839 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007840}
7841
Dan Gohmand858e902010-04-17 15:26:15 +00007842SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7843 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007844 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007845 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007846
Eli Friedman948e95a2009-05-23 09:59:16 +00007847 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007848 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007849 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7850 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007851
Chris Lattner27a6c732007-11-24 07:07:01 +00007852 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007853 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007854 FIST, StackSlot, MachinePointerInfo(),
7855 false, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007856}
7857
Dan Gohmand858e902010-04-17 15:26:15 +00007858SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7859 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007860 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7861 SDValue FIST = Vals.first, StackSlot = Vals.second;
7862 assert(FIST.getNode() && "Unexpected failure");
7863
7864 // Load the result.
7865 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007866 FIST, StackSlot, MachinePointerInfo(),
7867 false, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007868}
7869
Dan Gohmand858e902010-04-17 15:26:15 +00007870SDValue X86TargetLowering::LowerFABS(SDValue Op,
7871 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007872 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007873 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007874 EVT VT = Op.getValueType();
7875 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007876 if (VT.isVector())
7877 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007878 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007879 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007880 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007881 CV.push_back(C);
7882 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007883 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007884 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007885 CV.push_back(C);
7886 CV.push_back(C);
7887 CV.push_back(C);
7888 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007889 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007890 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007891 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007892 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007893 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007894 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007895 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007896}
7897
Dan Gohmand858e902010-04-17 15:26:15 +00007898SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007899 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007900 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007901 EVT VT = Op.getValueType();
7902 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007903 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007904 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007905 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007906 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007907 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007908 CV.push_back(C);
7909 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007910 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007911 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007912 CV.push_back(C);
7913 CV.push_back(C);
7914 CV.push_back(C);
7915 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007916 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007917 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007918 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007919 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007920 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007921 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007922 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007923 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007924 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007925 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007926 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007927 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007928 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007929 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007930 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007931}
7932
Dan Gohmand858e902010-04-17 15:26:15 +00007933SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007934 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007935 SDValue Op0 = Op.getOperand(0);
7936 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007937 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007938 EVT VT = Op.getValueType();
7939 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007940
7941 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007942 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007943 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007944 SrcVT = VT;
7945 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007946 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007947 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007948 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007949 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007950 }
7951
7952 // At this point the operands and the result should have the same
7953 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007954
Evan Cheng68c47cb2007-01-05 07:55:56 +00007955 // First get the sign bit of second operand.
7956 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007957 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007958 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7959 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007960 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007961 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7962 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7963 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7964 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007965 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007966 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007967 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007968 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007969 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007970 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007971 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007972
7973 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007974 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007975 // Op0 is MVT::f32, Op1 is MVT::f64.
7976 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7977 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7978 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007979 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007980 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007981 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007982 }
7983
Evan Cheng73d6cf12007-01-05 21:37:56 +00007984 // Clear first operand sign bit.
7985 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007986 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007987 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7988 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007989 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007990 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7991 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7992 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7993 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007994 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007995 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007996 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007997 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007998 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007999 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008000 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008001
8002 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008003 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008004}
8005
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008006SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8007 SDValue N0 = Op.getOperand(0);
8008 DebugLoc dl = Op.getDebugLoc();
8009 EVT VT = Op.getValueType();
8010
8011 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8012 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8013 DAG.getConstant(1, VT));
8014 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8015}
8016
Dan Gohman076aee32009-03-04 19:44:21 +00008017/// Emit nodes that will be selected as "test Op0,Op0", or something
8018/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008019SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008020 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008021 DebugLoc dl = Op.getDebugLoc();
8022
Dan Gohman31125812009-03-07 01:58:32 +00008023 // CF and OF aren't always set the way we want. Determine which
8024 // of these we need.
8025 bool NeedCF = false;
8026 bool NeedOF = false;
8027 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008028 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008029 case X86::COND_A: case X86::COND_AE:
8030 case X86::COND_B: case X86::COND_BE:
8031 NeedCF = true;
8032 break;
8033 case X86::COND_G: case X86::COND_GE:
8034 case X86::COND_L: case X86::COND_LE:
8035 case X86::COND_O: case X86::COND_NO:
8036 NeedOF = true;
8037 break;
Dan Gohman31125812009-03-07 01:58:32 +00008038 }
8039
Dan Gohman076aee32009-03-04 19:44:21 +00008040 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008041 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8042 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008043 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8044 // Emit a CMP with 0, which is the TEST pattern.
8045 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8046 DAG.getConstant(0, Op.getValueType()));
8047
8048 unsigned Opcode = 0;
8049 unsigned NumOperands = 0;
8050 switch (Op.getNode()->getOpcode()) {
8051 case ISD::ADD:
8052 // Due to an isel shortcoming, be conservative if this add is likely to be
8053 // selected as part of a load-modify-store instruction. When the root node
8054 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8055 // uses of other nodes in the match, such as the ADD in this case. This
8056 // leads to the ADD being left around and reselected, with the result being
8057 // two adds in the output. Alas, even if none our users are stores, that
8058 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8059 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8060 // climbing the DAG back to the root, and it doesn't seem to be worth the
8061 // effort.
8062 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008063 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8064 if (UI->getOpcode() != ISD::CopyToReg &&
8065 UI->getOpcode() != ISD::SETCC &&
8066 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008067 goto default_case;
8068
8069 if (ConstantSDNode *C =
8070 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8071 // An add of one will be selected as an INC.
8072 if (C->getAPIntValue() == 1) {
8073 Opcode = X86ISD::INC;
8074 NumOperands = 1;
8075 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008076 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008077
8078 // An add of negative one (subtract of one) will be selected as a DEC.
8079 if (C->getAPIntValue().isAllOnesValue()) {
8080 Opcode = X86ISD::DEC;
8081 NumOperands = 1;
8082 break;
8083 }
Dan Gohman076aee32009-03-04 19:44:21 +00008084 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008085
8086 // Otherwise use a regular EFLAGS-setting add.
8087 Opcode = X86ISD::ADD;
8088 NumOperands = 2;
8089 break;
8090 case ISD::AND: {
8091 // If the primary and result isn't used, don't bother using X86ISD::AND,
8092 // because a TEST instruction will be better.
8093 bool NonFlagUse = false;
8094 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8095 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8096 SDNode *User = *UI;
8097 unsigned UOpNo = UI.getOperandNo();
8098 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8099 // Look pass truncate.
8100 UOpNo = User->use_begin().getOperandNo();
8101 User = *User->use_begin();
8102 }
8103
8104 if (User->getOpcode() != ISD::BRCOND &&
8105 User->getOpcode() != ISD::SETCC &&
8106 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8107 NonFlagUse = true;
8108 break;
8109 }
Dan Gohman076aee32009-03-04 19:44:21 +00008110 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008111
8112 if (!NonFlagUse)
8113 break;
8114 }
8115 // FALL THROUGH
8116 case ISD::SUB:
8117 case ISD::OR:
8118 case ISD::XOR:
8119 // Due to the ISEL shortcoming noted above, be conservative if this op is
8120 // likely to be selected as part of a load-modify-store instruction.
8121 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8122 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8123 if (UI->getOpcode() == ISD::STORE)
8124 goto default_case;
8125
8126 // Otherwise use a regular EFLAGS-setting instruction.
8127 switch (Op.getNode()->getOpcode()) {
8128 default: llvm_unreachable("unexpected operator!");
8129 case ISD::SUB: Opcode = X86ISD::SUB; break;
8130 case ISD::OR: Opcode = X86ISD::OR; break;
8131 case ISD::XOR: Opcode = X86ISD::XOR; break;
8132 case ISD::AND: Opcode = X86ISD::AND; break;
8133 }
8134
8135 NumOperands = 2;
8136 break;
8137 case X86ISD::ADD:
8138 case X86ISD::SUB:
8139 case X86ISD::INC:
8140 case X86ISD::DEC:
8141 case X86ISD::OR:
8142 case X86ISD::XOR:
8143 case X86ISD::AND:
8144 return SDValue(Op.getNode(), 1);
8145 default:
8146 default_case:
8147 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008148 }
8149
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008150 if (Opcode == 0)
8151 // Emit a CMP with 0, which is the TEST pattern.
8152 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8153 DAG.getConstant(0, Op.getValueType()));
8154
8155 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8156 SmallVector<SDValue, 4> Ops;
8157 for (unsigned i = 0; i != NumOperands; ++i)
8158 Ops.push_back(Op.getOperand(i));
8159
8160 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8161 DAG.ReplaceAllUsesWith(Op, New);
8162 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008163}
8164
8165/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8166/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008167SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008168 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008169 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8170 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008171 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008172
8173 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008174 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008175}
8176
Evan Chengd40d03e2010-01-06 19:38:29 +00008177/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8178/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008179SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8180 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008181 SDValue Op0 = And.getOperand(0);
8182 SDValue Op1 = And.getOperand(1);
8183 if (Op0.getOpcode() == ISD::TRUNCATE)
8184 Op0 = Op0.getOperand(0);
8185 if (Op1.getOpcode() == ISD::TRUNCATE)
8186 Op1 = Op1.getOperand(0);
8187
Evan Chengd40d03e2010-01-06 19:38:29 +00008188 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008189 if (Op1.getOpcode() == ISD::SHL)
8190 std::swap(Op0, Op1);
8191 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008192 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8193 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008194 // If we looked past a truncate, check that it's only truncating away
8195 // known zeros.
8196 unsigned BitWidth = Op0.getValueSizeInBits();
8197 unsigned AndBitWidth = And.getValueSizeInBits();
8198 if (BitWidth > AndBitWidth) {
8199 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8200 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8201 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8202 return SDValue();
8203 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008204 LHS = Op1;
8205 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008206 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008207 } else if (Op1.getOpcode() == ISD::Constant) {
8208 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008209 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008210 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008211
8212 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008213 LHS = AndLHS.getOperand(0);
8214 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008215 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008216
8217 // Use BT if the immediate can't be encoded in a TEST instruction.
8218 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8219 LHS = AndLHS;
8220 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8221 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008222 }
Evan Cheng0488db92007-09-25 01:57:46 +00008223
Evan Chengd40d03e2010-01-06 19:38:29 +00008224 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008225 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008226 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008227 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008228 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008229 // Also promote i16 to i32 for performance / code size reason.
8230 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008231 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008232 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008233
Evan Chengd40d03e2010-01-06 19:38:29 +00008234 // If the operand types disagree, extend the shift amount to match. Since
8235 // BT ignores high bits (like shifts) we can use anyextend.
8236 if (LHS.getValueType() != RHS.getValueType())
8237 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008238
Evan Chengd40d03e2010-01-06 19:38:29 +00008239 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8240 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8241 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8242 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008243 }
8244
Evan Cheng54de3ea2010-01-05 06:52:31 +00008245 return SDValue();
8246}
8247
Dan Gohmand858e902010-04-17 15:26:15 +00008248SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008249
8250 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8251
Evan Cheng54de3ea2010-01-05 06:52:31 +00008252 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8253 SDValue Op0 = Op.getOperand(0);
8254 SDValue Op1 = Op.getOperand(1);
8255 DebugLoc dl = Op.getDebugLoc();
8256 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8257
8258 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008259 // Lower (X & (1 << N)) == 0 to BT(X, N).
8260 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8261 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008262 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008263 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008264 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008265 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8266 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8267 if (NewSetCC.getNode())
8268 return NewSetCC;
8269 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008270
Chris Lattner481eebc2010-12-19 21:23:48 +00008271 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8272 // these.
8273 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008274 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008275 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8276 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008277
Chris Lattner481eebc2010-12-19 21:23:48 +00008278 // If the input is a setcc, then reuse the input setcc or use a new one with
8279 // the inverted condition.
8280 if (Op0.getOpcode() == X86ISD::SETCC) {
8281 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8282 bool Invert = (CC == ISD::SETNE) ^
8283 cast<ConstantSDNode>(Op1)->isNullValue();
8284 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008285
Evan Cheng2c755ba2010-02-27 07:36:59 +00008286 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008287 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8288 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8289 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008290 }
8291
Evan Chenge5b51ac2010-04-17 06:13:15 +00008292 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008293 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008294 if (X86CC == X86::COND_INVALID)
8295 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008296
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008297 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008298 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008299 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008300}
8301
Craig Topper89af15e2011-09-18 08:03:58 +00008302// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008303// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008304static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008305 EVT VT = Op.getValueType();
8306
Duncan Sands28b77e92011-09-06 19:07:46 +00008307 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008308 "Unsupported value type for operation");
8309
8310 int NumElems = VT.getVectorNumElements();
8311 DebugLoc dl = Op.getDebugLoc();
8312 SDValue CC = Op.getOperand(2);
8313 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8314 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8315
8316 // Extract the LHS vectors
8317 SDValue LHS = Op.getOperand(0);
8318 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8319 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8320
8321 // Extract the RHS vectors
8322 SDValue RHS = Op.getOperand(1);
8323 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8324 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8325
8326 // Issue the operation on the smaller types and concatenate the result back
8327 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8328 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8329 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8330 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8331 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8332}
8333
8334
Dan Gohmand858e902010-04-17 15:26:15 +00008335SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008336 SDValue Cond;
8337 SDValue Op0 = Op.getOperand(0);
8338 SDValue Op1 = Op.getOperand(1);
8339 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008340 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008341 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8342 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008343 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008344
8345 if (isFP) {
8346 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008347 EVT EltVT = Op0.getValueType().getVectorElementType();
8348 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8349
8350 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008351 bool Swap = false;
8352
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008353 // SSE Condition code mapping:
8354 // 0 - EQ
8355 // 1 - LT
8356 // 2 - LE
8357 // 3 - UNORD
8358 // 4 - NEQ
8359 // 5 - NLT
8360 // 6 - NLE
8361 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008362 switch (SetCCOpcode) {
8363 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008364 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008365 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008366 case ISD::SETOGT:
8367 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008368 case ISD::SETLT:
8369 case ISD::SETOLT: SSECC = 1; break;
8370 case ISD::SETOGE:
8371 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008372 case ISD::SETLE:
8373 case ISD::SETOLE: SSECC = 2; break;
8374 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008375 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008376 case ISD::SETNE: SSECC = 4; break;
8377 case ISD::SETULE: Swap = true;
8378 case ISD::SETUGE: SSECC = 5; break;
8379 case ISD::SETULT: Swap = true;
8380 case ISD::SETUGT: SSECC = 6; break;
8381 case ISD::SETO: SSECC = 7; break;
8382 }
8383 if (Swap)
8384 std::swap(Op0, Op1);
8385
Nate Begemanfb8ead02008-07-25 19:05:58 +00008386 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008387 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008388 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008389 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008390 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8391 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008392 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Craig Topper0a150352011-11-09 08:06:13 +00008393 } else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008394 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008395 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8396 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008397 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008398 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008399 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008400 }
8401 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008402 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008403 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008404
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008405 // Break 256-bit integer vector compare into smaller ones.
Craig Topper0a150352011-11-09 08:06:13 +00008406 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008407 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008408
Nate Begeman30a0de92008-07-17 16:51:19 +00008409 // We are handling one of the integer comparisons here. Since SSE only has
8410 // GT and EQ comparisons for integer, swapping operands and multiple
8411 // operations may be required for some comparisons.
8412 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8413 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008414
Craig Topper0a150352011-11-09 08:06:13 +00008415 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008416 default: break;
Craig Topper0a150352011-11-09 08:06:13 +00008417 case MVT::i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
8418 case MVT::i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
8419 case MVT::i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8420 case MVT::i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008421 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008422
Nate Begeman30a0de92008-07-17 16:51:19 +00008423 switch (SetCCOpcode) {
8424 default: break;
8425 case ISD::SETNE: Invert = true;
8426 case ISD::SETEQ: Opc = EQOpc; break;
8427 case ISD::SETLT: Swap = true;
8428 case ISD::SETGT: Opc = GTOpc; break;
8429 case ISD::SETGE: Swap = true;
8430 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8431 case ISD::SETULT: Swap = true;
8432 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8433 case ISD::SETUGE: Swap = true;
8434 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8435 }
8436 if (Swap)
8437 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008438
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008439 // Check that the operation in question is available (most are plain SSE2,
8440 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topperc0d82852011-11-22 00:44:41 +00008441 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42orAVX())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008442 return SDValue();
Craig Topperc0d82852011-11-22 00:44:41 +00008443 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41orAVX())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008444 return SDValue();
8445
Nate Begeman30a0de92008-07-17 16:51:19 +00008446 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8447 // bits of the inputs before performing those operations.
8448 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008449 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008450 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8451 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008452 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008453 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8454 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008455 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8456 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008457 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008458
Dale Johannesenace16102009-02-03 19:33:06 +00008459 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008460
8461 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008462 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008463 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008464
Nate Begeman30a0de92008-07-17 16:51:19 +00008465 return Result;
8466}
Evan Cheng0488db92007-09-25 01:57:46 +00008467
Evan Cheng370e5342008-12-03 08:38:43 +00008468// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008469static bool isX86LogicalCmp(SDValue Op) {
8470 unsigned Opc = Op.getNode()->getOpcode();
8471 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8472 return true;
8473 if (Op.getResNo() == 1 &&
8474 (Opc == X86ISD::ADD ||
8475 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008476 Opc == X86ISD::ADC ||
8477 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008478 Opc == X86ISD::SMUL ||
8479 Opc == X86ISD::UMUL ||
8480 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008481 Opc == X86ISD::DEC ||
8482 Opc == X86ISD::OR ||
8483 Opc == X86ISD::XOR ||
8484 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008485 return true;
8486
Chris Lattner9637d5b2010-12-05 07:49:54 +00008487 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8488 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008489
Dan Gohman076aee32009-03-04 19:44:21 +00008490 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008491}
8492
Chris Lattnera2b56002010-12-05 01:23:24 +00008493static bool isZero(SDValue V) {
8494 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8495 return C && C->isNullValue();
8496}
8497
Chris Lattner96908b12010-12-05 02:00:51 +00008498static bool isAllOnes(SDValue V) {
8499 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8500 return C && C->isAllOnesValue();
8501}
8502
Dan Gohmand858e902010-04-17 15:26:15 +00008503SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008504 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008505 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008506 SDValue Op1 = Op.getOperand(1);
8507 SDValue Op2 = Op.getOperand(2);
8508 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008509 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008510
Dan Gohman1a492952009-10-20 16:22:37 +00008511 if (Cond.getOpcode() == ISD::SETCC) {
8512 SDValue NewCond = LowerSETCC(Cond, DAG);
8513 if (NewCond.getNode())
8514 Cond = NewCond;
8515 }
Evan Cheng734503b2006-09-11 02:19:56 +00008516
Chris Lattnera2b56002010-12-05 01:23:24 +00008517 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008518 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008519 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008520 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008521 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008522 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8523 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008524 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008525
Chris Lattnera2b56002010-12-05 01:23:24 +00008526 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008527
8528 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008529 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8530 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008531
8532 SDValue CmpOp0 = Cmp.getOperand(0);
8533 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8534 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008535
Chris Lattner96908b12010-12-05 02:00:51 +00008536 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008537 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8538 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008539
Chris Lattner96908b12010-12-05 02:00:51 +00008540 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8541 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008542
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008543 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008544 if (N2C == 0 || !N2C->isNullValue())
8545 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8546 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008547 }
8548 }
8549
Chris Lattnera2b56002010-12-05 01:23:24 +00008550 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008551 if (Cond.getOpcode() == ISD::AND &&
8552 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8553 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008554 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008555 Cond = Cond.getOperand(0);
8556 }
8557
Evan Cheng3f41d662007-10-08 22:16:29 +00008558 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8559 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008560 unsigned CondOpcode = Cond.getOpcode();
8561 if (CondOpcode == X86ISD::SETCC ||
8562 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008563 CC = Cond.getOperand(0);
8564
Dan Gohman475871a2008-07-27 21:46:04 +00008565 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008566 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008567 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008568
Evan Cheng3f41d662007-10-08 22:16:29 +00008569 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008570 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008571 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008572 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008573
Chris Lattnerd1980a52009-03-12 06:52:53 +00008574 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8575 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008576 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008577 addTest = false;
8578 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008579 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8580 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8581 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8582 Cond.getOperand(0).getValueType() != MVT::i8)) {
8583 SDValue LHS = Cond.getOperand(0);
8584 SDValue RHS = Cond.getOperand(1);
8585 unsigned X86Opcode;
8586 unsigned X86Cond;
8587 SDVTList VTs;
8588 switch (CondOpcode) {
8589 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8590 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8591 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8592 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8593 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8594 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8595 default: llvm_unreachable("unexpected overflowing operator");
8596 }
8597 if (CondOpcode == ISD::UMULO)
8598 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8599 MVT::i32);
8600 else
8601 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8602
8603 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8604
8605 if (CondOpcode == ISD::UMULO)
8606 Cond = X86Op.getValue(2);
8607 else
8608 Cond = X86Op.getValue(1);
8609
8610 CC = DAG.getConstant(X86Cond, MVT::i8);
8611 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008612 }
8613
8614 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008615 // Look pass the truncate.
8616 if (Cond.getOpcode() == ISD::TRUNCATE)
8617 Cond = Cond.getOperand(0);
8618
8619 // We know the result of AND is compared against zero. Try to match
8620 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008621 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008622 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008623 if (NewSetCC.getNode()) {
8624 CC = NewSetCC.getOperand(0);
8625 Cond = NewSetCC.getOperand(1);
8626 addTest = false;
8627 }
8628 }
8629 }
8630
8631 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008632 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008633 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008634 }
8635
Benjamin Kramere915ff32010-12-22 23:09:28 +00008636 // a < b ? -1 : 0 -> RES = ~setcc_carry
8637 // a < b ? 0 : -1 -> RES = setcc_carry
8638 // a >= b ? -1 : 0 -> RES = setcc_carry
8639 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8640 if (Cond.getOpcode() == X86ISD::CMP) {
8641 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8642
8643 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8644 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8645 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8646 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8647 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8648 return DAG.getNOT(DL, Res, Res.getValueType());
8649 return Res;
8650 }
8651 }
8652
Evan Cheng0488db92007-09-25 01:57:46 +00008653 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8654 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008655 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008656 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008657 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008658}
8659
Evan Cheng370e5342008-12-03 08:38:43 +00008660// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8661// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8662// from the AND / OR.
8663static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8664 Opc = Op.getOpcode();
8665 if (Opc != ISD::OR && Opc != ISD::AND)
8666 return false;
8667 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8668 Op.getOperand(0).hasOneUse() &&
8669 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8670 Op.getOperand(1).hasOneUse());
8671}
8672
Evan Cheng961d6d42009-02-02 08:19:07 +00008673// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8674// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008675static bool isXor1OfSetCC(SDValue Op) {
8676 if (Op.getOpcode() != ISD::XOR)
8677 return false;
8678 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8679 if (N1C && N1C->getAPIntValue() == 1) {
8680 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8681 Op.getOperand(0).hasOneUse();
8682 }
8683 return false;
8684}
8685
Dan Gohmand858e902010-04-17 15:26:15 +00008686SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008687 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008688 SDValue Chain = Op.getOperand(0);
8689 SDValue Cond = Op.getOperand(1);
8690 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008691 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008692 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008693 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008694
Dan Gohman1a492952009-10-20 16:22:37 +00008695 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008696 // Check for setcc([su]{add,sub,mul}o == 0).
8697 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8698 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8699 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8700 Cond.getOperand(0).getResNo() == 1 &&
8701 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
8702 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
8703 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
8704 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
8705 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
8706 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
8707 Inverted = true;
8708 Cond = Cond.getOperand(0);
8709 } else {
8710 SDValue NewCond = LowerSETCC(Cond, DAG);
8711 if (NewCond.getNode())
8712 Cond = NewCond;
8713 }
Dan Gohman1a492952009-10-20 16:22:37 +00008714 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008715#if 0
8716 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008717 else if (Cond.getOpcode() == X86ISD::ADD ||
8718 Cond.getOpcode() == X86ISD::SUB ||
8719 Cond.getOpcode() == X86ISD::SMUL ||
8720 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008721 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008722#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008723
Evan Chengad9c0a32009-12-15 00:53:42 +00008724 // Look pass (and (setcc_carry (cmp ...)), 1).
8725 if (Cond.getOpcode() == ISD::AND &&
8726 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8727 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008728 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008729 Cond = Cond.getOperand(0);
8730 }
8731
Evan Cheng3f41d662007-10-08 22:16:29 +00008732 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8733 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008734 unsigned CondOpcode = Cond.getOpcode();
8735 if (CondOpcode == X86ISD::SETCC ||
8736 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008737 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008738
Dan Gohman475871a2008-07-27 21:46:04 +00008739 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008740 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008741 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008742 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008743 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008744 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008745 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008746 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008747 default: break;
8748 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008749 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008750 // These can only come from an arithmetic instruction with overflow,
8751 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008752 Cond = Cond.getNode()->getOperand(1);
8753 addTest = false;
8754 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008755 }
Evan Cheng0488db92007-09-25 01:57:46 +00008756 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008757 }
8758 CondOpcode = Cond.getOpcode();
8759 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8760 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8761 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8762 Cond.getOperand(0).getValueType() != MVT::i8)) {
8763 SDValue LHS = Cond.getOperand(0);
8764 SDValue RHS = Cond.getOperand(1);
8765 unsigned X86Opcode;
8766 unsigned X86Cond;
8767 SDVTList VTs;
8768 switch (CondOpcode) {
8769 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8770 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8771 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8772 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8773 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8774 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8775 default: llvm_unreachable("unexpected overflowing operator");
8776 }
8777 if (Inverted)
8778 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
8779 if (CondOpcode == ISD::UMULO)
8780 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8781 MVT::i32);
8782 else
8783 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8784
8785 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
8786
8787 if (CondOpcode == ISD::UMULO)
8788 Cond = X86Op.getValue(2);
8789 else
8790 Cond = X86Op.getValue(1);
8791
8792 CC = DAG.getConstant(X86Cond, MVT::i8);
8793 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00008794 } else {
8795 unsigned CondOpc;
8796 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8797 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008798 if (CondOpc == ISD::OR) {
8799 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8800 // two branches instead of an explicit OR instruction with a
8801 // separate test.
8802 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008803 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008804 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008805 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008806 Chain, Dest, CC, Cmp);
8807 CC = Cond.getOperand(1).getOperand(0);
8808 Cond = Cmp;
8809 addTest = false;
8810 }
8811 } else { // ISD::AND
8812 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8813 // two branches instead of an explicit AND instruction with a
8814 // separate test. However, we only do this if this block doesn't
8815 // have a fall-through edge, because this requires an explicit
8816 // jmp when the condition is false.
8817 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008818 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008819 Op.getNode()->hasOneUse()) {
8820 X86::CondCode CCode =
8821 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8822 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008823 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008824 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008825 // Look for an unconditional branch following this conditional branch.
8826 // We need this because we need to reverse the successors in order
8827 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008828 if (User->getOpcode() == ISD::BR) {
8829 SDValue FalseBB = User->getOperand(1);
8830 SDNode *NewBR =
8831 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008832 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008833 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008834 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008835
Dale Johannesene4d209d2009-02-03 20:21:25 +00008836 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008837 Chain, Dest, CC, Cmp);
8838 X86::CondCode CCode =
8839 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8840 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008841 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008842 Cond = Cmp;
8843 addTest = false;
8844 }
8845 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008846 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008847 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8848 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8849 // It should be transformed during dag combiner except when the condition
8850 // is set by a arithmetics with overflow node.
8851 X86::CondCode CCode =
8852 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8853 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008854 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008855 Cond = Cond.getOperand(0).getOperand(1);
8856 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00008857 } else if (Cond.getOpcode() == ISD::SETCC &&
8858 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
8859 // For FCMP_OEQ, we can emit
8860 // two branches instead of an explicit AND instruction with a
8861 // separate test. However, we only do this if this block doesn't
8862 // have a fall-through edge, because this requires an explicit
8863 // jmp when the condition is false.
8864 if (Op.getNode()->hasOneUse()) {
8865 SDNode *User = *Op.getNode()->use_begin();
8866 // Look for an unconditional branch following this conditional branch.
8867 // We need this because we need to reverse the successors in order
8868 // to implement FCMP_OEQ.
8869 if (User->getOpcode() == ISD::BR) {
8870 SDValue FalseBB = User->getOperand(1);
8871 SDNode *NewBR =
8872 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8873 assert(NewBR == User);
8874 (void)NewBR;
8875 Dest = FalseBB;
8876
8877 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
8878 Cond.getOperand(0), Cond.getOperand(1));
8879 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8880 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8881 Chain, Dest, CC, Cmp);
8882 CC = DAG.getConstant(X86::COND_P, MVT::i8);
8883 Cond = Cmp;
8884 addTest = false;
8885 }
8886 }
8887 } else if (Cond.getOpcode() == ISD::SETCC &&
8888 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
8889 // For FCMP_UNE, we can emit
8890 // two branches instead of an explicit AND instruction with a
8891 // separate test. However, we only do this if this block doesn't
8892 // have a fall-through edge, because this requires an explicit
8893 // jmp when the condition is false.
8894 if (Op.getNode()->hasOneUse()) {
8895 SDNode *User = *Op.getNode()->use_begin();
8896 // Look for an unconditional branch following this conditional branch.
8897 // We need this because we need to reverse the successors in order
8898 // to implement FCMP_UNE.
8899 if (User->getOpcode() == ISD::BR) {
8900 SDValue FalseBB = User->getOperand(1);
8901 SDNode *NewBR =
8902 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8903 assert(NewBR == User);
8904 (void)NewBR;
8905
8906 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
8907 Cond.getOperand(0), Cond.getOperand(1));
8908 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8909 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8910 Chain, Dest, CC, Cmp);
8911 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
8912 Cond = Cmp;
8913 addTest = false;
8914 Dest = FalseBB;
8915 }
8916 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008917 }
Evan Cheng0488db92007-09-25 01:57:46 +00008918 }
8919
8920 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008921 // Look pass the truncate.
8922 if (Cond.getOpcode() == ISD::TRUNCATE)
8923 Cond = Cond.getOperand(0);
8924
8925 // We know the result of AND is compared against zero. Try to match
8926 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008927 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008928 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8929 if (NewSetCC.getNode()) {
8930 CC = NewSetCC.getOperand(0);
8931 Cond = NewSetCC.getOperand(1);
8932 addTest = false;
8933 }
8934 }
8935 }
8936
8937 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008938 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008939 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008940 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008941 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008942 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008943}
8944
Anton Korobeynikove060b532007-04-17 19:34:00 +00008945
8946// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8947// Calls to _alloca is needed to probe the stack when allocating more than 4k
8948// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8949// that the guard pages used by the OS virtual memory manager are allocated in
8950// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008951SDValue
8952X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008953 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008954 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008955 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008956 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008957 "are being used");
8958 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008959 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008960
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008961 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008962 SDValue Chain = Op.getOperand(0);
8963 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008964 // FIXME: Ensure alignment here
8965
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008966 bool Is64Bit = Subtarget->is64Bit();
8967 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008968
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008969 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008970 MachineFunction &MF = DAG.getMachineFunction();
8971 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008972
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008973 if (Is64Bit) {
8974 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008975 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008976 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008977
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008978 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8979 I != E; I++)
8980 if (I->hasNestAttr())
8981 report_fatal_error("Cannot use segmented stacks with functions that "
8982 "have nested arguments.");
8983 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008984
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008985 const TargetRegisterClass *AddrRegClass =
8986 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8987 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8988 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8989 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8990 DAG.getRegister(Vreg, SPTy));
8991 SDValue Ops1[2] = { Value, Chain };
8992 return DAG.getMergeValues(Ops1, 2, dl);
8993 } else {
8994 SDValue Flag;
8995 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008996
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008997 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8998 Flag = Chain.getValue(1);
8999 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009000
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009001 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9002 Flag = Chain.getValue(1);
9003
9004 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9005
9006 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9007 return DAG.getMergeValues(Ops1, 2, dl);
9008 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009009}
9010
Dan Gohmand858e902010-04-17 15:26:15 +00009011SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009012 MachineFunction &MF = DAG.getMachineFunction();
9013 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9014
Dan Gohman69de1932008-02-06 22:27:42 +00009015 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009016 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009017
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009018 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009019 // vastart just stores the address of the VarArgsFrameIndex slot into the
9020 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009021 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9022 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009023 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9024 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009025 }
9026
9027 // __va_list_tag:
9028 // gp_offset (0 - 6 * 8)
9029 // fp_offset (48 - 48 + 8 * 16)
9030 // overflow_arg_area (point to parameters coming in memory).
9031 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009032 SmallVector<SDValue, 8> MemOps;
9033 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009034 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009035 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009036 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9037 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009038 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009039 MemOps.push_back(Store);
9040
9041 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009042 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009043 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009044 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009045 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9046 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009047 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009048 MemOps.push_back(Store);
9049
9050 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009051 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009052 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009053 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9054 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009055 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9056 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009057 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009058 MemOps.push_back(Store);
9059
9060 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009061 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009062 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009063 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9064 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009065 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9066 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009067 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009068 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009069 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009070}
9071
Dan Gohmand858e902010-04-17 15:26:15 +00009072SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009073 assert(Subtarget->is64Bit() &&
9074 "LowerVAARG only handles 64-bit va_arg!");
9075 assert((Subtarget->isTargetLinux() ||
9076 Subtarget->isTargetDarwin()) &&
9077 "Unhandled target in LowerVAARG");
9078 assert(Op.getNode()->getNumOperands() == 4);
9079 SDValue Chain = Op.getOperand(0);
9080 SDValue SrcPtr = Op.getOperand(1);
9081 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9082 unsigned Align = Op.getConstantOperandVal(3);
9083 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009084
Dan Gohman320afb82010-10-12 18:00:49 +00009085 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009086 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009087 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9088 uint8_t ArgMode;
9089
9090 // Decide which area this value should be read from.
9091 // TODO: Implement the AMD64 ABI in its entirety. This simple
9092 // selection mechanism works only for the basic types.
9093 if (ArgVT == MVT::f80) {
9094 llvm_unreachable("va_arg for f80 not yet implemented");
9095 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9096 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9097 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9098 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9099 } else {
9100 llvm_unreachable("Unhandled argument type in LowerVAARG");
9101 }
9102
9103 if (ArgMode == 2) {
9104 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009105 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009106 !(DAG.getMachineFunction()
9107 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009108 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009109 }
9110
9111 // Insert VAARG_64 node into the DAG
9112 // VAARG_64 returns two values: Variable Argument Address, Chain
9113 SmallVector<SDValue, 11> InstOps;
9114 InstOps.push_back(Chain);
9115 InstOps.push_back(SrcPtr);
9116 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9117 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9118 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9119 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9120 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9121 VTs, &InstOps[0], InstOps.size(),
9122 MVT::i64,
9123 MachinePointerInfo(SV),
9124 /*Align=*/0,
9125 /*Volatile=*/false,
9126 /*ReadMem=*/true,
9127 /*WriteMem=*/true);
9128 Chain = VAARG.getValue(1);
9129
9130 // Load the next argument and return it
9131 return DAG.getLoad(ArgVT, dl,
9132 Chain,
9133 VAARG,
9134 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009135 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009136}
9137
Dan Gohmand858e902010-04-17 15:26:15 +00009138SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009139 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009140 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009141 SDValue Chain = Op.getOperand(0);
9142 SDValue DstPtr = Op.getOperand(1);
9143 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009144 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9145 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009146 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009147
Chris Lattnere72f2022010-09-21 05:40:29 +00009148 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009149 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009150 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009151 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009152}
9153
Dan Gohman475871a2008-07-27 21:46:04 +00009154SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009155X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009156 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009157 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009158 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009159 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009160 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009161 case Intrinsic::x86_sse_comieq_ss:
9162 case Intrinsic::x86_sse_comilt_ss:
9163 case Intrinsic::x86_sse_comile_ss:
9164 case Intrinsic::x86_sse_comigt_ss:
9165 case Intrinsic::x86_sse_comige_ss:
9166 case Intrinsic::x86_sse_comineq_ss:
9167 case Intrinsic::x86_sse_ucomieq_ss:
9168 case Intrinsic::x86_sse_ucomilt_ss:
9169 case Intrinsic::x86_sse_ucomile_ss:
9170 case Intrinsic::x86_sse_ucomigt_ss:
9171 case Intrinsic::x86_sse_ucomige_ss:
9172 case Intrinsic::x86_sse_ucomineq_ss:
9173 case Intrinsic::x86_sse2_comieq_sd:
9174 case Intrinsic::x86_sse2_comilt_sd:
9175 case Intrinsic::x86_sse2_comile_sd:
9176 case Intrinsic::x86_sse2_comigt_sd:
9177 case Intrinsic::x86_sse2_comige_sd:
9178 case Intrinsic::x86_sse2_comineq_sd:
9179 case Intrinsic::x86_sse2_ucomieq_sd:
9180 case Intrinsic::x86_sse2_ucomilt_sd:
9181 case Intrinsic::x86_sse2_ucomile_sd:
9182 case Intrinsic::x86_sse2_ucomigt_sd:
9183 case Intrinsic::x86_sse2_ucomige_sd:
9184 case Intrinsic::x86_sse2_ucomineq_sd: {
9185 unsigned Opc = 0;
9186 ISD::CondCode CC = ISD::SETCC_INVALID;
9187 switch (IntNo) {
9188 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009189 case Intrinsic::x86_sse_comieq_ss:
9190 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009191 Opc = X86ISD::COMI;
9192 CC = ISD::SETEQ;
9193 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009194 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009195 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009196 Opc = X86ISD::COMI;
9197 CC = ISD::SETLT;
9198 break;
9199 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009200 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009201 Opc = X86ISD::COMI;
9202 CC = ISD::SETLE;
9203 break;
9204 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009205 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009206 Opc = X86ISD::COMI;
9207 CC = ISD::SETGT;
9208 break;
9209 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009210 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009211 Opc = X86ISD::COMI;
9212 CC = ISD::SETGE;
9213 break;
9214 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009215 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009216 Opc = X86ISD::COMI;
9217 CC = ISD::SETNE;
9218 break;
9219 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009220 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009221 Opc = X86ISD::UCOMI;
9222 CC = ISD::SETEQ;
9223 break;
9224 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009225 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009226 Opc = X86ISD::UCOMI;
9227 CC = ISD::SETLT;
9228 break;
9229 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009230 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009231 Opc = X86ISD::UCOMI;
9232 CC = ISD::SETLE;
9233 break;
9234 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009235 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009236 Opc = X86ISD::UCOMI;
9237 CC = ISD::SETGT;
9238 break;
9239 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009240 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009241 Opc = X86ISD::UCOMI;
9242 CC = ISD::SETGE;
9243 break;
9244 case Intrinsic::x86_sse_ucomineq_ss:
9245 case Intrinsic::x86_sse2_ucomineq_sd:
9246 Opc = X86ISD::UCOMI;
9247 CC = ISD::SETNE;
9248 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009249 }
Evan Cheng734503b2006-09-11 02:19:56 +00009250
Dan Gohman475871a2008-07-27 21:46:04 +00009251 SDValue LHS = Op.getOperand(1);
9252 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009253 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009254 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009255 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9256 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9257 DAG.getConstant(X86CC, MVT::i8), Cond);
9258 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009259 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009260 // Arithmetic intrinsics.
9261 case Intrinsic::x86_sse3_hadd_ps:
9262 case Intrinsic::x86_sse3_hadd_pd:
9263 case Intrinsic::x86_avx_hadd_ps_256:
9264 case Intrinsic::x86_avx_hadd_pd_256:
9265 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9266 Op.getOperand(1), Op.getOperand(2));
9267 case Intrinsic::x86_sse3_hsub_ps:
9268 case Intrinsic::x86_sse3_hsub_pd:
9269 case Intrinsic::x86_avx_hsub_ps_256:
9270 case Intrinsic::x86_avx_hsub_pd_256:
9271 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9272 Op.getOperand(1), Op.getOperand(2));
Craig Topper98fc7292011-11-19 17:46:46 +00009273 case Intrinsic::x86_avx2_psllv_d:
9274 case Intrinsic::x86_avx2_psllv_q:
9275 case Intrinsic::x86_avx2_psllv_d_256:
9276 case Intrinsic::x86_avx2_psllv_q_256:
9277 return DAG.getNode(ISD::SHL, dl, Op.getValueType(),
9278 Op.getOperand(1), Op.getOperand(2));
9279 case Intrinsic::x86_avx2_psrlv_d:
9280 case Intrinsic::x86_avx2_psrlv_q:
9281 case Intrinsic::x86_avx2_psrlv_d_256:
9282 case Intrinsic::x86_avx2_psrlv_q_256:
9283 return DAG.getNode(ISD::SRL, dl, Op.getValueType(),
9284 Op.getOperand(1), Op.getOperand(2));
9285 case Intrinsic::x86_avx2_psrav_d:
9286 case Intrinsic::x86_avx2_psrav_d_256:
9287 return DAG.getNode(ISD::SRA, dl, Op.getValueType(),
9288 Op.getOperand(1), Op.getOperand(2));
9289
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009290 // ptest and testp intrinsics. The intrinsic these come from are designed to
9291 // return an integer value, not just an instruction so lower it to the ptest
9292 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009293 case Intrinsic::x86_sse41_ptestz:
9294 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009295 case Intrinsic::x86_sse41_ptestnzc:
9296 case Intrinsic::x86_avx_ptestz_256:
9297 case Intrinsic::x86_avx_ptestc_256:
9298 case Intrinsic::x86_avx_ptestnzc_256:
9299 case Intrinsic::x86_avx_vtestz_ps:
9300 case Intrinsic::x86_avx_vtestc_ps:
9301 case Intrinsic::x86_avx_vtestnzc_ps:
9302 case Intrinsic::x86_avx_vtestz_pd:
9303 case Intrinsic::x86_avx_vtestc_pd:
9304 case Intrinsic::x86_avx_vtestnzc_pd:
9305 case Intrinsic::x86_avx_vtestz_ps_256:
9306 case Intrinsic::x86_avx_vtestc_ps_256:
9307 case Intrinsic::x86_avx_vtestnzc_ps_256:
9308 case Intrinsic::x86_avx_vtestz_pd_256:
9309 case Intrinsic::x86_avx_vtestc_pd_256:
9310 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9311 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009312 unsigned X86CC = 0;
9313 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009314 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009315 case Intrinsic::x86_avx_vtestz_ps:
9316 case Intrinsic::x86_avx_vtestz_pd:
9317 case Intrinsic::x86_avx_vtestz_ps_256:
9318 case Intrinsic::x86_avx_vtestz_pd_256:
9319 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009320 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009321 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009322 // ZF = 1
9323 X86CC = X86::COND_E;
9324 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009325 case Intrinsic::x86_avx_vtestc_ps:
9326 case Intrinsic::x86_avx_vtestc_pd:
9327 case Intrinsic::x86_avx_vtestc_ps_256:
9328 case Intrinsic::x86_avx_vtestc_pd_256:
9329 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009330 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009331 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009332 // CF = 1
9333 X86CC = X86::COND_B;
9334 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009335 case Intrinsic::x86_avx_vtestnzc_ps:
9336 case Intrinsic::x86_avx_vtestnzc_pd:
9337 case Intrinsic::x86_avx_vtestnzc_ps_256:
9338 case Intrinsic::x86_avx_vtestnzc_pd_256:
9339 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009340 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009341 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009342 // ZF and CF = 0
9343 X86CC = X86::COND_A;
9344 break;
9345 }
Eric Christopherfd179292009-08-27 18:07:15 +00009346
Eric Christopher71c67532009-07-29 00:28:05 +00009347 SDValue LHS = Op.getOperand(1);
9348 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009349 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9350 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009351 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9352 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9353 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009354 }
Evan Cheng5759f972008-05-04 09:15:50 +00009355
9356 // Fix vector shift instructions where the last operand is a non-immediate
9357 // i32 value.
Craig Topper7be5dfd2011-11-12 09:58:49 +00009358 case Intrinsic::x86_avx2_pslli_w:
9359 case Intrinsic::x86_avx2_pslli_d:
9360 case Intrinsic::x86_avx2_pslli_q:
9361 case Intrinsic::x86_avx2_psrli_w:
9362 case Intrinsic::x86_avx2_psrli_d:
9363 case Intrinsic::x86_avx2_psrli_q:
9364 case Intrinsic::x86_avx2_psrai_w:
9365 case Intrinsic::x86_avx2_psrai_d:
Evan Cheng5759f972008-05-04 09:15:50 +00009366 case Intrinsic::x86_sse2_pslli_w:
9367 case Intrinsic::x86_sse2_pslli_d:
9368 case Intrinsic::x86_sse2_pslli_q:
9369 case Intrinsic::x86_sse2_psrli_w:
9370 case Intrinsic::x86_sse2_psrli_d:
9371 case Intrinsic::x86_sse2_psrli_q:
9372 case Intrinsic::x86_sse2_psrai_w:
9373 case Intrinsic::x86_sse2_psrai_d:
9374 case Intrinsic::x86_mmx_pslli_w:
9375 case Intrinsic::x86_mmx_pslli_d:
9376 case Intrinsic::x86_mmx_pslli_q:
9377 case Intrinsic::x86_mmx_psrli_w:
9378 case Intrinsic::x86_mmx_psrli_d:
9379 case Intrinsic::x86_mmx_psrli_q:
9380 case Intrinsic::x86_mmx_psrai_w:
9381 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009382 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009383 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009384 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009385
9386 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009387 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009388 switch (IntNo) {
9389 case Intrinsic::x86_sse2_pslli_w:
9390 NewIntNo = Intrinsic::x86_sse2_psll_w;
9391 break;
9392 case Intrinsic::x86_sse2_pslli_d:
9393 NewIntNo = Intrinsic::x86_sse2_psll_d;
9394 break;
9395 case Intrinsic::x86_sse2_pslli_q:
9396 NewIntNo = Intrinsic::x86_sse2_psll_q;
9397 break;
9398 case Intrinsic::x86_sse2_psrli_w:
9399 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9400 break;
9401 case Intrinsic::x86_sse2_psrli_d:
9402 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9403 break;
9404 case Intrinsic::x86_sse2_psrli_q:
9405 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9406 break;
9407 case Intrinsic::x86_sse2_psrai_w:
9408 NewIntNo = Intrinsic::x86_sse2_psra_w;
9409 break;
9410 case Intrinsic::x86_sse2_psrai_d:
9411 NewIntNo = Intrinsic::x86_sse2_psra_d;
9412 break;
Craig Topper7be5dfd2011-11-12 09:58:49 +00009413 case Intrinsic::x86_avx2_pslli_w:
9414 NewIntNo = Intrinsic::x86_avx2_psll_w;
9415 break;
9416 case Intrinsic::x86_avx2_pslli_d:
9417 NewIntNo = Intrinsic::x86_avx2_psll_d;
9418 break;
9419 case Intrinsic::x86_avx2_pslli_q:
9420 NewIntNo = Intrinsic::x86_avx2_psll_q;
9421 break;
9422 case Intrinsic::x86_avx2_psrli_w:
9423 NewIntNo = Intrinsic::x86_avx2_psrl_w;
9424 break;
9425 case Intrinsic::x86_avx2_psrli_d:
9426 NewIntNo = Intrinsic::x86_avx2_psrl_d;
9427 break;
9428 case Intrinsic::x86_avx2_psrli_q:
9429 NewIntNo = Intrinsic::x86_avx2_psrl_q;
9430 break;
9431 case Intrinsic::x86_avx2_psrai_w:
9432 NewIntNo = Intrinsic::x86_avx2_psra_w;
9433 break;
9434 case Intrinsic::x86_avx2_psrai_d:
9435 NewIntNo = Intrinsic::x86_avx2_psra_d;
9436 break;
Evan Cheng5759f972008-05-04 09:15:50 +00009437 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009438 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009439 switch (IntNo) {
9440 case Intrinsic::x86_mmx_pslli_w:
9441 NewIntNo = Intrinsic::x86_mmx_psll_w;
9442 break;
9443 case Intrinsic::x86_mmx_pslli_d:
9444 NewIntNo = Intrinsic::x86_mmx_psll_d;
9445 break;
9446 case Intrinsic::x86_mmx_pslli_q:
9447 NewIntNo = Intrinsic::x86_mmx_psll_q;
9448 break;
9449 case Intrinsic::x86_mmx_psrli_w:
9450 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9451 break;
9452 case Intrinsic::x86_mmx_psrli_d:
9453 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9454 break;
9455 case Intrinsic::x86_mmx_psrli_q:
9456 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9457 break;
9458 case Intrinsic::x86_mmx_psrai_w:
9459 NewIntNo = Intrinsic::x86_mmx_psra_w;
9460 break;
9461 case Intrinsic::x86_mmx_psrai_d:
9462 NewIntNo = Intrinsic::x86_mmx_psra_d;
9463 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009464 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009465 }
9466 break;
9467 }
9468 }
Mon P Wangefa42202009-09-03 19:56:25 +00009469
9470 // The vector shift intrinsics with scalars uses 32b shift amounts but
9471 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9472 // to be zero.
9473 SDValue ShOps[4];
9474 ShOps[0] = ShAmt;
9475 ShOps[1] = DAG.getConstant(0, MVT::i32);
9476 if (ShAmtVT == MVT::v4i32) {
9477 ShOps[2] = DAG.getUNDEF(MVT::i32);
9478 ShOps[3] = DAG.getUNDEF(MVT::i32);
9479 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9480 } else {
9481 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009482// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009483 }
9484
Owen Andersone50ed302009-08-10 22:56:29 +00009485 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009486 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009487 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009488 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009489 Op.getOperand(1), ShAmt);
9490 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009491 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009492}
Evan Cheng72261582005-12-20 06:22:03 +00009493
Dan Gohmand858e902010-04-17 15:26:15 +00009494SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9495 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009496 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9497 MFI->setReturnAddressIsTaken(true);
9498
Bill Wendling64e87322009-01-16 19:25:27 +00009499 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009500 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009501
9502 if (Depth > 0) {
9503 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9504 SDValue Offset =
9505 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009506 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009507 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009508 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009509 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009510 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009511 }
9512
9513 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009514 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009515 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009516 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009517}
9518
Dan Gohmand858e902010-04-17 15:26:15 +00009519SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009520 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9521 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009522
Owen Andersone50ed302009-08-10 22:56:29 +00009523 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009524 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009525 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9526 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009527 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009528 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009529 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9530 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009531 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009532 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009533}
9534
Dan Gohman475871a2008-07-27 21:46:04 +00009535SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009536 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009537 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009538}
9539
Dan Gohmand858e902010-04-17 15:26:15 +00009540SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009541 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009542 SDValue Chain = Op.getOperand(0);
9543 SDValue Offset = Op.getOperand(1);
9544 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009545 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009546
Dan Gohmand8816272010-08-11 18:14:00 +00009547 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9548 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9549 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009550 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009551
Dan Gohmand8816272010-08-11 18:14:00 +00009552 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9553 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009554 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009555 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9556 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009557 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009558 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009559
Dale Johannesene4d209d2009-02-03 20:21:25 +00009560 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009561 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009562 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009563}
9564
Duncan Sands4a544a72011-09-06 13:37:06 +00009565SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9566 SelectionDAG &DAG) const {
9567 return Op.getOperand(0);
9568}
9569
9570SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9571 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009572 SDValue Root = Op.getOperand(0);
9573 SDValue Trmp = Op.getOperand(1); // trampoline
9574 SDValue FPtr = Op.getOperand(2); // nested function
9575 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009576 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009577
Dan Gohman69de1932008-02-06 22:27:42 +00009578 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009579
9580 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009581 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009582
9583 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009584 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9585 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009586
Evan Cheng0e6a0522011-07-18 20:57:22 +00009587 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9588 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009589
9590 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9591
9592 // Load the pointer to the nested function into R11.
9593 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009594 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009595 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009596 Addr, MachinePointerInfo(TrmpAddr),
9597 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009598
Owen Anderson825b72b2009-08-11 20:47:22 +00009599 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9600 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009601 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9602 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009603 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009604
9605 // Load the 'nest' parameter value into R10.
9606 // R10 is specified in X86CallingConv.td
9607 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009608 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9609 DAG.getConstant(10, MVT::i64));
9610 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009611 Addr, MachinePointerInfo(TrmpAddr, 10),
9612 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009613
Owen Anderson825b72b2009-08-11 20:47:22 +00009614 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9615 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009616 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9617 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009618 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009619
9620 // Jump to the nested function.
9621 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009622 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9623 DAG.getConstant(20, MVT::i64));
9624 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009625 Addr, MachinePointerInfo(TrmpAddr, 20),
9626 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009627
9628 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009629 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9630 DAG.getConstant(22, MVT::i64));
9631 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009632 MachinePointerInfo(TrmpAddr, 22),
9633 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009634
Duncan Sands4a544a72011-09-06 13:37:06 +00009635 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009636 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009637 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009638 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009639 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009640 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009641
9642 switch (CC) {
9643 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009644 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009645 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009646 case CallingConv::X86_StdCall: {
9647 // Pass 'nest' parameter in ECX.
9648 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009649 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009650
9651 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009652 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009653 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009654
Chris Lattner58d74912008-03-12 17:45:29 +00009655 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009656 unsigned InRegCount = 0;
9657 unsigned Idx = 1;
9658
9659 for (FunctionType::param_iterator I = FTy->param_begin(),
9660 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009661 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009662 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009663 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009664
9665 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009666 report_fatal_error("Nest register in use - reduce number of inreg"
9667 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009668 }
9669 }
9670 break;
9671 }
9672 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009673 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009674 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009675 // Pass 'nest' parameter in EAX.
9676 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009677 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009678 break;
9679 }
9680
Dan Gohman475871a2008-07-27 21:46:04 +00009681 SDValue OutChains[4];
9682 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009683
Owen Anderson825b72b2009-08-11 20:47:22 +00009684 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9685 DAG.getConstant(10, MVT::i32));
9686 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009687
Chris Lattnera62fe662010-02-05 19:20:30 +00009688 // This is storing the opcode for MOV32ri.
9689 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009690 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009691 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009692 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009693 Trmp, MachinePointerInfo(TrmpAddr),
9694 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009695
Owen Anderson825b72b2009-08-11 20:47:22 +00009696 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9697 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009698 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9699 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009700 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009701
Chris Lattnera62fe662010-02-05 19:20:30 +00009702 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009703 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9704 DAG.getConstant(5, MVT::i32));
9705 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009706 MachinePointerInfo(TrmpAddr, 5),
9707 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009708
Owen Anderson825b72b2009-08-11 20:47:22 +00009709 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9710 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009711 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9712 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009713 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009714
Duncan Sands4a544a72011-09-06 13:37:06 +00009715 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009716 }
9717}
9718
Dan Gohmand858e902010-04-17 15:26:15 +00009719SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9720 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009721 /*
9722 The rounding mode is in bits 11:10 of FPSR, and has the following
9723 settings:
9724 00 Round to nearest
9725 01 Round to -inf
9726 10 Round to +inf
9727 11 Round to 0
9728
9729 FLT_ROUNDS, on the other hand, expects the following:
9730 -1 Undefined
9731 0 Round to 0
9732 1 Round to nearest
9733 2 Round to +inf
9734 3 Round to -inf
9735
9736 To perform the conversion, we do:
9737 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9738 */
9739
9740 MachineFunction &MF = DAG.getMachineFunction();
9741 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009742 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009743 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009744 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009745 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009746
9747 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009748 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009749 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009750
Michael J. Spencerec38de22010-10-10 22:04:20 +00009751
Chris Lattner2156b792010-09-22 01:11:26 +00009752 MachineMemOperand *MMO =
9753 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9754 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009755
Chris Lattner2156b792010-09-22 01:11:26 +00009756 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9757 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9758 DAG.getVTList(MVT::Other),
9759 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009760
9761 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009762 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009763 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009764
9765 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009766 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009767 DAG.getNode(ISD::SRL, DL, MVT::i16,
9768 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009769 CWD, DAG.getConstant(0x800, MVT::i16)),
9770 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009771 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009772 DAG.getNode(ISD::SRL, DL, MVT::i16,
9773 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009774 CWD, DAG.getConstant(0x400, MVT::i16)),
9775 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009776
Dan Gohman475871a2008-07-27 21:46:04 +00009777 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009778 DAG.getNode(ISD::AND, DL, MVT::i16,
9779 DAG.getNode(ISD::ADD, DL, MVT::i16,
9780 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009781 DAG.getConstant(1, MVT::i16)),
9782 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009783
9784
Duncan Sands83ec4b62008-06-06 12:08:01 +00009785 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009786 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009787}
9788
Dan Gohmand858e902010-04-17 15:26:15 +00009789SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009790 EVT VT = Op.getValueType();
9791 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009792 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009793 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009794
9795 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009796 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009797 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009798 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009799 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009800 }
Evan Cheng18efe262007-12-14 02:13:44 +00009801
Evan Cheng152804e2007-12-14 08:30:15 +00009802 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009803 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009804 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009805
9806 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009807 SDValue Ops[] = {
9808 Op,
9809 DAG.getConstant(NumBits+NumBits-1, OpVT),
9810 DAG.getConstant(X86::COND_E, MVT::i8),
9811 Op.getValue(1)
9812 };
9813 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009814
9815 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009816 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009817
Owen Anderson825b72b2009-08-11 20:47:22 +00009818 if (VT == MVT::i8)
9819 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009820 return Op;
9821}
9822
Dan Gohmand858e902010-04-17 15:26:15 +00009823SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009824 EVT VT = Op.getValueType();
9825 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009826 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009827 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009828
9829 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009830 if (VT == MVT::i8) {
9831 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009832 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009833 }
Evan Cheng152804e2007-12-14 08:30:15 +00009834
9835 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009836 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009837 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009838
9839 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009840 SDValue Ops[] = {
9841 Op,
9842 DAG.getConstant(NumBits, OpVT),
9843 DAG.getConstant(X86::COND_E, MVT::i8),
9844 Op.getValue(1)
9845 };
9846 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009847
Owen Anderson825b72b2009-08-11 20:47:22 +00009848 if (VT == MVT::i8)
9849 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009850 return Op;
9851}
9852
Craig Topper13894fa2011-08-24 06:14:18 +00009853// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9854// ones, and then concatenate the result back.
9855static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009856 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009857
9858 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9859 "Unsupported value type for operation");
9860
9861 int NumElems = VT.getVectorNumElements();
9862 DebugLoc dl = Op.getDebugLoc();
9863 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9864 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9865
9866 // Extract the LHS vectors
9867 SDValue LHS = Op.getOperand(0);
9868 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9869 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9870
9871 // Extract the RHS vectors
9872 SDValue RHS = Op.getOperand(1);
9873 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9874 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9875
9876 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9877 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9878
9879 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9880 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9881 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9882}
9883
9884SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9885 assert(Op.getValueType().getSizeInBits() == 256 &&
9886 Op.getValueType().isInteger() &&
9887 "Only handle AVX 256-bit vector integer operation");
9888 return Lower256IntArith(Op, DAG);
9889}
9890
9891SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9892 assert(Op.getValueType().getSizeInBits() == 256 &&
9893 Op.getValueType().isInteger() &&
9894 "Only handle AVX 256-bit vector integer operation");
9895 return Lower256IntArith(Op, DAG);
9896}
9897
9898SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9899 EVT VT = Op.getValueType();
9900
9901 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +00009902 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +00009903 return Lower256IntArith(Op, DAG);
9904
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009905 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009906
Craig Topperaaa643c2011-11-09 07:28:55 +00009907 SDValue A = Op.getOperand(0);
9908 SDValue B = Op.getOperand(1);
9909
9910 if (VT == MVT::v4i64) {
9911 assert(Subtarget->hasAVX2() && "Lowering v4i64 multiply requires AVX2");
9912
9913 // ulong2 Ahi = __builtin_ia32_psrlqi256( a, 32);
9914 // ulong2 Bhi = __builtin_ia32_psrlqi256( b, 32);
9915 // ulong2 AloBlo = __builtin_ia32_pmuludq256( a, b );
9916 // ulong2 AloBhi = __builtin_ia32_pmuludq256( a, Bhi );
9917 // ulong2 AhiBlo = __builtin_ia32_pmuludq256( Ahi, b );
9918 //
9919 // AloBhi = __builtin_ia32_psllqi256( AloBhi, 32 );
9920 // AhiBlo = __builtin_ia32_psllqi256( AhiBlo, 32 );
9921 // return AloBlo + AloBhi + AhiBlo;
9922
9923 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9924 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
9925 A, DAG.getConstant(32, MVT::i32));
9926 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9927 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
9928 B, DAG.getConstant(32, MVT::i32));
9929 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9930 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9931 A, B);
9932 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9933 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9934 A, Bhi);
9935 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9936 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9937 Ahi, B);
9938 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9939 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
9940 AloBhi, DAG.getConstant(32, MVT::i32));
9941 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9942 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
9943 AhiBlo, DAG.getConstant(32, MVT::i32));
9944 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9945 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
9946 return Res;
9947 }
9948
9949 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
9950
Mon P Wangaf9b9522008-12-18 21:42:19 +00009951 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9952 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9953 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9954 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9955 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9956 //
9957 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9958 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9959 // return AloBlo + AloBhi + AhiBlo;
9960
Dale Johannesene4d209d2009-02-03 20:21:25 +00009961 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009962 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9963 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009964 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009965 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9966 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009967 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009968 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009969 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009970 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009971 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009972 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009973 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009974 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009975 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009976 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009977 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9978 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009979 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009980 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9981 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009982 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9983 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009984 return Res;
9985}
9986
Nadav Rotem43012222011-05-11 08:12:09 +00009987SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
9988
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009989 EVT VT = Op.getValueType();
9990 DebugLoc dl = Op.getDebugLoc();
9991 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009992 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009993 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009994
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00009995 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009996 return SDValue();
9997
Nadav Rotem43012222011-05-11 08:12:09 +00009998 // Optimize shl/srl/sra with constant shift amount.
9999 if (isSplatVector(Amt.getNode())) {
10000 SDValue SclrAmt = Amt->getOperand(0);
10001 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10002 uint64_t ShiftAmt = C->getZExtValue();
10003
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010004 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SHL) {
10005 // Make a large shift.
10006 SDValue SHL =
10007 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10008 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10009 R, DAG.getConstant(ShiftAmt, MVT::i32));
10010 // Zero out the rightmost bits.
10011 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10012 MVT::i8));
10013 return DAG.getNode(ISD::AND, dl, VT, SHL,
10014 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10015 }
10016
Nadav Rotem43012222011-05-11 08:12:09 +000010017 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
10018 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10019 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10020 R, DAG.getConstant(ShiftAmt, MVT::i32));
10021
10022 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
10023 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10024 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10025 R, DAG.getConstant(ShiftAmt, MVT::i32));
10026
10027 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
10028 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10029 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10030 R, DAG.getConstant(ShiftAmt, MVT::i32));
10031
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010032 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRL) {
10033 // Make a large shift.
10034 SDValue SRL =
10035 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10036 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10037 R, DAG.getConstant(ShiftAmt, MVT::i32));
10038 // Zero out the leftmost bits.
10039 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10040 MVT::i8));
10041 return DAG.getNode(ISD::AND, dl, VT, SRL,
10042 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10043 }
10044
Nadav Rotem43012222011-05-11 08:12:09 +000010045 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
10046 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10047 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10048 R, DAG.getConstant(ShiftAmt, MVT::i32));
10049
10050 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
10051 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10052 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
10053 R, DAG.getConstant(ShiftAmt, MVT::i32));
10054
10055 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
10056 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10057 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10058 R, DAG.getConstant(ShiftAmt, MVT::i32));
10059
10060 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
10061 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10062 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
10063 R, DAG.getConstant(ShiftAmt, MVT::i32));
10064
10065 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
10066 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10067 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
10068 R, DAG.getConstant(ShiftAmt, MVT::i32));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010069
10070 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRA) {
10071 if (ShiftAmt == 7) {
10072 // R s>> 7 === R s< 0
10073 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10074 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10075 }
10076
10077 // R s>> a === ((R u>> a) ^ m) - m
10078 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10079 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10080 MVT::i8));
10081 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10082 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10083 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10084 return Res;
10085 }
Craig Topper46154eb2011-11-11 07:39:23 +000010086
Craig Topper0d86d462011-11-20 00:12:05 +000010087 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10088 if (Op.getOpcode() == ISD::SHL) {
10089 // Make a large shift.
10090 SDValue SHL =
10091 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10092 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
10093 R, DAG.getConstant(ShiftAmt, MVT::i32));
10094 // Zero out the rightmost bits.
10095 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10096 MVT::i8));
10097 return DAG.getNode(ISD::AND, dl, VT, SHL,
10098 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010099 }
Craig Topper0d86d462011-11-20 00:12:05 +000010100 if (Op.getOpcode() == ISD::SRL) {
10101 // Make a large shift.
10102 SDValue SRL =
10103 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10104 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
10105 R, DAG.getConstant(ShiftAmt, MVT::i32));
10106 // Zero out the leftmost bits.
10107 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10108 MVT::i8));
10109 return DAG.getNode(ISD::AND, dl, VT, SRL,
10110 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10111 }
10112 if (Op.getOpcode() == ISD::SRA) {
10113 if (ShiftAmt == 7) {
10114 // R s>> 7 === R s< 0
10115 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10116 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10117 }
10118
10119 // R s>> a === ((R u>> a) ^ m) - m
10120 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10121 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10122 MVT::i8));
10123 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10124 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10125 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10126 return Res;
10127 }
10128 }
Nadav Rotem43012222011-05-11 08:12:09 +000010129 }
10130 }
10131
10132 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010133 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010134 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10135 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10136 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
10137
10138 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010139
Nate Begeman51409212010-07-28 00:21:48 +000010140 std::vector<Constant*> CV(4, CI);
10141 Constant *C = ConstantVector::get(CV);
10142 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10143 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010144 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010145 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010146
10147 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010148 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010149 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10150 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10151 }
Nadav Rotem43012222011-05-11 08:12:09 +000010152 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010153 // a = a << 5;
10154 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10155 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10156 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
10157
10158 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
10159 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
10160
10161 std::vector<Constant*> CVM1(16, CM1);
10162 std::vector<Constant*> CVM2(16, CM2);
10163 Constant *C = ConstantVector::get(CVM1);
10164 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10165 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010166 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010167 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010168
10169 // r = pblendv(r, psllw(r & (char16)15, 4), a);
10170 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10171 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10172 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10173 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010174 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010175 // a += a
10176 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010177
Nate Begeman51409212010-07-28 00:21:48 +000010178 C = ConstantVector::get(CVM2);
10179 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10180 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010181 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010182 false, false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010183
Nate Begeman51409212010-07-28 00:21:48 +000010184 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10185 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10186 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10187 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10188 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010189 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010190 // a += a
10191 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010192
Nate Begeman51409212010-07-28 00:21:48 +000010193 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010194 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10195 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010196 return R;
10197 }
Craig Topper46154eb2011-11-11 07:39:23 +000010198
10199 // Decompose 256-bit shifts into smaller 128-bit shifts.
10200 if (VT.getSizeInBits() == 256) {
10201 int NumElems = VT.getVectorNumElements();
10202 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10203 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10204
10205 // Extract the two vectors
10206 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10207 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10208 DAG, dl);
10209
10210 // Recreate the shift amount vectors
10211 SDValue Amt1, Amt2;
10212 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10213 // Constant shift amount
10214 SmallVector<SDValue, 4> Amt1Csts;
10215 SmallVector<SDValue, 4> Amt2Csts;
10216 for (int i = 0; i < NumElems/2; ++i)
10217 Amt1Csts.push_back(Amt->getOperand(i));
10218 for (int i = NumElems/2; i < NumElems; ++i)
10219 Amt2Csts.push_back(Amt->getOperand(i));
10220
10221 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10222 &Amt1Csts[0], NumElems/2);
10223 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10224 &Amt2Csts[0], NumElems/2);
10225 } else {
10226 // Variable shift amount
10227 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10228 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10229 DAG, dl);
10230 }
10231
10232 // Issue new vector shifts for the smaller types
10233 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10234 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10235
10236 // Concatenate the result back
10237 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10238 }
10239
Nate Begeman51409212010-07-28 00:21:48 +000010240 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010241}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010242
Dan Gohmand858e902010-04-17 15:26:15 +000010243SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010244 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10245 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010246 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10247 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010248 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010249 SDValue LHS = N->getOperand(0);
10250 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010251 unsigned BaseOp = 0;
10252 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010253 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010254 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010255 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010256 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010257 // A subtract of one will be selected as a INC. Note that INC doesn't
10258 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010259 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10260 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010261 BaseOp = X86ISD::INC;
10262 Cond = X86::COND_O;
10263 break;
10264 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010265 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010266 Cond = X86::COND_O;
10267 break;
10268 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010269 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010270 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010271 break;
10272 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010273 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10274 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010275 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10276 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010277 BaseOp = X86ISD::DEC;
10278 Cond = X86::COND_O;
10279 break;
10280 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010281 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010282 Cond = X86::COND_O;
10283 break;
10284 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010285 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010286 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010287 break;
10288 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010289 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010290 Cond = X86::COND_O;
10291 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010292 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10293 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10294 MVT::i32);
10295 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010296
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010297 SDValue SetCC =
10298 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10299 DAG.getConstant(X86::COND_O, MVT::i32),
10300 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010301
Dan Gohman6e5fda22011-07-22 18:45:15 +000010302 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010303 }
Bill Wendling74c37652008-12-09 22:08:41 +000010304 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010305
Bill Wendling61edeb52008-12-02 01:06:39 +000010306 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010307 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010308 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010309
Bill Wendling61edeb52008-12-02 01:06:39 +000010310 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010311 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10312 DAG.getConstant(Cond, MVT::i32),
10313 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010314
Dan Gohman6e5fda22011-07-22 18:45:15 +000010315 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010316}
10317
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010318SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10319 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010320 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10321 EVT VT = Op.getValueType();
10322
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010323 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010324 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10325 ExtraVT.getScalarType().getSizeInBits();
10326 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10327
10328 unsigned SHLIntrinsicsID = 0;
10329 unsigned SRAIntrinsicsID = 0;
10330 switch (VT.getSimpleVT().SimpleTy) {
10331 default:
10332 return SDValue();
Craig Toppera124f942011-11-21 01:12:36 +000010333 case MVT::v4i32:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010334 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10335 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10336 break;
Craig Toppera124f942011-11-21 01:12:36 +000010337 case MVT::v8i16:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010338 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10339 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10340 break;
Craig Toppera124f942011-11-21 01:12:36 +000010341 case MVT::v8i32:
10342 case MVT::v16i16:
10343 if (!Subtarget->hasAVX())
10344 return SDValue();
10345 if (!Subtarget->hasAVX2()) {
10346 // needs to be split
10347 int NumElems = VT.getVectorNumElements();
10348 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10349 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10350
10351 // Extract the LHS vectors
10352 SDValue LHS = Op.getOperand(0);
10353 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10354 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10355
10356 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10357 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10358
10359 EVT ExtraEltVT = ExtraVT.getVectorElementType();
10360 int ExtraNumElems = ExtraVT.getVectorNumElements();
10361 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10362 ExtraNumElems/2);
10363 SDValue Extra = DAG.getValueType(ExtraVT);
10364
10365 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10366 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
10367
10368 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10369 }
10370 if (VT == MVT::v8i32) {
10371 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_d;
10372 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_d;
10373 } else {
10374 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_w;
10375 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_w;
10376 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010377 }
10378
10379 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10380 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
Craig Toppera124f942011-11-21 01:12:36 +000010381 Op.getOperand(0), ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010382
Nadav Rotema7934dd2011-10-10 19:31:45 +000010383 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10384 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10385 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010386 }
10387
10388 return SDValue();
10389}
10390
10391
Eric Christopher9a9d2752010-07-22 02:48:34 +000010392SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10393 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010394
Eric Christopher77ed1352011-07-08 00:04:56 +000010395 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10396 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010397 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010398 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010399 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010400 SDValue Ops[] = {
10401 DAG.getRegister(X86::ESP, MVT::i32), // Base
10402 DAG.getTargetConstant(1, MVT::i8), // Scale
10403 DAG.getRegister(0, MVT::i32), // Index
10404 DAG.getTargetConstant(0, MVT::i32), // Disp
10405 DAG.getRegister(0, MVT::i32), // Segment.
10406 Zero,
10407 Chain
10408 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010409 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010410 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10411 array_lengthof(Ops));
10412 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010413 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010414
Eric Christopher9a9d2752010-07-22 02:48:34 +000010415 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010416 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010417 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010418
Chris Lattner132929a2010-08-14 17:26:09 +000010419 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10420 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10421 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10422 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010423
Chris Lattner132929a2010-08-14 17:26:09 +000010424 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10425 if (!Op1 && !Op2 && !Op3 && Op4)
10426 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010427
Chris Lattner132929a2010-08-14 17:26:09 +000010428 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10429 if (Op1 && !Op2 && !Op3 && !Op4)
10430 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010431
10432 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010433 // (MFENCE)>;
10434 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010435}
10436
Eli Friedman14648462011-07-27 22:21:52 +000010437SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10438 SelectionDAG &DAG) const {
10439 DebugLoc dl = Op.getDebugLoc();
10440 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10441 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10442 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10443 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10444
10445 // The only fence that needs an instruction is a sequentially-consistent
10446 // cross-thread fence.
10447 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10448 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10449 // no-sse2). There isn't any reason to disable it if the target processor
10450 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010451 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010452 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10453
10454 SDValue Chain = Op.getOperand(0);
10455 SDValue Zero = DAG.getConstant(0, MVT::i32);
10456 SDValue Ops[] = {
10457 DAG.getRegister(X86::ESP, MVT::i32), // Base
10458 DAG.getTargetConstant(1, MVT::i8), // Scale
10459 DAG.getRegister(0, MVT::i32), // Index
10460 DAG.getTargetConstant(0, MVT::i32), // Disp
10461 DAG.getRegister(0, MVT::i32), // Segment.
10462 Zero,
10463 Chain
10464 };
10465 SDNode *Res =
10466 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10467 array_lengthof(Ops));
10468 return SDValue(Res, 0);
10469 }
10470
10471 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10472 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10473}
10474
10475
Dan Gohmand858e902010-04-17 15:26:15 +000010476SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010477 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010478 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010479 unsigned Reg = 0;
10480 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010481 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010482 default:
10483 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010484 case MVT::i8: Reg = X86::AL; size = 1; break;
10485 case MVT::i16: Reg = X86::AX; size = 2; break;
10486 case MVT::i32: Reg = X86::EAX; size = 4; break;
10487 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010488 assert(Subtarget->is64Bit() && "Node not type legal!");
10489 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010490 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010491 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010492 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010493 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010494 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010495 Op.getOperand(1),
10496 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010497 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010498 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010499 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010500 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10501 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10502 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010503 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010504 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010505 return cpOut;
10506}
10507
Duncan Sands1607f052008-12-01 11:39:25 +000010508SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010509 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010510 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010511 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010512 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010513 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010514 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010515 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10516 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010517 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010518 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10519 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010520 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010521 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010522 rdx.getValue(1)
10523 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010524 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010525}
10526
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010527SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010528 SelectionDAG &DAG) const {
10529 EVT SrcVT = Op.getOperand(0).getValueType();
10530 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010531 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010532 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010533 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010534 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010535 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010536 // i64 <=> MMX conversions are Legal.
10537 if (SrcVT==MVT::i64 && DstVT.isVector())
10538 return Op;
10539 if (DstVT==MVT::i64 && SrcVT.isVector())
10540 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010541 // MMX <=> MMX conversions are Legal.
10542 if (SrcVT.isVector() && DstVT.isVector())
10543 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010544 // All other conversions need to be expanded.
10545 return SDValue();
10546}
Chris Lattner5b856542010-12-20 00:59:46 +000010547
Dan Gohmand858e902010-04-17 15:26:15 +000010548SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010549 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010550 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010551 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010552 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010553 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010554 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010555 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010556 Node->getOperand(0),
10557 Node->getOperand(1), negOp,
10558 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010559 cast<AtomicSDNode>(Node)->getAlignment(),
10560 cast<AtomicSDNode>(Node)->getOrdering(),
10561 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010562}
10563
Eli Friedman327236c2011-08-24 20:50:09 +000010564static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10565 SDNode *Node = Op.getNode();
10566 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010567 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010568
10569 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010570 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10571 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10572 // (The only way to get a 16-byte store is cmpxchg16b)
10573 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10574 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10575 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010576 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10577 cast<AtomicSDNode>(Node)->getMemoryVT(),
10578 Node->getOperand(0),
10579 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010580 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010581 cast<AtomicSDNode>(Node)->getOrdering(),
10582 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010583 return Swap.getValue(1);
10584 }
10585 // Other atomic stores have a simple pattern.
10586 return Op;
10587}
10588
Chris Lattner5b856542010-12-20 00:59:46 +000010589static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10590 EVT VT = Op.getNode()->getValueType(0);
10591
10592 // Let legalize expand this if it isn't a legal type yet.
10593 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10594 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010595
Chris Lattner5b856542010-12-20 00:59:46 +000010596 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010597
Chris Lattner5b856542010-12-20 00:59:46 +000010598 unsigned Opc;
10599 bool ExtraOp = false;
10600 switch (Op.getOpcode()) {
10601 default: assert(0 && "Invalid code");
10602 case ISD::ADDC: Opc = X86ISD::ADD; break;
10603 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10604 case ISD::SUBC: Opc = X86ISD::SUB; break;
10605 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10606 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010607
Chris Lattner5b856542010-12-20 00:59:46 +000010608 if (!ExtraOp)
10609 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10610 Op.getOperand(1));
10611 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10612 Op.getOperand(1), Op.getOperand(2));
10613}
10614
Evan Cheng0db9fe62006-04-25 20:13:52 +000010615/// LowerOperation - Provide custom lowering hooks for some operations.
10616///
Dan Gohmand858e902010-04-17 15:26:15 +000010617SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010618 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010619 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010620 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010621 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010622 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010623 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10624 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010625 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010626 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010627 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010628 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10629 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10630 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010631 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010632 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010633 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10634 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10635 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010636 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010637 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010638 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010639 case ISD::SHL_PARTS:
10640 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010641 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010642 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010643 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010644 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010645 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010646 case ISD::FABS: return LowerFABS(Op, DAG);
10647 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010648 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010649 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010650 case ISD::SETCC: return LowerSETCC(Op, DAG);
10651 case ISD::SELECT: return LowerSELECT(Op, DAG);
10652 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010653 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010654 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010655 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010656 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010657 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010658 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10659 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010660 case ISD::FRAME_TO_ARGS_OFFSET:
10661 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010662 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010663 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010664 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10665 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010666 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010667 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10668 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010669 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010670 case ISD::SRA:
10671 case ISD::SRL:
10672 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010673 case ISD::SADDO:
10674 case ISD::UADDO:
10675 case ISD::SSUBO:
10676 case ISD::USUBO:
10677 case ISD::SMULO:
10678 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010679 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010680 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010681 case ISD::ADDC:
10682 case ISD::ADDE:
10683 case ISD::SUBC:
10684 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010685 case ISD::ADD: return LowerADD(Op, DAG);
10686 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010687 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010688}
10689
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010690static void ReplaceATOMIC_LOAD(SDNode *Node,
10691 SmallVectorImpl<SDValue> &Results,
10692 SelectionDAG &DAG) {
10693 DebugLoc dl = Node->getDebugLoc();
10694 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10695
10696 // Convert wide load -> cmpxchg8b/cmpxchg16b
10697 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10698 // (The only way to get a 16-byte load is cmpxchg16b)
10699 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010700 SDValue Zero = DAG.getConstant(0, VT);
10701 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010702 Node->getOperand(0),
10703 Node->getOperand(1), Zero, Zero,
10704 cast<AtomicSDNode>(Node)->getMemOperand(),
10705 cast<AtomicSDNode>(Node)->getOrdering(),
10706 cast<AtomicSDNode>(Node)->getSynchScope());
10707 Results.push_back(Swap.getValue(0));
10708 Results.push_back(Swap.getValue(1));
10709}
10710
Duncan Sands1607f052008-12-01 11:39:25 +000010711void X86TargetLowering::
10712ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010713 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010714 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000010715 assert (Node->getValueType(0) == MVT::i64 &&
10716 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010717
10718 SDValue Chain = Node->getOperand(0);
10719 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010720 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010721 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010722 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010723 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010724 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010725 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010726 SDValue Result =
10727 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10728 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010729 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010730 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010731 Results.push_back(Result.getValue(2));
10732}
10733
Duncan Sands126d9072008-07-04 11:47:58 +000010734/// ReplaceNodeResults - Replace a node with an illegal result type
10735/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010736void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10737 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010738 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010739 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010740 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010741 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010742 assert(false && "Do not know how to custom type legalize this operation!");
10743 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010744 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010745 case ISD::ADDC:
10746 case ISD::ADDE:
10747 case ISD::SUBC:
10748 case ISD::SUBE:
10749 // We don't want to expand or promote these.
10750 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010751 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010752 std::pair<SDValue,SDValue> Vals =
10753 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010754 SDValue FIST = Vals.first, StackSlot = Vals.second;
10755 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010756 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010757 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010758 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010759 MachinePointerInfo(),
10760 false, false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010761 }
10762 return;
10763 }
10764 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010765 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010766 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010767 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010768 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010769 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010770 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010771 eax.getValue(2));
10772 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10773 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010774 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010775 Results.push_back(edx.getValue(1));
10776 return;
10777 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010778 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010779 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010780 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010781 bool Regs64bit = T == MVT::i128;
10782 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010783 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010784 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10785 DAG.getConstant(0, HalfT));
10786 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10787 DAG.getConstant(1, HalfT));
10788 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10789 Regs64bit ? X86::RAX : X86::EAX,
10790 cpInL, SDValue());
10791 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10792 Regs64bit ? X86::RDX : X86::EDX,
10793 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010794 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010795 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10796 DAG.getConstant(0, HalfT));
10797 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10798 DAG.getConstant(1, HalfT));
10799 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10800 Regs64bit ? X86::RBX : X86::EBX,
10801 swapInL, cpInH.getValue(1));
10802 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10803 Regs64bit ? X86::RCX : X86::ECX,
10804 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010805 SDValue Ops[] = { swapInH.getValue(0),
10806 N->getOperand(1),
10807 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010808 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010809 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010810 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10811 X86ISD::LCMPXCHG8_DAG;
10812 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010813 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010814 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10815 Regs64bit ? X86::RAX : X86::EAX,
10816 HalfT, Result.getValue(1));
10817 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10818 Regs64bit ? X86::RDX : X86::EDX,
10819 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010820 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010821 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010822 Results.push_back(cpOutH.getValue(1));
10823 return;
10824 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010825 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010826 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10827 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010828 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010829 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10830 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010831 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010832 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10833 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010834 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010835 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10836 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010837 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010838 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10839 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010840 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010841 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10842 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010843 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010844 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10845 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010846 case ISD::ATOMIC_LOAD:
10847 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010848 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010849}
10850
Evan Cheng72261582005-12-20 06:22:03 +000010851const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10852 switch (Opcode) {
10853 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010854 case X86ISD::BSF: return "X86ISD::BSF";
10855 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010856 case X86ISD::SHLD: return "X86ISD::SHLD";
10857 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010858 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010859 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010860 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010861 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010862 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010863 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010864 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10865 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10866 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010867 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010868 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010869 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010870 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010871 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010872 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010873 case X86ISD::COMI: return "X86ISD::COMI";
10874 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010875 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010876 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010877 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10878 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010879 case X86ISD::CMOV: return "X86ISD::CMOV";
10880 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010881 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010882 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10883 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010884 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010885 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010886 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010887 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010888 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010889 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10890 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010891 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010892 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010893 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000010894 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000010895 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Craig Topperfe033152011-12-06 09:31:36 +000010896 case X86ISD::HADD: return "X86ISD::HADD";
10897 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000010898 case X86ISD::FHADD: return "X86ISD::FHADD";
10899 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000010900 case X86ISD::FMAX: return "X86ISD::FMAX";
10901 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010902 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10903 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010904 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010905 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010906 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010907 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010908 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010909 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10910 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010911 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10912 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10913 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10914 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10915 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10916 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010917 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10918 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010919 case X86ISD::VSHL: return "X86ISD::VSHL";
10920 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010921 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10922 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10923 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10924 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10925 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10926 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10927 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10928 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10929 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10930 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010931 case X86ISD::ADD: return "X86ISD::ADD";
10932 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010933 case X86ISD::ADC: return "X86ISD::ADC";
10934 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010935 case X86ISD::SMUL: return "X86ISD::SMUL";
10936 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010937 case X86ISD::INC: return "X86ISD::INC";
10938 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010939 case X86ISD::OR: return "X86ISD::OR";
10940 case X86ISD::XOR: return "X86ISD::XOR";
10941 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000010942 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000010943 case X86ISD::BLSI: return "X86ISD::BLSI";
10944 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
10945 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000010946 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010947 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010948 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010949 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10950 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10951 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10952 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10953 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10954 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10955 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10956 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10957 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010958 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010959 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010960 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010961 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10962 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010963 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10964 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10965 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10966 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10967 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10968 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10969 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000010970 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
10971 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010972 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000010973 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000010974 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010975 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010976 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010977 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010978 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010979 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010980 }
10981}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010982
Chris Lattnerc9addb72007-03-30 23:15:24 +000010983// isLegalAddressingMode - Return true if the addressing mode represented
10984// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010985bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010986 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010987 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010988 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010989 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010990
Chris Lattnerc9addb72007-03-30 23:15:24 +000010991 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010992 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010993 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010994
Chris Lattnerc9addb72007-03-30 23:15:24 +000010995 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010996 unsigned GVFlags =
10997 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010998
Chris Lattnerdfed4132009-07-10 07:38:24 +000010999 // If a reference to this global requires an extra load, we can't fold it.
11000 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011001 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011002
Chris Lattnerdfed4132009-07-10 07:38:24 +000011003 // If BaseGV requires a register for the PIC base, we cannot also have a
11004 // BaseReg specified.
11005 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011006 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011007
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011008 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011009 if ((M != CodeModel::Small || R != Reloc::Static) &&
11010 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011011 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011012 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011013
Chris Lattnerc9addb72007-03-30 23:15:24 +000011014 switch (AM.Scale) {
11015 case 0:
11016 case 1:
11017 case 2:
11018 case 4:
11019 case 8:
11020 // These scales always work.
11021 break;
11022 case 3:
11023 case 5:
11024 case 9:
11025 // These scales are formed with basereg+scalereg. Only accept if there is
11026 // no basereg yet.
11027 if (AM.HasBaseReg)
11028 return false;
11029 break;
11030 default: // Other stuff never works.
11031 return false;
11032 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011033
Chris Lattnerc9addb72007-03-30 23:15:24 +000011034 return true;
11035}
11036
11037
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011038bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011039 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011040 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011041 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11042 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011043 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011044 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011045 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011046}
11047
Owen Andersone50ed302009-08-10 22:56:29 +000011048bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011049 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011050 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011051 unsigned NumBits1 = VT1.getSizeInBits();
11052 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011053 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011054 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011055 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011056}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011057
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011058bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011059 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011060 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011061}
11062
Owen Andersone50ed302009-08-10 22:56:29 +000011063bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011064 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011065 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011066}
11067
Owen Andersone50ed302009-08-10 22:56:29 +000011068bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011069 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011070 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011071}
11072
Evan Cheng60c07e12006-07-05 22:17:51 +000011073/// isShuffleMaskLegal - Targets can use this to indicate that they only
11074/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11075/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11076/// are assumed to be legal.
11077bool
Eric Christopherfd179292009-08-27 18:07:15 +000011078X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011079 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011080 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011081 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011082 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011083
Nate Begemana09008b2009-10-19 02:17:23 +000011084 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011085 return (VT.getVectorNumElements() == 2 ||
11086 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11087 isMOVLMask(M, VT) ||
11088 isSHUFPMask(M, VT) ||
11089 isPSHUFDMask(M, VT) ||
11090 isPSHUFHWMask(M, VT) ||
11091 isPSHUFLWMask(M, VT) ||
Craig Topperc0d82852011-11-22 00:44:41 +000011092 isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX()) ||
Craig Topper6347e862011-11-21 06:57:39 +000011093 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11094 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011095 isUNPCKL_v_undef_Mask(M, VT) ||
11096 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011097}
11098
Dan Gohman7d8143f2008-04-09 20:09:42 +000011099bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011100X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011101 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011102 unsigned NumElts = VT.getVectorNumElements();
11103 // FIXME: This collection of masks seems suspect.
11104 if (NumElts == 2)
11105 return true;
11106 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11107 return (isMOVLMask(Mask, VT) ||
11108 isCommutedMOVLMask(Mask, VT, true) ||
11109 isSHUFPMask(Mask, VT) ||
Craig Topper1ff73d72011-12-06 04:59:07 +000011110 isSHUFPMask(Mask, VT, /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011111 }
11112 return false;
11113}
11114
11115//===----------------------------------------------------------------------===//
11116// X86 Scheduler Hooks
11117//===----------------------------------------------------------------------===//
11118
Mon P Wang63307c32008-05-05 19:05:59 +000011119// private utility function
11120MachineBasicBlock *
11121X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11122 MachineBasicBlock *MBB,
11123 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011124 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011125 unsigned LoadOpc,
11126 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011127 unsigned notOpc,
11128 unsigned EAXreg,
11129 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011130 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011131 // For the atomic bitwise operator, we generate
11132 // thisMBB:
11133 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011134 // ld t1 = [bitinstr.addr]
11135 // op t2 = t1, [bitinstr.val]
11136 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011137 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11138 // bz newMBB
11139 // fallthrough -->nextMBB
11140 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11141 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011142 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011143 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011144
Mon P Wang63307c32008-05-05 19:05:59 +000011145 /// First build the CFG
11146 MachineFunction *F = MBB->getParent();
11147 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011148 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11149 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11150 F->insert(MBBIter, newMBB);
11151 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011152
Dan Gohman14152b42010-07-06 20:24:04 +000011153 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11154 nextMBB->splice(nextMBB->begin(), thisMBB,
11155 llvm::next(MachineBasicBlock::iterator(bInstr)),
11156 thisMBB->end());
11157 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011158
Mon P Wang63307c32008-05-05 19:05:59 +000011159 // Update thisMBB to fall through to newMBB
11160 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011161
Mon P Wang63307c32008-05-05 19:05:59 +000011162 // newMBB jumps to itself and fall through to nextMBB
11163 newMBB->addSuccessor(nextMBB);
11164 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011165
Mon P Wang63307c32008-05-05 19:05:59 +000011166 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011167 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011168 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011169 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011170 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011171 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011172 int numArgs = bInstr->getNumOperands() - 1;
11173 for (int i=0; i < numArgs; ++i)
11174 argOpers[i] = &bInstr->getOperand(i+1);
11175
11176 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011177 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011178 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011179
Dale Johannesen140be2d2008-08-19 18:47:28 +000011180 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011181 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011182 for (int i=0; i <= lastAddrIndx; ++i)
11183 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011184
Dale Johannesen140be2d2008-08-19 18:47:28 +000011185 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011186 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011187 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011188 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011189 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011190 tt = t1;
11191
Dale Johannesen140be2d2008-08-19 18:47:28 +000011192 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011193 assert((argOpers[valArgIndx]->isReg() ||
11194 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011195 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011196 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011197 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011198 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011199 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011200 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011201 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011202
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011203 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011204 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011205
Dale Johannesene4d209d2009-02-03 20:21:25 +000011206 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011207 for (int i=0; i <= lastAddrIndx; ++i)
11208 (*MIB).addOperand(*argOpers[i]);
11209 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011210 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011211 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11212 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011213
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011214 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011215 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011216
Mon P Wang63307c32008-05-05 19:05:59 +000011217 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011218 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011219
Dan Gohman14152b42010-07-06 20:24:04 +000011220 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011221 return nextMBB;
11222}
11223
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011224// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011225MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011226X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11227 MachineBasicBlock *MBB,
11228 unsigned regOpcL,
11229 unsigned regOpcH,
11230 unsigned immOpcL,
11231 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011232 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011233 // For the atomic bitwise operator, we generate
11234 // thisMBB (instructions are in pairs, except cmpxchg8b)
11235 // ld t1,t2 = [bitinstr.addr]
11236 // newMBB:
11237 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11238 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011239 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011240 // mov ECX, EBX <- t5, t6
11241 // mov EAX, EDX <- t1, t2
11242 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11243 // mov t3, t4 <- EAX, EDX
11244 // bz newMBB
11245 // result in out1, out2
11246 // fallthrough -->nextMBB
11247
11248 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11249 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011250 const unsigned NotOpc = X86::NOT32r;
11251 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11252 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11253 MachineFunction::iterator MBBIter = MBB;
11254 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011255
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011256 /// First build the CFG
11257 MachineFunction *F = MBB->getParent();
11258 MachineBasicBlock *thisMBB = MBB;
11259 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11260 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11261 F->insert(MBBIter, newMBB);
11262 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011263
Dan Gohman14152b42010-07-06 20:24:04 +000011264 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11265 nextMBB->splice(nextMBB->begin(), thisMBB,
11266 llvm::next(MachineBasicBlock::iterator(bInstr)),
11267 thisMBB->end());
11268 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011269
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011270 // Update thisMBB to fall through to newMBB
11271 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011272
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011273 // newMBB jumps to itself and fall through to nextMBB
11274 newMBB->addSuccessor(nextMBB);
11275 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011276
Dale Johannesene4d209d2009-02-03 20:21:25 +000011277 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011278 // Insert instructions into newMBB based on incoming instruction
11279 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011280 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011281 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011282 MachineOperand& dest1Oper = bInstr->getOperand(0);
11283 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011284 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11285 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011286 argOpers[i] = &bInstr->getOperand(i+2);
11287
Dan Gohman71ea4e52010-05-14 21:01:44 +000011288 // We use some of the operands multiple times, so conservatively just
11289 // clear any kill flags that might be present.
11290 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11291 argOpers[i]->setIsKill(false);
11292 }
11293
Evan Chengad5b52f2010-01-08 19:14:57 +000011294 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011295 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011296
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011297 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011298 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011299 for (int i=0; i <= lastAddrIndx; ++i)
11300 (*MIB).addOperand(*argOpers[i]);
11301 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011302 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011303 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011304 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011305 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011306 MachineOperand newOp3 = *(argOpers[3]);
11307 if (newOp3.isImm())
11308 newOp3.setImm(newOp3.getImm()+4);
11309 else
11310 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011311 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011312 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011313
11314 // t3/4 are defined later, at the bottom of the loop
11315 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11316 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011317 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011318 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011319 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011320 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11321
Evan Cheng306b4ca2010-01-08 23:41:50 +000011322 // The subsequent operations should be using the destination registers of
11323 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011324 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011325 t1 = F->getRegInfo().createVirtualRegister(RC);
11326 t2 = F->getRegInfo().createVirtualRegister(RC);
11327 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11328 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011329 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011330 t1 = dest1Oper.getReg();
11331 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011332 }
11333
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011334 int valArgIndx = lastAddrIndx + 1;
11335 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011336 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011337 "invalid operand");
11338 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11339 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011340 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011341 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011342 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011343 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011344 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011345 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011346 (*MIB).addOperand(*argOpers[valArgIndx]);
11347 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011348 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011349 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011350 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011351 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011352 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011353 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011354 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011355 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011356 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011357 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011358
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011359 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011360 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011361 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011362 MIB.addReg(t2);
11363
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011364 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011365 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011366 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011367 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011368
Dale Johannesene4d209d2009-02-03 20:21:25 +000011369 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011370 for (int i=0; i <= lastAddrIndx; ++i)
11371 (*MIB).addOperand(*argOpers[i]);
11372
11373 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011374 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11375 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011376
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011377 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011378 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011379 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011380 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011381
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011382 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011383 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011384
Dan Gohman14152b42010-07-06 20:24:04 +000011385 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011386 return nextMBB;
11387}
11388
11389// private utility function
11390MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011391X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11392 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011393 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011394 // For the atomic min/max operator, we generate
11395 // thisMBB:
11396 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011397 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011398 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011399 // cmp t1, t2
11400 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011401 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011402 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11403 // bz newMBB
11404 // fallthrough -->nextMBB
11405 //
11406 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11407 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011408 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011409 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011410
Mon P Wang63307c32008-05-05 19:05:59 +000011411 /// First build the CFG
11412 MachineFunction *F = MBB->getParent();
11413 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011414 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11415 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11416 F->insert(MBBIter, newMBB);
11417 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011418
Dan Gohman14152b42010-07-06 20:24:04 +000011419 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11420 nextMBB->splice(nextMBB->begin(), thisMBB,
11421 llvm::next(MachineBasicBlock::iterator(mInstr)),
11422 thisMBB->end());
11423 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011424
Mon P Wang63307c32008-05-05 19:05:59 +000011425 // Update thisMBB to fall through to newMBB
11426 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011427
Mon P Wang63307c32008-05-05 19:05:59 +000011428 // newMBB jumps to newMBB and fall through to nextMBB
11429 newMBB->addSuccessor(nextMBB);
11430 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011431
Dale Johannesene4d209d2009-02-03 20:21:25 +000011432 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011433 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011434 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011435 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011436 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011437 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011438 int numArgs = mInstr->getNumOperands() - 1;
11439 for (int i=0; i < numArgs; ++i)
11440 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011441
Mon P Wang63307c32008-05-05 19:05:59 +000011442 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011443 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011444 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011445
Mon P Wangab3e7472008-05-05 22:56:23 +000011446 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011447 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011448 for (int i=0; i <= lastAddrIndx; ++i)
11449 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011450
Mon P Wang63307c32008-05-05 19:05:59 +000011451 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011452 assert((argOpers[valArgIndx]->isReg() ||
11453 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011454 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011455
11456 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011457 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011458 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011459 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011460 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011461 (*MIB).addOperand(*argOpers[valArgIndx]);
11462
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011463 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011464 MIB.addReg(t1);
11465
Dale Johannesene4d209d2009-02-03 20:21:25 +000011466 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011467 MIB.addReg(t1);
11468 MIB.addReg(t2);
11469
11470 // Generate movc
11471 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011472 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011473 MIB.addReg(t2);
11474 MIB.addReg(t1);
11475
11476 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011477 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011478 for (int i=0; i <= lastAddrIndx; ++i)
11479 (*MIB).addOperand(*argOpers[i]);
11480 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011481 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011482 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11483 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011484
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011485 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011486 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011487
Mon P Wang63307c32008-05-05 19:05:59 +000011488 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011489 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011490
Dan Gohman14152b42010-07-06 20:24:04 +000011491 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011492 return nextMBB;
11493}
11494
Eric Christopherf83a5de2009-08-27 18:08:16 +000011495// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011496// or XMM0_V32I8 in AVX all of this code can be replaced with that
11497// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011498MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011499X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011500 unsigned numArgs, bool memArg) const {
Craig Topperc0d82852011-11-22 00:44:41 +000011501 assert(Subtarget->hasSSE42orAVX() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011502 "Target must have SSE4.2 or AVX features enabled");
11503
Eric Christopherb120ab42009-08-18 22:50:32 +000011504 DebugLoc dl = MI->getDebugLoc();
11505 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011506 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011507 if (!Subtarget->hasAVX()) {
11508 if (memArg)
11509 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11510 else
11511 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11512 } else {
11513 if (memArg)
11514 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11515 else
11516 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11517 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011518
Eric Christopher41c902f2010-11-30 08:20:21 +000011519 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011520 for (unsigned i = 0; i < numArgs; ++i) {
11521 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011522 if (!(Op.isReg() && Op.isImplicit()))
11523 MIB.addOperand(Op);
11524 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011525 BuildMI(*BB, MI, dl,
11526 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11527 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011528 .addReg(X86::XMM0);
11529
Dan Gohman14152b42010-07-06 20:24:04 +000011530 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011531 return BB;
11532}
11533
11534MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011535X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011536 DebugLoc dl = MI->getDebugLoc();
11537 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011538
Eric Christopher228232b2010-11-30 07:20:12 +000011539 // Address into RAX/EAX, other two args into ECX, EDX.
11540 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11541 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11542 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11543 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011544 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011545
Eric Christopher228232b2010-11-30 07:20:12 +000011546 unsigned ValOps = X86::AddrNumOperands;
11547 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11548 .addReg(MI->getOperand(ValOps).getReg());
11549 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11550 .addReg(MI->getOperand(ValOps+1).getReg());
11551
11552 // The instruction doesn't actually take any operands though.
11553 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011554
Eric Christopher228232b2010-11-30 07:20:12 +000011555 MI->eraseFromParent(); // The pseudo is gone now.
11556 return BB;
11557}
11558
11559MachineBasicBlock *
11560X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011561 DebugLoc dl = MI->getDebugLoc();
11562 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011563
Eric Christopher228232b2010-11-30 07:20:12 +000011564 // First arg in ECX, the second in EAX.
11565 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11566 .addReg(MI->getOperand(0).getReg());
11567 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11568 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011569
Eric Christopher228232b2010-11-30 07:20:12 +000011570 // The instruction doesn't actually take any operands though.
11571 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011572
Eric Christopher228232b2010-11-30 07:20:12 +000011573 MI->eraseFromParent(); // The pseudo is gone now.
11574 return BB;
11575}
11576
11577MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011578X86TargetLowering::EmitVAARG64WithCustomInserter(
11579 MachineInstr *MI,
11580 MachineBasicBlock *MBB) const {
11581 // Emit va_arg instruction on X86-64.
11582
11583 // Operands to this pseudo-instruction:
11584 // 0 ) Output : destination address (reg)
11585 // 1-5) Input : va_list address (addr, i64mem)
11586 // 6 ) ArgSize : Size (in bytes) of vararg type
11587 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11588 // 8 ) Align : Alignment of type
11589 // 9 ) EFLAGS (implicit-def)
11590
11591 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11592 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11593
11594 unsigned DestReg = MI->getOperand(0).getReg();
11595 MachineOperand &Base = MI->getOperand(1);
11596 MachineOperand &Scale = MI->getOperand(2);
11597 MachineOperand &Index = MI->getOperand(3);
11598 MachineOperand &Disp = MI->getOperand(4);
11599 MachineOperand &Segment = MI->getOperand(5);
11600 unsigned ArgSize = MI->getOperand(6).getImm();
11601 unsigned ArgMode = MI->getOperand(7).getImm();
11602 unsigned Align = MI->getOperand(8).getImm();
11603
11604 // Memory Reference
11605 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11606 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11607 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11608
11609 // Machine Information
11610 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11611 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11612 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11613 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11614 DebugLoc DL = MI->getDebugLoc();
11615
11616 // struct va_list {
11617 // i32 gp_offset
11618 // i32 fp_offset
11619 // i64 overflow_area (address)
11620 // i64 reg_save_area (address)
11621 // }
11622 // sizeof(va_list) = 24
11623 // alignment(va_list) = 8
11624
11625 unsigned TotalNumIntRegs = 6;
11626 unsigned TotalNumXMMRegs = 8;
11627 bool UseGPOffset = (ArgMode == 1);
11628 bool UseFPOffset = (ArgMode == 2);
11629 unsigned MaxOffset = TotalNumIntRegs * 8 +
11630 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11631
11632 /* Align ArgSize to a multiple of 8 */
11633 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11634 bool NeedsAlign = (Align > 8);
11635
11636 MachineBasicBlock *thisMBB = MBB;
11637 MachineBasicBlock *overflowMBB;
11638 MachineBasicBlock *offsetMBB;
11639 MachineBasicBlock *endMBB;
11640
11641 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11642 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11643 unsigned OffsetReg = 0;
11644
11645 if (!UseGPOffset && !UseFPOffset) {
11646 // If we only pull from the overflow region, we don't create a branch.
11647 // We don't need to alter control flow.
11648 OffsetDestReg = 0; // unused
11649 OverflowDestReg = DestReg;
11650
11651 offsetMBB = NULL;
11652 overflowMBB = thisMBB;
11653 endMBB = thisMBB;
11654 } else {
11655 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11656 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11657 // If not, pull from overflow_area. (branch to overflowMBB)
11658 //
11659 // thisMBB
11660 // | .
11661 // | .
11662 // offsetMBB overflowMBB
11663 // | .
11664 // | .
11665 // endMBB
11666
11667 // Registers for the PHI in endMBB
11668 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11669 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11670
11671 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11672 MachineFunction *MF = MBB->getParent();
11673 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11674 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11675 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11676
11677 MachineFunction::iterator MBBIter = MBB;
11678 ++MBBIter;
11679
11680 // Insert the new basic blocks
11681 MF->insert(MBBIter, offsetMBB);
11682 MF->insert(MBBIter, overflowMBB);
11683 MF->insert(MBBIter, endMBB);
11684
11685 // Transfer the remainder of MBB and its successor edges to endMBB.
11686 endMBB->splice(endMBB->begin(), thisMBB,
11687 llvm::next(MachineBasicBlock::iterator(MI)),
11688 thisMBB->end());
11689 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11690
11691 // Make offsetMBB and overflowMBB successors of thisMBB
11692 thisMBB->addSuccessor(offsetMBB);
11693 thisMBB->addSuccessor(overflowMBB);
11694
11695 // endMBB is a successor of both offsetMBB and overflowMBB
11696 offsetMBB->addSuccessor(endMBB);
11697 overflowMBB->addSuccessor(endMBB);
11698
11699 // Load the offset value into a register
11700 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11701 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11702 .addOperand(Base)
11703 .addOperand(Scale)
11704 .addOperand(Index)
11705 .addDisp(Disp, UseFPOffset ? 4 : 0)
11706 .addOperand(Segment)
11707 .setMemRefs(MMOBegin, MMOEnd);
11708
11709 // Check if there is enough room left to pull this argument.
11710 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11711 .addReg(OffsetReg)
11712 .addImm(MaxOffset + 8 - ArgSizeA8);
11713
11714 // Branch to "overflowMBB" if offset >= max
11715 // Fall through to "offsetMBB" otherwise
11716 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11717 .addMBB(overflowMBB);
11718 }
11719
11720 // In offsetMBB, emit code to use the reg_save_area.
11721 if (offsetMBB) {
11722 assert(OffsetReg != 0);
11723
11724 // Read the reg_save_area address.
11725 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11726 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11727 .addOperand(Base)
11728 .addOperand(Scale)
11729 .addOperand(Index)
11730 .addDisp(Disp, 16)
11731 .addOperand(Segment)
11732 .setMemRefs(MMOBegin, MMOEnd);
11733
11734 // Zero-extend the offset
11735 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11736 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11737 .addImm(0)
11738 .addReg(OffsetReg)
11739 .addImm(X86::sub_32bit);
11740
11741 // Add the offset to the reg_save_area to get the final address.
11742 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11743 .addReg(OffsetReg64)
11744 .addReg(RegSaveReg);
11745
11746 // Compute the offset for the next argument
11747 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11748 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11749 .addReg(OffsetReg)
11750 .addImm(UseFPOffset ? 16 : 8);
11751
11752 // Store it back into the va_list.
11753 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11754 .addOperand(Base)
11755 .addOperand(Scale)
11756 .addOperand(Index)
11757 .addDisp(Disp, UseFPOffset ? 4 : 0)
11758 .addOperand(Segment)
11759 .addReg(NextOffsetReg)
11760 .setMemRefs(MMOBegin, MMOEnd);
11761
11762 // Jump to endMBB
11763 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11764 .addMBB(endMBB);
11765 }
11766
11767 //
11768 // Emit code to use overflow area
11769 //
11770
11771 // Load the overflow_area address into a register.
11772 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11773 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11774 .addOperand(Base)
11775 .addOperand(Scale)
11776 .addOperand(Index)
11777 .addDisp(Disp, 8)
11778 .addOperand(Segment)
11779 .setMemRefs(MMOBegin, MMOEnd);
11780
11781 // If we need to align it, do so. Otherwise, just copy the address
11782 // to OverflowDestReg.
11783 if (NeedsAlign) {
11784 // Align the overflow address
11785 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11786 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11787
11788 // aligned_addr = (addr + (align-1)) & ~(align-1)
11789 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11790 .addReg(OverflowAddrReg)
11791 .addImm(Align-1);
11792
11793 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11794 .addReg(TmpReg)
11795 .addImm(~(uint64_t)(Align-1));
11796 } else {
11797 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11798 .addReg(OverflowAddrReg);
11799 }
11800
11801 // Compute the next overflow address after this argument.
11802 // (the overflow address should be kept 8-byte aligned)
11803 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11804 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11805 .addReg(OverflowDestReg)
11806 .addImm(ArgSizeA8);
11807
11808 // Store the new overflow address.
11809 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11810 .addOperand(Base)
11811 .addOperand(Scale)
11812 .addOperand(Index)
11813 .addDisp(Disp, 8)
11814 .addOperand(Segment)
11815 .addReg(NextAddrReg)
11816 .setMemRefs(MMOBegin, MMOEnd);
11817
11818 // If we branched, emit the PHI to the front of endMBB.
11819 if (offsetMBB) {
11820 BuildMI(*endMBB, endMBB->begin(), DL,
11821 TII->get(X86::PHI), DestReg)
11822 .addReg(OffsetDestReg).addMBB(offsetMBB)
11823 .addReg(OverflowDestReg).addMBB(overflowMBB);
11824 }
11825
11826 // Erase the pseudo instruction
11827 MI->eraseFromParent();
11828
11829 return endMBB;
11830}
11831
11832MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011833X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11834 MachineInstr *MI,
11835 MachineBasicBlock *MBB) const {
11836 // Emit code to save XMM registers to the stack. The ABI says that the
11837 // number of registers to save is given in %al, so it's theoretically
11838 // possible to do an indirect jump trick to avoid saving all of them,
11839 // however this code takes a simpler approach and just executes all
11840 // of the stores if %al is non-zero. It's less code, and it's probably
11841 // easier on the hardware branch predictor, and stores aren't all that
11842 // expensive anyway.
11843
11844 // Create the new basic blocks. One block contains all the XMM stores,
11845 // and one block is the final destination regardless of whether any
11846 // stores were performed.
11847 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11848 MachineFunction *F = MBB->getParent();
11849 MachineFunction::iterator MBBIter = MBB;
11850 ++MBBIter;
11851 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11852 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11853 F->insert(MBBIter, XMMSaveMBB);
11854 F->insert(MBBIter, EndMBB);
11855
Dan Gohman14152b42010-07-06 20:24:04 +000011856 // Transfer the remainder of MBB and its successor edges to EndMBB.
11857 EndMBB->splice(EndMBB->begin(), MBB,
11858 llvm::next(MachineBasicBlock::iterator(MI)),
11859 MBB->end());
11860 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11861
Dan Gohmand6708ea2009-08-15 01:38:56 +000011862 // The original block will now fall through to the XMM save block.
11863 MBB->addSuccessor(XMMSaveMBB);
11864 // The XMMSaveMBB will fall through to the end block.
11865 XMMSaveMBB->addSuccessor(EndMBB);
11866
11867 // Now add the instructions.
11868 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11869 DebugLoc DL = MI->getDebugLoc();
11870
11871 unsigned CountReg = MI->getOperand(0).getReg();
11872 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11873 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11874
11875 if (!Subtarget->isTargetWin64()) {
11876 // If %al is 0, branch around the XMM save block.
11877 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011878 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011879 MBB->addSuccessor(EndMBB);
11880 }
11881
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011882 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011883 // In the XMM save block, save all the XMM argument registers.
11884 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11885 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011886 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011887 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011888 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011889 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011890 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011891 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011892 .addFrameIndex(RegSaveFrameIndex)
11893 .addImm(/*Scale=*/1)
11894 .addReg(/*IndexReg=*/0)
11895 .addImm(/*Disp=*/Offset)
11896 .addReg(/*Segment=*/0)
11897 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011898 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011899 }
11900
Dan Gohman14152b42010-07-06 20:24:04 +000011901 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011902
11903 return EndMBB;
11904}
Mon P Wang63307c32008-05-05 19:05:59 +000011905
Evan Cheng60c07e12006-07-05 22:17:51 +000011906MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011907X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011908 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011909 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11910 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011911
Chris Lattner52600972009-09-02 05:57:00 +000011912 // To "insert" a SELECT_CC instruction, we actually have to insert the
11913 // diamond control-flow pattern. The incoming instruction knows the
11914 // destination vreg to set, the condition code register to branch on, the
11915 // true/false values to select between, and a branch opcode to use.
11916 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11917 MachineFunction::iterator It = BB;
11918 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011919
Chris Lattner52600972009-09-02 05:57:00 +000011920 // thisMBB:
11921 // ...
11922 // TrueVal = ...
11923 // cmpTY ccX, r1, r2
11924 // bCC copy1MBB
11925 // fallthrough --> copy0MBB
11926 MachineBasicBlock *thisMBB = BB;
11927 MachineFunction *F = BB->getParent();
11928 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11929 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011930 F->insert(It, copy0MBB);
11931 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011932
Bill Wendling730c07e2010-06-25 20:48:10 +000011933 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11934 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011935 if (!MI->killsRegister(X86::EFLAGS)) {
11936 copy0MBB->addLiveIn(X86::EFLAGS);
11937 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011938 }
11939
Dan Gohman14152b42010-07-06 20:24:04 +000011940 // Transfer the remainder of BB and its successor edges to sinkMBB.
11941 sinkMBB->splice(sinkMBB->begin(), BB,
11942 llvm::next(MachineBasicBlock::iterator(MI)),
11943 BB->end());
11944 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11945
11946 // Add the true and fallthrough blocks as its successors.
11947 BB->addSuccessor(copy0MBB);
11948 BB->addSuccessor(sinkMBB);
11949
11950 // Create the conditional branch instruction.
11951 unsigned Opc =
11952 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11953 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11954
Chris Lattner52600972009-09-02 05:57:00 +000011955 // copy0MBB:
11956 // %FalseValue = ...
11957 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011958 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011959
Chris Lattner52600972009-09-02 05:57:00 +000011960 // sinkMBB:
11961 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11962 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011963 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11964 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011965 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11966 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11967
Dan Gohman14152b42010-07-06 20:24:04 +000011968 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011969 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011970}
11971
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011972MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011973X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11974 bool Is64Bit) const {
11975 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11976 DebugLoc DL = MI->getDebugLoc();
11977 MachineFunction *MF = BB->getParent();
11978 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11979
Nick Lewycky8a8d4792011-12-02 22:16:29 +000011980 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011981
11982 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11983 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11984
11985 // BB:
11986 // ... [Till the alloca]
11987 // If stacklet is not large enough, jump to mallocMBB
11988 //
11989 // bumpMBB:
11990 // Allocate by subtracting from RSP
11991 // Jump to continueMBB
11992 //
11993 // mallocMBB:
11994 // Allocate by call to runtime
11995 //
11996 // continueMBB:
11997 // ...
11998 // [rest of original BB]
11999 //
12000
12001 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12002 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12003 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12004
12005 MachineRegisterInfo &MRI = MF->getRegInfo();
12006 const TargetRegisterClass *AddrRegClass =
12007 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12008
12009 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12010 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12011 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012012 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012013 sizeVReg = MI->getOperand(1).getReg(),
12014 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12015
12016 MachineFunction::iterator MBBIter = BB;
12017 ++MBBIter;
12018
12019 MF->insert(MBBIter, bumpMBB);
12020 MF->insert(MBBIter, mallocMBB);
12021 MF->insert(MBBIter, continueMBB);
12022
12023 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12024 (MachineBasicBlock::iterator(MI)), BB->end());
12025 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12026
12027 // Add code to the main basic block to check if the stack limit has been hit,
12028 // and if so, jump to mallocMBB otherwise to bumpMBB.
12029 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012030 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012031 .addReg(tmpSPVReg).addReg(sizeVReg);
12032 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
12033 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012034 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012035 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12036
12037 // bumpMBB simply decreases the stack pointer, since we know the current
12038 // stacklet has enough space.
12039 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012040 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012041 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012042 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012043 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12044
12045 // Calls into a routine in libgcc to allocate more space from the heap.
12046 if (Is64Bit) {
12047 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12048 .addReg(sizeVReg);
12049 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12050 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12051 } else {
12052 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12053 .addImm(12);
12054 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12055 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12056 .addExternalSymbol("__morestack_allocate_stack_space");
12057 }
12058
12059 if (!Is64Bit)
12060 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12061 .addImm(16);
12062
12063 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12064 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12065 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12066
12067 // Set up the CFG correctly.
12068 BB->addSuccessor(bumpMBB);
12069 BB->addSuccessor(mallocMBB);
12070 mallocMBB->addSuccessor(continueMBB);
12071 bumpMBB->addSuccessor(continueMBB);
12072
12073 // Take care of the PHI nodes.
12074 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12075 MI->getOperand(0).getReg())
12076 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12077 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12078
12079 // Delete the original pseudo instruction.
12080 MI->eraseFromParent();
12081
12082 // And we're done.
12083 return continueMBB;
12084}
12085
12086MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012087X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012088 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012089 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12090 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012091
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012092 assert(!Subtarget->isTargetEnvMacho());
12093
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012094 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12095 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012096
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012097 if (Subtarget->isTargetWin64()) {
12098 if (Subtarget->isTargetCygMing()) {
12099 // ___chkstk(Mingw64):
12100 // Clobbers R10, R11, RAX and EFLAGS.
12101 // Updates RSP.
12102 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12103 .addExternalSymbol("___chkstk")
12104 .addReg(X86::RAX, RegState::Implicit)
12105 .addReg(X86::RSP, RegState::Implicit)
12106 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12107 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12108 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12109 } else {
12110 // __chkstk(MSVCRT): does not update stack pointer.
12111 // Clobbers R10, R11 and EFLAGS.
12112 // FIXME: RAX(allocated size) might be reused and not killed.
12113 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12114 .addExternalSymbol("__chkstk")
12115 .addReg(X86::RAX, RegState::Implicit)
12116 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12117 // RAX has the offset to subtracted from RSP.
12118 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12119 .addReg(X86::RSP)
12120 .addReg(X86::RAX);
12121 }
12122 } else {
12123 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012124 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12125
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012126 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12127 .addExternalSymbol(StackProbeSymbol)
12128 .addReg(X86::EAX, RegState::Implicit)
12129 .addReg(X86::ESP, RegState::Implicit)
12130 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12131 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12132 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12133 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012134
Dan Gohman14152b42010-07-06 20:24:04 +000012135 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012136 return BB;
12137}
Chris Lattner52600972009-09-02 05:57:00 +000012138
12139MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012140X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12141 MachineBasicBlock *BB) const {
12142 // This is pretty easy. We're taking the value that we received from
12143 // our load from the relocation, sticking it in either RDI (x86-64)
12144 // or EAX and doing an indirect call. The return value will then
12145 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012146 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012147 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012148 DebugLoc DL = MI->getDebugLoc();
12149 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012150
12151 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012152 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012153
Eric Christopher30ef0e52010-06-03 04:07:48 +000012154 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012155 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12156 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012157 .addReg(X86::RIP)
12158 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012159 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012160 MI->getOperand(3).getTargetFlags())
12161 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012162 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012163 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012164 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012165 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12166 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012167 .addReg(0)
12168 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012169 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012170 MI->getOperand(3).getTargetFlags())
12171 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012172 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012173 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012174 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012175 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12176 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012177 .addReg(TII->getGlobalBaseReg(F))
12178 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012179 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012180 MI->getOperand(3).getTargetFlags())
12181 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012182 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012183 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012184 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012185
Dan Gohman14152b42010-07-06 20:24:04 +000012186 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012187 return BB;
12188}
12189
12190MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012191X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012192 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012193 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000012194 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012195 case X86::TAILJMPd64:
12196 case X86::TAILJMPr64:
12197 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000012198 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012199 case X86::TCRETURNdi64:
12200 case X86::TCRETURNri64:
12201 case X86::TCRETURNmi64:
12202 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12203 // On AMD64, additional defs should be added before register allocation.
12204 if (!Subtarget->isTargetWin64()) {
12205 MI->addRegisterDefined(X86::RSI);
12206 MI->addRegisterDefined(X86::RDI);
12207 MI->addRegisterDefined(X86::XMM6);
12208 MI->addRegisterDefined(X86::XMM7);
12209 MI->addRegisterDefined(X86::XMM8);
12210 MI->addRegisterDefined(X86::XMM9);
12211 MI->addRegisterDefined(X86::XMM10);
12212 MI->addRegisterDefined(X86::XMM11);
12213 MI->addRegisterDefined(X86::XMM12);
12214 MI->addRegisterDefined(X86::XMM13);
12215 MI->addRegisterDefined(X86::XMM14);
12216 MI->addRegisterDefined(X86::XMM15);
12217 }
12218 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012219 case X86::WIN_ALLOCA:
12220 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012221 case X86::SEG_ALLOCA_32:
12222 return EmitLoweredSegAlloca(MI, BB, false);
12223 case X86::SEG_ALLOCA_64:
12224 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012225 case X86::TLSCall_32:
12226 case X86::TLSCall_64:
12227 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012228 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012229 case X86::CMOV_FR32:
12230 case X86::CMOV_FR64:
12231 case X86::CMOV_V4F32:
12232 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012233 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012234 case X86::CMOV_V8F32:
12235 case X86::CMOV_V4F64:
12236 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012237 case X86::CMOV_GR16:
12238 case X86::CMOV_GR32:
12239 case X86::CMOV_RFP32:
12240 case X86::CMOV_RFP64:
12241 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012242 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012243
Dale Johannesen849f2142007-07-03 00:53:03 +000012244 case X86::FP32_TO_INT16_IN_MEM:
12245 case X86::FP32_TO_INT32_IN_MEM:
12246 case X86::FP32_TO_INT64_IN_MEM:
12247 case X86::FP64_TO_INT16_IN_MEM:
12248 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012249 case X86::FP64_TO_INT64_IN_MEM:
12250 case X86::FP80_TO_INT16_IN_MEM:
12251 case X86::FP80_TO_INT32_IN_MEM:
12252 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012253 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12254 DebugLoc DL = MI->getDebugLoc();
12255
Evan Cheng60c07e12006-07-05 22:17:51 +000012256 // Change the floating point control register to use "round towards zero"
12257 // mode when truncating to an integer value.
12258 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012259 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012260 addFrameReference(BuildMI(*BB, MI, DL,
12261 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012262
12263 // Load the old value of the high byte of the control word...
12264 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012265 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012266 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012267 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012268
12269 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012270 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012271 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012272
12273 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012274 addFrameReference(BuildMI(*BB, MI, DL,
12275 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012276
12277 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012278 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012279 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012280
12281 // Get the X86 opcode to use.
12282 unsigned Opc;
12283 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012284 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012285 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12286 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12287 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12288 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12289 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12290 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012291 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12292 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12293 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012294 }
12295
12296 X86AddressMode AM;
12297 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012298 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012299 AM.BaseType = X86AddressMode::RegBase;
12300 AM.Base.Reg = Op.getReg();
12301 } else {
12302 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012303 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012304 }
12305 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012306 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012307 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012308 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012309 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012310 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012311 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012312 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012313 AM.GV = Op.getGlobal();
12314 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012315 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012316 }
Dan Gohman14152b42010-07-06 20:24:04 +000012317 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012318 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012319
12320 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012321 addFrameReference(BuildMI(*BB, MI, DL,
12322 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012323
Dan Gohman14152b42010-07-06 20:24:04 +000012324 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012325 return BB;
12326 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012327 // String/text processing lowering.
12328 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012329 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012330 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12331 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012332 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012333 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12334 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012335 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012336 return EmitPCMP(MI, BB, 5, false /* in mem */);
12337 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012338 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012339 return EmitPCMP(MI, BB, 5, true /* in mem */);
12340
Eric Christopher228232b2010-11-30 07:20:12 +000012341 // Thread synchronization.
12342 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012343 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012344 case X86::MWAIT:
12345 return EmitMwait(MI, BB);
12346
Eric Christopherb120ab42009-08-18 22:50:32 +000012347 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012348 case X86::ATOMAND32:
12349 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012350 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012351 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012352 X86::NOT32r, X86::EAX,
12353 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012354 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012355 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12356 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012357 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012358 X86::NOT32r, X86::EAX,
12359 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012360 case X86::ATOMXOR32:
12361 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012362 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012363 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012364 X86::NOT32r, X86::EAX,
12365 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012366 case X86::ATOMNAND32:
12367 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012368 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012369 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012370 X86::NOT32r, X86::EAX,
12371 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012372 case X86::ATOMMIN32:
12373 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12374 case X86::ATOMMAX32:
12375 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12376 case X86::ATOMUMIN32:
12377 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12378 case X86::ATOMUMAX32:
12379 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012380
12381 case X86::ATOMAND16:
12382 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12383 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012384 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012385 X86::NOT16r, X86::AX,
12386 X86::GR16RegisterClass);
12387 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012388 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012389 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012390 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012391 X86::NOT16r, X86::AX,
12392 X86::GR16RegisterClass);
12393 case X86::ATOMXOR16:
12394 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12395 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012396 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012397 X86::NOT16r, X86::AX,
12398 X86::GR16RegisterClass);
12399 case X86::ATOMNAND16:
12400 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12401 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012402 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012403 X86::NOT16r, X86::AX,
12404 X86::GR16RegisterClass, true);
12405 case X86::ATOMMIN16:
12406 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12407 case X86::ATOMMAX16:
12408 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12409 case X86::ATOMUMIN16:
12410 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12411 case X86::ATOMUMAX16:
12412 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12413
12414 case X86::ATOMAND8:
12415 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12416 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012417 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012418 X86::NOT8r, X86::AL,
12419 X86::GR8RegisterClass);
12420 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012421 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012422 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012423 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012424 X86::NOT8r, X86::AL,
12425 X86::GR8RegisterClass);
12426 case X86::ATOMXOR8:
12427 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12428 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012429 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012430 X86::NOT8r, X86::AL,
12431 X86::GR8RegisterClass);
12432 case X86::ATOMNAND8:
12433 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12434 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012435 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012436 X86::NOT8r, X86::AL,
12437 X86::GR8RegisterClass, true);
12438 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012439 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012440 case X86::ATOMAND64:
12441 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012442 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012443 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012444 X86::NOT64r, X86::RAX,
12445 X86::GR64RegisterClass);
12446 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012447 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12448 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012449 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012450 X86::NOT64r, X86::RAX,
12451 X86::GR64RegisterClass);
12452 case X86::ATOMXOR64:
12453 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012454 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012455 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012456 X86::NOT64r, X86::RAX,
12457 X86::GR64RegisterClass);
12458 case X86::ATOMNAND64:
12459 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12460 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012461 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012462 X86::NOT64r, X86::RAX,
12463 X86::GR64RegisterClass, true);
12464 case X86::ATOMMIN64:
12465 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12466 case X86::ATOMMAX64:
12467 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12468 case X86::ATOMUMIN64:
12469 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12470 case X86::ATOMUMAX64:
12471 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012472
12473 // This group does 64-bit operations on a 32-bit host.
12474 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012475 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012476 X86::AND32rr, X86::AND32rr,
12477 X86::AND32ri, X86::AND32ri,
12478 false);
12479 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012480 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012481 X86::OR32rr, X86::OR32rr,
12482 X86::OR32ri, X86::OR32ri,
12483 false);
12484 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012485 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012486 X86::XOR32rr, X86::XOR32rr,
12487 X86::XOR32ri, X86::XOR32ri,
12488 false);
12489 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012490 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012491 X86::AND32rr, X86::AND32rr,
12492 X86::AND32ri, X86::AND32ri,
12493 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012494 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012495 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012496 X86::ADD32rr, X86::ADC32rr,
12497 X86::ADD32ri, X86::ADC32ri,
12498 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012499 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012500 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012501 X86::SUB32rr, X86::SBB32rr,
12502 X86::SUB32ri, X86::SBB32ri,
12503 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012504 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012505 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012506 X86::MOV32rr, X86::MOV32rr,
12507 X86::MOV32ri, X86::MOV32ri,
12508 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012509 case X86::VASTART_SAVE_XMM_REGS:
12510 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012511
12512 case X86::VAARG_64:
12513 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012514 }
12515}
12516
12517//===----------------------------------------------------------------------===//
12518// X86 Optimization Hooks
12519//===----------------------------------------------------------------------===//
12520
Dan Gohman475871a2008-07-27 21:46:04 +000012521void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012522 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012523 APInt &KnownZero,
12524 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012525 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012526 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012527 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012528 assert((Opc >= ISD::BUILTIN_OP_END ||
12529 Opc == ISD::INTRINSIC_WO_CHAIN ||
12530 Opc == ISD::INTRINSIC_W_CHAIN ||
12531 Opc == ISD::INTRINSIC_VOID) &&
12532 "Should use MaskedValueIsZero if you don't know whether Op"
12533 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012534
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012535 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012536 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012537 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012538 case X86ISD::ADD:
12539 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012540 case X86ISD::ADC:
12541 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012542 case X86ISD::SMUL:
12543 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012544 case X86ISD::INC:
12545 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012546 case X86ISD::OR:
12547 case X86ISD::XOR:
12548 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012549 // These nodes' second result is a boolean.
12550 if (Op.getResNo() == 0)
12551 break;
12552 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012553 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012554 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12555 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012556 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012557 case ISD::INTRINSIC_WO_CHAIN: {
12558 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12559 unsigned NumLoBits = 0;
12560 switch (IntId) {
12561 default: break;
12562 case Intrinsic::x86_sse_movmsk_ps:
12563 case Intrinsic::x86_avx_movmsk_ps_256:
12564 case Intrinsic::x86_sse2_movmsk_pd:
12565 case Intrinsic::x86_avx_movmsk_pd_256:
12566 case Intrinsic::x86_mmx_pmovmskb:
12567 case Intrinsic::x86_sse2_pmovmskb_128: {
12568 // High bits of movmskp{s|d}, pmovmskb are known zero.
12569 switch (IntId) {
12570 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12571 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12572 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12573 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12574 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12575 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
12576 }
12577 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12578 Mask.getBitWidth() - NumLoBits);
12579 break;
12580 }
12581 }
12582 break;
12583 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012584 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012585}
Chris Lattner259e97c2006-01-31 19:43:35 +000012586
Owen Andersonbc146b02010-09-21 20:42:50 +000012587unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12588 unsigned Depth) const {
12589 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12590 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12591 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012592
Owen Andersonbc146b02010-09-21 20:42:50 +000012593 // Fallback case.
12594 return 1;
12595}
12596
Evan Cheng206ee9d2006-07-07 08:33:52 +000012597/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012598/// node is a GlobalAddress + offset.
12599bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012600 const GlobalValue* &GA,
12601 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012602 if (N->getOpcode() == X86ISD::Wrapper) {
12603 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012604 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012605 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012606 return true;
12607 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012608 }
Evan Chengad4196b2008-05-12 19:56:52 +000012609 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012610}
12611
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012612/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12613/// same as extracting the high 128-bit part of 256-bit vector and then
12614/// inserting the result into the low part of a new 256-bit vector
12615static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12616 EVT VT = SVOp->getValueType(0);
12617 int NumElems = VT.getVectorNumElements();
12618
12619 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12620 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12621 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12622 SVOp->getMaskElt(j) >= 0)
12623 return false;
12624
12625 return true;
12626}
12627
12628/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12629/// same as extracting the low 128-bit part of 256-bit vector and then
12630/// inserting the result into the high part of a new 256-bit vector
12631static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12632 EVT VT = SVOp->getValueType(0);
12633 int NumElems = VT.getVectorNumElements();
12634
12635 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12636 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12637 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12638 SVOp->getMaskElt(j) >= 0)
12639 return false;
12640
12641 return true;
12642}
12643
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012644/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12645static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12646 TargetLowering::DAGCombinerInfo &DCI) {
12647 DebugLoc dl = N->getDebugLoc();
12648 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12649 SDValue V1 = SVOp->getOperand(0);
12650 SDValue V2 = SVOp->getOperand(1);
12651 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012652 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012653
12654 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12655 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12656 //
12657 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012658 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012659 // V UNDEF BUILD_VECTOR UNDEF
12660 // \ / \ /
12661 // CONCAT_VECTOR CONCAT_VECTOR
12662 // \ /
12663 // \ /
12664 // RESULT: V + zero extended
12665 //
12666 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12667 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12668 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12669 return SDValue();
12670
12671 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12672 return SDValue();
12673
12674 // To match the shuffle mask, the first half of the mask should
12675 // be exactly the first vector, and all the rest a splat with the
12676 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012677 for (int i = 0; i < NumElems/2; ++i)
12678 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12679 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12680 return SDValue();
12681
12682 // Emit a zeroed vector and insert the desired subvector on its
12683 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012684 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012685 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12686 DAG.getConstant(0, MVT::i32), DAG, dl);
12687 return DCI.CombineTo(N, InsV);
12688 }
12689
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012690 //===--------------------------------------------------------------------===//
12691 // Combine some shuffles into subvector extracts and inserts:
12692 //
12693
12694 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12695 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12696 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12697 DAG, dl);
12698 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12699 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12700 return DCI.CombineTo(N, InsV);
12701 }
12702
12703 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12704 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12705 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12706 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12707 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12708 return DCI.CombineTo(N, InsV);
12709 }
12710
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012711 return SDValue();
12712}
12713
12714/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012715static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012716 TargetLowering::DAGCombinerInfo &DCI,
12717 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012718 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012719 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012720
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012721 // Don't create instructions with illegal types after legalize types has run.
12722 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12723 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12724 return SDValue();
12725
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012726 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12727 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12728 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012729 return PerformShuffleCombine256(N, DAG, DCI);
12730
12731 // Only handle 128 wide vector from here on.
12732 if (VT.getSizeInBits() != 128)
12733 return SDValue();
12734
12735 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12736 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12737 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012738 SmallVector<SDValue, 16> Elts;
12739 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012740 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012741
Nate Begemanfdea31a2010-03-24 20:49:50 +000012742 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012743}
Evan Chengd880b972008-05-09 21:53:03 +000012744
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012745/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12746/// generation and convert it from being a bunch of shuffles and extracts
12747/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012748static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12749 const TargetLowering &TLI) {
12750 SDValue InputVector = N->getOperand(0);
12751
12752 // Only operate on vectors of 4 elements, where the alternative shuffling
12753 // gets to be more expensive.
12754 if (InputVector.getValueType() != MVT::v4i32)
12755 return SDValue();
12756
12757 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12758 // single use which is a sign-extend or zero-extend, and all elements are
12759 // used.
12760 SmallVector<SDNode *, 4> Uses;
12761 unsigned ExtractedElements = 0;
12762 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12763 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12764 if (UI.getUse().getResNo() != InputVector.getResNo())
12765 return SDValue();
12766
12767 SDNode *Extract = *UI;
12768 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12769 return SDValue();
12770
12771 if (Extract->getValueType(0) != MVT::i32)
12772 return SDValue();
12773 if (!Extract->hasOneUse())
12774 return SDValue();
12775 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12776 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12777 return SDValue();
12778 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12779 return SDValue();
12780
12781 // Record which element was extracted.
12782 ExtractedElements |=
12783 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12784
12785 Uses.push_back(Extract);
12786 }
12787
12788 // If not all the elements were used, this may not be worthwhile.
12789 if (ExtractedElements != 15)
12790 return SDValue();
12791
12792 // Ok, we've now decided to do the transformation.
12793 DebugLoc dl = InputVector.getDebugLoc();
12794
12795 // Store the value to a temporary stack slot.
12796 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012797 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12798 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012799
12800 // Replace each use (extract) with a load of the appropriate element.
12801 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12802 UE = Uses.end(); UI != UE; ++UI) {
12803 SDNode *Extract = *UI;
12804
Nadav Rotem86694292011-05-17 08:31:57 +000012805 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012806 SDValue Idx = Extract->getOperand(1);
12807 unsigned EltSize =
12808 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12809 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12810 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12811
Nadav Rotem86694292011-05-17 08:31:57 +000012812 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012813 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012814
12815 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012816 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012817 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000012818 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012819
12820 // Replace the exact with the load.
12821 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12822 }
12823
12824 // The replacement was made in place; don't return anything.
12825 return SDValue();
12826}
12827
Duncan Sands6bcd2192011-09-17 16:49:39 +000012828/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
12829/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012830static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012831 const X86Subtarget *Subtarget) {
12832 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012833 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012834 // Get the LHS/RHS of the select.
12835 SDValue LHS = N->getOperand(1);
12836 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000012837 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000012838
Dan Gohman670e5392009-09-21 18:03:22 +000012839 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012840 // instructions match the semantics of the common C idiom x<y?x:y but not
12841 // x<=y?x:y, because of how they handle negative zero (which can be
12842 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000012843 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
12844 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
12845 (Subtarget->hasXMMInt() ||
12846 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012847 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012848
Chris Lattner47b4ce82009-03-11 05:48:52 +000012849 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012850 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012851 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12852 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012853 switch (CC) {
12854 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012855 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012856 // Converting this to a min would handle NaNs incorrectly, and swapping
12857 // the operands would cause it to handle comparisons between positive
12858 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012859 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012860 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012861 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12862 break;
12863 std::swap(LHS, RHS);
12864 }
Dan Gohman670e5392009-09-21 18:03:22 +000012865 Opcode = X86ISD::FMIN;
12866 break;
12867 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012868 // Converting this to a min would handle comparisons between positive
12869 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012870 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012871 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12872 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012873 Opcode = X86ISD::FMIN;
12874 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012875 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012876 // Converting this to a min would handle both negative zeros and NaNs
12877 // incorrectly, but we can swap the operands to fix both.
12878 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012879 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012880 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012881 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012882 Opcode = X86ISD::FMIN;
12883 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012884
Dan Gohman670e5392009-09-21 18:03:22 +000012885 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012886 // Converting this to a max would handle comparisons between positive
12887 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012888 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012889 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012890 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012891 Opcode = X86ISD::FMAX;
12892 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012893 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012894 // Converting this to a max would handle NaNs incorrectly, and swapping
12895 // the operands would cause it to handle comparisons between positive
12896 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012897 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012898 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012899 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12900 break;
12901 std::swap(LHS, RHS);
12902 }
Dan Gohman670e5392009-09-21 18:03:22 +000012903 Opcode = X86ISD::FMAX;
12904 break;
12905 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012906 // Converting this to a max would handle both negative zeros and NaNs
12907 // incorrectly, but we can swap the operands to fix both.
12908 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012909 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012910 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012911 case ISD::SETGE:
12912 Opcode = X86ISD::FMAX;
12913 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012914 }
Dan Gohman670e5392009-09-21 18:03:22 +000012915 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012916 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12917 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012918 switch (CC) {
12919 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012920 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012921 // Converting this to a min would handle comparisons between positive
12922 // and negative zero incorrectly, and swapping the operands would
12923 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012924 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012925 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012926 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012927 break;
12928 std::swap(LHS, RHS);
12929 }
Dan Gohman670e5392009-09-21 18:03:22 +000012930 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012931 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012932 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012933 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012934 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012935 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12936 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012937 Opcode = X86ISD::FMIN;
12938 break;
12939 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012940 // Converting this to a min would handle both negative zeros and NaNs
12941 // incorrectly, but we can swap the operands to fix both.
12942 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012943 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012944 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012945 case ISD::SETGE:
12946 Opcode = X86ISD::FMIN;
12947 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012948
Dan Gohman670e5392009-09-21 18:03:22 +000012949 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012950 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012951 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012952 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012953 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012954 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012955 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012956 // Converting this to a max would handle comparisons between positive
12957 // and negative zero incorrectly, and swapping the operands would
12958 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012959 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012960 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012961 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012962 break;
12963 std::swap(LHS, RHS);
12964 }
Dan Gohman670e5392009-09-21 18:03:22 +000012965 Opcode = X86ISD::FMAX;
12966 break;
12967 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012968 // Converting this to a max would handle both negative zeros and NaNs
12969 // incorrectly, but we can swap the operands to fix both.
12970 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012971 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012972 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012973 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012974 Opcode = X86ISD::FMAX;
12975 break;
12976 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012977 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012978
Chris Lattner47b4ce82009-03-11 05:48:52 +000012979 if (Opcode)
12980 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012981 }
Eric Christopherfd179292009-08-27 18:07:15 +000012982
Chris Lattnerd1980a52009-03-12 06:52:53 +000012983 // If this is a select between two integer constants, try to do some
12984 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012985 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12986 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012987 // Don't do this for crazy integer types.
12988 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12989 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012990 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012991 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012992
Chris Lattnercee56e72009-03-13 05:53:31 +000012993 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012994 // Efficiently invertible.
12995 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12996 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12997 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12998 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012999 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013000 }
Eric Christopherfd179292009-08-27 18:07:15 +000013001
Chris Lattnerd1980a52009-03-12 06:52:53 +000013002 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013003 if (FalseC->getAPIntValue() == 0 &&
13004 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013005 if (NeedsCondInvert) // Invert the condition if needed.
13006 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13007 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013008
Chris Lattnerd1980a52009-03-12 06:52:53 +000013009 // Zero extend the condition if needed.
13010 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013011
Chris Lattnercee56e72009-03-13 05:53:31 +000013012 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013013 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013014 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013015 }
Eric Christopherfd179292009-08-27 18:07:15 +000013016
Chris Lattner97a29a52009-03-13 05:22:11 +000013017 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013018 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013019 if (NeedsCondInvert) // Invert the condition if needed.
13020 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13021 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013022
Chris Lattner97a29a52009-03-13 05:22:11 +000013023 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013024 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13025 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013026 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013027 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013028 }
Eric Christopherfd179292009-08-27 18:07:15 +000013029
Chris Lattnercee56e72009-03-13 05:53:31 +000013030 // Optimize cases that will turn into an LEA instruction. This requires
13031 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013032 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013033 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013034 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013035
Chris Lattnercee56e72009-03-13 05:53:31 +000013036 bool isFastMultiplier = false;
13037 if (Diff < 10) {
13038 switch ((unsigned char)Diff) {
13039 default: break;
13040 case 1: // result = add base, cond
13041 case 2: // result = lea base( , cond*2)
13042 case 3: // result = lea base(cond, cond*2)
13043 case 4: // result = lea base( , cond*4)
13044 case 5: // result = lea base(cond, cond*4)
13045 case 8: // result = lea base( , cond*8)
13046 case 9: // result = lea base(cond, cond*8)
13047 isFastMultiplier = true;
13048 break;
13049 }
13050 }
Eric Christopherfd179292009-08-27 18:07:15 +000013051
Chris Lattnercee56e72009-03-13 05:53:31 +000013052 if (isFastMultiplier) {
13053 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13054 if (NeedsCondInvert) // Invert the condition if needed.
13055 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13056 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013057
Chris Lattnercee56e72009-03-13 05:53:31 +000013058 // Zero extend the condition if needed.
13059 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13060 Cond);
13061 // Scale the condition by the difference.
13062 if (Diff != 1)
13063 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13064 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013065
Chris Lattnercee56e72009-03-13 05:53:31 +000013066 // Add the base if non-zero.
13067 if (FalseC->getAPIntValue() != 0)
13068 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13069 SDValue(FalseC, 0));
13070 return Cond;
13071 }
Eric Christopherfd179292009-08-27 18:07:15 +000013072 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013073 }
13074 }
Eric Christopherfd179292009-08-27 18:07:15 +000013075
Dan Gohman475871a2008-07-27 21:46:04 +000013076 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013077}
13078
Chris Lattnerd1980a52009-03-12 06:52:53 +000013079/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13080static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13081 TargetLowering::DAGCombinerInfo &DCI) {
13082 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013083
Chris Lattnerd1980a52009-03-12 06:52:53 +000013084 // If the flag operand isn't dead, don't touch this CMOV.
13085 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13086 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013087
Evan Chengb5a55d92011-05-24 01:48:22 +000013088 SDValue FalseOp = N->getOperand(0);
13089 SDValue TrueOp = N->getOperand(1);
13090 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13091 SDValue Cond = N->getOperand(3);
13092 if (CC == X86::COND_E || CC == X86::COND_NE) {
13093 switch (Cond.getOpcode()) {
13094 default: break;
13095 case X86ISD::BSR:
13096 case X86ISD::BSF:
13097 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13098 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13099 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13100 }
13101 }
13102
Chris Lattnerd1980a52009-03-12 06:52:53 +000013103 // If this is a select between two integer constants, try to do some
13104 // optimizations. Note that the operands are ordered the opposite of SELECT
13105 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013106 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13107 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013108 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13109 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013110 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13111 CC = X86::GetOppositeBranchCondition(CC);
13112 std::swap(TrueC, FalseC);
13113 }
Eric Christopherfd179292009-08-27 18:07:15 +000013114
Chris Lattnerd1980a52009-03-12 06:52:53 +000013115 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013116 // This is efficient for any integer data type (including i8/i16) and
13117 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013118 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013119 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13120 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013121
Chris Lattnerd1980a52009-03-12 06:52:53 +000013122 // Zero extend the condition if needed.
13123 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013124
Chris Lattnerd1980a52009-03-12 06:52:53 +000013125 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13126 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013127 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013128 if (N->getNumValues() == 2) // Dead flag value?
13129 return DCI.CombineTo(N, Cond, SDValue());
13130 return Cond;
13131 }
Eric Christopherfd179292009-08-27 18:07:15 +000013132
Chris Lattnercee56e72009-03-13 05:53:31 +000013133 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13134 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013135 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013136 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13137 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013138
Chris Lattner97a29a52009-03-13 05:22:11 +000013139 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013140 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13141 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013142 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13143 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013144
Chris Lattner97a29a52009-03-13 05:22:11 +000013145 if (N->getNumValues() == 2) // Dead flag value?
13146 return DCI.CombineTo(N, Cond, SDValue());
13147 return Cond;
13148 }
Eric Christopherfd179292009-08-27 18:07:15 +000013149
Chris Lattnercee56e72009-03-13 05:53:31 +000013150 // Optimize cases that will turn into an LEA instruction. This requires
13151 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013152 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013153 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013154 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013155
Chris Lattnercee56e72009-03-13 05:53:31 +000013156 bool isFastMultiplier = false;
13157 if (Diff < 10) {
13158 switch ((unsigned char)Diff) {
13159 default: break;
13160 case 1: // result = add base, cond
13161 case 2: // result = lea base( , cond*2)
13162 case 3: // result = lea base(cond, cond*2)
13163 case 4: // result = lea base( , cond*4)
13164 case 5: // result = lea base(cond, cond*4)
13165 case 8: // result = lea base( , cond*8)
13166 case 9: // result = lea base(cond, cond*8)
13167 isFastMultiplier = true;
13168 break;
13169 }
13170 }
Eric Christopherfd179292009-08-27 18:07:15 +000013171
Chris Lattnercee56e72009-03-13 05:53:31 +000013172 if (isFastMultiplier) {
13173 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013174 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13175 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013176 // Zero extend the condition if needed.
13177 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13178 Cond);
13179 // Scale the condition by the difference.
13180 if (Diff != 1)
13181 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13182 DAG.getConstant(Diff, Cond.getValueType()));
13183
13184 // Add the base if non-zero.
13185 if (FalseC->getAPIntValue() != 0)
13186 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13187 SDValue(FalseC, 0));
13188 if (N->getNumValues() == 2) // Dead flag value?
13189 return DCI.CombineTo(N, Cond, SDValue());
13190 return Cond;
13191 }
Eric Christopherfd179292009-08-27 18:07:15 +000013192 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013193 }
13194 }
13195 return SDValue();
13196}
13197
13198
Evan Cheng0b0cd912009-03-28 05:57:29 +000013199/// PerformMulCombine - Optimize a single multiply with constant into two
13200/// in order to implement it with two cheaper instructions, e.g.
13201/// LEA + SHL, LEA + LEA.
13202static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13203 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013204 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13205 return SDValue();
13206
Owen Andersone50ed302009-08-10 22:56:29 +000013207 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013208 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013209 return SDValue();
13210
13211 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13212 if (!C)
13213 return SDValue();
13214 uint64_t MulAmt = C->getZExtValue();
13215 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13216 return SDValue();
13217
13218 uint64_t MulAmt1 = 0;
13219 uint64_t MulAmt2 = 0;
13220 if ((MulAmt % 9) == 0) {
13221 MulAmt1 = 9;
13222 MulAmt2 = MulAmt / 9;
13223 } else if ((MulAmt % 5) == 0) {
13224 MulAmt1 = 5;
13225 MulAmt2 = MulAmt / 5;
13226 } else if ((MulAmt % 3) == 0) {
13227 MulAmt1 = 3;
13228 MulAmt2 = MulAmt / 3;
13229 }
13230 if (MulAmt2 &&
13231 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13232 DebugLoc DL = N->getDebugLoc();
13233
13234 if (isPowerOf2_64(MulAmt2) &&
13235 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13236 // If second multiplifer is pow2, issue it first. We want the multiply by
13237 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13238 // is an add.
13239 std::swap(MulAmt1, MulAmt2);
13240
13241 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013242 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013243 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013244 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013245 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013246 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013247 DAG.getConstant(MulAmt1, VT));
13248
Eric Christopherfd179292009-08-27 18:07:15 +000013249 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013250 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013251 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013252 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013253 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013254 DAG.getConstant(MulAmt2, VT));
13255
13256 // Do not add new nodes to DAG combiner worklist.
13257 DCI.CombineTo(N, NewMul, false);
13258 }
13259 return SDValue();
13260}
13261
Evan Chengad9c0a32009-12-15 00:53:42 +000013262static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13263 SDValue N0 = N->getOperand(0);
13264 SDValue N1 = N->getOperand(1);
13265 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13266 EVT VT = N0.getValueType();
13267
13268 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13269 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013270 if (VT.isInteger() && !VT.isVector() &&
13271 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013272 N0.getOperand(1).getOpcode() == ISD::Constant) {
13273 SDValue N00 = N0.getOperand(0);
13274 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13275 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13276 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13277 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13278 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13279 APInt ShAmt = N1C->getAPIntValue();
13280 Mask = Mask.shl(ShAmt);
13281 if (Mask != 0)
13282 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13283 N00, DAG.getConstant(Mask, VT));
13284 }
13285 }
13286
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013287
13288 // Hardware support for vector shifts is sparse which makes us scalarize the
13289 // vector operations in many cases. Also, on sandybridge ADD is faster than
13290 // shl.
13291 // (shl V, 1) -> add V,V
13292 if (isSplatVector(N1.getNode())) {
13293 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13294 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13295 // We shift all of the values by one. In many cases we do not have
13296 // hardware support for this operation. This is better expressed as an ADD
13297 // of two values.
13298 if (N1C && (1 == N1C->getZExtValue())) {
13299 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13300 }
13301 }
13302
Evan Chengad9c0a32009-12-15 00:53:42 +000013303 return SDValue();
13304}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013305
Nate Begeman740ab032009-01-26 00:52:55 +000013306/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13307/// when possible.
13308static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13309 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013310 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013311 if (N->getOpcode() == ISD::SHL) {
13312 SDValue V = PerformSHLCombine(N, DAG);
13313 if (V.getNode()) return V;
13314 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013315
Nate Begeman740ab032009-01-26 00:52:55 +000013316 // On X86 with SSE2 support, we can transform this to a vector shift if
13317 // all elements are shifted by the same amount. We can't do this in legalize
13318 // because the a constant vector is typically transformed to a constant pool
13319 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013320 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013321 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013322
Craig Topper7be5dfd2011-11-12 09:58:49 +000013323 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
13324 (!Subtarget->hasAVX2() ||
13325 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013326 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013327
Mon P Wang3becd092009-01-28 08:12:05 +000013328 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013329 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013330 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013331 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013332 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13333 unsigned NumElts = VT.getVectorNumElements();
13334 unsigned i = 0;
13335 for (; i != NumElts; ++i) {
13336 SDValue Arg = ShAmtOp.getOperand(i);
13337 if (Arg.getOpcode() == ISD::UNDEF) continue;
13338 BaseShAmt = Arg;
13339 break;
13340 }
13341 for (; i != NumElts; ++i) {
13342 SDValue Arg = ShAmtOp.getOperand(i);
13343 if (Arg.getOpcode() == ISD::UNDEF) continue;
13344 if (Arg != BaseShAmt) {
13345 return SDValue();
13346 }
13347 }
13348 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013349 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013350 SDValue InVec = ShAmtOp.getOperand(0);
13351 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13352 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13353 unsigned i = 0;
13354 for (; i != NumElts; ++i) {
13355 SDValue Arg = InVec.getOperand(i);
13356 if (Arg.getOpcode() == ISD::UNDEF) continue;
13357 BaseShAmt = Arg;
13358 break;
13359 }
13360 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13361 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013362 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013363 if (C->getZExtValue() == SplatIdx)
13364 BaseShAmt = InVec.getOperand(1);
13365 }
13366 }
13367 if (BaseShAmt.getNode() == 0)
13368 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13369 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013370 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013371 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013372
Mon P Wangefa42202009-09-03 19:56:25 +000013373 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013374 if (EltVT.bitsGT(MVT::i32))
13375 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13376 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013377 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013378
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013379 // The shift amount is identical so we can do a vector shift.
13380 SDValue ValOp = N->getOperand(0);
13381 switch (N->getOpcode()) {
13382 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013383 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013384 break;
13385 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013386 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013387 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013388 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013389 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013390 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013391 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013392 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013393 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013394 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013395 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013396 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013397 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013398 if (VT == MVT::v4i64)
13399 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13400 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
13401 ValOp, BaseShAmt);
13402 if (VT == MVT::v8i32)
13403 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13404 DAG.getConstant(Intrinsic::x86_avx2_pslli_d, MVT::i32),
13405 ValOp, BaseShAmt);
13406 if (VT == MVT::v16i16)
13407 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13408 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
13409 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013410 break;
13411 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013412 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013413 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013414 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013415 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013416 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013417 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013418 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013419 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013420 if (VT == MVT::v8i32)
13421 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13422 DAG.getConstant(Intrinsic::x86_avx2_psrai_d, MVT::i32),
13423 ValOp, BaseShAmt);
13424 if (VT == MVT::v16i16)
13425 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13426 DAG.getConstant(Intrinsic::x86_avx2_psrai_w, MVT::i32),
13427 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013428 break;
13429 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013430 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013431 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013432 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013433 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013434 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013435 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013436 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013437 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013438 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013439 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013440 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013441 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013442 if (VT == MVT::v4i64)
13443 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13444 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
13445 ValOp, BaseShAmt);
13446 if (VT == MVT::v8i32)
13447 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13448 DAG.getConstant(Intrinsic::x86_avx2_psrli_d, MVT::i32),
13449 ValOp, BaseShAmt);
13450 if (VT == MVT::v16i16)
13451 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13452 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
13453 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013454 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013455 }
13456 return SDValue();
13457}
13458
Nate Begemanb65c1752010-12-17 22:55:37 +000013459
Stuart Hastings865f0932011-06-03 23:53:54 +000013460// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13461// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13462// and friends. Likewise for OR -> CMPNEQSS.
13463static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13464 TargetLowering::DAGCombinerInfo &DCI,
13465 const X86Subtarget *Subtarget) {
13466 unsigned opcode;
13467
13468 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13469 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013470 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013471 SDValue N0 = N->getOperand(0);
13472 SDValue N1 = N->getOperand(1);
13473 SDValue CMP0 = N0->getOperand(1);
13474 SDValue CMP1 = N1->getOperand(1);
13475 DebugLoc DL = N->getDebugLoc();
13476
13477 // The SETCCs should both refer to the same CMP.
13478 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13479 return SDValue();
13480
13481 SDValue CMP00 = CMP0->getOperand(0);
13482 SDValue CMP01 = CMP0->getOperand(1);
13483 EVT VT = CMP00.getValueType();
13484
13485 if (VT == MVT::f32 || VT == MVT::f64) {
13486 bool ExpectingFlags = false;
13487 // Check for any users that want flags:
13488 for (SDNode::use_iterator UI = N->use_begin(),
13489 UE = N->use_end();
13490 !ExpectingFlags && UI != UE; ++UI)
13491 switch (UI->getOpcode()) {
13492 default:
13493 case ISD::BR_CC:
13494 case ISD::BRCOND:
13495 case ISD::SELECT:
13496 ExpectingFlags = true;
13497 break;
13498 case ISD::CopyToReg:
13499 case ISD::SIGN_EXTEND:
13500 case ISD::ZERO_EXTEND:
13501 case ISD::ANY_EXTEND:
13502 break;
13503 }
13504
13505 if (!ExpectingFlags) {
13506 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13507 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13508
13509 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13510 X86::CondCode tmp = cc0;
13511 cc0 = cc1;
13512 cc1 = tmp;
13513 }
13514
13515 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13516 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13517 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13518 X86ISD::NodeType NTOperator = is64BitFP ?
13519 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13520 // FIXME: need symbolic constants for these magic numbers.
13521 // See X86ATTInstPrinter.cpp:printSSECC().
13522 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13523 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13524 DAG.getConstant(x86cc, MVT::i8));
13525 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13526 OnesOrZeroesF);
13527 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13528 DAG.getConstant(1, MVT::i32));
13529 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13530 return OneBitOfTruth;
13531 }
13532 }
13533 }
13534 }
13535 return SDValue();
13536}
13537
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013538/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13539/// so it can be folded inside ANDNP.
13540static bool CanFoldXORWithAllOnes(const SDNode *N) {
13541 EVT VT = N->getValueType(0);
13542
13543 // Match direct AllOnes for 128 and 256-bit vectors
13544 if (ISD::isBuildVectorAllOnes(N))
13545 return true;
13546
13547 // Look through a bit convert.
13548 if (N->getOpcode() == ISD::BITCAST)
13549 N = N->getOperand(0).getNode();
13550
13551 // Sometimes the operand may come from a insert_subvector building a 256-bit
13552 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013553 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013554 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13555 SDValue V1 = N->getOperand(0);
13556 SDValue V2 = N->getOperand(1);
13557
13558 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13559 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13560 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13561 ISD::isBuildVectorAllOnes(V2.getNode()))
13562 return true;
13563 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013564
13565 return false;
13566}
13567
Nate Begemanb65c1752010-12-17 22:55:37 +000013568static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13569 TargetLowering::DAGCombinerInfo &DCI,
13570 const X86Subtarget *Subtarget) {
13571 if (DCI.isBeforeLegalizeOps())
13572 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013573
Stuart Hastings865f0932011-06-03 23:53:54 +000013574 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13575 if (R.getNode())
13576 return R;
13577
Craig Topper54a11172011-10-14 07:06:56 +000013578 EVT VT = N->getValueType(0);
13579
Craig Topperb4c94572011-10-21 06:55:01 +000013580 // Create ANDN, BLSI, and BLSR instructions
13581 // BLSI is X & (-X)
13582 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013583 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13584 SDValue N0 = N->getOperand(0);
13585 SDValue N1 = N->getOperand(1);
13586 DebugLoc DL = N->getDebugLoc();
13587
13588 // Check LHS for not
13589 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13590 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13591 // Check RHS for not
13592 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13593 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13594
Craig Topperb4c94572011-10-21 06:55:01 +000013595 // Check LHS for neg
13596 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13597 isZero(N0.getOperand(0)))
13598 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13599
13600 // Check RHS for neg
13601 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13602 isZero(N1.getOperand(0)))
13603 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13604
13605 // Check LHS for X-1
13606 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13607 isAllOnes(N0.getOperand(1)))
13608 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13609
13610 // Check RHS for X-1
13611 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13612 isAllOnes(N1.getOperand(1)))
13613 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13614
Craig Topper54a11172011-10-14 07:06:56 +000013615 return SDValue();
13616 }
13617
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013618 // Want to form ANDNP nodes:
13619 // 1) In the hopes of then easily combining them with OR and AND nodes
13620 // to form PBLEND/PSIGN.
13621 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013622 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013623 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013624
Nate Begemanb65c1752010-12-17 22:55:37 +000013625 SDValue N0 = N->getOperand(0);
13626 SDValue N1 = N->getOperand(1);
13627 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013628
Nate Begemanb65c1752010-12-17 22:55:37 +000013629 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013630 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013631 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13632 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013633 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013634
13635 // Check RHS for vnot
13636 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013637 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13638 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013639 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013640
Nate Begemanb65c1752010-12-17 22:55:37 +000013641 return SDValue();
13642}
13643
Evan Cheng760d1942010-01-04 21:22:48 +000013644static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013645 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013646 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013647 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013648 return SDValue();
13649
Stuart Hastings865f0932011-06-03 23:53:54 +000013650 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13651 if (R.getNode())
13652 return R;
13653
Evan Cheng760d1942010-01-04 21:22:48 +000013654 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000013655
Evan Cheng760d1942010-01-04 21:22:48 +000013656 SDValue N0 = N->getOperand(0);
13657 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013658
Nate Begemanb65c1752010-12-17 22:55:37 +000013659 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000013660 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperc0d82852011-11-22 00:44:41 +000013661 if (!Subtarget->hasSSSE3orAVX() ||
Craig Topper1666cb62011-11-19 07:07:26 +000013662 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
13663 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013664
Craig Topper1666cb62011-11-19 07:07:26 +000013665 // Canonicalize pandn to RHS
13666 if (N0.getOpcode() == X86ISD::ANDNP)
13667 std::swap(N0, N1);
13668 // or (and (m, x), (pandn m, y))
13669 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
13670 SDValue Mask = N1.getOperand(0);
13671 SDValue X = N1.getOperand(1);
13672 SDValue Y;
13673 if (N0.getOperand(0) == Mask)
13674 Y = N0.getOperand(1);
13675 if (N0.getOperand(1) == Mask)
13676 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013677
Craig Topper1666cb62011-11-19 07:07:26 +000013678 // Check to see if the mask appeared in both the AND and ANDNP and
13679 if (!Y.getNode())
13680 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013681
Craig Topper1666cb62011-11-19 07:07:26 +000013682 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13683 if (Mask.getOpcode() != ISD::BITCAST ||
13684 X.getOpcode() != ISD::BITCAST ||
13685 Y.getOpcode() != ISD::BITCAST)
13686 return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013687
Craig Topper1666cb62011-11-19 07:07:26 +000013688 // Look through mask bitcast.
13689 Mask = Mask.getOperand(0);
13690 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013691
Craig Topper1666cb62011-11-19 07:07:26 +000013692 // Validate that the Mask operand is a vector sra node. The sra node
13693 // will be an intrinsic.
13694 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13695 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013696
Craig Topper1666cb62011-11-19 07:07:26 +000013697 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13698 // there is no psrai.b
13699 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13700 case Intrinsic::x86_sse2_psrai_w:
13701 case Intrinsic::x86_sse2_psrai_d:
13702 case Intrinsic::x86_avx2_psrai_w:
13703 case Intrinsic::x86_avx2_psrai_d:
13704 break;
13705 default: return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013706 }
Craig Topper1666cb62011-11-19 07:07:26 +000013707
13708 // Check that the SRA is all signbits.
13709 SDValue SraC = Mask.getOperand(2);
13710 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13711 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13712 if ((SraAmt + 1) != EltBits)
13713 return SDValue();
13714
13715 DebugLoc DL = N->getDebugLoc();
13716
13717 // Now we know we at least have a plendvb with the mask val. See if
13718 // we can form a psignb/w/d.
13719 // psign = x.type == y.type == mask.type && y = sub(0, x);
13720 X = X.getOperand(0);
13721 Y = Y.getOperand(0);
13722 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13723 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Topper31133842011-11-19 07:33:10 +000013724 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType() &&
13725 (EltBits == 8 || EltBits == 16 || EltBits == 32)) {
13726 SDValue Sign = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X,
13727 Mask.getOperand(1));
13728 return DAG.getNode(ISD::BITCAST, DL, VT, Sign);
Craig Topper1666cb62011-11-19 07:07:26 +000013729 }
13730 // PBLENDVB only available on SSE 4.1
Craig Topperc0d82852011-11-22 00:44:41 +000013731 if (!Subtarget->hasSSE41orAVX())
Craig Topper1666cb62011-11-19 07:07:26 +000013732 return SDValue();
13733
13734 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
13735
13736 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
13737 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
13738 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000013739 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000013740 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000013741 }
13742 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013743
Craig Topper1666cb62011-11-19 07:07:26 +000013744 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
13745 return SDValue();
13746
Nate Begemanb65c1752010-12-17 22:55:37 +000013747 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013748 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13749 std::swap(N0, N1);
13750 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13751 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013752 if (!N0.hasOneUse() || !N1.hasOneUse())
13753 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013754
13755 SDValue ShAmt0 = N0.getOperand(1);
13756 if (ShAmt0.getValueType() != MVT::i8)
13757 return SDValue();
13758 SDValue ShAmt1 = N1.getOperand(1);
13759 if (ShAmt1.getValueType() != MVT::i8)
13760 return SDValue();
13761 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13762 ShAmt0 = ShAmt0.getOperand(0);
13763 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13764 ShAmt1 = ShAmt1.getOperand(0);
13765
13766 DebugLoc DL = N->getDebugLoc();
13767 unsigned Opc = X86ISD::SHLD;
13768 SDValue Op0 = N0.getOperand(0);
13769 SDValue Op1 = N1.getOperand(0);
13770 if (ShAmt0.getOpcode() == ISD::SUB) {
13771 Opc = X86ISD::SHRD;
13772 std::swap(Op0, Op1);
13773 std::swap(ShAmt0, ShAmt1);
13774 }
13775
Evan Cheng8b1190a2010-04-28 01:18:01 +000013776 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013777 if (ShAmt1.getOpcode() == ISD::SUB) {
13778 SDValue Sum = ShAmt1.getOperand(0);
13779 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013780 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13781 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13782 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13783 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013784 return DAG.getNode(Opc, DL, VT,
13785 Op0, Op1,
13786 DAG.getNode(ISD::TRUNCATE, DL,
13787 MVT::i8, ShAmt0));
13788 }
13789 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13790 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13791 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013792 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013793 return DAG.getNode(Opc, DL, VT,
13794 N0.getOperand(0), N1.getOperand(0),
13795 DAG.getNode(ISD::TRUNCATE, DL,
13796 MVT::i8, ShAmt0));
13797 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013798
Evan Cheng760d1942010-01-04 21:22:48 +000013799 return SDValue();
13800}
13801
Craig Topperb4c94572011-10-21 06:55:01 +000013802static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
13803 TargetLowering::DAGCombinerInfo &DCI,
13804 const X86Subtarget *Subtarget) {
13805 if (DCI.isBeforeLegalizeOps())
13806 return SDValue();
13807
13808 EVT VT = N->getValueType(0);
13809
13810 if (VT != MVT::i32 && VT != MVT::i64)
13811 return SDValue();
13812
13813 // Create BLSMSK instructions by finding X ^ (X-1)
13814 SDValue N0 = N->getOperand(0);
13815 SDValue N1 = N->getOperand(1);
13816 DebugLoc DL = N->getDebugLoc();
13817
13818 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13819 isAllOnes(N0.getOperand(1)))
13820 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
13821
13822 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13823 isAllOnes(N1.getOperand(1)))
13824 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
13825
13826 return SDValue();
13827}
13828
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013829/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
13830static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
13831 const X86Subtarget *Subtarget) {
13832 LoadSDNode *Ld = cast<LoadSDNode>(N);
13833 EVT RegVT = Ld->getValueType(0);
13834 EVT MemVT = Ld->getMemoryVT();
13835 DebugLoc dl = Ld->getDebugLoc();
13836 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13837
13838 ISD::LoadExtType Ext = Ld->getExtensionType();
13839
Nadav Rotemca6f2962011-09-18 19:00:23 +000013840 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013841 // shuffle. We need SSE4 for the shuffles.
13842 // TODO: It is possible to support ZExt by zeroing the undef values
13843 // during the shuffle phase or after the shuffle.
13844 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
13845 assert(MemVT != RegVT && "Cannot extend to the same type");
13846 assert(MemVT.isVector() && "Must load a vector from memory");
13847
13848 unsigned NumElems = RegVT.getVectorNumElements();
13849 unsigned RegSz = RegVT.getSizeInBits();
13850 unsigned MemSz = MemVT.getSizeInBits();
13851 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000013852 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013853 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
13854
13855 // Attempt to load the original value using a single load op.
13856 // Find a scalar type which is equal to the loaded word size.
13857 MVT SclrLoadTy = MVT::i8;
13858 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13859 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13860 MVT Tp = (MVT::SimpleValueType)tp;
13861 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
13862 SclrLoadTy = Tp;
13863 break;
13864 }
13865 }
13866
13867 // Proceed if a load word is found.
13868 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
13869
13870 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
13871 RegSz/SclrLoadTy.getSizeInBits());
13872
13873 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
13874 RegSz/MemVT.getScalarType().getSizeInBits());
13875 // Can't shuffle using an illegal type.
13876 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13877
13878 // Perform a single load.
13879 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
13880 Ld->getBasePtr(),
13881 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013882 Ld->isNonTemporal(), Ld->isInvariant(),
13883 Ld->getAlignment());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013884
13885 // Insert the word loaded into a vector.
13886 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
13887 LoadUnitVecVT, ScalarLoad);
13888
13889 // Bitcast the loaded value to a vector of the original element type, in
13890 // the size of the target vector type.
13891 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
13892 unsigned SizeRatio = RegSz/MemSz;
13893
13894 // Redistribute the loaded elements into the different locations.
13895 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13896 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
13897
13898 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
13899 DAG.getUNDEF(SlicedVec.getValueType()),
13900 ShuffleVec.data());
13901
13902 // Bitcast to the requested type.
13903 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
13904 // Replace the original load with the new sequence
13905 // and return the new chain.
13906 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
13907 return SDValue(ScalarLoad.getNode(), 1);
13908 }
13909
13910 return SDValue();
13911}
13912
Chris Lattner149a4e52008-02-22 02:09:43 +000013913/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013914static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013915 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013916 StoreSDNode *St = cast<StoreSDNode>(N);
13917 EVT VT = St->getValue().getValueType();
13918 EVT StVT = St->getMemoryVT();
13919 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013920 SDValue StoredVal = St->getOperand(1);
13921 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13922
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013923 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013924 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13925 // 128-bit ones. If in the future the cost becomes only one memory access the
13926 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013927 if (VT.getSizeInBits() == 256 &&
13928 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13929 StoredVal.getNumOperands() == 2) {
13930
13931 SDValue Value0 = StoredVal.getOperand(0);
13932 SDValue Value1 = StoredVal.getOperand(1);
13933
13934 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13935 SDValue Ptr0 = St->getBasePtr();
13936 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13937
13938 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13939 St->getPointerInfo(), St->isVolatile(),
13940 St->isNonTemporal(), St->getAlignment());
13941 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13942 St->getPointerInfo(), St->isVolatile(),
13943 St->isNonTemporal(), St->getAlignment());
13944 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13945 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013946
13947 // Optimize trunc store (of multiple scalars) to shuffle and store.
13948 // First, pack all of the elements in one place. Next, store to memory
13949 // in fewer chunks.
13950 if (St->isTruncatingStore() && VT.isVector()) {
13951 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13952 unsigned NumElems = VT.getVectorNumElements();
13953 assert(StVT != VT && "Cannot truncate to the same type");
13954 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13955 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13956
13957 // From, To sizes and ElemCount must be pow of two
13958 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013959 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000013960 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013961 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013962
Nadav Rotem614061b2011-08-10 19:30:14 +000013963 unsigned SizeRatio = FromSz / ToSz;
13964
13965 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13966
13967 // Create a type on which we perform the shuffle
13968 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13969 StVT.getScalarType(), NumElems*SizeRatio);
13970
13971 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13972
13973 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13974 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13975 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13976
13977 // Can't shuffle using an illegal type
13978 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13979
13980 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13981 DAG.getUNDEF(WideVec.getValueType()),
13982 ShuffleVec.data());
13983 // At this point all of the data is stored at the bottom of the
13984 // register. We now need to save it to mem.
13985
13986 // Find the largest store unit
13987 MVT StoreType = MVT::i8;
13988 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13989 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13990 MVT Tp = (MVT::SimpleValueType)tp;
13991 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13992 StoreType = Tp;
13993 }
13994
13995 // Bitcast the original vector into a vector of store-size units
13996 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13997 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13998 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13999 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
14000 SmallVector<SDValue, 8> Chains;
14001 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
14002 TLI.getPointerTy());
14003 SDValue Ptr = St->getBasePtr();
14004
14005 // Perform one or more big stores into memory.
14006 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
14007 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14008 StoreType, ShuffWide,
14009 DAG.getIntPtrConstant(i));
14010 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14011 St->getPointerInfo(), St->isVolatile(),
14012 St->isNonTemporal(), St->getAlignment());
14013 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14014 Chains.push_back(Ch);
14015 }
14016
14017 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14018 Chains.size());
14019 }
14020
14021
Chris Lattner149a4e52008-02-22 02:09:43 +000014022 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14023 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014024 // A preferable solution to the general problem is to figure out the right
14025 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014026
14027 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014028 if (VT.getSizeInBits() != 64)
14029 return SDValue();
14030
Devang Patel578efa92009-06-05 21:57:13 +000014031 const Function *F = DAG.getMachineFunction().getFunction();
14032 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014033 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000014034 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000014035 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014036 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014037 isa<LoadSDNode>(St->getValue()) &&
14038 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14039 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014040 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014041 LoadSDNode *Ld = 0;
14042 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014043 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014044 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014045 // Must be a store of a load. We currently handle two cases: the load
14046 // is a direct child, and it's under an intervening TokenFactor. It is
14047 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014048 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014049 Ld = cast<LoadSDNode>(St->getChain());
14050 else if (St->getValue().hasOneUse() &&
14051 ChainVal->getOpcode() == ISD::TokenFactor) {
14052 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014053 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014054 TokenFactorIndex = i;
14055 Ld = cast<LoadSDNode>(St->getValue());
14056 } else
14057 Ops.push_back(ChainVal->getOperand(i));
14058 }
14059 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014060
Evan Cheng536e6672009-03-12 05:59:15 +000014061 if (!Ld || !ISD::isNormalLoad(Ld))
14062 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014063
Evan Cheng536e6672009-03-12 05:59:15 +000014064 // If this is not the MMX case, i.e. we are just turning i64 load/store
14065 // into f64 load/store, avoid the transformation if there are multiple
14066 // uses of the loaded value.
14067 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14068 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014069
Evan Cheng536e6672009-03-12 05:59:15 +000014070 DebugLoc LdDL = Ld->getDebugLoc();
14071 DebugLoc StDL = N->getDebugLoc();
14072 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14073 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14074 // pair instead.
14075 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014076 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014077 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14078 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014079 Ld->isNonTemporal(), Ld->isInvariant(),
14080 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014081 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014082 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014083 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014084 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014085 Ops.size());
14086 }
Evan Cheng536e6672009-03-12 05:59:15 +000014087 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014088 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014089 St->isVolatile(), St->isNonTemporal(),
14090 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014091 }
Evan Cheng536e6672009-03-12 05:59:15 +000014092
14093 // Otherwise, lower to two pairs of 32-bit loads / stores.
14094 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014095 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14096 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014097
Owen Anderson825b72b2009-08-11 20:47:22 +000014098 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014099 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014100 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014101 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014102 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014103 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014104 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014105 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014106 MinAlign(Ld->getAlignment(), 4));
14107
14108 SDValue NewChain = LoLd.getValue(1);
14109 if (TokenFactorIndex != -1) {
14110 Ops.push_back(LoLd);
14111 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014112 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014113 Ops.size());
14114 }
14115
14116 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014117 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14118 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014119
14120 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014121 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014122 St->isVolatile(), St->isNonTemporal(),
14123 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014124 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014125 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014126 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014127 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014128 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014129 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014130 }
Dan Gohman475871a2008-07-27 21:46:04 +000014131 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014132}
14133
Duncan Sands17470be2011-09-22 20:15:48 +000014134/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14135/// and return the operands for the horizontal operation in LHS and RHS. A
14136/// horizontal operation performs the binary operation on successive elements
14137/// of its first operand, then on successive elements of its second operand,
14138/// returning the resulting values in a vector. For example, if
14139/// A = < float a0, float a1, float a2, float a3 >
14140/// and
14141/// B = < float b0, float b1, float b2, float b3 >
14142/// then the result of doing a horizontal operation on A and B is
14143/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14144/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14145/// A horizontal-op B, for some already available A and B, and if so then LHS is
14146/// set to A, RHS to B, and the routine returns 'true'.
14147/// Note that the binary operation should have the property that if one of the
14148/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014149static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000014150 // Look for the following pattern: if
14151 // A = < float a0, float a1, float a2, float a3 >
14152 // B = < float b0, float b1, float b2, float b3 >
14153 // and
14154 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14155 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14156 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14157 // which is A horizontal-op B.
14158
14159 // At least one of the operands should be a vector shuffle.
14160 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14161 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14162 return false;
14163
14164 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000014165
14166 assert((VT.is128BitVector() || VT.is256BitVector()) &&
14167 "Unsupported vector type for horizontal add/sub");
14168
14169 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
14170 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000014171 unsigned NumElts = VT.getVectorNumElements();
14172 unsigned NumLanes = VT.getSizeInBits()/128;
14173 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000014174 assert((NumLaneElts % 2 == 0) &&
14175 "Vector type should have an even number of elements in each lane");
14176 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000014177
14178 // View LHS in the form
14179 // LHS = VECTOR_SHUFFLE A, B, LMask
14180 // If LHS is not a shuffle then pretend it is the shuffle
14181 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14182 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14183 // type VT.
14184 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000014185 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014186 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14187 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14188 A = LHS.getOperand(0);
14189 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14190 B = LHS.getOperand(1);
14191 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
14192 } else {
14193 if (LHS.getOpcode() != ISD::UNDEF)
14194 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014195 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014196 LMask[i] = i;
14197 }
14198
14199 // Likewise, view RHS in the form
14200 // RHS = VECTOR_SHUFFLE C, D, RMask
14201 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000014202 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014203 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14204 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14205 C = RHS.getOperand(0);
14206 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14207 D = RHS.getOperand(1);
14208 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
14209 } else {
14210 if (RHS.getOpcode() != ISD::UNDEF)
14211 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014212 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014213 RMask[i] = i;
14214 }
14215
14216 // Check that the shuffles are both shuffling the same vectors.
14217 if (!(A == C && B == D) && !(A == D && B == C))
14218 return false;
14219
14220 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14221 if (!A.getNode() && !B.getNode())
14222 return false;
14223
14224 // If A and B occur in reverse order in RHS, then "swap" them (which means
14225 // rewriting the mask).
14226 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000014227 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014228
14229 // At this point LHS and RHS are equivalent to
14230 // LHS = VECTOR_SHUFFLE A, B, LMask
14231 // RHS = VECTOR_SHUFFLE A, B, RMask
14232 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000014233 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000014234 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000014235
Craig Topperf8363302011-12-02 08:18:41 +000014236 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014237 if (LIdx < 0 || RIdx < 0 ||
14238 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
14239 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000014240 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000014241
Craig Topperf8363302011-12-02 08:18:41 +000014242 // Check that successive elements are being operated on. If not, this is
14243 // not a horizontal operation.
14244 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
14245 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000014246 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000014247 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000014248 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000014249 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000014250 }
14251
14252 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14253 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14254 return true;
14255}
14256
14257/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14258static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14259 const X86Subtarget *Subtarget) {
14260 EVT VT = N->getValueType(0);
14261 SDValue LHS = N->getOperand(0);
14262 SDValue RHS = N->getOperand(1);
14263
14264 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topper138a5c62011-12-02 07:16:01 +000014265 if (((Subtarget->hasSSE3orAVX() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
14266 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014267 isHorizontalBinOp(LHS, RHS, true))
14268 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14269 return SDValue();
14270}
14271
14272/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14273static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14274 const X86Subtarget *Subtarget) {
14275 EVT VT = N->getValueType(0);
14276 SDValue LHS = N->getOperand(0);
14277 SDValue RHS = N->getOperand(1);
14278
14279 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topper138a5c62011-12-02 07:16:01 +000014280 if (((Subtarget->hasSSE3orAVX() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
14281 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014282 isHorizontalBinOp(LHS, RHS, false))
14283 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14284 return SDValue();
14285}
14286
Chris Lattner6cf73262008-01-25 06:14:17 +000014287/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14288/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014289static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014290 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14291 // F[X]OR(0.0, x) -> x
14292 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014293 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14294 if (C->getValueAPF().isPosZero())
14295 return N->getOperand(1);
14296 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14297 if (C->getValueAPF().isPosZero())
14298 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014299 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014300}
14301
14302/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014303static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014304 // FAND(0.0, x) -> 0.0
14305 // FAND(x, 0.0) -> 0.0
14306 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14307 if (C->getValueAPF().isPosZero())
14308 return N->getOperand(0);
14309 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14310 if (C->getValueAPF().isPosZero())
14311 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014312 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014313}
14314
Dan Gohmane5af2d32009-01-29 01:59:02 +000014315static SDValue PerformBTCombine(SDNode *N,
14316 SelectionDAG &DAG,
14317 TargetLowering::DAGCombinerInfo &DCI) {
14318 // BT ignores high bits in the bit index operand.
14319 SDValue Op1 = N->getOperand(1);
14320 if (Op1.hasOneUse()) {
14321 unsigned BitWidth = Op1.getValueSizeInBits();
14322 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14323 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014324 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14325 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014326 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014327 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14328 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14329 DCI.CommitTargetLoweringOpt(TLO);
14330 }
14331 return SDValue();
14332}
Chris Lattner83e6c992006-10-04 06:57:07 +000014333
Eli Friedman7a5e5552009-06-07 06:52:44 +000014334static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14335 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014336 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014337 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014338 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014339 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014340 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014341 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014342 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014343 }
14344 return SDValue();
14345}
14346
Evan Cheng2e489c42009-12-16 00:53:11 +000014347static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14348 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14349 // (and (i32 x86isd::setcc_carry), 1)
14350 // This eliminates the zext. This transformation is necessary because
14351 // ISD::SETCC is always legalized to i8.
14352 DebugLoc dl = N->getDebugLoc();
14353 SDValue N0 = N->getOperand(0);
14354 EVT VT = N->getValueType(0);
14355 if (N0.getOpcode() == ISD::AND &&
14356 N0.hasOneUse() &&
14357 N0.getOperand(0).hasOneUse()) {
14358 SDValue N00 = N0.getOperand(0);
14359 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14360 return SDValue();
14361 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14362 if (!C || C->getZExtValue() != 1)
14363 return SDValue();
14364 return DAG.getNode(ISD::AND, dl, VT,
14365 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14366 N00.getOperand(0), N00.getOperand(1)),
14367 DAG.getConstant(1, VT));
14368 }
14369
14370 return SDValue();
14371}
14372
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014373// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14374static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14375 unsigned X86CC = N->getConstantOperandVal(0);
14376 SDValue EFLAG = N->getOperand(1);
14377 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014378
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014379 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14380 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14381 // cases.
14382 if (X86CC == X86::COND_B)
14383 return DAG.getNode(ISD::AND, DL, MVT::i8,
14384 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14385 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14386 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014387
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014388 return SDValue();
14389}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014390
Benjamin Kramer1396c402011-06-18 11:09:41 +000014391static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14392 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014393 SDValue Op0 = N->getOperand(0);
14394 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14395 // a 32-bit target where SSE doesn't support i64->FP operations.
14396 if (Op0.getOpcode() == ISD::LOAD) {
14397 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14398 EVT VT = Ld->getValueType(0);
14399 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14400 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14401 !XTLI->getSubtarget()->is64Bit() &&
14402 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014403 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14404 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014405 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14406 return FILDChain;
14407 }
14408 }
14409 return SDValue();
14410}
14411
Chris Lattner23a01992010-12-20 01:37:09 +000014412// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14413static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14414 X86TargetLowering::DAGCombinerInfo &DCI) {
14415 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14416 // the result is either zero or one (depending on the input carry bit).
14417 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14418 if (X86::isZeroNode(N->getOperand(0)) &&
14419 X86::isZeroNode(N->getOperand(1)) &&
14420 // We don't have a good way to replace an EFLAGS use, so only do this when
14421 // dead right now.
14422 SDValue(N, 1).use_empty()) {
14423 DebugLoc DL = N->getDebugLoc();
14424 EVT VT = N->getValueType(0);
14425 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14426 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14427 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14428 DAG.getConstant(X86::COND_B,MVT::i8),
14429 N->getOperand(2)),
14430 DAG.getConstant(1, VT));
14431 return DCI.CombineTo(N, Res1, CarryOut);
14432 }
14433
14434 return SDValue();
14435}
14436
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014437// fold (add Y, (sete X, 0)) -> adc 0, Y
14438// (add Y, (setne X, 0)) -> sbb -1, Y
14439// (sub (sete X, 0), Y) -> sbb 0, Y
14440// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014441static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014442 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014443
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014444 // Look through ZExts.
14445 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14446 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14447 return SDValue();
14448
14449 SDValue SetCC = Ext.getOperand(0);
14450 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14451 return SDValue();
14452
14453 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14454 if (CC != X86::COND_E && CC != X86::COND_NE)
14455 return SDValue();
14456
14457 SDValue Cmp = SetCC.getOperand(1);
14458 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014459 !X86::isZeroNode(Cmp.getOperand(1)) ||
14460 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014461 return SDValue();
14462
14463 SDValue CmpOp0 = Cmp.getOperand(0);
14464 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14465 DAG.getConstant(1, CmpOp0.getValueType()));
14466
14467 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14468 if (CC == X86::COND_NE)
14469 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14470 DL, OtherVal.getValueType(), OtherVal,
14471 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14472 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14473 DL, OtherVal.getValueType(), OtherVal,
14474 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14475}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014476
Craig Topper54f952a2011-11-19 09:02:40 +000014477/// PerformADDCombine - Do target-specific dag combines on integer adds.
14478static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
14479 const X86Subtarget *Subtarget) {
14480 EVT VT = N->getValueType(0);
14481 SDValue Op0 = N->getOperand(0);
14482 SDValue Op1 = N->getOperand(1);
14483
14484 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperb72039c2011-11-30 09:10:50 +000014485 if (((Subtarget->hasSSSE3orAVX() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
14486 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000014487 isHorizontalBinOp(Op0, Op1, true))
14488 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
14489
14490 return OptimizeConditionalInDecrement(N, DAG);
14491}
14492
14493static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
14494 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014495 SDValue Op0 = N->getOperand(0);
14496 SDValue Op1 = N->getOperand(1);
14497
14498 // X86 can't encode an immediate LHS of a sub. See if we can push the
14499 // negation into a preceding instruction.
14500 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014501 // If the RHS of the sub is a XOR with one use and a constant, invert the
14502 // immediate. Then add one to the LHS of the sub so we can turn
14503 // X-Y -> X+~Y+1, saving one register.
14504 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14505 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014506 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014507 EVT VT = Op0.getValueType();
14508 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14509 Op1.getOperand(0),
14510 DAG.getConstant(~XorC, VT));
14511 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014512 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014513 }
14514 }
14515
Craig Topper54f952a2011-11-19 09:02:40 +000014516 // Try to synthesize horizontal adds from adds of shuffles.
14517 EVT VT = N->getValueType(0);
Craig Topperb72039c2011-11-30 09:10:50 +000014518 if (((Subtarget->hasSSSE3orAVX() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
14519 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
14520 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000014521 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
14522
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014523 return OptimizeConditionalInDecrement(N, DAG);
14524}
14525
Dan Gohman475871a2008-07-27 21:46:04 +000014526SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014527 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014528 SelectionDAG &DAG = DCI.DAG;
14529 switch (N->getOpcode()) {
14530 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014531 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014532 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014533 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014534 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014535 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Craig Topper54f952a2011-11-19 09:02:40 +000014536 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
14537 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000014538 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014539 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014540 case ISD::SHL:
14541 case ISD::SRA:
14542 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014543 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014544 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014545 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014546 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014547 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014548 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014549 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14550 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014551 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014552 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14553 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014554 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014555 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014556 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014557 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014558 case X86ISD::SHUFPS: // Handle all target specific shuffles
14559 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014560 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000014561 case X86ISD::UNPCKH:
14562 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014563 case X86ISD::MOVHLPS:
14564 case X86ISD::MOVLHPS:
14565 case X86ISD::PSHUFD:
14566 case X86ISD::PSHUFHW:
14567 case X86ISD::PSHUFLW:
14568 case X86ISD::MOVSS:
14569 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000014570 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000014571 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014572 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014573 }
14574
Dan Gohman475871a2008-07-27 21:46:04 +000014575 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014576}
14577
Evan Chenge5b51ac2010-04-17 06:13:15 +000014578/// isTypeDesirableForOp - Return true if the target has native support for
14579/// the specified value type and it is 'desirable' to use the type for the
14580/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14581/// instruction encodings are longer and some i16 instructions are slow.
14582bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14583 if (!isTypeLegal(VT))
14584 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014585 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014586 return true;
14587
14588 switch (Opc) {
14589 default:
14590 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014591 case ISD::LOAD:
14592 case ISD::SIGN_EXTEND:
14593 case ISD::ZERO_EXTEND:
14594 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014595 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014596 case ISD::SRL:
14597 case ISD::SUB:
14598 case ISD::ADD:
14599 case ISD::MUL:
14600 case ISD::AND:
14601 case ISD::OR:
14602 case ISD::XOR:
14603 return false;
14604 }
14605}
14606
14607/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014608/// beneficial for dag combiner to promote the specified node. If true, it
14609/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014610bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014611 EVT VT = Op.getValueType();
14612 if (VT != MVT::i16)
14613 return false;
14614
Evan Cheng4c26e932010-04-19 19:29:22 +000014615 bool Promote = false;
14616 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014617 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014618 default: break;
14619 case ISD::LOAD: {
14620 LoadSDNode *LD = cast<LoadSDNode>(Op);
14621 // If the non-extending load has a single use and it's not live out, then it
14622 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014623 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14624 Op.hasOneUse()*/) {
14625 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14626 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14627 // The only case where we'd want to promote LOAD (rather then it being
14628 // promoted as an operand is when it's only use is liveout.
14629 if (UI->getOpcode() != ISD::CopyToReg)
14630 return false;
14631 }
14632 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014633 Promote = true;
14634 break;
14635 }
14636 case ISD::SIGN_EXTEND:
14637 case ISD::ZERO_EXTEND:
14638 case ISD::ANY_EXTEND:
14639 Promote = true;
14640 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014641 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014642 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014643 SDValue N0 = Op.getOperand(0);
14644 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014645 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014646 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014647 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014648 break;
14649 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014650 case ISD::ADD:
14651 case ISD::MUL:
14652 case ISD::AND:
14653 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014654 case ISD::XOR:
14655 Commute = true;
14656 // fallthrough
14657 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014658 SDValue N0 = Op.getOperand(0);
14659 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014660 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014661 return false;
14662 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014663 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014664 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014665 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014666 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014667 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014668 }
14669 }
14670
14671 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014672 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014673}
14674
Evan Cheng60c07e12006-07-05 22:17:51 +000014675//===----------------------------------------------------------------------===//
14676// X86 Inline Assembly Support
14677//===----------------------------------------------------------------------===//
14678
Chris Lattnerb8105652009-07-20 17:51:36 +000014679bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14680 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014681
14682 std::string AsmStr = IA->getAsmString();
14683
14684 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014685 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014686 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014687
14688 switch (AsmPieces.size()) {
14689 default: return false;
14690 case 1:
14691 AsmStr = AsmPieces[0];
14692 AsmPieces.clear();
14693 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14694
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014695 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014696 // we will turn this bswap into something that will be lowered to logical ops
14697 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14698 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014699 // bswap $0
14700 if (AsmPieces.size() == 2 &&
14701 (AsmPieces[0] == "bswap" ||
14702 AsmPieces[0] == "bswapq" ||
14703 AsmPieces[0] == "bswapl") &&
14704 (AsmPieces[1] == "$0" ||
14705 AsmPieces[1] == "${0:q}")) {
14706 // No need to check constraints, nothing other than the equivalent of
14707 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014708 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014709 if (!Ty || Ty->getBitWidth() % 16 != 0)
14710 return false;
14711 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014712 }
14713 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014714 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014715 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014716 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014717 AsmPieces[1] == "$$8," &&
14718 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014719 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14720 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014721 const std::string &ConstraintsStr = IA->getConstraintString();
14722 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014723 std::sort(AsmPieces.begin(), AsmPieces.end());
14724 if (AsmPieces.size() == 4 &&
14725 AsmPieces[0] == "~{cc}" &&
14726 AsmPieces[1] == "~{dirflag}" &&
14727 AsmPieces[2] == "~{flags}" &&
14728 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014729 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014730 if (!Ty || Ty->getBitWidth() % 16 != 0)
14731 return false;
14732 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014733 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014734 }
14735 break;
14736 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014737 if (CI->getType()->isIntegerTy(32) &&
14738 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14739 SmallVector<StringRef, 4> Words;
14740 SplitString(AsmPieces[0], Words, " \t,");
14741 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14742 Words[2] == "${0:w}") {
14743 Words.clear();
14744 SplitString(AsmPieces[1], Words, " \t,");
14745 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14746 Words[2] == "$0") {
14747 Words.clear();
14748 SplitString(AsmPieces[2], Words, " \t,");
14749 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14750 Words[2] == "${0:w}") {
14751 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014752 const std::string &ConstraintsStr = IA->getConstraintString();
14753 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014754 std::sort(AsmPieces.begin(), AsmPieces.end());
14755 if (AsmPieces.size() == 4 &&
14756 AsmPieces[0] == "~{cc}" &&
14757 AsmPieces[1] == "~{dirflag}" &&
14758 AsmPieces[2] == "~{flags}" &&
14759 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014760 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014761 if (!Ty || Ty->getBitWidth() % 16 != 0)
14762 return false;
14763 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014764 }
14765 }
14766 }
14767 }
14768 }
Evan Cheng55d42002011-01-08 01:24:27 +000014769
14770 if (CI->getType()->isIntegerTy(64)) {
14771 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14772 if (Constraints.size() >= 2 &&
14773 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14774 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14775 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14776 SmallVector<StringRef, 4> Words;
14777 SplitString(AsmPieces[0], Words, " \t");
14778 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014779 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014780 SplitString(AsmPieces[1], Words, " \t");
14781 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14782 Words.clear();
14783 SplitString(AsmPieces[2], Words, " \t,");
14784 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14785 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014786 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014787 if (!Ty || Ty->getBitWidth() % 16 != 0)
14788 return false;
14789 return IntrinsicLowering::LowerToByteSwap(CI);
14790 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014791 }
14792 }
14793 }
14794 }
14795 break;
14796 }
14797 return false;
14798}
14799
14800
14801
Chris Lattnerf4dff842006-07-11 02:54:03 +000014802/// getConstraintType - Given a constraint letter, return the type of
14803/// constraint it is for this target.
14804X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014805X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14806 if (Constraint.size() == 1) {
14807 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014808 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014809 case 'q':
14810 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014811 case 'f':
14812 case 't':
14813 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014814 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014815 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014816 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014817 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014818 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014819 case 'a':
14820 case 'b':
14821 case 'c':
14822 case 'd':
14823 case 'S':
14824 case 'D':
14825 case 'A':
14826 return C_Register;
14827 case 'I':
14828 case 'J':
14829 case 'K':
14830 case 'L':
14831 case 'M':
14832 case 'N':
14833 case 'G':
14834 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014835 case 'e':
14836 case 'Z':
14837 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014838 default:
14839 break;
14840 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014841 }
Chris Lattner4234f572007-03-25 02:14:49 +000014842 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014843}
14844
John Thompson44ab89e2010-10-29 17:29:13 +000014845/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014846/// This object must already have been set up with the operand type
14847/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014848TargetLowering::ConstraintWeight
14849 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014850 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014851 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014852 Value *CallOperandVal = info.CallOperandVal;
14853 // If we don't have a value, we can't do a match,
14854 // but allow it at the lowest weight.
14855 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014856 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014857 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014858 // Look at the constraint type.
14859 switch (*constraint) {
14860 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014861 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14862 case 'R':
14863 case 'q':
14864 case 'Q':
14865 case 'a':
14866 case 'b':
14867 case 'c':
14868 case 'd':
14869 case 'S':
14870 case 'D':
14871 case 'A':
14872 if (CallOperandVal->getType()->isIntegerTy())
14873 weight = CW_SpecificReg;
14874 break;
14875 case 'f':
14876 case 't':
14877 case 'u':
14878 if (type->isFloatingPointTy())
14879 weight = CW_SpecificReg;
14880 break;
14881 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014882 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014883 weight = CW_SpecificReg;
14884 break;
14885 case 'x':
14886 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014887 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014888 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014889 break;
14890 case 'I':
14891 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14892 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014893 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014894 }
14895 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014896 case 'J':
14897 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14898 if (C->getZExtValue() <= 63)
14899 weight = CW_Constant;
14900 }
14901 break;
14902 case 'K':
14903 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14904 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14905 weight = CW_Constant;
14906 }
14907 break;
14908 case 'L':
14909 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14910 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14911 weight = CW_Constant;
14912 }
14913 break;
14914 case 'M':
14915 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14916 if (C->getZExtValue() <= 3)
14917 weight = CW_Constant;
14918 }
14919 break;
14920 case 'N':
14921 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14922 if (C->getZExtValue() <= 0xff)
14923 weight = CW_Constant;
14924 }
14925 break;
14926 case 'G':
14927 case 'C':
14928 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14929 weight = CW_Constant;
14930 }
14931 break;
14932 case 'e':
14933 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14934 if ((C->getSExtValue() >= -0x80000000LL) &&
14935 (C->getSExtValue() <= 0x7fffffffLL))
14936 weight = CW_Constant;
14937 }
14938 break;
14939 case 'Z':
14940 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14941 if (C->getZExtValue() <= 0xffffffff)
14942 weight = CW_Constant;
14943 }
14944 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014945 }
14946 return weight;
14947}
14948
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014949/// LowerXConstraint - try to replace an X constraint, which matches anything,
14950/// with another that has more specific requirements based on the type of the
14951/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014952const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014953LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014954 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14955 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014956 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014957 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014958 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014959 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014960 return "x";
14961 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014962
Chris Lattner5e764232008-04-26 23:02:14 +000014963 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014964}
14965
Chris Lattner48884cd2007-08-25 00:47:38 +000014966/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14967/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014968void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014969 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014970 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014971 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014972 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014973
Eric Christopher100c8332011-06-02 23:16:42 +000014974 // Only support length 1 constraints for now.
14975 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014976
Eric Christopher100c8332011-06-02 23:16:42 +000014977 char ConstraintLetter = Constraint[0];
14978 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014979 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014980 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014981 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014982 if (C->getZExtValue() <= 31) {
14983 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014984 break;
14985 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014986 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014987 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014988 case 'J':
14989 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014990 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014991 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14992 break;
14993 }
14994 }
14995 return;
14996 case 'K':
14997 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014998 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000014999 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15000 break;
15001 }
15002 }
15003 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000015004 case 'N':
15005 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015006 if (C->getZExtValue() <= 255) {
15007 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015008 break;
15009 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000015010 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015011 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000015012 case 'e': {
15013 // 32-bit signed value
15014 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015015 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15016 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015017 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015018 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000015019 break;
15020 }
15021 // FIXME gcc accepts some relocatable values here too, but only in certain
15022 // memory models; it's complicated.
15023 }
15024 return;
15025 }
15026 case 'Z': {
15027 // 32-bit unsigned value
15028 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015029 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15030 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015031 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15032 break;
15033 }
15034 }
15035 // FIXME gcc accepts some relocatable values here too, but only in certain
15036 // memory models; it's complicated.
15037 return;
15038 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015039 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015040 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000015041 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015042 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015043 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000015044 break;
15045 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015046
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015047 // In any sort of PIC mode addresses need to be computed at runtime by
15048 // adding in a register or some sort of table lookup. These can't
15049 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000015050 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015051 return;
15052
Chris Lattnerdc43a882007-05-03 16:52:29 +000015053 // If we are in non-pic codegen mode, we allow the address of a global (with
15054 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000015055 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015056 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000015057
Chris Lattner49921962009-05-08 18:23:14 +000015058 // Match either (GA), (GA+C), (GA+C1+C2), etc.
15059 while (1) {
15060 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15061 Offset += GA->getOffset();
15062 break;
15063 } else if (Op.getOpcode() == ISD::ADD) {
15064 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15065 Offset += C->getZExtValue();
15066 Op = Op.getOperand(0);
15067 continue;
15068 }
15069 } else if (Op.getOpcode() == ISD::SUB) {
15070 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15071 Offset += -C->getZExtValue();
15072 Op = Op.getOperand(0);
15073 continue;
15074 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015075 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015076
Chris Lattner49921962009-05-08 18:23:14 +000015077 // Otherwise, this isn't something we can handle, reject it.
15078 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015079 }
Eric Christopherfd179292009-08-27 18:07:15 +000015080
Dan Gohman46510a72010-04-15 01:51:59 +000015081 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015082 // If we require an extra load to get this address, as in PIC mode, we
15083 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015084 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15085 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015086 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015087
Devang Patel0d881da2010-07-06 22:08:15 +000015088 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15089 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015090 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015091 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015092 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015093
Gabor Greifba36cb52008-08-28 21:40:38 +000015094 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015095 Ops.push_back(Result);
15096 return;
15097 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015098 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015099}
15100
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015101std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015102X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015103 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015104 // First, see if this is a constraint that directly corresponds to an LLVM
15105 // register class.
15106 if (Constraint.size() == 1) {
15107 // GCC Constraint Letters
15108 switch (Constraint[0]) {
15109 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015110 // TODO: Slight differences here in allocation order and leaving
15111 // RIP in the class. Do they matter any more here than they do
15112 // in the normal allocation?
15113 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15114 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015115 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015116 return std::make_pair(0U, X86::GR32RegisterClass);
15117 else if (VT == MVT::i16)
15118 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015119 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015120 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015121 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015122 return std::make_pair(0U, X86::GR64RegisterClass);
15123 break;
15124 }
15125 // 32-bit fallthrough
15126 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015127 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015128 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
15129 else if (VT == MVT::i16)
15130 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015131 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015132 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
15133 else if (VT == MVT::i64)
15134 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15135 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015136 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015137 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015138 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015139 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015140 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015141 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015142 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015143 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015144 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015145 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015146 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015147 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15148 if (VT == MVT::i16)
15149 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15150 if (VT == MVT::i32 || !Subtarget->is64Bit())
15151 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15152 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015153 case 'f': // FP Stack registers.
15154 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15155 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015156 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015157 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015158 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015159 return std::make_pair(0U, X86::RFP64RegisterClass);
15160 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015161 case 'y': // MMX_REGS if MMX allowed.
15162 if (!Subtarget->hasMMX()) break;
15163 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015164 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015165 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015166 // FALL THROUGH.
15167 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015168 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015169
Owen Anderson825b72b2009-08-11 20:47:22 +000015170 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015171 default: break;
15172 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015173 case MVT::f32:
15174 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015175 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015176 case MVT::f64:
15177 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015178 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015179 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015180 case MVT::v16i8:
15181 case MVT::v8i16:
15182 case MVT::v4i32:
15183 case MVT::v2i64:
15184 case MVT::v4f32:
15185 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015186 return std::make_pair(0U, X86::VR128RegisterClass);
15187 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015188 break;
15189 }
15190 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015191
Chris Lattnerf76d1802006-07-31 23:26:50 +000015192 // Use the default implementation in TargetLowering to convert the register
15193 // constraint into a member of a register class.
15194 std::pair<unsigned, const TargetRegisterClass*> Res;
15195 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015196
15197 // Not found as a standard register?
15198 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015199 // Map st(0) -> st(7) -> ST0
15200 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15201 tolower(Constraint[1]) == 's' &&
15202 tolower(Constraint[2]) == 't' &&
15203 Constraint[3] == '(' &&
15204 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15205 Constraint[5] == ')' &&
15206 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015207
Chris Lattner56d77c72009-09-13 22:41:48 +000015208 Res.first = X86::ST0+Constraint[4]-'0';
15209 Res.second = X86::RFP80RegisterClass;
15210 return Res;
15211 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015212
Chris Lattner56d77c72009-09-13 22:41:48 +000015213 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015214 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015215 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015216 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015217 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015218 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015219
15220 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015221 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015222 Res.first = X86::EFLAGS;
15223 Res.second = X86::CCRRegisterClass;
15224 return Res;
15225 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015226
Dale Johannesen330169f2008-11-13 21:52:36 +000015227 // 'A' means EAX + EDX.
15228 if (Constraint == "A") {
15229 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015230 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015231 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015232 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015233 return Res;
15234 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015235
Chris Lattnerf76d1802006-07-31 23:26:50 +000015236 // Otherwise, check to see if this is a register class of the wrong value
15237 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15238 // turn into {ax},{dx}.
15239 if (Res.second->hasType(VT))
15240 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015241
Chris Lattnerf76d1802006-07-31 23:26:50 +000015242 // All of the single-register GCC register classes map their values onto
15243 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15244 // really want an 8-bit or 32-bit register, map to the appropriate register
15245 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015246 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015247 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015248 unsigned DestReg = 0;
15249 switch (Res.first) {
15250 default: break;
15251 case X86::AX: DestReg = X86::AL; break;
15252 case X86::DX: DestReg = X86::DL; break;
15253 case X86::CX: DestReg = X86::CL; break;
15254 case X86::BX: DestReg = X86::BL; break;
15255 }
15256 if (DestReg) {
15257 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015258 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015259 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015260 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015261 unsigned DestReg = 0;
15262 switch (Res.first) {
15263 default: break;
15264 case X86::AX: DestReg = X86::EAX; break;
15265 case X86::DX: DestReg = X86::EDX; break;
15266 case X86::CX: DestReg = X86::ECX; break;
15267 case X86::BX: DestReg = X86::EBX; break;
15268 case X86::SI: DestReg = X86::ESI; break;
15269 case X86::DI: DestReg = X86::EDI; break;
15270 case X86::BP: DestReg = X86::EBP; break;
15271 case X86::SP: DestReg = X86::ESP; break;
15272 }
15273 if (DestReg) {
15274 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015275 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015276 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015277 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015278 unsigned DestReg = 0;
15279 switch (Res.first) {
15280 default: break;
15281 case X86::AX: DestReg = X86::RAX; break;
15282 case X86::DX: DestReg = X86::RDX; break;
15283 case X86::CX: DestReg = X86::RCX; break;
15284 case X86::BX: DestReg = X86::RBX; break;
15285 case X86::SI: DestReg = X86::RSI; break;
15286 case X86::DI: DestReg = X86::RDI; break;
15287 case X86::BP: DestReg = X86::RBP; break;
15288 case X86::SP: DestReg = X86::RSP; break;
15289 }
15290 if (DestReg) {
15291 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015292 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015293 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015294 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015295 } else if (Res.second == X86::FR32RegisterClass ||
15296 Res.second == X86::FR64RegisterClass ||
15297 Res.second == X86::VR128RegisterClass) {
15298 // Handle references to XMM physical registers that got mapped into the
15299 // wrong class. This can happen with constraints like {xmm0} where the
15300 // target independent register mapper will just pick the first match it can
15301 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015302 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015303 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015304 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015305 Res.second = X86::FR64RegisterClass;
15306 else if (X86::VR128RegisterClass->hasType(VT))
15307 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015308 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015309
Chris Lattnerf76d1802006-07-31 23:26:50 +000015310 return Res;
15311}