blob: 0d78aab899c19610f8f192a07e4ae6a9d90c6f57 [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
Chandler Carruth63974b22011-12-13 01:56:10 +0000381 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Expand);
382 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i16 , Expand);
383 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
384 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i64 , Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000385 if (Subtarget->hasBMI()) {
386 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
387 } else {
388 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
389 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
390 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
391 if (Subtarget->is64Bit())
392 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
393 }
Craig Topper37f21672011-10-11 06:44:02 +0000394
Chandler Carruth63974b22011-12-13 01:56:10 +0000395 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i8 , Expand);
396 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i16 , Expand);
397 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
398 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i64 , Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000399 if (Subtarget->hasLZCNT()) {
400 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
401 } else {
402 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
403 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
404 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
405 if (Subtarget->is64Bit())
406 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000407 }
408
Benjamin Kramer1292c222010-12-04 20:32:23 +0000409 if (Subtarget->hasPOPCNT()) {
410 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
411 } else {
412 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
413 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
414 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
415 if (Subtarget->is64Bit())
416 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
417 }
418
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
420 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000421
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000422 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000423 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000424 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000425 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000426 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
428 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
429 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
430 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
431 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000432 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
434 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
435 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
436 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000437 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000438 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000439 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000440 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000442
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000443 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
445 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
446 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
447 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000448 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000449 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
450 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000451 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000452 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
454 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
455 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
456 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000457 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000458 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000459 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
461 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
462 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000463 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
465 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
466 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000467 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000468
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000469 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000470 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000471
Eric Christopher9a9d2752010-07-22 02:48:34 +0000472 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000473 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000474
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000475 // On X86 and X86-64, atomic operations are lowered to locked instructions.
476 // Locked instructions, in turn, have implicit fence semantics (all memory
477 // operations are flushed before issuing the locked instruction, and they
478 // are not buffered), so we can fold away the common pattern of
479 // fence-atomic-fence.
480 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000481
Mon P Wang63307c32008-05-05 19:05:59 +0000482 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000483 for (unsigned i = 0, e = 4; i != e; ++i) {
484 MVT VT = IntVTs[i];
485 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
486 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000487 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000488 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000489
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000490 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000491 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
493 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
494 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
495 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
496 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
497 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
498 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000499 }
500
Eli Friedman43f51ae2011-08-26 21:21:21 +0000501 if (Subtarget->hasCmpxchg16b()) {
502 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
503 }
504
Evan Cheng3c992d22006-03-07 02:02:57 +0000505 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000506 if (!Subtarget->isTargetDarwin() &&
507 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000508 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000509 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000510 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000511
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
513 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
514 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
515 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000516 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000517 setExceptionPointerRegister(X86::RAX);
518 setExceptionSelectorRegister(X86::RDX);
519 } else {
520 setExceptionPointerRegister(X86::EAX);
521 setExceptionSelectorRegister(X86::EDX);
522 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000523 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
524 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000525
Duncan Sands4a544a72011-09-06 13:37:06 +0000526 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
527 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000528
Owen Anderson825b72b2009-08-11 20:47:22 +0000529 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000530
Nate Begemanacc398c2006-01-25 18:21:52 +0000531 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000532 setOperationAction(ISD::VASTART , MVT::Other, Custom);
533 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000534 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::VAARG , MVT::Other, Custom);
536 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000537 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::VAARG , MVT::Other, Expand);
539 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000540 }
Evan Chengae642192007-03-02 23:16:35 +0000541
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
543 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000544
545 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
546 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
547 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000548 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000549 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
550 MVT::i64 : MVT::i32, Custom);
551 else
552 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
553 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000554
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000555 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000556 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000557 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
559 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000560
Evan Cheng223547a2006-01-31 22:28:30 +0000561 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::FABS , MVT::f64, Custom);
563 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000564
565 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setOperationAction(ISD::FNEG , MVT::f64, Custom);
567 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000568
Evan Cheng68c47cb2007-01-05 07:55:56 +0000569 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
571 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000572
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000573 // Lower this to FGETSIGNx86 plus an AND.
574 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
575 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
576
Evan Chengd25e9e82006-02-02 00:28:23 +0000577 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 setOperationAction(ISD::FSIN , MVT::f64, Expand);
579 setOperationAction(ISD::FCOS , MVT::f64, Expand);
580 setOperationAction(ISD::FSIN , MVT::f32, Expand);
581 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000582
Chris Lattnera54aa942006-01-29 06:26:08 +0000583 // Expand FP immediates into loads from the stack, except for the special
584 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000585 addLegalFPImmediate(APFloat(+0.0)); // xorpd
586 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000587 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000588 // Use SSE for f32, x87 for f64.
589 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
591 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
593 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000595
596 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000598
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600
601 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000602 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
603 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000604
605 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::FSIN , MVT::f32, Expand);
607 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000608
Nate Begemane1795842008-02-14 08:57:00 +0000609 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000610 addLegalFPImmediate(APFloat(+0.0f)); // xorps
611 addLegalFPImmediate(APFloat(+0.0)); // FLD0
612 addLegalFPImmediate(APFloat(+1.0)); // FLD1
613 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
614 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
615
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000616 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
618 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000619 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000620 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000621 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000622 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
624 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000625
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
627 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
628 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
629 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000630
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000631 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
633 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000634 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000635 addLegalFPImmediate(APFloat(+0.0)); // FLD0
636 addLegalFPImmediate(APFloat(+1.0)); // FLD1
637 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
638 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000639 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
640 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
641 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
642 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000643 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000644
Cameron Zwarich33390842011-07-08 21:39:21 +0000645 // We don't support FMA.
646 setOperationAction(ISD::FMA, MVT::f64, Expand);
647 setOperationAction(ISD::FMA, MVT::f32, Expand);
648
Dale Johannesen59a58732007-08-05 18:49:15 +0000649 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000650 if (!TM.Options.UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
652 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
653 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000654 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000655 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000656 addLegalFPImmediate(TmpFlt); // FLD0
657 TmpFlt.changeSign();
658 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000659
660 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000661 APFloat TmpFlt2(+1.0);
662 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
663 &ignored);
664 addLegalFPImmediate(TmpFlt2); // FLD1
665 TmpFlt2.changeSign();
666 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
667 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000668
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000669 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
671 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000672 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000673
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000674 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
675 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
676 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
677 setOperationAction(ISD::FRINT, MVT::f80, Expand);
678 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000679 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000680 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000681
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000682 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
684 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
685 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000686
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 setOperationAction(ISD::FLOG, MVT::f80, Expand);
688 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
689 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
690 setOperationAction(ISD::FEXP, MVT::f80, Expand);
691 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000692
Mon P Wangf007a8b2008-11-06 05:31:54 +0000693 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000694 // (for widening) or expand (for scalarization). Then we will selectively
695 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000696 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
697 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
698 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000713 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000714 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
715 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000730 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000731 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000732 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000733 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000739 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000740 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
745 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
746 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
747 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
748 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000749 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000750 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
751 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
752 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
753 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000754 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000755 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
756 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
757 setTruncStoreAction((MVT::SimpleValueType)VT,
758 (MVT::SimpleValueType)InnerVT, Expand);
759 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
760 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
761 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000762 }
763
Evan Chengc7ce29b2009-02-13 22:36:38 +0000764 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
765 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000766 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000767 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000768 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000769 }
770
Dale Johannesen0488fb62010-09-30 23:57:10 +0000771 // MMX-sized vectors (other than x86mmx) are expected to be expanded
772 // into smaller operations.
773 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
774 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
775 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
776 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
777 setOperationAction(ISD::AND, MVT::v8i8, Expand);
778 setOperationAction(ISD::AND, MVT::v4i16, Expand);
779 setOperationAction(ISD::AND, MVT::v2i32, Expand);
780 setOperationAction(ISD::AND, MVT::v1i64, Expand);
781 setOperationAction(ISD::OR, MVT::v8i8, Expand);
782 setOperationAction(ISD::OR, MVT::v4i16, Expand);
783 setOperationAction(ISD::OR, MVT::v2i32, Expand);
784 setOperationAction(ISD::OR, MVT::v1i64, Expand);
785 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
786 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
787 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
788 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
789 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
790 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
791 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
792 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
793 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
794 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
795 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
796 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
797 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000798 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
799 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
800 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
801 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000802
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000803 if (!TM.Options.UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000805
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
807 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
808 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
809 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
810 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
811 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
812 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
813 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
814 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
815 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
816 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000817 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000818 }
819
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000820 if (!TM.Options.UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000822
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000823 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
824 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000825 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
826 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
827 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
828 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000829
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
831 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
832 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
833 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
834 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
835 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
836 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
837 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
838 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
839 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
840 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
841 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
842 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
843 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
844 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
845 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000846
Nadav Rotem354efd82011-09-18 14:57:03 +0000847 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000848 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
849 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
850 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000851
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
853 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
854 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
855 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
856 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000857
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000858 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
859 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
860 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
861 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
862 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
863
Evan Cheng2c3ae372006-04-12 21:21:57 +0000864 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000865 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
866 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000867 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000868 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000869 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000870 // Do not attempt to custom lower non-128-bit vectors
871 if (!VT.is128BitVector())
872 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000873 setOperationAction(ISD::BUILD_VECTOR,
874 VT.getSimpleVT().SimpleTy, Custom);
875 setOperationAction(ISD::VECTOR_SHUFFLE,
876 VT.getSimpleVT().SimpleTy, Custom);
877 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
878 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000879 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000880
Owen Anderson825b72b2009-08-11 20:47:22 +0000881 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
882 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
883 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
884 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
885 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
886 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000887
Nate Begemancdd1eec2008-02-12 22:51:28 +0000888 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
890 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000891 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000892
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000893 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
895 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000896 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000897
898 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000899 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000900 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000901
Owen Andersond6662ad2009-08-10 20:46:15 +0000902 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000904 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000906 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000908 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000909 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000910 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000911 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000912 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000913
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000915
Evan Cheng2c3ae372006-04-12 21:21:57 +0000916 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000917 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
918 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
919 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
920 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000921
Owen Anderson825b72b2009-08-11 20:47:22 +0000922 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
923 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000924 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000925
Craig Topperc0d82852011-11-22 00:44:41 +0000926 if (Subtarget->hasSSE41orAVX()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000927 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
928 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
929 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
930 setOperationAction(ISD::FRINT, MVT::f32, Legal);
931 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
932 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
933 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
934 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
935 setOperationAction(ISD::FRINT, MVT::f64, Legal);
936 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
937
Nate Begeman14d12ca2008-02-11 04:19:36 +0000938 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000939 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000940
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000941 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
942 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
943 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
944 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
945 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000946
Nate Begeman14d12ca2008-02-11 04:19:36 +0000947 // i8 and i16 vectors are custom , because the source register and source
948 // source memory operand types are not the same width. f32 vectors are
949 // custom since the immediate controlling the insert encodes additional
950 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000951 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
952 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
953 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
954 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000955
Owen Anderson825b72b2009-08-11 20:47:22 +0000956 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
957 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
958 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
959 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000960
Pete Coopera77214a2011-11-14 19:38:42 +0000961 // FIXME: these should be Legal but thats only for the case where
962 // the index is constant. For now custom expand to deal with that
Nate Begeman14d12ca2008-02-11 04:19:36 +0000963 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000964 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
965 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000966 }
967 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000968
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000969 if (Subtarget->hasXMMInt()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000970 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000971 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000972
Nadav Rotem43012222011-05-11 08:12:09 +0000973 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000974 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000975
Nadav Rotem43012222011-05-11 08:12:09 +0000976 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000977 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000978
979 if (Subtarget->hasAVX2()) {
980 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
981 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
982
983 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
984 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
985
986 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
987 } else {
988 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
989 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
990
991 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
992 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
993
994 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
995 }
Nadav Rotem43012222011-05-11 08:12:09 +0000996 }
997
Craig Topperc0d82852011-11-22 00:44:41 +0000998 if (Subtarget->hasSSE42orAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000999 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +00001000
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001001 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +00001002 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
1003 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
1004 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
1005 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
1006 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
1007 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +00001008
Owen Anderson825b72b2009-08-11 20:47:22 +00001009 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001010 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1011 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001012
Owen Anderson825b72b2009-08-11 20:47:22 +00001013 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1014 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1015 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1016 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1017 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
1018 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001019
Owen Anderson825b72b2009-08-11 20:47:22 +00001020 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1021 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1022 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1023 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1024 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1025 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001026
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001027 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1028 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001029 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001030
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00001031 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
1032 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
1033 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
1034 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
1035 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1036 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1037
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001038 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1039 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1040
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001041 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1042 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1043
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001044 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001045 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001046
Duncan Sands28b77e92011-09-06 19:07:46 +00001047 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1048 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1049 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1050 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001051
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001052 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1053 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1054 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1055
Craig Topperaaa643c2011-11-09 07:28:55 +00001056 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1057 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1058 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1059 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001060
Craig Topperaaa643c2011-11-09 07:28:55 +00001061 if (Subtarget->hasAVX2()) {
1062 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1063 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1064 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1065 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001066
Craig Topperaaa643c2011-11-09 07:28:55 +00001067 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1068 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1069 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1070 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001071
Craig Topperaaa643c2011-11-09 07:28:55 +00001072 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1073 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1074 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001075 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001076
1077 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001078
1079 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1080 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1081
1082 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1083 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1084
1085 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001086 } else {
1087 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1088 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1089 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1090 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1091
1092 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1093 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1094 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1095 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1096
1097 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1098 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1099 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1100 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001101
1102 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1103 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1104
1105 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1106 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1107
1108 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001109 }
Craig Topper13894fa2011-08-24 06:14:18 +00001110
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001111 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001112 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001113 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1114 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1115 EVT VT = SVT;
1116
1117 // Extract subvector is special because the value type
1118 // (result) is 128-bit but the source is 256-bit wide.
1119 if (VT.is128BitVector())
1120 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1121
1122 // Do not attempt to custom lower other non-256-bit vectors
1123 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001124 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001125
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001126 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1127 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1128 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1129 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001130 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001131 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001132 }
1133
David Greene54d8eba2011-01-27 22:38:56 +00001134 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001135 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1136 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1137 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001138
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001139 // Do not attempt to promote non-256-bit vectors
1140 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001141 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001142
1143 setOperationAction(ISD::AND, SVT, Promote);
1144 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1145 setOperationAction(ISD::OR, SVT, Promote);
1146 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1147 setOperationAction(ISD::XOR, SVT, Promote);
1148 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1149 setOperationAction(ISD::LOAD, SVT, Promote);
1150 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1151 setOperationAction(ISD::SELECT, SVT, Promote);
1152 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001153 }
David Greene9b9838d2009-06-29 16:47:10 +00001154 }
1155
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001156 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1157 // of this type with custom code.
1158 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1159 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1160 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1161 }
1162
Evan Cheng6be2c582006-04-05 23:38:46 +00001163 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001164 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001165
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001166
Eli Friedman962f5492010-06-02 19:35:46 +00001167 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1168 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001169 //
Eli Friedman962f5492010-06-02 19:35:46 +00001170 // FIXME: We really should do custom legalization for addition and
1171 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1172 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001173 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1174 // Add/Sub/Mul with overflow operations are custom lowered.
1175 MVT VT = IntVTs[i];
1176 setOperationAction(ISD::SADDO, VT, Custom);
1177 setOperationAction(ISD::UADDO, VT, Custom);
1178 setOperationAction(ISD::SSUBO, VT, Custom);
1179 setOperationAction(ISD::USUBO, VT, Custom);
1180 setOperationAction(ISD::SMULO, VT, Custom);
1181 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001182 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001183
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001184 // There are no 8-bit 3-address imul/mul instructions
1185 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1186 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001187
Evan Chengd54f2d52009-03-31 19:38:51 +00001188 if (!Subtarget->is64Bit()) {
1189 // These libcalls are not available in 32-bit.
1190 setLibcallName(RTLIB::SHL_I128, 0);
1191 setLibcallName(RTLIB::SRL_I128, 0);
1192 setLibcallName(RTLIB::SRA_I128, 0);
1193 }
1194
Evan Cheng206ee9d2006-07-07 08:33:52 +00001195 // We have target-specific dag combine patterns for the following nodes:
1196 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001197 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001198 setTargetDAGCombine(ISD::BUILD_VECTOR);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001199 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001200 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001201 setTargetDAGCombine(ISD::SHL);
1202 setTargetDAGCombine(ISD::SRA);
1203 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001204 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001205 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001206 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001207 setTargetDAGCombine(ISD::FADD);
1208 setTargetDAGCombine(ISD::FSUB);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001209 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001210 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001211 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001212 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001213 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001214 if (Subtarget->is64Bit())
1215 setTargetDAGCombine(ISD::MUL);
Craig Topperb4c94572011-10-21 06:55:01 +00001216 if (Subtarget->hasBMI())
1217 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001218
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001219 computeRegisterProperties();
1220
Evan Cheng05219282011-01-06 06:52:41 +00001221 // On Darwin, -Os means optimize for size without hurting performance,
1222 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001223 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001224 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001225 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001226 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1227 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1228 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001229 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001230 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001231
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001232 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001233}
1234
Scott Michel5b8f82e2008-03-10 15:42:14 +00001235
Duncan Sands28b77e92011-09-06 19:07:46 +00001236EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1237 if (!VT.isVector()) return MVT::i8;
1238 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001239}
1240
1241
Evan Cheng29286502008-01-23 23:17:41 +00001242/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1243/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001244static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001245 if (MaxAlign == 16)
1246 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001247 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001248 if (VTy->getBitWidth() == 128)
1249 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001250 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001251 unsigned EltAlign = 0;
1252 getMaxByValAlign(ATy->getElementType(), EltAlign);
1253 if (EltAlign > MaxAlign)
1254 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001255 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001256 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1257 unsigned EltAlign = 0;
1258 getMaxByValAlign(STy->getElementType(i), EltAlign);
1259 if (EltAlign > MaxAlign)
1260 MaxAlign = EltAlign;
1261 if (MaxAlign == 16)
1262 break;
1263 }
1264 }
1265 return;
1266}
1267
1268/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1269/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001270/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1271/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001272unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001273 if (Subtarget->is64Bit()) {
1274 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001275 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001276 if (TyAlign > 8)
1277 return TyAlign;
1278 return 8;
1279 }
1280
Evan Cheng29286502008-01-23 23:17:41 +00001281 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001282 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001283 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001284 return Align;
1285}
Chris Lattner2b02a442007-02-25 08:29:00 +00001286
Evan Chengf0df0312008-05-15 08:39:06 +00001287/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001288/// and store operations as a result of memset, memcpy, and memmove
1289/// lowering. If DstAlign is zero that means it's safe to destination
1290/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1291/// means there isn't a need to check it against alignment requirement,
1292/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001293/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001294/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1295/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1296/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001297/// It returns EVT::Other if the type should be determined using generic
1298/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001299EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001300X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1301 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001302 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001303 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001304 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001305 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1306 // linux. This is because the stack realignment code can't handle certain
1307 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001308 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001309 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001310 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001311 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001312 (Subtarget->isUnalignedMemAccessFast() ||
1313 ((DstAlign == 0 || DstAlign >= 16) &&
1314 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001315 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001316 if (Subtarget->hasAVX() &&
1317 Subtarget->getStackAlignment() >= 32)
1318 return MVT::v8f32;
1319 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001320 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001321 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001322 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001323 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001324 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001325 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001326 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001327 // Do not use f64 to lower memcpy if source is string constant. It's
1328 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001329 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001330 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001331 }
Evan Chengf0df0312008-05-15 08:39:06 +00001332 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001333 return MVT::i64;
1334 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001335}
1336
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001337/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1338/// current function. The returned value is a member of the
1339/// MachineJumpTableInfo::JTEntryKind enum.
1340unsigned X86TargetLowering::getJumpTableEncoding() const {
1341 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1342 // symbol.
1343 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1344 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001345 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001346
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001347 // Otherwise, use the normal jump table encoding heuristics.
1348 return TargetLowering::getJumpTableEncoding();
1349}
1350
Chris Lattnerc64daab2010-01-26 05:02:42 +00001351const MCExpr *
1352X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1353 const MachineBasicBlock *MBB,
1354 unsigned uid,MCContext &Ctx) const{
1355 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1356 Subtarget->isPICStyleGOT());
1357 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1358 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001359 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1360 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001361}
1362
Evan Chengcc415862007-11-09 01:32:10 +00001363/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1364/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001365SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001366 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001367 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001368 // This doesn't have DebugLoc associated with it, but is not really the
1369 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001370 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001371 return Table;
1372}
1373
Chris Lattner589c6f62010-01-26 06:28:43 +00001374/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1375/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1376/// MCExpr.
1377const MCExpr *X86TargetLowering::
1378getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1379 MCContext &Ctx) const {
1380 // X86-64 uses RIP relative addressing based on the jump table label.
1381 if (Subtarget->isPICStyleRIPRel())
1382 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1383
1384 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001385 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001386}
1387
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001388// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001389std::pair<const TargetRegisterClass*, uint8_t>
1390X86TargetLowering::findRepresentativeClass(EVT VT) const{
1391 const TargetRegisterClass *RRC = 0;
1392 uint8_t Cost = 1;
1393 switch (VT.getSimpleVT().SimpleTy) {
1394 default:
1395 return TargetLowering::findRepresentativeClass(VT);
1396 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1397 RRC = (Subtarget->is64Bit()
1398 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1399 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001400 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001401 RRC = X86::VR64RegisterClass;
1402 break;
1403 case MVT::f32: case MVT::f64:
1404 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1405 case MVT::v4f32: case MVT::v2f64:
1406 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1407 case MVT::v4f64:
1408 RRC = X86::VR128RegisterClass;
1409 break;
1410 }
1411 return std::make_pair(RRC, Cost);
1412}
1413
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001414bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1415 unsigned &Offset) const {
1416 if (!Subtarget->isTargetLinux())
1417 return false;
1418
1419 if (Subtarget->is64Bit()) {
1420 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1421 Offset = 0x28;
1422 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1423 AddressSpace = 256;
1424 else
1425 AddressSpace = 257;
1426 } else {
1427 // %gs:0x14 on i386
1428 Offset = 0x14;
1429 AddressSpace = 256;
1430 }
1431 return true;
1432}
1433
1434
Chris Lattner2b02a442007-02-25 08:29:00 +00001435//===----------------------------------------------------------------------===//
1436// Return Value Calling Convention Implementation
1437//===----------------------------------------------------------------------===//
1438
Chris Lattner59ed56b2007-02-28 04:55:35 +00001439#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001440
Michael J. Spencerec38de22010-10-10 22:04:20 +00001441bool
Eric Christopher471e4222011-06-08 23:55:35 +00001442X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1443 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001444 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001445 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001446 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001447 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001448 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001449 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001450}
1451
Dan Gohman98ca4f22009-08-05 01:29:28 +00001452SDValue
1453X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001454 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001455 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001456 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001457 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001458 MachineFunction &MF = DAG.getMachineFunction();
1459 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001460
Chris Lattner9774c912007-02-27 05:28:59 +00001461 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001462 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001463 RVLocs, *DAG.getContext());
1464 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001465
Evan Chengdcea1632010-02-04 02:40:39 +00001466 // Add the regs to the liveout set for the function.
1467 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1468 for (unsigned i = 0; i != RVLocs.size(); ++i)
1469 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1470 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001471
Dan Gohman475871a2008-07-27 21:46:04 +00001472 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001473
Dan Gohman475871a2008-07-27 21:46:04 +00001474 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001475 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1476 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001477 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1478 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001479
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001480 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001481 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1482 CCValAssign &VA = RVLocs[i];
1483 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001484 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001485 EVT ValVT = ValToCopy.getValueType();
1486
Dale Johannesenc4510512010-09-24 19:05:48 +00001487 // If this is x86-64, and we disabled SSE, we can't return FP values,
1488 // or SSE or MMX vectors.
1489 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1490 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001491 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001492 report_fatal_error("SSE register return with SSE disabled");
1493 }
1494 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1495 // llvm-gcc has never done it right and no one has noticed, so this
1496 // should be OK for now.
1497 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001498 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001499 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001500
Chris Lattner447ff682008-03-11 03:23:40 +00001501 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1502 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001503 if (VA.getLocReg() == X86::ST0 ||
1504 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001505 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1506 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001507 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001508 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001509 RetOps.push_back(ValToCopy);
1510 // Don't emit a copytoreg.
1511 continue;
1512 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001513
Evan Cheng242b38b2009-02-23 09:03:22 +00001514 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1515 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001516 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001517 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001518 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001519 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001520 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1521 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001522 // If we don't have SSE2 available, convert to v4f32 so the generated
1523 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001524 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001525 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001526 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001527 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001528 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001529
Dale Johannesendd64c412009-02-04 00:33:20 +00001530 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001531 Flag = Chain.getValue(1);
1532 }
Dan Gohman61a92132008-04-21 23:59:07 +00001533
1534 // The x86-64 ABI for returning structs by value requires that we copy
1535 // the sret argument into %rax for the return. We saved the argument into
1536 // a virtual register in the entry block, so now we copy the value out
1537 // and into %rax.
1538 if (Subtarget->is64Bit() &&
1539 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1540 MachineFunction &MF = DAG.getMachineFunction();
1541 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1542 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001543 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001544 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001545 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001546
Dale Johannesendd64c412009-02-04 00:33:20 +00001547 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001548 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001549
1550 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001551 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001552 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001553
Chris Lattner447ff682008-03-11 03:23:40 +00001554 RetOps[0] = Chain; // Update chain.
1555
1556 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001557 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001558 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001559
1560 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001561 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001562}
1563
Evan Cheng3d2125c2010-11-30 23:55:39 +00001564bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1565 if (N->getNumValues() != 1)
1566 return false;
1567 if (!N->hasNUsesOfValue(1, 0))
1568 return false;
1569
1570 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001571 if (Copy->getOpcode() != ISD::CopyToReg &&
1572 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001573 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001574
1575 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001576 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001577 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001578 if (UI->getOpcode() != X86ISD::RET_FLAG)
1579 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001580 HasRet = true;
1581 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001582
Evan Cheng1bf891a2010-12-01 22:59:46 +00001583 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001584}
1585
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001586EVT
1587X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001588 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001589 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001590 // TODO: Is this also valid on 32-bit?
1591 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001592 ReturnMVT = MVT::i8;
1593 else
1594 ReturnMVT = MVT::i32;
1595
1596 EVT MinVT = getRegisterType(Context, ReturnMVT);
1597 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001598}
1599
Dan Gohman98ca4f22009-08-05 01:29:28 +00001600/// LowerCallResult - Lower the result values of a call into the
1601/// appropriate copies out of appropriate physical registers.
1602///
1603SDValue
1604X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001605 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001606 const SmallVectorImpl<ISD::InputArg> &Ins,
1607 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001608 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001609
Chris Lattnere32bbf62007-02-28 07:09:55 +00001610 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001611 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001612 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001613 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1614 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001615 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001616
Chris Lattner3085e152007-02-25 08:59:22 +00001617 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001618 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001619 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001620 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001621
Torok Edwin3f142c32009-02-01 18:15:56 +00001622 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001623 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001624 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001625 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001626 }
1627
Evan Cheng79fb3b42009-02-20 20:43:02 +00001628 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001629
1630 // If this is a call to a function that returns an fp value on the floating
1631 // point stack, we must guarantee the the value is popped from the stack, so
1632 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001633 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001634 // instead.
1635 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1636 // If we prefer to use the value in xmm registers, copy it out as f80 and
1637 // use a truncate to move it from fp stack reg to xmm reg.
1638 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001639 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001640 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1641 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001642 Val = Chain.getValue(0);
1643
1644 // Round the f80 to the right size, which also moves it to the appropriate
1645 // xmm register.
1646 if (CopyVT != VA.getValVT())
1647 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1648 // This truncation won't change the value.
1649 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001650 } else {
1651 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1652 CopyVT, InFlag).getValue(1);
1653 Val = Chain.getValue(0);
1654 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001655 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001656 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001657 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001658
Dan Gohman98ca4f22009-08-05 01:29:28 +00001659 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001660}
1661
1662
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001663//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001664// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001665//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001666// StdCall calling convention seems to be standard for many Windows' API
1667// routines and around. It differs from C calling convention just a little:
1668// callee should clean up the stack, not caller. Symbols should be also
1669// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001670// For info on fast calling convention see Fast Calling Convention (tail call)
1671// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001672
Dan Gohman98ca4f22009-08-05 01:29:28 +00001673/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001674/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001675static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1676 if (Outs.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 Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001680}
1681
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001682/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001683/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001684static bool
1685ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1686 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001687 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001688
Dan Gohman98ca4f22009-08-05 01:29:28 +00001689 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001690}
1691
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001692/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1693/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001694/// the specific parameter attribute. The copy will be passed as a byval
1695/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001696static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001697CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001698 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1699 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001700 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001701
Dale Johannesendd64c412009-02-04 00:33:20 +00001702 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001703 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001704 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001705}
1706
Chris Lattner29689432010-03-11 00:22:57 +00001707/// IsTailCallConvention - Return true if the calling convention is one that
1708/// supports tail call optimization.
1709static bool IsTailCallConvention(CallingConv::ID CC) {
1710 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1711}
1712
Evan Cheng485fafc2011-03-21 01:19:09 +00001713bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1714 if (!CI->isTailCall())
1715 return false;
1716
1717 CallSite CS(CI);
1718 CallingConv::ID CalleeCC = CS.getCallingConv();
1719 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1720 return false;
1721
1722 return true;
1723}
1724
Evan Cheng0c439eb2010-01-27 00:07:07 +00001725/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1726/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001727static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1728 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001729 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001730}
1731
Dan Gohman98ca4f22009-08-05 01:29:28 +00001732SDValue
1733X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001734 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001735 const SmallVectorImpl<ISD::InputArg> &Ins,
1736 DebugLoc dl, SelectionDAG &DAG,
1737 const CCValAssign &VA,
1738 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001739 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001740 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001741 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001742 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1743 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001744 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001745 EVT ValVT;
1746
1747 // If value is passed by pointer we have address passed instead of the value
1748 // itself.
1749 if (VA.getLocInfo() == CCValAssign::Indirect)
1750 ValVT = VA.getLocVT();
1751 else
1752 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001753
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001754 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001755 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001756 // In case of tail call optimization mark all arguments mutable. Since they
1757 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001758 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001759 unsigned Bytes = Flags.getByValSize();
1760 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1761 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001762 return DAG.getFrameIndex(FI, getPointerTy());
1763 } else {
1764 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001765 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001766 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1767 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001768 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001769 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001770 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001771}
1772
Dan Gohman475871a2008-07-27 21:46:04 +00001773SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001774X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001775 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001776 bool isVarArg,
1777 const SmallVectorImpl<ISD::InputArg> &Ins,
1778 DebugLoc dl,
1779 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001780 SmallVectorImpl<SDValue> &InVals)
1781 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001782 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001783 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001784
Gordon Henriksen86737662008-01-05 16:56:59 +00001785 const Function* Fn = MF.getFunction();
1786 if (Fn->hasExternalLinkage() &&
1787 Subtarget->isTargetCygMing() &&
1788 Fn->getName() == "main")
1789 FuncInfo->setForceFramePointer(true);
1790
Evan Cheng1bc78042006-04-26 01:20:17 +00001791 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001792 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001793 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001794
Chris Lattner29689432010-03-11 00:22:57 +00001795 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1796 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001797
Chris Lattner638402b2007-02-28 07:00:42 +00001798 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001799 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001800 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001801 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001802
1803 // Allocate shadow area for Win64
1804 if (IsWin64) {
1805 CCInfo.AllocateStack(32, 8);
1806 }
1807
Duncan Sands45907662010-10-31 13:21:44 +00001808 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001809
Chris Lattnerf39f7712007-02-28 05:46:49 +00001810 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001811 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001812 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1813 CCValAssign &VA = ArgLocs[i];
1814 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1815 // places.
1816 assert(VA.getValNo() != LastVal &&
1817 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001818 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001819 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001820
Chris Lattnerf39f7712007-02-28 05:46:49 +00001821 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001822 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001823 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001824 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001825 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001826 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001827 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001828 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001829 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001830 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001831 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001832 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1833 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001834 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001835 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001836 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001837 RC = X86::VR64RegisterClass;
1838 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001839 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001840
Devang Patel68e6bee2011-02-21 23:21:26 +00001841 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001842 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001843
Chris Lattnerf39f7712007-02-28 05:46:49 +00001844 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1845 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1846 // right size.
1847 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001848 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001849 DAG.getValueType(VA.getValVT()));
1850 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001851 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001852 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001853 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001854 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001855
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001856 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001857 // Handle MMX values passed in XMM regs.
1858 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001859 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1860 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001861 } else
1862 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001863 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001864 } else {
1865 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001866 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001867 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001868
1869 // If value is passed via pointer - do a load.
1870 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001871 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001872 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001873
Dan Gohman98ca4f22009-08-05 01:29:28 +00001874 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001875 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001876
Dan Gohman61a92132008-04-21 23:59:07 +00001877 // The x86-64 ABI for returning structs by value requires that we copy
1878 // the sret argument into %rax for the return. Save the argument into
1879 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001880 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001881 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1882 unsigned Reg = FuncInfo->getSRetReturnReg();
1883 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001884 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001885 FuncInfo->setSRetReturnReg(Reg);
1886 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001887 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001888 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001889 }
1890
Chris Lattnerf39f7712007-02-28 05:46:49 +00001891 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001892 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001893 if (FuncIsMadeTailCallSafe(CallConv,
1894 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001895 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001896
Evan Cheng1bc78042006-04-26 01:20:17 +00001897 // If the function takes variable number of arguments, make a frame index for
1898 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001899 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001900 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1901 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001902 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001903 }
1904 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001905 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1906
1907 // FIXME: We should really autogenerate these arrays
1908 static const unsigned GPR64ArgRegsWin64[] = {
1909 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001910 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001911 static const unsigned GPR64ArgRegs64Bit[] = {
1912 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1913 };
1914 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001915 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1916 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1917 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001918 const unsigned *GPR64ArgRegs;
1919 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001920
1921 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001922 // The XMM registers which might contain var arg parameters are shadowed
1923 // in their paired GPR. So we only need to save the GPR to their home
1924 // slots.
1925 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001926 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001927 } else {
1928 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1929 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001930
1931 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001932 }
1933 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1934 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001935
Devang Patel578efa92009-06-05 21:57:13 +00001936 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001937 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001938 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001939 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
1940 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001941 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001942 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
1943 !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001944 // Kernel mode asks for SSE to be disabled, so don't push them
1945 // on the stack.
1946 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001947
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001948 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001949 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001950 // Get to the caller-allocated home save location. Add 8 to account
1951 // for the return address.
1952 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001953 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001954 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001955 // Fixup to set vararg frame on shadow area (4 x i64).
1956 if (NumIntRegs < 4)
1957 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001958 } else {
1959 // For X86-64, if there are vararg parameters that are passed via
1960 // registers, then we must store them to their spots on the stack so they
1961 // may be loaded by deferencing the result of va_next.
1962 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1963 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1964 FuncInfo->setRegSaveFrameIndex(
1965 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001966 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001967 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001968
Gordon Henriksen86737662008-01-05 16:56:59 +00001969 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001970 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001971 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1972 getPointerTy());
1973 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001974 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001975 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1976 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001977 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001978 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001979 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001980 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001981 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001982 MachinePointerInfo::getFixedStack(
1983 FuncInfo->getRegSaveFrameIndex(), Offset),
1984 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001985 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001986 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001987 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001988
Dan Gohmanface41a2009-08-16 21:24:25 +00001989 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1990 // Now store the XMM (fp + vector) parameter registers.
1991 SmallVector<SDValue, 11> SaveXMMOps;
1992 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001993
Devang Patel68e6bee2011-02-21 23:21:26 +00001994 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001995 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1996 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001997
Dan Gohman1e93df62010-04-17 14:41:14 +00001998 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1999 FuncInfo->getRegSaveFrameIndex()));
2000 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2001 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002002
Dan Gohmanface41a2009-08-16 21:24:25 +00002003 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002004 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00002005 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002006 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2007 SaveXMMOps.push_back(Val);
2008 }
2009 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2010 MVT::Other,
2011 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002012 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002013
2014 if (!MemOps.empty())
2015 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2016 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002017 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002018 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002019
Gordon Henriksen86737662008-01-05 16:56:59 +00002020 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002021 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2022 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002023 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002024 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002025 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002026 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00002027 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00002028 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002029 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002030
Gordon Henriksen86737662008-01-05 16:56:59 +00002031 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002032 // RegSaveFrameIndex is X86-64 only.
2033 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002034 if (CallConv == CallingConv::X86_FastCall ||
2035 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002036 // fastcc functions can't have varargs.
2037 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002038 }
Evan Cheng25caf632006-05-23 21:06:34 +00002039
Rafael Espindola76927d752011-08-30 19:39:58 +00002040 FuncInfo->setArgumentStackSize(StackSize);
2041
Dan Gohman98ca4f22009-08-05 01:29:28 +00002042 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002043}
2044
Dan Gohman475871a2008-07-27 21:46:04 +00002045SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002046X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2047 SDValue StackPtr, SDValue Arg,
2048 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002049 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002050 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002051 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002052 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002053 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002054 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002055 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002056
2057 return DAG.getStore(Chain, dl, Arg, PtrOff,
2058 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002059 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002060}
2061
Bill Wendling64e87322009-01-16 19:25:27 +00002062/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002063/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002064SDValue
2065X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002066 SDValue &OutRetAddr, SDValue Chain,
2067 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002068 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002069 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002070 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002071 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002072
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002073 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002074 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002075 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002076 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002077}
2078
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002079/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002080/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002081static SDValue
2082EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002083 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002084 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002085 // Store the return address to the appropriate stack slot.
2086 if (!FPDiff) return Chain;
2087 // Calculate the new stack slot for the return address.
2088 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002089 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002090 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002091 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002092 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002093 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002094 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002095 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002096 return Chain;
2097}
2098
Dan Gohman98ca4f22009-08-05 01:29:28 +00002099SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002100X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002101 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002102 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002103 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002104 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002105 const SmallVectorImpl<ISD::InputArg> &Ins,
2106 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002107 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002108 MachineFunction &MF = DAG.getMachineFunction();
2109 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002110 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002111 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002112 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002113
Evan Cheng5f941932010-02-05 02:21:12 +00002114 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002115 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002116 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2117 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002118 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002119
2120 // Sibcalls are automatically detected tailcalls which do not require
2121 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002122 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002123 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002124
2125 if (isTailCall)
2126 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002127 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002128
Chris Lattner29689432010-03-11 00:22:57 +00002129 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2130 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002131
Chris Lattner638402b2007-02-28 07:00:42 +00002132 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002133 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002134 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002135 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002136
2137 // Allocate shadow area for Win64
2138 if (IsWin64) {
2139 CCInfo.AllocateStack(32, 8);
2140 }
2141
Duncan Sands45907662010-10-31 13:21:44 +00002142 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002143
Chris Lattner423c5f42007-02-28 05:31:48 +00002144 // Get a count of how many bytes are to be pushed on the stack.
2145 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002146 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002147 // This is a sibcall. The memory operands are available in caller's
2148 // own caller's stack.
2149 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002150 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2151 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002152 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002153
Gordon Henriksen86737662008-01-05 16:56:59 +00002154 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002155 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002156 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002157 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002158 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2159 FPDiff = NumBytesCallerPushed - NumBytes;
2160
2161 // Set the delta of movement of the returnaddr stackslot.
2162 // But only set if delta is greater than previous delta.
2163 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2164 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2165 }
2166
Evan Chengf22f9b32010-02-06 03:28:46 +00002167 if (!IsSibcall)
2168 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002169
Dan Gohman475871a2008-07-27 21:46:04 +00002170 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002171 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002172 if (isTailCall && FPDiff)
2173 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2174 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002175
Dan Gohman475871a2008-07-27 21:46:04 +00002176 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2177 SmallVector<SDValue, 8> MemOpChains;
2178 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002179
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002180 // Walk the register/memloc assignments, inserting copies/loads. In the case
2181 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002182 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2183 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002184 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002185 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002186 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002187 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002188
Chris Lattner423c5f42007-02-28 05:31:48 +00002189 // Promote the value if needed.
2190 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002191 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002192 case CCValAssign::Full: break;
2193 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002194 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002195 break;
2196 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002197 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002198 break;
2199 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002200 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2201 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002202 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002203 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2204 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002205 } else
2206 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2207 break;
2208 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002209 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002210 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002211 case CCValAssign::Indirect: {
2212 // Store the argument.
2213 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002214 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002215 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002216 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002217 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002218 Arg = SpillSlot;
2219 break;
2220 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002221 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002222
Chris Lattner423c5f42007-02-28 05:31:48 +00002223 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002224 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2225 if (isVarArg && IsWin64) {
2226 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2227 // shadow reg if callee is a varargs function.
2228 unsigned ShadowReg = 0;
2229 switch (VA.getLocReg()) {
2230 case X86::XMM0: ShadowReg = X86::RCX; break;
2231 case X86::XMM1: ShadowReg = X86::RDX; break;
2232 case X86::XMM2: ShadowReg = X86::R8; break;
2233 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002234 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002235 if (ShadowReg)
2236 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002237 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002238 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002239 assert(VA.isMemLoc());
2240 if (StackPtr.getNode() == 0)
2241 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2242 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2243 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002244 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002245 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002246
Evan Cheng32fe1032006-05-25 00:59:30 +00002247 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002248 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002249 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002250
Evan Cheng347d5f72006-04-28 21:29:37 +00002251 // Build a sequence of copy-to-reg nodes chained together with token chain
2252 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002253 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002254 // Tail call byval lowering might overwrite argument registers so in case of
2255 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002256 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002257 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002258 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002259 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002260 InFlag = Chain.getValue(1);
2261 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002262
Chris Lattner88e1fd52009-07-09 04:24:46 +00002263 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002264 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2265 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002266 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002267 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2268 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002269 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002270 InFlag);
2271 InFlag = Chain.getValue(1);
2272 } else {
2273 // If we are tail calling and generating PIC/GOT style code load the
2274 // address of the callee into ECX. The value in ecx is used as target of
2275 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2276 // for tail calls on PIC/GOT architectures. Normally we would just put the
2277 // address of GOT into ebx and then call target@PLT. But for tail calls
2278 // ebx would be restored (since ebx is callee saved) before jumping to the
2279 // target@PLT.
2280
2281 // Note: The actual moving to ECX is done further down.
2282 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2283 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2284 !G->getGlobal()->hasProtectedVisibility())
2285 Callee = LowerGlobalAddress(Callee, DAG);
2286 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002287 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002288 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002289 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002290
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002291 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002292 // From AMD64 ABI document:
2293 // For calls that may call functions that use varargs or stdargs
2294 // (prototype-less calls or calls to functions containing ellipsis (...) in
2295 // the declaration) %al is used as hidden argument to specify the number
2296 // of SSE registers used. The contents of %al do not need to match exactly
2297 // the number of registers, but must be an ubound on the number of SSE
2298 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002299
Gordon Henriksen86737662008-01-05 16:56:59 +00002300 // Count the number of XMM registers allocated.
2301 static const unsigned XMMArgRegs[] = {
2302 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2303 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2304 };
2305 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002306 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002307 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002308
Dale Johannesendd64c412009-02-04 00:33:20 +00002309 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002310 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002311 InFlag = Chain.getValue(1);
2312 }
2313
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002314
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002315 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002316 if (isTailCall) {
2317 // Force all the incoming stack arguments to be loaded from the stack
2318 // before any new outgoing arguments are stored to the stack, because the
2319 // outgoing stack slots may alias the incoming argument stack slots, and
2320 // the alias isn't otherwise explicit. This is slightly more conservative
2321 // than necessary, because it means that each store effectively depends
2322 // on every argument instead of just those arguments it would clobber.
2323 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2324
Dan Gohman475871a2008-07-27 21:46:04 +00002325 SmallVector<SDValue, 8> MemOpChains2;
2326 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002327 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002328 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002329 InFlag = SDValue();
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002330 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002331 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2332 CCValAssign &VA = ArgLocs[i];
2333 if (VA.isRegLoc())
2334 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002335 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002336 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002337 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002338 // Create frame index.
2339 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002340 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002341 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002342 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002343
Duncan Sands276dcbd2008-03-21 09:14:45 +00002344 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002345 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002346 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002347 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002348 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002349 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002350 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002351
Dan Gohman98ca4f22009-08-05 01:29:28 +00002352 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2353 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002354 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002355 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002356 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002357 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002358 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002359 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002360 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002361 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002362 }
2363 }
2364
2365 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002366 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002367 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002368
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002369 // Copy arguments to their registers.
2370 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002371 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002372 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002373 InFlag = Chain.getValue(1);
2374 }
Dan Gohman475871a2008-07-27 21:46:04 +00002375 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002376
Gordon Henriksen86737662008-01-05 16:56:59 +00002377 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002378 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002379 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002380 }
2381
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002382 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2383 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2384 // In the 64-bit large code model, we have to make all calls
2385 // through a register, since the call instruction's 32-bit
2386 // pc-relative offset may not be large enough to hold the whole
2387 // address.
2388 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002389 // If the callee is a GlobalAddress node (quite common, every direct call
2390 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2391 // it.
2392
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002393 // We should use extra load for direct calls to dllimported functions in
2394 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002395 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002396 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002397 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002398 bool ExtraLoad = false;
2399 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002400
Chris Lattner48a7d022009-07-09 05:02:21 +00002401 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2402 // external symbols most go through the PLT in PIC mode. If the symbol
2403 // has hidden or protected visibility, or if it is static or local, then
2404 // we don't need to use the PLT - we can directly call it.
2405 if (Subtarget->isTargetELF() &&
2406 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002407 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002408 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002409 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002410 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002411 (!Subtarget->getTargetTriple().isMacOSX() ||
2412 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002413 // PC-relative references to external symbols should go through $stub,
2414 // unless we're building with the leopard linker or later, which
2415 // automatically synthesizes these stubs.
2416 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002417 } else if (Subtarget->isPICStyleRIPRel() &&
2418 isa<Function>(GV) &&
2419 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2420 // If the function is marked as non-lazy, generate an indirect call
2421 // which loads from the GOT directly. This avoids runtime overhead
2422 // at the cost of eager binding (and one extra byte of encoding).
2423 OpFlags = X86II::MO_GOTPCREL;
2424 WrapperKind = X86ISD::WrapperRIP;
2425 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002426 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002427
Devang Patel0d881da2010-07-06 22:08:15 +00002428 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002429 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002430
2431 // Add a wrapper if needed.
2432 if (WrapperKind != ISD::DELETED_NODE)
2433 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2434 // Add extra indirection if needed.
2435 if (ExtraLoad)
2436 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2437 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002438 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002439 }
Bill Wendling056292f2008-09-16 21:48:12 +00002440 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002441 unsigned char OpFlags = 0;
2442
Evan Cheng1bf891a2010-12-01 22:59:46 +00002443 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2444 // external symbols should go through the PLT.
2445 if (Subtarget->isTargetELF() &&
2446 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2447 OpFlags = X86II::MO_PLT;
2448 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002449 (!Subtarget->getTargetTriple().isMacOSX() ||
2450 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002451 // PC-relative references to external symbols should go through $stub,
2452 // unless we're building with the leopard linker or later, which
2453 // automatically synthesizes these stubs.
2454 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002455 }
Eric Christopherfd179292009-08-27 18:07:15 +00002456
Chris Lattner48a7d022009-07-09 05:02:21 +00002457 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2458 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002459 }
2460
Chris Lattnerd96d0722007-02-25 06:40:16 +00002461 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002462 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002463 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002464
Evan Chengf22f9b32010-02-06 03:28:46 +00002465 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002466 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2467 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002468 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002469 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002470
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002471 Ops.push_back(Chain);
2472 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002473
Dan Gohman98ca4f22009-08-05 01:29:28 +00002474 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002475 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002476
Gordon Henriksen86737662008-01-05 16:56:59 +00002477 // Add argument registers to the end of the list so that they are known live
2478 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002479 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2480 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2481 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002482
Evan Cheng586ccac2008-03-18 23:36:35 +00002483 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002484 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002485 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2486
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002487 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002488 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002489 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002490
Gabor Greifba36cb52008-08-28 21:40:38 +00002491 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002492 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002493
Dan Gohman98ca4f22009-08-05 01:29:28 +00002494 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002495 // We used to do:
2496 //// If this is the first return lowered for this function, add the regs
2497 //// to the liveout set for the function.
2498 // This isn't right, although it's probably harmless on x86; liveouts
2499 // should be computed from returns not tail calls. Consider a void
2500 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002501 return DAG.getNode(X86ISD::TC_RETURN, dl,
2502 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002503 }
2504
Dale Johannesenace16102009-02-03 19:33:06 +00002505 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002506 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002507
Chris Lattner2d297092006-05-23 18:50:38 +00002508 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002509 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002510 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2511 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002512 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002513 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002514 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002515 // pops the hidden struct pointer, so we have to push it back.
2516 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002517 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002518 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002519 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002520
Gordon Henriksenae636f82008-01-03 16:47:34 +00002521 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002522 if (!IsSibcall) {
2523 Chain = DAG.getCALLSEQ_END(Chain,
2524 DAG.getIntPtrConstant(NumBytes, true),
2525 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2526 true),
2527 InFlag);
2528 InFlag = Chain.getValue(1);
2529 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002530
Chris Lattner3085e152007-02-25 08:59:22 +00002531 // Handle result values, copying them out of physregs into vregs that we
2532 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002533 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2534 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002535}
2536
Evan Cheng25ab6902006-09-08 06:48:29 +00002537
2538//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002539// Fast Calling Convention (tail call) implementation
2540//===----------------------------------------------------------------------===//
2541
2542// Like std call, callee cleans arguments, convention except that ECX is
2543// reserved for storing the tail called function address. Only 2 registers are
2544// free for argument passing (inreg). Tail call optimization is performed
2545// provided:
2546// * tailcallopt is enabled
2547// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002548// On X86_64 architecture with GOT-style position independent code only local
2549// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002550// To keep the stack aligned according to platform abi the function
2551// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2552// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002553// If a tail called function callee has more arguments than the caller the
2554// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002555// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002556// original REtADDR, but before the saved framepointer or the spilled registers
2557// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2558// stack layout:
2559// arg1
2560// arg2
2561// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002562// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002563// move area ]
2564// (possible EBP)
2565// ESI
2566// EDI
2567// local1 ..
2568
2569/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2570/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002571unsigned
2572X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2573 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002574 MachineFunction &MF = DAG.getMachineFunction();
2575 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002576 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002577 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002578 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002579 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002580 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002581 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2582 // Number smaller than 12 so just add the difference.
2583 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2584 } else {
2585 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002586 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002587 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002588 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002589 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002590}
2591
Evan Cheng5f941932010-02-05 02:21:12 +00002592/// MatchingStackOffset - Return true if the given stack call argument is
2593/// already available in the same position (relatively) of the caller's
2594/// incoming argument stack.
2595static
2596bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2597 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2598 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002599 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2600 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002601 if (Arg.getOpcode() == ISD::CopyFromReg) {
2602 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002603 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002604 return false;
2605 MachineInstr *Def = MRI->getVRegDef(VR);
2606 if (!Def)
2607 return false;
2608 if (!Flags.isByVal()) {
2609 if (!TII->isLoadFromStackSlot(Def, FI))
2610 return false;
2611 } else {
2612 unsigned Opcode = Def->getOpcode();
2613 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2614 Def->getOperand(1).isFI()) {
2615 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002616 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002617 } else
2618 return false;
2619 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002620 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2621 if (Flags.isByVal())
2622 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002623 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002624 // define @foo(%struct.X* %A) {
2625 // tail call @bar(%struct.X* byval %A)
2626 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002627 return false;
2628 SDValue Ptr = Ld->getBasePtr();
2629 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2630 if (!FINode)
2631 return false;
2632 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002633 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002634 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002635 FI = FINode->getIndex();
2636 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002637 } else
2638 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002639
Evan Cheng4cae1332010-03-05 08:38:04 +00002640 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002641 if (!MFI->isFixedObjectIndex(FI))
2642 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002643 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002644}
2645
Dan Gohman98ca4f22009-08-05 01:29:28 +00002646/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2647/// for tail call optimization. Targets which want to do tail call
2648/// optimization should implement this function.
2649bool
2650X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002651 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002652 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002653 bool isCalleeStructRet,
2654 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002655 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002656 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002657 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002658 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002659 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002660 CalleeCC != CallingConv::C)
2661 return false;
2662
Evan Cheng7096ae42010-01-29 06:45:59 +00002663 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002664 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002665 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002666 CallingConv::ID CallerCC = CallerF->getCallingConv();
2667 bool CCMatch = CallerCC == CalleeCC;
2668
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002669 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002670 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002671 return true;
2672 return false;
2673 }
2674
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002675 // Look for obvious safe cases to perform tail call optimization that do not
2676 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002677
Evan Cheng2c12cb42010-03-26 16:26:03 +00002678 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2679 // emit a special epilogue.
2680 if (RegInfo->needsStackRealignment(MF))
2681 return false;
2682
Evan Chenga375d472010-03-15 18:54:48 +00002683 // Also avoid sibcall optimization if either caller or callee uses struct
2684 // return semantics.
2685 if (isCalleeStructRet || isCallerStructRet)
2686 return false;
2687
Chad Rosier2416da32011-06-24 21:15:36 +00002688 // An stdcall caller is expected to clean up its arguments; the callee
2689 // isn't going to do that.
2690 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2691 return false;
2692
Chad Rosier871f6642011-05-18 19:59:50 +00002693 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002694 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002695 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002696
2697 // Optimizing for varargs on Win64 is unlikely to be safe without
2698 // additional testing.
2699 if (Subtarget->isTargetWin64())
2700 return false;
2701
Chad Rosier871f6642011-05-18 19:59:50 +00002702 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002703 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2704 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002705
Chad Rosier871f6642011-05-18 19:59:50 +00002706 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2707 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2708 if (!ArgLocs[i].isRegLoc())
2709 return false;
2710 }
2711
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002712 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2713 // Therefore if it's not used by the call it is not safe to optimize this into
2714 // a sibcall.
2715 bool Unused = false;
2716 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2717 if (!Ins[i].Used) {
2718 Unused = true;
2719 break;
2720 }
2721 }
2722 if (Unused) {
2723 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002724 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2725 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002726 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002727 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002728 CCValAssign &VA = RVLocs[i];
2729 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2730 return false;
2731 }
2732 }
2733
Evan Cheng13617962010-04-30 01:12:32 +00002734 // If the calling conventions do not match, then we'd better make sure the
2735 // results are returned in the same way as what the caller expects.
2736 if (!CCMatch) {
2737 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002738 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2739 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002740 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2741
2742 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002743 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2744 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002745 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2746
2747 if (RVLocs1.size() != RVLocs2.size())
2748 return false;
2749 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2750 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2751 return false;
2752 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2753 return false;
2754 if (RVLocs1[i].isRegLoc()) {
2755 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2756 return false;
2757 } else {
2758 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2759 return false;
2760 }
2761 }
2762 }
2763
Evan Chenga6bff982010-01-30 01:22:00 +00002764 // If the callee takes no arguments then go on to check the results of the
2765 // call.
2766 if (!Outs.empty()) {
2767 // Check if stack adjustment is needed. For now, do not do this if any
2768 // argument is passed on the stack.
2769 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002770 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2771 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002772
2773 // Allocate shadow area for Win64
2774 if (Subtarget->isTargetWin64()) {
2775 CCInfo.AllocateStack(32, 8);
2776 }
2777
Duncan Sands45907662010-10-31 13:21:44 +00002778 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002779 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002780 MachineFunction &MF = DAG.getMachineFunction();
2781 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2782 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002783
2784 // Check if the arguments are already laid out in the right way as
2785 // the caller's fixed stack objects.
2786 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002787 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2788 const X86InstrInfo *TII =
2789 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002790 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2791 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002792 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002793 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002794 if (VA.getLocInfo() == CCValAssign::Indirect)
2795 return false;
2796 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002797 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2798 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002799 return false;
2800 }
2801 }
2802 }
Evan Cheng9c044672010-05-29 01:35:22 +00002803
2804 // If the tailcall address may be in a register, then make sure it's
2805 // possible to register allocate for it. In 32-bit, the call address can
2806 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002807 // callee-saved registers are restored. These happen to be the same
2808 // registers used to pass 'inreg' arguments so watch out for those.
2809 if (!Subtarget->is64Bit() &&
2810 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002811 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002812 unsigned NumInRegs = 0;
2813 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2814 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002815 if (!VA.isRegLoc())
2816 continue;
2817 unsigned Reg = VA.getLocReg();
2818 switch (Reg) {
2819 default: break;
2820 case X86::EAX: case X86::EDX: case X86::ECX:
2821 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002822 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002823 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002824 }
2825 }
2826 }
Evan Chenga6bff982010-01-30 01:22:00 +00002827 }
Evan Chengb1712452010-01-27 06:25:16 +00002828
Evan Cheng86809cc2010-02-03 03:28:02 +00002829 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002830}
2831
Dan Gohman3df24e62008-09-03 23:12:08 +00002832FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002833X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2834 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002835}
2836
2837
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002838//===----------------------------------------------------------------------===//
2839// Other Lowering Hooks
2840//===----------------------------------------------------------------------===//
2841
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002842static bool MayFoldLoad(SDValue Op) {
2843 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2844}
2845
2846static bool MayFoldIntoStore(SDValue Op) {
2847 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2848}
2849
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002850static bool isTargetShuffle(unsigned Opcode) {
2851 switch(Opcode) {
2852 default: return false;
2853 case X86ISD::PSHUFD:
2854 case X86ISD::PSHUFHW:
2855 case X86ISD::PSHUFLW:
2856 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002857 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002858 case X86ISD::SHUFPS:
2859 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002860 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002861 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002862 case X86ISD::MOVLPS:
2863 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002864 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002865 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002866 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002867 case X86ISD::MOVSS:
2868 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002869 case X86ISD::UNPCKL:
2870 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002871 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002872 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002873 return true;
2874 }
2875 return false;
2876}
2877
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002878static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002879 SDValue V1, SelectionDAG &DAG) {
2880 switch(Opc) {
2881 default: llvm_unreachable("Unknown x86 shuffle node");
2882 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002883 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002884 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002885 return DAG.getNode(Opc, dl, VT, V1);
2886 }
2887
2888 return SDValue();
2889}
2890
2891static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002892 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002893 switch(Opc) {
2894 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002895 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002896 case X86ISD::PSHUFHW:
2897 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002898 case X86ISD::VPERMILP:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002899 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2900 }
2901
2902 return SDValue();
2903}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002904
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002905static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2906 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2907 switch(Opc) {
2908 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002909 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002910 case X86ISD::SHUFPD:
2911 case X86ISD::SHUFPS:
Craig Topperec24e612011-11-30 07:47:51 +00002912 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002913 return DAG.getNode(Opc, dl, VT, V1, V2,
2914 DAG.getConstant(TargetMask, MVT::i8));
2915 }
2916 return SDValue();
2917}
2918
2919static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2920 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2921 switch(Opc) {
2922 default: llvm_unreachable("Unknown x86 shuffle node");
2923 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002924 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002925 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002926 case X86ISD::MOVLPS:
2927 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002928 case X86ISD::MOVSS:
2929 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002930 case X86ISD::UNPCKL:
2931 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002932 return DAG.getNode(Opc, dl, VT, V1, V2);
2933 }
2934 return SDValue();
2935}
2936
Dan Gohmand858e902010-04-17 15:26:15 +00002937SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002938 MachineFunction &MF = DAG.getMachineFunction();
2939 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2940 int ReturnAddrIndex = FuncInfo->getRAIndex();
2941
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002942 if (ReturnAddrIndex == 0) {
2943 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002944 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002945 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002946 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002947 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002948 }
2949
Evan Cheng25ab6902006-09-08 06:48:29 +00002950 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002951}
2952
2953
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002954bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2955 bool hasSymbolicDisplacement) {
2956 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002957 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002958 return false;
2959
2960 // If we don't have a symbolic displacement - we don't have any extra
2961 // restrictions.
2962 if (!hasSymbolicDisplacement)
2963 return true;
2964
2965 // FIXME: Some tweaks might be needed for medium code model.
2966 if (M != CodeModel::Small && M != CodeModel::Kernel)
2967 return false;
2968
2969 // For small code model we assume that latest object is 16MB before end of 31
2970 // bits boundary. We may also accept pretty large negative constants knowing
2971 // that all objects are in the positive half of address space.
2972 if (M == CodeModel::Small && Offset < 16*1024*1024)
2973 return true;
2974
2975 // For kernel code model we know that all object resist in the negative half
2976 // of 32bits address space. We may not accept negative offsets, since they may
2977 // be just off and we may accept pretty large positive ones.
2978 if (M == CodeModel::Kernel && Offset > 0)
2979 return true;
2980
2981 return false;
2982}
2983
Evan Chengef41ff62011-06-23 17:54:54 +00002984/// isCalleePop - Determines whether the callee is required to pop its
2985/// own arguments. Callee pop is necessary to support tail calls.
2986bool X86::isCalleePop(CallingConv::ID CallingConv,
2987 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2988 if (IsVarArg)
2989 return false;
2990
2991 switch (CallingConv) {
2992 default:
2993 return false;
2994 case CallingConv::X86_StdCall:
2995 return !is64Bit;
2996 case CallingConv::X86_FastCall:
2997 return !is64Bit;
2998 case CallingConv::X86_ThisCall:
2999 return !is64Bit;
3000 case CallingConv::Fast:
3001 return TailCallOpt;
3002 case CallingConv::GHC:
3003 return TailCallOpt;
3004 }
3005}
3006
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003007/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3008/// specific condition code, returning the condition code and the LHS/RHS of the
3009/// comparison to make.
3010static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3011 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003012 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003013 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3014 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3015 // X > -1 -> X == 0, jump !sign.
3016 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003017 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003018 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
3019 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003020 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00003021 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003022 // X < 1 -> X <= 0
3023 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003024 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003025 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003026 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003027
Evan Chengd9558e02006-01-06 00:43:03 +00003028 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003029 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003030 case ISD::SETEQ: return X86::COND_E;
3031 case ISD::SETGT: return X86::COND_G;
3032 case ISD::SETGE: return X86::COND_GE;
3033 case ISD::SETLT: return X86::COND_L;
3034 case ISD::SETLE: return X86::COND_LE;
3035 case ISD::SETNE: return X86::COND_NE;
3036 case ISD::SETULT: return X86::COND_B;
3037 case ISD::SETUGT: return X86::COND_A;
3038 case ISD::SETULE: return X86::COND_BE;
3039 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003040 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003041 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003042
Chris Lattner4c78e022008-12-23 23:42:27 +00003043 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003044
Chris Lattner4c78e022008-12-23 23:42:27 +00003045 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003046 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3047 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003048 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3049 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003050 }
3051
Chris Lattner4c78e022008-12-23 23:42:27 +00003052 switch (SetCCOpcode) {
3053 default: break;
3054 case ISD::SETOLT:
3055 case ISD::SETOLE:
3056 case ISD::SETUGT:
3057 case ISD::SETUGE:
3058 std::swap(LHS, RHS);
3059 break;
3060 }
3061
3062 // On a floating point condition, the flags are set as follows:
3063 // ZF PF CF op
3064 // 0 | 0 | 0 | X > Y
3065 // 0 | 0 | 1 | X < Y
3066 // 1 | 0 | 0 | X == Y
3067 // 1 | 1 | 1 | unordered
3068 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003069 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003070 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003071 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003072 case ISD::SETOLT: // flipped
3073 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003074 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003075 case ISD::SETOLE: // flipped
3076 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003077 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003078 case ISD::SETUGT: // flipped
3079 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003080 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003081 case ISD::SETUGE: // flipped
3082 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003083 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003084 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003085 case ISD::SETNE: return X86::COND_NE;
3086 case ISD::SETUO: return X86::COND_P;
3087 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003088 case ISD::SETOEQ:
3089 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003090 }
Evan Chengd9558e02006-01-06 00:43:03 +00003091}
3092
Evan Cheng4a460802006-01-11 00:33:36 +00003093/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3094/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003095/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003096static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003097 switch (X86CC) {
3098 default:
3099 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003100 case X86::COND_B:
3101 case X86::COND_BE:
3102 case X86::COND_E:
3103 case X86::COND_P:
3104 case X86::COND_A:
3105 case X86::COND_AE:
3106 case X86::COND_NE:
3107 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003108 return true;
3109 }
3110}
3111
Evan Chengeb2f9692009-10-27 19:56:55 +00003112/// isFPImmLegal - Returns true if the target can instruction select the
3113/// specified FP immediate natively. If false, the legalizer will
3114/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003115bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003116 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3117 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3118 return true;
3119 }
3120 return false;
3121}
3122
Nate Begeman9008ca62009-04-27 18:41:29 +00003123/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3124/// the specified range (L, H].
3125static bool isUndefOrInRange(int Val, int Low, int Hi) {
3126 return (Val < 0) || (Val >= Low && Val < Hi);
3127}
3128
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003129/// isUndefOrInRange - Return true if every element in Mask, begining
3130/// from position Pos and ending in Pos+Size, falls within the specified
3131/// range (L, L+Pos]. or is undef.
3132static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3133 int Pos, int Size, int Low, int Hi) {
3134 for (int i = Pos, e = Pos+Size; i != e; ++i)
3135 if (!isUndefOrInRange(Mask[i], Low, Hi))
3136 return false;
3137 return true;
3138}
3139
Nate Begeman9008ca62009-04-27 18:41:29 +00003140/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3141/// specified value.
3142static bool isUndefOrEqual(int Val, int CmpVal) {
3143 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003144 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003145 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003146}
3147
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003148/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3149/// from position Pos and ending in Pos+Size, falls within the specified
3150/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003151static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3152 int Pos, int Size, int Low) {
3153 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3154 if (!isUndefOrEqual(Mask[i], Low))
3155 return false;
3156 return true;
3157}
3158
Nate Begeman9008ca62009-04-27 18:41:29 +00003159/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3160/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3161/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003162static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003163 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003164 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003165 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003166 return (Mask[0] < 2 && Mask[1] < 2);
3167 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003168}
3169
Nate Begeman9008ca62009-04-27 18:41:29 +00003170bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003171 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003172 N->getMask(M);
3173 return ::isPSHUFDMask(M, N->getValueType(0));
3174}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003175
Nate Begeman9008ca62009-04-27 18:41:29 +00003176/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3177/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003178static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003179 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003180 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003181
Nate Begeman9008ca62009-04-27 18:41:29 +00003182 // Lower quadword copied in order or undef.
3183 for (int i = 0; i != 4; ++i)
3184 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003185 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003186
Evan Cheng506d3df2006-03-29 23:07:14 +00003187 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003188 for (int i = 4; i != 8; ++i)
3189 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003190 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003191
Evan Cheng506d3df2006-03-29 23:07:14 +00003192 return true;
3193}
3194
Nate Begeman9008ca62009-04-27 18:41:29 +00003195bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003196 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 N->getMask(M);
3198 return ::isPSHUFHWMask(M, N->getValueType(0));
3199}
Evan Cheng506d3df2006-03-29 23:07:14 +00003200
Nate Begeman9008ca62009-04-27 18:41:29 +00003201/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3202/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003203static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003204 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003205 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003206
Rafael Espindola15684b22009-04-24 12:40:33 +00003207 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003208 for (int i = 4; i != 8; ++i)
3209 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003210 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003211
Rafael Espindola15684b22009-04-24 12:40:33 +00003212 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003213 for (int i = 0; i != 4; ++i)
3214 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003215 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003216
Rafael Espindola15684b22009-04-24 12:40:33 +00003217 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003218}
3219
Nate Begeman9008ca62009-04-27 18:41:29 +00003220bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003221 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003222 N->getMask(M);
3223 return ::isPSHUFLWMask(M, N->getValueType(0));
3224}
3225
Nate Begemana09008b2009-10-19 02:17:23 +00003226/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3227/// is suitable for input to PALIGNR.
3228static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003229 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003230 int i, e = VT.getVectorNumElements();
Craig Topper1dc0fbc2011-12-05 07:27:14 +00003231 if (VT.getSizeInBits() != 128)
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003232 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003233
Nate Begemana09008b2009-10-19 02:17:23 +00003234 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003235 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003236 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003237
Nate Begemana09008b2009-10-19 02:17:23 +00003238 for (i = 0; i != e; ++i)
3239 if (Mask[i] >= 0)
3240 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003241
Nate Begemana09008b2009-10-19 02:17:23 +00003242 // All undef, not a palignr.
3243 if (i == e)
3244 return false;
3245
Eli Friedman63f8dde2011-07-25 21:36:45 +00003246 // Make sure we're shifting in the right direction.
3247 if (Mask[i] <= i)
3248 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003249
3250 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003251
Nate Begemana09008b2009-10-19 02:17:23 +00003252 // Check the rest of the elements to see if they are consecutive.
3253 for (++i; i != e; ++i) {
3254 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003255 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003256 return false;
3257 }
3258 return true;
3259}
3260
Craig Topper9d7025b2011-11-27 21:41:12 +00003261/// isVSHUFPYMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003262/// specifies a shuffle of elements that is suitable for input to 256-bit
3263/// VSHUFPSY.
Craig Topper9d7025b2011-11-27 21:41:12 +00003264static bool isVSHUFPYMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper1ff73d72011-12-06 04:59:07 +00003265 bool HasAVX, bool Commuted = false) {
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003266 int NumElems = VT.getVectorNumElements();
3267
Craig Topper71c4c122011-11-28 01:14:24 +00003268 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003269 return false;
3270
Craig Topper9d7025b2011-11-27 21:41:12 +00003271 if (NumElems != 4 && NumElems != 8)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003272 return false;
3273
3274 // VSHUFPSY 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 => X7 X6 X5 X4 X3 X2 X1 X0
3279 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3280 //
3281 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3282 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3283 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003284 // VSHUFPDY divides the resulting vector into 4 chunks.
3285 // The sources are also splitted into 4 chunks, and each destination
3286 // chunk must come from a different source chunk.
3287 //
3288 // SRC1 => X3 X2 X1 X0
3289 // SRC2 => Y3 Y2 Y1 Y0
3290 //
3291 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3292 //
Craig Topper1ff73d72011-12-06 04:59:07 +00003293 unsigned QuarterSize = NumElems/4;
3294 unsigned HalfSize = QuarterSize*2;
3295 for (unsigned l = 0; l != 2; ++l) {
3296 unsigned LaneStart = l*HalfSize;
3297 for (unsigned s = 0; s != 2; ++s) {
3298 unsigned QuarterStart = s*QuarterSize;
3299 unsigned Src = (Commuted) ? (1-s) : s;
3300 unsigned SrcStart = Src*NumElems + LaneStart;
3301 for (unsigned i = 0; i != QuarterSize; ++i) {
3302 int Idx = Mask[i+QuarterStart+LaneStart];
3303 if (!isUndefOrInRange(Idx, SrcStart, SrcStart+HalfSize))
3304 return false;
3305 // For VSHUFPSY, the mask of the second half must be the same as the first
3306 // but with the appropriate offsets. This works in the same way as
3307 // VPERMILPS works with masks.
3308 if (NumElems == 4 || l == 0 || Mask[i+QuarterStart] < 0)
3309 continue;
3310 if (!isUndefOrEqual(Idx, Mask[i+QuarterStart]+HalfSize))
3311 return false;
3312 }
3313 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003314 }
3315
3316 return true;
3317}
3318
Craig Topper9d7025b2011-11-27 21:41:12 +00003319/// getShuffleVSHUFPYImmediate - Return the appropriate immediate to shuffle
3320/// the specified VECTOR_MASK mask with VSHUFPSY/VSHUFPDY instructions.
3321static unsigned getShuffleVSHUFPYImmediate(SDNode *N) {
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003322 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3323 EVT VT = SVOp->getValueType(0);
3324 int NumElems = VT.getVectorNumElements();
3325
Craig Topper9d7025b2011-11-27 21:41:12 +00003326 assert(VT.getSizeInBits() == 256 && "Only supports 256-bit types");
3327 assert((NumElems == 4 || NumElems == 8) && "Only supports v4 and v8 types");
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003328
3329 int HalfSize = NumElems/2;
Craig Topper9d7025b2011-11-27 21:41:12 +00003330 unsigned Mul = (NumElems == 8) ? 2 : 1;
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003331 unsigned Mask = 0;
Craig Topper71c4c122011-11-28 01:14:24 +00003332 for (int i = 0; i != NumElems; ++i) {
Craig Topper9d7025b2011-11-27 21:41:12 +00003333 int Elt = SVOp->getMaskElt(i);
3334 if (Elt < 0)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003335 continue;
Craig Topper9d7025b2011-11-27 21:41:12 +00003336 Elt %= HalfSize;
3337 unsigned Shamt = i;
3338 // For VSHUFPSY, the mask of the first half must be equal to the second one.
3339 if (NumElems == 8) Shamt %= HalfSize;
3340 Mask |= Elt << (Shamt*Mul);
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003341 }
3342
3343 return Mask;
3344}
3345
Elena Demikhovsky52a35a82011-11-23 10:23:16 +00003346/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3347/// the two vector operands have swapped position.
Craig Topperbeabc6c2011-12-05 06:56:46 +00003348static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3349 unsigned NumElems) {
Elena Demikhovsky52a35a82011-11-23 10:23:16 +00003350 for (unsigned i = 0; i != NumElems; ++i) {
3351 int idx = Mask[i];
3352 if (idx < 0)
3353 continue;
3354 else if (idx < (int)NumElems)
3355 Mask[i] = idx + NumElems;
3356 else
3357 Mask[i] = idx - NumElems;
3358 }
3359}
3360
Evan Cheng14aed5e2006-03-24 01:18:28 +00003361/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003362/// specifies a shuffle of elements that is suitable for input to 128-bit
Craig Topper1ff73d72011-12-06 04:59:07 +00003363/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3364/// reverse of what x86 shuffles want.
3365static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT,
3366 bool Commuted = false) {
3367 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003368
3369 if (VT.getSizeInBits() != 128)
3370 return false;
3371
Nate Begeman9008ca62009-04-27 18:41:29 +00003372 if (NumElems != 2 && NumElems != 4)
3373 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003374
Craig Topper1ff73d72011-12-06 04:59:07 +00003375 unsigned Half = NumElems / 2;
3376 unsigned SrcStart = Commuted ? NumElems : 0;
3377 for (unsigned i = 0; i != Half; ++i)
3378 if (!isUndefOrInRange(Mask[i], SrcStart, SrcStart+NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003379 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003380 SrcStart = Commuted ? 0 : NumElems;
3381 for (unsigned i = Half; i != NumElems; ++i)
3382 if (!isUndefOrInRange(Mask[i], SrcStart, SrcStart+NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003383 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003384
Evan Cheng14aed5e2006-03-24 01:18:28 +00003385 return true;
3386}
3387
Nate Begeman9008ca62009-04-27 18:41:29 +00003388bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3389 SmallVector<int, 8> M;
3390 N->getMask(M);
3391 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003392}
3393
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003394/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3395/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003396bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003397 EVT VT = N->getValueType(0);
3398 unsigned NumElems = VT.getVectorNumElements();
3399
3400 if (VT.getSizeInBits() != 128)
3401 return false;
3402
3403 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003404 return false;
3405
Evan Cheng2064a2b2006-03-28 06:50:32 +00003406 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003407 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3408 isUndefOrEqual(N->getMaskElt(1), 7) &&
3409 isUndefOrEqual(N->getMaskElt(2), 2) &&
3410 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003411}
3412
Nate Begeman0b10b912009-11-07 23:17:15 +00003413/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3414/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3415/// <2, 3, 2, 3>
3416bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003417 EVT VT = N->getValueType(0);
3418 unsigned NumElems = VT.getVectorNumElements();
3419
3420 if (VT.getSizeInBits() != 128)
3421 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003422
Nate Begeman0b10b912009-11-07 23:17:15 +00003423 if (NumElems != 4)
3424 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003425
Nate Begeman0b10b912009-11-07 23:17:15 +00003426 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003427 isUndefOrEqual(N->getMaskElt(1), 3) &&
3428 isUndefOrEqual(N->getMaskElt(2), 2) &&
3429 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003430}
3431
Evan Cheng5ced1d82006-04-06 23:23:56 +00003432/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3433/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003434bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3435 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003436
Evan Cheng5ced1d82006-04-06 23:23:56 +00003437 if (NumElems != 2 && NumElems != 4)
3438 return false;
3439
Evan Chengc5cdff22006-04-07 21:53:05 +00003440 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003441 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003442 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003443
Evan Chengc5cdff22006-04-07 21:53:05 +00003444 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003445 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003446 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003447
3448 return true;
3449}
3450
Nate Begeman0b10b912009-11-07 23:17:15 +00003451/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3452/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3453bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003454 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003455
David Greenea20244d2011-03-02 17:23:43 +00003456 if ((NumElems != 2 && NumElems != 4)
3457 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003458 return false;
3459
Evan Chengc5cdff22006-04-07 21:53:05 +00003460 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003461 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003462 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003463
Nate Begeman9008ca62009-04-27 18:41:29 +00003464 for (unsigned i = 0; i < NumElems/2; ++i)
3465 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003466 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003467
3468 return true;
3469}
3470
Evan Cheng0038e592006-03-28 00:39:58 +00003471/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3472/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003473static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003474 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003475 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003476
3477 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3478 "Unsupported vector type for unpckh");
3479
Craig Topper6347e862011-11-21 06:57:39 +00003480 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003481 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003482 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003483
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003484 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3485 // independently on 128-bit lanes.
3486 unsigned NumLanes = VT.getSizeInBits()/128;
3487 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003488
Craig Topper94438ba2011-12-16 08:06:31 +00003489 for (unsigned l = 0; l != NumLanes; ++l) {
3490 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3491 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003492 i += 2, ++j) {
3493 int BitI = Mask[i];
3494 int BitI1 = Mask[i+1];
3495 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003496 return false;
David Greenea20244d2011-03-02 17:23:43 +00003497 if (V2IsSplat) {
3498 if (!isUndefOrEqual(BitI1, NumElts))
3499 return false;
3500 } else {
3501 if (!isUndefOrEqual(BitI1, j + NumElts))
3502 return false;
3503 }
Evan Cheng39623da2006-04-20 08:58:49 +00003504 }
Evan Cheng0038e592006-03-28 00:39:58 +00003505 }
David Greenea20244d2011-03-02 17:23:43 +00003506
Evan Cheng0038e592006-03-28 00:39:58 +00003507 return true;
3508}
3509
Craig Topper6347e862011-11-21 06:57:39 +00003510bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003511 SmallVector<int, 8> M;
3512 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003513 return ::isUNPCKLMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003514}
3515
Evan Cheng4fcb9222006-03-28 02:43:26 +00003516/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3517/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003518static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003519 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003520 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003521
3522 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3523 "Unsupported vector type for unpckh");
3524
Craig Topper6347e862011-11-21 06:57:39 +00003525 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003526 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003527 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003528
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003529 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3530 // independently on 128-bit lanes.
3531 unsigned NumLanes = VT.getSizeInBits()/128;
3532 unsigned NumLaneElts = NumElts/NumLanes;
3533
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003534 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003535 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3536 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003537 int BitI = Mask[i];
3538 int BitI1 = Mask[i+1];
3539 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003540 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003541 if (V2IsSplat) {
3542 if (isUndefOrEqual(BitI1, NumElts))
3543 return false;
3544 } else {
3545 if (!isUndefOrEqual(BitI1, j+NumElts))
3546 return false;
3547 }
Evan Cheng39623da2006-04-20 08:58:49 +00003548 }
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>
Craig Topper94438ba2011-12-16 08:06:31 +00003562static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3563 bool HasAVX2) {
3564 unsigned NumElts = VT.getVectorNumElements();
3565
3566 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3567 "Unsupported vector type for unpckh");
3568
3569 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3570 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003571 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003572
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003573 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3574 // FIXME: Need a better way to get rid of this, there's no latency difference
3575 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3576 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003577 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003578 return false;
3579
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003580 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3581 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003582 unsigned NumLanes = VT.getSizeInBits()/128;
3583 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003584
Craig Topper94438ba2011-12-16 08:06:31 +00003585 for (unsigned l = 0; l != NumLanes; ++l) {
3586 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3587 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003588 i += 2, ++j) {
3589 int BitI = Mask[i];
3590 int BitI1 = Mask[i+1];
3591
3592 if (!isUndefOrEqual(BitI, j))
3593 return false;
3594 if (!isUndefOrEqual(BitI1, j))
3595 return false;
3596 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003597 }
David Greenea20244d2011-03-02 17:23:43 +00003598
Rafael Espindola15684b22009-04-24 12:40:33 +00003599 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003600}
3601
Craig Topper94438ba2011-12-16 08:06:31 +00003602bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N, bool HasAVX2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003603 SmallVector<int, 8> M;
3604 N->getMask(M);
Craig Topper94438ba2011-12-16 08:06:31 +00003605 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0), HasAVX2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003606}
3607
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003608/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3609/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3610/// <2, 2, 3, 3>
Craig Topper94438ba2011-12-16 08:06:31 +00003611static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3612 bool HasAVX2) {
3613 unsigned NumElts = VT.getVectorNumElements();
3614
3615 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3616 "Unsupported vector type for unpckh");
3617
3618 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3619 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003620 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003621
Craig Topper94438ba2011-12-16 08:06:31 +00003622 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3623 // independently on 128-bit lanes.
3624 unsigned NumLanes = VT.getSizeInBits()/128;
3625 unsigned NumLaneElts = NumElts/NumLanes;
3626
3627 for (unsigned l = 0; l != NumLanes; ++l) {
3628 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3629 i != (l+1)*NumLaneElts; i += 2, ++j) {
3630 int BitI = Mask[i];
3631 int BitI1 = Mask[i+1];
3632 if (!isUndefOrEqual(BitI, j))
3633 return false;
3634 if (!isUndefOrEqual(BitI1, j))
3635 return false;
3636 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003637 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003638 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003639}
3640
Craig Topper94438ba2011-12-16 08:06:31 +00003641bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N, bool HasAVX2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003642 SmallVector<int, 8> M;
3643 N->getMask(M);
Craig Topper94438ba2011-12-16 08:06:31 +00003644 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0), HasAVX2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003645}
3646
Evan Cheng017dcc62006-04-21 01:05:10 +00003647/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3648/// specifies a shuffle of elements that is suitable for input to MOVSS,
3649/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003650static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003651 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003652 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003653
3654 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003655
Nate Begeman9008ca62009-04-27 18:41:29 +00003656 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003657 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003658
Nate Begeman9008ca62009-04-27 18:41:29 +00003659 for (int i = 1; i < NumElts; ++i)
3660 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003661 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003662
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003663 return true;
3664}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003665
Nate Begeman9008ca62009-04-27 18:41:29 +00003666bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3667 SmallVector<int, 8> M;
3668 N->getMask(M);
3669 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003670}
3671
Craig Topper70b883b2011-11-28 10:14:51 +00003672/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003673/// as permutations between 128-bit chunks or halves. As an example: this
3674/// shuffle bellow:
3675/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3676/// The first half comes from the second half of V1 and the second half from the
3677/// the second half of V2.
Craig Topper70b883b2011-11-28 10:14:51 +00003678static bool isVPERM2X128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3679 bool HasAVX) {
3680 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003681 return false;
3682
3683 // The shuffle result is divided into half A and half B. In total the two
3684 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3685 // B must come from C, D, E or F.
3686 int HalfSize = VT.getVectorNumElements()/2;
3687 bool MatchA = false, MatchB = false;
3688
3689 // Check if A comes from one of C, D, E, F.
3690 for (int Half = 0; Half < 4; ++Half) {
3691 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3692 MatchA = true;
3693 break;
3694 }
3695 }
3696
3697 // Check if B comes from one of C, D, E, F.
3698 for (int Half = 0; Half < 4; ++Half) {
3699 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3700 MatchB = true;
3701 break;
3702 }
3703 }
3704
3705 return MatchA && MatchB;
3706}
3707
Craig Topper70b883b2011-11-28 10:14:51 +00003708/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3709/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003710static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003711 EVT VT = SVOp->getValueType(0);
3712
3713 int HalfSize = VT.getVectorNumElements()/2;
3714
3715 int FstHalf = 0, SndHalf = 0;
3716 for (int i = 0; i < HalfSize; ++i) {
3717 if (SVOp->getMaskElt(i) > 0) {
3718 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3719 break;
3720 }
3721 }
3722 for (int i = HalfSize; i < HalfSize*2; ++i) {
3723 if (SVOp->getMaskElt(i) > 0) {
3724 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3725 break;
3726 }
3727 }
3728
3729 return (FstHalf | (SndHalf << 4));
3730}
3731
Craig Topper70b883b2011-11-28 10:14:51 +00003732/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003733/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3734/// Note that VPERMIL mask matching is different depending whether theunderlying
3735/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3736/// to the same elements of the low, but to the higher half of the source.
3737/// In VPERMILPD the two lanes could be shuffled independently of each other
3738/// with the same restriction that lanes can't be crossed.
Craig Topper70b883b2011-11-28 10:14:51 +00003739static bool isVPERMILPMask(const SmallVectorImpl<int> &Mask, EVT VT,
3740 bool HasAVX) {
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003741 int NumElts = VT.getVectorNumElements();
3742 int NumLanes = VT.getSizeInBits()/128;
3743
Craig Topper70b883b2011-11-28 10:14:51 +00003744 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003745 return false;
3746
Craig Topper70b883b2011-11-28 10:14:51 +00003747 // Only match 256-bit with 32/64-bit types
3748 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003749 return false;
3750
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003751 int LaneSize = NumElts/NumLanes;
Craig Topper70b883b2011-11-28 10:14:51 +00003752 for (int l = 0; l != NumLanes; ++l) {
3753 int LaneStart = l*LaneSize;
3754 for (int i = 0; i != LaneSize; ++i) {
3755 if (!isUndefOrInRange(Mask[i+LaneStart], LaneStart, LaneStart+LaneSize))
3756 return false;
3757 if (NumElts == 4 || l == 0)
3758 continue;
3759 // VPERMILPS handling
3760 if (Mask[i] < 0)
3761 continue;
3762 if (!isUndefOrEqual(Mask[i+LaneStart], Mask[i]+LaneSize))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003763 return false;
3764 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003765 }
3766
3767 return true;
3768}
3769
Craig Topper70b883b2011-11-28 10:14:51 +00003770/// getShuffleVPERMILPImmediate - Return the appropriate immediate to shuffle
3771/// the specified VECTOR_MASK mask with VPERMILPS/D* instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003772static unsigned getShuffleVPERMILPImmediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003773 EVT VT = SVOp->getValueType(0);
3774
3775 int NumElts = VT.getVectorNumElements();
3776 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003777 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003778
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003779 // Although the mask is equal for both lanes do it twice to get the cases
3780 // where a mask will match because the same mask element is undef on the
3781 // first half but valid on the second. This would get pathological cases
3782 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Craig Topper70b883b2011-11-28 10:14:51 +00003783 unsigned Shift = (LaneSize == 4) ? 2 : 1;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003784 unsigned Mask = 0;
Craig Topper70b883b2011-11-28 10:14:51 +00003785 for (int i = 0; i != NumElts; ++i) {
3786 int MaskElt = SVOp->getMaskElt(i);
3787 if (MaskElt < 0)
3788 continue;
3789 MaskElt %= LaneSize;
3790 unsigned Shamt = i;
3791 // VPERMILPSY, the mask of the first half must be equal to the second one
3792 if (NumElts == 8) Shamt %= LaneSize;
3793 Mask |= MaskElt << (Shamt*Shift);
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003794 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003795
3796 return Mask;
3797}
3798
Evan Cheng017dcc62006-04-21 01:05:10 +00003799/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3800/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003801/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003802static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003803 bool V2IsSplat = false, bool V2IsUndef = false) {
3804 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003805 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003806 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003807
Nate Begeman9008ca62009-04-27 18:41:29 +00003808 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003809 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003810
Nate Begeman9008ca62009-04-27 18:41:29 +00003811 for (int i = 1; i < NumOps; ++i)
3812 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3813 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3814 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003815 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003816
Evan Cheng39623da2006-04-20 08:58:49 +00003817 return true;
3818}
3819
Nate Begeman9008ca62009-04-27 18:41:29 +00003820static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003821 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003822 SmallVector<int, 8> M;
3823 N->getMask(M);
3824 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003825}
3826
Evan Chengd9539472006-04-14 21:59:03 +00003827/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3828/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003829/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3830bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3831 const X86Subtarget *Subtarget) {
Craig Topperc0d82852011-11-22 00:44:41 +00003832 if (!Subtarget->hasSSE3orAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003833 return false;
3834
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003835 // The second vector must be undef
3836 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3837 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003838
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003839 EVT VT = N->getValueType(0);
3840 unsigned NumElems = VT.getVectorNumElements();
3841
3842 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3843 (VT.getSizeInBits() == 256 && NumElems != 8))
3844 return false;
3845
3846 // "i+1" is the value the indexed mask element must have
3847 for (unsigned i = 0; i < NumElems; i += 2)
3848 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3849 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003850 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003851
3852 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003853}
3854
3855/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3856/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003857/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3858bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3859 const X86Subtarget *Subtarget) {
Craig Topperc0d82852011-11-22 00:44:41 +00003860 if (!Subtarget->hasSSE3orAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003861 return false;
3862
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003863 // The second vector must be undef
3864 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3865 return false;
3866
3867 EVT VT = N->getValueType(0);
3868 unsigned NumElems = VT.getVectorNumElements();
3869
3870 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3871 (VT.getSizeInBits() == 256 && NumElems != 8))
3872 return false;
3873
3874 // "i" is the value the indexed mask element must have
3875 for (unsigned i = 0; i < NumElems; i += 2)
3876 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3877 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003878 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003879
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003880 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003881}
3882
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003883/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3884/// specifies a shuffle of elements that is suitable for input to 256-bit
3885/// version of MOVDDUP.
Craig Topperbeabc6c2011-12-05 06:56:46 +00003886static bool isMOVDDUPYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3887 bool HasAVX) {
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003888 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003889
Craig Topperbeabc6c2011-12-05 06:56:46 +00003890 if (!HasAVX || VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003891 return false;
3892
3893 for (int i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003894 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003895 return false;
3896 for (int i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003897 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003898 return false;
3899 return true;
3900}
3901
Evan Cheng0b457f02008-09-25 20:50:48 +00003902/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003903/// specifies a shuffle of elements that is suitable for input to 128-bit
3904/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003905bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003906 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003907
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003908 if (VT.getSizeInBits() != 128)
3909 return false;
3910
3911 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003912 for (int i = 0; i < e; ++i)
3913 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003914 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003915 for (int i = 0; i < e; ++i)
3916 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003917 return false;
3918 return true;
3919}
3920
David Greenec38a03e2011-02-03 15:50:00 +00003921/// isVEXTRACTF128Index - Return true if the specified
3922/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3923/// suitable for input to VEXTRACTF128.
3924bool X86::isVEXTRACTF128Index(SDNode *N) {
3925 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3926 return false;
3927
3928 // The index should be aligned on a 128-bit boundary.
3929 uint64_t Index =
3930 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3931
3932 unsigned VL = N->getValueType(0).getVectorNumElements();
3933 unsigned VBits = N->getValueType(0).getSizeInBits();
3934 unsigned ElSize = VBits / VL;
3935 bool Result = (Index * ElSize) % 128 == 0;
3936
3937 return Result;
3938}
3939
David Greeneccacdc12011-02-04 16:08:29 +00003940/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3941/// operand specifies a subvector insert that is suitable for input to
3942/// VINSERTF128.
3943bool X86::isVINSERTF128Index(SDNode *N) {
3944 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3945 return false;
3946
3947 // The index should be aligned on a 128-bit boundary.
3948 uint64_t Index =
3949 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3950
3951 unsigned VL = N->getValueType(0).getVectorNumElements();
3952 unsigned VBits = N->getValueType(0).getSizeInBits();
3953 unsigned ElSize = VBits / VL;
3954 bool Result = (Index * ElSize) % 128 == 0;
3955
3956 return Result;
3957}
3958
Evan Cheng63d33002006-03-22 08:01:21 +00003959/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003960/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003961unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003962 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3963 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3964
Evan Chengb9df0ca2006-03-22 02:53:00 +00003965 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3966 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003967 for (int i = 0; i < NumOperands; ++i) {
3968 int Val = SVOp->getMaskElt(NumOperands-i-1);
3969 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003970 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003971 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003972 if (i != NumOperands - 1)
3973 Mask <<= Shift;
3974 }
Evan Cheng63d33002006-03-22 08:01:21 +00003975 return Mask;
3976}
3977
Evan Cheng506d3df2006-03-29 23:07:14 +00003978/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003979/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003980unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003981 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003982 unsigned Mask = 0;
3983 // 8 nodes, but we only care about the last 4.
3984 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003985 int Val = SVOp->getMaskElt(i);
3986 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003987 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003988 if (i != 4)
3989 Mask <<= 2;
3990 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003991 return Mask;
3992}
3993
3994/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003995/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003996unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003997 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003998 unsigned Mask = 0;
3999 // 8 nodes, but we only care about the first 4.
4000 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004001 int Val = SVOp->getMaskElt(i);
4002 if (Val >= 0)
4003 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004004 if (i != 0)
4005 Mask <<= 2;
4006 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004007 return Mask;
4008}
4009
Nate Begemana09008b2009-10-19 02:17:23 +00004010/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4011/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004012static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4013 EVT VT = SVOp->getValueType(0);
4014 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004015 int Val = 0;
4016
4017 unsigned i, e;
Craig Topperd93e4c32011-12-11 19:12:35 +00004018 for (i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004019 Val = SVOp->getMaskElt(i);
4020 if (Val >= 0)
4021 break;
4022 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004023 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004024 return (Val - i) * EltSize;
4025}
4026
David Greenec38a03e2011-02-03 15:50:00 +00004027/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4028/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4029/// instructions.
4030unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4031 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4032 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4033
4034 uint64_t Index =
4035 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4036
4037 EVT VecVT = N->getOperand(0).getValueType();
4038 EVT ElVT = VecVT.getVectorElementType();
4039
4040 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004041 return Index / NumElemsPerChunk;
4042}
4043
David Greeneccacdc12011-02-04 16:08:29 +00004044/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4045/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4046/// instructions.
4047unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4048 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4049 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4050
4051 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004052 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004053
4054 EVT VecVT = N->getValueType(0);
4055 EVT ElVT = VecVT.getVectorElementType();
4056
4057 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004058 return Index / NumElemsPerChunk;
4059}
4060
Evan Cheng37b73872009-07-30 08:33:02 +00004061/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4062/// constant +0.0.
4063bool X86::isZeroNode(SDValue Elt) {
4064 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004065 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004066 (isa<ConstantFPSDNode>(Elt) &&
4067 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4068}
4069
Nate Begeman9008ca62009-04-27 18:41:29 +00004070/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4071/// their permute mask.
4072static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4073 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004074 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004075 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004076 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004077
Nate Begeman5a5ca152009-04-29 05:20:52 +00004078 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004079 int idx = SVOp->getMaskElt(i);
4080 if (idx < 0)
4081 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004082 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004083 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004084 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004085 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004086 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004087 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4088 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004089}
4090
Evan Cheng533a0aa2006-04-19 20:35:22 +00004091/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4092/// match movhlps. The lower half elements should come from upper half of
4093/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004094/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004095static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004096 EVT VT = Op->getValueType(0);
4097 if (VT.getSizeInBits() != 128)
4098 return false;
4099 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004100 return false;
4101 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004102 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004103 return false;
4104 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004105 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004106 return false;
4107 return true;
4108}
4109
Evan Cheng5ced1d82006-04-06 23:23:56 +00004110/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004111/// is promoted to a vector. It also returns the LoadSDNode by reference if
4112/// required.
4113static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004114 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4115 return false;
4116 N = N->getOperand(0).getNode();
4117 if (!ISD::isNON_EXTLoad(N))
4118 return false;
4119 if (LD)
4120 *LD = cast<LoadSDNode>(N);
4121 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004122}
4123
Dan Gohman65fd6562011-11-03 21:49:52 +00004124// Test whether the given value is a vector value which will be legalized
4125// into a load.
4126static bool WillBeConstantPoolLoad(SDNode *N) {
4127 if (N->getOpcode() != ISD::BUILD_VECTOR)
4128 return false;
4129
4130 // Check for any non-constant elements.
4131 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4132 switch (N->getOperand(i).getNode()->getOpcode()) {
4133 case ISD::UNDEF:
4134 case ISD::ConstantFP:
4135 case ISD::Constant:
4136 break;
4137 default:
4138 return false;
4139 }
4140
4141 // Vectors of all-zeros and all-ones are materialized with special
4142 // instructions rather than being loaded.
4143 return !ISD::isBuildVectorAllZeros(N) &&
4144 !ISD::isBuildVectorAllOnes(N);
4145}
4146
Evan Cheng533a0aa2006-04-19 20:35:22 +00004147/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4148/// match movlp{s|d}. The lower half elements should come from lower half of
4149/// V1 (and in order), and the upper half elements should come from the upper
4150/// half of V2 (and in order). And since V1 will become the source of the
4151/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004152static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4153 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004154 EVT VT = Op->getValueType(0);
4155 if (VT.getSizeInBits() != 128)
4156 return false;
4157
Evan Cheng466685d2006-10-09 20:57:25 +00004158 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004159 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004160 // Is V2 is a vector load, don't do this transformation. We will try to use
4161 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004162 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004163 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004164
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004165 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004166
Evan Cheng533a0aa2006-04-19 20:35:22 +00004167 if (NumElems != 2 && NumElems != 4)
4168 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004169 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004170 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004171 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004172 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004173 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004174 return false;
4175 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004176}
4177
Evan Cheng39623da2006-04-20 08:58:49 +00004178/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4179/// all the same.
4180static bool isSplatVector(SDNode *N) {
4181 if (N->getOpcode() != ISD::BUILD_VECTOR)
4182 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004183
Dan Gohman475871a2008-07-27 21:46:04 +00004184 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004185 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4186 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004187 return false;
4188 return true;
4189}
4190
Evan Cheng213d2cf2007-05-17 18:45:50 +00004191/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004192/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004193/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004194static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004195 SDValue V1 = N->getOperand(0);
4196 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004197 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4198 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004199 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004200 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004201 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004202 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4203 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004204 if (Opc != ISD::BUILD_VECTOR ||
4205 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004206 return false;
4207 } else if (Idx >= 0) {
4208 unsigned Opc = V1.getOpcode();
4209 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4210 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004211 if (Opc != ISD::BUILD_VECTOR ||
4212 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004213 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004214 }
4215 }
4216 return true;
4217}
4218
4219/// getZeroVector - Returns a vector of specified type with all zero elements.
4220///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004221static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004222 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004223 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004224
Dale Johannesen0488fb62010-09-30 23:57:10 +00004225 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004226 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004227 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004228 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004229 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004230 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4231 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4232 } else { // SSE1
4233 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4234 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4235 }
4236 } else if (VT.getSizeInBits() == 256) { // AVX
4237 // 256-bit logic and arithmetic instructions in AVX are
4238 // all floating-point, no support for integer ops. Default
4239 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004240 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004241 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4242 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004243 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004244 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004245}
4246
Chris Lattner8a594482007-11-25 00:24:49 +00004247/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004248/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4249/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4250/// Then bitcast to their original type, ensuring they get CSE'd.
4251static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4252 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004253 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004254 assert((VT.is128BitVector() || VT.is256BitVector())
4255 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004256
Owen Anderson825b72b2009-08-11 20:47:22 +00004257 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004258 SDValue Vec;
4259 if (VT.getSizeInBits() == 256) {
4260 if (HasAVX2) { // AVX2
4261 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4262 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4263 } else { // AVX
4264 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4265 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4266 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4267 Vec = Insert128BitVector(InsV, Vec,
4268 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4269 }
4270 } else {
4271 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004272 }
4273
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004274 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004275}
4276
Evan Cheng39623da2006-04-20 08:58:49 +00004277/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4278/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004279static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004280 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004281 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004282
Evan Cheng39623da2006-04-20 08:58:49 +00004283 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004284 SmallVector<int, 8> MaskVec;
4285 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004286
Nate Begeman5a5ca152009-04-29 05:20:52 +00004287 for (unsigned i = 0; i != NumElems; ++i) {
4288 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004289 MaskVec[i] = NumElems;
4290 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004291 }
Evan Cheng39623da2006-04-20 08:58:49 +00004292 }
Evan Cheng39623da2006-04-20 08:58:49 +00004293 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004294 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4295 SVOp->getOperand(1), &MaskVec[0]);
4296 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004297}
4298
Evan Cheng017dcc62006-04-21 01:05:10 +00004299/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4300/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004301static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004302 SDValue V2) {
4303 unsigned NumElems = VT.getVectorNumElements();
4304 SmallVector<int, 8> Mask;
4305 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004306 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004307 Mask.push_back(i);
4308 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004309}
4310
Nate Begeman9008ca62009-04-27 18:41:29 +00004311/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004312static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004313 SDValue V2) {
4314 unsigned NumElems = VT.getVectorNumElements();
4315 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004316 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004317 Mask.push_back(i);
4318 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004319 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004320 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004321}
4322
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004323/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004324static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004325 SDValue V2) {
4326 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004327 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004328 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004329 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004330 Mask.push_back(i + Half);
4331 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004332 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004333 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004334}
4335
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004336// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004337// a generic shuffle instruction because the target has no such instructions.
4338// Generate shuffles which repeat i16 and i8 several times until they can be
4339// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004340static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004341 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004342 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004343 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004344
Nate Begeman9008ca62009-04-27 18:41:29 +00004345 while (NumElems > 4) {
4346 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004347 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004348 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004349 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004350 EltNo -= NumElems/2;
4351 }
4352 NumElems >>= 1;
4353 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004354 return V;
4355}
Eric Christopherfd179292009-08-27 18:07:15 +00004356
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004357/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4358static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4359 EVT VT = V.getValueType();
4360 DebugLoc dl = V.getDebugLoc();
4361 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4362 && "Vector size not supported");
4363
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004364 if (VT.getSizeInBits() == 128) {
4365 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004366 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004367 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4368 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004369 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004370 // To use VPERMILPS to splat scalars, the second half of indicies must
4371 // refer to the higher part, which is a duplication of the lower one,
4372 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004373 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4374 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004375
4376 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4377 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4378 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004379 }
4380
4381 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4382}
4383
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004384/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004385static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4386 EVT SrcVT = SV->getValueType(0);
4387 SDValue V1 = SV->getOperand(0);
4388 DebugLoc dl = SV->getDebugLoc();
4389
4390 int EltNo = SV->getSplatIndex();
4391 int NumElems = SrcVT.getVectorNumElements();
4392 unsigned Size = SrcVT.getSizeInBits();
4393
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004394 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4395 "Unknown how to promote splat for type");
4396
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004397 // Extract the 128-bit part containing the splat element and update
4398 // the splat element index when it refers to the higher register.
4399 if (Size == 256) {
4400 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4401 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4402 if (Idx > 0)
4403 EltNo -= NumElems/2;
4404 }
4405
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004406 // All i16 and i8 vector types can't be used directly by a generic shuffle
4407 // instruction because the target has no such instruction. Generate shuffles
4408 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004409 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004410 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004411 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004412 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004413
4414 // Recreate the 256-bit vector and place the same 128-bit vector
4415 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004416 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004417 if (Size == 256) {
4418 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4419 DAG.getConstant(0, MVT::i32), DAG, dl);
4420 V1 = Insert128BitVector(InsV, V1,
4421 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4422 }
4423
4424 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004425}
4426
Evan Chengba05f722006-04-21 23:03:30 +00004427/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004428/// vector of zero or undef vector. This produces a shuffle where the low
4429/// element of V2 is swizzled into the zero/undef vector, landing at element
4430/// 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 +00004431static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004432 bool isZero, bool HasXMMInt,
4433 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004434 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004435 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004436 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004437 unsigned NumElems = VT.getVectorNumElements();
4438 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004439 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004440 // If this is the insertion idx, put the low elt of V2 here.
4441 MaskVec.push_back(i == Idx ? NumElems : i);
4442 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004443}
4444
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004445/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4446/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004447static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4448 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004449 if (Depth == 6)
4450 return SDValue(); // Limit search depth.
4451
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004452 SDValue V = SDValue(N, 0);
4453 EVT VT = V.getValueType();
4454 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004455
4456 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4457 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4458 Index = SV->getMaskElt(Index);
4459
4460 if (Index < 0)
4461 return DAG.getUNDEF(VT.getVectorElementType());
4462
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004463 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004464 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004465 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004466 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004467
4468 // Recurse into target specific vector shuffles to find scalars.
4469 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004470 int NumElems = VT.getVectorNumElements();
4471 SmallVector<unsigned, 16> ShuffleMask;
4472 SDValue ImmN;
4473
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004474 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004475 case X86ISD::SHUFPS:
4476 case X86ISD::SHUFPD:
4477 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper36e36ac2011-11-29 07:49:05 +00004478 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4479 ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004480 break;
Craig Topper34671b82011-12-06 08:21:25 +00004481 case X86ISD::UNPCKH:
Craig Topper3d8c2ce2011-12-06 05:31:16 +00004482 DecodeUNPCKHMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004483 break;
Craig Topper34671b82011-12-06 08:21:25 +00004484 case X86ISD::UNPCKL:
Craig Topper3d8c2ce2011-12-06 05:31:16 +00004485 DecodeUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004486 break;
4487 case X86ISD::MOVHLPS:
4488 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4489 break;
4490 case X86ISD::MOVLHPS:
4491 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4492 break;
4493 case X86ISD::PSHUFD:
4494 ImmN = N->getOperand(N->getNumOperands()-1);
4495 DecodePSHUFMask(NumElems,
4496 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4497 ShuffleMask);
4498 break;
4499 case X86ISD::PSHUFHW:
4500 ImmN = N->getOperand(N->getNumOperands()-1);
4501 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4502 ShuffleMask);
4503 break;
4504 case X86ISD::PSHUFLW:
4505 ImmN = N->getOperand(N->getNumOperands()-1);
4506 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4507 ShuffleMask);
4508 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004509 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004510 case X86ISD::MOVSD: {
4511 // The index 0 always comes from the first element of the second source,
4512 // this is why MOVSS and MOVSD are used in the first place. The other
4513 // elements come from the other positions of the first source vector.
4514 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004515 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4516 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004517 }
Craig Topper316cd2a2011-11-30 06:25:25 +00004518 case X86ISD::VPERMILP:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004519 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper316cd2a2011-11-30 06:25:25 +00004520 DecodeVPERMILPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004521 ShuffleMask);
4522 break;
Craig Topperec24e612011-11-30 07:47:51 +00004523 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004524 ImmN = N->getOperand(N->getNumOperands()-1);
4525 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4526 ShuffleMask);
4527 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004528 case X86ISD::MOVDDUP:
4529 case X86ISD::MOVLHPD:
4530 case X86ISD::MOVLPD:
4531 case X86ISD::MOVLPS:
4532 case X86ISD::MOVSHDUP:
4533 case X86ISD::MOVSLDUP:
4534 case X86ISD::PALIGN:
4535 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004536 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004537 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004538 return SDValue();
4539 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004540
4541 Index = ShuffleMask[Index];
4542 if (Index < 0)
4543 return DAG.getUNDEF(VT.getVectorElementType());
4544
4545 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4546 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4547 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004548 }
4549
4550 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004551 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004552 V = V.getOperand(0);
4553 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004554 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004555
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004556 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004557 return SDValue();
4558 }
4559
4560 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4561 return (Index == 0) ? V.getOperand(0)
4562 : DAG.getUNDEF(VT.getVectorElementType());
4563
4564 if (V.getOpcode() == ISD::BUILD_VECTOR)
4565 return V.getOperand(Index);
4566
4567 return SDValue();
4568}
4569
4570/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4571/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004572/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004573static
4574unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4575 bool ZerosFromLeft, SelectionDAG &DAG) {
4576 int i = 0;
4577
4578 while (i < NumElems) {
4579 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004580 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004581 if (!(Elt.getNode() &&
4582 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4583 break;
4584 ++i;
4585 }
4586
4587 return i;
4588}
4589
4590/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4591/// MaskE correspond consecutively to elements from one of the vector operands,
4592/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4593static
4594bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4595 int OpIdx, int NumElems, unsigned &OpNum) {
4596 bool SeenV1 = false;
4597 bool SeenV2 = false;
4598
4599 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4600 int Idx = SVOp->getMaskElt(i);
4601 // Ignore undef indicies
4602 if (Idx < 0)
4603 continue;
4604
4605 if (Idx < NumElems)
4606 SeenV1 = true;
4607 else
4608 SeenV2 = true;
4609
4610 // Only accept consecutive elements from the same vector
4611 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4612 return false;
4613 }
4614
4615 OpNum = SeenV1 ? 0 : 1;
4616 return true;
4617}
4618
4619/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4620/// logical left shift of a vector.
4621static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4622 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4623 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4624 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4625 false /* check zeros from right */, DAG);
4626 unsigned OpSrc;
4627
4628 if (!NumZeros)
4629 return false;
4630
4631 // Considering the elements in the mask that are not consecutive zeros,
4632 // check if they consecutively come from only one of the source vectors.
4633 //
4634 // V1 = {X, A, B, C} 0
4635 // \ \ \ /
4636 // vector_shuffle V1, V2 <1, 2, 3, X>
4637 //
4638 if (!isShuffleMaskConsecutive(SVOp,
4639 0, // Mask Start Index
4640 NumElems-NumZeros-1, // Mask End Index
4641 NumZeros, // Where to start looking in the src vector
4642 NumElems, // Number of elements in vector
4643 OpSrc)) // Which source operand ?
4644 return false;
4645
4646 isLeft = false;
4647 ShAmt = NumZeros;
4648 ShVal = SVOp->getOperand(OpSrc);
4649 return true;
4650}
4651
4652/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4653/// logical left shift of a vector.
4654static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4655 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4656 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4657 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4658 true /* check zeros from left */, DAG);
4659 unsigned OpSrc;
4660
4661 if (!NumZeros)
4662 return false;
4663
4664 // Considering the elements in the mask that are not consecutive zeros,
4665 // check if they consecutively come from only one of the source vectors.
4666 //
4667 // 0 { A, B, X, X } = V2
4668 // / \ / /
4669 // vector_shuffle V1, V2 <X, X, 4, 5>
4670 //
4671 if (!isShuffleMaskConsecutive(SVOp,
4672 NumZeros, // Mask Start Index
4673 NumElems-1, // Mask End Index
4674 0, // Where to start looking in the src vector
4675 NumElems, // Number of elements in vector
4676 OpSrc)) // Which source operand ?
4677 return false;
4678
4679 isLeft = true;
4680 ShAmt = NumZeros;
4681 ShVal = SVOp->getOperand(OpSrc);
4682 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004683}
4684
4685/// isVectorShift - Returns true if the shuffle can be implemented as a
4686/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004687static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004688 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004689 // Although the logic below support any bitwidth size, there are no
4690 // shift instructions which handle more than 128-bit vectors.
4691 if (SVOp->getValueType(0).getSizeInBits() > 128)
4692 return false;
4693
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004694 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4695 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4696 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004697
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004698 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004699}
4700
Evan Chengc78d3b42006-04-24 18:01:45 +00004701/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4702///
Dan Gohman475871a2008-07-27 21:46:04 +00004703static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004704 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004705 SelectionDAG &DAG,
4706 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004707 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004708 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004709
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004710 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004711 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004712 bool First = true;
4713 for (unsigned i = 0; i < 16; ++i) {
4714 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4715 if (ThisIsNonZero && First) {
4716 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004717 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004718 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004719 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004720 First = false;
4721 }
4722
4723 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004724 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004725 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4726 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004727 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004728 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004729 }
4730 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004731 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4732 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4733 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004734 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004735 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004736 } else
4737 ThisElt = LastElt;
4738
Gabor Greifba36cb52008-08-28 21:40:38 +00004739 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004740 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004741 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004742 }
4743 }
4744
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004745 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004746}
4747
Bill Wendlinga348c562007-03-22 18:42:45 +00004748/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004749///
Dan Gohman475871a2008-07-27 21:46:04 +00004750static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004751 unsigned NumNonZero, unsigned NumZero,
4752 SelectionDAG &DAG,
4753 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004754 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004755 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004756
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004757 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004758 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004759 bool First = true;
4760 for (unsigned i = 0; i < 8; ++i) {
4761 bool isNonZero = (NonZeros & (1 << i)) != 0;
4762 if (isNonZero) {
4763 if (First) {
4764 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004765 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004766 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004767 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004768 First = false;
4769 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004770 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004771 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004772 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004773 }
4774 }
4775
4776 return V;
4777}
4778
Evan Chengf26ffe92008-05-29 08:22:04 +00004779/// getVShift - Return a vector logical shift node.
4780///
Owen Andersone50ed302009-08-10 22:56:29 +00004781static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004782 unsigned NumBits, SelectionDAG &DAG,
4783 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004784 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004785 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004786 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004787 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4788 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004789 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004790 DAG.getConstant(NumBits,
4791 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004792}
4793
Dan Gohman475871a2008-07-27 21:46:04 +00004794SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004795X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004796 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004797
Evan Chengc3630942009-12-09 21:00:30 +00004798 // Check if the scalar load can be widened into a vector load. And if
4799 // the address is "base + cst" see if the cst can be "absorbed" into
4800 // the shuffle mask.
4801 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4802 SDValue Ptr = LD->getBasePtr();
4803 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4804 return SDValue();
4805 EVT PVT = LD->getValueType(0);
4806 if (PVT != MVT::i32 && PVT != MVT::f32)
4807 return SDValue();
4808
4809 int FI = -1;
4810 int64_t Offset = 0;
4811 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4812 FI = FINode->getIndex();
4813 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004814 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004815 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4816 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4817 Offset = Ptr.getConstantOperandVal(1);
4818 Ptr = Ptr.getOperand(0);
4819 } else {
4820 return SDValue();
4821 }
4822
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004823 // FIXME: 256-bit vector instructions don't require a strict alignment,
4824 // improve this code to support it better.
4825 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004826 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004827 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004828 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004829 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004830 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004831 // Can't change the alignment. FIXME: It's possible to compute
4832 // the exact stack offset and reference FI + adjust offset instead.
4833 // If someone *really* cares about this. That's the way to implement it.
4834 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004835 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004836 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004837 }
4838 }
4839
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004840 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004841 // Ptr + (Offset & ~15).
4842 if (Offset < 0)
4843 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004844 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004845 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004846 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004847 if (StartOffset)
4848 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4849 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4850
4851 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004852 int NumElems = VT.getVectorNumElements();
4853
4854 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4855 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4856 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004857 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004858 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004859
4860 // Canonicalize it to a v4i32 or v8i32 shuffle.
4861 SmallVector<int, 8> Mask;
4862 for (int i = 0; i < NumElems; ++i)
4863 Mask.push_back(EltNo);
4864
4865 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4866 return DAG.getNode(ISD::BITCAST, dl, NVT,
4867 DAG.getVectorShuffle(CanonVT, dl, V1,
4868 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004869 }
4870
4871 return SDValue();
4872}
4873
Michael J. Spencerec38de22010-10-10 22:04:20 +00004874/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4875/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004876/// load which has the same value as a build_vector whose operands are 'elts'.
4877///
4878/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004879///
Nate Begeman1449f292010-03-24 22:19:06 +00004880/// FIXME: we'd also like to handle the case where the last elements are zero
4881/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4882/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004883static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004884 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004885 EVT EltVT = VT.getVectorElementType();
4886 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004887
Nate Begemanfdea31a2010-03-24 20:49:50 +00004888 LoadSDNode *LDBase = NULL;
4889 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004890
Nate Begeman1449f292010-03-24 22:19:06 +00004891 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004892 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004893 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004894 for (unsigned i = 0; i < NumElems; ++i) {
4895 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004896
Nate Begemanfdea31a2010-03-24 20:49:50 +00004897 if (!Elt.getNode() ||
4898 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4899 return SDValue();
4900 if (!LDBase) {
4901 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4902 return SDValue();
4903 LDBase = cast<LoadSDNode>(Elt.getNode());
4904 LastLoadedElt = i;
4905 continue;
4906 }
4907 if (Elt.getOpcode() == ISD::UNDEF)
4908 continue;
4909
4910 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4911 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4912 return SDValue();
4913 LastLoadedElt = i;
4914 }
Nate Begeman1449f292010-03-24 22:19:06 +00004915
4916 // If we have found an entire vector of loads and undefs, then return a large
4917 // load of the entire vector width starting at the base pointer. If we found
4918 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004919 if (LastLoadedElt == NumElems - 1) {
4920 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004921 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004922 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004923 LDBase->isVolatile(), LDBase->isNonTemporal(),
4924 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004925 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004926 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004927 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004928 LDBase->isInvariant(), LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00004929 } else if (NumElems == 4 && LastLoadedElt == 1 &&
4930 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004931 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4932 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00004933 SDValue ResNode =
4934 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
4935 LDBase->getPointerInfo(),
4936 LDBase->getAlignment(),
4937 false/*isVolatile*/, true/*ReadMem*/,
4938 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004939 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004940 }
4941 return SDValue();
4942}
4943
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004944/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
4945/// a vbroadcast node. We support two patterns:
4946/// 1. A splat BUILD_VECTOR which uses a single scalar load.
4947/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
4948/// a scalar load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004949/// The scalar load node is returned when a pattern is found,
4950/// or SDValue() otherwise.
4951static SDValue isVectorBroadcast(SDValue &Op, bool hasAVX2) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004952 EVT VT = Op.getValueType();
4953 SDValue V = Op;
4954
4955 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
4956 V = V.getOperand(0);
4957
4958 //A suspected load to be broadcasted.
4959 SDValue Ld;
4960
4961 switch (V.getOpcode()) {
4962 default:
4963 // Unknown pattern found.
4964 return SDValue();
4965
4966 case ISD::BUILD_VECTOR: {
4967 // The BUILD_VECTOR node must be a splat.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004968 if (!isSplatVector(V.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004969 return SDValue();
4970
4971 Ld = V.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004972
4973 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004974 // of its users are from the BUILD_VECTOR node.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004975 if (!Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004976 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004977 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004978 }
4979
4980 case ISD::VECTOR_SHUFFLE: {
4981 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
4982
4983 // Shuffles must have a splat mask where the first element is
4984 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004985 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004986 return SDValue();
4987
4988 SDValue Sc = Op.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004989 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004990 return SDValue();
4991
4992 Ld = Sc.getOperand(0);
4993
4994 // The scalar_to_vector node and the suspected
4995 // load node must have exactly one user.
4996 if (!Sc.hasOneUse() || !Ld.hasOneUse())
4997 return SDValue();
4998 break;
4999 }
5000 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005001
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005002 // The scalar source must be a normal load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005003 if (!ISD::isNormalLoad(Ld.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005004 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005005
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005006 bool Is256 = VT.getSizeInBits() == 256;
5007 bool Is128 = VT.getSizeInBits() == 128;
5008 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5009
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005010 if (hasAVX2) {
5011 // VBroadcast to YMM
5012 if (Is256 && (ScalarSize == 8 || ScalarSize == 16 ||
5013 ScalarSize == 32 || ScalarSize == 64 ))
5014 return Ld;
5015
5016 // VBroadcast to XMM
5017 if (Is128 && (ScalarSize == 8 || ScalarSize == 32 ||
5018 ScalarSize == 16 || ScalarSize == 64 ))
5019 return Ld;
5020 }
5021
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005022 // VBroadcast to YMM
5023 if (Is256 && (ScalarSize == 32 || ScalarSize == 64))
5024 return Ld;
5025
5026 // VBroadcast to XMM
5027 if (Is128 && (ScalarSize == 32))
5028 return Ld;
5029
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005030
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005031 // Unsupported broadcast.
5032 return SDValue();
5033}
5034
Evan Chengc3630942009-12-09 21:00:30 +00005035SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005036X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005037 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005038
David Greenef125a292011-02-08 19:04:41 +00005039 EVT VT = Op.getValueType();
5040 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005041 unsigned NumElems = Op.getNumOperands();
5042
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005043 // Vectors containing all zeros can be matched by pxor and xorps later
5044 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5045 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5046 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005047 if (Op.getValueType() == MVT::v4i32 ||
5048 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005049 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005050
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005051 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005052 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005053
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005054 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005055 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5056 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005057 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper745a86b2011-11-19 22:34:59 +00005058 if (Op.getValueType() == MVT::v4i32 ||
5059 (Op.getValueType() == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005060 return Op;
5061
Craig Topper745a86b2011-11-19 22:34:59 +00005062 return getOnesVector(Op.getValueType(), Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005063 }
5064
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005065 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005066 if (Subtarget->hasAVX() && LD.getNode())
5067 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
5068
Owen Andersone50ed302009-08-10 22:56:29 +00005069 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005070
Evan Cheng0db9fe62006-04-25 20:13:52 +00005071 unsigned NumZero = 0;
5072 unsigned NumNonZero = 0;
5073 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005074 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005075 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005076 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005077 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005078 if (Elt.getOpcode() == ISD::UNDEF)
5079 continue;
5080 Values.insert(Elt);
5081 if (Elt.getOpcode() != ISD::Constant &&
5082 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005083 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005084 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005085 NumZero++;
5086 else {
5087 NonZeros |= (1 << i);
5088 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005089 }
5090 }
5091
Chris Lattner97a2a562010-08-26 05:24:29 +00005092 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5093 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005094 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005095
Chris Lattner67f453a2008-03-09 05:42:06 +00005096 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005097 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005098 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005099 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005100
Chris Lattner62098042008-03-09 01:05:04 +00005101 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5102 // the value are obviously zero, truncate the value to i32 and do the
5103 // insertion that way. Only do this if the value is non-constant or if the
5104 // value is a constant being inserted into element 0. It is cheaper to do
5105 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005106 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005107 (!IsAllConstants || Idx == 0)) {
5108 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005109 // Handle SSE only.
5110 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5111 EVT VecVT = MVT::v4i32;
5112 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005113
Chris Lattner62098042008-03-09 01:05:04 +00005114 // Truncate the value (which may itself be a constant) to i32, and
5115 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005116 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005117 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005118 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005119 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005120
Chris Lattner62098042008-03-09 01:05:04 +00005121 // Now we have our 32-bit value zero extended in the low element of
5122 // a vector. If Idx != 0, swizzle it into place.
5123 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005124 SmallVector<int, 4> Mask;
5125 Mask.push_back(Idx);
5126 for (unsigned i = 1; i != VecElts; ++i)
5127 Mask.push_back(i);
5128 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005129 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005130 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005131 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005132 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005133 }
5134 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005135
Chris Lattner19f79692008-03-08 22:59:52 +00005136 // If we have a constant or non-constant insertion into the low element of
5137 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5138 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005139 // depending on what the source datatype is.
5140 if (Idx == 0) {
5141 if (NumZero == 0) {
5142 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005143 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5144 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005145 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5146 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005147 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005148 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005149 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5150 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Chad Rosier0660cfe2011-12-15 21:34:44 +00005151 unsigned NumBits = VT.getSizeInBits();
5152 assert((NumBits == 128 || NumBits == 256) &&
5153 "Expected an SSE or AVX value type!");
5154 EVT MiddleVT = NumBits == 128 ? MVT::v4i32 : MVT::v8i32;
Eli Friedman10415532009-06-06 06:05:10 +00005155 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5156 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005157 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005158 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005159 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005160 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005161
5162 // Is it a vector logical left shift?
5163 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005164 X86::isZeroNode(Op.getOperand(0)) &&
5165 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005166 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005167 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005168 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005169 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005170 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005171 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005172
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005173 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005174 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005175
Chris Lattner19f79692008-03-08 22:59:52 +00005176 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5177 // is a non-constant being inserted into an element other than the low one,
5178 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5179 // movd/movss) to move this into the low element, then shuffle it into
5180 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005181 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005182 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005183
Evan Cheng0db9fe62006-04-25 20:13:52 +00005184 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005185 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005186 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005187 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005188 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005189 MaskVec.push_back(i == Idx ? 0 : 1);
5190 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005191 }
5192 }
5193
Chris Lattner67f453a2008-03-09 05:42:06 +00005194 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005195 if (Values.size() == 1) {
5196 if (EVTBits == 32) {
5197 // Instead of a shuffle like this:
5198 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5199 // Check if it's possible to issue this instead.
5200 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5201 unsigned Idx = CountTrailingZeros_32(NonZeros);
5202 SDValue Item = Op.getOperand(Idx);
5203 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5204 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5205 }
Dan Gohman475871a2008-07-27 21:46:04 +00005206 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005207 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005208
Dan Gohmana3941172007-07-24 22:55:08 +00005209 // A vector full of immediates; various special cases are already
5210 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005211 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005212 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005213
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005214 // For AVX-length vectors, build the individual 128-bit pieces and use
5215 // shuffles to put them in place.
5216 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5217 SmallVector<SDValue, 32> V;
5218 for (unsigned i = 0; i < NumElems; ++i)
5219 V.push_back(Op.getOperand(i));
5220
5221 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5222
5223 // Build both the lower and upper subvector.
5224 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5225 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5226 NumElems/2);
5227
5228 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005229 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5230 DAG.getConstant(0, MVT::i32), DAG, dl);
5231 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005232 DAG, dl);
5233 }
5234
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005235 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005236 if (EVTBits == 64) {
5237 if (NumNonZero == 1) {
5238 // One half is zero or undef.
5239 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005240 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005241 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005242 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005243 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005244 }
Dan Gohman475871a2008-07-27 21:46:04 +00005245 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005246 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005247
5248 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005249 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005250 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005251 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005252 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005253 }
5254
Bill Wendling826f36f2007-03-28 00:57:11 +00005255 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005256 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005257 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005258 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005259 }
5260
5261 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005262 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005263 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005264 if (NumElems == 4 && NumZero > 0) {
5265 for (unsigned i = 0; i < 4; ++i) {
5266 bool isZero = !(NonZeros & (1 << i));
5267 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005268 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005269 else
Dale Johannesenace16102009-02-03 19:33:06 +00005270 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005271 }
5272
5273 for (unsigned i = 0; i < 2; ++i) {
5274 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5275 default: break;
5276 case 0:
5277 V[i] = V[i*2]; // Must be a zero vector.
5278 break;
5279 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005280 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005281 break;
5282 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005283 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005284 break;
5285 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005286 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005287 break;
5288 }
5289 }
5290
Nate Begeman9008ca62009-04-27 18:41:29 +00005291 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005292 bool Reverse = (NonZeros & 0x3) == 2;
5293 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005294 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005295 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5296 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005297 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5298 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005299 }
5300
Nate Begemanfdea31a2010-03-24 20:49:50 +00005301 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5302 // Check for a build vector of consecutive loads.
5303 for (unsigned i = 0; i < NumElems; ++i)
5304 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005305
Nate Begemanfdea31a2010-03-24 20:49:50 +00005306 // Check for elements which are consecutive loads.
5307 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5308 if (LD.getNode())
5309 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005310
5311 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperc0d82852011-11-22 00:44:41 +00005312 if (getSubtarget()->hasSSE41orAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005313 SDValue Result;
5314 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5315 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5316 else
5317 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005318
Chris Lattner24faf612010-08-28 17:59:08 +00005319 for (unsigned i = 1; i < NumElems; ++i) {
5320 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5321 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005322 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005323 }
5324 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005325 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005326
Chris Lattner6e80e442010-08-28 17:15:43 +00005327 // Otherwise, expand into a number of unpckl*, start by extending each of
5328 // our (non-undef) elements to the full vector width with the element in the
5329 // bottom slot of the vector (which generates no code for SSE).
5330 for (unsigned i = 0; i < NumElems; ++i) {
5331 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5332 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5333 else
5334 V[i] = DAG.getUNDEF(VT);
5335 }
5336
5337 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005338 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5339 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5340 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005341 unsigned EltStride = NumElems >> 1;
5342 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005343 for (unsigned i = 0; i < EltStride; ++i) {
5344 // If V[i+EltStride] is undef and this is the first round of mixing,
5345 // then it is safe to just drop this shuffle: V[i] is already in the
5346 // right place, the one element (since it's the first round) being
5347 // inserted as undef can be dropped. This isn't safe for successive
5348 // rounds because they will permute elements within both vectors.
5349 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5350 EltStride == NumElems/2)
5351 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005352
Chris Lattner6e80e442010-08-28 17:15:43 +00005353 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005354 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005355 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005356 }
5357 return V[0];
5358 }
Dan Gohman475871a2008-07-27 21:46:04 +00005359 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005360}
5361
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005362// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5363// them in a MMX register. This is better than doing a stack convert.
5364static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005365 DebugLoc dl = Op.getDebugLoc();
5366 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005367
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005368 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5369 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5370 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005371 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005372 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5373 InVec = Op.getOperand(1);
5374 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5375 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005376 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005377 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5378 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5379 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005380 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005381 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5382 Mask[0] = 0; Mask[1] = 2;
5383 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5384 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005385 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005386}
5387
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005388// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5389// to create 256-bit vectors from two other 128-bit ones.
5390static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5391 DebugLoc dl = Op.getDebugLoc();
5392 EVT ResVT = Op.getValueType();
5393
5394 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5395
5396 SDValue V1 = Op.getOperand(0);
5397 SDValue V2 = Op.getOperand(1);
5398 unsigned NumElems = ResVT.getVectorNumElements();
5399
5400 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5401 DAG.getConstant(0, MVT::i32), DAG, dl);
5402 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5403 DAG, dl);
5404}
5405
5406SDValue
5407X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005408 EVT ResVT = Op.getValueType();
5409
5410 assert(Op.getNumOperands() == 2);
5411 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5412 "Unsupported CONCAT_VECTORS for value type");
5413
5414 // We support concatenate two MMX registers and place them in a MMX register.
5415 // This is better than doing a stack convert.
5416 if (ResVT.is128BitVector())
5417 return LowerMMXCONCAT_VECTORS(Op, DAG);
5418
5419 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5420 // from two other 128-bit ones.
5421 return LowerAVXCONCAT_VECTORS(Op, DAG);
5422}
5423
Nate Begemanb9a47b82009-02-23 08:49:38 +00005424// v8i16 shuffles - Prefer shuffles in the following order:
5425// 1. [all] pshuflw, pshufhw, optional move
5426// 2. [ssse3] 1 x pshufb
5427// 3. [ssse3] 2 x pshufb + 1 x por
5428// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005429SDValue
5430X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5431 SelectionDAG &DAG) const {
5432 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005433 SDValue V1 = SVOp->getOperand(0);
5434 SDValue V2 = SVOp->getOperand(1);
5435 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005436 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005437
Nate Begemanb9a47b82009-02-23 08:49:38 +00005438 // Determine if more than 1 of the words in each of the low and high quadwords
5439 // of the result come from the same quadword of one of the two inputs. Undef
5440 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005441 unsigned LoQuad[] = { 0, 0, 0, 0 };
5442 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005443 BitVector InputQuads(4);
5444 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005445 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005446 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005447 MaskVals.push_back(EltIdx);
5448 if (EltIdx < 0) {
5449 ++Quad[0];
5450 ++Quad[1];
5451 ++Quad[2];
5452 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005453 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005454 }
5455 ++Quad[EltIdx / 4];
5456 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005457 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005458
Nate Begemanb9a47b82009-02-23 08:49:38 +00005459 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005460 unsigned MaxQuad = 1;
5461 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005462 if (LoQuad[i] > MaxQuad) {
5463 BestLoQuad = i;
5464 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005465 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005466 }
5467
Nate Begemanb9a47b82009-02-23 08:49:38 +00005468 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005469 MaxQuad = 1;
5470 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005471 if (HiQuad[i] > MaxQuad) {
5472 BestHiQuad = i;
5473 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005474 }
5475 }
5476
Nate Begemanb9a47b82009-02-23 08:49:38 +00005477 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005478 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005479 // single pshufb instruction is necessary. If There are more than 2 input
5480 // quads, disable the next transformation since it does not help SSSE3.
5481 bool V1Used = InputQuads[0] || InputQuads[1];
5482 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperc0d82852011-11-22 00:44:41 +00005483 if (Subtarget->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005484 if (InputQuads.count() == 2 && V1Used && V2Used) {
5485 BestLoQuad = InputQuads.find_first();
5486 BestHiQuad = InputQuads.find_next(BestLoQuad);
5487 }
5488 if (InputQuads.count() > 2) {
5489 BestLoQuad = -1;
5490 BestHiQuad = -1;
5491 }
5492 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005493
Nate Begemanb9a47b82009-02-23 08:49:38 +00005494 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5495 // the shuffle mask. If a quad is scored as -1, that means that it contains
5496 // words from all 4 input quadwords.
5497 SDValue NewV;
5498 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005499 SmallVector<int, 8> MaskV;
5500 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5501 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005502 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005503 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5504 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5505 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005506
Nate Begemanb9a47b82009-02-23 08:49:38 +00005507 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5508 // source words for the shuffle, to aid later transformations.
5509 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005510 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005511 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005512 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005513 if (idx != (int)i)
5514 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005515 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005516 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005517 AllWordsInNewV = false;
5518 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005519 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005520
Nate Begemanb9a47b82009-02-23 08:49:38 +00005521 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5522 if (AllWordsInNewV) {
5523 for (int i = 0; i != 8; ++i) {
5524 int idx = MaskVals[i];
5525 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005526 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005527 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005528 if ((idx != i) && idx < 4)
5529 pshufhw = false;
5530 if ((idx != i) && idx > 3)
5531 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005532 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005533 V1 = NewV;
5534 V2Used = false;
5535 BestLoQuad = 0;
5536 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005537 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005538
Nate Begemanb9a47b82009-02-23 08:49:38 +00005539 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5540 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005541 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005542 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5543 unsigned TargetMask = 0;
5544 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005545 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005546 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5547 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5548 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005549 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005550 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005551 }
Eric Christopherfd179292009-08-27 18:07:15 +00005552
Nate Begemanb9a47b82009-02-23 08:49:38 +00005553 // If we have SSSE3, and all words of the result are from 1 input vector,
5554 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5555 // is present, fall back to case 4.
Craig Topperc0d82852011-11-22 00:44:41 +00005556 if (Subtarget->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005557 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005558
Nate Begemanb9a47b82009-02-23 08:49:38 +00005559 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005560 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005561 // mask, and elements that come from V1 in the V2 mask, so that the two
5562 // results can be OR'd together.
5563 bool TwoInputs = V1Used && V2Used;
5564 for (unsigned i = 0; i != 8; ++i) {
5565 int EltIdx = MaskVals[i] * 2;
5566 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005567 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5568 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005569 continue;
5570 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005571 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5572 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005573 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005574 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005575 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005576 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005577 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005578 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005579 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005580
Nate Begemanb9a47b82009-02-23 08:49:38 +00005581 // Calculate the shuffle mask for the second input, shuffle it, and
5582 // OR it with the first shuffled input.
5583 pshufbMask.clear();
5584 for (unsigned i = 0; i != 8; ++i) {
5585 int EltIdx = MaskVals[i] * 2;
5586 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005587 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5588 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005589 continue;
5590 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005591 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5592 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005593 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005594 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005595 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005596 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005597 MVT::v16i8, &pshufbMask[0], 16));
5598 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005599 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005600 }
5601
5602 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5603 // and update MaskVals with new element order.
5604 BitVector InOrder(8);
5605 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005606 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005607 for (int i = 0; i != 4; ++i) {
5608 int idx = MaskVals[i];
5609 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005610 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005611 InOrder.set(i);
5612 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005613 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005614 InOrder.set(i);
5615 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005616 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005617 }
5618 }
5619 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005620 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005621 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005622 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005623
Craig Topperc0d82852011-11-22 00:44:41 +00005624 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3orAVX())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005625 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5626 NewV.getOperand(0),
5627 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5628 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005629 }
Eric Christopherfd179292009-08-27 18:07:15 +00005630
Nate Begemanb9a47b82009-02-23 08:49:38 +00005631 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5632 // and update MaskVals with the new element order.
5633 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005634 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005635 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005636 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005637 for (unsigned i = 4; i != 8; ++i) {
5638 int idx = MaskVals[i];
5639 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005640 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005641 InOrder.set(i);
5642 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005643 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005644 InOrder.set(i);
5645 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005646 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005647 }
5648 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005649 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005650 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005651
Craig Topperc0d82852011-11-22 00:44:41 +00005652 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3orAVX())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005653 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5654 NewV.getOperand(0),
5655 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5656 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005657 }
Eric Christopherfd179292009-08-27 18:07:15 +00005658
Nate Begemanb9a47b82009-02-23 08:49:38 +00005659 // In case BestHi & BestLo were both -1, which means each quadword has a word
5660 // from each of the four input quadwords, calculate the InOrder bitvector now
5661 // before falling through to the insert/extract cleanup.
5662 if (BestLoQuad == -1 && BestHiQuad == -1) {
5663 NewV = V1;
5664 for (int i = 0; i != 8; ++i)
5665 if (MaskVals[i] < 0 || MaskVals[i] == i)
5666 InOrder.set(i);
5667 }
Eric Christopherfd179292009-08-27 18:07:15 +00005668
Nate Begemanb9a47b82009-02-23 08:49:38 +00005669 // The other elements are put in the right place using pextrw and pinsrw.
5670 for (unsigned i = 0; i != 8; ++i) {
5671 if (InOrder[i])
5672 continue;
5673 int EltIdx = MaskVals[i];
5674 if (EltIdx < 0)
5675 continue;
5676 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005677 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005678 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005679 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005680 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005681 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005682 DAG.getIntPtrConstant(i));
5683 }
5684 return NewV;
5685}
5686
5687// v16i8 shuffles - Prefer shuffles in the following order:
5688// 1. [ssse3] 1 x pshufb
5689// 2. [ssse3] 2 x pshufb + 1 x por
5690// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5691static
Nate Begeman9008ca62009-04-27 18:41:29 +00005692SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005693 SelectionDAG &DAG,
5694 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005695 SDValue V1 = SVOp->getOperand(0);
5696 SDValue V2 = SVOp->getOperand(1);
5697 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005698 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005699 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005700
Nate Begemanb9a47b82009-02-23 08:49:38 +00005701 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005702 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005703 // present, fall back to case 3.
5704 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5705 bool V1Only = true;
5706 bool V2Only = true;
5707 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005708 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005709 if (EltIdx < 0)
5710 continue;
5711 if (EltIdx < 16)
5712 V2Only = false;
5713 else
5714 V1Only = false;
5715 }
Eric Christopherfd179292009-08-27 18:07:15 +00005716
Nate Begemanb9a47b82009-02-23 08:49:38 +00005717 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperc0d82852011-11-22 00:44:41 +00005718 if (TLI.getSubtarget()->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005719 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005720
Nate Begemanb9a47b82009-02-23 08:49:38 +00005721 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005722 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005723 //
5724 // Otherwise, we have elements from both input vectors, and must zero out
5725 // elements that come from V2 in the first mask, and V1 in the second mask
5726 // so that we can OR them together.
5727 bool TwoInputs = !(V1Only || V2Only);
5728 for (unsigned i = 0; i != 16; ++i) {
5729 int EltIdx = MaskVals[i];
5730 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005731 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005732 continue;
5733 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005734 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005735 }
5736 // If all the elements are from V2, assign it to V1 and return after
5737 // building the first pshufb.
5738 if (V2Only)
5739 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005740 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005741 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005742 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005743 if (!TwoInputs)
5744 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005745
Nate Begemanb9a47b82009-02-23 08:49:38 +00005746 // Calculate the shuffle mask for the second input, shuffle it, and
5747 // OR it with the first shuffled input.
5748 pshufbMask.clear();
5749 for (unsigned i = 0; i != 16; ++i) {
5750 int EltIdx = MaskVals[i];
5751 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005752 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005753 continue;
5754 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005755 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005756 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005757 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005758 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005759 MVT::v16i8, &pshufbMask[0], 16));
5760 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005761 }
Eric Christopherfd179292009-08-27 18:07:15 +00005762
Nate Begemanb9a47b82009-02-23 08:49:38 +00005763 // No SSSE3 - Calculate in place words and then fix all out of place words
5764 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5765 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005766 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5767 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 SDValue NewV = V2Only ? V2 : V1;
5769 for (int i = 0; i != 8; ++i) {
5770 int Elt0 = MaskVals[i*2];
5771 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005772
Nate Begemanb9a47b82009-02-23 08:49:38 +00005773 // This word of the result is all undef, skip it.
5774 if (Elt0 < 0 && Elt1 < 0)
5775 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005776
Nate Begemanb9a47b82009-02-23 08:49:38 +00005777 // This word of the result is already in the correct place, skip it.
5778 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5779 continue;
5780 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5781 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005782
Nate Begemanb9a47b82009-02-23 08:49:38 +00005783 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5784 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5785 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005786
5787 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5788 // using a single extract together, load it and store it.
5789 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005790 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005791 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005792 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005793 DAG.getIntPtrConstant(i));
5794 continue;
5795 }
5796
Nate Begemanb9a47b82009-02-23 08:49:38 +00005797 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005798 // source byte is not also odd, shift the extracted word left 8 bits
5799 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005800 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005801 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005802 DAG.getIntPtrConstant(Elt1 / 2));
5803 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005804 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005805 DAG.getConstant(8,
5806 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005807 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005808 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5809 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005810 }
5811 // If Elt0 is defined, extract it from the appropriate source. If the
5812 // source byte is not also even, shift the extracted word right 8 bits. If
5813 // Elt1 was also defined, OR the extracted values together before
5814 // inserting them in the result.
5815 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005816 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005817 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5818 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005819 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005820 DAG.getConstant(8,
5821 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005822 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005823 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5824 DAG.getConstant(0x00FF, MVT::i16));
5825 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005826 : InsElt0;
5827 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005828 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005829 DAG.getIntPtrConstant(i));
5830 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005831 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005832}
5833
Evan Cheng7a831ce2007-12-15 03:00:47 +00005834/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005835/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005836/// done when every pair / quad of shuffle mask elements point to elements in
5837/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005838/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005839static
Nate Begeman9008ca62009-04-27 18:41:29 +00005840SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005841 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005842 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005843 SDValue V1 = SVOp->getOperand(0);
5844 SDValue V2 = SVOp->getOperand(1);
5845 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005846 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005847 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005848 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005849 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005850 case MVT::v4f32: NewVT = MVT::v2f64; break;
5851 case MVT::v4i32: NewVT = MVT::v2i64; break;
5852 case MVT::v8i16: NewVT = MVT::v4i32; break;
5853 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005854 }
5855
Nate Begeman9008ca62009-04-27 18:41:29 +00005856 int Scale = NumElems / NewWidth;
5857 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005858 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005859 int StartIdx = -1;
5860 for (int j = 0; j < Scale; ++j) {
5861 int EltIdx = SVOp->getMaskElt(i+j);
5862 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005863 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005864 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005865 StartIdx = EltIdx - (EltIdx % Scale);
5866 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005867 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005868 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005869 if (StartIdx == -1)
5870 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005871 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005872 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005873 }
5874
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005875 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5876 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005877 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005878}
5879
Evan Chengd880b972008-05-09 21:53:03 +00005880/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005881///
Owen Andersone50ed302009-08-10 22:56:29 +00005882static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005883 SDValue SrcOp, SelectionDAG &DAG,
5884 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005885 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005886 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005887 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005888 LD = dyn_cast<LoadSDNode>(SrcOp);
5889 if (!LD) {
5890 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5891 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005892 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005893 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005894 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005895 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005896 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005897 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005898 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005899 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005900 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5901 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5902 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005903 SrcOp.getOperand(0)
5904 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005905 }
5906 }
5907 }
5908
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005909 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005910 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005911 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005912 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005913}
5914
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005915/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5916/// shuffle node referes to only one lane in the sources.
5917static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5918 EVT VT = SVOp->getValueType(0);
5919 int NumElems = VT.getVectorNumElements();
5920 int HalfSize = NumElems/2;
5921 SmallVector<int, 16> M;
5922 SVOp->getMask(M);
5923 bool MatchA = false, MatchB = false;
5924
5925 for (int l = 0; l < NumElems*2; l += HalfSize) {
5926 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5927 MatchA = true;
5928 break;
5929 }
5930 }
5931
5932 for (int l = 0; l < NumElems*2; l += HalfSize) {
5933 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5934 MatchB = true;
5935 break;
5936 }
5937 }
5938
5939 return MatchA && MatchB;
5940}
5941
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005942/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5943/// which could not be matched by any known target speficic shuffle
5944static SDValue
5945LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005946 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5947 // If each half of a vector shuffle node referes to only one lane in the
5948 // source vectors, extract each used 128-bit lane and shuffle them using
5949 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5950 // the work to the legalizer.
5951 DebugLoc dl = SVOp->getDebugLoc();
5952 EVT VT = SVOp->getValueType(0);
5953 int NumElems = VT.getVectorNumElements();
5954 int HalfSize = NumElems/2;
5955
5956 // Extract the reference for each half
5957 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5958 int FstVecOpNum = 0, SndVecOpNum = 0;
5959 for (int i = 0; i < HalfSize; ++i) {
5960 int Elt = SVOp->getMaskElt(i);
5961 if (SVOp->getMaskElt(i) < 0)
5962 continue;
5963 FstVecOpNum = Elt/NumElems;
5964 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5965 break;
5966 }
5967 for (int i = HalfSize; i < NumElems; ++i) {
5968 int Elt = SVOp->getMaskElt(i);
5969 if (SVOp->getMaskElt(i) < 0)
5970 continue;
5971 SndVecOpNum = Elt/NumElems;
5972 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5973 break;
5974 }
5975
5976 // Extract the subvectors
5977 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5978 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5979 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5980 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5981
5982 // Generate 128-bit shuffles
5983 SmallVector<int, 16> MaskV1, MaskV2;
5984 for (int i = 0; i < HalfSize; ++i) {
5985 int Elt = SVOp->getMaskElt(i);
5986 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5987 }
5988 for (int i = HalfSize; i < NumElems; ++i) {
5989 int Elt = SVOp->getMaskElt(i);
5990 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5991 }
5992
5993 EVT NVT = V1.getValueType();
5994 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5995 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5996
5997 // Concatenate the result back
5998 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
5999 DAG.getConstant(0, MVT::i32), DAG, dl);
6000 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
6001 DAG, dl);
6002 }
6003
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006004 return SDValue();
6005}
6006
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006007/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6008/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006009static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006010LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006011 SDValue V1 = SVOp->getOperand(0);
6012 SDValue V2 = SVOp->getOperand(1);
6013 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006014 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006015
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006016 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6017
Evan Chengace3c172008-07-22 21:13:36 +00006018 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006019 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006020 SmallVector<int, 8> Mask1(4U, -1);
6021 SmallVector<int, 8> PermMask;
6022 SVOp->getMask(PermMask);
6023
Evan Chengace3c172008-07-22 21:13:36 +00006024 unsigned NumHi = 0;
6025 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006026 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006027 int Idx = PermMask[i];
6028 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006029 Locs[i] = std::make_pair(-1, -1);
6030 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006031 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6032 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006033 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006034 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006035 NumLo++;
6036 } else {
6037 Locs[i] = std::make_pair(1, NumHi);
6038 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006039 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006040 NumHi++;
6041 }
6042 }
6043 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006044
Evan Chengace3c172008-07-22 21:13:36 +00006045 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006046 // If no more than two elements come from either vector. This can be
6047 // implemented with two shuffles. First shuffle gather the elements.
6048 // The second shuffle, which takes the first shuffle as both of its
6049 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006050 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006051
Nate Begeman9008ca62009-04-27 18:41:29 +00006052 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006053
Evan Chengace3c172008-07-22 21:13:36 +00006054 for (unsigned i = 0; i != 4; ++i) {
6055 if (Locs[i].first == -1)
6056 continue;
6057 else {
6058 unsigned Idx = (i < 2) ? 0 : 4;
6059 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006060 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006061 }
6062 }
6063
Nate Begeman9008ca62009-04-27 18:41:29 +00006064 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006065 } else if (NumLo == 3 || NumHi == 3) {
6066 // Otherwise, we must have three elements from one vector, call it X, and
6067 // one element from the other, call it Y. First, use a shufps to build an
6068 // intermediate vector with the one element from Y and the element from X
6069 // that will be in the same half in the final destination (the indexes don't
6070 // matter). Then, use a shufps to build the final vector, taking the half
6071 // containing the element from Y from the intermediate, and the other half
6072 // from X.
6073 if (NumHi == 3) {
6074 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006075 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006076 std::swap(V1, V2);
6077 }
6078
6079 // Find the element from V2.
6080 unsigned HiIndex;
6081 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006082 int Val = PermMask[HiIndex];
6083 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006084 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006085 if (Val >= 4)
6086 break;
6087 }
6088
Nate Begeman9008ca62009-04-27 18:41:29 +00006089 Mask1[0] = PermMask[HiIndex];
6090 Mask1[1] = -1;
6091 Mask1[2] = PermMask[HiIndex^1];
6092 Mask1[3] = -1;
6093 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006094
6095 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006096 Mask1[0] = PermMask[0];
6097 Mask1[1] = PermMask[1];
6098 Mask1[2] = HiIndex & 1 ? 6 : 4;
6099 Mask1[3] = HiIndex & 1 ? 4 : 6;
6100 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006101 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006102 Mask1[0] = HiIndex & 1 ? 2 : 0;
6103 Mask1[1] = HiIndex & 1 ? 0 : 2;
6104 Mask1[2] = PermMask[2];
6105 Mask1[3] = PermMask[3];
6106 if (Mask1[2] >= 0)
6107 Mask1[2] += 4;
6108 if (Mask1[3] >= 0)
6109 Mask1[3] += 4;
6110 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006111 }
Evan Chengace3c172008-07-22 21:13:36 +00006112 }
6113
6114 // Break it into (shuffle shuffle_hi, shuffle_lo).
6115 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006116 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006117 SmallVector<int,8> LoMask(4U, -1);
6118 SmallVector<int,8> HiMask(4U, -1);
6119
6120 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006121 unsigned MaskIdx = 0;
6122 unsigned LoIdx = 0;
6123 unsigned HiIdx = 2;
6124 for (unsigned i = 0; i != 4; ++i) {
6125 if (i == 2) {
6126 MaskPtr = &HiMask;
6127 MaskIdx = 1;
6128 LoIdx = 0;
6129 HiIdx = 2;
6130 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006131 int Idx = PermMask[i];
6132 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006133 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006134 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006135 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006136 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006137 LoIdx++;
6138 } else {
6139 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006140 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006141 HiIdx++;
6142 }
6143 }
6144
Nate Begeman9008ca62009-04-27 18:41:29 +00006145 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6146 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6147 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006148 for (unsigned i = 0; i != 4; ++i) {
6149 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006150 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006151 } else {
6152 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006153 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006154 }
6155 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006156 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006157}
6158
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006159static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006160 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006161 V = V.getOperand(0);
6162 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6163 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006164 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6165 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6166 // BUILD_VECTOR (load), undef
6167 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006168 if (MayFoldLoad(V))
6169 return true;
6170 return false;
6171}
6172
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006173// FIXME: the version above should always be used. Since there's
6174// a bug where several vector shuffles can't be folded because the
6175// DAG is not updated during lowering and a node claims to have two
6176// uses while it only has one, use this version, and let isel match
6177// another instruction if the load really happens to have more than
6178// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006179// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006180static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006181 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006182 V = V.getOperand(0);
6183 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6184 V = V.getOperand(0);
6185 if (ISD::isNormalLoad(V.getNode()))
6186 return true;
6187 return false;
6188}
6189
6190/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6191/// a vector extract, and if both can be later optimized into a single load.
6192/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6193/// here because otherwise a target specific shuffle node is going to be
6194/// emitted for this shuffle, and the optimization not done.
6195/// FIXME: This is probably not the best approach, but fix the problem
6196/// until the right path is decided.
6197static
6198bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6199 const TargetLowering &TLI) {
6200 EVT VT = V.getValueType();
6201 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6202
6203 // Be sure that the vector shuffle is present in a pattern like this:
6204 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6205 if (!V.hasOneUse())
6206 return false;
6207
6208 SDNode *N = *V.getNode()->use_begin();
6209 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6210 return false;
6211
6212 SDValue EltNo = N->getOperand(1);
6213 if (!isa<ConstantSDNode>(EltNo))
6214 return false;
6215
6216 // If the bit convert changed the number of elements, it is unsafe
6217 // to examine the mask.
6218 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006219 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006220 EVT SrcVT = V.getOperand(0).getValueType();
6221 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6222 return false;
6223 V = V.getOperand(0);
6224 HasShuffleIntoBitcast = true;
6225 }
6226
6227 // Select the input vector, guarding against out of range extract vector.
6228 unsigned NumElems = VT.getVectorNumElements();
6229 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6230 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6231 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6232
6233 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006234 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006235 V = V.getOperand(0);
6236
6237 if (ISD::isNormalLoad(V.getNode())) {
6238 // Is the original load suitable?
6239 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6240
6241 // FIXME: avoid the multi-use bug that is preventing lots of
6242 // of foldings to be detected, this is still wrong of course, but
6243 // give the temporary desired behavior, and if it happens that
6244 // the load has real more uses, during isel it will not fold, and
6245 // will generate poor code.
6246 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6247 return false;
6248
6249 if (!HasShuffleIntoBitcast)
6250 return true;
6251
6252 // If there's a bitcast before the shuffle, check if the load type and
6253 // alignment is valid.
6254 unsigned Align = LN0->getAlignment();
6255 unsigned NewAlign =
6256 TLI.getTargetData()->getABITypeAlignment(
6257 VT.getTypeForEVT(*DAG.getContext()));
6258
6259 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6260 return false;
6261 }
6262
6263 return true;
6264}
6265
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006266static
Evan Cheng835580f2010-10-07 20:50:20 +00006267SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6268 EVT VT = Op.getValueType();
6269
6270 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006271 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6272 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006273 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6274 V1, DAG));
6275}
6276
6277static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006278SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006279 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006280 SDValue V1 = Op.getOperand(0);
6281 SDValue V2 = Op.getOperand(1);
6282 EVT VT = Op.getValueType();
6283
6284 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6285
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006286 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006287 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6288
Evan Cheng0899f5c2011-08-31 02:05:24 +00006289 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6290 return DAG.getNode(ISD::BITCAST, dl, VT,
6291 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6292 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6293 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006294}
6295
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006296static
6297SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6298 SDValue V1 = Op.getOperand(0);
6299 SDValue V2 = Op.getOperand(1);
6300 EVT VT = Op.getValueType();
6301
6302 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6303 "unsupported shuffle type");
6304
6305 if (V2.getOpcode() == ISD::UNDEF)
6306 V2 = V1;
6307
6308 // v4i32 or v4f32
6309 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6310}
6311
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006312static inline unsigned getSHUFPOpcode(EVT VT) {
6313 switch(VT.getSimpleVT().SimpleTy) {
6314 case MVT::v8i32: // Use fp unit for int unpack.
6315 case MVT::v8f32:
6316 case MVT::v4i32: // Use fp unit for int unpack.
6317 case MVT::v4f32: return X86ISD::SHUFPS;
6318 case MVT::v4i64: // Use fp unit for int unpack.
6319 case MVT::v4f64:
6320 case MVT::v2i64: // Use fp unit for int unpack.
6321 case MVT::v2f64: return X86ISD::SHUFPD;
6322 default:
6323 llvm_unreachable("Unknown type for shufp*");
6324 }
6325 return 0;
6326}
6327
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006328static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006329SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006330 SDValue V1 = Op.getOperand(0);
6331 SDValue V2 = Op.getOperand(1);
6332 EVT VT = Op.getValueType();
6333 unsigned NumElems = VT.getVectorNumElements();
6334
6335 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6336 // operand of these instructions is only memory, so check if there's a
6337 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6338 // same masks.
6339 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006340
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006341 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006342 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006343 CanFoldLoad = true;
6344
6345 // When V1 is a load, it can be folded later into a store in isel, example:
6346 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6347 // turns into:
6348 // (MOVLPSmr addr:$src1, VR128:$src2)
6349 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006350 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006351 CanFoldLoad = true;
6352
Dan Gohman65fd6562011-11-03 21:49:52 +00006353 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006354 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006355 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006356 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6357
6358 if (NumElems == 4)
Dan Gohman65fd6562011-11-03 21:49:52 +00006359 // If we don't care about the second element, procede to use movss.
6360 if (SVOp->getMaskElt(1) != -1)
6361 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006362 }
6363
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006364 // movl and movlp will both match v2i64, but v2i64 is never matched by
6365 // movl earlier because we make it strict to avoid messing with the movlp load
6366 // folding logic (see the code above getMOVLP call). Match it here then,
6367 // this is horrible, but will stay like this until we move all shuffle
6368 // matching to x86 specific nodes. Note that for the 1st condition all
6369 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006370 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006371 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6372 // as to remove this logic from here, as much as possible
6373 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006374 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006375 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006376 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006377
6378 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6379
6380 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006381 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006382 X86::getShuffleSHUFImmediate(SVOp), DAG);
6383}
6384
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006385static
6386SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006387 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006388 const X86Subtarget *Subtarget) {
6389 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6390 EVT VT = Op.getValueType();
6391 DebugLoc dl = Op.getDebugLoc();
6392 SDValue V1 = Op.getOperand(0);
6393 SDValue V2 = Op.getOperand(1);
6394
6395 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006396 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006397
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006398 // Handle splat operations
6399 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006400 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006401 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006402 // Special case, this is the only place now where it's allowed to return
6403 // a vector_shuffle operation without using a target specific node, because
6404 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6405 // this be moved to DAGCombine instead?
6406 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006407 return Op;
6408
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006409 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00006410 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00006411 if (Subtarget->hasAVX() && LD.getNode())
6412 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006413
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006414 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006415 if ((Size == 128 && NumElem <= 4) ||
6416 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006417 return SDValue();
6418
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006419 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006420 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006421 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006422
6423 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6424 // do it!
6425 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6426 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6427 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006428 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006429 } else if ((VT == MVT::v4i32 ||
6430 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006431 // FIXME: Figure out a cleaner way to do this.
6432 // Try to make use of movq to zero out the top part.
6433 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6434 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6435 if (NewOp.getNode()) {
6436 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6437 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6438 DAG, Subtarget, dl);
6439 }
6440 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6441 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6442 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6443 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6444 DAG, Subtarget, dl);
6445 }
6446 }
6447 return SDValue();
6448}
6449
Dan Gohman475871a2008-07-27 21:46:04 +00006450SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006451X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006452 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006453 SDValue V1 = Op.getOperand(0);
6454 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006455 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006456 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006457 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006458 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006459 bool V1IsSplat = false;
6460 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006461 bool HasXMMInt = Subtarget->hasXMMInt();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006462 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006463 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006464 MachineFunction &MF = DAG.getMachineFunction();
6465 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006466
Craig Topper3426a3e2011-11-14 06:46:21 +00006467 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006468
Craig Topper38034c52011-11-26 22:55:48 +00006469 assert(V1.getOpcode() != ISD::UNDEF && "Op 1 of shuffle should not be undef");
6470
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006471 // Vector shuffle lowering takes 3 steps:
6472 //
6473 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6474 // narrowing and commutation of operands should be handled.
6475 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6476 // shuffle nodes.
6477 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6478 // so the shuffle can be broken into other shuffles and the legalizer can
6479 // try the lowering again.
6480 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006481 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006482 // be matched during isel, all of them must be converted to a target specific
6483 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006484
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006485 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6486 // narrowing and commutation of operands should be handled. The actual code
6487 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006488 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006489 if (NewOp.getNode())
6490 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006491
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006492 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6493 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper94438ba2011-12-16 08:06:31 +00006494 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006495 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006496 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006497 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006498
Craig Topperc0d82852011-11-22 00:44:41 +00006499 if (X86::isMOVDDUPMask(SVOp) && Subtarget->hasSSE3orAVX() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006500 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006501 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006502
Dale Johannesen0488fb62010-09-30 23:57:10 +00006503 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006504 return getMOVHighToLow(Op, dl, DAG);
6505
6506 // Use to match splats
Craig Topperc0d82852011-11-22 00:44:41 +00006507 if (HasXMMInt && X86::isUNPCKHMask(SVOp, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006508 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006509 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006510
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006511 if (X86::isPSHUFDMask(SVOp)) {
6512 // The actual implementation will match the mask in the if above and then
6513 // during isel it can match several different instructions, not only pshufd
6514 // as its name says, sad but true, emulate the behavior for now...
6515 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6516 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6517
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006518 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6519
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006520 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006521 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6522
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006523 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6524 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006525 }
Eric Christopherfd179292009-08-27 18:07:15 +00006526
Evan Chengf26ffe92008-05-29 08:22:04 +00006527 // Check if this can be converted into a logical shift.
6528 bool isLeft = false;
6529 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006530 SDValue ShVal;
Craig Topperc0d82852011-11-22 00:44:41 +00006531 bool isShift = HasXMMInt && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006532 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006533 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006534 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006535 EVT EltVT = VT.getVectorElementType();
6536 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006537 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006538 }
Eric Christopherfd179292009-08-27 18:07:15 +00006539
Nate Begeman9008ca62009-04-27 18:41:29 +00006540 if (X86::isMOVLMask(SVOp)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006541 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006542 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006543 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006544 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006545 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6546
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006547 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006548 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6549 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006550 }
Eric Christopherfd179292009-08-27 18:07:15 +00006551
Nate Begeman9008ca62009-04-27 18:41:29 +00006552 // FIXME: fold these into legal mask.
Craig Topperc0d82852011-11-22 00:44:41 +00006553 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp, HasAVX2))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006554 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006555
Dale Johannesen0488fb62010-09-30 23:57:10 +00006556 if (X86::isMOVHLPSMask(SVOp))
6557 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006558
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006559 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006560 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006561
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006562 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006563 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006564
Dale Johannesen0488fb62010-09-30 23:57:10 +00006565 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006566 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006567
Nate Begeman9008ca62009-04-27 18:41:29 +00006568 if (ShouldXformToMOVHLPS(SVOp) ||
6569 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6570 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006571
Evan Chengf26ffe92008-05-29 08:22:04 +00006572 if (isShift) {
6573 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006574 EVT EltVT = VT.getVectorElementType();
6575 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006576 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006577 }
Eric Christopherfd179292009-08-27 18:07:15 +00006578
Evan Cheng9eca5e82006-10-25 21:49:50 +00006579 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006580 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6581 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006582 V1IsSplat = isSplatVector(V1.getNode());
6583 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006584
Chris Lattner8a594482007-11-25 00:24:49 +00006585 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper38034c52011-11-26 22:55:48 +00006586 if (V1IsSplat && !V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006587 Op = CommuteVectorShuffle(SVOp, DAG);
6588 SVOp = cast<ShuffleVectorSDNode>(Op);
6589 V1 = SVOp->getOperand(0);
6590 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006591 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006592 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006593 }
6594
Craig Topperbeabc6c2011-12-05 06:56:46 +00006595 SmallVector<int, 32> M;
6596 SVOp->getMask(M);
6597
6598 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006599 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006600 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006601 return V1;
6602 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6603 // the instruction selector will not match, so get a canonical MOVL with
6604 // swapped operands to undo the commute.
6605 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006606 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006607
Craig Topperbeabc6c2011-12-05 06:56:46 +00006608 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006609 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006610
Craig Topperbeabc6c2011-12-05 06:56:46 +00006611 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006612 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006613
Evan Cheng9bbbb982006-10-25 20:48:19 +00006614 if (V2IsSplat) {
6615 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006616 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006617 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006618 SDValue NewMask = NormalizeMask(SVOp, DAG);
6619 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6620 if (NSVOp != SVOp) {
Craig Topperc0d82852011-11-22 00:44:41 +00006621 if (X86::isUNPCKLMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006622 return NewMask;
Craig Topperc0d82852011-11-22 00:44:41 +00006623 } else if (X86::isUNPCKHMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006624 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006625 }
6626 }
6627 }
6628
Evan Cheng9eca5e82006-10-25 21:49:50 +00006629 if (Commuted) {
6630 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006631 // FIXME: this seems wrong.
6632 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6633 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006634
Craig Topperc0d82852011-11-22 00:44:41 +00006635 if (X86::isUNPCKLMask(NewSVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006636 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006637
Craig Topperc0d82852011-11-22 00:44:41 +00006638 if (X86::isUNPCKHMask(NewSVOp, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006639 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006640 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006641
Nate Begeman9008ca62009-04-27 18:41:29 +00006642 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1ff73d72011-12-06 04:59:07 +00006643 if (!V2IsUndef && (isSHUFPMask(M, VT, /* Commuted */ true) ||
6644 isVSHUFPYMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006645 return CommuteVectorShuffle(SVOp, DAG);
6646
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006647 // The checks below are all present in isShuffleMaskLegal, but they are
6648 // inlined here right now to enable us to directly emit target specific
6649 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006650
Craig Topperc0d82852011-11-22 00:44:41 +00006651 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006652 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006653 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006654 DAG);
6655
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006656 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6657 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006658 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006659 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006660 }
6661
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006662 if (isPSHUFHWMask(M, VT))
6663 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6664 X86::getShufflePSHUFHWImmediate(SVOp),
6665 DAG);
6666
6667 if (isPSHUFLWMask(M, VT))
6668 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6669 X86::getShufflePSHUFLWImmediate(SVOp),
6670 DAG);
6671
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006672 if (isSHUFPMask(M, VT))
6673 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6674 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006675
Craig Topper94438ba2011-12-16 08:06:31 +00006676 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006677 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006678 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006679 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006680
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006681 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006682 // Generate target specific nodes for 128 or 256-bit shuffles only
6683 // supported in the AVX instruction set.
6684 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006685
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006686 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006687 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006688 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6689
Craig Topper70b883b2011-11-28 10:14:51 +00006690 // Handle VPERMILPS/D* permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006691 if (isVPERMILPMask(M, VT, HasAVX))
Craig Topper316cd2a2011-11-30 06:25:25 +00006692 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006693 getShuffleVPERMILPImmediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006694
Craig Topper70b883b2011-11-28 10:14:51 +00006695 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006696 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006697 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006698 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006699
Craig Topper70b883b2011-11-28 10:14:51 +00006700 // Handle VSHUFPS/DY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006701 if (isVSHUFPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006702 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
Craig Topper9d7025b2011-11-27 21:41:12 +00006703 getShuffleVSHUFPYImmediate(SVOp), DAG);
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006704
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006705 //===--------------------------------------------------------------------===//
6706 // Since no target specific shuffle was selected for this generic one,
6707 // lower it into other known shuffles. FIXME: this isn't true yet, but
6708 // this is the plan.
6709 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006710
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006711 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6712 if (VT == MVT::v8i16) {
6713 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6714 if (NewOp.getNode())
6715 return NewOp;
6716 }
6717
6718 if (VT == MVT::v16i8) {
6719 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6720 if (NewOp.getNode())
6721 return NewOp;
6722 }
6723
6724 // Handle all 128-bit wide vectors with 4 elements, and match them with
6725 // several different shuffle types.
6726 if (NumElems == 4 && VT.getSizeInBits() == 128)
6727 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6728
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006729 // Handle general 256-bit shuffles
6730 if (VT.is256BitVector())
6731 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6732
Dan Gohman475871a2008-07-27 21:46:04 +00006733 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006734}
6735
Dan Gohman475871a2008-07-27 21:46:04 +00006736SDValue
6737X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006738 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006739 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006740 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006741
6742 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6743 return SDValue();
6744
Duncan Sands83ec4b62008-06-06 12:08:01 +00006745 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006746 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006747 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006748 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006749 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006750 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006751 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006752 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6753 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6754 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006755 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6756 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006757 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006758 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006759 Op.getOperand(0)),
6760 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006761 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006762 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006763 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006764 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006765 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006766 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006767 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6768 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006769 // result has a single use which is a store or a bitcast to i32. And in
6770 // the case of a store, it's not worth it if the index is a constant 0,
6771 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006772 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006773 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006774 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006775 if ((User->getOpcode() != ISD::STORE ||
6776 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6777 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006778 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006779 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006780 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006781 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006782 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006783 Op.getOperand(0)),
6784 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006785 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Pete Coopera77214a2011-11-14 19:38:42 +00006786 } else if (VT == MVT::i32 || VT == MVT::i64) {
6787 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006788 if (isa<ConstantSDNode>(Op.getOperand(1)))
6789 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006790 }
Dan Gohman475871a2008-07-27 21:46:04 +00006791 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006792}
6793
6794
Dan Gohman475871a2008-07-27 21:46:04 +00006795SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006796X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6797 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006798 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006799 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006800
David Greene74a579d2011-02-10 16:57:36 +00006801 SDValue Vec = Op.getOperand(0);
6802 EVT VecVT = Vec.getValueType();
6803
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006804 // If this is a 256-bit vector result, first extract the 128-bit vector and
6805 // then extract the element from the 128-bit vector.
6806 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006807 DebugLoc dl = Op.getNode()->getDebugLoc();
6808 unsigned NumElems = VecVT.getVectorNumElements();
6809 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006810 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6811
6812 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006813 bool Upper = IdxVal >= NumElems/2;
6814 Vec = Extract128BitVector(Vec,
6815 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006816
David Greene74a579d2011-02-10 16:57:36 +00006817 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006818 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006819 }
6820
6821 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6822
Craig Topperc0d82852011-11-22 00:44:41 +00006823 if (Subtarget->hasSSE41orAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006824 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006825 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006826 return Res;
6827 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006828
Owen Andersone50ed302009-08-10 22:56:29 +00006829 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006830 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006831 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006832 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006833 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006834 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006835 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006836 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6837 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006838 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006839 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006840 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006841 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006842 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006843 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006844 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006845 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006846 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006847 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006848 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006849 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006850 if (Idx == 0)
6851 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006852
Evan Cheng0db9fe62006-04-25 20:13:52 +00006853 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006854 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006855 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006856 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006857 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006858 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006859 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006860 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006861 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6862 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6863 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006864 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006865 if (Idx == 0)
6866 return Op;
6867
6868 // UNPCKHPD the element to the lowest double word, then movsd.
6869 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6870 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006871 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006872 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006873 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006874 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006875 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006876 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006877 }
6878
Dan Gohman475871a2008-07-27 21:46:04 +00006879 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006880}
6881
Dan Gohman475871a2008-07-27 21:46:04 +00006882SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006883X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6884 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006885 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006886 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006887 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006888
Dan Gohman475871a2008-07-27 21:46:04 +00006889 SDValue N0 = Op.getOperand(0);
6890 SDValue N1 = Op.getOperand(1);
6891 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006892
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006893 if (VT.getSizeInBits() == 256)
6894 return SDValue();
6895
Dan Gohman8a55ce42009-09-23 21:02:20 +00006896 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006897 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006898 unsigned Opc;
6899 if (VT == MVT::v8i16)
6900 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006901 else if (VT == MVT::v16i8)
6902 Opc = X86ISD::PINSRB;
6903 else
6904 Opc = X86ISD::PINSRB;
6905
Nate Begeman14d12ca2008-02-11 04:19:36 +00006906 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6907 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006908 if (N1.getValueType() != MVT::i32)
6909 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6910 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006911 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006912 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006913 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006914 // Bits [7:6] of the constant are the source select. This will always be
6915 // zero here. The DAG Combiner may combine an extract_elt index into these
6916 // bits. For example (insert (extract, 3), 2) could be matched by putting
6917 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006918 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006919 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006920 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006921 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006922 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006923 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006924 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006925 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Pete Coopera77214a2011-11-14 19:38:42 +00006926 } else if ((EltVT == MVT::i32 || EltVT == MVT::i64) &&
6927 isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006928 // PINSR* works with constant index.
6929 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006930 }
Dan Gohman475871a2008-07-27 21:46:04 +00006931 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006932}
6933
Dan Gohman475871a2008-07-27 21:46:04 +00006934SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006935X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006936 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006937 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006938
David Greene6b381262011-02-09 15:32:06 +00006939 DebugLoc dl = Op.getDebugLoc();
6940 SDValue N0 = Op.getOperand(0);
6941 SDValue N1 = Op.getOperand(1);
6942 SDValue N2 = Op.getOperand(2);
6943
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006944 // If this is a 256-bit vector result, first extract the 128-bit vector,
6945 // insert the element into the extracted half and then place it back.
6946 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00006947 if (!isa<ConstantSDNode>(N2))
6948 return SDValue();
6949
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006950 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00006951 unsigned NumElems = VT.getVectorNumElements();
6952 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006953 bool Upper = IdxVal >= NumElems/2;
6954 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
6955 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00006956
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006957 // Insert the element into the desired half.
6958 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
6959 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00006960
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006961 // Insert the changed part back to the 256-bit vector
6962 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00006963 }
6964
Craig Topperc0d82852011-11-22 00:44:41 +00006965 if (Subtarget->hasSSE41orAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00006966 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6967
Dan Gohman8a55ce42009-09-23 21:02:20 +00006968 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006969 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006970
Dan Gohman8a55ce42009-09-23 21:02:20 +00006971 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006972 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6973 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006974 if (N1.getValueType() != MVT::i32)
6975 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6976 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006977 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006978 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006979 }
Dan Gohman475871a2008-07-27 21:46:04 +00006980 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006981}
6982
Dan Gohman475871a2008-07-27 21:46:04 +00006983SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006984X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00006985 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006986 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006987 EVT OpVT = Op.getValueType();
6988
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00006989 // If this is a 256-bit vector result, first insert into a 128-bit
6990 // vector and then insert into the 256-bit vector.
6991 if (OpVT.getSizeInBits() > 128) {
6992 // Insert into a 128-bit vector.
6993 EVT VT128 = EVT::getVectorVT(*Context,
6994 OpVT.getVectorElementType(),
6995 OpVT.getVectorNumElements() / 2);
6996
6997 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6998
6999 // Insert the 128-bit vector.
7000 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7001 DAG.getConstant(0, MVT::i32),
7002 DAG, dl);
7003 }
7004
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007005 if (Op.getValueType() == MVT::v1i64 &&
7006 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007007 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007008
Owen Anderson825b72b2009-08-11 20:47:22 +00007009 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007010 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7011 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007012 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007013 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007014}
7015
David Greene91585092011-01-26 15:38:49 +00007016// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7017// a simple subregister reference or explicit instructions to grab
7018// upper bits of a vector.
7019SDValue
7020X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7021 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007022 DebugLoc dl = Op.getNode()->getDebugLoc();
7023 SDValue Vec = Op.getNode()->getOperand(0);
7024 SDValue Idx = Op.getNode()->getOperand(1);
7025
7026 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7027 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7028 return Extract128BitVector(Vec, Idx, DAG, dl);
7029 }
David Greene91585092011-01-26 15:38:49 +00007030 }
7031 return SDValue();
7032}
7033
David Greenecfe33c42011-01-26 19:13:22 +00007034// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7035// simple superregister reference or explicit instructions to insert
7036// the upper bits of a vector.
7037SDValue
7038X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7039 if (Subtarget->hasAVX()) {
7040 DebugLoc dl = Op.getNode()->getDebugLoc();
7041 SDValue Vec = Op.getNode()->getOperand(0);
7042 SDValue SubVec = Op.getNode()->getOperand(1);
7043 SDValue Idx = Op.getNode()->getOperand(2);
7044
7045 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7046 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007047 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007048 }
7049 }
7050 return SDValue();
7051}
7052
Bill Wendling056292f2008-09-16 21:48:12 +00007053// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7054// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7055// one of the above mentioned nodes. It has to be wrapped because otherwise
7056// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7057// be used to form addressing mode. These wrapped nodes will be selected
7058// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007059SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007060X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007061 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007062
Chris Lattner41621a22009-06-26 19:22:52 +00007063 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7064 // global base reg.
7065 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007066 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007067 CodeModel::Model M = getTargetMachine().getCodeModel();
7068
Chris Lattner4f066492009-07-11 20:29:19 +00007069 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007070 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007071 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007072 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007073 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007074 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007075 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007076
Evan Cheng1606e8e2009-03-13 07:51:59 +00007077 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007078 CP->getAlignment(),
7079 CP->getOffset(), OpFlag);
7080 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007081 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007082 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007083 if (OpFlag) {
7084 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007085 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007086 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007087 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007088 }
7089
7090 return Result;
7091}
7092
Dan Gohmand858e902010-04-17 15:26:15 +00007093SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007094 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007095
Chris Lattner18c59872009-06-27 04:16:01 +00007096 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7097 // global base reg.
7098 unsigned char OpFlag = 0;
7099 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007100 CodeModel::Model M = getTargetMachine().getCodeModel();
7101
Chris Lattner4f066492009-07-11 20:29:19 +00007102 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007103 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007104 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007105 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007106 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007107 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007108 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007109
Chris Lattner18c59872009-06-27 04:16:01 +00007110 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7111 OpFlag);
7112 DebugLoc DL = JT->getDebugLoc();
7113 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007114
Chris Lattner18c59872009-06-27 04:16:01 +00007115 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007116 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007117 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7118 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007119 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007120 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007121
Chris Lattner18c59872009-06-27 04:16:01 +00007122 return Result;
7123}
7124
7125SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007126X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007127 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007128
Chris Lattner18c59872009-06-27 04:16:01 +00007129 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7130 // global base reg.
7131 unsigned char OpFlag = 0;
7132 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007133 CodeModel::Model M = getTargetMachine().getCodeModel();
7134
Chris Lattner4f066492009-07-11 20:29:19 +00007135 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007136 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7137 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7138 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007139 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007140 } else if (Subtarget->isPICStyleGOT()) {
7141 OpFlag = X86II::MO_GOT;
7142 } else if (Subtarget->isPICStyleStubPIC()) {
7143 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7144 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7145 OpFlag = X86II::MO_DARWIN_NONLAZY;
7146 }
Eric Christopherfd179292009-08-27 18:07:15 +00007147
Chris Lattner18c59872009-06-27 04:16:01 +00007148 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007149
Chris Lattner18c59872009-06-27 04:16:01 +00007150 DebugLoc DL = Op.getDebugLoc();
7151 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007152
7153
Chris Lattner18c59872009-06-27 04:16:01 +00007154 // With PIC, the address is actually $g + Offset.
7155 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007156 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007157 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7158 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007159 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007160 Result);
7161 }
Eric Christopherfd179292009-08-27 18:07:15 +00007162
Eli Friedman586272d2011-08-11 01:48:05 +00007163 // For symbols that require a load from a stub to get the address, emit the
7164 // load.
7165 if (isGlobalStubReference(OpFlag))
7166 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007167 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007168
Chris Lattner18c59872009-06-27 04:16:01 +00007169 return Result;
7170}
7171
Dan Gohman475871a2008-07-27 21:46:04 +00007172SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007173X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007174 // Create the TargetBlockAddressAddress node.
7175 unsigned char OpFlags =
7176 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007177 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007178 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007179 DebugLoc dl = Op.getDebugLoc();
7180 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7181 /*isTarget=*/true, OpFlags);
7182
Dan Gohmanf705adb2009-10-30 01:28:02 +00007183 if (Subtarget->isPICStyleRIPRel() &&
7184 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007185 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7186 else
7187 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007188
Dan Gohman29cbade2009-11-20 23:18:13 +00007189 // With PIC, the address is actually $g + Offset.
7190 if (isGlobalRelativeToPICBase(OpFlags)) {
7191 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7192 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7193 Result);
7194 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007195
7196 return Result;
7197}
7198
7199SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007200X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007201 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007202 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007203 // Create the TargetGlobalAddress node, folding in the constant
7204 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007205 unsigned char OpFlags =
7206 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007207 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007208 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007209 if (OpFlags == X86II::MO_NO_FLAG &&
7210 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007211 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007212 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007213 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007214 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007215 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007216 }
Eric Christopherfd179292009-08-27 18:07:15 +00007217
Chris Lattner4f066492009-07-11 20:29:19 +00007218 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007219 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007220 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7221 else
7222 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007223
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007224 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007225 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007226 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7227 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007228 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007229 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007230
Chris Lattner36c25012009-07-10 07:34:39 +00007231 // For globals that require a load from a stub to get the address, emit the
7232 // load.
7233 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007234 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007235 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007236
Dan Gohman6520e202008-10-18 02:06:02 +00007237 // If there was a non-zero offset that we didn't fold, create an explicit
7238 // addition for it.
7239 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007240 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007241 DAG.getConstant(Offset, getPointerTy()));
7242
Evan Cheng0db9fe62006-04-25 20:13:52 +00007243 return Result;
7244}
7245
Evan Chengda43bcf2008-09-24 00:05:32 +00007246SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007247X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007248 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007249 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007250 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007251}
7252
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007253static SDValue
7254GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007255 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007256 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007257 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007258 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007259 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007260 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007261 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007262 GA->getOffset(),
7263 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007264 if (InFlag) {
7265 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007266 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007267 } else {
7268 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007269 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007270 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007271
7272 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007273 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007274
Rafael Espindola15f1b662009-04-24 12:59:40 +00007275 SDValue Flag = Chain.getValue(1);
7276 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007277}
7278
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007279// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007280static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007281LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007282 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007283 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007284 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7285 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007286 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007287 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007288 InFlag = Chain.getValue(1);
7289
Chris Lattnerb903bed2009-06-26 21:20:29 +00007290 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007291}
7292
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007293// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007294static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007295LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007296 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007297 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7298 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007299}
7300
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007301// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7302// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007303static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007304 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007305 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007306 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007307
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007308 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7309 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7310 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007311
Michael J. Spencerec38de22010-10-10 22:04:20 +00007312 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007313 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007314 MachinePointerInfo(Ptr),
7315 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007316
Chris Lattnerb903bed2009-06-26 21:20:29 +00007317 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007318 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7319 // initialexec.
7320 unsigned WrapperKind = X86ISD::Wrapper;
7321 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007322 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007323 } else if (is64Bit) {
7324 assert(model == TLSModel::InitialExec);
7325 OperandFlags = X86II::MO_GOTTPOFF;
7326 WrapperKind = X86ISD::WrapperRIP;
7327 } else {
7328 assert(model == TLSModel::InitialExec);
7329 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007330 }
Eric Christopherfd179292009-08-27 18:07:15 +00007331
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007332 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7333 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007334 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007335 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007336 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007337 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007338
Rafael Espindola9a580232009-02-27 13:37:18 +00007339 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007340 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007341 MachinePointerInfo::getGOT(), false, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007342
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007343 // The address of the thread local variable is the add of the thread
7344 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007345 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007346}
7347
Dan Gohman475871a2008-07-27 21:46:04 +00007348SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007349X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007350
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007351 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007352 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007353
Eric Christopher30ef0e52010-06-03 04:07:48 +00007354 if (Subtarget->isTargetELF()) {
7355 // TODO: implement the "local dynamic" model
7356 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007357
Eric Christopher30ef0e52010-06-03 04:07:48 +00007358 // If GV is an alias then use the aliasee for determining
7359 // thread-localness.
7360 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7361 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007362
7363 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007364 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007365
Eric Christopher30ef0e52010-06-03 04:07:48 +00007366 switch (model) {
7367 case TLSModel::GeneralDynamic:
7368 case TLSModel::LocalDynamic: // not implemented
7369 if (Subtarget->is64Bit())
7370 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7371 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007372
Eric Christopher30ef0e52010-06-03 04:07:48 +00007373 case TLSModel::InitialExec:
7374 case TLSModel::LocalExec:
7375 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7376 Subtarget->is64Bit());
7377 }
7378 } else if (Subtarget->isTargetDarwin()) {
7379 // Darwin only has one model of TLS. Lower to that.
7380 unsigned char OpFlag = 0;
7381 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7382 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007383
Eric Christopher30ef0e52010-06-03 04:07:48 +00007384 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7385 // global base reg.
7386 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7387 !Subtarget->is64Bit();
7388 if (PIC32)
7389 OpFlag = X86II::MO_TLVP_PIC_BASE;
7390 else
7391 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007392 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007393 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007394 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007395 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007396 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007397
Eric Christopher30ef0e52010-06-03 04:07:48 +00007398 // With PIC32, the address is actually $g + Offset.
7399 if (PIC32)
7400 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7401 DAG.getNode(X86ISD::GlobalBaseReg,
7402 DebugLoc(), getPointerTy()),
7403 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007404
Eric Christopher30ef0e52010-06-03 04:07:48 +00007405 // Lowering the machine isd will make sure everything is in the right
7406 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007407 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007408 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007409 SDValue Args[] = { Chain, Offset };
7410 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007411
Eric Christopher30ef0e52010-06-03 04:07:48 +00007412 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7413 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7414 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007415
Eric Christopher30ef0e52010-06-03 04:07:48 +00007416 // And our return value (tls address) is in the standard call return value
7417 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007418 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007419 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7420 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007421 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007422
Eric Christopher30ef0e52010-06-03 04:07:48 +00007423 assert(false &&
7424 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007425
Torok Edwinc23197a2009-07-14 16:55:14 +00007426 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007427 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007428}
7429
Evan Cheng0db9fe62006-04-25 20:13:52 +00007430
Nadav Rotem43012222011-05-11 08:12:09 +00007431/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007432/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007433SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007434 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007435 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007436 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007437 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007438 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007439 SDValue ShOpLo = Op.getOperand(0);
7440 SDValue ShOpHi = Op.getOperand(1);
7441 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007442 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007443 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007444 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007445
Dan Gohman475871a2008-07-27 21:46:04 +00007446 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007447 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007448 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7449 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007450 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007451 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7452 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007453 }
Evan Chenge3413162006-01-09 18:33:28 +00007454
Owen Anderson825b72b2009-08-11 20:47:22 +00007455 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7456 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007457 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007458 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007459
Dan Gohman475871a2008-07-27 21:46:04 +00007460 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007461 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007462 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7463 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007464
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007465 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007466 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7467 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007468 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007469 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7470 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007471 }
7472
Dan Gohman475871a2008-07-27 21:46:04 +00007473 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007474 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007475}
Evan Chenga3195e82006-01-12 22:54:21 +00007476
Dan Gohmand858e902010-04-17 15:26:15 +00007477SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7478 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007479 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007480
Dale Johannesen0488fb62010-09-30 23:57:10 +00007481 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007482 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007483
Owen Anderson825b72b2009-08-11 20:47:22 +00007484 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007485 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007486
Eli Friedman36df4992009-05-27 00:47:34 +00007487 // These are really Legal; return the operand so the caller accepts it as
7488 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007489 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007490 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007491 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007492 Subtarget->is64Bit()) {
7493 return Op;
7494 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007495
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007496 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007497 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007498 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007499 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007500 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007501 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007502 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007503 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007504 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007505 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7506}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007507
Owen Andersone50ed302009-08-10 22:56:29 +00007508SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007509 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007510 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007511 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007512 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007513 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007514 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007515 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007516 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007517 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007518 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007519
Chris Lattner492a43e2010-09-22 01:28:21 +00007520 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007521
Stuart Hastings84be9582011-06-02 15:57:11 +00007522 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7523 MachineMemOperand *MMO;
7524 if (FI) {
7525 int SSFI = FI->getIndex();
7526 MMO =
7527 DAG.getMachineFunction()
7528 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7529 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7530 } else {
7531 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7532 StackSlot = StackSlot.getOperand(1);
7533 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007534 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007535 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7536 X86ISD::FILD, DL,
7537 Tys, Ops, array_lengthof(Ops),
7538 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007539
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007540 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007541 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007542 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007543
7544 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7545 // shouldn't be necessary except that RFP cannot be live across
7546 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007547 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007548 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7549 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007550 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007551 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007552 SDValue Ops[] = {
7553 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7554 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007555 MachineMemOperand *MMO =
7556 DAG.getMachineFunction()
7557 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007558 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007559
Chris Lattner492a43e2010-09-22 01:28:21 +00007560 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7561 Ops, array_lengthof(Ops),
7562 Op.getValueType(), MMO);
7563 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007564 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007565 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007566 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007567
Evan Cheng0db9fe62006-04-25 20:13:52 +00007568 return Result;
7569}
7570
Bill Wendling8b8a6362009-01-17 03:56:04 +00007571// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007572SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7573 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007574 // This algorithm is not obvious. Here it is in C code, more or less:
7575 /*
7576 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7577 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7578 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007579
Bill Wendling8b8a6362009-01-17 03:56:04 +00007580 // Copy ints to xmm registers.
7581 __m128i xh = _mm_cvtsi32_si128( hi );
7582 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007583
Bill Wendling8b8a6362009-01-17 03:56:04 +00007584 // Combine into low half of a single xmm register.
7585 __m128i x = _mm_unpacklo_epi32( xh, xl );
7586 __m128d d;
7587 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007588
Bill Wendling8b8a6362009-01-17 03:56:04 +00007589 // Merge in appropriate exponents to give the integer bits the right
7590 // magnitude.
7591 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007592
Bill Wendling8b8a6362009-01-17 03:56:04 +00007593 // Subtract away the biases to deal with the IEEE-754 double precision
7594 // implicit 1.
7595 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007596
Bill Wendling8b8a6362009-01-17 03:56:04 +00007597 // All conversions up to here are exact. The correctly rounded result is
7598 // calculated using the current rounding mode using the following
7599 // horizontal add.
7600 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7601 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7602 // store doesn't really need to be here (except
7603 // maybe to zero the other double)
7604 return sd;
7605 }
7606 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007607
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007608 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007609 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007610
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007611 // Build some magic constants.
Chad Rosier01d426e2011-12-15 01:16:09 +00007612 SmallVector<Constant*,4> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007613 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7614 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7615 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7616 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007617 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007618 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007619
Chad Rosier01d426e2011-12-15 01:16:09 +00007620 SmallVector<Constant*,2> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007621 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007622 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007623 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007624 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007625 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007626 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007627
Owen Anderson825b72b2009-08-11 20:47:22 +00007628 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7629 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007630 Op.getOperand(0),
7631 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007632 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7633 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007634 Op.getOperand(0),
7635 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007636 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7637 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007638 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007639 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007640 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007641 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007642 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007643 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007644 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007645 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007646
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007647 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007648 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007649 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7650 DAG.getUNDEF(MVT::v2f64), ShufMask);
7651 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7652 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007653 DAG.getIntPtrConstant(0));
7654}
7655
Bill Wendling8b8a6362009-01-17 03:56:04 +00007656// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007657SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7658 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007659 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007660 // FP constant to bias correct the final result.
7661 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007662 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007663
7664 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007665 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007666 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007667
Eli Friedmanf3704762011-08-29 21:15:46 +00007668 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007669 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7670 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007671
Owen Anderson825b72b2009-08-11 20:47:22 +00007672 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007673 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007674 DAG.getIntPtrConstant(0));
7675
7676 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007677 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007678 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007679 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007680 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007681 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007682 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007683 MVT::v2f64, Bias)));
7684 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007685 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007686 DAG.getIntPtrConstant(0));
7687
7688 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007689 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007690
7691 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007692 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007693
Owen Anderson825b72b2009-08-11 20:47:22 +00007694 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007695 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007696 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007697 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007698 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007699 }
7700
7701 // Handle final rounding.
7702 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007703}
7704
Dan Gohmand858e902010-04-17 15:26:15 +00007705SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7706 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007707 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007708 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007709
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007710 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007711 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7712 // the optimization here.
7713 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007714 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007715
Owen Andersone50ed302009-08-10 22:56:29 +00007716 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007717 EVT DstVT = Op.getValueType();
7718 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007719 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007720 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007721 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007722
7723 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007724 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007725 if (SrcVT == MVT::i32) {
7726 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7727 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7728 getPointerTy(), StackSlot, WordOff);
7729 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007730 StackSlot, MachinePointerInfo(),
7731 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007732 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007733 OffsetSlot, MachinePointerInfo(),
7734 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007735 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7736 return Fild;
7737 }
7738
7739 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7740 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007741 StackSlot, MachinePointerInfo(),
7742 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007743 // For i64 source, we need to add the appropriate power of 2 if the input
7744 // was negative. This is the same as the optimization in
7745 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7746 // we must be careful to do the computation in x87 extended precision, not
7747 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007748 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7749 MachineMemOperand *MMO =
7750 DAG.getMachineFunction()
7751 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7752 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007753
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007754 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7755 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007756 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7757 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007758
7759 APInt FF(32, 0x5F800000ULL);
7760
7761 // Check whether the sign bit is set.
7762 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7763 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7764 ISD::SETLT);
7765
7766 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7767 SDValue FudgePtr = DAG.getConstantPool(
7768 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7769 getPointerTy());
7770
7771 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7772 SDValue Zero = DAG.getIntPtrConstant(0);
7773 SDValue Four = DAG.getIntPtrConstant(4);
7774 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7775 Zero, Four);
7776 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7777
7778 // Load the value out, extending it from f32 to f80.
7779 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007780 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007781 FudgePtr, MachinePointerInfo::getConstantPool(),
7782 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007783 // Extend everything to 80 bits to force it to be done on x87.
7784 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7785 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007786}
7787
Dan Gohman475871a2008-07-27 21:46:04 +00007788std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007789FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007790 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007791
Owen Andersone50ed302009-08-10 22:56:29 +00007792 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007793
7794 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007795 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7796 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007797 }
7798
Owen Anderson825b72b2009-08-11 20:47:22 +00007799 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7800 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007801 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007802
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007803 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007804 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007805 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007806 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007807 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007808 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007809 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007810 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007811
Evan Cheng87c89352007-10-15 20:11:21 +00007812 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7813 // stack slot.
7814 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007815 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007816 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007817 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007818
Michael J. Spencerec38de22010-10-10 22:04:20 +00007819
7820
Evan Cheng0db9fe62006-04-25 20:13:52 +00007821 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007822 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007823 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007824 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7825 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7826 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007827 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007828
Dan Gohman475871a2008-07-27 21:46:04 +00007829 SDValue Chain = DAG.getEntryNode();
7830 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007831 EVT TheVT = Op.getOperand(0).getValueType();
7832 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007833 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007834 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007835 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007836 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007837 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007838 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007839 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007840 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007841
Chris Lattner492a43e2010-09-22 01:28:21 +00007842 MachineMemOperand *MMO =
7843 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7844 MachineMemOperand::MOLoad, MemSize, MemSize);
7845 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7846 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007847 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007848 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007849 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7850 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007851
Chris Lattner07290932010-09-22 01:05:16 +00007852 MachineMemOperand *MMO =
7853 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7854 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007855
Evan Cheng0db9fe62006-04-25 20:13:52 +00007856 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007857 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007858 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7859 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007860
Chris Lattner27a6c732007-11-24 07:07:01 +00007861 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007862}
7863
Dan Gohmand858e902010-04-17 15:26:15 +00007864SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7865 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007866 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007867 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007868
Eli Friedman948e95a2009-05-23 09:59:16 +00007869 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007870 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007871 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7872 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007873
Chris Lattner27a6c732007-11-24 07:07:01 +00007874 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007875 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007876 FIST, StackSlot, MachinePointerInfo(),
7877 false, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007878}
7879
Dan Gohmand858e902010-04-17 15:26:15 +00007880SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7881 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007882 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7883 SDValue FIST = Vals.first, StackSlot = Vals.second;
7884 assert(FIST.getNode() && "Unexpected failure");
7885
7886 // Load the result.
7887 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007888 FIST, StackSlot, MachinePointerInfo(),
7889 false, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007890}
7891
Dan Gohmand858e902010-04-17 15:26:15 +00007892SDValue X86TargetLowering::LowerFABS(SDValue Op,
7893 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007894 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007895 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007896 EVT VT = Op.getValueType();
7897 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007898 if (VT.isVector())
7899 EltVT = VT.getVectorElementType();
Chad Rosier01d426e2011-12-15 01:16:09 +00007900 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007901 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007902 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Chad Rosier01d426e2011-12-15 01:16:09 +00007903 CV.assign(2, C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007904 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007905 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Chad Rosier01d426e2011-12-15 01:16:09 +00007906 CV.assign(4, C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007907 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007908 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007909 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007910 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007911 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007912 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007913 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007914}
7915
Dan Gohmand858e902010-04-17 15:26:15 +00007916SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007917 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007918 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007919 EVT VT = Op.getValueType();
7920 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00007921 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
7922 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007923 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00007924 NumElts = VT.getVectorNumElements();
7925 }
7926 SmallVector<Constant*,8> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007927 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007928 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Chad Rosiera860b182011-12-15 01:02:25 +00007929 CV.assign(NumElts, C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007930 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007931 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Chad Rosiera860b182011-12-15 01:02:25 +00007932 CV.assign(NumElts, C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007933 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007934 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007935 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007936 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007937 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007938 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007939 if (VT.isVector()) {
Chad Rosiera860b182011-12-15 01:02:25 +00007940 MVT XORVT = VT.getSizeInBits() == 128 ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007941 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00007942 DAG.getNode(ISD::XOR, dl, XORVT,
7943 DAG.getNode(ISD::BITCAST, dl, XORVT,
Dale Johannesenace16102009-02-03 19:33:06 +00007944 Op.getOperand(0)),
Chad Rosiera860b182011-12-15 01:02:25 +00007945 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007946 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007947 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007948 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007949}
7950
Dan Gohmand858e902010-04-17 15:26:15 +00007951SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007952 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007953 SDValue Op0 = Op.getOperand(0);
7954 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007955 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007956 EVT VT = Op.getValueType();
7957 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007958
7959 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007960 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007961 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007962 SrcVT = VT;
7963 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007964 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007965 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007966 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007967 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007968 }
7969
7970 // At this point the operands and the result should have the same
7971 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007972
Evan Cheng68c47cb2007-01-05 07:55:56 +00007973 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00007974 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007975 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007976 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7977 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007978 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007979 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7980 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7981 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7982 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007983 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007984 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007985 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007986 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007987 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007988 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007989 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007990
7991 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007992 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007993 // Op0 is MVT::f32, Op1 is MVT::f64.
7994 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7995 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7996 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007997 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007998 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007999 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008000 }
8001
Evan Cheng73d6cf12007-01-05 21:37:56 +00008002 // Clear first operand sign bit.
8003 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008004 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008005 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8006 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008007 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008008 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8009 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8010 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8011 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008012 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008013 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008014 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008015 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008016 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008017 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008018 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008019
8020 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008021 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008022}
8023
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008024SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8025 SDValue N0 = Op.getOperand(0);
8026 DebugLoc dl = Op.getDebugLoc();
8027 EVT VT = Op.getValueType();
8028
8029 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8030 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8031 DAG.getConstant(1, VT));
8032 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8033}
8034
Dan Gohman076aee32009-03-04 19:44:21 +00008035/// Emit nodes that will be selected as "test Op0,Op0", or something
8036/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008037SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008038 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008039 DebugLoc dl = Op.getDebugLoc();
8040
Dan Gohman31125812009-03-07 01:58:32 +00008041 // CF and OF aren't always set the way we want. Determine which
8042 // of these we need.
8043 bool NeedCF = false;
8044 bool NeedOF = false;
8045 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008046 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008047 case X86::COND_A: case X86::COND_AE:
8048 case X86::COND_B: case X86::COND_BE:
8049 NeedCF = true;
8050 break;
8051 case X86::COND_G: case X86::COND_GE:
8052 case X86::COND_L: case X86::COND_LE:
8053 case X86::COND_O: case X86::COND_NO:
8054 NeedOF = true;
8055 break;
Dan Gohman31125812009-03-07 01:58:32 +00008056 }
8057
Dan Gohman076aee32009-03-04 19:44:21 +00008058 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008059 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8060 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008061 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8062 // Emit a CMP with 0, which is the TEST pattern.
8063 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8064 DAG.getConstant(0, Op.getValueType()));
8065
8066 unsigned Opcode = 0;
8067 unsigned NumOperands = 0;
8068 switch (Op.getNode()->getOpcode()) {
8069 case ISD::ADD:
8070 // Due to an isel shortcoming, be conservative if this add is likely to be
8071 // selected as part of a load-modify-store instruction. When the root node
8072 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8073 // uses of other nodes in the match, such as the ADD in this case. This
8074 // leads to the ADD being left around and reselected, with the result being
8075 // two adds in the output. Alas, even if none our users are stores, that
8076 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8077 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8078 // climbing the DAG back to the root, and it doesn't seem to be worth the
8079 // effort.
8080 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008081 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8082 if (UI->getOpcode() != ISD::CopyToReg &&
8083 UI->getOpcode() != ISD::SETCC &&
8084 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008085 goto default_case;
8086
8087 if (ConstantSDNode *C =
8088 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8089 // An add of one will be selected as an INC.
8090 if (C->getAPIntValue() == 1) {
8091 Opcode = X86ISD::INC;
8092 NumOperands = 1;
8093 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008094 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008095
8096 // An add of negative one (subtract of one) will be selected as a DEC.
8097 if (C->getAPIntValue().isAllOnesValue()) {
8098 Opcode = X86ISD::DEC;
8099 NumOperands = 1;
8100 break;
8101 }
Dan Gohman076aee32009-03-04 19:44:21 +00008102 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008103
8104 // Otherwise use a regular EFLAGS-setting add.
8105 Opcode = X86ISD::ADD;
8106 NumOperands = 2;
8107 break;
8108 case ISD::AND: {
8109 // If the primary and result isn't used, don't bother using X86ISD::AND,
8110 // because a TEST instruction will be better.
8111 bool NonFlagUse = false;
8112 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8113 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8114 SDNode *User = *UI;
8115 unsigned UOpNo = UI.getOperandNo();
8116 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8117 // Look pass truncate.
8118 UOpNo = User->use_begin().getOperandNo();
8119 User = *User->use_begin();
8120 }
8121
8122 if (User->getOpcode() != ISD::BRCOND &&
8123 User->getOpcode() != ISD::SETCC &&
8124 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8125 NonFlagUse = true;
8126 break;
8127 }
Dan Gohman076aee32009-03-04 19:44:21 +00008128 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008129
8130 if (!NonFlagUse)
8131 break;
8132 }
8133 // FALL THROUGH
8134 case ISD::SUB:
8135 case ISD::OR:
8136 case ISD::XOR:
8137 // Due to the ISEL shortcoming noted above, be conservative if this op is
8138 // likely to be selected as part of a load-modify-store instruction.
8139 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8140 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8141 if (UI->getOpcode() == ISD::STORE)
8142 goto default_case;
8143
8144 // Otherwise use a regular EFLAGS-setting instruction.
8145 switch (Op.getNode()->getOpcode()) {
8146 default: llvm_unreachable("unexpected operator!");
8147 case ISD::SUB: Opcode = X86ISD::SUB; break;
8148 case ISD::OR: Opcode = X86ISD::OR; break;
8149 case ISD::XOR: Opcode = X86ISD::XOR; break;
8150 case ISD::AND: Opcode = X86ISD::AND; break;
8151 }
8152
8153 NumOperands = 2;
8154 break;
8155 case X86ISD::ADD:
8156 case X86ISD::SUB:
8157 case X86ISD::INC:
8158 case X86ISD::DEC:
8159 case X86ISD::OR:
8160 case X86ISD::XOR:
8161 case X86ISD::AND:
8162 return SDValue(Op.getNode(), 1);
8163 default:
8164 default_case:
8165 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008166 }
8167
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008168 if (Opcode == 0)
8169 // Emit a CMP with 0, which is the TEST pattern.
8170 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8171 DAG.getConstant(0, Op.getValueType()));
8172
8173 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8174 SmallVector<SDValue, 4> Ops;
8175 for (unsigned i = 0; i != NumOperands; ++i)
8176 Ops.push_back(Op.getOperand(i));
8177
8178 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8179 DAG.ReplaceAllUsesWith(Op, New);
8180 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008181}
8182
8183/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8184/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008185SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008186 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008187 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8188 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008189 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008190
8191 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008192 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008193}
8194
Evan Chengd40d03e2010-01-06 19:38:29 +00008195/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8196/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008197SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8198 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008199 SDValue Op0 = And.getOperand(0);
8200 SDValue Op1 = And.getOperand(1);
8201 if (Op0.getOpcode() == ISD::TRUNCATE)
8202 Op0 = Op0.getOperand(0);
8203 if (Op1.getOpcode() == ISD::TRUNCATE)
8204 Op1 = Op1.getOperand(0);
8205
Evan Chengd40d03e2010-01-06 19:38:29 +00008206 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008207 if (Op1.getOpcode() == ISD::SHL)
8208 std::swap(Op0, Op1);
8209 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008210 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8211 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008212 // If we looked past a truncate, check that it's only truncating away
8213 // known zeros.
8214 unsigned BitWidth = Op0.getValueSizeInBits();
8215 unsigned AndBitWidth = And.getValueSizeInBits();
8216 if (BitWidth > AndBitWidth) {
8217 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8218 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8219 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8220 return SDValue();
8221 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008222 LHS = Op1;
8223 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008224 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008225 } else if (Op1.getOpcode() == ISD::Constant) {
8226 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008227 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008228 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008229
8230 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008231 LHS = AndLHS.getOperand(0);
8232 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008233 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008234
8235 // Use BT if the immediate can't be encoded in a TEST instruction.
8236 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8237 LHS = AndLHS;
8238 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8239 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008240 }
Evan Cheng0488db92007-09-25 01:57:46 +00008241
Evan Chengd40d03e2010-01-06 19:38:29 +00008242 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008243 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008244 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008245 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008246 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008247 // Also promote i16 to i32 for performance / code size reason.
8248 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008249 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008250 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008251
Evan Chengd40d03e2010-01-06 19:38:29 +00008252 // If the operand types disagree, extend the shift amount to match. Since
8253 // BT ignores high bits (like shifts) we can use anyextend.
8254 if (LHS.getValueType() != RHS.getValueType())
8255 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008256
Evan Chengd40d03e2010-01-06 19:38:29 +00008257 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8258 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8259 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8260 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008261 }
8262
Evan Cheng54de3ea2010-01-05 06:52:31 +00008263 return SDValue();
8264}
8265
Dan Gohmand858e902010-04-17 15:26:15 +00008266SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008267
8268 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8269
Evan Cheng54de3ea2010-01-05 06:52:31 +00008270 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8271 SDValue Op0 = Op.getOperand(0);
8272 SDValue Op1 = Op.getOperand(1);
8273 DebugLoc dl = Op.getDebugLoc();
8274 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8275
8276 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008277 // Lower (X & (1 << N)) == 0 to BT(X, N).
8278 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8279 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008280 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008281 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008282 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008283 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8284 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8285 if (NewSetCC.getNode())
8286 return NewSetCC;
8287 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008288
Chris Lattner481eebc2010-12-19 21:23:48 +00008289 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8290 // these.
8291 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008292 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008293 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8294 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008295
Chris Lattner481eebc2010-12-19 21:23:48 +00008296 // If the input is a setcc, then reuse the input setcc or use a new one with
8297 // the inverted condition.
8298 if (Op0.getOpcode() == X86ISD::SETCC) {
8299 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8300 bool Invert = (CC == ISD::SETNE) ^
8301 cast<ConstantSDNode>(Op1)->isNullValue();
8302 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008303
Evan Cheng2c755ba2010-02-27 07:36:59 +00008304 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008305 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8306 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8307 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008308 }
8309
Evan Chenge5b51ac2010-04-17 06:13:15 +00008310 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008311 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008312 if (X86CC == X86::COND_INVALID)
8313 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008314
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008315 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008316 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008317 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008318}
8319
Craig Topper89af15e2011-09-18 08:03:58 +00008320// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008321// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008322static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008323 EVT VT = Op.getValueType();
8324
Duncan Sands28b77e92011-09-06 19:07:46 +00008325 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008326 "Unsupported value type for operation");
8327
8328 int NumElems = VT.getVectorNumElements();
8329 DebugLoc dl = Op.getDebugLoc();
8330 SDValue CC = Op.getOperand(2);
8331 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8332 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8333
8334 // Extract the LHS vectors
8335 SDValue LHS = Op.getOperand(0);
8336 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8337 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8338
8339 // Extract the RHS vectors
8340 SDValue RHS = Op.getOperand(1);
8341 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8342 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8343
8344 // Issue the operation on the smaller types and concatenate the result back
8345 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8346 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8347 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8348 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8349 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8350}
8351
8352
Dan Gohmand858e902010-04-17 15:26:15 +00008353SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008354 SDValue Cond;
8355 SDValue Op0 = Op.getOperand(0);
8356 SDValue Op1 = Op.getOperand(1);
8357 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008358 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008359 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8360 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008361 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008362
8363 if (isFP) {
8364 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008365 EVT EltVT = Op0.getValueType().getVectorElementType();
8366 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8367
8368 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008369 bool Swap = false;
8370
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008371 // SSE Condition code mapping:
8372 // 0 - EQ
8373 // 1 - LT
8374 // 2 - LE
8375 // 3 - UNORD
8376 // 4 - NEQ
8377 // 5 - NLT
8378 // 6 - NLE
8379 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008380 switch (SetCCOpcode) {
8381 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008382 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008383 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008384 case ISD::SETOGT:
8385 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008386 case ISD::SETLT:
8387 case ISD::SETOLT: SSECC = 1; break;
8388 case ISD::SETOGE:
8389 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008390 case ISD::SETLE:
8391 case ISD::SETOLE: SSECC = 2; break;
8392 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008393 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008394 case ISD::SETNE: SSECC = 4; break;
8395 case ISD::SETULE: Swap = true;
8396 case ISD::SETUGE: SSECC = 5; break;
8397 case ISD::SETULT: Swap = true;
8398 case ISD::SETUGT: SSECC = 6; break;
8399 case ISD::SETO: SSECC = 7; break;
8400 }
8401 if (Swap)
8402 std::swap(Op0, Op1);
8403
Nate Begemanfb8ead02008-07-25 19:05:58 +00008404 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008405 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008406 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008407 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008408 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8409 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008410 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Craig Topper0a150352011-11-09 08:06:13 +00008411 } else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008412 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008413 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8414 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008415 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008416 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008417 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008418 }
8419 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008420 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008421 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008422
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008423 // Break 256-bit integer vector compare into smaller ones.
Craig Topper0a150352011-11-09 08:06:13 +00008424 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008425 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008426
Nate Begeman30a0de92008-07-17 16:51:19 +00008427 // We are handling one of the integer comparisons here. Since SSE only has
8428 // GT and EQ comparisons for integer, swapping operands and multiple
8429 // operations may be required for some comparisons.
8430 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8431 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008432
Craig Topper0a150352011-11-09 08:06:13 +00008433 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008434 default: break;
Craig Topper0a150352011-11-09 08:06:13 +00008435 case MVT::i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
8436 case MVT::i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
8437 case MVT::i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8438 case MVT::i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008439 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008440
Nate Begeman30a0de92008-07-17 16:51:19 +00008441 switch (SetCCOpcode) {
8442 default: break;
8443 case ISD::SETNE: Invert = true;
8444 case ISD::SETEQ: Opc = EQOpc; break;
8445 case ISD::SETLT: Swap = true;
8446 case ISD::SETGT: Opc = GTOpc; break;
8447 case ISD::SETGE: Swap = true;
8448 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8449 case ISD::SETULT: Swap = true;
8450 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8451 case ISD::SETUGE: Swap = true;
8452 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8453 }
8454 if (Swap)
8455 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008456
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008457 // Check that the operation in question is available (most are plain SSE2,
8458 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topperc0d82852011-11-22 00:44:41 +00008459 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42orAVX())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008460 return SDValue();
Craig Topperc0d82852011-11-22 00:44:41 +00008461 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41orAVX())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008462 return SDValue();
8463
Nate Begeman30a0de92008-07-17 16:51:19 +00008464 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8465 // bits of the inputs before performing those operations.
8466 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008467 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008468 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8469 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008470 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008471 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8472 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008473 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8474 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008475 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008476
Dale Johannesenace16102009-02-03 19:33:06 +00008477 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008478
8479 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008480 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008481 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008482
Nate Begeman30a0de92008-07-17 16:51:19 +00008483 return Result;
8484}
Evan Cheng0488db92007-09-25 01:57:46 +00008485
Evan Cheng370e5342008-12-03 08:38:43 +00008486// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008487static bool isX86LogicalCmp(SDValue Op) {
8488 unsigned Opc = Op.getNode()->getOpcode();
8489 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8490 return true;
8491 if (Op.getResNo() == 1 &&
8492 (Opc == X86ISD::ADD ||
8493 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008494 Opc == X86ISD::ADC ||
8495 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008496 Opc == X86ISD::SMUL ||
8497 Opc == X86ISD::UMUL ||
8498 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008499 Opc == X86ISD::DEC ||
8500 Opc == X86ISD::OR ||
8501 Opc == X86ISD::XOR ||
8502 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008503 return true;
8504
Chris Lattner9637d5b2010-12-05 07:49:54 +00008505 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8506 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008507
Dan Gohman076aee32009-03-04 19:44:21 +00008508 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008509}
8510
Chris Lattnera2b56002010-12-05 01:23:24 +00008511static bool isZero(SDValue V) {
8512 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8513 return C && C->isNullValue();
8514}
8515
Chris Lattner96908b12010-12-05 02:00:51 +00008516static bool isAllOnes(SDValue V) {
8517 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8518 return C && C->isAllOnesValue();
8519}
8520
Dan Gohmand858e902010-04-17 15:26:15 +00008521SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008522 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008523 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008524 SDValue Op1 = Op.getOperand(1);
8525 SDValue Op2 = Op.getOperand(2);
8526 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008527 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008528
Dan Gohman1a492952009-10-20 16:22:37 +00008529 if (Cond.getOpcode() == ISD::SETCC) {
8530 SDValue NewCond = LowerSETCC(Cond, DAG);
8531 if (NewCond.getNode())
8532 Cond = NewCond;
8533 }
Evan Cheng734503b2006-09-11 02:19:56 +00008534
Chris Lattnera2b56002010-12-05 01:23:24 +00008535 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008536 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008537 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008538 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008539 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008540 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8541 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008542 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008543
Chris Lattnera2b56002010-12-05 01:23:24 +00008544 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008545
8546 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008547 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8548 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008549
8550 SDValue CmpOp0 = Cmp.getOperand(0);
8551 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8552 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008553
Chris Lattner96908b12010-12-05 02:00:51 +00008554 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008555 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8556 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008557
Chris Lattner96908b12010-12-05 02:00:51 +00008558 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8559 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008560
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008561 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008562 if (N2C == 0 || !N2C->isNullValue())
8563 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8564 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008565 }
8566 }
8567
Chris Lattnera2b56002010-12-05 01:23:24 +00008568 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008569 if (Cond.getOpcode() == ISD::AND &&
8570 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8571 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008572 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008573 Cond = Cond.getOperand(0);
8574 }
8575
Evan Cheng3f41d662007-10-08 22:16:29 +00008576 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8577 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008578 unsigned CondOpcode = Cond.getOpcode();
8579 if (CondOpcode == X86ISD::SETCC ||
8580 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008581 CC = Cond.getOperand(0);
8582
Dan Gohman475871a2008-07-27 21:46:04 +00008583 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008584 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008585 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008586
Evan Cheng3f41d662007-10-08 22:16:29 +00008587 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008588 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008589 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008590 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008591
Chris Lattnerd1980a52009-03-12 06:52:53 +00008592 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8593 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008594 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008595 addTest = false;
8596 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008597 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8598 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8599 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8600 Cond.getOperand(0).getValueType() != MVT::i8)) {
8601 SDValue LHS = Cond.getOperand(0);
8602 SDValue RHS = Cond.getOperand(1);
8603 unsigned X86Opcode;
8604 unsigned X86Cond;
8605 SDVTList VTs;
8606 switch (CondOpcode) {
8607 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8608 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8609 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8610 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8611 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8612 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8613 default: llvm_unreachable("unexpected overflowing operator");
8614 }
8615 if (CondOpcode == ISD::UMULO)
8616 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8617 MVT::i32);
8618 else
8619 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8620
8621 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8622
8623 if (CondOpcode == ISD::UMULO)
8624 Cond = X86Op.getValue(2);
8625 else
8626 Cond = X86Op.getValue(1);
8627
8628 CC = DAG.getConstant(X86Cond, MVT::i8);
8629 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008630 }
8631
8632 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008633 // Look pass the truncate.
8634 if (Cond.getOpcode() == ISD::TRUNCATE)
8635 Cond = Cond.getOperand(0);
8636
8637 // We know the result of AND is compared against zero. Try to match
8638 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008639 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008640 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008641 if (NewSetCC.getNode()) {
8642 CC = NewSetCC.getOperand(0);
8643 Cond = NewSetCC.getOperand(1);
8644 addTest = false;
8645 }
8646 }
8647 }
8648
8649 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008650 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008651 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008652 }
8653
Benjamin Kramere915ff32010-12-22 23:09:28 +00008654 // a < b ? -1 : 0 -> RES = ~setcc_carry
8655 // a < b ? 0 : -1 -> RES = setcc_carry
8656 // a >= b ? -1 : 0 -> RES = setcc_carry
8657 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8658 if (Cond.getOpcode() == X86ISD::CMP) {
8659 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8660
8661 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8662 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8663 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8664 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8665 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8666 return DAG.getNOT(DL, Res, Res.getValueType());
8667 return Res;
8668 }
8669 }
8670
Evan Cheng0488db92007-09-25 01:57:46 +00008671 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8672 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008673 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008674 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008675 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008676}
8677
Evan Cheng370e5342008-12-03 08:38:43 +00008678// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8679// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8680// from the AND / OR.
8681static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8682 Opc = Op.getOpcode();
8683 if (Opc != ISD::OR && Opc != ISD::AND)
8684 return false;
8685 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8686 Op.getOperand(0).hasOneUse() &&
8687 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8688 Op.getOperand(1).hasOneUse());
8689}
8690
Evan Cheng961d6d42009-02-02 08:19:07 +00008691// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8692// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008693static bool isXor1OfSetCC(SDValue Op) {
8694 if (Op.getOpcode() != ISD::XOR)
8695 return false;
8696 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8697 if (N1C && N1C->getAPIntValue() == 1) {
8698 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8699 Op.getOperand(0).hasOneUse();
8700 }
8701 return false;
8702}
8703
Dan Gohmand858e902010-04-17 15:26:15 +00008704SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008705 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008706 SDValue Chain = Op.getOperand(0);
8707 SDValue Cond = Op.getOperand(1);
8708 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008709 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008710 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008711 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008712
Dan Gohman1a492952009-10-20 16:22:37 +00008713 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008714 // Check for setcc([su]{add,sub,mul}o == 0).
8715 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8716 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8717 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8718 Cond.getOperand(0).getResNo() == 1 &&
8719 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
8720 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
8721 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
8722 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
8723 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
8724 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
8725 Inverted = true;
8726 Cond = Cond.getOperand(0);
8727 } else {
8728 SDValue NewCond = LowerSETCC(Cond, DAG);
8729 if (NewCond.getNode())
8730 Cond = NewCond;
8731 }
Dan Gohman1a492952009-10-20 16:22:37 +00008732 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008733#if 0
8734 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008735 else if (Cond.getOpcode() == X86ISD::ADD ||
8736 Cond.getOpcode() == X86ISD::SUB ||
8737 Cond.getOpcode() == X86ISD::SMUL ||
8738 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008739 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008740#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008741
Evan Chengad9c0a32009-12-15 00:53:42 +00008742 // Look pass (and (setcc_carry (cmp ...)), 1).
8743 if (Cond.getOpcode() == ISD::AND &&
8744 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8745 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008746 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008747 Cond = Cond.getOperand(0);
8748 }
8749
Evan Cheng3f41d662007-10-08 22:16:29 +00008750 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8751 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008752 unsigned CondOpcode = Cond.getOpcode();
8753 if (CondOpcode == X86ISD::SETCC ||
8754 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008755 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008756
Dan Gohman475871a2008-07-27 21:46:04 +00008757 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008758 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008759 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008760 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008761 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008762 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008763 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008764 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008765 default: break;
8766 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008767 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008768 // These can only come from an arithmetic instruction with overflow,
8769 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008770 Cond = Cond.getNode()->getOperand(1);
8771 addTest = false;
8772 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008773 }
Evan Cheng0488db92007-09-25 01:57:46 +00008774 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008775 }
8776 CondOpcode = Cond.getOpcode();
8777 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8778 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8779 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8780 Cond.getOperand(0).getValueType() != MVT::i8)) {
8781 SDValue LHS = Cond.getOperand(0);
8782 SDValue RHS = Cond.getOperand(1);
8783 unsigned X86Opcode;
8784 unsigned X86Cond;
8785 SDVTList VTs;
8786 switch (CondOpcode) {
8787 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8788 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8789 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8790 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8791 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8792 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8793 default: llvm_unreachable("unexpected overflowing operator");
8794 }
8795 if (Inverted)
8796 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
8797 if (CondOpcode == ISD::UMULO)
8798 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8799 MVT::i32);
8800 else
8801 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8802
8803 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
8804
8805 if (CondOpcode == ISD::UMULO)
8806 Cond = X86Op.getValue(2);
8807 else
8808 Cond = X86Op.getValue(1);
8809
8810 CC = DAG.getConstant(X86Cond, MVT::i8);
8811 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00008812 } else {
8813 unsigned CondOpc;
8814 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8815 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008816 if (CondOpc == ISD::OR) {
8817 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8818 // two branches instead of an explicit OR instruction with a
8819 // separate test.
8820 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008821 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008822 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008823 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008824 Chain, Dest, CC, Cmp);
8825 CC = Cond.getOperand(1).getOperand(0);
8826 Cond = Cmp;
8827 addTest = false;
8828 }
8829 } else { // ISD::AND
8830 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8831 // two branches instead of an explicit AND instruction with a
8832 // separate test. However, we only do this if this block doesn't
8833 // have a fall-through edge, because this requires an explicit
8834 // jmp when the condition is false.
8835 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008836 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008837 Op.getNode()->hasOneUse()) {
8838 X86::CondCode CCode =
8839 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8840 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008841 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008842 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008843 // Look for an unconditional branch following this conditional branch.
8844 // We need this because we need to reverse the successors in order
8845 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008846 if (User->getOpcode() == ISD::BR) {
8847 SDValue FalseBB = User->getOperand(1);
8848 SDNode *NewBR =
8849 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008850 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008851 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008852 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008853
Dale Johannesene4d209d2009-02-03 20:21:25 +00008854 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008855 Chain, Dest, CC, Cmp);
8856 X86::CondCode CCode =
8857 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8858 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008859 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008860 Cond = Cmp;
8861 addTest = false;
8862 }
8863 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008864 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008865 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8866 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8867 // It should be transformed during dag combiner except when the condition
8868 // is set by a arithmetics with overflow node.
8869 X86::CondCode CCode =
8870 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8871 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008872 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008873 Cond = Cond.getOperand(0).getOperand(1);
8874 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00008875 } else if (Cond.getOpcode() == ISD::SETCC &&
8876 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
8877 // For FCMP_OEQ, we can emit
8878 // two branches instead of an explicit AND instruction with a
8879 // separate test. However, we only do this if this block doesn't
8880 // have a fall-through edge, because this requires an explicit
8881 // jmp when the condition is false.
8882 if (Op.getNode()->hasOneUse()) {
8883 SDNode *User = *Op.getNode()->use_begin();
8884 // Look for an unconditional branch following this conditional branch.
8885 // We need this because we need to reverse the successors in order
8886 // to implement FCMP_OEQ.
8887 if (User->getOpcode() == ISD::BR) {
8888 SDValue FalseBB = User->getOperand(1);
8889 SDNode *NewBR =
8890 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8891 assert(NewBR == User);
8892 (void)NewBR;
8893 Dest = FalseBB;
8894
8895 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
8896 Cond.getOperand(0), Cond.getOperand(1));
8897 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8898 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8899 Chain, Dest, CC, Cmp);
8900 CC = DAG.getConstant(X86::COND_P, MVT::i8);
8901 Cond = Cmp;
8902 addTest = false;
8903 }
8904 }
8905 } else if (Cond.getOpcode() == ISD::SETCC &&
8906 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
8907 // For FCMP_UNE, we can emit
8908 // two branches instead of an explicit AND instruction with a
8909 // separate test. However, we only do this if this block doesn't
8910 // have a fall-through edge, because this requires an explicit
8911 // jmp when the condition is false.
8912 if (Op.getNode()->hasOneUse()) {
8913 SDNode *User = *Op.getNode()->use_begin();
8914 // Look for an unconditional branch following this conditional branch.
8915 // We need this because we need to reverse the successors in order
8916 // to implement FCMP_UNE.
8917 if (User->getOpcode() == ISD::BR) {
8918 SDValue FalseBB = User->getOperand(1);
8919 SDNode *NewBR =
8920 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8921 assert(NewBR == User);
8922 (void)NewBR;
8923
8924 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
8925 Cond.getOperand(0), Cond.getOperand(1));
8926 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8927 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8928 Chain, Dest, CC, Cmp);
8929 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
8930 Cond = Cmp;
8931 addTest = false;
8932 Dest = FalseBB;
8933 }
8934 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008935 }
Evan Cheng0488db92007-09-25 01:57:46 +00008936 }
8937
8938 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008939 // Look pass the truncate.
8940 if (Cond.getOpcode() == ISD::TRUNCATE)
8941 Cond = Cond.getOperand(0);
8942
8943 // We know the result of AND is compared against zero. Try to match
8944 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008945 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008946 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8947 if (NewSetCC.getNode()) {
8948 CC = NewSetCC.getOperand(0);
8949 Cond = NewSetCC.getOperand(1);
8950 addTest = false;
8951 }
8952 }
8953 }
8954
8955 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008956 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008957 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008958 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008959 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008960 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008961}
8962
Anton Korobeynikove060b532007-04-17 19:34:00 +00008963
8964// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8965// Calls to _alloca is needed to probe the stack when allocating more than 4k
8966// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8967// that the guard pages used by the OS virtual memory manager are allocated in
8968// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008969SDValue
8970X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008971 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008972 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008973 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008974 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008975 "are being used");
8976 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008977 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008978
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008979 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008980 SDValue Chain = Op.getOperand(0);
8981 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008982 // FIXME: Ensure alignment here
8983
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008984 bool Is64Bit = Subtarget->is64Bit();
8985 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008986
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008987 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008988 MachineFunction &MF = DAG.getMachineFunction();
8989 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008990
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008991 if (Is64Bit) {
8992 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008993 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008994 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008995
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008996 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8997 I != E; I++)
8998 if (I->hasNestAttr())
8999 report_fatal_error("Cannot use segmented stacks with functions that "
9000 "have nested arguments.");
9001 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009002
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009003 const TargetRegisterClass *AddrRegClass =
9004 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9005 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9006 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9007 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9008 DAG.getRegister(Vreg, SPTy));
9009 SDValue Ops1[2] = { Value, Chain };
9010 return DAG.getMergeValues(Ops1, 2, dl);
9011 } else {
9012 SDValue Flag;
9013 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009014
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009015 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9016 Flag = Chain.getValue(1);
9017 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009018
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009019 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9020 Flag = Chain.getValue(1);
9021
9022 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9023
9024 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9025 return DAG.getMergeValues(Ops1, 2, dl);
9026 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009027}
9028
Dan Gohmand858e902010-04-17 15:26:15 +00009029SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009030 MachineFunction &MF = DAG.getMachineFunction();
9031 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9032
Dan Gohman69de1932008-02-06 22:27:42 +00009033 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009034 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009035
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009036 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009037 // vastart just stores the address of the VarArgsFrameIndex slot into the
9038 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009039 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9040 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009041 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9042 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009043 }
9044
9045 // __va_list_tag:
9046 // gp_offset (0 - 6 * 8)
9047 // fp_offset (48 - 48 + 8 * 16)
9048 // overflow_arg_area (point to parameters coming in memory).
9049 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009050 SmallVector<SDValue, 8> MemOps;
9051 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009052 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009053 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009054 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9055 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009056 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009057 MemOps.push_back(Store);
9058
9059 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009060 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009061 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009062 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009063 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9064 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009065 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009066 MemOps.push_back(Store);
9067
9068 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009069 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009070 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009071 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9072 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009073 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9074 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009075 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009076 MemOps.push_back(Store);
9077
9078 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009079 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009080 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009081 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9082 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009083 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9084 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009085 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009086 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009087 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009088}
9089
Dan Gohmand858e902010-04-17 15:26:15 +00009090SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009091 assert(Subtarget->is64Bit() &&
9092 "LowerVAARG only handles 64-bit va_arg!");
9093 assert((Subtarget->isTargetLinux() ||
9094 Subtarget->isTargetDarwin()) &&
9095 "Unhandled target in LowerVAARG");
9096 assert(Op.getNode()->getNumOperands() == 4);
9097 SDValue Chain = Op.getOperand(0);
9098 SDValue SrcPtr = Op.getOperand(1);
9099 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9100 unsigned Align = Op.getConstantOperandVal(3);
9101 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009102
Dan Gohman320afb82010-10-12 18:00:49 +00009103 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009104 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009105 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9106 uint8_t ArgMode;
9107
9108 // Decide which area this value should be read from.
9109 // TODO: Implement the AMD64 ABI in its entirety. This simple
9110 // selection mechanism works only for the basic types.
9111 if (ArgVT == MVT::f80) {
9112 llvm_unreachable("va_arg for f80 not yet implemented");
9113 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9114 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9115 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9116 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9117 } else {
9118 llvm_unreachable("Unhandled argument type in LowerVAARG");
9119 }
9120
9121 if (ArgMode == 2) {
9122 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009123 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009124 !(DAG.getMachineFunction()
9125 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009126 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009127 }
9128
9129 // Insert VAARG_64 node into the DAG
9130 // VAARG_64 returns two values: Variable Argument Address, Chain
9131 SmallVector<SDValue, 11> InstOps;
9132 InstOps.push_back(Chain);
9133 InstOps.push_back(SrcPtr);
9134 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9135 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9136 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9137 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9138 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9139 VTs, &InstOps[0], InstOps.size(),
9140 MVT::i64,
9141 MachinePointerInfo(SV),
9142 /*Align=*/0,
9143 /*Volatile=*/false,
9144 /*ReadMem=*/true,
9145 /*WriteMem=*/true);
9146 Chain = VAARG.getValue(1);
9147
9148 // Load the next argument and return it
9149 return DAG.getLoad(ArgVT, dl,
9150 Chain,
9151 VAARG,
9152 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009153 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009154}
9155
Dan Gohmand858e902010-04-17 15:26:15 +00009156SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009157 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009158 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009159 SDValue Chain = Op.getOperand(0);
9160 SDValue DstPtr = Op.getOperand(1);
9161 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009162 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9163 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009164 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009165
Chris Lattnere72f2022010-09-21 05:40:29 +00009166 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009167 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009168 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009169 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009170}
9171
Dan Gohman475871a2008-07-27 21:46:04 +00009172SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009173X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009174 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009175 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009176 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009177 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009178 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009179 case Intrinsic::x86_sse_comieq_ss:
9180 case Intrinsic::x86_sse_comilt_ss:
9181 case Intrinsic::x86_sse_comile_ss:
9182 case Intrinsic::x86_sse_comigt_ss:
9183 case Intrinsic::x86_sse_comige_ss:
9184 case Intrinsic::x86_sse_comineq_ss:
9185 case Intrinsic::x86_sse_ucomieq_ss:
9186 case Intrinsic::x86_sse_ucomilt_ss:
9187 case Intrinsic::x86_sse_ucomile_ss:
9188 case Intrinsic::x86_sse_ucomigt_ss:
9189 case Intrinsic::x86_sse_ucomige_ss:
9190 case Intrinsic::x86_sse_ucomineq_ss:
9191 case Intrinsic::x86_sse2_comieq_sd:
9192 case Intrinsic::x86_sse2_comilt_sd:
9193 case Intrinsic::x86_sse2_comile_sd:
9194 case Intrinsic::x86_sse2_comigt_sd:
9195 case Intrinsic::x86_sse2_comige_sd:
9196 case Intrinsic::x86_sse2_comineq_sd:
9197 case Intrinsic::x86_sse2_ucomieq_sd:
9198 case Intrinsic::x86_sse2_ucomilt_sd:
9199 case Intrinsic::x86_sse2_ucomile_sd:
9200 case Intrinsic::x86_sse2_ucomigt_sd:
9201 case Intrinsic::x86_sse2_ucomige_sd:
9202 case Intrinsic::x86_sse2_ucomineq_sd: {
9203 unsigned Opc = 0;
9204 ISD::CondCode CC = ISD::SETCC_INVALID;
9205 switch (IntNo) {
9206 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009207 case Intrinsic::x86_sse_comieq_ss:
9208 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009209 Opc = X86ISD::COMI;
9210 CC = ISD::SETEQ;
9211 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009212 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009213 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009214 Opc = X86ISD::COMI;
9215 CC = ISD::SETLT;
9216 break;
9217 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009218 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009219 Opc = X86ISD::COMI;
9220 CC = ISD::SETLE;
9221 break;
9222 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009223 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009224 Opc = X86ISD::COMI;
9225 CC = ISD::SETGT;
9226 break;
9227 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009228 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009229 Opc = X86ISD::COMI;
9230 CC = ISD::SETGE;
9231 break;
9232 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009233 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009234 Opc = X86ISD::COMI;
9235 CC = ISD::SETNE;
9236 break;
9237 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009238 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009239 Opc = X86ISD::UCOMI;
9240 CC = ISD::SETEQ;
9241 break;
9242 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009243 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009244 Opc = X86ISD::UCOMI;
9245 CC = ISD::SETLT;
9246 break;
9247 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009248 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009249 Opc = X86ISD::UCOMI;
9250 CC = ISD::SETLE;
9251 break;
9252 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009253 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009254 Opc = X86ISD::UCOMI;
9255 CC = ISD::SETGT;
9256 break;
9257 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009258 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009259 Opc = X86ISD::UCOMI;
9260 CC = ISD::SETGE;
9261 break;
9262 case Intrinsic::x86_sse_ucomineq_ss:
9263 case Intrinsic::x86_sse2_ucomineq_sd:
9264 Opc = X86ISD::UCOMI;
9265 CC = ISD::SETNE;
9266 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009267 }
Evan Cheng734503b2006-09-11 02:19:56 +00009268
Dan Gohman475871a2008-07-27 21:46:04 +00009269 SDValue LHS = Op.getOperand(1);
9270 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009271 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009272 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009273 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9274 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9275 DAG.getConstant(X86CC, MVT::i8), Cond);
9276 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009277 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009278 // Arithmetic intrinsics.
9279 case Intrinsic::x86_sse3_hadd_ps:
9280 case Intrinsic::x86_sse3_hadd_pd:
9281 case Intrinsic::x86_avx_hadd_ps_256:
9282 case Intrinsic::x86_avx_hadd_pd_256:
9283 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9284 Op.getOperand(1), Op.getOperand(2));
9285 case Intrinsic::x86_sse3_hsub_ps:
9286 case Intrinsic::x86_sse3_hsub_pd:
9287 case Intrinsic::x86_avx_hsub_ps_256:
9288 case Intrinsic::x86_avx_hsub_pd_256:
9289 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9290 Op.getOperand(1), Op.getOperand(2));
Craig Topper98fc7292011-11-19 17:46:46 +00009291 case Intrinsic::x86_avx2_psllv_d:
9292 case Intrinsic::x86_avx2_psllv_q:
9293 case Intrinsic::x86_avx2_psllv_d_256:
9294 case Intrinsic::x86_avx2_psllv_q_256:
9295 return DAG.getNode(ISD::SHL, dl, Op.getValueType(),
9296 Op.getOperand(1), Op.getOperand(2));
9297 case Intrinsic::x86_avx2_psrlv_d:
9298 case Intrinsic::x86_avx2_psrlv_q:
9299 case Intrinsic::x86_avx2_psrlv_d_256:
9300 case Intrinsic::x86_avx2_psrlv_q_256:
9301 return DAG.getNode(ISD::SRL, dl, Op.getValueType(),
9302 Op.getOperand(1), Op.getOperand(2));
9303 case Intrinsic::x86_avx2_psrav_d:
9304 case Intrinsic::x86_avx2_psrav_d_256:
9305 return DAG.getNode(ISD::SRA, dl, Op.getValueType(),
9306 Op.getOperand(1), Op.getOperand(2));
9307
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009308 // ptest and testp intrinsics. The intrinsic these come from are designed to
9309 // return an integer value, not just an instruction so lower it to the ptest
9310 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009311 case Intrinsic::x86_sse41_ptestz:
9312 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009313 case Intrinsic::x86_sse41_ptestnzc:
9314 case Intrinsic::x86_avx_ptestz_256:
9315 case Intrinsic::x86_avx_ptestc_256:
9316 case Intrinsic::x86_avx_ptestnzc_256:
9317 case Intrinsic::x86_avx_vtestz_ps:
9318 case Intrinsic::x86_avx_vtestc_ps:
9319 case Intrinsic::x86_avx_vtestnzc_ps:
9320 case Intrinsic::x86_avx_vtestz_pd:
9321 case Intrinsic::x86_avx_vtestc_pd:
9322 case Intrinsic::x86_avx_vtestnzc_pd:
9323 case Intrinsic::x86_avx_vtestz_ps_256:
9324 case Intrinsic::x86_avx_vtestc_ps_256:
9325 case Intrinsic::x86_avx_vtestnzc_ps_256:
9326 case Intrinsic::x86_avx_vtestz_pd_256:
9327 case Intrinsic::x86_avx_vtestc_pd_256:
9328 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9329 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009330 unsigned X86CC = 0;
9331 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009332 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009333 case Intrinsic::x86_avx_vtestz_ps:
9334 case Intrinsic::x86_avx_vtestz_pd:
9335 case Intrinsic::x86_avx_vtestz_ps_256:
9336 case Intrinsic::x86_avx_vtestz_pd_256:
9337 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009338 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009339 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009340 // ZF = 1
9341 X86CC = X86::COND_E;
9342 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009343 case Intrinsic::x86_avx_vtestc_ps:
9344 case Intrinsic::x86_avx_vtestc_pd:
9345 case Intrinsic::x86_avx_vtestc_ps_256:
9346 case Intrinsic::x86_avx_vtestc_pd_256:
9347 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009348 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009349 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009350 // CF = 1
9351 X86CC = X86::COND_B;
9352 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009353 case Intrinsic::x86_avx_vtestnzc_ps:
9354 case Intrinsic::x86_avx_vtestnzc_pd:
9355 case Intrinsic::x86_avx_vtestnzc_ps_256:
9356 case Intrinsic::x86_avx_vtestnzc_pd_256:
9357 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009358 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009359 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009360 // ZF and CF = 0
9361 X86CC = X86::COND_A;
9362 break;
9363 }
Eric Christopherfd179292009-08-27 18:07:15 +00009364
Eric Christopher71c67532009-07-29 00:28:05 +00009365 SDValue LHS = Op.getOperand(1);
9366 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009367 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9368 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009369 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9370 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9371 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009372 }
Evan Cheng5759f972008-05-04 09:15:50 +00009373
9374 // Fix vector shift instructions where the last operand is a non-immediate
9375 // i32 value.
Craig Topper7be5dfd2011-11-12 09:58:49 +00009376 case Intrinsic::x86_avx2_pslli_w:
9377 case Intrinsic::x86_avx2_pslli_d:
9378 case Intrinsic::x86_avx2_pslli_q:
9379 case Intrinsic::x86_avx2_psrli_w:
9380 case Intrinsic::x86_avx2_psrli_d:
9381 case Intrinsic::x86_avx2_psrli_q:
9382 case Intrinsic::x86_avx2_psrai_w:
9383 case Intrinsic::x86_avx2_psrai_d:
Evan Cheng5759f972008-05-04 09:15:50 +00009384 case Intrinsic::x86_sse2_pslli_w:
9385 case Intrinsic::x86_sse2_pslli_d:
9386 case Intrinsic::x86_sse2_pslli_q:
9387 case Intrinsic::x86_sse2_psrli_w:
9388 case Intrinsic::x86_sse2_psrli_d:
9389 case Intrinsic::x86_sse2_psrli_q:
9390 case Intrinsic::x86_sse2_psrai_w:
9391 case Intrinsic::x86_sse2_psrai_d:
9392 case Intrinsic::x86_mmx_pslli_w:
9393 case Intrinsic::x86_mmx_pslli_d:
9394 case Intrinsic::x86_mmx_pslli_q:
9395 case Intrinsic::x86_mmx_psrli_w:
9396 case Intrinsic::x86_mmx_psrli_d:
9397 case Intrinsic::x86_mmx_psrli_q:
9398 case Intrinsic::x86_mmx_psrai_w:
9399 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009400 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009401 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009402 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009403
9404 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009405 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009406 switch (IntNo) {
9407 case Intrinsic::x86_sse2_pslli_w:
9408 NewIntNo = Intrinsic::x86_sse2_psll_w;
9409 break;
9410 case Intrinsic::x86_sse2_pslli_d:
9411 NewIntNo = Intrinsic::x86_sse2_psll_d;
9412 break;
9413 case Intrinsic::x86_sse2_pslli_q:
9414 NewIntNo = Intrinsic::x86_sse2_psll_q;
9415 break;
9416 case Intrinsic::x86_sse2_psrli_w:
9417 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9418 break;
9419 case Intrinsic::x86_sse2_psrli_d:
9420 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9421 break;
9422 case Intrinsic::x86_sse2_psrli_q:
9423 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9424 break;
9425 case Intrinsic::x86_sse2_psrai_w:
9426 NewIntNo = Intrinsic::x86_sse2_psra_w;
9427 break;
9428 case Intrinsic::x86_sse2_psrai_d:
9429 NewIntNo = Intrinsic::x86_sse2_psra_d;
9430 break;
Craig Topper7be5dfd2011-11-12 09:58:49 +00009431 case Intrinsic::x86_avx2_pslli_w:
9432 NewIntNo = Intrinsic::x86_avx2_psll_w;
9433 break;
9434 case Intrinsic::x86_avx2_pslli_d:
9435 NewIntNo = Intrinsic::x86_avx2_psll_d;
9436 break;
9437 case Intrinsic::x86_avx2_pslli_q:
9438 NewIntNo = Intrinsic::x86_avx2_psll_q;
9439 break;
9440 case Intrinsic::x86_avx2_psrli_w:
9441 NewIntNo = Intrinsic::x86_avx2_psrl_w;
9442 break;
9443 case Intrinsic::x86_avx2_psrli_d:
9444 NewIntNo = Intrinsic::x86_avx2_psrl_d;
9445 break;
9446 case Intrinsic::x86_avx2_psrli_q:
9447 NewIntNo = Intrinsic::x86_avx2_psrl_q;
9448 break;
9449 case Intrinsic::x86_avx2_psrai_w:
9450 NewIntNo = Intrinsic::x86_avx2_psra_w;
9451 break;
9452 case Intrinsic::x86_avx2_psrai_d:
9453 NewIntNo = Intrinsic::x86_avx2_psra_d;
9454 break;
Evan Cheng5759f972008-05-04 09:15:50 +00009455 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009456 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009457 switch (IntNo) {
9458 case Intrinsic::x86_mmx_pslli_w:
9459 NewIntNo = Intrinsic::x86_mmx_psll_w;
9460 break;
9461 case Intrinsic::x86_mmx_pslli_d:
9462 NewIntNo = Intrinsic::x86_mmx_psll_d;
9463 break;
9464 case Intrinsic::x86_mmx_pslli_q:
9465 NewIntNo = Intrinsic::x86_mmx_psll_q;
9466 break;
9467 case Intrinsic::x86_mmx_psrli_w:
9468 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9469 break;
9470 case Intrinsic::x86_mmx_psrli_d:
9471 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9472 break;
9473 case Intrinsic::x86_mmx_psrli_q:
9474 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9475 break;
9476 case Intrinsic::x86_mmx_psrai_w:
9477 NewIntNo = Intrinsic::x86_mmx_psra_w;
9478 break;
9479 case Intrinsic::x86_mmx_psrai_d:
9480 NewIntNo = Intrinsic::x86_mmx_psra_d;
9481 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009482 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009483 }
9484 break;
9485 }
9486 }
Mon P Wangefa42202009-09-03 19:56:25 +00009487
9488 // The vector shift intrinsics with scalars uses 32b shift amounts but
9489 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9490 // to be zero.
9491 SDValue ShOps[4];
9492 ShOps[0] = ShAmt;
9493 ShOps[1] = DAG.getConstant(0, MVT::i32);
9494 if (ShAmtVT == MVT::v4i32) {
9495 ShOps[2] = DAG.getUNDEF(MVT::i32);
9496 ShOps[3] = DAG.getUNDEF(MVT::i32);
9497 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9498 } else {
9499 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009500// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009501 }
9502
Owen Andersone50ed302009-08-10 22:56:29 +00009503 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009504 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009505 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009506 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009507 Op.getOperand(1), ShAmt);
9508 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009509 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009510}
Evan Cheng72261582005-12-20 06:22:03 +00009511
Dan Gohmand858e902010-04-17 15:26:15 +00009512SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9513 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009514 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9515 MFI->setReturnAddressIsTaken(true);
9516
Bill Wendling64e87322009-01-16 19:25:27 +00009517 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009518 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009519
9520 if (Depth > 0) {
9521 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9522 SDValue Offset =
9523 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009524 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009525 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009526 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009527 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009528 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009529 }
9530
9531 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009532 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009533 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009534 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009535}
9536
Dan Gohmand858e902010-04-17 15:26:15 +00009537SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009538 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9539 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009540
Owen Andersone50ed302009-08-10 22:56:29 +00009541 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009542 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009543 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9544 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009545 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009546 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009547 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9548 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009549 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009550 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009551}
9552
Dan Gohman475871a2008-07-27 21:46:04 +00009553SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009554 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009555 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009556}
9557
Dan Gohmand858e902010-04-17 15:26:15 +00009558SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009559 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009560 SDValue Chain = Op.getOperand(0);
9561 SDValue Offset = Op.getOperand(1);
9562 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009563 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009564
Dan Gohmand8816272010-08-11 18:14:00 +00009565 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9566 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9567 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009568 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009569
Dan Gohmand8816272010-08-11 18:14:00 +00009570 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9571 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009572 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009573 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9574 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009575 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009576 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009577
Dale Johannesene4d209d2009-02-03 20:21:25 +00009578 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009579 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009580 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009581}
9582
Duncan Sands4a544a72011-09-06 13:37:06 +00009583SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9584 SelectionDAG &DAG) const {
9585 return Op.getOperand(0);
9586}
9587
9588SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9589 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009590 SDValue Root = Op.getOperand(0);
9591 SDValue Trmp = Op.getOperand(1); // trampoline
9592 SDValue FPtr = Op.getOperand(2); // nested function
9593 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009594 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009595
Dan Gohman69de1932008-02-06 22:27:42 +00009596 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009597
9598 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009599 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009600
9601 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009602 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9603 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009604
Evan Cheng0e6a0522011-07-18 20:57:22 +00009605 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9606 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009607
9608 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9609
9610 // Load the pointer to the nested function into R11.
9611 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009612 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009613 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009614 Addr, MachinePointerInfo(TrmpAddr),
9615 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009616
Owen Anderson825b72b2009-08-11 20:47:22 +00009617 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9618 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009619 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9620 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009621 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009622
9623 // Load the 'nest' parameter value into R10.
9624 // R10 is specified in X86CallingConv.td
9625 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009626 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9627 DAG.getConstant(10, MVT::i64));
9628 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009629 Addr, MachinePointerInfo(TrmpAddr, 10),
9630 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009631
Owen Anderson825b72b2009-08-11 20:47:22 +00009632 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9633 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009634 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9635 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009636 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009637
9638 // Jump to the nested function.
9639 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009640 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9641 DAG.getConstant(20, MVT::i64));
9642 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009643 Addr, MachinePointerInfo(TrmpAddr, 20),
9644 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009645
9646 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009647 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9648 DAG.getConstant(22, MVT::i64));
9649 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009650 MachinePointerInfo(TrmpAddr, 22),
9651 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009652
Duncan Sands4a544a72011-09-06 13:37:06 +00009653 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009654 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009655 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009656 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009657 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009658 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009659
9660 switch (CC) {
9661 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009662 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009663 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009664 case CallingConv::X86_StdCall: {
9665 // Pass 'nest' parameter in ECX.
9666 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009667 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009668
9669 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009670 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009671 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009672
Chris Lattner58d74912008-03-12 17:45:29 +00009673 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009674 unsigned InRegCount = 0;
9675 unsigned Idx = 1;
9676
9677 for (FunctionType::param_iterator I = FTy->param_begin(),
9678 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009679 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009680 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009681 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009682
9683 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009684 report_fatal_error("Nest register in use - reduce number of inreg"
9685 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009686 }
9687 }
9688 break;
9689 }
9690 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009691 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009692 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009693 // Pass 'nest' parameter in EAX.
9694 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009695 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009696 break;
9697 }
9698
Dan Gohman475871a2008-07-27 21:46:04 +00009699 SDValue OutChains[4];
9700 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009701
Owen Anderson825b72b2009-08-11 20:47:22 +00009702 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9703 DAG.getConstant(10, MVT::i32));
9704 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009705
Chris Lattnera62fe662010-02-05 19:20:30 +00009706 // This is storing the opcode for MOV32ri.
9707 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009708 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009709 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009710 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009711 Trmp, MachinePointerInfo(TrmpAddr),
9712 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009713
Owen Anderson825b72b2009-08-11 20:47:22 +00009714 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9715 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009716 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9717 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009718 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009719
Chris Lattnera62fe662010-02-05 19:20:30 +00009720 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009721 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9722 DAG.getConstant(5, MVT::i32));
9723 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009724 MachinePointerInfo(TrmpAddr, 5),
9725 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009726
Owen Anderson825b72b2009-08-11 20:47:22 +00009727 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9728 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009729 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9730 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009731 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009732
Duncan Sands4a544a72011-09-06 13:37:06 +00009733 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009734 }
9735}
9736
Dan Gohmand858e902010-04-17 15:26:15 +00009737SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9738 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009739 /*
9740 The rounding mode is in bits 11:10 of FPSR, and has the following
9741 settings:
9742 00 Round to nearest
9743 01 Round to -inf
9744 10 Round to +inf
9745 11 Round to 0
9746
9747 FLT_ROUNDS, on the other hand, expects the following:
9748 -1 Undefined
9749 0 Round to 0
9750 1 Round to nearest
9751 2 Round to +inf
9752 3 Round to -inf
9753
9754 To perform the conversion, we do:
9755 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9756 */
9757
9758 MachineFunction &MF = DAG.getMachineFunction();
9759 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009760 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009761 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009762 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009763 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009764
9765 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009766 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009767 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009768
Michael J. Spencerec38de22010-10-10 22:04:20 +00009769
Chris Lattner2156b792010-09-22 01:11:26 +00009770 MachineMemOperand *MMO =
9771 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9772 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009773
Chris Lattner2156b792010-09-22 01:11:26 +00009774 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9775 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9776 DAG.getVTList(MVT::Other),
9777 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009778
9779 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009780 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009781 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009782
9783 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009784 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009785 DAG.getNode(ISD::SRL, DL, MVT::i16,
9786 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009787 CWD, DAG.getConstant(0x800, MVT::i16)),
9788 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009789 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009790 DAG.getNode(ISD::SRL, DL, MVT::i16,
9791 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009792 CWD, DAG.getConstant(0x400, MVT::i16)),
9793 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009794
Dan Gohman475871a2008-07-27 21:46:04 +00009795 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009796 DAG.getNode(ISD::AND, DL, MVT::i16,
9797 DAG.getNode(ISD::ADD, DL, MVT::i16,
9798 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009799 DAG.getConstant(1, MVT::i16)),
9800 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009801
9802
Duncan Sands83ec4b62008-06-06 12:08:01 +00009803 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009804 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009805}
9806
Dan Gohmand858e902010-04-17 15:26:15 +00009807SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009808 EVT VT = Op.getValueType();
9809 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009810 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009811 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009812
9813 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009814 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009815 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009816 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009817 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009818 }
Evan Cheng18efe262007-12-14 02:13:44 +00009819
Evan Cheng152804e2007-12-14 08:30:15 +00009820 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009821 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009822 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009823
9824 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009825 SDValue Ops[] = {
9826 Op,
9827 DAG.getConstant(NumBits+NumBits-1, OpVT),
9828 DAG.getConstant(X86::COND_E, MVT::i8),
9829 Op.getValue(1)
9830 };
9831 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009832
9833 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009834 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009835
Owen Anderson825b72b2009-08-11 20:47:22 +00009836 if (VT == MVT::i8)
9837 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009838 return Op;
9839}
9840
Dan Gohmand858e902010-04-17 15:26:15 +00009841SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009842 EVT VT = Op.getValueType();
9843 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009844 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009845 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009846
9847 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009848 if (VT == MVT::i8) {
9849 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009850 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009851 }
Evan Cheng152804e2007-12-14 08:30:15 +00009852
9853 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009854 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009855 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009856
9857 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009858 SDValue Ops[] = {
9859 Op,
9860 DAG.getConstant(NumBits, OpVT),
9861 DAG.getConstant(X86::COND_E, MVT::i8),
9862 Op.getValue(1)
9863 };
9864 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009865
Owen Anderson825b72b2009-08-11 20:47:22 +00009866 if (VT == MVT::i8)
9867 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009868 return Op;
9869}
9870
Craig Topper13894fa2011-08-24 06:14:18 +00009871// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9872// ones, and then concatenate the result back.
9873static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009874 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009875
9876 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9877 "Unsupported value type for operation");
9878
9879 int NumElems = VT.getVectorNumElements();
9880 DebugLoc dl = Op.getDebugLoc();
9881 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9882 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9883
9884 // Extract the LHS vectors
9885 SDValue LHS = Op.getOperand(0);
9886 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9887 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9888
9889 // Extract the RHS vectors
9890 SDValue RHS = Op.getOperand(1);
9891 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9892 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9893
9894 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9895 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9896
9897 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9898 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9899 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9900}
9901
9902SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9903 assert(Op.getValueType().getSizeInBits() == 256 &&
9904 Op.getValueType().isInteger() &&
9905 "Only handle AVX 256-bit vector integer operation");
9906 return Lower256IntArith(Op, DAG);
9907}
9908
9909SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9910 assert(Op.getValueType().getSizeInBits() == 256 &&
9911 Op.getValueType().isInteger() &&
9912 "Only handle AVX 256-bit vector integer operation");
9913 return Lower256IntArith(Op, DAG);
9914}
9915
9916SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9917 EVT VT = Op.getValueType();
9918
9919 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +00009920 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +00009921 return Lower256IntArith(Op, DAG);
9922
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009923 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009924
Craig Topperaaa643c2011-11-09 07:28:55 +00009925 SDValue A = Op.getOperand(0);
9926 SDValue B = Op.getOperand(1);
9927
9928 if (VT == MVT::v4i64) {
9929 assert(Subtarget->hasAVX2() && "Lowering v4i64 multiply requires AVX2");
9930
9931 // ulong2 Ahi = __builtin_ia32_psrlqi256( a, 32);
9932 // ulong2 Bhi = __builtin_ia32_psrlqi256( b, 32);
9933 // ulong2 AloBlo = __builtin_ia32_pmuludq256( a, b );
9934 // ulong2 AloBhi = __builtin_ia32_pmuludq256( a, Bhi );
9935 // ulong2 AhiBlo = __builtin_ia32_pmuludq256( Ahi, b );
9936 //
9937 // AloBhi = __builtin_ia32_psllqi256( AloBhi, 32 );
9938 // AhiBlo = __builtin_ia32_psllqi256( AhiBlo, 32 );
9939 // return AloBlo + AloBhi + AhiBlo;
9940
9941 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9942 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
9943 A, DAG.getConstant(32, MVT::i32));
9944 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9945 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
9946 B, DAG.getConstant(32, MVT::i32));
9947 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9948 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9949 A, B);
9950 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9951 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9952 A, Bhi);
9953 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9954 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
9955 Ahi, B);
9956 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9957 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
9958 AloBhi, DAG.getConstant(32, MVT::i32));
9959 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9960 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
9961 AhiBlo, DAG.getConstant(32, MVT::i32));
9962 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9963 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
9964 return Res;
9965 }
9966
9967 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
9968
Mon P Wangaf9b9522008-12-18 21:42:19 +00009969 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9970 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9971 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9972 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9973 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9974 //
9975 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9976 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9977 // return AloBlo + AloBhi + AhiBlo;
9978
Dale Johannesene4d209d2009-02-03 20:21:25 +00009979 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009980 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9981 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009982 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009983 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9984 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009985 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009986 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009987 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009988 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009989 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009990 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009991 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009992 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009993 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009994 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009995 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9996 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009997 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009998 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9999 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010000 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10001 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010002 return Res;
10003}
10004
Nadav Rotem43012222011-05-11 08:12:09 +000010005SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10006
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010007 EVT VT = Op.getValueType();
10008 DebugLoc dl = Op.getDebugLoc();
10009 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010010 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010011 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010012
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010013 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010014 return SDValue();
10015
Nadav Rotem43012222011-05-11 08:12:09 +000010016 // Optimize shl/srl/sra with constant shift amount.
10017 if (isSplatVector(Amt.getNode())) {
10018 SDValue SclrAmt = Amt->getOperand(0);
10019 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10020 uint64_t ShiftAmt = C->getZExtValue();
10021
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010022 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SHL) {
10023 // Make a large shift.
10024 SDValue SHL =
10025 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10026 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10027 R, DAG.getConstant(ShiftAmt, MVT::i32));
10028 // Zero out the rightmost bits.
10029 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10030 MVT::i8));
10031 return DAG.getNode(ISD::AND, dl, VT, SHL,
10032 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10033 }
10034
Nadav Rotem43012222011-05-11 08:12:09 +000010035 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
10036 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10037 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10038 R, DAG.getConstant(ShiftAmt, MVT::i32));
10039
10040 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
10041 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10042 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10043 R, DAG.getConstant(ShiftAmt, MVT::i32));
10044
10045 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
10046 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10047 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10048 R, DAG.getConstant(ShiftAmt, MVT::i32));
10049
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010050 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRL) {
10051 // Make a large shift.
10052 SDValue SRL =
10053 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10054 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10055 R, DAG.getConstant(ShiftAmt, MVT::i32));
10056 // Zero out the leftmost bits.
10057 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10058 MVT::i8));
10059 return DAG.getNode(ISD::AND, dl, VT, SRL,
10060 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10061 }
10062
Nadav Rotem43012222011-05-11 08:12:09 +000010063 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
10064 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10065 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10066 R, DAG.getConstant(ShiftAmt, MVT::i32));
10067
10068 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
10069 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10070 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
10071 R, DAG.getConstant(ShiftAmt, MVT::i32));
10072
10073 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
10074 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10075 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10076 R, DAG.getConstant(ShiftAmt, MVT::i32));
10077
10078 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
10079 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10080 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
10081 R, DAG.getConstant(ShiftAmt, MVT::i32));
10082
10083 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
10084 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10085 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
10086 R, DAG.getConstant(ShiftAmt, MVT::i32));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010087
10088 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRA) {
10089 if (ShiftAmt == 7) {
10090 // R s>> 7 === R s< 0
10091 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10092 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10093 }
10094
10095 // R s>> a === ((R u>> a) ^ m) - m
10096 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10097 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10098 MVT::i8));
10099 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10100 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10101 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10102 return Res;
10103 }
Craig Topper46154eb2011-11-11 07:39:23 +000010104
Craig Topper0d86d462011-11-20 00:12:05 +000010105 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10106 if (Op.getOpcode() == ISD::SHL) {
10107 // Make a large shift.
10108 SDValue SHL =
10109 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10110 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
10111 R, DAG.getConstant(ShiftAmt, MVT::i32));
10112 // Zero out the rightmost bits.
10113 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10114 MVT::i8));
10115 return DAG.getNode(ISD::AND, dl, VT, SHL,
10116 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010117 }
Craig Topper0d86d462011-11-20 00:12:05 +000010118 if (Op.getOpcode() == ISD::SRL) {
10119 // Make a large shift.
10120 SDValue SRL =
10121 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10122 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
10123 R, DAG.getConstant(ShiftAmt, MVT::i32));
10124 // Zero out the leftmost bits.
10125 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10126 MVT::i8));
10127 return DAG.getNode(ISD::AND, dl, VT, SRL,
10128 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10129 }
10130 if (Op.getOpcode() == ISD::SRA) {
10131 if (ShiftAmt == 7) {
10132 // R s>> 7 === R s< 0
10133 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10134 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10135 }
10136
10137 // R s>> a === ((R u>> a) ^ m) - m
10138 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10139 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10140 MVT::i8));
10141 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10142 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10143 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10144 return Res;
10145 }
10146 }
Nadav Rotem43012222011-05-11 08:12:09 +000010147 }
10148 }
10149
10150 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010151 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010152 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10153 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10154 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
10155
10156 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010157
Nate Begeman51409212010-07-28 00:21:48 +000010158 std::vector<Constant*> CV(4, CI);
10159 Constant *C = ConstantVector::get(CV);
10160 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10161 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010162 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010163 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010164
10165 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010166 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010167 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10168 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10169 }
Nadav Rotem43012222011-05-11 08:12:09 +000010170 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Lang Hames8b99c1e2011-12-17 01:08:46 +000010171 assert((Subtarget->hasSSE2() || Subtarget->hasAVX()) &&
10172 "Need SSE2 for pslli/pcmpeq.");
10173
Nate Begeman51409212010-07-28 00:21:48 +000010174 // a = a << 5;
10175 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10176 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10177 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
10178
Lang Hames8b99c1e2011-12-17 01:08:46 +000010179 // Turn 'a' into a mask suitable for VSELECT
10180 SDValue VSelM = DAG.getConstant(0x80, VT);
10181 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
10182 OpVSel = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10183 DAG.getConstant(Intrinsic::x86_sse2_pcmpeq_b, MVT::i32),
10184 OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010185
Lang Hames8b99c1e2011-12-17 01:08:46 +000010186 SDValue CM1 = DAG.getConstant(0x0f, VT);
10187 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010188
Lang Hames8b99c1e2011-12-17 01:08:46 +000010189 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10190 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Nate Begeman51409212010-07-28 00:21:48 +000010191 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10192 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10193 DAG.getConstant(4, MVT::i32));
Lang Hames8b99c1e2011-12-17 01:08:46 +000010194 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10195
Nate Begeman51409212010-07-28 00:21:48 +000010196 // a += a
10197 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010198 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
10199 OpVSel = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10200 DAG.getConstant(Intrinsic::x86_sse2_pcmpeq_b, MVT::i32),
10201 OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010202
Lang Hames8b99c1e2011-12-17 01:08:46 +000010203 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10204 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Nate Begeman51409212010-07-28 00:21:48 +000010205 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10206 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10207 DAG.getConstant(2, MVT::i32));
Lang Hames8b99c1e2011-12-17 01:08:46 +000010208 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10209
Nate Begeman51409212010-07-28 00:21:48 +000010210 // a += a
10211 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010212 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
10213 OpVSel = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10214 DAG.getConstant(Intrinsic::x86_sse2_pcmpeq_b, MVT::i32),
10215 OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010216
Lang Hames8b99c1e2011-12-17 01:08:46 +000010217 // return VSELECT(r, r+r, a);
10218 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010219 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010220 return R;
10221 }
Craig Topper46154eb2011-11-11 07:39:23 +000010222
10223 // Decompose 256-bit shifts into smaller 128-bit shifts.
10224 if (VT.getSizeInBits() == 256) {
10225 int NumElems = VT.getVectorNumElements();
10226 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10227 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10228
10229 // Extract the two vectors
10230 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10231 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10232 DAG, dl);
10233
10234 // Recreate the shift amount vectors
10235 SDValue Amt1, Amt2;
10236 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10237 // Constant shift amount
10238 SmallVector<SDValue, 4> Amt1Csts;
10239 SmallVector<SDValue, 4> Amt2Csts;
10240 for (int i = 0; i < NumElems/2; ++i)
10241 Amt1Csts.push_back(Amt->getOperand(i));
10242 for (int i = NumElems/2; i < NumElems; ++i)
10243 Amt2Csts.push_back(Amt->getOperand(i));
10244
10245 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10246 &Amt1Csts[0], NumElems/2);
10247 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10248 &Amt2Csts[0], NumElems/2);
10249 } else {
10250 // Variable shift amount
10251 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10252 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10253 DAG, dl);
10254 }
10255
10256 // Issue new vector shifts for the smaller types
10257 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10258 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10259
10260 // Concatenate the result back
10261 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10262 }
10263
Nate Begeman51409212010-07-28 00:21:48 +000010264 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010265}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010266
Dan Gohmand858e902010-04-17 15:26:15 +000010267SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010268 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10269 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010270 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10271 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010272 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010273 SDValue LHS = N->getOperand(0);
10274 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010275 unsigned BaseOp = 0;
10276 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010277 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010278 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010279 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010280 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010281 // A subtract of one will be selected as a INC. Note that INC doesn't
10282 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010283 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10284 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010285 BaseOp = X86ISD::INC;
10286 Cond = X86::COND_O;
10287 break;
10288 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010289 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010290 Cond = X86::COND_O;
10291 break;
10292 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010293 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010294 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010295 break;
10296 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010297 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10298 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010299 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10300 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010301 BaseOp = X86ISD::DEC;
10302 Cond = X86::COND_O;
10303 break;
10304 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010305 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010306 Cond = X86::COND_O;
10307 break;
10308 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010309 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010310 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010311 break;
10312 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010313 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010314 Cond = X86::COND_O;
10315 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010316 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10317 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10318 MVT::i32);
10319 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010320
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010321 SDValue SetCC =
10322 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10323 DAG.getConstant(X86::COND_O, MVT::i32),
10324 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010325
Dan Gohman6e5fda22011-07-22 18:45:15 +000010326 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010327 }
Bill Wendling74c37652008-12-09 22:08:41 +000010328 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010329
Bill Wendling61edeb52008-12-02 01:06:39 +000010330 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010331 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010332 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010333
Bill Wendling61edeb52008-12-02 01:06:39 +000010334 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010335 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10336 DAG.getConstant(Cond, MVT::i32),
10337 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010338
Dan Gohman6e5fda22011-07-22 18:45:15 +000010339 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010340}
10341
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010342SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10343 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010344 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10345 EVT VT = Op.getValueType();
10346
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010347 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010348 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10349 ExtraVT.getScalarType().getSizeInBits();
10350 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10351
10352 unsigned SHLIntrinsicsID = 0;
10353 unsigned SRAIntrinsicsID = 0;
10354 switch (VT.getSimpleVT().SimpleTy) {
10355 default:
10356 return SDValue();
Craig Toppera124f942011-11-21 01:12:36 +000010357 case MVT::v4i32:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010358 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10359 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10360 break;
Craig Toppera124f942011-11-21 01:12:36 +000010361 case MVT::v8i16:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010362 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10363 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10364 break;
Craig Toppera124f942011-11-21 01:12:36 +000010365 case MVT::v8i32:
10366 case MVT::v16i16:
10367 if (!Subtarget->hasAVX())
10368 return SDValue();
10369 if (!Subtarget->hasAVX2()) {
10370 // needs to be split
10371 int NumElems = VT.getVectorNumElements();
10372 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10373 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10374
10375 // Extract the LHS vectors
10376 SDValue LHS = Op.getOperand(0);
10377 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10378 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10379
10380 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10381 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10382
10383 EVT ExtraEltVT = ExtraVT.getVectorElementType();
10384 int ExtraNumElems = ExtraVT.getVectorNumElements();
10385 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10386 ExtraNumElems/2);
10387 SDValue Extra = DAG.getValueType(ExtraVT);
10388
10389 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10390 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
10391
10392 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10393 }
10394 if (VT == MVT::v8i32) {
10395 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_d;
10396 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_d;
10397 } else {
10398 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_w;
10399 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_w;
10400 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010401 }
10402
10403 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10404 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
Craig Toppera124f942011-11-21 01:12:36 +000010405 Op.getOperand(0), ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010406
Nadav Rotema7934dd2011-10-10 19:31:45 +000010407 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10408 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10409 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010410 }
10411
10412 return SDValue();
10413}
10414
10415
Eric Christopher9a9d2752010-07-22 02:48:34 +000010416SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10417 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010418
Eric Christopher77ed1352011-07-08 00:04:56 +000010419 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10420 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010421 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010422 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010423 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010424 SDValue Ops[] = {
10425 DAG.getRegister(X86::ESP, MVT::i32), // Base
10426 DAG.getTargetConstant(1, MVT::i8), // Scale
10427 DAG.getRegister(0, MVT::i32), // Index
10428 DAG.getTargetConstant(0, MVT::i32), // Disp
10429 DAG.getRegister(0, MVT::i32), // Segment.
10430 Zero,
10431 Chain
10432 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010433 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010434 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10435 array_lengthof(Ops));
10436 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010437 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010438
Eric Christopher9a9d2752010-07-22 02:48:34 +000010439 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010440 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010441 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010442
Chris Lattner132929a2010-08-14 17:26:09 +000010443 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10444 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10445 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10446 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010447
Chris Lattner132929a2010-08-14 17:26:09 +000010448 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10449 if (!Op1 && !Op2 && !Op3 && Op4)
10450 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010451
Chris Lattner132929a2010-08-14 17:26:09 +000010452 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10453 if (Op1 && !Op2 && !Op3 && !Op4)
10454 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010455
10456 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010457 // (MFENCE)>;
10458 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010459}
10460
Eli Friedman14648462011-07-27 22:21:52 +000010461SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10462 SelectionDAG &DAG) const {
10463 DebugLoc dl = Op.getDebugLoc();
10464 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10465 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10466 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10467 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10468
10469 // The only fence that needs an instruction is a sequentially-consistent
10470 // cross-thread fence.
10471 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10472 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10473 // no-sse2). There isn't any reason to disable it if the target processor
10474 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010475 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010476 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10477
10478 SDValue Chain = Op.getOperand(0);
10479 SDValue Zero = DAG.getConstant(0, MVT::i32);
10480 SDValue Ops[] = {
10481 DAG.getRegister(X86::ESP, MVT::i32), // Base
10482 DAG.getTargetConstant(1, MVT::i8), // Scale
10483 DAG.getRegister(0, MVT::i32), // Index
10484 DAG.getTargetConstant(0, MVT::i32), // Disp
10485 DAG.getRegister(0, MVT::i32), // Segment.
10486 Zero,
10487 Chain
10488 };
10489 SDNode *Res =
10490 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10491 array_lengthof(Ops));
10492 return SDValue(Res, 0);
10493 }
10494
10495 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10496 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10497}
10498
10499
Dan Gohmand858e902010-04-17 15:26:15 +000010500SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010501 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010502 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010503 unsigned Reg = 0;
10504 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010505 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010506 default:
10507 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010508 case MVT::i8: Reg = X86::AL; size = 1; break;
10509 case MVT::i16: Reg = X86::AX; size = 2; break;
10510 case MVT::i32: Reg = X86::EAX; size = 4; break;
10511 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010512 assert(Subtarget->is64Bit() && "Node not type legal!");
10513 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010514 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010515 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010516 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010517 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010518 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010519 Op.getOperand(1),
10520 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010521 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010522 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010523 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010524 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10525 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10526 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010527 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010528 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010529 return cpOut;
10530}
10531
Duncan Sands1607f052008-12-01 11:39:25 +000010532SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010533 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010534 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010535 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010536 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010537 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010538 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010539 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10540 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010541 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010542 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10543 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010544 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010545 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010546 rdx.getValue(1)
10547 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010548 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010549}
10550
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010551SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010552 SelectionDAG &DAG) const {
10553 EVT SrcVT = Op.getOperand(0).getValueType();
10554 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010555 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010556 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010557 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010558 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010559 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010560 // i64 <=> MMX conversions are Legal.
10561 if (SrcVT==MVT::i64 && DstVT.isVector())
10562 return Op;
10563 if (DstVT==MVT::i64 && SrcVT.isVector())
10564 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010565 // MMX <=> MMX conversions are Legal.
10566 if (SrcVT.isVector() && DstVT.isVector())
10567 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010568 // All other conversions need to be expanded.
10569 return SDValue();
10570}
Chris Lattner5b856542010-12-20 00:59:46 +000010571
Dan Gohmand858e902010-04-17 15:26:15 +000010572SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010573 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010574 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010575 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010576 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010577 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010578 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010579 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010580 Node->getOperand(0),
10581 Node->getOperand(1), negOp,
10582 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010583 cast<AtomicSDNode>(Node)->getAlignment(),
10584 cast<AtomicSDNode>(Node)->getOrdering(),
10585 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010586}
10587
Eli Friedman327236c2011-08-24 20:50:09 +000010588static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10589 SDNode *Node = Op.getNode();
10590 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010591 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010592
10593 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010594 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10595 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10596 // (The only way to get a 16-byte store is cmpxchg16b)
10597 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10598 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10599 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010600 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10601 cast<AtomicSDNode>(Node)->getMemoryVT(),
10602 Node->getOperand(0),
10603 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010604 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010605 cast<AtomicSDNode>(Node)->getOrdering(),
10606 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010607 return Swap.getValue(1);
10608 }
10609 // Other atomic stores have a simple pattern.
10610 return Op;
10611}
10612
Chris Lattner5b856542010-12-20 00:59:46 +000010613static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10614 EVT VT = Op.getNode()->getValueType(0);
10615
10616 // Let legalize expand this if it isn't a legal type yet.
10617 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10618 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010619
Chris Lattner5b856542010-12-20 00:59:46 +000010620 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010621
Chris Lattner5b856542010-12-20 00:59:46 +000010622 unsigned Opc;
10623 bool ExtraOp = false;
10624 switch (Op.getOpcode()) {
10625 default: assert(0 && "Invalid code");
10626 case ISD::ADDC: Opc = X86ISD::ADD; break;
10627 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10628 case ISD::SUBC: Opc = X86ISD::SUB; break;
10629 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10630 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010631
Chris Lattner5b856542010-12-20 00:59:46 +000010632 if (!ExtraOp)
10633 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10634 Op.getOperand(1));
10635 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10636 Op.getOperand(1), Op.getOperand(2));
10637}
10638
Evan Cheng0db9fe62006-04-25 20:13:52 +000010639/// LowerOperation - Provide custom lowering hooks for some operations.
10640///
Dan Gohmand858e902010-04-17 15:26:15 +000010641SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010642 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010643 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010644 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010645 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010646 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010647 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10648 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010649 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010650 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010651 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010652 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10653 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10654 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010655 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010656 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010657 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10658 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10659 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010660 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010661 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010662 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010663 case ISD::SHL_PARTS:
10664 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010665 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010666 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010667 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010668 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010669 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010670 case ISD::FABS: return LowerFABS(Op, DAG);
10671 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010672 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010673 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010674 case ISD::SETCC: return LowerSETCC(Op, DAG);
10675 case ISD::SELECT: return LowerSELECT(Op, DAG);
10676 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010677 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010678 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010679 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010680 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010681 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010682 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10683 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010684 case ISD::FRAME_TO_ARGS_OFFSET:
10685 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010686 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010687 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010688 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10689 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010690 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010691 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10692 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010693 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010694 case ISD::SRA:
10695 case ISD::SRL:
10696 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010697 case ISD::SADDO:
10698 case ISD::UADDO:
10699 case ISD::SSUBO:
10700 case ISD::USUBO:
10701 case ISD::SMULO:
10702 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010703 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010704 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010705 case ISD::ADDC:
10706 case ISD::ADDE:
10707 case ISD::SUBC:
10708 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010709 case ISD::ADD: return LowerADD(Op, DAG);
10710 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010711 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010712}
10713
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010714static void ReplaceATOMIC_LOAD(SDNode *Node,
10715 SmallVectorImpl<SDValue> &Results,
10716 SelectionDAG &DAG) {
10717 DebugLoc dl = Node->getDebugLoc();
10718 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10719
10720 // Convert wide load -> cmpxchg8b/cmpxchg16b
10721 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10722 // (The only way to get a 16-byte load is cmpxchg16b)
10723 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010724 SDValue Zero = DAG.getConstant(0, VT);
10725 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010726 Node->getOperand(0),
10727 Node->getOperand(1), Zero, Zero,
10728 cast<AtomicSDNode>(Node)->getMemOperand(),
10729 cast<AtomicSDNode>(Node)->getOrdering(),
10730 cast<AtomicSDNode>(Node)->getSynchScope());
10731 Results.push_back(Swap.getValue(0));
10732 Results.push_back(Swap.getValue(1));
10733}
10734
Duncan Sands1607f052008-12-01 11:39:25 +000010735void X86TargetLowering::
10736ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010737 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010738 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000010739 assert (Node->getValueType(0) == MVT::i64 &&
10740 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010741
10742 SDValue Chain = Node->getOperand(0);
10743 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010744 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010745 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010746 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010747 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010748 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010749 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010750 SDValue Result =
10751 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10752 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010753 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010754 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010755 Results.push_back(Result.getValue(2));
10756}
10757
Duncan Sands126d9072008-07-04 11:47:58 +000010758/// ReplaceNodeResults - Replace a node with an illegal result type
10759/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010760void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10761 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010762 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010763 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010764 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010765 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010766 assert(false && "Do not know how to custom type legalize this operation!");
10767 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010768 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010769 case ISD::ADDC:
10770 case ISD::ADDE:
10771 case ISD::SUBC:
10772 case ISD::SUBE:
10773 // We don't want to expand or promote these.
10774 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010775 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010776 std::pair<SDValue,SDValue> Vals =
10777 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010778 SDValue FIST = Vals.first, StackSlot = Vals.second;
10779 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010780 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010781 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010782 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010783 MachinePointerInfo(),
10784 false, false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010785 }
10786 return;
10787 }
10788 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010789 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010790 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010791 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010792 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010793 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010794 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010795 eax.getValue(2));
10796 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10797 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010798 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010799 Results.push_back(edx.getValue(1));
10800 return;
10801 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010802 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010803 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010804 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010805 bool Regs64bit = T == MVT::i128;
10806 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010807 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010808 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10809 DAG.getConstant(0, HalfT));
10810 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10811 DAG.getConstant(1, HalfT));
10812 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10813 Regs64bit ? X86::RAX : X86::EAX,
10814 cpInL, SDValue());
10815 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10816 Regs64bit ? X86::RDX : X86::EDX,
10817 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010818 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010819 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10820 DAG.getConstant(0, HalfT));
10821 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10822 DAG.getConstant(1, HalfT));
10823 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10824 Regs64bit ? X86::RBX : X86::EBX,
10825 swapInL, cpInH.getValue(1));
10826 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10827 Regs64bit ? X86::RCX : X86::ECX,
10828 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010829 SDValue Ops[] = { swapInH.getValue(0),
10830 N->getOperand(1),
10831 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010832 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010833 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010834 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10835 X86ISD::LCMPXCHG8_DAG;
10836 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010837 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010838 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10839 Regs64bit ? X86::RAX : X86::EAX,
10840 HalfT, Result.getValue(1));
10841 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10842 Regs64bit ? X86::RDX : X86::EDX,
10843 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010844 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010845 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010846 Results.push_back(cpOutH.getValue(1));
10847 return;
10848 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010849 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010850 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10851 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010852 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010853 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10854 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010855 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010856 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10857 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010858 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010859 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10860 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010861 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010862 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10863 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010864 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010865 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10866 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010867 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010868 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10869 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010870 case ISD::ATOMIC_LOAD:
10871 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010872 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010873}
10874
Evan Cheng72261582005-12-20 06:22:03 +000010875const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10876 switch (Opcode) {
10877 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010878 case X86ISD::BSF: return "X86ISD::BSF";
10879 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010880 case X86ISD::SHLD: return "X86ISD::SHLD";
10881 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010882 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010883 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010884 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010885 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010886 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010887 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010888 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10889 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10890 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010891 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010892 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010893 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010894 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010895 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010896 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010897 case X86ISD::COMI: return "X86ISD::COMI";
10898 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010899 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010900 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010901 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10902 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010903 case X86ISD::CMOV: return "X86ISD::CMOV";
10904 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010905 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010906 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10907 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010908 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010909 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010910 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010911 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010912 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010913 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10914 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010915 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010916 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010917 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000010918 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000010919 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Craig Topperfe033152011-12-06 09:31:36 +000010920 case X86ISD::HADD: return "X86ISD::HADD";
10921 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000010922 case X86ISD::FHADD: return "X86ISD::FHADD";
10923 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000010924 case X86ISD::FMAX: return "X86ISD::FMAX";
10925 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010926 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10927 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010928 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010929 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010930 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010931 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010932 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010933 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10934 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010935 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10936 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10937 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10938 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10939 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10940 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010941 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10942 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010943 case X86ISD::VSHL: return "X86ISD::VSHL";
10944 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010945 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10946 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10947 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10948 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10949 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10950 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10951 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10952 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10953 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10954 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010955 case X86ISD::ADD: return "X86ISD::ADD";
10956 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010957 case X86ISD::ADC: return "X86ISD::ADC";
10958 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010959 case X86ISD::SMUL: return "X86ISD::SMUL";
10960 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010961 case X86ISD::INC: return "X86ISD::INC";
10962 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010963 case X86ISD::OR: return "X86ISD::OR";
10964 case X86ISD::XOR: return "X86ISD::XOR";
10965 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000010966 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000010967 case X86ISD::BLSI: return "X86ISD::BLSI";
10968 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
10969 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000010970 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010971 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010972 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010973 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10974 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10975 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10976 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10977 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10978 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10979 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10980 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10981 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010982 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010983 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010984 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010985 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10986 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010987 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10988 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10989 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10990 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10991 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10992 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10993 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000010994 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
10995 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010996 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000010997 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000010998 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010999 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011000 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011001 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011002 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011003 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000011004 }
11005}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011006
Chris Lattnerc9addb72007-03-30 23:15:24 +000011007// isLegalAddressingMode - Return true if the addressing mode represented
11008// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011009bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011010 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011011 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011012 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011013 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011014
Chris Lattnerc9addb72007-03-30 23:15:24 +000011015 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011016 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011017 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011018
Chris Lattnerc9addb72007-03-30 23:15:24 +000011019 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011020 unsigned GVFlags =
11021 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011022
Chris Lattnerdfed4132009-07-10 07:38:24 +000011023 // If a reference to this global requires an extra load, we can't fold it.
11024 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011025 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011026
Chris Lattnerdfed4132009-07-10 07:38:24 +000011027 // If BaseGV requires a register for the PIC base, we cannot also have a
11028 // BaseReg specified.
11029 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011030 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011031
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011032 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011033 if ((M != CodeModel::Small || R != Reloc::Static) &&
11034 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011035 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011036 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011037
Chris Lattnerc9addb72007-03-30 23:15:24 +000011038 switch (AM.Scale) {
11039 case 0:
11040 case 1:
11041 case 2:
11042 case 4:
11043 case 8:
11044 // These scales always work.
11045 break;
11046 case 3:
11047 case 5:
11048 case 9:
11049 // These scales are formed with basereg+scalereg. Only accept if there is
11050 // no basereg yet.
11051 if (AM.HasBaseReg)
11052 return false;
11053 break;
11054 default: // Other stuff never works.
11055 return false;
11056 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011057
Chris Lattnerc9addb72007-03-30 23:15:24 +000011058 return true;
11059}
11060
11061
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011062bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011063 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011064 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011065 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11066 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011067 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011068 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011069 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011070}
11071
Owen Andersone50ed302009-08-10 22:56:29 +000011072bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011073 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011074 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011075 unsigned NumBits1 = VT1.getSizeInBits();
11076 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011077 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011078 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011079 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011080}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011081
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011082bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011083 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011084 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011085}
11086
Owen Andersone50ed302009-08-10 22:56:29 +000011087bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011088 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011089 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011090}
11091
Owen Andersone50ed302009-08-10 22:56:29 +000011092bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011093 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011094 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011095}
11096
Evan Cheng60c07e12006-07-05 22:17:51 +000011097/// isShuffleMaskLegal - Targets can use this to indicate that they only
11098/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11099/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11100/// are assumed to be legal.
11101bool
Eric Christopherfd179292009-08-27 18:07:15 +000011102X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011103 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011104 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011105 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011106 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011107
Nate Begemana09008b2009-10-19 02:17:23 +000011108 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011109 return (VT.getVectorNumElements() == 2 ||
11110 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11111 isMOVLMask(M, VT) ||
11112 isSHUFPMask(M, VT) ||
11113 isPSHUFDMask(M, VT) ||
11114 isPSHUFHWMask(M, VT) ||
11115 isPSHUFLWMask(M, VT) ||
Craig Topperc0d82852011-11-22 00:44:41 +000011116 isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX()) ||
Craig Topper6347e862011-11-21 06:57:39 +000011117 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11118 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011119 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11120 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011121}
11122
Dan Gohman7d8143f2008-04-09 20:09:42 +000011123bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011124X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011125 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011126 unsigned NumElts = VT.getVectorNumElements();
11127 // FIXME: This collection of masks seems suspect.
11128 if (NumElts == 2)
11129 return true;
11130 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11131 return (isMOVLMask(Mask, VT) ||
11132 isCommutedMOVLMask(Mask, VT, true) ||
11133 isSHUFPMask(Mask, VT) ||
Craig Topper1ff73d72011-12-06 04:59:07 +000011134 isSHUFPMask(Mask, VT, /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011135 }
11136 return false;
11137}
11138
11139//===----------------------------------------------------------------------===//
11140// X86 Scheduler Hooks
11141//===----------------------------------------------------------------------===//
11142
Mon P Wang63307c32008-05-05 19:05:59 +000011143// private utility function
11144MachineBasicBlock *
11145X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11146 MachineBasicBlock *MBB,
11147 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011148 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011149 unsigned LoadOpc,
11150 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011151 unsigned notOpc,
11152 unsigned EAXreg,
11153 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011154 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011155 // For the atomic bitwise operator, we generate
11156 // thisMBB:
11157 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011158 // ld t1 = [bitinstr.addr]
11159 // op t2 = t1, [bitinstr.val]
11160 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011161 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11162 // bz newMBB
11163 // fallthrough -->nextMBB
11164 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11165 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011166 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011167 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011168
Mon P Wang63307c32008-05-05 19:05:59 +000011169 /// First build the CFG
11170 MachineFunction *F = MBB->getParent();
11171 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011172 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11173 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11174 F->insert(MBBIter, newMBB);
11175 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011176
Dan Gohman14152b42010-07-06 20:24:04 +000011177 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11178 nextMBB->splice(nextMBB->begin(), thisMBB,
11179 llvm::next(MachineBasicBlock::iterator(bInstr)),
11180 thisMBB->end());
11181 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011182
Mon P Wang63307c32008-05-05 19:05:59 +000011183 // Update thisMBB to fall through to newMBB
11184 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011185
Mon P Wang63307c32008-05-05 19:05:59 +000011186 // newMBB jumps to itself and fall through to nextMBB
11187 newMBB->addSuccessor(nextMBB);
11188 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011189
Mon P Wang63307c32008-05-05 19:05:59 +000011190 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011191 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011192 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011193 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011194 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011195 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011196 int numArgs = bInstr->getNumOperands() - 1;
11197 for (int i=0; i < numArgs; ++i)
11198 argOpers[i] = &bInstr->getOperand(i+1);
11199
11200 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011201 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011202 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011203
Dale Johannesen140be2d2008-08-19 18:47:28 +000011204 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011205 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011206 for (int i=0; i <= lastAddrIndx; ++i)
11207 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011208
Dale Johannesen140be2d2008-08-19 18:47:28 +000011209 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011210 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011211 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011212 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011213 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011214 tt = t1;
11215
Dale Johannesen140be2d2008-08-19 18:47:28 +000011216 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011217 assert((argOpers[valArgIndx]->isReg() ||
11218 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011219 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011220 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011221 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011222 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011223 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011224 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011225 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011226
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011227 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011228 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011229
Dale Johannesene4d209d2009-02-03 20:21:25 +000011230 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011231 for (int i=0; i <= lastAddrIndx; ++i)
11232 (*MIB).addOperand(*argOpers[i]);
11233 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011234 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011235 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11236 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011237
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011238 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011239 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011240
Mon P Wang63307c32008-05-05 19:05:59 +000011241 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011242 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011243
Dan Gohman14152b42010-07-06 20:24:04 +000011244 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011245 return nextMBB;
11246}
11247
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011248// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011249MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011250X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11251 MachineBasicBlock *MBB,
11252 unsigned regOpcL,
11253 unsigned regOpcH,
11254 unsigned immOpcL,
11255 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011256 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011257 // For the atomic bitwise operator, we generate
11258 // thisMBB (instructions are in pairs, except cmpxchg8b)
11259 // ld t1,t2 = [bitinstr.addr]
11260 // newMBB:
11261 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11262 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011263 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011264 // mov ECX, EBX <- t5, t6
11265 // mov EAX, EDX <- t1, t2
11266 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11267 // mov t3, t4 <- EAX, EDX
11268 // bz newMBB
11269 // result in out1, out2
11270 // fallthrough -->nextMBB
11271
11272 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11273 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011274 const unsigned NotOpc = X86::NOT32r;
11275 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11276 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11277 MachineFunction::iterator MBBIter = MBB;
11278 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011279
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011280 /// First build the CFG
11281 MachineFunction *F = MBB->getParent();
11282 MachineBasicBlock *thisMBB = MBB;
11283 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11284 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11285 F->insert(MBBIter, newMBB);
11286 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011287
Dan Gohman14152b42010-07-06 20:24:04 +000011288 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11289 nextMBB->splice(nextMBB->begin(), thisMBB,
11290 llvm::next(MachineBasicBlock::iterator(bInstr)),
11291 thisMBB->end());
11292 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011293
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011294 // Update thisMBB to fall through to newMBB
11295 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011296
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011297 // newMBB jumps to itself and fall through to nextMBB
11298 newMBB->addSuccessor(nextMBB);
11299 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011300
Dale Johannesene4d209d2009-02-03 20:21:25 +000011301 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011302 // Insert instructions into newMBB based on incoming instruction
11303 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011304 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011305 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011306 MachineOperand& dest1Oper = bInstr->getOperand(0);
11307 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011308 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11309 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011310 argOpers[i] = &bInstr->getOperand(i+2);
11311
Dan Gohman71ea4e52010-05-14 21:01:44 +000011312 // We use some of the operands multiple times, so conservatively just
11313 // clear any kill flags that might be present.
11314 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11315 argOpers[i]->setIsKill(false);
11316 }
11317
Evan Chengad5b52f2010-01-08 19:14:57 +000011318 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011319 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011320
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011321 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011322 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011323 for (int i=0; i <= lastAddrIndx; ++i)
11324 (*MIB).addOperand(*argOpers[i]);
11325 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011326 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011327 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011328 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011329 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011330 MachineOperand newOp3 = *(argOpers[3]);
11331 if (newOp3.isImm())
11332 newOp3.setImm(newOp3.getImm()+4);
11333 else
11334 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011335 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011336 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011337
11338 // t3/4 are defined later, at the bottom of the loop
11339 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11340 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011341 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011342 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011343 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011344 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11345
Evan Cheng306b4ca2010-01-08 23:41:50 +000011346 // The subsequent operations should be using the destination registers of
11347 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011348 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011349 t1 = F->getRegInfo().createVirtualRegister(RC);
11350 t2 = F->getRegInfo().createVirtualRegister(RC);
11351 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11352 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011353 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011354 t1 = dest1Oper.getReg();
11355 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011356 }
11357
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011358 int valArgIndx = lastAddrIndx + 1;
11359 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011360 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011361 "invalid operand");
11362 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11363 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011364 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011365 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011366 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011367 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011368 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011369 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011370 (*MIB).addOperand(*argOpers[valArgIndx]);
11371 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011372 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011373 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011374 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011375 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011376 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011377 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011378 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011379 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011380 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011381 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011382
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011383 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011384 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011385 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011386 MIB.addReg(t2);
11387
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011388 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011389 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011390 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011391 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011392
Dale Johannesene4d209d2009-02-03 20:21:25 +000011393 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011394 for (int i=0; i <= lastAddrIndx; ++i)
11395 (*MIB).addOperand(*argOpers[i]);
11396
11397 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011398 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11399 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011400
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011401 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011402 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011403 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011404 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011405
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011406 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011407 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011408
Dan Gohman14152b42010-07-06 20:24:04 +000011409 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011410 return nextMBB;
11411}
11412
11413// private utility function
11414MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011415X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11416 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011417 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011418 // For the atomic min/max operator, we generate
11419 // thisMBB:
11420 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011421 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011422 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011423 // cmp t1, t2
11424 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011425 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011426 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11427 // bz newMBB
11428 // fallthrough -->nextMBB
11429 //
11430 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11431 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011432 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011433 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011434
Mon P Wang63307c32008-05-05 19:05:59 +000011435 /// First build the CFG
11436 MachineFunction *F = MBB->getParent();
11437 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011438 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11439 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11440 F->insert(MBBIter, newMBB);
11441 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011442
Dan Gohman14152b42010-07-06 20:24:04 +000011443 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11444 nextMBB->splice(nextMBB->begin(), thisMBB,
11445 llvm::next(MachineBasicBlock::iterator(mInstr)),
11446 thisMBB->end());
11447 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011448
Mon P Wang63307c32008-05-05 19:05:59 +000011449 // Update thisMBB to fall through to newMBB
11450 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011451
Mon P Wang63307c32008-05-05 19:05:59 +000011452 // newMBB jumps to newMBB and fall through to nextMBB
11453 newMBB->addSuccessor(nextMBB);
11454 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011455
Dale Johannesene4d209d2009-02-03 20:21:25 +000011456 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011457 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011458 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011459 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011460 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011461 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011462 int numArgs = mInstr->getNumOperands() - 1;
11463 for (int i=0; i < numArgs; ++i)
11464 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011465
Mon P Wang63307c32008-05-05 19:05:59 +000011466 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011467 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011468 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011469
Mon P Wangab3e7472008-05-05 22:56:23 +000011470 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011471 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011472 for (int i=0; i <= lastAddrIndx; ++i)
11473 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011474
Mon P Wang63307c32008-05-05 19:05:59 +000011475 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011476 assert((argOpers[valArgIndx]->isReg() ||
11477 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011478 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011479
11480 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011481 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011482 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011483 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011484 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011485 (*MIB).addOperand(*argOpers[valArgIndx]);
11486
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011487 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011488 MIB.addReg(t1);
11489
Dale Johannesene4d209d2009-02-03 20:21:25 +000011490 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011491 MIB.addReg(t1);
11492 MIB.addReg(t2);
11493
11494 // Generate movc
11495 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011496 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011497 MIB.addReg(t2);
11498 MIB.addReg(t1);
11499
11500 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011501 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011502 for (int i=0; i <= lastAddrIndx; ++i)
11503 (*MIB).addOperand(*argOpers[i]);
11504 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011505 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011506 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11507 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011508
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011509 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011510 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011511
Mon P Wang63307c32008-05-05 19:05:59 +000011512 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011513 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011514
Dan Gohman14152b42010-07-06 20:24:04 +000011515 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011516 return nextMBB;
11517}
11518
Eric Christopherf83a5de2009-08-27 18:08:16 +000011519// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011520// or XMM0_V32I8 in AVX all of this code can be replaced with that
11521// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011522MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011523X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011524 unsigned numArgs, bool memArg) const {
Craig Topperc0d82852011-11-22 00:44:41 +000011525 assert(Subtarget->hasSSE42orAVX() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011526 "Target must have SSE4.2 or AVX features enabled");
11527
Eric Christopherb120ab42009-08-18 22:50:32 +000011528 DebugLoc dl = MI->getDebugLoc();
11529 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011530 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011531 if (!Subtarget->hasAVX()) {
11532 if (memArg)
11533 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11534 else
11535 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11536 } else {
11537 if (memArg)
11538 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11539 else
11540 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11541 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011542
Eric Christopher41c902f2010-11-30 08:20:21 +000011543 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011544 for (unsigned i = 0; i < numArgs; ++i) {
11545 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011546 if (!(Op.isReg() && Op.isImplicit()))
11547 MIB.addOperand(Op);
11548 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011549 BuildMI(*BB, MI, dl,
11550 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11551 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011552 .addReg(X86::XMM0);
11553
Dan Gohman14152b42010-07-06 20:24:04 +000011554 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011555 return BB;
11556}
11557
11558MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011559X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011560 DebugLoc dl = MI->getDebugLoc();
11561 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011562
Eric Christopher228232b2010-11-30 07:20:12 +000011563 // Address into RAX/EAX, other two args into ECX, EDX.
11564 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11565 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11566 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11567 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011568 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011569
Eric Christopher228232b2010-11-30 07:20:12 +000011570 unsigned ValOps = X86::AddrNumOperands;
11571 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11572 .addReg(MI->getOperand(ValOps).getReg());
11573 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11574 .addReg(MI->getOperand(ValOps+1).getReg());
11575
11576 // The instruction doesn't actually take any operands though.
11577 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011578
Eric Christopher228232b2010-11-30 07:20:12 +000011579 MI->eraseFromParent(); // The pseudo is gone now.
11580 return BB;
11581}
11582
11583MachineBasicBlock *
11584X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011585 DebugLoc dl = MI->getDebugLoc();
11586 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011587
Eric Christopher228232b2010-11-30 07:20:12 +000011588 // First arg in ECX, the second in EAX.
11589 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11590 .addReg(MI->getOperand(0).getReg());
11591 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11592 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011593
Eric Christopher228232b2010-11-30 07:20:12 +000011594 // The instruction doesn't actually take any operands though.
11595 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011596
Eric Christopher228232b2010-11-30 07:20:12 +000011597 MI->eraseFromParent(); // The pseudo is gone now.
11598 return BB;
11599}
11600
11601MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011602X86TargetLowering::EmitVAARG64WithCustomInserter(
11603 MachineInstr *MI,
11604 MachineBasicBlock *MBB) const {
11605 // Emit va_arg instruction on X86-64.
11606
11607 // Operands to this pseudo-instruction:
11608 // 0 ) Output : destination address (reg)
11609 // 1-5) Input : va_list address (addr, i64mem)
11610 // 6 ) ArgSize : Size (in bytes) of vararg type
11611 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11612 // 8 ) Align : Alignment of type
11613 // 9 ) EFLAGS (implicit-def)
11614
11615 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11616 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11617
11618 unsigned DestReg = MI->getOperand(0).getReg();
11619 MachineOperand &Base = MI->getOperand(1);
11620 MachineOperand &Scale = MI->getOperand(2);
11621 MachineOperand &Index = MI->getOperand(3);
11622 MachineOperand &Disp = MI->getOperand(4);
11623 MachineOperand &Segment = MI->getOperand(5);
11624 unsigned ArgSize = MI->getOperand(6).getImm();
11625 unsigned ArgMode = MI->getOperand(7).getImm();
11626 unsigned Align = MI->getOperand(8).getImm();
11627
11628 // Memory Reference
11629 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11630 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11631 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11632
11633 // Machine Information
11634 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11635 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11636 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11637 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11638 DebugLoc DL = MI->getDebugLoc();
11639
11640 // struct va_list {
11641 // i32 gp_offset
11642 // i32 fp_offset
11643 // i64 overflow_area (address)
11644 // i64 reg_save_area (address)
11645 // }
11646 // sizeof(va_list) = 24
11647 // alignment(va_list) = 8
11648
11649 unsigned TotalNumIntRegs = 6;
11650 unsigned TotalNumXMMRegs = 8;
11651 bool UseGPOffset = (ArgMode == 1);
11652 bool UseFPOffset = (ArgMode == 2);
11653 unsigned MaxOffset = TotalNumIntRegs * 8 +
11654 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11655
11656 /* Align ArgSize to a multiple of 8 */
11657 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11658 bool NeedsAlign = (Align > 8);
11659
11660 MachineBasicBlock *thisMBB = MBB;
11661 MachineBasicBlock *overflowMBB;
11662 MachineBasicBlock *offsetMBB;
11663 MachineBasicBlock *endMBB;
11664
11665 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11666 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11667 unsigned OffsetReg = 0;
11668
11669 if (!UseGPOffset && !UseFPOffset) {
11670 // If we only pull from the overflow region, we don't create a branch.
11671 // We don't need to alter control flow.
11672 OffsetDestReg = 0; // unused
11673 OverflowDestReg = DestReg;
11674
11675 offsetMBB = NULL;
11676 overflowMBB = thisMBB;
11677 endMBB = thisMBB;
11678 } else {
11679 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11680 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11681 // If not, pull from overflow_area. (branch to overflowMBB)
11682 //
11683 // thisMBB
11684 // | .
11685 // | .
11686 // offsetMBB overflowMBB
11687 // | .
11688 // | .
11689 // endMBB
11690
11691 // Registers for the PHI in endMBB
11692 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11693 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11694
11695 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11696 MachineFunction *MF = MBB->getParent();
11697 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11698 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11699 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11700
11701 MachineFunction::iterator MBBIter = MBB;
11702 ++MBBIter;
11703
11704 // Insert the new basic blocks
11705 MF->insert(MBBIter, offsetMBB);
11706 MF->insert(MBBIter, overflowMBB);
11707 MF->insert(MBBIter, endMBB);
11708
11709 // Transfer the remainder of MBB and its successor edges to endMBB.
11710 endMBB->splice(endMBB->begin(), thisMBB,
11711 llvm::next(MachineBasicBlock::iterator(MI)),
11712 thisMBB->end());
11713 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11714
11715 // Make offsetMBB and overflowMBB successors of thisMBB
11716 thisMBB->addSuccessor(offsetMBB);
11717 thisMBB->addSuccessor(overflowMBB);
11718
11719 // endMBB is a successor of both offsetMBB and overflowMBB
11720 offsetMBB->addSuccessor(endMBB);
11721 overflowMBB->addSuccessor(endMBB);
11722
11723 // Load the offset value into a register
11724 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11725 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11726 .addOperand(Base)
11727 .addOperand(Scale)
11728 .addOperand(Index)
11729 .addDisp(Disp, UseFPOffset ? 4 : 0)
11730 .addOperand(Segment)
11731 .setMemRefs(MMOBegin, MMOEnd);
11732
11733 // Check if there is enough room left to pull this argument.
11734 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11735 .addReg(OffsetReg)
11736 .addImm(MaxOffset + 8 - ArgSizeA8);
11737
11738 // Branch to "overflowMBB" if offset >= max
11739 // Fall through to "offsetMBB" otherwise
11740 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11741 .addMBB(overflowMBB);
11742 }
11743
11744 // In offsetMBB, emit code to use the reg_save_area.
11745 if (offsetMBB) {
11746 assert(OffsetReg != 0);
11747
11748 // Read the reg_save_area address.
11749 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11750 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11751 .addOperand(Base)
11752 .addOperand(Scale)
11753 .addOperand(Index)
11754 .addDisp(Disp, 16)
11755 .addOperand(Segment)
11756 .setMemRefs(MMOBegin, MMOEnd);
11757
11758 // Zero-extend the offset
11759 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11760 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11761 .addImm(0)
11762 .addReg(OffsetReg)
11763 .addImm(X86::sub_32bit);
11764
11765 // Add the offset to the reg_save_area to get the final address.
11766 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11767 .addReg(OffsetReg64)
11768 .addReg(RegSaveReg);
11769
11770 // Compute the offset for the next argument
11771 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11772 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11773 .addReg(OffsetReg)
11774 .addImm(UseFPOffset ? 16 : 8);
11775
11776 // Store it back into the va_list.
11777 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11778 .addOperand(Base)
11779 .addOperand(Scale)
11780 .addOperand(Index)
11781 .addDisp(Disp, UseFPOffset ? 4 : 0)
11782 .addOperand(Segment)
11783 .addReg(NextOffsetReg)
11784 .setMemRefs(MMOBegin, MMOEnd);
11785
11786 // Jump to endMBB
11787 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11788 .addMBB(endMBB);
11789 }
11790
11791 //
11792 // Emit code to use overflow area
11793 //
11794
11795 // Load the overflow_area address into a register.
11796 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11797 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11798 .addOperand(Base)
11799 .addOperand(Scale)
11800 .addOperand(Index)
11801 .addDisp(Disp, 8)
11802 .addOperand(Segment)
11803 .setMemRefs(MMOBegin, MMOEnd);
11804
11805 // If we need to align it, do so. Otherwise, just copy the address
11806 // to OverflowDestReg.
11807 if (NeedsAlign) {
11808 // Align the overflow address
11809 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11810 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11811
11812 // aligned_addr = (addr + (align-1)) & ~(align-1)
11813 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11814 .addReg(OverflowAddrReg)
11815 .addImm(Align-1);
11816
11817 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11818 .addReg(TmpReg)
11819 .addImm(~(uint64_t)(Align-1));
11820 } else {
11821 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11822 .addReg(OverflowAddrReg);
11823 }
11824
11825 // Compute the next overflow address after this argument.
11826 // (the overflow address should be kept 8-byte aligned)
11827 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11828 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11829 .addReg(OverflowDestReg)
11830 .addImm(ArgSizeA8);
11831
11832 // Store the new overflow address.
11833 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11834 .addOperand(Base)
11835 .addOperand(Scale)
11836 .addOperand(Index)
11837 .addDisp(Disp, 8)
11838 .addOperand(Segment)
11839 .addReg(NextAddrReg)
11840 .setMemRefs(MMOBegin, MMOEnd);
11841
11842 // If we branched, emit the PHI to the front of endMBB.
11843 if (offsetMBB) {
11844 BuildMI(*endMBB, endMBB->begin(), DL,
11845 TII->get(X86::PHI), DestReg)
11846 .addReg(OffsetDestReg).addMBB(offsetMBB)
11847 .addReg(OverflowDestReg).addMBB(overflowMBB);
11848 }
11849
11850 // Erase the pseudo instruction
11851 MI->eraseFromParent();
11852
11853 return endMBB;
11854}
11855
11856MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011857X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11858 MachineInstr *MI,
11859 MachineBasicBlock *MBB) const {
11860 // Emit code to save XMM registers to the stack. The ABI says that the
11861 // number of registers to save is given in %al, so it's theoretically
11862 // possible to do an indirect jump trick to avoid saving all of them,
11863 // however this code takes a simpler approach and just executes all
11864 // of the stores if %al is non-zero. It's less code, and it's probably
11865 // easier on the hardware branch predictor, and stores aren't all that
11866 // expensive anyway.
11867
11868 // Create the new basic blocks. One block contains all the XMM stores,
11869 // and one block is the final destination regardless of whether any
11870 // stores were performed.
11871 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11872 MachineFunction *F = MBB->getParent();
11873 MachineFunction::iterator MBBIter = MBB;
11874 ++MBBIter;
11875 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11876 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11877 F->insert(MBBIter, XMMSaveMBB);
11878 F->insert(MBBIter, EndMBB);
11879
Dan Gohman14152b42010-07-06 20:24:04 +000011880 // Transfer the remainder of MBB and its successor edges to EndMBB.
11881 EndMBB->splice(EndMBB->begin(), MBB,
11882 llvm::next(MachineBasicBlock::iterator(MI)),
11883 MBB->end());
11884 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11885
Dan Gohmand6708ea2009-08-15 01:38:56 +000011886 // The original block will now fall through to the XMM save block.
11887 MBB->addSuccessor(XMMSaveMBB);
11888 // The XMMSaveMBB will fall through to the end block.
11889 XMMSaveMBB->addSuccessor(EndMBB);
11890
11891 // Now add the instructions.
11892 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11893 DebugLoc DL = MI->getDebugLoc();
11894
11895 unsigned CountReg = MI->getOperand(0).getReg();
11896 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11897 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11898
11899 if (!Subtarget->isTargetWin64()) {
11900 // If %al is 0, branch around the XMM save block.
11901 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011902 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011903 MBB->addSuccessor(EndMBB);
11904 }
11905
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011906 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011907 // In the XMM save block, save all the XMM argument registers.
11908 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11909 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011910 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011911 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011912 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011913 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011914 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011915 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011916 .addFrameIndex(RegSaveFrameIndex)
11917 .addImm(/*Scale=*/1)
11918 .addReg(/*IndexReg=*/0)
11919 .addImm(/*Disp=*/Offset)
11920 .addReg(/*Segment=*/0)
11921 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011922 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011923 }
11924
Dan Gohman14152b42010-07-06 20:24:04 +000011925 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011926
11927 return EndMBB;
11928}
Mon P Wang63307c32008-05-05 19:05:59 +000011929
Evan Cheng60c07e12006-07-05 22:17:51 +000011930MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011931X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011932 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011933 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11934 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011935
Chris Lattner52600972009-09-02 05:57:00 +000011936 // To "insert" a SELECT_CC instruction, we actually have to insert the
11937 // diamond control-flow pattern. The incoming instruction knows the
11938 // destination vreg to set, the condition code register to branch on, the
11939 // true/false values to select between, and a branch opcode to use.
11940 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11941 MachineFunction::iterator It = BB;
11942 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011943
Chris Lattner52600972009-09-02 05:57:00 +000011944 // thisMBB:
11945 // ...
11946 // TrueVal = ...
11947 // cmpTY ccX, r1, r2
11948 // bCC copy1MBB
11949 // fallthrough --> copy0MBB
11950 MachineBasicBlock *thisMBB = BB;
11951 MachineFunction *F = BB->getParent();
11952 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11953 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011954 F->insert(It, copy0MBB);
11955 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011956
Bill Wendling730c07e2010-06-25 20:48:10 +000011957 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11958 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011959 if (!MI->killsRegister(X86::EFLAGS)) {
11960 copy0MBB->addLiveIn(X86::EFLAGS);
11961 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011962 }
11963
Dan Gohman14152b42010-07-06 20:24:04 +000011964 // Transfer the remainder of BB and its successor edges to sinkMBB.
11965 sinkMBB->splice(sinkMBB->begin(), BB,
11966 llvm::next(MachineBasicBlock::iterator(MI)),
11967 BB->end());
11968 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11969
11970 // Add the true and fallthrough blocks as its successors.
11971 BB->addSuccessor(copy0MBB);
11972 BB->addSuccessor(sinkMBB);
11973
11974 // Create the conditional branch instruction.
11975 unsigned Opc =
11976 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11977 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11978
Chris Lattner52600972009-09-02 05:57:00 +000011979 // copy0MBB:
11980 // %FalseValue = ...
11981 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011982 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011983
Chris Lattner52600972009-09-02 05:57:00 +000011984 // sinkMBB:
11985 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11986 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011987 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11988 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011989 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11990 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11991
Dan Gohman14152b42010-07-06 20:24:04 +000011992 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011993 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011994}
11995
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011996MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011997X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11998 bool Is64Bit) const {
11999 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12000 DebugLoc DL = MI->getDebugLoc();
12001 MachineFunction *MF = BB->getParent();
12002 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12003
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012004 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012005
12006 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12007 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12008
12009 // BB:
12010 // ... [Till the alloca]
12011 // If stacklet is not large enough, jump to mallocMBB
12012 //
12013 // bumpMBB:
12014 // Allocate by subtracting from RSP
12015 // Jump to continueMBB
12016 //
12017 // mallocMBB:
12018 // Allocate by call to runtime
12019 //
12020 // continueMBB:
12021 // ...
12022 // [rest of original BB]
12023 //
12024
12025 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12026 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12027 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12028
12029 MachineRegisterInfo &MRI = MF->getRegInfo();
12030 const TargetRegisterClass *AddrRegClass =
12031 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12032
12033 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12034 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12035 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012036 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012037 sizeVReg = MI->getOperand(1).getReg(),
12038 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12039
12040 MachineFunction::iterator MBBIter = BB;
12041 ++MBBIter;
12042
12043 MF->insert(MBBIter, bumpMBB);
12044 MF->insert(MBBIter, mallocMBB);
12045 MF->insert(MBBIter, continueMBB);
12046
12047 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12048 (MachineBasicBlock::iterator(MI)), BB->end());
12049 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12050
12051 // Add code to the main basic block to check if the stack limit has been hit,
12052 // and if so, jump to mallocMBB otherwise to bumpMBB.
12053 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012054 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012055 .addReg(tmpSPVReg).addReg(sizeVReg);
12056 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
12057 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012058 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012059 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12060
12061 // bumpMBB simply decreases the stack pointer, since we know the current
12062 // stacklet has enough space.
12063 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012064 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012065 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012066 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012067 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12068
12069 // Calls into a routine in libgcc to allocate more space from the heap.
12070 if (Is64Bit) {
12071 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12072 .addReg(sizeVReg);
12073 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12074 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12075 } else {
12076 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12077 .addImm(12);
12078 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12079 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12080 .addExternalSymbol("__morestack_allocate_stack_space");
12081 }
12082
12083 if (!Is64Bit)
12084 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12085 .addImm(16);
12086
12087 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12088 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12089 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12090
12091 // Set up the CFG correctly.
12092 BB->addSuccessor(bumpMBB);
12093 BB->addSuccessor(mallocMBB);
12094 mallocMBB->addSuccessor(continueMBB);
12095 bumpMBB->addSuccessor(continueMBB);
12096
12097 // Take care of the PHI nodes.
12098 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12099 MI->getOperand(0).getReg())
12100 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12101 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12102
12103 // Delete the original pseudo instruction.
12104 MI->eraseFromParent();
12105
12106 // And we're done.
12107 return continueMBB;
12108}
12109
12110MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012111X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012112 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012113 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12114 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012115
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012116 assert(!Subtarget->isTargetEnvMacho());
12117
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012118 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12119 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012120
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012121 if (Subtarget->isTargetWin64()) {
12122 if (Subtarget->isTargetCygMing()) {
12123 // ___chkstk(Mingw64):
12124 // Clobbers R10, R11, RAX and EFLAGS.
12125 // Updates RSP.
12126 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12127 .addExternalSymbol("___chkstk")
12128 .addReg(X86::RAX, RegState::Implicit)
12129 .addReg(X86::RSP, RegState::Implicit)
12130 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12131 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12132 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12133 } else {
12134 // __chkstk(MSVCRT): does not update stack pointer.
12135 // Clobbers R10, R11 and EFLAGS.
12136 // FIXME: RAX(allocated size) might be reused and not killed.
12137 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12138 .addExternalSymbol("__chkstk")
12139 .addReg(X86::RAX, RegState::Implicit)
12140 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12141 // RAX has the offset to subtracted from RSP.
12142 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12143 .addReg(X86::RSP)
12144 .addReg(X86::RAX);
12145 }
12146 } else {
12147 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012148 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12149
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012150 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12151 .addExternalSymbol(StackProbeSymbol)
12152 .addReg(X86::EAX, RegState::Implicit)
12153 .addReg(X86::ESP, RegState::Implicit)
12154 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12155 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12156 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12157 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012158
Dan Gohman14152b42010-07-06 20:24:04 +000012159 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012160 return BB;
12161}
Chris Lattner52600972009-09-02 05:57:00 +000012162
12163MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012164X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12165 MachineBasicBlock *BB) const {
12166 // This is pretty easy. We're taking the value that we received from
12167 // our load from the relocation, sticking it in either RDI (x86-64)
12168 // or EAX and doing an indirect call. The return value will then
12169 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012170 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012171 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012172 DebugLoc DL = MI->getDebugLoc();
12173 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012174
12175 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012176 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012177
Eric Christopher30ef0e52010-06-03 04:07:48 +000012178 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012179 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12180 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012181 .addReg(X86::RIP)
12182 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012183 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012184 MI->getOperand(3).getTargetFlags())
12185 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012186 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012187 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012188 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012189 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12190 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012191 .addReg(0)
12192 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012193 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012194 MI->getOperand(3).getTargetFlags())
12195 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012196 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012197 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012198 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012199 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12200 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012201 .addReg(TII->getGlobalBaseReg(F))
12202 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012203 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012204 MI->getOperand(3).getTargetFlags())
12205 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012206 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012207 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012208 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012209
Dan Gohman14152b42010-07-06 20:24:04 +000012210 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012211 return BB;
12212}
12213
12214MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012215X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012216 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012217 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000012218 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012219 case X86::TAILJMPd64:
12220 case X86::TAILJMPr64:
12221 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000012222 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012223 case X86::TCRETURNdi64:
12224 case X86::TCRETURNri64:
12225 case X86::TCRETURNmi64:
12226 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12227 // On AMD64, additional defs should be added before register allocation.
12228 if (!Subtarget->isTargetWin64()) {
12229 MI->addRegisterDefined(X86::RSI);
12230 MI->addRegisterDefined(X86::RDI);
12231 MI->addRegisterDefined(X86::XMM6);
12232 MI->addRegisterDefined(X86::XMM7);
12233 MI->addRegisterDefined(X86::XMM8);
12234 MI->addRegisterDefined(X86::XMM9);
12235 MI->addRegisterDefined(X86::XMM10);
12236 MI->addRegisterDefined(X86::XMM11);
12237 MI->addRegisterDefined(X86::XMM12);
12238 MI->addRegisterDefined(X86::XMM13);
12239 MI->addRegisterDefined(X86::XMM14);
12240 MI->addRegisterDefined(X86::XMM15);
12241 }
12242 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012243 case X86::WIN_ALLOCA:
12244 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012245 case X86::SEG_ALLOCA_32:
12246 return EmitLoweredSegAlloca(MI, BB, false);
12247 case X86::SEG_ALLOCA_64:
12248 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012249 case X86::TLSCall_32:
12250 case X86::TLSCall_64:
12251 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012252 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012253 case X86::CMOV_FR32:
12254 case X86::CMOV_FR64:
12255 case X86::CMOV_V4F32:
12256 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012257 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012258 case X86::CMOV_V8F32:
12259 case X86::CMOV_V4F64:
12260 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012261 case X86::CMOV_GR16:
12262 case X86::CMOV_GR32:
12263 case X86::CMOV_RFP32:
12264 case X86::CMOV_RFP64:
12265 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012266 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012267
Dale Johannesen849f2142007-07-03 00:53:03 +000012268 case X86::FP32_TO_INT16_IN_MEM:
12269 case X86::FP32_TO_INT32_IN_MEM:
12270 case X86::FP32_TO_INT64_IN_MEM:
12271 case X86::FP64_TO_INT16_IN_MEM:
12272 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012273 case X86::FP64_TO_INT64_IN_MEM:
12274 case X86::FP80_TO_INT16_IN_MEM:
12275 case X86::FP80_TO_INT32_IN_MEM:
12276 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012277 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12278 DebugLoc DL = MI->getDebugLoc();
12279
Evan Cheng60c07e12006-07-05 22:17:51 +000012280 // Change the floating point control register to use "round towards zero"
12281 // mode when truncating to an integer value.
12282 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012283 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012284 addFrameReference(BuildMI(*BB, MI, DL,
12285 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012286
12287 // Load the old value of the high byte of the control word...
12288 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012289 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012290 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012291 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012292
12293 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012294 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012295 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012296
12297 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012298 addFrameReference(BuildMI(*BB, MI, DL,
12299 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012300
12301 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012302 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012303 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012304
12305 // Get the X86 opcode to use.
12306 unsigned Opc;
12307 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012308 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012309 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12310 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12311 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12312 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12313 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12314 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012315 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12316 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12317 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012318 }
12319
12320 X86AddressMode AM;
12321 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012322 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012323 AM.BaseType = X86AddressMode::RegBase;
12324 AM.Base.Reg = Op.getReg();
12325 } else {
12326 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012327 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012328 }
12329 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012330 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012331 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012332 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012333 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012334 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012335 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012336 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012337 AM.GV = Op.getGlobal();
12338 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012339 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012340 }
Dan Gohman14152b42010-07-06 20:24:04 +000012341 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012342 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012343
12344 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012345 addFrameReference(BuildMI(*BB, MI, DL,
12346 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012347
Dan Gohman14152b42010-07-06 20:24:04 +000012348 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012349 return BB;
12350 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012351 // String/text processing lowering.
12352 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012353 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012354 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12355 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012356 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012357 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12358 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012359 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012360 return EmitPCMP(MI, BB, 5, false /* in mem */);
12361 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012362 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012363 return EmitPCMP(MI, BB, 5, true /* in mem */);
12364
Eric Christopher228232b2010-11-30 07:20:12 +000012365 // Thread synchronization.
12366 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012367 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012368 case X86::MWAIT:
12369 return EmitMwait(MI, BB);
12370
Eric Christopherb120ab42009-08-18 22:50:32 +000012371 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012372 case X86::ATOMAND32:
12373 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012374 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012375 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012376 X86::NOT32r, X86::EAX,
12377 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012378 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012379 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12380 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012381 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012382 X86::NOT32r, X86::EAX,
12383 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012384 case X86::ATOMXOR32:
12385 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012386 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012387 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012388 X86::NOT32r, X86::EAX,
12389 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012390 case X86::ATOMNAND32:
12391 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012392 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012393 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012394 X86::NOT32r, X86::EAX,
12395 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012396 case X86::ATOMMIN32:
12397 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12398 case X86::ATOMMAX32:
12399 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12400 case X86::ATOMUMIN32:
12401 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12402 case X86::ATOMUMAX32:
12403 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012404
12405 case X86::ATOMAND16:
12406 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12407 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012408 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012409 X86::NOT16r, X86::AX,
12410 X86::GR16RegisterClass);
12411 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012412 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012413 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012414 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012415 X86::NOT16r, X86::AX,
12416 X86::GR16RegisterClass);
12417 case X86::ATOMXOR16:
12418 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12419 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012420 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012421 X86::NOT16r, X86::AX,
12422 X86::GR16RegisterClass);
12423 case X86::ATOMNAND16:
12424 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12425 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012426 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012427 X86::NOT16r, X86::AX,
12428 X86::GR16RegisterClass, true);
12429 case X86::ATOMMIN16:
12430 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12431 case X86::ATOMMAX16:
12432 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12433 case X86::ATOMUMIN16:
12434 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12435 case X86::ATOMUMAX16:
12436 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12437
12438 case X86::ATOMAND8:
12439 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12440 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012441 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012442 X86::NOT8r, X86::AL,
12443 X86::GR8RegisterClass);
12444 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012445 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012446 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012447 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012448 X86::NOT8r, X86::AL,
12449 X86::GR8RegisterClass);
12450 case X86::ATOMXOR8:
12451 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12452 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012453 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012454 X86::NOT8r, X86::AL,
12455 X86::GR8RegisterClass);
12456 case X86::ATOMNAND8:
12457 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12458 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012459 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012460 X86::NOT8r, X86::AL,
12461 X86::GR8RegisterClass, true);
12462 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012463 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012464 case X86::ATOMAND64:
12465 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012466 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012467 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012468 X86::NOT64r, X86::RAX,
12469 X86::GR64RegisterClass);
12470 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012471 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12472 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012473 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012474 X86::NOT64r, X86::RAX,
12475 X86::GR64RegisterClass);
12476 case X86::ATOMXOR64:
12477 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012478 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012479 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012480 X86::NOT64r, X86::RAX,
12481 X86::GR64RegisterClass);
12482 case X86::ATOMNAND64:
12483 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12484 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012485 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012486 X86::NOT64r, X86::RAX,
12487 X86::GR64RegisterClass, true);
12488 case X86::ATOMMIN64:
12489 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12490 case X86::ATOMMAX64:
12491 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12492 case X86::ATOMUMIN64:
12493 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12494 case X86::ATOMUMAX64:
12495 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012496
12497 // This group does 64-bit operations on a 32-bit host.
12498 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012499 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012500 X86::AND32rr, X86::AND32rr,
12501 X86::AND32ri, X86::AND32ri,
12502 false);
12503 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012504 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012505 X86::OR32rr, X86::OR32rr,
12506 X86::OR32ri, X86::OR32ri,
12507 false);
12508 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012509 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012510 X86::XOR32rr, X86::XOR32rr,
12511 X86::XOR32ri, X86::XOR32ri,
12512 false);
12513 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012514 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012515 X86::AND32rr, X86::AND32rr,
12516 X86::AND32ri, X86::AND32ri,
12517 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012518 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012519 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012520 X86::ADD32rr, X86::ADC32rr,
12521 X86::ADD32ri, X86::ADC32ri,
12522 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012523 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012524 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012525 X86::SUB32rr, X86::SBB32rr,
12526 X86::SUB32ri, X86::SBB32ri,
12527 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012528 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012529 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012530 X86::MOV32rr, X86::MOV32rr,
12531 X86::MOV32ri, X86::MOV32ri,
12532 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012533 case X86::VASTART_SAVE_XMM_REGS:
12534 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012535
12536 case X86::VAARG_64:
12537 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012538 }
12539}
12540
12541//===----------------------------------------------------------------------===//
12542// X86 Optimization Hooks
12543//===----------------------------------------------------------------------===//
12544
Dan Gohman475871a2008-07-27 21:46:04 +000012545void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012546 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012547 APInt &KnownZero,
12548 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012549 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012550 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012551 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012552 assert((Opc >= ISD::BUILTIN_OP_END ||
12553 Opc == ISD::INTRINSIC_WO_CHAIN ||
12554 Opc == ISD::INTRINSIC_W_CHAIN ||
12555 Opc == ISD::INTRINSIC_VOID) &&
12556 "Should use MaskedValueIsZero if you don't know whether Op"
12557 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012558
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012559 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012560 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012561 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012562 case X86ISD::ADD:
12563 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012564 case X86ISD::ADC:
12565 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012566 case X86ISD::SMUL:
12567 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012568 case X86ISD::INC:
12569 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012570 case X86ISD::OR:
12571 case X86ISD::XOR:
12572 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012573 // These nodes' second result is a boolean.
12574 if (Op.getResNo() == 0)
12575 break;
12576 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012577 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012578 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12579 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012580 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012581 case ISD::INTRINSIC_WO_CHAIN: {
12582 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12583 unsigned NumLoBits = 0;
12584 switch (IntId) {
12585 default: break;
12586 case Intrinsic::x86_sse_movmsk_ps:
12587 case Intrinsic::x86_avx_movmsk_ps_256:
12588 case Intrinsic::x86_sse2_movmsk_pd:
12589 case Intrinsic::x86_avx_movmsk_pd_256:
12590 case Intrinsic::x86_mmx_pmovmskb:
12591 case Intrinsic::x86_sse2_pmovmskb_128: {
12592 // High bits of movmskp{s|d}, pmovmskb are known zero.
12593 switch (IntId) {
12594 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12595 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12596 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12597 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12598 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12599 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
12600 }
12601 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12602 Mask.getBitWidth() - NumLoBits);
12603 break;
12604 }
12605 }
12606 break;
12607 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012608 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012609}
Chris Lattner259e97c2006-01-31 19:43:35 +000012610
Owen Andersonbc146b02010-09-21 20:42:50 +000012611unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12612 unsigned Depth) const {
12613 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12614 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12615 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012616
Owen Andersonbc146b02010-09-21 20:42:50 +000012617 // Fallback case.
12618 return 1;
12619}
12620
Evan Cheng206ee9d2006-07-07 08:33:52 +000012621/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012622/// node is a GlobalAddress + offset.
12623bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012624 const GlobalValue* &GA,
12625 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012626 if (N->getOpcode() == X86ISD::Wrapper) {
12627 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012628 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012629 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012630 return true;
12631 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012632 }
Evan Chengad4196b2008-05-12 19:56:52 +000012633 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012634}
12635
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012636/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12637/// same as extracting the high 128-bit part of 256-bit vector and then
12638/// inserting the result into the low part of a new 256-bit vector
12639static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12640 EVT VT = SVOp->getValueType(0);
12641 int NumElems = VT.getVectorNumElements();
12642
12643 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12644 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12645 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12646 SVOp->getMaskElt(j) >= 0)
12647 return false;
12648
12649 return true;
12650}
12651
12652/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12653/// same as extracting the low 128-bit part of 256-bit vector and then
12654/// inserting the result into the high part of a new 256-bit vector
12655static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12656 EVT VT = SVOp->getValueType(0);
12657 int NumElems = VT.getVectorNumElements();
12658
12659 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12660 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12661 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12662 SVOp->getMaskElt(j) >= 0)
12663 return false;
12664
12665 return true;
12666}
12667
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012668/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12669static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12670 TargetLowering::DAGCombinerInfo &DCI) {
12671 DebugLoc dl = N->getDebugLoc();
12672 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12673 SDValue V1 = SVOp->getOperand(0);
12674 SDValue V2 = SVOp->getOperand(1);
12675 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012676 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012677
12678 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12679 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12680 //
12681 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012682 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012683 // V UNDEF BUILD_VECTOR UNDEF
12684 // \ / \ /
12685 // CONCAT_VECTOR CONCAT_VECTOR
12686 // \ /
12687 // \ /
12688 // RESULT: V + zero extended
12689 //
12690 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12691 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12692 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12693 return SDValue();
12694
12695 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12696 return SDValue();
12697
12698 // To match the shuffle mask, the first half of the mask should
12699 // be exactly the first vector, and all the rest a splat with the
12700 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012701 for (int i = 0; i < NumElems/2; ++i)
12702 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12703 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12704 return SDValue();
12705
12706 // Emit a zeroed vector and insert the desired subvector on its
12707 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012708 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012709 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12710 DAG.getConstant(0, MVT::i32), DAG, dl);
12711 return DCI.CombineTo(N, InsV);
12712 }
12713
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012714 //===--------------------------------------------------------------------===//
12715 // Combine some shuffles into subvector extracts and inserts:
12716 //
12717
12718 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12719 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12720 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12721 DAG, dl);
12722 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12723 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12724 return DCI.CombineTo(N, InsV);
12725 }
12726
12727 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12728 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12729 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12730 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12731 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12732 return DCI.CombineTo(N, InsV);
12733 }
12734
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012735 return SDValue();
12736}
12737
12738/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012739static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012740 TargetLowering::DAGCombinerInfo &DCI,
12741 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012742 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012743 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012744
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012745 // Don't create instructions with illegal types after legalize types has run.
12746 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12747 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12748 return SDValue();
12749
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012750 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12751 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12752 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012753 return PerformShuffleCombine256(N, DAG, DCI);
12754
12755 // Only handle 128 wide vector from here on.
12756 if (VT.getSizeInBits() != 128)
12757 return SDValue();
12758
12759 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12760 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12761 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012762 SmallVector<SDValue, 16> Elts;
12763 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012764 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012765
Nate Begemanfdea31a2010-03-24 20:49:50 +000012766 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012767}
Evan Chengd880b972008-05-09 21:53:03 +000012768
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012769/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12770/// generation and convert it from being a bunch of shuffles and extracts
12771/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012772static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12773 const TargetLowering &TLI) {
12774 SDValue InputVector = N->getOperand(0);
12775
12776 // Only operate on vectors of 4 elements, where the alternative shuffling
12777 // gets to be more expensive.
12778 if (InputVector.getValueType() != MVT::v4i32)
12779 return SDValue();
12780
12781 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12782 // single use which is a sign-extend or zero-extend, and all elements are
12783 // used.
12784 SmallVector<SDNode *, 4> Uses;
12785 unsigned ExtractedElements = 0;
12786 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12787 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12788 if (UI.getUse().getResNo() != InputVector.getResNo())
12789 return SDValue();
12790
12791 SDNode *Extract = *UI;
12792 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12793 return SDValue();
12794
12795 if (Extract->getValueType(0) != MVT::i32)
12796 return SDValue();
12797 if (!Extract->hasOneUse())
12798 return SDValue();
12799 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12800 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12801 return SDValue();
12802 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12803 return SDValue();
12804
12805 // Record which element was extracted.
12806 ExtractedElements |=
12807 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12808
12809 Uses.push_back(Extract);
12810 }
12811
12812 // If not all the elements were used, this may not be worthwhile.
12813 if (ExtractedElements != 15)
12814 return SDValue();
12815
12816 // Ok, we've now decided to do the transformation.
12817 DebugLoc dl = InputVector.getDebugLoc();
12818
12819 // Store the value to a temporary stack slot.
12820 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012821 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12822 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012823
12824 // Replace each use (extract) with a load of the appropriate element.
12825 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12826 UE = Uses.end(); UI != UE; ++UI) {
12827 SDNode *Extract = *UI;
12828
Nadav Rotem86694292011-05-17 08:31:57 +000012829 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012830 SDValue Idx = Extract->getOperand(1);
12831 unsigned EltSize =
12832 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12833 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12834 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12835
Nadav Rotem86694292011-05-17 08:31:57 +000012836 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012837 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012838
12839 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012840 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012841 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000012842 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012843
12844 // Replace the exact with the load.
12845 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12846 }
12847
12848 // The replacement was made in place; don't return anything.
12849 return SDValue();
12850}
12851
Duncan Sands6bcd2192011-09-17 16:49:39 +000012852/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
12853/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012854static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012855 const X86Subtarget *Subtarget) {
12856 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012857 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012858 // Get the LHS/RHS of the select.
12859 SDValue LHS = N->getOperand(1);
12860 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000012861 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000012862
Dan Gohman670e5392009-09-21 18:03:22 +000012863 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012864 // instructions match the semantics of the common C idiom x<y?x:y but not
12865 // x<=y?x:y, because of how they handle negative zero (which can be
12866 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000012867 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
12868 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
12869 (Subtarget->hasXMMInt() ||
12870 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012871 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012872
Chris Lattner47b4ce82009-03-11 05:48:52 +000012873 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012874 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012875 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12876 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012877 switch (CC) {
12878 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012879 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012880 // Converting this to a min would handle NaNs incorrectly, and swapping
12881 // the operands would cause it to handle comparisons between positive
12882 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012883 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012884 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012885 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12886 break;
12887 std::swap(LHS, RHS);
12888 }
Dan Gohman670e5392009-09-21 18:03:22 +000012889 Opcode = X86ISD::FMIN;
12890 break;
12891 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012892 // Converting this to a min would handle comparisons between positive
12893 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012894 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012895 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12896 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012897 Opcode = X86ISD::FMIN;
12898 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012899 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012900 // Converting this to a min would handle both negative zeros and NaNs
12901 // incorrectly, but we can swap the operands to fix both.
12902 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012903 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012904 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012905 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012906 Opcode = X86ISD::FMIN;
12907 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012908
Dan Gohman670e5392009-09-21 18:03:22 +000012909 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012910 // Converting this to a max would handle comparisons between positive
12911 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012912 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012913 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012914 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012915 Opcode = X86ISD::FMAX;
12916 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012917 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012918 // Converting this to a max would handle NaNs incorrectly, and swapping
12919 // the operands would cause it to handle comparisons between positive
12920 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012921 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012922 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012923 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12924 break;
12925 std::swap(LHS, RHS);
12926 }
Dan Gohman670e5392009-09-21 18:03:22 +000012927 Opcode = X86ISD::FMAX;
12928 break;
12929 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012930 // Converting this to a max would handle both negative zeros and NaNs
12931 // incorrectly, but we can swap the operands to fix both.
12932 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012933 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012934 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012935 case ISD::SETGE:
12936 Opcode = X86ISD::FMAX;
12937 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012938 }
Dan Gohman670e5392009-09-21 18:03:22 +000012939 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012940 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12941 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012942 switch (CC) {
12943 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012944 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012945 // Converting this to a min would handle comparisons between positive
12946 // and negative zero incorrectly, and swapping the operands would
12947 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012948 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012949 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012950 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012951 break;
12952 std::swap(LHS, RHS);
12953 }
Dan Gohman670e5392009-09-21 18:03:22 +000012954 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012955 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012956 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012957 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012958 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012959 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12960 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012961 Opcode = X86ISD::FMIN;
12962 break;
12963 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012964 // Converting this to a min would handle both negative zeros and NaNs
12965 // incorrectly, but we can swap the operands to fix both.
12966 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012967 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012968 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012969 case ISD::SETGE:
12970 Opcode = X86ISD::FMIN;
12971 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012972
Dan Gohman670e5392009-09-21 18:03:22 +000012973 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012974 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012975 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012976 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012977 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012978 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012979 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012980 // Converting this to a max would handle comparisons between positive
12981 // and negative zero incorrectly, and swapping the operands would
12982 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012983 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000012984 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012985 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012986 break;
12987 std::swap(LHS, RHS);
12988 }
Dan Gohman670e5392009-09-21 18:03:22 +000012989 Opcode = X86ISD::FMAX;
12990 break;
12991 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012992 // Converting this to a max would handle both negative zeros and NaNs
12993 // incorrectly, but we can swap the operands to fix both.
12994 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012995 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012996 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012997 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012998 Opcode = X86ISD::FMAX;
12999 break;
13000 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013001 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013002
Chris Lattner47b4ce82009-03-11 05:48:52 +000013003 if (Opcode)
13004 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013005 }
Eric Christopherfd179292009-08-27 18:07:15 +000013006
Chris Lattnerd1980a52009-03-12 06:52:53 +000013007 // If this is a select between two integer constants, try to do some
13008 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013009 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13010 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013011 // Don't do this for crazy integer types.
13012 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13013 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013014 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013015 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013016
Chris Lattnercee56e72009-03-13 05:53:31 +000013017 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013018 // Efficiently invertible.
13019 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13020 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13021 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13022 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013023 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013024 }
Eric Christopherfd179292009-08-27 18:07:15 +000013025
Chris Lattnerd1980a52009-03-12 06:52:53 +000013026 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013027 if (FalseC->getAPIntValue() == 0 &&
13028 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013029 if (NeedsCondInvert) // Invert the condition if needed.
13030 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13031 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013032
Chris Lattnerd1980a52009-03-12 06:52:53 +000013033 // Zero extend the condition if needed.
13034 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013035
Chris Lattnercee56e72009-03-13 05:53:31 +000013036 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013037 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013038 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013039 }
Eric Christopherfd179292009-08-27 18:07:15 +000013040
Chris Lattner97a29a52009-03-13 05:22:11 +000013041 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013042 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013043 if (NeedsCondInvert) // Invert the condition if needed.
13044 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13045 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013046
Chris Lattner97a29a52009-03-13 05:22:11 +000013047 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013048 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13049 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013050 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013051 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013052 }
Eric Christopherfd179292009-08-27 18:07:15 +000013053
Chris Lattnercee56e72009-03-13 05:53:31 +000013054 // Optimize cases that will turn into an LEA instruction. This requires
13055 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013056 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013057 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013058 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013059
Chris Lattnercee56e72009-03-13 05:53:31 +000013060 bool isFastMultiplier = false;
13061 if (Diff < 10) {
13062 switch ((unsigned char)Diff) {
13063 default: break;
13064 case 1: // result = add base, cond
13065 case 2: // result = lea base( , cond*2)
13066 case 3: // result = lea base(cond, cond*2)
13067 case 4: // result = lea base( , cond*4)
13068 case 5: // result = lea base(cond, cond*4)
13069 case 8: // result = lea base( , cond*8)
13070 case 9: // result = lea base(cond, cond*8)
13071 isFastMultiplier = true;
13072 break;
13073 }
13074 }
Eric Christopherfd179292009-08-27 18:07:15 +000013075
Chris Lattnercee56e72009-03-13 05:53:31 +000013076 if (isFastMultiplier) {
13077 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13078 if (NeedsCondInvert) // Invert the condition if needed.
13079 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13080 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013081
Chris Lattnercee56e72009-03-13 05:53:31 +000013082 // Zero extend the condition if needed.
13083 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13084 Cond);
13085 // Scale the condition by the difference.
13086 if (Diff != 1)
13087 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13088 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013089
Chris Lattnercee56e72009-03-13 05:53:31 +000013090 // Add the base if non-zero.
13091 if (FalseC->getAPIntValue() != 0)
13092 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13093 SDValue(FalseC, 0));
13094 return Cond;
13095 }
Eric Christopherfd179292009-08-27 18:07:15 +000013096 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013097 }
13098 }
Eric Christopherfd179292009-08-27 18:07:15 +000013099
Dan Gohman475871a2008-07-27 21:46:04 +000013100 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013101}
13102
Chris Lattnerd1980a52009-03-12 06:52:53 +000013103/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13104static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13105 TargetLowering::DAGCombinerInfo &DCI) {
13106 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013107
Chris Lattnerd1980a52009-03-12 06:52:53 +000013108 // If the flag operand isn't dead, don't touch this CMOV.
13109 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13110 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013111
Evan Chengb5a55d92011-05-24 01:48:22 +000013112 SDValue FalseOp = N->getOperand(0);
13113 SDValue TrueOp = N->getOperand(1);
13114 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13115 SDValue Cond = N->getOperand(3);
13116 if (CC == X86::COND_E || CC == X86::COND_NE) {
13117 switch (Cond.getOpcode()) {
13118 default: break;
13119 case X86ISD::BSR:
13120 case X86ISD::BSF:
13121 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13122 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13123 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13124 }
13125 }
13126
Chris Lattnerd1980a52009-03-12 06:52:53 +000013127 // If this is a select between two integer constants, try to do some
13128 // optimizations. Note that the operands are ordered the opposite of SELECT
13129 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013130 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13131 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013132 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13133 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013134 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13135 CC = X86::GetOppositeBranchCondition(CC);
13136 std::swap(TrueC, FalseC);
13137 }
Eric Christopherfd179292009-08-27 18:07:15 +000013138
Chris Lattnerd1980a52009-03-12 06:52:53 +000013139 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013140 // This is efficient for any integer data type (including i8/i16) and
13141 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013142 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013143 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13144 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013145
Chris Lattnerd1980a52009-03-12 06:52:53 +000013146 // Zero extend the condition if needed.
13147 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013148
Chris Lattnerd1980a52009-03-12 06:52:53 +000013149 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13150 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013151 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013152 if (N->getNumValues() == 2) // Dead flag value?
13153 return DCI.CombineTo(N, Cond, SDValue());
13154 return Cond;
13155 }
Eric Christopherfd179292009-08-27 18:07:15 +000013156
Chris Lattnercee56e72009-03-13 05:53:31 +000013157 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13158 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013159 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013160 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13161 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013162
Chris Lattner97a29a52009-03-13 05:22:11 +000013163 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013164 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13165 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013166 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13167 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013168
Chris Lattner97a29a52009-03-13 05:22:11 +000013169 if (N->getNumValues() == 2) // Dead flag value?
13170 return DCI.CombineTo(N, Cond, SDValue());
13171 return Cond;
13172 }
Eric Christopherfd179292009-08-27 18:07:15 +000013173
Chris Lattnercee56e72009-03-13 05:53:31 +000013174 // Optimize cases that will turn into an LEA instruction. This requires
13175 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013176 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013177 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013178 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013179
Chris Lattnercee56e72009-03-13 05:53:31 +000013180 bool isFastMultiplier = false;
13181 if (Diff < 10) {
13182 switch ((unsigned char)Diff) {
13183 default: break;
13184 case 1: // result = add base, cond
13185 case 2: // result = lea base( , cond*2)
13186 case 3: // result = lea base(cond, cond*2)
13187 case 4: // result = lea base( , cond*4)
13188 case 5: // result = lea base(cond, cond*4)
13189 case 8: // result = lea base( , cond*8)
13190 case 9: // result = lea base(cond, cond*8)
13191 isFastMultiplier = true;
13192 break;
13193 }
13194 }
Eric Christopherfd179292009-08-27 18:07:15 +000013195
Chris Lattnercee56e72009-03-13 05:53:31 +000013196 if (isFastMultiplier) {
13197 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013198 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13199 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013200 // Zero extend the condition if needed.
13201 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13202 Cond);
13203 // Scale the condition by the difference.
13204 if (Diff != 1)
13205 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13206 DAG.getConstant(Diff, Cond.getValueType()));
13207
13208 // Add the base if non-zero.
13209 if (FalseC->getAPIntValue() != 0)
13210 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13211 SDValue(FalseC, 0));
13212 if (N->getNumValues() == 2) // Dead flag value?
13213 return DCI.CombineTo(N, Cond, SDValue());
13214 return Cond;
13215 }
Eric Christopherfd179292009-08-27 18:07:15 +000013216 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013217 }
13218 }
13219 return SDValue();
13220}
13221
13222
Evan Cheng0b0cd912009-03-28 05:57:29 +000013223/// PerformMulCombine - Optimize a single multiply with constant into two
13224/// in order to implement it with two cheaper instructions, e.g.
13225/// LEA + SHL, LEA + LEA.
13226static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13227 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013228 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13229 return SDValue();
13230
Owen Andersone50ed302009-08-10 22:56:29 +000013231 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013232 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013233 return SDValue();
13234
13235 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13236 if (!C)
13237 return SDValue();
13238 uint64_t MulAmt = C->getZExtValue();
13239 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13240 return SDValue();
13241
13242 uint64_t MulAmt1 = 0;
13243 uint64_t MulAmt2 = 0;
13244 if ((MulAmt % 9) == 0) {
13245 MulAmt1 = 9;
13246 MulAmt2 = MulAmt / 9;
13247 } else if ((MulAmt % 5) == 0) {
13248 MulAmt1 = 5;
13249 MulAmt2 = MulAmt / 5;
13250 } else if ((MulAmt % 3) == 0) {
13251 MulAmt1 = 3;
13252 MulAmt2 = MulAmt / 3;
13253 }
13254 if (MulAmt2 &&
13255 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13256 DebugLoc DL = N->getDebugLoc();
13257
13258 if (isPowerOf2_64(MulAmt2) &&
13259 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13260 // If second multiplifer is pow2, issue it first. We want the multiply by
13261 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13262 // is an add.
13263 std::swap(MulAmt1, MulAmt2);
13264
13265 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013266 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013267 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013268 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013269 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013270 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013271 DAG.getConstant(MulAmt1, VT));
13272
Eric Christopherfd179292009-08-27 18:07:15 +000013273 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013274 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013275 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013276 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013277 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013278 DAG.getConstant(MulAmt2, VT));
13279
13280 // Do not add new nodes to DAG combiner worklist.
13281 DCI.CombineTo(N, NewMul, false);
13282 }
13283 return SDValue();
13284}
13285
Evan Chengad9c0a32009-12-15 00:53:42 +000013286static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13287 SDValue N0 = N->getOperand(0);
13288 SDValue N1 = N->getOperand(1);
13289 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13290 EVT VT = N0.getValueType();
13291
13292 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13293 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013294 if (VT.isInteger() && !VT.isVector() &&
13295 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013296 N0.getOperand(1).getOpcode() == ISD::Constant) {
13297 SDValue N00 = N0.getOperand(0);
13298 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13299 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13300 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13301 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13302 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13303 APInt ShAmt = N1C->getAPIntValue();
13304 Mask = Mask.shl(ShAmt);
13305 if (Mask != 0)
13306 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13307 N00, DAG.getConstant(Mask, VT));
13308 }
13309 }
13310
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013311
13312 // Hardware support for vector shifts is sparse which makes us scalarize the
13313 // vector operations in many cases. Also, on sandybridge ADD is faster than
13314 // shl.
13315 // (shl V, 1) -> add V,V
13316 if (isSplatVector(N1.getNode())) {
13317 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13318 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13319 // We shift all of the values by one. In many cases we do not have
13320 // hardware support for this operation. This is better expressed as an ADD
13321 // of two values.
13322 if (N1C && (1 == N1C->getZExtValue())) {
13323 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13324 }
13325 }
13326
Evan Chengad9c0a32009-12-15 00:53:42 +000013327 return SDValue();
13328}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013329
Nate Begeman740ab032009-01-26 00:52:55 +000013330/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13331/// when possible.
13332static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13333 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013334 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013335 if (N->getOpcode() == ISD::SHL) {
13336 SDValue V = PerformSHLCombine(N, DAG);
13337 if (V.getNode()) return V;
13338 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013339
Nate Begeman740ab032009-01-26 00:52:55 +000013340 // On X86 with SSE2 support, we can transform this to a vector shift if
13341 // all elements are shifted by the same amount. We can't do this in legalize
13342 // because the a constant vector is typically transformed to a constant pool
13343 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013344 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013345 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013346
Craig Topper7be5dfd2011-11-12 09:58:49 +000013347 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
13348 (!Subtarget->hasAVX2() ||
13349 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013350 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013351
Mon P Wang3becd092009-01-28 08:12:05 +000013352 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013353 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013354 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013355 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013356 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13357 unsigned NumElts = VT.getVectorNumElements();
13358 unsigned i = 0;
13359 for (; i != NumElts; ++i) {
13360 SDValue Arg = ShAmtOp.getOperand(i);
13361 if (Arg.getOpcode() == ISD::UNDEF) continue;
13362 BaseShAmt = Arg;
13363 break;
13364 }
13365 for (; i != NumElts; ++i) {
13366 SDValue Arg = ShAmtOp.getOperand(i);
13367 if (Arg.getOpcode() == ISD::UNDEF) continue;
13368 if (Arg != BaseShAmt) {
13369 return SDValue();
13370 }
13371 }
13372 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013373 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013374 SDValue InVec = ShAmtOp.getOperand(0);
13375 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13376 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13377 unsigned i = 0;
13378 for (; i != NumElts; ++i) {
13379 SDValue Arg = InVec.getOperand(i);
13380 if (Arg.getOpcode() == ISD::UNDEF) continue;
13381 BaseShAmt = Arg;
13382 break;
13383 }
13384 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13385 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013386 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013387 if (C->getZExtValue() == SplatIdx)
13388 BaseShAmt = InVec.getOperand(1);
13389 }
13390 }
13391 if (BaseShAmt.getNode() == 0)
13392 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13393 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013394 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013395 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013396
Mon P Wangefa42202009-09-03 19:56:25 +000013397 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013398 if (EltVT.bitsGT(MVT::i32))
13399 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13400 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013401 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013402
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013403 // The shift amount is identical so we can do a vector shift.
13404 SDValue ValOp = N->getOperand(0);
13405 switch (N->getOpcode()) {
13406 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013407 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013408 break;
13409 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013410 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013411 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013412 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013413 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013414 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013415 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013416 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013417 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013418 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013419 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013420 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013421 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013422 if (VT == MVT::v4i64)
13423 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13424 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
13425 ValOp, BaseShAmt);
13426 if (VT == MVT::v8i32)
13427 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13428 DAG.getConstant(Intrinsic::x86_avx2_pslli_d, MVT::i32),
13429 ValOp, BaseShAmt);
13430 if (VT == MVT::v16i16)
13431 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13432 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
13433 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013434 break;
13435 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013436 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013437 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013438 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013439 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013440 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013441 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013442 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013443 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013444 if (VT == MVT::v8i32)
13445 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13446 DAG.getConstant(Intrinsic::x86_avx2_psrai_d, MVT::i32),
13447 ValOp, BaseShAmt);
13448 if (VT == MVT::v16i16)
13449 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13450 DAG.getConstant(Intrinsic::x86_avx2_psrai_w, MVT::i32),
13451 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013452 break;
13453 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013454 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013455 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013456 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013457 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013458 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013459 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013460 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013461 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013462 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013463 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013464 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013465 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013466 if (VT == MVT::v4i64)
13467 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13468 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
13469 ValOp, BaseShAmt);
13470 if (VT == MVT::v8i32)
13471 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13472 DAG.getConstant(Intrinsic::x86_avx2_psrli_d, MVT::i32),
13473 ValOp, BaseShAmt);
13474 if (VT == MVT::v16i16)
13475 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13476 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
13477 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013478 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013479 }
13480 return SDValue();
13481}
13482
Nate Begemanb65c1752010-12-17 22:55:37 +000013483
Stuart Hastings865f0932011-06-03 23:53:54 +000013484// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13485// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13486// and friends. Likewise for OR -> CMPNEQSS.
13487static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13488 TargetLowering::DAGCombinerInfo &DCI,
13489 const X86Subtarget *Subtarget) {
13490 unsigned opcode;
13491
13492 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13493 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013494 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013495 SDValue N0 = N->getOperand(0);
13496 SDValue N1 = N->getOperand(1);
13497 SDValue CMP0 = N0->getOperand(1);
13498 SDValue CMP1 = N1->getOperand(1);
13499 DebugLoc DL = N->getDebugLoc();
13500
13501 // The SETCCs should both refer to the same CMP.
13502 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13503 return SDValue();
13504
13505 SDValue CMP00 = CMP0->getOperand(0);
13506 SDValue CMP01 = CMP0->getOperand(1);
13507 EVT VT = CMP00.getValueType();
13508
13509 if (VT == MVT::f32 || VT == MVT::f64) {
13510 bool ExpectingFlags = false;
13511 // Check for any users that want flags:
13512 for (SDNode::use_iterator UI = N->use_begin(),
13513 UE = N->use_end();
13514 !ExpectingFlags && UI != UE; ++UI)
13515 switch (UI->getOpcode()) {
13516 default:
13517 case ISD::BR_CC:
13518 case ISD::BRCOND:
13519 case ISD::SELECT:
13520 ExpectingFlags = true;
13521 break;
13522 case ISD::CopyToReg:
13523 case ISD::SIGN_EXTEND:
13524 case ISD::ZERO_EXTEND:
13525 case ISD::ANY_EXTEND:
13526 break;
13527 }
13528
13529 if (!ExpectingFlags) {
13530 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13531 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13532
13533 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13534 X86::CondCode tmp = cc0;
13535 cc0 = cc1;
13536 cc1 = tmp;
13537 }
13538
13539 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13540 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13541 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13542 X86ISD::NodeType NTOperator = is64BitFP ?
13543 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13544 // FIXME: need symbolic constants for these magic numbers.
13545 // See X86ATTInstPrinter.cpp:printSSECC().
13546 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13547 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13548 DAG.getConstant(x86cc, MVT::i8));
13549 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13550 OnesOrZeroesF);
13551 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13552 DAG.getConstant(1, MVT::i32));
13553 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13554 return OneBitOfTruth;
13555 }
13556 }
13557 }
13558 }
13559 return SDValue();
13560}
13561
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013562/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13563/// so it can be folded inside ANDNP.
13564static bool CanFoldXORWithAllOnes(const SDNode *N) {
13565 EVT VT = N->getValueType(0);
13566
13567 // Match direct AllOnes for 128 and 256-bit vectors
13568 if (ISD::isBuildVectorAllOnes(N))
13569 return true;
13570
13571 // Look through a bit convert.
13572 if (N->getOpcode() == ISD::BITCAST)
13573 N = N->getOperand(0).getNode();
13574
13575 // Sometimes the operand may come from a insert_subvector building a 256-bit
13576 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013577 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013578 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13579 SDValue V1 = N->getOperand(0);
13580 SDValue V2 = N->getOperand(1);
13581
13582 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13583 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13584 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13585 ISD::isBuildVectorAllOnes(V2.getNode()))
13586 return true;
13587 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013588
13589 return false;
13590}
13591
Nate Begemanb65c1752010-12-17 22:55:37 +000013592static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13593 TargetLowering::DAGCombinerInfo &DCI,
13594 const X86Subtarget *Subtarget) {
13595 if (DCI.isBeforeLegalizeOps())
13596 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013597
Stuart Hastings865f0932011-06-03 23:53:54 +000013598 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13599 if (R.getNode())
13600 return R;
13601
Craig Topper54a11172011-10-14 07:06:56 +000013602 EVT VT = N->getValueType(0);
13603
Craig Topperb4c94572011-10-21 06:55:01 +000013604 // Create ANDN, BLSI, and BLSR instructions
13605 // BLSI is X & (-X)
13606 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013607 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13608 SDValue N0 = N->getOperand(0);
13609 SDValue N1 = N->getOperand(1);
13610 DebugLoc DL = N->getDebugLoc();
13611
13612 // Check LHS for not
13613 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13614 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13615 // Check RHS for not
13616 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13617 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13618
Craig Topperb4c94572011-10-21 06:55:01 +000013619 // Check LHS for neg
13620 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13621 isZero(N0.getOperand(0)))
13622 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13623
13624 // Check RHS for neg
13625 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13626 isZero(N1.getOperand(0)))
13627 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13628
13629 // Check LHS for X-1
13630 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13631 isAllOnes(N0.getOperand(1)))
13632 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13633
13634 // Check RHS for X-1
13635 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13636 isAllOnes(N1.getOperand(1)))
13637 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13638
Craig Topper54a11172011-10-14 07:06:56 +000013639 return SDValue();
13640 }
13641
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013642 // Want to form ANDNP nodes:
13643 // 1) In the hopes of then easily combining them with OR and AND nodes
13644 // to form PBLEND/PSIGN.
13645 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013646 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013647 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013648
Nate Begemanb65c1752010-12-17 22:55:37 +000013649 SDValue N0 = N->getOperand(0);
13650 SDValue N1 = N->getOperand(1);
13651 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013652
Nate Begemanb65c1752010-12-17 22:55:37 +000013653 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013654 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013655 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13656 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013657 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013658
13659 // Check RHS for vnot
13660 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013661 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13662 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013663 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013664
Nate Begemanb65c1752010-12-17 22:55:37 +000013665 return SDValue();
13666}
13667
Evan Cheng760d1942010-01-04 21:22:48 +000013668static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013669 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013670 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013671 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013672 return SDValue();
13673
Stuart Hastings865f0932011-06-03 23:53:54 +000013674 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13675 if (R.getNode())
13676 return R;
13677
Evan Cheng760d1942010-01-04 21:22:48 +000013678 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000013679
Evan Cheng760d1942010-01-04 21:22:48 +000013680 SDValue N0 = N->getOperand(0);
13681 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013682
Nate Begemanb65c1752010-12-17 22:55:37 +000013683 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000013684 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperc0d82852011-11-22 00:44:41 +000013685 if (!Subtarget->hasSSSE3orAVX() ||
Craig Topper1666cb62011-11-19 07:07:26 +000013686 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
13687 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013688
Craig Topper1666cb62011-11-19 07:07:26 +000013689 // Canonicalize pandn to RHS
13690 if (N0.getOpcode() == X86ISD::ANDNP)
13691 std::swap(N0, N1);
13692 // or (and (m, x), (pandn m, y))
13693 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
13694 SDValue Mask = N1.getOperand(0);
13695 SDValue X = N1.getOperand(1);
13696 SDValue Y;
13697 if (N0.getOperand(0) == Mask)
13698 Y = N0.getOperand(1);
13699 if (N0.getOperand(1) == Mask)
13700 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013701
Craig Topper1666cb62011-11-19 07:07:26 +000013702 // Check to see if the mask appeared in both the AND and ANDNP and
13703 if (!Y.getNode())
13704 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013705
Craig Topper1666cb62011-11-19 07:07:26 +000013706 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13707 if (Mask.getOpcode() != ISD::BITCAST ||
13708 X.getOpcode() != ISD::BITCAST ||
13709 Y.getOpcode() != ISD::BITCAST)
13710 return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013711
Craig Topper1666cb62011-11-19 07:07:26 +000013712 // Look through mask bitcast.
13713 Mask = Mask.getOperand(0);
13714 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013715
Craig Topper1666cb62011-11-19 07:07:26 +000013716 // Validate that the Mask operand is a vector sra node. The sra node
13717 // will be an intrinsic.
13718 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13719 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013720
Craig Topper1666cb62011-11-19 07:07:26 +000013721 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13722 // there is no psrai.b
13723 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13724 case Intrinsic::x86_sse2_psrai_w:
13725 case Intrinsic::x86_sse2_psrai_d:
13726 case Intrinsic::x86_avx2_psrai_w:
13727 case Intrinsic::x86_avx2_psrai_d:
13728 break;
13729 default: return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000013730 }
Craig Topper1666cb62011-11-19 07:07:26 +000013731
13732 // Check that the SRA is all signbits.
13733 SDValue SraC = Mask.getOperand(2);
13734 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13735 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13736 if ((SraAmt + 1) != EltBits)
13737 return SDValue();
13738
13739 DebugLoc DL = N->getDebugLoc();
13740
13741 // Now we know we at least have a plendvb with the mask val. See if
13742 // we can form a psignb/w/d.
13743 // psign = x.type == y.type == mask.type && y = sub(0, x);
13744 X = X.getOperand(0);
13745 Y = Y.getOperand(0);
13746 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13747 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Topper31133842011-11-19 07:33:10 +000013748 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType() &&
13749 (EltBits == 8 || EltBits == 16 || EltBits == 32)) {
13750 SDValue Sign = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X,
13751 Mask.getOperand(1));
13752 return DAG.getNode(ISD::BITCAST, DL, VT, Sign);
Craig Topper1666cb62011-11-19 07:07:26 +000013753 }
13754 // PBLENDVB only available on SSE 4.1
Craig Topperc0d82852011-11-22 00:44:41 +000013755 if (!Subtarget->hasSSE41orAVX())
Craig Topper1666cb62011-11-19 07:07:26 +000013756 return SDValue();
13757
13758 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
13759
13760 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
13761 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
13762 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000013763 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000013764 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000013765 }
13766 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013767
Craig Topper1666cb62011-11-19 07:07:26 +000013768 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
13769 return SDValue();
13770
Nate Begemanb65c1752010-12-17 22:55:37 +000013771 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013772 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13773 std::swap(N0, N1);
13774 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13775 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013776 if (!N0.hasOneUse() || !N1.hasOneUse())
13777 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013778
13779 SDValue ShAmt0 = N0.getOperand(1);
13780 if (ShAmt0.getValueType() != MVT::i8)
13781 return SDValue();
13782 SDValue ShAmt1 = N1.getOperand(1);
13783 if (ShAmt1.getValueType() != MVT::i8)
13784 return SDValue();
13785 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13786 ShAmt0 = ShAmt0.getOperand(0);
13787 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13788 ShAmt1 = ShAmt1.getOperand(0);
13789
13790 DebugLoc DL = N->getDebugLoc();
13791 unsigned Opc = X86ISD::SHLD;
13792 SDValue Op0 = N0.getOperand(0);
13793 SDValue Op1 = N1.getOperand(0);
13794 if (ShAmt0.getOpcode() == ISD::SUB) {
13795 Opc = X86ISD::SHRD;
13796 std::swap(Op0, Op1);
13797 std::swap(ShAmt0, ShAmt1);
13798 }
13799
Evan Cheng8b1190a2010-04-28 01:18:01 +000013800 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013801 if (ShAmt1.getOpcode() == ISD::SUB) {
13802 SDValue Sum = ShAmt1.getOperand(0);
13803 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013804 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13805 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13806 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13807 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013808 return DAG.getNode(Opc, DL, VT,
13809 Op0, Op1,
13810 DAG.getNode(ISD::TRUNCATE, DL,
13811 MVT::i8, ShAmt0));
13812 }
13813 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13814 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13815 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013816 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013817 return DAG.getNode(Opc, DL, VT,
13818 N0.getOperand(0), N1.getOperand(0),
13819 DAG.getNode(ISD::TRUNCATE, DL,
13820 MVT::i8, ShAmt0));
13821 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013822
Evan Cheng760d1942010-01-04 21:22:48 +000013823 return SDValue();
13824}
13825
Craig Topperb4c94572011-10-21 06:55:01 +000013826static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
13827 TargetLowering::DAGCombinerInfo &DCI,
13828 const X86Subtarget *Subtarget) {
13829 if (DCI.isBeforeLegalizeOps())
13830 return SDValue();
13831
13832 EVT VT = N->getValueType(0);
13833
13834 if (VT != MVT::i32 && VT != MVT::i64)
13835 return SDValue();
13836
13837 // Create BLSMSK instructions by finding X ^ (X-1)
13838 SDValue N0 = N->getOperand(0);
13839 SDValue N1 = N->getOperand(1);
13840 DebugLoc DL = N->getDebugLoc();
13841
13842 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13843 isAllOnes(N0.getOperand(1)))
13844 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
13845
13846 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13847 isAllOnes(N1.getOperand(1)))
13848 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
13849
13850 return SDValue();
13851}
13852
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013853/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
13854static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
13855 const X86Subtarget *Subtarget) {
13856 LoadSDNode *Ld = cast<LoadSDNode>(N);
13857 EVT RegVT = Ld->getValueType(0);
13858 EVT MemVT = Ld->getMemoryVT();
13859 DebugLoc dl = Ld->getDebugLoc();
13860 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13861
13862 ISD::LoadExtType Ext = Ld->getExtensionType();
13863
Nadav Rotemca6f2962011-09-18 19:00:23 +000013864 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013865 // shuffle. We need SSE4 for the shuffles.
13866 // TODO: It is possible to support ZExt by zeroing the undef values
13867 // during the shuffle phase or after the shuffle.
13868 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
13869 assert(MemVT != RegVT && "Cannot extend to the same type");
13870 assert(MemVT.isVector() && "Must load a vector from memory");
13871
13872 unsigned NumElems = RegVT.getVectorNumElements();
13873 unsigned RegSz = RegVT.getSizeInBits();
13874 unsigned MemSz = MemVT.getSizeInBits();
13875 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000013876 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013877 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
13878
13879 // Attempt to load the original value using a single load op.
13880 // Find a scalar type which is equal to the loaded word size.
13881 MVT SclrLoadTy = MVT::i8;
13882 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13883 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13884 MVT Tp = (MVT::SimpleValueType)tp;
13885 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
13886 SclrLoadTy = Tp;
13887 break;
13888 }
13889 }
13890
13891 // Proceed if a load word is found.
13892 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
13893
13894 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
13895 RegSz/SclrLoadTy.getSizeInBits());
13896
13897 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
13898 RegSz/MemVT.getScalarType().getSizeInBits());
13899 // Can't shuffle using an illegal type.
13900 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13901
13902 // Perform a single load.
13903 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
13904 Ld->getBasePtr(),
13905 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013906 Ld->isNonTemporal(), Ld->isInvariant(),
13907 Ld->getAlignment());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013908
13909 // Insert the word loaded into a vector.
13910 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
13911 LoadUnitVecVT, ScalarLoad);
13912
13913 // Bitcast the loaded value to a vector of the original element type, in
13914 // the size of the target vector type.
13915 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
13916 unsigned SizeRatio = RegSz/MemSz;
13917
13918 // Redistribute the loaded elements into the different locations.
13919 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13920 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
13921
13922 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
13923 DAG.getUNDEF(SlicedVec.getValueType()),
13924 ShuffleVec.data());
13925
13926 // Bitcast to the requested type.
13927 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
13928 // Replace the original load with the new sequence
13929 // and return the new chain.
13930 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
13931 return SDValue(ScalarLoad.getNode(), 1);
13932 }
13933
13934 return SDValue();
13935}
13936
Chris Lattner149a4e52008-02-22 02:09:43 +000013937/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013938static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013939 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013940 StoreSDNode *St = cast<StoreSDNode>(N);
13941 EVT VT = St->getValue().getValueType();
13942 EVT StVT = St->getMemoryVT();
13943 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013944 SDValue StoredVal = St->getOperand(1);
13945 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13946
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013947 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013948 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13949 // 128-bit ones. If in the future the cost becomes only one memory access the
13950 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013951 if (VT.getSizeInBits() == 256 &&
13952 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13953 StoredVal.getNumOperands() == 2) {
13954
13955 SDValue Value0 = StoredVal.getOperand(0);
13956 SDValue Value1 = StoredVal.getOperand(1);
13957
13958 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13959 SDValue Ptr0 = St->getBasePtr();
13960 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13961
13962 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13963 St->getPointerInfo(), St->isVolatile(),
13964 St->isNonTemporal(), St->getAlignment());
13965 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13966 St->getPointerInfo(), St->isVolatile(),
13967 St->isNonTemporal(), St->getAlignment());
13968 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13969 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013970
13971 // Optimize trunc store (of multiple scalars) to shuffle and store.
13972 // First, pack all of the elements in one place. Next, store to memory
13973 // in fewer chunks.
13974 if (St->isTruncatingStore() && VT.isVector()) {
13975 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13976 unsigned NumElems = VT.getVectorNumElements();
13977 assert(StVT != VT && "Cannot truncate to the same type");
13978 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13979 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13980
13981 // From, To sizes and ElemCount must be pow of two
13982 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013983 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000013984 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013985 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013986
Nadav Rotem614061b2011-08-10 19:30:14 +000013987 unsigned SizeRatio = FromSz / ToSz;
13988
13989 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13990
13991 // Create a type on which we perform the shuffle
13992 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13993 StVT.getScalarType(), NumElems*SizeRatio);
13994
13995 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13996
13997 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13998 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13999 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
14000
14001 // Can't shuffle using an illegal type
14002 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14003
14004 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
14005 DAG.getUNDEF(WideVec.getValueType()),
14006 ShuffleVec.data());
14007 // At this point all of the data is stored at the bottom of the
14008 // register. We now need to save it to mem.
14009
14010 // Find the largest store unit
14011 MVT StoreType = MVT::i8;
14012 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14013 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14014 MVT Tp = (MVT::SimpleValueType)tp;
14015 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
14016 StoreType = Tp;
14017 }
14018
14019 // Bitcast the original vector into a vector of store-size units
14020 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
14021 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
14022 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
14023 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
14024 SmallVector<SDValue, 8> Chains;
14025 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
14026 TLI.getPointerTy());
14027 SDValue Ptr = St->getBasePtr();
14028
14029 // Perform one or more big stores into memory.
14030 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
14031 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14032 StoreType, ShuffWide,
14033 DAG.getIntPtrConstant(i));
14034 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14035 St->getPointerInfo(), St->isVolatile(),
14036 St->isNonTemporal(), St->getAlignment());
14037 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14038 Chains.push_back(Ch);
14039 }
14040
14041 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14042 Chains.size());
14043 }
14044
14045
Chris Lattner149a4e52008-02-22 02:09:43 +000014046 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14047 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014048 // A preferable solution to the general problem is to figure out the right
14049 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014050
14051 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014052 if (VT.getSizeInBits() != 64)
14053 return SDValue();
14054
Devang Patel578efa92009-06-05 21:57:13 +000014055 const Function *F = DAG.getMachineFunction().getFunction();
14056 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014057 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000014058 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000014059 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014060 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014061 isa<LoadSDNode>(St->getValue()) &&
14062 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14063 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014064 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014065 LoadSDNode *Ld = 0;
14066 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014067 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014068 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014069 // Must be a store of a load. We currently handle two cases: the load
14070 // is a direct child, and it's under an intervening TokenFactor. It is
14071 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014072 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014073 Ld = cast<LoadSDNode>(St->getChain());
14074 else if (St->getValue().hasOneUse() &&
14075 ChainVal->getOpcode() == ISD::TokenFactor) {
14076 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014077 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014078 TokenFactorIndex = i;
14079 Ld = cast<LoadSDNode>(St->getValue());
14080 } else
14081 Ops.push_back(ChainVal->getOperand(i));
14082 }
14083 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014084
Evan Cheng536e6672009-03-12 05:59:15 +000014085 if (!Ld || !ISD::isNormalLoad(Ld))
14086 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014087
Evan Cheng536e6672009-03-12 05:59:15 +000014088 // If this is not the MMX case, i.e. we are just turning i64 load/store
14089 // into f64 load/store, avoid the transformation if there are multiple
14090 // uses of the loaded value.
14091 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14092 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014093
Evan Cheng536e6672009-03-12 05:59:15 +000014094 DebugLoc LdDL = Ld->getDebugLoc();
14095 DebugLoc StDL = N->getDebugLoc();
14096 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14097 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14098 // pair instead.
14099 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014100 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014101 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14102 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014103 Ld->isNonTemporal(), Ld->isInvariant(),
14104 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014105 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014106 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014107 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014108 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014109 Ops.size());
14110 }
Evan Cheng536e6672009-03-12 05:59:15 +000014111 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014112 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014113 St->isVolatile(), St->isNonTemporal(),
14114 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014115 }
Evan Cheng536e6672009-03-12 05:59:15 +000014116
14117 // Otherwise, lower to two pairs of 32-bit loads / stores.
14118 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014119 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14120 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014121
Owen Anderson825b72b2009-08-11 20:47:22 +000014122 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014123 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014124 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014125 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014126 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014127 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014128 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014129 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014130 MinAlign(Ld->getAlignment(), 4));
14131
14132 SDValue NewChain = LoLd.getValue(1);
14133 if (TokenFactorIndex != -1) {
14134 Ops.push_back(LoLd);
14135 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014136 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014137 Ops.size());
14138 }
14139
14140 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014141 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14142 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014143
14144 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014145 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014146 St->isVolatile(), St->isNonTemporal(),
14147 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014148 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014149 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014150 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014151 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014152 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014153 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014154 }
Dan Gohman475871a2008-07-27 21:46:04 +000014155 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014156}
14157
Duncan Sands17470be2011-09-22 20:15:48 +000014158/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14159/// and return the operands for the horizontal operation in LHS and RHS. A
14160/// horizontal operation performs the binary operation on successive elements
14161/// of its first operand, then on successive elements of its second operand,
14162/// returning the resulting values in a vector. For example, if
14163/// A = < float a0, float a1, float a2, float a3 >
14164/// and
14165/// B = < float b0, float b1, float b2, float b3 >
14166/// then the result of doing a horizontal operation on A and B is
14167/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14168/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14169/// A horizontal-op B, for some already available A and B, and if so then LHS is
14170/// set to A, RHS to B, and the routine returns 'true'.
14171/// Note that the binary operation should have the property that if one of the
14172/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014173static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000014174 // Look for the following pattern: if
14175 // A = < float a0, float a1, float a2, float a3 >
14176 // B = < float b0, float b1, float b2, float b3 >
14177 // and
14178 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14179 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14180 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14181 // which is A horizontal-op B.
14182
14183 // At least one of the operands should be a vector shuffle.
14184 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14185 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14186 return false;
14187
14188 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000014189
14190 assert((VT.is128BitVector() || VT.is256BitVector()) &&
14191 "Unsupported vector type for horizontal add/sub");
14192
14193 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
14194 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000014195 unsigned NumElts = VT.getVectorNumElements();
14196 unsigned NumLanes = VT.getSizeInBits()/128;
14197 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000014198 assert((NumLaneElts % 2 == 0) &&
14199 "Vector type should have an even number of elements in each lane");
14200 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000014201
14202 // View LHS in the form
14203 // LHS = VECTOR_SHUFFLE A, B, LMask
14204 // If LHS is not a shuffle then pretend it is the shuffle
14205 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14206 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14207 // type VT.
14208 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000014209 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014210 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14211 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14212 A = LHS.getOperand(0);
14213 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14214 B = LHS.getOperand(1);
14215 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
14216 } else {
14217 if (LHS.getOpcode() != ISD::UNDEF)
14218 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014219 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014220 LMask[i] = i;
14221 }
14222
14223 // Likewise, view RHS in the form
14224 // RHS = VECTOR_SHUFFLE C, D, RMask
14225 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000014226 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014227 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14228 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14229 C = RHS.getOperand(0);
14230 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14231 D = RHS.getOperand(1);
14232 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
14233 } else {
14234 if (RHS.getOpcode() != ISD::UNDEF)
14235 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000014236 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000014237 RMask[i] = i;
14238 }
14239
14240 // Check that the shuffles are both shuffling the same vectors.
14241 if (!(A == C && B == D) && !(A == D && B == C))
14242 return false;
14243
14244 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14245 if (!A.getNode() && !B.getNode())
14246 return false;
14247
14248 // If A and B occur in reverse order in RHS, then "swap" them (which means
14249 // rewriting the mask).
14250 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000014251 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000014252
14253 // At this point LHS and RHS are equivalent to
14254 // LHS = VECTOR_SHUFFLE A, B, LMask
14255 // RHS = VECTOR_SHUFFLE A, B, RMask
14256 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000014257 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000014258 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000014259
Craig Topperf8363302011-12-02 08:18:41 +000014260 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014261 if (LIdx < 0 || RIdx < 0 ||
14262 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
14263 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000014264 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000014265
Craig Topperf8363302011-12-02 08:18:41 +000014266 // Check that successive elements are being operated on. If not, this is
14267 // not a horizontal operation.
14268 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
14269 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000014270 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000014271 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000014272 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000014273 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000014274 }
14275
14276 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14277 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14278 return true;
14279}
14280
14281/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14282static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14283 const X86Subtarget *Subtarget) {
14284 EVT VT = N->getValueType(0);
14285 SDValue LHS = N->getOperand(0);
14286 SDValue RHS = N->getOperand(1);
14287
14288 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topper138a5c62011-12-02 07:16:01 +000014289 if (((Subtarget->hasSSE3orAVX() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
14290 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014291 isHorizontalBinOp(LHS, RHS, true))
14292 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14293 return SDValue();
14294}
14295
14296/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14297static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14298 const X86Subtarget *Subtarget) {
14299 EVT VT = N->getValueType(0);
14300 SDValue LHS = N->getOperand(0);
14301 SDValue RHS = N->getOperand(1);
14302
14303 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topper138a5c62011-12-02 07:16:01 +000014304 if (((Subtarget->hasSSE3orAVX() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
14305 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014306 isHorizontalBinOp(LHS, RHS, false))
14307 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14308 return SDValue();
14309}
14310
Chris Lattner6cf73262008-01-25 06:14:17 +000014311/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14312/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014313static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014314 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14315 // F[X]OR(0.0, x) -> x
14316 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014317 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14318 if (C->getValueAPF().isPosZero())
14319 return N->getOperand(1);
14320 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14321 if (C->getValueAPF().isPosZero())
14322 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014323 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014324}
14325
14326/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014327static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014328 // FAND(0.0, x) -> 0.0
14329 // FAND(x, 0.0) -> 0.0
14330 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14331 if (C->getValueAPF().isPosZero())
14332 return N->getOperand(0);
14333 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14334 if (C->getValueAPF().isPosZero())
14335 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014336 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014337}
14338
Dan Gohmane5af2d32009-01-29 01:59:02 +000014339static SDValue PerformBTCombine(SDNode *N,
14340 SelectionDAG &DAG,
14341 TargetLowering::DAGCombinerInfo &DCI) {
14342 // BT ignores high bits in the bit index operand.
14343 SDValue Op1 = N->getOperand(1);
14344 if (Op1.hasOneUse()) {
14345 unsigned BitWidth = Op1.getValueSizeInBits();
14346 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14347 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014348 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14349 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014350 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014351 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14352 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14353 DCI.CommitTargetLoweringOpt(TLO);
14354 }
14355 return SDValue();
14356}
Chris Lattner83e6c992006-10-04 06:57:07 +000014357
Eli Friedman7a5e5552009-06-07 06:52:44 +000014358static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14359 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014360 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014361 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014362 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014363 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014364 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014365 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014366 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014367 }
14368 return SDValue();
14369}
14370
Evan Cheng2e489c42009-12-16 00:53:11 +000014371static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14372 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14373 // (and (i32 x86isd::setcc_carry), 1)
14374 // This eliminates the zext. This transformation is necessary because
14375 // ISD::SETCC is always legalized to i8.
14376 DebugLoc dl = N->getDebugLoc();
14377 SDValue N0 = N->getOperand(0);
14378 EVT VT = N->getValueType(0);
14379 if (N0.getOpcode() == ISD::AND &&
14380 N0.hasOneUse() &&
14381 N0.getOperand(0).hasOneUse()) {
14382 SDValue N00 = N0.getOperand(0);
14383 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14384 return SDValue();
14385 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14386 if (!C || C->getZExtValue() != 1)
14387 return SDValue();
14388 return DAG.getNode(ISD::AND, dl, VT,
14389 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14390 N00.getOperand(0), N00.getOperand(1)),
14391 DAG.getConstant(1, VT));
14392 }
14393
14394 return SDValue();
14395}
14396
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014397// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14398static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14399 unsigned X86CC = N->getConstantOperandVal(0);
14400 SDValue EFLAG = N->getOperand(1);
14401 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014402
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014403 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14404 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14405 // cases.
14406 if (X86CC == X86::COND_B)
14407 return DAG.getNode(ISD::AND, DL, MVT::i8,
14408 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14409 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14410 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014411
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014412 return SDValue();
14413}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014414
Benjamin Kramer1396c402011-06-18 11:09:41 +000014415static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14416 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014417 SDValue Op0 = N->getOperand(0);
14418 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14419 // a 32-bit target where SSE doesn't support i64->FP operations.
14420 if (Op0.getOpcode() == ISD::LOAD) {
14421 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14422 EVT VT = Ld->getValueType(0);
14423 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14424 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14425 !XTLI->getSubtarget()->is64Bit() &&
14426 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014427 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14428 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014429 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14430 return FILDChain;
14431 }
14432 }
14433 return SDValue();
14434}
14435
Chris Lattner23a01992010-12-20 01:37:09 +000014436// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14437static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14438 X86TargetLowering::DAGCombinerInfo &DCI) {
14439 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14440 // the result is either zero or one (depending on the input carry bit).
14441 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14442 if (X86::isZeroNode(N->getOperand(0)) &&
14443 X86::isZeroNode(N->getOperand(1)) &&
14444 // We don't have a good way to replace an EFLAGS use, so only do this when
14445 // dead right now.
14446 SDValue(N, 1).use_empty()) {
14447 DebugLoc DL = N->getDebugLoc();
14448 EVT VT = N->getValueType(0);
14449 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14450 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14451 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14452 DAG.getConstant(X86::COND_B,MVT::i8),
14453 N->getOperand(2)),
14454 DAG.getConstant(1, VT));
14455 return DCI.CombineTo(N, Res1, CarryOut);
14456 }
14457
14458 return SDValue();
14459}
14460
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014461// fold (add Y, (sete X, 0)) -> adc 0, Y
14462// (add Y, (setne X, 0)) -> sbb -1, Y
14463// (sub (sete X, 0), Y) -> sbb 0, Y
14464// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014465static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014466 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014467
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014468 // Look through ZExts.
14469 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14470 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14471 return SDValue();
14472
14473 SDValue SetCC = Ext.getOperand(0);
14474 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14475 return SDValue();
14476
14477 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14478 if (CC != X86::COND_E && CC != X86::COND_NE)
14479 return SDValue();
14480
14481 SDValue Cmp = SetCC.getOperand(1);
14482 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014483 !X86::isZeroNode(Cmp.getOperand(1)) ||
14484 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014485 return SDValue();
14486
14487 SDValue CmpOp0 = Cmp.getOperand(0);
14488 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14489 DAG.getConstant(1, CmpOp0.getValueType()));
14490
14491 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14492 if (CC == X86::COND_NE)
14493 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14494 DL, OtherVal.getValueType(), OtherVal,
14495 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14496 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14497 DL, OtherVal.getValueType(), OtherVal,
14498 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14499}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014500
Craig Topper54f952a2011-11-19 09:02:40 +000014501/// PerformADDCombine - Do target-specific dag combines on integer adds.
14502static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
14503 const X86Subtarget *Subtarget) {
14504 EVT VT = N->getValueType(0);
14505 SDValue Op0 = N->getOperand(0);
14506 SDValue Op1 = N->getOperand(1);
14507
14508 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperb72039c2011-11-30 09:10:50 +000014509 if (((Subtarget->hasSSSE3orAVX() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
14510 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000014511 isHorizontalBinOp(Op0, Op1, true))
14512 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
14513
14514 return OptimizeConditionalInDecrement(N, DAG);
14515}
14516
14517static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
14518 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014519 SDValue Op0 = N->getOperand(0);
14520 SDValue Op1 = N->getOperand(1);
14521
14522 // X86 can't encode an immediate LHS of a sub. See if we can push the
14523 // negation into a preceding instruction.
14524 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014525 // If the RHS of the sub is a XOR with one use and a constant, invert the
14526 // immediate. Then add one to the LHS of the sub so we can turn
14527 // X-Y -> X+~Y+1, saving one register.
14528 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14529 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014530 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014531 EVT VT = Op0.getValueType();
14532 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14533 Op1.getOperand(0),
14534 DAG.getConstant(~XorC, VT));
14535 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014536 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014537 }
14538 }
14539
Craig Topper54f952a2011-11-19 09:02:40 +000014540 // Try to synthesize horizontal adds from adds of shuffles.
14541 EVT VT = N->getValueType(0);
Craig Topperb72039c2011-11-30 09:10:50 +000014542 if (((Subtarget->hasSSSE3orAVX() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
14543 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
14544 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000014545 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
14546
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014547 return OptimizeConditionalInDecrement(N, DAG);
14548}
14549
Dan Gohman475871a2008-07-27 21:46:04 +000014550SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014551 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014552 SelectionDAG &DAG = DCI.DAG;
14553 switch (N->getOpcode()) {
14554 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014555 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014556 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014557 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014558 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014559 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Craig Topper54f952a2011-11-19 09:02:40 +000014560 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
14561 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000014562 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014563 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014564 case ISD::SHL:
14565 case ISD::SRA:
14566 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014567 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014568 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014569 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014570 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014571 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014572 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014573 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14574 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014575 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014576 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14577 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014578 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014579 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014580 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014581 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014582 case X86ISD::SHUFPS: // Handle all target specific shuffles
14583 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014584 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000014585 case X86ISD::UNPCKH:
14586 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014587 case X86ISD::MOVHLPS:
14588 case X86ISD::MOVLHPS:
14589 case X86ISD::PSHUFD:
14590 case X86ISD::PSHUFHW:
14591 case X86ISD::PSHUFLW:
14592 case X86ISD::MOVSS:
14593 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000014594 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000014595 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014596 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014597 }
14598
Dan Gohman475871a2008-07-27 21:46:04 +000014599 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014600}
14601
Evan Chenge5b51ac2010-04-17 06:13:15 +000014602/// isTypeDesirableForOp - Return true if the target has native support for
14603/// the specified value type and it is 'desirable' to use the type for the
14604/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14605/// instruction encodings are longer and some i16 instructions are slow.
14606bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14607 if (!isTypeLegal(VT))
14608 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014609 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014610 return true;
14611
14612 switch (Opc) {
14613 default:
14614 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014615 case ISD::LOAD:
14616 case ISD::SIGN_EXTEND:
14617 case ISD::ZERO_EXTEND:
14618 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014619 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014620 case ISD::SRL:
14621 case ISD::SUB:
14622 case ISD::ADD:
14623 case ISD::MUL:
14624 case ISD::AND:
14625 case ISD::OR:
14626 case ISD::XOR:
14627 return false;
14628 }
14629}
14630
14631/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014632/// beneficial for dag combiner to promote the specified node. If true, it
14633/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014634bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014635 EVT VT = Op.getValueType();
14636 if (VT != MVT::i16)
14637 return false;
14638
Evan Cheng4c26e932010-04-19 19:29:22 +000014639 bool Promote = false;
14640 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014641 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014642 default: break;
14643 case ISD::LOAD: {
14644 LoadSDNode *LD = cast<LoadSDNode>(Op);
14645 // If the non-extending load has a single use and it's not live out, then it
14646 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014647 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14648 Op.hasOneUse()*/) {
14649 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14650 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14651 // The only case where we'd want to promote LOAD (rather then it being
14652 // promoted as an operand is when it's only use is liveout.
14653 if (UI->getOpcode() != ISD::CopyToReg)
14654 return false;
14655 }
14656 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014657 Promote = true;
14658 break;
14659 }
14660 case ISD::SIGN_EXTEND:
14661 case ISD::ZERO_EXTEND:
14662 case ISD::ANY_EXTEND:
14663 Promote = true;
14664 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014665 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014666 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014667 SDValue N0 = Op.getOperand(0);
14668 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014669 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014670 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014671 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014672 break;
14673 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014674 case ISD::ADD:
14675 case ISD::MUL:
14676 case ISD::AND:
14677 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014678 case ISD::XOR:
14679 Commute = true;
14680 // fallthrough
14681 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014682 SDValue N0 = Op.getOperand(0);
14683 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014684 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014685 return false;
14686 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014687 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014688 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014689 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014690 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014691 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014692 }
14693 }
14694
14695 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014696 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014697}
14698
Evan Cheng60c07e12006-07-05 22:17:51 +000014699//===----------------------------------------------------------------------===//
14700// X86 Inline Assembly Support
14701//===----------------------------------------------------------------------===//
14702
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014703// Helper to match a string separated by whitespace.
14704static bool END_WITH_NULL matchAsm(StringRef s, ...) {
14705 va_list ap;
14706 va_start(ap, s);
14707 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
14708
14709 while (const char *p = va_arg(ap, const char *)) {
14710 StringRef piece(p);
14711 if (!s.startswith(piece)) { // Check if the piece matches.
14712 va_end(ap);
14713 return false;
14714 }
14715
14716 s = s.substr(piece.size());
14717 StringRef::size_type i = s.find_first_not_of(" \t");
14718 if (i == 0) { // We matched a prefix.
14719 va_end(ap);
14720 return false;
14721 }
14722 s = s.substr(i);
14723 }
14724
14725 va_end(ap);
14726 return s.empty();
14727}
14728
Chris Lattnerb8105652009-07-20 17:51:36 +000014729bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14730 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014731
14732 std::string AsmStr = IA->getAsmString();
14733
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014734 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
14735 if (!Ty || Ty->getBitWidth() % 16 != 0)
14736 return false;
14737
Chris Lattnerb8105652009-07-20 17:51:36 +000014738 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014739 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014740 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014741
14742 switch (AsmPieces.size()) {
14743 default: return false;
14744 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014745 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014746 // we will turn this bswap into something that will be lowered to logical
14747 // ops instead of emitting the bswap asm. For now, we don't support 486 or
14748 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014749 // bswap $0
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014750 if (matchAsm(AsmPieces[0], "bswap", "$0", NULL) ||
14751 matchAsm(AsmPieces[0], "bswapl", "$0", NULL) ||
14752 matchAsm(AsmPieces[0], "bswapq", "$0", NULL) ||
14753 matchAsm(AsmPieces[0], "bswap", "${0:q}", NULL) ||
14754 matchAsm(AsmPieces[0], "bswapl", "${0:q}", NULL) ||
14755 matchAsm(AsmPieces[0], "bswapq", "${0:q}", NULL)) {
Chris Lattnerb8105652009-07-20 17:51:36 +000014756 // No need to check constraints, nothing other than the equivalent of
14757 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000014758 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014759 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014760
Chris Lattnerb8105652009-07-20 17:51:36 +000014761 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014762 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014763 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
14764 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}", NULL) ||
14765 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}", NULL))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000014766 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014767 const std::string &ConstraintsStr = IA->getConstraintString();
14768 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014769 std::sort(AsmPieces.begin(), AsmPieces.end());
14770 if (AsmPieces.size() == 4 &&
14771 AsmPieces[0] == "~{cc}" &&
14772 AsmPieces[1] == "~{dirflag}" &&
14773 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014774 AsmPieces[3] == "~{fpsr}")
14775 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014776 }
14777 break;
14778 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014779 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014780 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
14781 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}", NULL) &&
14782 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0", NULL) &&
14783 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}", NULL)) {
14784 AsmPieces.clear();
14785 const std::string &ConstraintsStr = IA->getConstraintString();
14786 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
14787 std::sort(AsmPieces.begin(), AsmPieces.end());
14788 if (AsmPieces.size() == 4 &&
14789 AsmPieces[0] == "~{cc}" &&
14790 AsmPieces[1] == "~{dirflag}" &&
14791 AsmPieces[2] == "~{flags}" &&
14792 AsmPieces[3] == "~{fpsr}")
14793 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014794 }
Evan Cheng55d42002011-01-08 01:24:27 +000014795
14796 if (CI->getType()->isIntegerTy(64)) {
14797 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14798 if (Constraints.size() >= 2 &&
14799 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14800 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14801 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramere6cddb72011-12-17 14:36:05 +000014802 if (matchAsm(AsmPieces[0], "bswap", "%eax", NULL) &&
14803 matchAsm(AsmPieces[1], "bswap", "%edx", NULL) &&
14804 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx", NULL))
14805 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014806 }
14807 }
14808 break;
14809 }
14810 return false;
14811}
14812
14813
14814
Chris Lattnerf4dff842006-07-11 02:54:03 +000014815/// getConstraintType - Given a constraint letter, return the type of
14816/// constraint it is for this target.
14817X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014818X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14819 if (Constraint.size() == 1) {
14820 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014821 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014822 case 'q':
14823 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014824 case 'f':
14825 case 't':
14826 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014827 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014828 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014829 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014830 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014831 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014832 case 'a':
14833 case 'b':
14834 case 'c':
14835 case 'd':
14836 case 'S':
14837 case 'D':
14838 case 'A':
14839 return C_Register;
14840 case 'I':
14841 case 'J':
14842 case 'K':
14843 case 'L':
14844 case 'M':
14845 case 'N':
14846 case 'G':
14847 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014848 case 'e':
14849 case 'Z':
14850 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014851 default:
14852 break;
14853 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014854 }
Chris Lattner4234f572007-03-25 02:14:49 +000014855 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014856}
14857
John Thompson44ab89e2010-10-29 17:29:13 +000014858/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014859/// This object must already have been set up with the operand type
14860/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014861TargetLowering::ConstraintWeight
14862 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014863 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014864 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014865 Value *CallOperandVal = info.CallOperandVal;
14866 // If we don't have a value, we can't do a match,
14867 // but allow it at the lowest weight.
14868 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014869 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014870 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014871 // Look at the constraint type.
14872 switch (*constraint) {
14873 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014874 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14875 case 'R':
14876 case 'q':
14877 case 'Q':
14878 case 'a':
14879 case 'b':
14880 case 'c':
14881 case 'd':
14882 case 'S':
14883 case 'D':
14884 case 'A':
14885 if (CallOperandVal->getType()->isIntegerTy())
14886 weight = CW_SpecificReg;
14887 break;
14888 case 'f':
14889 case 't':
14890 case 'u':
14891 if (type->isFloatingPointTy())
14892 weight = CW_SpecificReg;
14893 break;
14894 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014895 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014896 weight = CW_SpecificReg;
14897 break;
14898 case 'x':
14899 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014900 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014901 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014902 break;
14903 case 'I':
14904 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14905 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014906 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014907 }
14908 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014909 case 'J':
14910 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14911 if (C->getZExtValue() <= 63)
14912 weight = CW_Constant;
14913 }
14914 break;
14915 case 'K':
14916 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14917 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14918 weight = CW_Constant;
14919 }
14920 break;
14921 case 'L':
14922 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14923 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14924 weight = CW_Constant;
14925 }
14926 break;
14927 case 'M':
14928 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14929 if (C->getZExtValue() <= 3)
14930 weight = CW_Constant;
14931 }
14932 break;
14933 case 'N':
14934 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14935 if (C->getZExtValue() <= 0xff)
14936 weight = CW_Constant;
14937 }
14938 break;
14939 case 'G':
14940 case 'C':
14941 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14942 weight = CW_Constant;
14943 }
14944 break;
14945 case 'e':
14946 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14947 if ((C->getSExtValue() >= -0x80000000LL) &&
14948 (C->getSExtValue() <= 0x7fffffffLL))
14949 weight = CW_Constant;
14950 }
14951 break;
14952 case 'Z':
14953 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14954 if (C->getZExtValue() <= 0xffffffff)
14955 weight = CW_Constant;
14956 }
14957 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014958 }
14959 return weight;
14960}
14961
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014962/// LowerXConstraint - try to replace an X constraint, which matches anything,
14963/// with another that has more specific requirements based on the type of the
14964/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014965const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014966LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014967 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14968 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014969 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014970 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014971 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014972 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014973 return "x";
14974 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014975
Chris Lattner5e764232008-04-26 23:02:14 +000014976 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014977}
14978
Chris Lattner48884cd2007-08-25 00:47:38 +000014979/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14980/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014981void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014982 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014983 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014984 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014985 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014986
Eric Christopher100c8332011-06-02 23:16:42 +000014987 // Only support length 1 constraints for now.
14988 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014989
Eric Christopher100c8332011-06-02 23:16:42 +000014990 char ConstraintLetter = Constraint[0];
14991 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014992 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014993 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014994 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014995 if (C->getZExtValue() <= 31) {
14996 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014997 break;
14998 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014999 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015000 return;
Evan Cheng364091e2008-09-22 23:57:37 +000015001 case 'J':
15002 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015003 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000015004 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15005 break;
15006 }
15007 }
15008 return;
15009 case 'K':
15010 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015011 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000015012 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15013 break;
15014 }
15015 }
15016 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000015017 case 'N':
15018 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015019 if (C->getZExtValue() <= 255) {
15020 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015021 break;
15022 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000015023 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015024 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000015025 case 'e': {
15026 // 32-bit signed value
15027 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015028 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15029 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015030 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015031 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000015032 break;
15033 }
15034 // FIXME gcc accepts some relocatable values here too, but only in certain
15035 // memory models; it's complicated.
15036 }
15037 return;
15038 }
15039 case 'Z': {
15040 // 32-bit unsigned value
15041 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015042 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15043 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015044 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15045 break;
15046 }
15047 }
15048 // FIXME gcc accepts some relocatable values here too, but only in certain
15049 // memory models; it's complicated.
15050 return;
15051 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015052 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015053 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000015054 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015055 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015056 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000015057 break;
15058 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015059
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015060 // In any sort of PIC mode addresses need to be computed at runtime by
15061 // adding in a register or some sort of table lookup. These can't
15062 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000015063 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015064 return;
15065
Chris Lattnerdc43a882007-05-03 16:52:29 +000015066 // If we are in non-pic codegen mode, we allow the address of a global (with
15067 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000015068 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015069 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000015070
Chris Lattner49921962009-05-08 18:23:14 +000015071 // Match either (GA), (GA+C), (GA+C1+C2), etc.
15072 while (1) {
15073 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15074 Offset += GA->getOffset();
15075 break;
15076 } else if (Op.getOpcode() == ISD::ADD) {
15077 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15078 Offset += C->getZExtValue();
15079 Op = Op.getOperand(0);
15080 continue;
15081 }
15082 } else if (Op.getOpcode() == ISD::SUB) {
15083 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15084 Offset += -C->getZExtValue();
15085 Op = Op.getOperand(0);
15086 continue;
15087 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015088 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015089
Chris Lattner49921962009-05-08 18:23:14 +000015090 // Otherwise, this isn't something we can handle, reject it.
15091 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015092 }
Eric Christopherfd179292009-08-27 18:07:15 +000015093
Dan Gohman46510a72010-04-15 01:51:59 +000015094 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015095 // If we require an extra load to get this address, as in PIC mode, we
15096 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015097 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15098 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015099 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015100
Devang Patel0d881da2010-07-06 22:08:15 +000015101 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15102 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015103 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015104 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015105 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015106
Gabor Greifba36cb52008-08-28 21:40:38 +000015107 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015108 Ops.push_back(Result);
15109 return;
15110 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015111 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015112}
15113
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015114std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015115X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015116 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015117 // First, see if this is a constraint that directly corresponds to an LLVM
15118 // register class.
15119 if (Constraint.size() == 1) {
15120 // GCC Constraint Letters
15121 switch (Constraint[0]) {
15122 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015123 // TODO: Slight differences here in allocation order and leaving
15124 // RIP in the class. Do they matter any more here than they do
15125 // in the normal allocation?
15126 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15127 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015128 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015129 return std::make_pair(0U, X86::GR32RegisterClass);
15130 else if (VT == MVT::i16)
15131 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015132 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015133 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015134 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015135 return std::make_pair(0U, X86::GR64RegisterClass);
15136 break;
15137 }
15138 // 32-bit fallthrough
15139 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015140 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015141 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
15142 else if (VT == MVT::i16)
15143 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015144 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015145 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
15146 else if (VT == MVT::i64)
15147 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15148 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015149 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015150 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015151 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015152 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015153 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015154 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015155 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015156 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015157 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015158 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015159 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015160 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15161 if (VT == MVT::i16)
15162 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15163 if (VT == MVT::i32 || !Subtarget->is64Bit())
15164 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15165 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015166 case 'f': // FP Stack registers.
15167 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15168 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015169 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015170 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015171 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015172 return std::make_pair(0U, X86::RFP64RegisterClass);
15173 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015174 case 'y': // MMX_REGS if MMX allowed.
15175 if (!Subtarget->hasMMX()) break;
15176 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015177 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015178 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015179 // FALL THROUGH.
15180 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015181 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015182
Owen Anderson825b72b2009-08-11 20:47:22 +000015183 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015184 default: break;
15185 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015186 case MVT::f32:
15187 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015188 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015189 case MVT::f64:
15190 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015191 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015192 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015193 case MVT::v16i8:
15194 case MVT::v8i16:
15195 case MVT::v4i32:
15196 case MVT::v2i64:
15197 case MVT::v4f32:
15198 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015199 return std::make_pair(0U, X86::VR128RegisterClass);
15200 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015201 break;
15202 }
15203 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015204
Chris Lattnerf76d1802006-07-31 23:26:50 +000015205 // Use the default implementation in TargetLowering to convert the register
15206 // constraint into a member of a register class.
15207 std::pair<unsigned, const TargetRegisterClass*> Res;
15208 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015209
15210 // Not found as a standard register?
15211 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015212 // Map st(0) -> st(7) -> ST0
15213 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15214 tolower(Constraint[1]) == 's' &&
15215 tolower(Constraint[2]) == 't' &&
15216 Constraint[3] == '(' &&
15217 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15218 Constraint[5] == ')' &&
15219 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015220
Chris Lattner56d77c72009-09-13 22:41:48 +000015221 Res.first = X86::ST0+Constraint[4]-'0';
15222 Res.second = X86::RFP80RegisterClass;
15223 return Res;
15224 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015225
Chris Lattner56d77c72009-09-13 22:41:48 +000015226 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015227 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015228 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015229 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015230 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015231 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015232
15233 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015234 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015235 Res.first = X86::EFLAGS;
15236 Res.second = X86::CCRRegisterClass;
15237 return Res;
15238 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015239
Dale Johannesen330169f2008-11-13 21:52:36 +000015240 // 'A' means EAX + EDX.
15241 if (Constraint == "A") {
15242 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015243 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015244 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015245 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015246 return Res;
15247 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015248
Chris Lattnerf76d1802006-07-31 23:26:50 +000015249 // Otherwise, check to see if this is a register class of the wrong value
15250 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15251 // turn into {ax},{dx}.
15252 if (Res.second->hasType(VT))
15253 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015254
Chris Lattnerf76d1802006-07-31 23:26:50 +000015255 // All of the single-register GCC register classes map their values onto
15256 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15257 // really want an 8-bit or 32-bit register, map to the appropriate register
15258 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015259 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015260 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015261 unsigned DestReg = 0;
15262 switch (Res.first) {
15263 default: break;
15264 case X86::AX: DestReg = X86::AL; break;
15265 case X86::DX: DestReg = X86::DL; break;
15266 case X86::CX: DestReg = X86::CL; break;
15267 case X86::BX: DestReg = X86::BL; break;
15268 }
15269 if (DestReg) {
15270 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015271 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015272 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015273 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015274 unsigned DestReg = 0;
15275 switch (Res.first) {
15276 default: break;
15277 case X86::AX: DestReg = X86::EAX; break;
15278 case X86::DX: DestReg = X86::EDX; break;
15279 case X86::CX: DestReg = X86::ECX; break;
15280 case X86::BX: DestReg = X86::EBX; break;
15281 case X86::SI: DestReg = X86::ESI; break;
15282 case X86::DI: DestReg = X86::EDI; break;
15283 case X86::BP: DestReg = X86::EBP; break;
15284 case X86::SP: DestReg = X86::ESP; break;
15285 }
15286 if (DestReg) {
15287 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015288 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015289 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015290 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015291 unsigned DestReg = 0;
15292 switch (Res.first) {
15293 default: break;
15294 case X86::AX: DestReg = X86::RAX; break;
15295 case X86::DX: DestReg = X86::RDX; break;
15296 case X86::CX: DestReg = X86::RCX; break;
15297 case X86::BX: DestReg = X86::RBX; break;
15298 case X86::SI: DestReg = X86::RSI; break;
15299 case X86::DI: DestReg = X86::RDI; break;
15300 case X86::BP: DestReg = X86::RBP; break;
15301 case X86::SP: DestReg = X86::RSP; break;
15302 }
15303 if (DestReg) {
15304 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015305 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015306 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015307 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015308 } else if (Res.second == X86::FR32RegisterClass ||
15309 Res.second == X86::FR64RegisterClass ||
15310 Res.second == X86::VR128RegisterClass) {
15311 // Handle references to XMM physical registers that got mapped into the
15312 // wrong class. This can happen with constraints like {xmm0} where the
15313 // target independent register mapper will just pick the first match it can
15314 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015315 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015316 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015317 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015318 Res.second = X86::FR64RegisterClass;
15319 else if (X86::VR128RegisterClass->hasType(VT))
15320 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015321 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015322
Chris Lattnerf76d1802006-07-31 23:26:50 +000015323 return Res;
15324}