blob: cf03c30399d61b51ffd42101d17124700a82beba [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"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000048#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000054#include "llvm/Target/TargetOptions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000056using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000057
Evan Chengb1712452010-01-27 06:25:16 +000058STATISTIC(NumTailCalls, "Number of tail calls");
59
Evan Cheng10e86422008-04-25 19:11:04 +000060// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000061static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000062 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000063
David Greenea5f26012011-02-07 19:36:54 +000064static SDValue Insert128BitVector(SDValue Result,
65 SDValue Vec,
66 SDValue Idx,
67 SelectionDAG &DAG,
68 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000069
David Greenea5f26012011-02-07 19:36:54 +000070static SDValue Extract128BitVector(SDValue Vec,
71 SDValue Idx,
72 SelectionDAG &DAG,
73 DebugLoc dl);
74
75/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
76/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000077/// simple subregister reference. Idx is an index in the 128 bits we
78/// want. It need not be aligned to a 128-bit bounday. That makes
79/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000080static SDValue Extract128BitVector(SDValue Vec,
81 SDValue Idx,
82 SelectionDAG &DAG,
83 DebugLoc dl) {
84 EVT VT = Vec.getValueType();
85 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000086 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000087 int Factor = VT.getSizeInBits()/128;
88 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
89 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000090
91 // Extract from UNDEF is UNDEF.
92 if (Vec.getOpcode() == ISD::UNDEF)
93 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
94
95 if (isa<ConstantSDNode>(Idx)) {
96 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
97
98 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
99 // we can match to VEXTRACTF128.
100 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
101
102 // This is the index of the first element of the 128-bit chunk
103 // we want.
104 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
105 * ElemsPerChunk);
106
107 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000108 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
109 VecIdx);
110
111 return Result;
112 }
113
114 return SDValue();
115}
116
117/// Generate a DAG to put 128-bits into a vector > 128 bits. This
118/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000119/// simple superregister reference. Idx is an index in the 128 bits
120/// we want. It need not be aligned to a 128-bit bounday. That makes
121/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000122static SDValue Insert128BitVector(SDValue Result,
123 SDValue Vec,
124 SDValue Idx,
125 SelectionDAG &DAG,
126 DebugLoc dl) {
127 if (isa<ConstantSDNode>(Idx)) {
128 EVT VT = Vec.getValueType();
129 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
130
131 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000132 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000133 EVT ResultVT = Result.getValueType();
134
135 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000136 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000137
138 // This is the index of the first element of the 128-bit chunk
139 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000140 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000141 * ElemsPerChunk);
142
143 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000144 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
145 VecIdx);
146 return Result;
147 }
148
149 return SDValue();
150}
151
Chris Lattnerf0144122009-07-28 03:13:23 +0000152static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000153 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
154 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000155
Evan Cheng2bffee22011-02-01 01:14:13 +0000156 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000157 if (is64Bit)
158 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000159 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000160 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000161
Evan Cheng203576a2011-07-20 19:50:42 +0000162 if (Subtarget->isTargetELF())
163 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000164 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000165 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000166 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000167}
168
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000169X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000170 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000171 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000172 X86ScalarSSEf64 = Subtarget->hasXMMInt();
173 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000175
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000176 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000177 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000178
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000179 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000180 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181
182 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000183 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000184 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
185 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000186
Eric Christopherde5e1012011-03-11 01:05:58 +0000187 // For 64-bit since we have so many registers use the ILP scheduler, for
188 // 32-bit code use the register pressure specific scheduling.
189 if (Subtarget->is64Bit())
190 setSchedulingPreference(Sched::ILP);
191 else
192 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000193 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000194
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000195 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000196 // Setup Windows compiler runtime calls.
197 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000198 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000199 setLibcallName(RTLIB::SREM_I64, "_allrem");
200 setLibcallName(RTLIB::UREM_I64, "_aullrem");
201 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000202 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000203 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000204 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000205 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000206 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
208 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000209 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
210 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000211 }
212
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000213 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000214 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000215 setUseUnderscoreSetJmp(false);
216 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000217 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000218 // MS runtime is weird: it exports _setjmp, but longjmp!
219 setUseUnderscoreSetJmp(true);
220 setUseUnderscoreLongJmp(false);
221 } else {
222 setUseUnderscoreSetJmp(true);
223 setUseUnderscoreLongJmp(true);
224 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000225
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000228 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000232
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000234
Scott Michelfdc40a02009-02-17 22:15:04 +0000235 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000237 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000239 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
241 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000242
243 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
246 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
249 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000250
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000251 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
252 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
255 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000256
Evan Cheng25ab6902006-09-08 06:48:29 +0000257 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
259 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000260 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000261 // We have an algorithm for SSE2->double, and we turn this into a
262 // 64-bit FILD followed by conditional FADD for other targets.
263 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000264 // We have an algorithm for SSE2, and we turn this into a 64-bit
265 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000266 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000267 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000268
269 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
270 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
272 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000273
Devang Patel6a784892009-06-05 18:48:29 +0000274 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 // SSE has no i16 to fp conversion, only i32
276 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000278 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000280 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
282 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000283 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000284 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
286 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000287 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000288
Dale Johannesen73328d12007-09-19 23:55:34 +0000289 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
290 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
292 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000293
Evan Cheng02568ff2006-01-30 22:13:22 +0000294 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
295 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
297 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000298
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000299 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000301 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000303 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
305 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306 }
307
308 // Handle FP_TO_UINT by promoting the destination to a larger signed
309 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
312 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000313
Evan Cheng25ab6902006-09-08 06:48:29 +0000314 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
316 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000317 } else if (!UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000318 // Since AVX is a superset of SSE3, only check for SSE here.
319 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 // Expand FP_TO_UINT into a select.
321 // FIXME: We would like to use a Custom expander here eventually to do
322 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000325 // With SSE3 we can use fisttpll to convert to a signed i64; without
326 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000328 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000329
Chris Lattner399610a2006-12-05 18:22:22 +0000330 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000331 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000332 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
333 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000334 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000335 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000336 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000337 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000338 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000339 }
Chris Lattner21f66852005-12-23 05:15:23 +0000340
Dan Gohmanb00ee212008-02-18 19:34:53 +0000341 // Scalar integer divide and remainder are lowered to use operations that
342 // produce two results, to match the available instructions. This exposes
343 // the two-result form to trivial CSE, which is able to combine x/y and x%y
344 // into a single instruction.
345 //
346 // Scalar integer multiply-high is also lowered to use two-result
347 // operations, to match the available instructions. However, plain multiply
348 // (low) operations are left as Legal, as there are single-result
349 // instructions for this in x86. Using the two-result multiply instructions
350 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000351 for (unsigned i = 0, e = 4; i != e; ++i) {
352 MVT VT = IntVTs[i];
353 setOperationAction(ISD::MULHS, VT, Expand);
354 setOperationAction(ISD::MULHU, VT, Expand);
355 setOperationAction(ISD::SDIV, VT, Expand);
356 setOperationAction(ISD::UDIV, VT, Expand);
357 setOperationAction(ISD::SREM, VT, Expand);
358 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000359
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000360 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000361 setOperationAction(ISD::ADDC, VT, Custom);
362 setOperationAction(ISD::ADDE, VT, Custom);
363 setOperationAction(ISD::SUBC, VT, Custom);
364 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000365 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000366
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
368 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
369 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
370 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000371 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
376 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f32 , Expand);
378 setOperationAction(ISD::FREM , MVT::f64 , Expand);
379 setOperationAction(ISD::FREM , MVT::f80 , Expand);
380 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000381
Craig Topper909652f2011-10-14 03:21:46 +0000382 if (Subtarget->hasBMI()) {
383 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
384 } else {
385 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
386 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
387 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
388 if (Subtarget->is64Bit())
389 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
390 }
Craig Topper37f21672011-10-11 06:44:02 +0000391
392 if (Subtarget->hasLZCNT()) {
393 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
394 } else {
395 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
396 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
397 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
398 if (Subtarget->is64Bit())
399 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000400 }
401
Benjamin Kramer1292c222010-12-04 20:32:23 +0000402 if (Subtarget->hasPOPCNT()) {
403 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
404 } else {
405 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
406 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
407 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
408 if (Subtarget->is64Bit())
409 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
410 }
411
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
413 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000414
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000415 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000416 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000417 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000418 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000419 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
421 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
422 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
423 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
424 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000425 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
427 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
428 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
429 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000430 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000432 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000433 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000435
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000436 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
438 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
439 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
440 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000441 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000442 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
443 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000444 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000445 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000446 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
447 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
448 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
449 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000450 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000451 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000452 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
454 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
455 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000456 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
458 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
459 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000460 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000461
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000462 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000464
Eric Christopher9a9d2752010-07-22 02:48:34 +0000465 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000466 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000467
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000468 // On X86 and X86-64, atomic operations are lowered to locked instructions.
469 // Locked instructions, in turn, have implicit fence semantics (all memory
470 // operations are flushed before issuing the locked instruction, and they
471 // are not buffered), so we can fold away the common pattern of
472 // fence-atomic-fence.
473 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000474
Mon P Wang63307c32008-05-05 19:05:59 +0000475 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000476 for (unsigned i = 0, e = 4; i != e; ++i) {
477 MVT VT = IntVTs[i];
478 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
479 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000480 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000481 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000482
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000483 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000484 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
486 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
487 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
488 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
489 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
490 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
491 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000492 }
493
Eli Friedman43f51ae2011-08-26 21:21:21 +0000494 if (Subtarget->hasCmpxchg16b()) {
495 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
496 }
497
Evan Cheng3c992d22006-03-07 02:02:57 +0000498 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000499 if (!Subtarget->isTargetDarwin() &&
500 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000501 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000503 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000504
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
506 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
507 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
508 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000509 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000510 setExceptionPointerRegister(X86::RAX);
511 setExceptionSelectorRegister(X86::RDX);
512 } else {
513 setExceptionPointerRegister(X86::EAX);
514 setExceptionSelectorRegister(X86::EDX);
515 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
517 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000518
Duncan Sands4a544a72011-09-06 13:37:06 +0000519 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
520 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000521
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000523
Nate Begemanacc398c2006-01-25 18:21:52 +0000524 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000525 setOperationAction(ISD::VASTART , MVT::Other, Custom);
526 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000527 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::VAARG , MVT::Other, Custom);
529 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000530 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::VAARG , MVT::Other, Expand);
532 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000533 }
Evan Chengae642192007-03-02 23:16:35 +0000534
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
536 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000537
538 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
539 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
540 MVT::i64 : MVT::i32, Custom);
541 else if (EnableSegmentedStacks)
542 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
543 MVT::i64 : MVT::i32, Custom);
544 else
545 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
546 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000547
Evan Chengc7ce29b2009-02-13 22:36:38 +0000548 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000549 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000550 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
552 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000553
Evan Cheng223547a2006-01-31 22:28:30 +0000554 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 setOperationAction(ISD::FABS , MVT::f64, Custom);
556 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000557
558 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000559 setOperationAction(ISD::FNEG , MVT::f64, Custom);
560 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000561
Evan Cheng68c47cb2007-01-05 07:55:56 +0000562 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
564 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000565
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000566 // Lower this to FGETSIGNx86 plus an AND.
567 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
568 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
569
Evan Chengd25e9e82006-02-02 00:28:23 +0000570 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 setOperationAction(ISD::FSIN , MVT::f64, Expand);
572 setOperationAction(ISD::FCOS , MVT::f64, Expand);
573 setOperationAction(ISD::FSIN , MVT::f32, Expand);
574 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000575
Chris Lattnera54aa942006-01-29 06:26:08 +0000576 // Expand FP immediates into loads from the stack, except for the special
577 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000578 addLegalFPImmediate(APFloat(+0.0)); // xorpd
579 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000580 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000581 // Use SSE for f32, x87 for f64.
582 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
584 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000585
586 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000587 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000588
589 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000591
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000593
594 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000595 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
596 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000597
598 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 setOperationAction(ISD::FSIN , MVT::f32, Expand);
600 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000601
Nate Begemane1795842008-02-14 08:57:00 +0000602 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000603 addLegalFPImmediate(APFloat(+0.0f)); // xorps
604 addLegalFPImmediate(APFloat(+0.0)); // FLD0
605 addLegalFPImmediate(APFloat(+1.0)); // FLD1
606 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
607 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
608
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000609 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
611 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000612 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000613 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000614 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000615 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
617 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000618
Owen Anderson825b72b2009-08-11 20:47:22 +0000619 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
620 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
621 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
622 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000623
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000624 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
626 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000627 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000628 addLegalFPImmediate(APFloat(+0.0)); // FLD0
629 addLegalFPImmediate(APFloat(+1.0)); // FLD1
630 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
631 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000632 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
633 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
634 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
635 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000636 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000637
Cameron Zwarich33390842011-07-08 21:39:21 +0000638 // We don't support FMA.
639 setOperationAction(ISD::FMA, MVT::f64, Expand);
640 setOperationAction(ISD::FMA, MVT::f32, Expand);
641
Dale Johannesen59a58732007-08-05 18:49:15 +0000642 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000643 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
645 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
646 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000647 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000648 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000649 addLegalFPImmediate(TmpFlt); // FLD0
650 TmpFlt.changeSign();
651 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000652
653 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000654 APFloat TmpFlt2(+1.0);
655 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
656 &ignored);
657 addLegalFPImmediate(TmpFlt2); // FLD1
658 TmpFlt2.changeSign();
659 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
660 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000661
Evan Chengc7ce29b2009-02-13 22:36:38 +0000662 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
664 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000665 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000666
667 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000668 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000669
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000670 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
672 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
673 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000674
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 setOperationAction(ISD::FLOG, MVT::f80, Expand);
676 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
677 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
678 setOperationAction(ISD::FEXP, MVT::f80, Expand);
679 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000680
Mon P Wangf007a8b2008-11-06 05:31:54 +0000681 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000682 // (for widening) or expand (for scalarization). Then we will selectively
683 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
685 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
686 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000702 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
703 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000704 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000725 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000735 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000736 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000740 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000741 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
742 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
743 setTruncStoreAction((MVT::SimpleValueType)VT,
744 (MVT::SimpleValueType)InnerVT, Expand);
745 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
746 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
747 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000748 }
749
Evan Chengc7ce29b2009-02-13 22:36:38 +0000750 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
751 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000752 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000753 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000754 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000755 }
756
Dale Johannesen0488fb62010-09-30 23:57:10 +0000757 // MMX-sized vectors (other than x86mmx) are expected to be expanded
758 // into smaller operations.
759 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
760 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
761 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
762 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
763 setOperationAction(ISD::AND, MVT::v8i8, Expand);
764 setOperationAction(ISD::AND, MVT::v4i16, Expand);
765 setOperationAction(ISD::AND, MVT::v2i32, Expand);
766 setOperationAction(ISD::AND, MVT::v1i64, Expand);
767 setOperationAction(ISD::OR, MVT::v8i8, Expand);
768 setOperationAction(ISD::OR, MVT::v4i16, Expand);
769 setOperationAction(ISD::OR, MVT::v2i32, Expand);
770 setOperationAction(ISD::OR, MVT::v1i64, Expand);
771 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
772 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
773 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
774 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
775 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
776 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
777 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
778 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
779 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
780 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
781 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
782 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
783 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000784 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
785 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
786 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
787 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000788
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000789 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000791
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
793 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
794 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
795 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
796 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
797 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
798 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
799 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
800 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
801 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
802 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000803 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000804 }
805
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000806 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000808
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000809 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
810 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
812 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
813 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
814 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000815
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
817 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
818 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
819 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
820 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
821 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
822 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
823 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
824 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
825 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
826 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
827 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
828 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
829 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
830 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
831 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000832
Nadav Rotem354efd82011-09-18 14:57:03 +0000833 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000834 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
835 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
836 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000837
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
839 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
840 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
842 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000843
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000844 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
845 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
846 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
847 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
848 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
849
Evan Cheng2c3ae372006-04-12 21:21:57 +0000850 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
852 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000853 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000854 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000855 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000856 // Do not attempt to custom lower non-128-bit vectors
857 if (!VT.is128BitVector())
858 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000859 setOperationAction(ISD::BUILD_VECTOR,
860 VT.getSimpleVT().SimpleTy, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE,
862 VT.getSimpleVT().SimpleTy, Custom);
863 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
864 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000865 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000866
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
868 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
869 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
870 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
871 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
872 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000873
Nate Begemancdd1eec2008-02-12 22:51:28 +0000874 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000877 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000878
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000879 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
881 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000882 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000883
884 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000885 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000886 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000887
Owen Andersond6662ad2009-08-10 20:46:15 +0000888 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000890 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000894 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000896 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000898 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000899
Owen Anderson825b72b2009-08-11 20:47:22 +0000900 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000901
Evan Cheng2c3ae372006-04-12 21:21:57 +0000902 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
904 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
905 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
906 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000907
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
909 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000910 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000911
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000912 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000913 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
914 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
915 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
916 setOperationAction(ISD::FRINT, MVT::f32, Legal);
917 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
918 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
919 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
920 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
921 setOperationAction(ISD::FRINT, MVT::f64, Legal);
922 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
923
Nate Begeman14d12ca2008-02-11 04:19:36 +0000924 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000926
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000927 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
928 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
929 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
930 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
931 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000932
Nate Begeman14d12ca2008-02-11 04:19:36 +0000933 // i8 and i16 vectors are custom , because the source register and source
934 // source memory operand types are not the same width. f32 vectors are
935 // custom since the immediate controlling the insert encodes additional
936 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000937 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
938 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
939 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
940 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000941
Owen Anderson825b72b2009-08-11 20:47:22 +0000942 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
943 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
944 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
945 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000946
947 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
949 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000950 }
951 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000952
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000953 if (Subtarget->hasXMMInt()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000954 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000955 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000956
Nadav Rotem43012222011-05-11 08:12:09 +0000957 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000958 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000959
Nadav Rotem43012222011-05-11 08:12:09 +0000960 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000961 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000962
963 if (Subtarget->hasAVX2()) {
964 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
965 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
966
967 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
968 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
969
970 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
971 } else {
972 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
973 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
974
975 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
976 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
977
978 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
979 }
Nadav Rotem43012222011-05-11 08:12:09 +0000980 }
981
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000982 if (Subtarget->hasSSE42() || Subtarget->hasAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000983 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000984
David Greene9b9838d2009-06-29 16:47:10 +0000985 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000986 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
987 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
988 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
989 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
990 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
991 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000992
Owen Anderson825b72b2009-08-11 20:47:22 +0000993 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000994 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
995 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000996
Owen Anderson825b72b2009-08-11 20:47:22 +0000997 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
998 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
999 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1000 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1001 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
1002 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001003
Owen Anderson825b72b2009-08-11 20:47:22 +00001004 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1005 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1006 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1007 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1008 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1009 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001010
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001011 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1012 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001013 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001014
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00001015 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
1016 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
1017 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
1018 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
1019 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1020 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1021
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001022 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1023 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1024
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001025 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1026 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1027
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001028 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001029 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001030
Duncan Sands28b77e92011-09-06 19:07:46 +00001031 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1032 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1033 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1034 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001035
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001036 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1037 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1038 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1039
Craig Topperaaa643c2011-11-09 07:28:55 +00001040 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1041 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1042 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1043 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001044
Craig Topperaaa643c2011-11-09 07:28:55 +00001045 if (Subtarget->hasAVX2()) {
1046 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1047 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1048 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1049 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001050
Craig Topperaaa643c2011-11-09 07:28:55 +00001051 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1052 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1053 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1054 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001055
Craig Topperaaa643c2011-11-09 07:28:55 +00001056 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1057 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1058 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001059 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001060
1061 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001062
1063 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1064 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1065
1066 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1067 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1068
1069 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001070 } else {
1071 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1072 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1073 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1074 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1075
1076 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1077 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1078 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1079 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1080
1081 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1082 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1083 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1084 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001085
1086 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1087 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1088
1089 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1090 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1091
1092 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001093 }
Craig Topper13894fa2011-08-24 06:14:18 +00001094
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001095 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001096 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001097 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1098 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1099 EVT VT = SVT;
1100
1101 // Extract subvector is special because the value type
1102 // (result) is 128-bit but the source is 256-bit wide.
1103 if (VT.is128BitVector())
1104 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1105
1106 // Do not attempt to custom lower other non-256-bit vectors
1107 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001108 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001109
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001110 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1111 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1112 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1113 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001114 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001115 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001116 }
1117
David Greene54d8eba2011-01-27 22:38:56 +00001118 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001119 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1120 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1121 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001122
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001123 // Do not attempt to promote non-256-bit vectors
1124 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001125 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001126
1127 setOperationAction(ISD::AND, SVT, Promote);
1128 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1129 setOperationAction(ISD::OR, SVT, Promote);
1130 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1131 setOperationAction(ISD::XOR, SVT, Promote);
1132 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1133 setOperationAction(ISD::LOAD, SVT, Promote);
1134 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1135 setOperationAction(ISD::SELECT, SVT, Promote);
1136 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001137 }
David Greene9b9838d2009-06-29 16:47:10 +00001138 }
1139
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001140 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1141 // of this type with custom code.
1142 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1143 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1144 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1145 }
1146
Evan Cheng6be2c582006-04-05 23:38:46 +00001147 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001148 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001149
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001150
Eli Friedman962f5492010-06-02 19:35:46 +00001151 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1152 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001153 //
Eli Friedman962f5492010-06-02 19:35:46 +00001154 // FIXME: We really should do custom legalization for addition and
1155 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1156 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001157 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1158 // Add/Sub/Mul with overflow operations are custom lowered.
1159 MVT VT = IntVTs[i];
1160 setOperationAction(ISD::SADDO, VT, Custom);
1161 setOperationAction(ISD::UADDO, VT, Custom);
1162 setOperationAction(ISD::SSUBO, VT, Custom);
1163 setOperationAction(ISD::USUBO, VT, Custom);
1164 setOperationAction(ISD::SMULO, VT, Custom);
1165 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001166 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001167
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001168 // There are no 8-bit 3-address imul/mul instructions
1169 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1170 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001171
Evan Chengd54f2d52009-03-31 19:38:51 +00001172 if (!Subtarget->is64Bit()) {
1173 // These libcalls are not available in 32-bit.
1174 setLibcallName(RTLIB::SHL_I128, 0);
1175 setLibcallName(RTLIB::SRL_I128, 0);
1176 setLibcallName(RTLIB::SRA_I128, 0);
1177 }
1178
Evan Cheng206ee9d2006-07-07 08:33:52 +00001179 // We have target-specific dag combine patterns for the following nodes:
1180 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001181 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001182 setTargetDAGCombine(ISD::BUILD_VECTOR);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001183 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001184 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001185 setTargetDAGCombine(ISD::SHL);
1186 setTargetDAGCombine(ISD::SRA);
1187 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001188 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001189 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001190 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001191 setTargetDAGCombine(ISD::FADD);
1192 setTargetDAGCombine(ISD::FSUB);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001193 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001194 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001195 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001196 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001197 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001198 if (Subtarget->is64Bit())
1199 setTargetDAGCombine(ISD::MUL);
Craig Topperb4c94572011-10-21 06:55:01 +00001200 if (Subtarget->hasBMI())
1201 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001202
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001203 computeRegisterProperties();
1204
Evan Cheng05219282011-01-06 06:52:41 +00001205 // On Darwin, -Os means optimize for size without hurting performance,
1206 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001207 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001208 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001209 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001210 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1211 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1212 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001213 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001214 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001215
1216 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001217}
1218
Scott Michel5b8f82e2008-03-10 15:42:14 +00001219
Duncan Sands28b77e92011-09-06 19:07:46 +00001220EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1221 if (!VT.isVector()) return MVT::i8;
1222 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001223}
1224
1225
Evan Cheng29286502008-01-23 23:17:41 +00001226/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1227/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001228static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001229 if (MaxAlign == 16)
1230 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001231 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001232 if (VTy->getBitWidth() == 128)
1233 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001234 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001235 unsigned EltAlign = 0;
1236 getMaxByValAlign(ATy->getElementType(), EltAlign);
1237 if (EltAlign > MaxAlign)
1238 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001239 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001240 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1241 unsigned EltAlign = 0;
1242 getMaxByValAlign(STy->getElementType(i), EltAlign);
1243 if (EltAlign > MaxAlign)
1244 MaxAlign = EltAlign;
1245 if (MaxAlign == 16)
1246 break;
1247 }
1248 }
1249 return;
1250}
1251
1252/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1253/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001254/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1255/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001256unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001257 if (Subtarget->is64Bit()) {
1258 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001259 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001260 if (TyAlign > 8)
1261 return TyAlign;
1262 return 8;
1263 }
1264
Evan Cheng29286502008-01-23 23:17:41 +00001265 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001266 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001267 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001268 return Align;
1269}
Chris Lattner2b02a442007-02-25 08:29:00 +00001270
Evan Chengf0df0312008-05-15 08:39:06 +00001271/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001272/// and store operations as a result of memset, memcpy, and memmove
1273/// lowering. If DstAlign is zero that means it's safe to destination
1274/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1275/// means there isn't a need to check it against alignment requirement,
1276/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001277/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001278/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1279/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1280/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001281/// It returns EVT::Other if the type should be determined using generic
1282/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001283EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001284X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1285 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001286 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001287 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001288 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001289 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1290 // linux. This is because the stack realignment code can't handle certain
1291 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001292 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001293 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001294 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001295 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001296 (Subtarget->isUnalignedMemAccessFast() ||
1297 ((DstAlign == 0 || DstAlign >= 16) &&
1298 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001299 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001300 if (Subtarget->hasAVX() &&
1301 Subtarget->getStackAlignment() >= 32)
1302 return MVT::v8f32;
1303 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001304 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001305 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001306 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001307 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001308 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001309 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001310 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001311 // Do not use f64 to lower memcpy if source is string constant. It's
1312 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001313 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001314 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001315 }
Evan Chengf0df0312008-05-15 08:39:06 +00001316 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001317 return MVT::i64;
1318 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001319}
1320
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001321/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1322/// current function. The returned value is a member of the
1323/// MachineJumpTableInfo::JTEntryKind enum.
1324unsigned X86TargetLowering::getJumpTableEncoding() const {
1325 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1326 // symbol.
1327 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1328 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001329 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001330
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001331 // Otherwise, use the normal jump table encoding heuristics.
1332 return TargetLowering::getJumpTableEncoding();
1333}
1334
Chris Lattnerc64daab2010-01-26 05:02:42 +00001335const MCExpr *
1336X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1337 const MachineBasicBlock *MBB,
1338 unsigned uid,MCContext &Ctx) const{
1339 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1340 Subtarget->isPICStyleGOT());
1341 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1342 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001343 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1344 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001345}
1346
Evan Chengcc415862007-11-09 01:32:10 +00001347/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1348/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001349SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001350 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001351 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001352 // This doesn't have DebugLoc associated with it, but is not really the
1353 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001354 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001355 return Table;
1356}
1357
Chris Lattner589c6f62010-01-26 06:28:43 +00001358/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1359/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1360/// MCExpr.
1361const MCExpr *X86TargetLowering::
1362getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1363 MCContext &Ctx) const {
1364 // X86-64 uses RIP relative addressing based on the jump table label.
1365 if (Subtarget->isPICStyleRIPRel())
1366 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1367
1368 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001369 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001370}
1371
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001372// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001373std::pair<const TargetRegisterClass*, uint8_t>
1374X86TargetLowering::findRepresentativeClass(EVT VT) const{
1375 const TargetRegisterClass *RRC = 0;
1376 uint8_t Cost = 1;
1377 switch (VT.getSimpleVT().SimpleTy) {
1378 default:
1379 return TargetLowering::findRepresentativeClass(VT);
1380 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1381 RRC = (Subtarget->is64Bit()
1382 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1383 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001384 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001385 RRC = X86::VR64RegisterClass;
1386 break;
1387 case MVT::f32: case MVT::f64:
1388 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1389 case MVT::v4f32: case MVT::v2f64:
1390 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1391 case MVT::v4f64:
1392 RRC = X86::VR128RegisterClass;
1393 break;
1394 }
1395 return std::make_pair(RRC, Cost);
1396}
1397
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001398bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1399 unsigned &Offset) const {
1400 if (!Subtarget->isTargetLinux())
1401 return false;
1402
1403 if (Subtarget->is64Bit()) {
1404 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1405 Offset = 0x28;
1406 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1407 AddressSpace = 256;
1408 else
1409 AddressSpace = 257;
1410 } else {
1411 // %gs:0x14 on i386
1412 Offset = 0x14;
1413 AddressSpace = 256;
1414 }
1415 return true;
1416}
1417
1418
Chris Lattner2b02a442007-02-25 08:29:00 +00001419//===----------------------------------------------------------------------===//
1420// Return Value Calling Convention Implementation
1421//===----------------------------------------------------------------------===//
1422
Chris Lattner59ed56b2007-02-28 04:55:35 +00001423#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001424
Michael J. Spencerec38de22010-10-10 22:04:20 +00001425bool
Eric Christopher471e4222011-06-08 23:55:35 +00001426X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1427 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001428 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001429 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001430 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001431 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001432 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001433 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001434}
1435
Dan Gohman98ca4f22009-08-05 01:29:28 +00001436SDValue
1437X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001438 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001439 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001440 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001441 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001442 MachineFunction &MF = DAG.getMachineFunction();
1443 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001444
Chris Lattner9774c912007-02-27 05:28:59 +00001445 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001446 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001447 RVLocs, *DAG.getContext());
1448 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001449
Evan Chengdcea1632010-02-04 02:40:39 +00001450 // Add the regs to the liveout set for the function.
1451 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1452 for (unsigned i = 0; i != RVLocs.size(); ++i)
1453 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1454 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001455
Dan Gohman475871a2008-07-27 21:46:04 +00001456 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001457
Dan Gohman475871a2008-07-27 21:46:04 +00001458 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001459 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1460 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001461 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1462 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001463
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001464 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001465 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1466 CCValAssign &VA = RVLocs[i];
1467 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001468 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001469 EVT ValVT = ValToCopy.getValueType();
1470
Dale Johannesenc4510512010-09-24 19:05:48 +00001471 // If this is x86-64, and we disabled SSE, we can't return FP values,
1472 // or SSE or MMX vectors.
1473 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1474 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001475 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001476 report_fatal_error("SSE register return with SSE disabled");
1477 }
1478 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1479 // llvm-gcc has never done it right and no one has noticed, so this
1480 // should be OK for now.
1481 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001482 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001483 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001484
Chris Lattner447ff682008-03-11 03:23:40 +00001485 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1486 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001487 if (VA.getLocReg() == X86::ST0 ||
1488 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001489 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1490 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001491 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001492 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001493 RetOps.push_back(ValToCopy);
1494 // Don't emit a copytoreg.
1495 continue;
1496 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001497
Evan Cheng242b38b2009-02-23 09:03:22 +00001498 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1499 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001500 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001501 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001502 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001503 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001504 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1505 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001506 // If we don't have SSE2 available, convert to v4f32 so the generated
1507 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001508 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001509 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001510 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001511 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001512 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001513
Dale Johannesendd64c412009-02-04 00:33:20 +00001514 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001515 Flag = Chain.getValue(1);
1516 }
Dan Gohman61a92132008-04-21 23:59:07 +00001517
1518 // The x86-64 ABI for returning structs by value requires that we copy
1519 // the sret argument into %rax for the return. We saved the argument into
1520 // a virtual register in the entry block, so now we copy the value out
1521 // and into %rax.
1522 if (Subtarget->is64Bit() &&
1523 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1524 MachineFunction &MF = DAG.getMachineFunction();
1525 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1526 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001527 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001528 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001529 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001530
Dale Johannesendd64c412009-02-04 00:33:20 +00001531 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001532 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001533
1534 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001535 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001536 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001537
Chris Lattner447ff682008-03-11 03:23:40 +00001538 RetOps[0] = Chain; // Update chain.
1539
1540 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001541 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001542 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001543
1544 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001545 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001546}
1547
Evan Cheng3d2125c2010-11-30 23:55:39 +00001548bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1549 if (N->getNumValues() != 1)
1550 return false;
1551 if (!N->hasNUsesOfValue(1, 0))
1552 return false;
1553
1554 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001555 if (Copy->getOpcode() != ISD::CopyToReg &&
1556 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001557 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001558
1559 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001560 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001561 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001562 if (UI->getOpcode() != X86ISD::RET_FLAG)
1563 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001564 HasRet = true;
1565 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001566
Evan Cheng1bf891a2010-12-01 22:59:46 +00001567 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001568}
1569
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001570EVT
1571X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001572 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001573 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001574 // TODO: Is this also valid on 32-bit?
1575 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001576 ReturnMVT = MVT::i8;
1577 else
1578 ReturnMVT = MVT::i32;
1579
1580 EVT MinVT = getRegisterType(Context, ReturnMVT);
1581 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001582}
1583
Dan Gohman98ca4f22009-08-05 01:29:28 +00001584/// LowerCallResult - Lower the result values of a call into the
1585/// appropriate copies out of appropriate physical registers.
1586///
1587SDValue
1588X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001589 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001590 const SmallVectorImpl<ISD::InputArg> &Ins,
1591 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001592 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001593
Chris Lattnere32bbf62007-02-28 07:09:55 +00001594 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001595 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001596 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001597 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1598 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001599 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001600
Chris Lattner3085e152007-02-25 08:59:22 +00001601 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001602 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001603 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001604 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001605
Torok Edwin3f142c32009-02-01 18:15:56 +00001606 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001607 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001608 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001609 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001610 }
1611
Evan Cheng79fb3b42009-02-20 20:43:02 +00001612 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001613
1614 // If this is a call to a function that returns an fp value on the floating
1615 // point stack, we must guarantee the the value is popped from the stack, so
1616 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001617 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001618 // instead.
1619 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1620 // If we prefer to use the value in xmm registers, copy it out as f80 and
1621 // use a truncate to move it from fp stack reg to xmm reg.
1622 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001623 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001624 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1625 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001626 Val = Chain.getValue(0);
1627
1628 // Round the f80 to the right size, which also moves it to the appropriate
1629 // xmm register.
1630 if (CopyVT != VA.getValVT())
1631 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1632 // This truncation won't change the value.
1633 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001634 } else {
1635 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1636 CopyVT, InFlag).getValue(1);
1637 Val = Chain.getValue(0);
1638 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001639 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001640 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001641 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001642
Dan Gohman98ca4f22009-08-05 01:29:28 +00001643 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001644}
1645
1646
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001647//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001648// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001649//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001650// StdCall calling convention seems to be standard for many Windows' API
1651// routines and around. It differs from C calling convention just a little:
1652// callee should clean up the stack, not caller. Symbols should be also
1653// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001654// For info on fast calling convention see Fast Calling Convention (tail call)
1655// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001656
Dan Gohman98ca4f22009-08-05 01:29:28 +00001657/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001658/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001659static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1660 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001661 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001662
Dan Gohman98ca4f22009-08-05 01:29:28 +00001663 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001664}
1665
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001666/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001667/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001668static bool
1669ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1670 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001671 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001672
Dan Gohman98ca4f22009-08-05 01:29:28 +00001673 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001674}
1675
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001676/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1677/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001678/// the specific parameter attribute. The copy will be passed as a byval
1679/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001680static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001681CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001682 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1683 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001684 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001685
Dale Johannesendd64c412009-02-04 00:33:20 +00001686 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001687 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001688 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001689}
1690
Chris Lattner29689432010-03-11 00:22:57 +00001691/// IsTailCallConvention - Return true if the calling convention is one that
1692/// supports tail call optimization.
1693static bool IsTailCallConvention(CallingConv::ID CC) {
1694 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1695}
1696
Evan Cheng485fafc2011-03-21 01:19:09 +00001697bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1698 if (!CI->isTailCall())
1699 return false;
1700
1701 CallSite CS(CI);
1702 CallingConv::ID CalleeCC = CS.getCallingConv();
1703 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1704 return false;
1705
1706 return true;
1707}
1708
Evan Cheng0c439eb2010-01-27 00:07:07 +00001709/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1710/// a tailcall target by changing its ABI.
1711static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001712 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001713}
1714
Dan Gohman98ca4f22009-08-05 01:29:28 +00001715SDValue
1716X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001717 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001718 const SmallVectorImpl<ISD::InputArg> &Ins,
1719 DebugLoc dl, SelectionDAG &DAG,
1720 const CCValAssign &VA,
1721 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001722 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001723 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001724 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001725 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001726 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001727 EVT ValVT;
1728
1729 // If value is passed by pointer we have address passed instead of the value
1730 // itself.
1731 if (VA.getLocInfo() == CCValAssign::Indirect)
1732 ValVT = VA.getLocVT();
1733 else
1734 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001735
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001736 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001737 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001738 // In case of tail call optimization mark all arguments mutable. Since they
1739 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001740 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001741 unsigned Bytes = Flags.getByValSize();
1742 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1743 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001744 return DAG.getFrameIndex(FI, getPointerTy());
1745 } else {
1746 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001747 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001748 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1749 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001750 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001751 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001752 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001753}
1754
Dan Gohman475871a2008-07-27 21:46:04 +00001755SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001756X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001757 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001758 bool isVarArg,
1759 const SmallVectorImpl<ISD::InputArg> &Ins,
1760 DebugLoc dl,
1761 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001762 SmallVectorImpl<SDValue> &InVals)
1763 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001764 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001765 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001766
Gordon Henriksen86737662008-01-05 16:56:59 +00001767 const Function* Fn = MF.getFunction();
1768 if (Fn->hasExternalLinkage() &&
1769 Subtarget->isTargetCygMing() &&
1770 Fn->getName() == "main")
1771 FuncInfo->setForceFramePointer(true);
1772
Evan Cheng1bc78042006-04-26 01:20:17 +00001773 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001774 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001775 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001776
Chris Lattner29689432010-03-11 00:22:57 +00001777 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1778 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001779
Chris Lattner638402b2007-02-28 07:00:42 +00001780 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001781 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001782 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001783 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001784
1785 // Allocate shadow area for Win64
1786 if (IsWin64) {
1787 CCInfo.AllocateStack(32, 8);
1788 }
1789
Duncan Sands45907662010-10-31 13:21:44 +00001790 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001791
Chris Lattnerf39f7712007-02-28 05:46:49 +00001792 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001793 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001794 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1795 CCValAssign &VA = ArgLocs[i];
1796 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1797 // places.
1798 assert(VA.getValNo() != LastVal &&
1799 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001800 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001801 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001802
Chris Lattnerf39f7712007-02-28 05:46:49 +00001803 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001804 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001805 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001806 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001807 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001808 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001809 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001810 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001811 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001812 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001813 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001814 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1815 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001816 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001817 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001818 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001819 RC = X86::VR64RegisterClass;
1820 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001821 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001822
Devang Patel68e6bee2011-02-21 23:21:26 +00001823 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001824 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001825
Chris Lattnerf39f7712007-02-28 05:46:49 +00001826 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1827 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1828 // right size.
1829 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001830 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001831 DAG.getValueType(VA.getValVT()));
1832 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001833 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001834 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001835 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001836 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001837
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001838 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001839 // Handle MMX values passed in XMM regs.
1840 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001841 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1842 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001843 } else
1844 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001845 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001846 } else {
1847 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001848 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001849 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001850
1851 // If value is passed via pointer - do a load.
1852 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001853 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001854 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001855
Dan Gohman98ca4f22009-08-05 01:29:28 +00001856 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001857 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001858
Dan Gohman61a92132008-04-21 23:59:07 +00001859 // The x86-64 ABI for returning structs by value requires that we copy
1860 // the sret argument into %rax for the return. Save the argument into
1861 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001862 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001863 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1864 unsigned Reg = FuncInfo->getSRetReturnReg();
1865 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001866 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001867 FuncInfo->setSRetReturnReg(Reg);
1868 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001869 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001870 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001871 }
1872
Chris Lattnerf39f7712007-02-28 05:46:49 +00001873 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001874 // Align stack specially for tail calls.
1875 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001876 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001877
Evan Cheng1bc78042006-04-26 01:20:17 +00001878 // If the function takes variable number of arguments, make a frame index for
1879 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001880 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001881 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1882 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001883 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001884 }
1885 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001886 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1887
1888 // FIXME: We should really autogenerate these arrays
1889 static const unsigned GPR64ArgRegsWin64[] = {
1890 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001891 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001892 static const unsigned GPR64ArgRegs64Bit[] = {
1893 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1894 };
1895 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001896 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1897 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1898 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001899 const unsigned *GPR64ArgRegs;
1900 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001901
1902 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001903 // The XMM registers which might contain var arg parameters are shadowed
1904 // in their paired GPR. So we only need to save the GPR to their home
1905 // slots.
1906 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001907 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001908 } else {
1909 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1910 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001911
1912 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001913 }
1914 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1915 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001916
Devang Patel578efa92009-06-05 21:57:13 +00001917 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001918 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001919 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001920 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001921 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001922 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001923 // Kernel mode asks for SSE to be disabled, so don't push them
1924 // on the stack.
1925 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001926
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001927 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001928 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001929 // Get to the caller-allocated home save location. Add 8 to account
1930 // for the return address.
1931 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001932 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001933 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001934 // Fixup to set vararg frame on shadow area (4 x i64).
1935 if (NumIntRegs < 4)
1936 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001937 } else {
1938 // For X86-64, if there are vararg parameters that are passed via
1939 // registers, then we must store them to their spots on the stack so they
1940 // may be loaded by deferencing the result of va_next.
1941 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1942 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1943 FuncInfo->setRegSaveFrameIndex(
1944 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001945 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001946 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001947
Gordon Henriksen86737662008-01-05 16:56:59 +00001948 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001949 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001950 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1951 getPointerTy());
1952 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001953 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001954 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1955 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001956 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001957 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001958 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001959 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001960 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001961 MachinePointerInfo::getFixedStack(
1962 FuncInfo->getRegSaveFrameIndex(), Offset),
1963 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001964 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001965 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001966 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001967
Dan Gohmanface41a2009-08-16 21:24:25 +00001968 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1969 // Now store the XMM (fp + vector) parameter registers.
1970 SmallVector<SDValue, 11> SaveXMMOps;
1971 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001972
Devang Patel68e6bee2011-02-21 23:21:26 +00001973 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001974 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1975 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001976
Dan Gohman1e93df62010-04-17 14:41:14 +00001977 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1978 FuncInfo->getRegSaveFrameIndex()));
1979 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1980 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001981
Dan Gohmanface41a2009-08-16 21:24:25 +00001982 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001983 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001984 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001985 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1986 SaveXMMOps.push_back(Val);
1987 }
1988 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1989 MVT::Other,
1990 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001991 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001992
1993 if (!MemOps.empty())
1994 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1995 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001996 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001997 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001998
Gordon Henriksen86737662008-01-05 16:56:59 +00001999 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00002000 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002001 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002002 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002003 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002004 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00002005 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00002006 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002007 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002008
Gordon Henriksen86737662008-01-05 16:56:59 +00002009 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002010 // RegSaveFrameIndex is X86-64 only.
2011 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002012 if (CallConv == CallingConv::X86_FastCall ||
2013 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002014 // fastcc functions can't have varargs.
2015 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002016 }
Evan Cheng25caf632006-05-23 21:06:34 +00002017
Rafael Espindola76927d752011-08-30 19:39:58 +00002018 FuncInfo->setArgumentStackSize(StackSize);
2019
Dan Gohman98ca4f22009-08-05 01:29:28 +00002020 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002021}
2022
Dan Gohman475871a2008-07-27 21:46:04 +00002023SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002024X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2025 SDValue StackPtr, SDValue Arg,
2026 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002027 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002028 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002029 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002030 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002031 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002032 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002033 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002034
2035 return DAG.getStore(Chain, dl, Arg, PtrOff,
2036 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002037 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002038}
2039
Bill Wendling64e87322009-01-16 19:25:27 +00002040/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002041/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002042SDValue
2043X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002044 SDValue &OutRetAddr, SDValue Chain,
2045 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002046 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002047 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002048 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002049 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002050
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002051 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002052 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002053 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002054 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002055}
2056
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002057/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002058/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002059static SDValue
2060EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002061 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002062 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002063 // Store the return address to the appropriate stack slot.
2064 if (!FPDiff) return Chain;
2065 // Calculate the new stack slot for the return address.
2066 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002067 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002068 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002069 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002070 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002071 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002072 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002073 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002074 return Chain;
2075}
2076
Dan Gohman98ca4f22009-08-05 01:29:28 +00002077SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002078X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002079 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002080 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002081 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002082 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002083 const SmallVectorImpl<ISD::InputArg> &Ins,
2084 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002085 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002086 MachineFunction &MF = DAG.getMachineFunction();
2087 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002088 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002089 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002090 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002091
Evan Cheng5f941932010-02-05 02:21:12 +00002092 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002093 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002094 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2095 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002096 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002097
2098 // Sibcalls are automatically detected tailcalls which do not require
2099 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002100 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002101 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002102
2103 if (isTailCall)
2104 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002105 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002106
Chris Lattner29689432010-03-11 00:22:57 +00002107 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2108 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002109
Chris Lattner638402b2007-02-28 07:00:42 +00002110 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002111 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002112 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002113 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002114
2115 // Allocate shadow area for Win64
2116 if (IsWin64) {
2117 CCInfo.AllocateStack(32, 8);
2118 }
2119
Duncan Sands45907662010-10-31 13:21:44 +00002120 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002121
Chris Lattner423c5f42007-02-28 05:31:48 +00002122 // Get a count of how many bytes are to be pushed on the stack.
2123 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002124 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002125 // This is a sibcall. The memory operands are available in caller's
2126 // own caller's stack.
2127 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002128 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002129 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002130
Gordon Henriksen86737662008-01-05 16:56:59 +00002131 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002132 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002133 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002134 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002135 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2136 FPDiff = NumBytesCallerPushed - NumBytes;
2137
2138 // Set the delta of movement of the returnaddr stackslot.
2139 // But only set if delta is greater than previous delta.
2140 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2141 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2142 }
2143
Evan Chengf22f9b32010-02-06 03:28:46 +00002144 if (!IsSibcall)
2145 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002146
Dan Gohman475871a2008-07-27 21:46:04 +00002147 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002148 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002149 if (isTailCall && FPDiff)
2150 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2151 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002152
Dan Gohman475871a2008-07-27 21:46:04 +00002153 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2154 SmallVector<SDValue, 8> MemOpChains;
2155 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002156
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002157 // Walk the register/memloc assignments, inserting copies/loads. In the case
2158 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002159 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2160 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002161 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002162 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002163 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002164 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002165
Chris Lattner423c5f42007-02-28 05:31:48 +00002166 // Promote the value if needed.
2167 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002168 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002169 case CCValAssign::Full: break;
2170 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002171 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002172 break;
2173 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002174 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002175 break;
2176 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002177 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2178 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002179 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002180 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2181 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002182 } else
2183 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2184 break;
2185 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002186 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002187 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002188 case CCValAssign::Indirect: {
2189 // Store the argument.
2190 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002191 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002192 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002193 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002194 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002195 Arg = SpillSlot;
2196 break;
2197 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002198 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002199
Chris Lattner423c5f42007-02-28 05:31:48 +00002200 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002201 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2202 if (isVarArg && IsWin64) {
2203 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2204 // shadow reg if callee is a varargs function.
2205 unsigned ShadowReg = 0;
2206 switch (VA.getLocReg()) {
2207 case X86::XMM0: ShadowReg = X86::RCX; break;
2208 case X86::XMM1: ShadowReg = X86::RDX; break;
2209 case X86::XMM2: ShadowReg = X86::R8; break;
2210 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002211 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002212 if (ShadowReg)
2213 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002214 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002215 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002216 assert(VA.isMemLoc());
2217 if (StackPtr.getNode() == 0)
2218 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2219 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2220 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002221 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002222 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002223
Evan Cheng32fe1032006-05-25 00:59:30 +00002224 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002225 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002226 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002227
Evan Cheng347d5f72006-04-28 21:29:37 +00002228 // Build a sequence of copy-to-reg nodes chained together with token chain
2229 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002230 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002231 // Tail call byval lowering might overwrite argument registers so in case of
2232 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002233 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002234 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002235 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002236 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002237 InFlag = Chain.getValue(1);
2238 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002239
Chris Lattner88e1fd52009-07-09 04:24:46 +00002240 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002241 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2242 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002243 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002244 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2245 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002246 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002247 InFlag);
2248 InFlag = Chain.getValue(1);
2249 } else {
2250 // If we are tail calling and generating PIC/GOT style code load the
2251 // address of the callee into ECX. The value in ecx is used as target of
2252 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2253 // for tail calls on PIC/GOT architectures. Normally we would just put the
2254 // address of GOT into ebx and then call target@PLT. But for tail calls
2255 // ebx would be restored (since ebx is callee saved) before jumping to the
2256 // target@PLT.
2257
2258 // Note: The actual moving to ECX is done further down.
2259 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2260 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2261 !G->getGlobal()->hasProtectedVisibility())
2262 Callee = LowerGlobalAddress(Callee, DAG);
2263 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002264 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002265 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002266 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002267
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002268 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002269 // From AMD64 ABI document:
2270 // For calls that may call functions that use varargs or stdargs
2271 // (prototype-less calls or calls to functions containing ellipsis (...) in
2272 // the declaration) %al is used as hidden argument to specify the number
2273 // of SSE registers used. The contents of %al do not need to match exactly
2274 // the number of registers, but must be an ubound on the number of SSE
2275 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002276
Gordon Henriksen86737662008-01-05 16:56:59 +00002277 // Count the number of XMM registers allocated.
2278 static const unsigned XMMArgRegs[] = {
2279 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2280 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2281 };
2282 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002283 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002284 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002285
Dale Johannesendd64c412009-02-04 00:33:20 +00002286 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002287 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002288 InFlag = Chain.getValue(1);
2289 }
2290
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002291
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002292 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002293 if (isTailCall) {
2294 // Force all the incoming stack arguments to be loaded from the stack
2295 // before any new outgoing arguments are stored to the stack, because the
2296 // outgoing stack slots may alias the incoming argument stack slots, and
2297 // the alias isn't otherwise explicit. This is slightly more conservative
2298 // than necessary, because it means that each store effectively depends
2299 // on every argument instead of just those arguments it would clobber.
2300 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2301
Dan Gohman475871a2008-07-27 21:46:04 +00002302 SmallVector<SDValue, 8> MemOpChains2;
2303 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002304 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002305 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002306 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002307 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002308 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2309 CCValAssign &VA = ArgLocs[i];
2310 if (VA.isRegLoc())
2311 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002312 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002313 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002314 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002315 // Create frame index.
2316 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002317 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002318 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002319 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002320
Duncan Sands276dcbd2008-03-21 09:14:45 +00002321 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002322 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002323 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002324 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002325 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002326 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002327 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002328
Dan Gohman98ca4f22009-08-05 01:29:28 +00002329 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2330 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002331 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002332 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002333 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002334 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002335 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002336 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002337 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002338 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002339 }
2340 }
2341
2342 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002343 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002344 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002345
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002346 // Copy arguments to their registers.
2347 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002348 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002349 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002350 InFlag = Chain.getValue(1);
2351 }
Dan Gohman475871a2008-07-27 21:46:04 +00002352 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002353
Gordon Henriksen86737662008-01-05 16:56:59 +00002354 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002355 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002356 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002357 }
2358
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002359 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2360 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2361 // In the 64-bit large code model, we have to make all calls
2362 // through a register, since the call instruction's 32-bit
2363 // pc-relative offset may not be large enough to hold the whole
2364 // address.
2365 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002366 // If the callee is a GlobalAddress node (quite common, every direct call
2367 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2368 // it.
2369
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002370 // We should use extra load for direct calls to dllimported functions in
2371 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002372 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002373 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002374 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002375 bool ExtraLoad = false;
2376 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002377
Chris Lattner48a7d022009-07-09 05:02:21 +00002378 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2379 // external symbols most go through the PLT in PIC mode. If the symbol
2380 // has hidden or protected visibility, or if it is static or local, then
2381 // we don't need to use the PLT - we can directly call it.
2382 if (Subtarget->isTargetELF() &&
2383 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002384 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002385 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002386 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002387 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002388 (!Subtarget->getTargetTriple().isMacOSX() ||
2389 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002390 // PC-relative references to external symbols should go through $stub,
2391 // unless we're building with the leopard linker or later, which
2392 // automatically synthesizes these stubs.
2393 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002394 } else if (Subtarget->isPICStyleRIPRel() &&
2395 isa<Function>(GV) &&
2396 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2397 // If the function is marked as non-lazy, generate an indirect call
2398 // which loads from the GOT directly. This avoids runtime overhead
2399 // at the cost of eager binding (and one extra byte of encoding).
2400 OpFlags = X86II::MO_GOTPCREL;
2401 WrapperKind = X86ISD::WrapperRIP;
2402 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002403 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002404
Devang Patel0d881da2010-07-06 22:08:15 +00002405 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002406 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002407
2408 // Add a wrapper if needed.
2409 if (WrapperKind != ISD::DELETED_NODE)
2410 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2411 // Add extra indirection if needed.
2412 if (ExtraLoad)
2413 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2414 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002415 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002416 }
Bill Wendling056292f2008-09-16 21:48:12 +00002417 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002418 unsigned char OpFlags = 0;
2419
Evan Cheng1bf891a2010-12-01 22:59:46 +00002420 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2421 // external symbols should go through the PLT.
2422 if (Subtarget->isTargetELF() &&
2423 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2424 OpFlags = X86II::MO_PLT;
2425 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002426 (!Subtarget->getTargetTriple().isMacOSX() ||
2427 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002428 // PC-relative references to external symbols should go through $stub,
2429 // unless we're building with the leopard linker or later, which
2430 // automatically synthesizes these stubs.
2431 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002432 }
Eric Christopherfd179292009-08-27 18:07:15 +00002433
Chris Lattner48a7d022009-07-09 05:02:21 +00002434 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2435 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002436 }
2437
Chris Lattnerd96d0722007-02-25 06:40:16 +00002438 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002439 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002440 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002441
Evan Chengf22f9b32010-02-06 03:28:46 +00002442 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002443 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2444 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002445 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002446 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002447
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002448 Ops.push_back(Chain);
2449 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002450
Dan Gohman98ca4f22009-08-05 01:29:28 +00002451 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002452 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002453
Gordon Henriksen86737662008-01-05 16:56:59 +00002454 // Add argument registers to the end of the list so that they are known live
2455 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002456 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2457 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2458 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002459
Evan Cheng586ccac2008-03-18 23:36:35 +00002460 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002461 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002462 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2463
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002464 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002465 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002466 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002467
Gabor Greifba36cb52008-08-28 21:40:38 +00002468 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002469 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002470
Dan Gohman98ca4f22009-08-05 01:29:28 +00002471 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002472 // We used to do:
2473 //// If this is the first return lowered for this function, add the regs
2474 //// to the liveout set for the function.
2475 // This isn't right, although it's probably harmless on x86; liveouts
2476 // should be computed from returns not tail calls. Consider a void
2477 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002478 return DAG.getNode(X86ISD::TC_RETURN, dl,
2479 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002480 }
2481
Dale Johannesenace16102009-02-03 19:33:06 +00002482 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002483 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002484
Chris Lattner2d297092006-05-23 18:50:38 +00002485 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002486 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002487 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002488 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002489 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002490 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002491 // pops the hidden struct pointer, so we have to push it back.
2492 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002493 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002494 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002495 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002496
Gordon Henriksenae636f82008-01-03 16:47:34 +00002497 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002498 if (!IsSibcall) {
2499 Chain = DAG.getCALLSEQ_END(Chain,
2500 DAG.getIntPtrConstant(NumBytes, true),
2501 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2502 true),
2503 InFlag);
2504 InFlag = Chain.getValue(1);
2505 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002506
Chris Lattner3085e152007-02-25 08:59:22 +00002507 // Handle result values, copying them out of physregs into vregs that we
2508 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002509 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2510 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002511}
2512
Evan Cheng25ab6902006-09-08 06:48:29 +00002513
2514//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002515// Fast Calling Convention (tail call) implementation
2516//===----------------------------------------------------------------------===//
2517
2518// Like std call, callee cleans arguments, convention except that ECX is
2519// reserved for storing the tail called function address. Only 2 registers are
2520// free for argument passing (inreg). Tail call optimization is performed
2521// provided:
2522// * tailcallopt is enabled
2523// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002524// On X86_64 architecture with GOT-style position independent code only local
2525// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002526// To keep the stack aligned according to platform abi the function
2527// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2528// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002529// If a tail called function callee has more arguments than the caller the
2530// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002531// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002532// original REtADDR, but before the saved framepointer or the spilled registers
2533// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2534// stack layout:
2535// arg1
2536// arg2
2537// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002538// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002539// move area ]
2540// (possible EBP)
2541// ESI
2542// EDI
2543// local1 ..
2544
2545/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2546/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002547unsigned
2548X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2549 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002550 MachineFunction &MF = DAG.getMachineFunction();
2551 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002552 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002553 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002554 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002555 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002556 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002557 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2558 // Number smaller than 12 so just add the difference.
2559 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2560 } else {
2561 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002562 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002563 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002564 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002565 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002566}
2567
Evan Cheng5f941932010-02-05 02:21:12 +00002568/// MatchingStackOffset - Return true if the given stack call argument is
2569/// already available in the same position (relatively) of the caller's
2570/// incoming argument stack.
2571static
2572bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2573 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2574 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002575 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2576 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002577 if (Arg.getOpcode() == ISD::CopyFromReg) {
2578 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002579 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002580 return false;
2581 MachineInstr *Def = MRI->getVRegDef(VR);
2582 if (!Def)
2583 return false;
2584 if (!Flags.isByVal()) {
2585 if (!TII->isLoadFromStackSlot(Def, FI))
2586 return false;
2587 } else {
2588 unsigned Opcode = Def->getOpcode();
2589 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2590 Def->getOperand(1).isFI()) {
2591 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002592 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002593 } else
2594 return false;
2595 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002596 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2597 if (Flags.isByVal())
2598 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002599 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002600 // define @foo(%struct.X* %A) {
2601 // tail call @bar(%struct.X* byval %A)
2602 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002603 return false;
2604 SDValue Ptr = Ld->getBasePtr();
2605 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2606 if (!FINode)
2607 return false;
2608 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002609 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002610 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002611 FI = FINode->getIndex();
2612 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002613 } else
2614 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002615
Evan Cheng4cae1332010-03-05 08:38:04 +00002616 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002617 if (!MFI->isFixedObjectIndex(FI))
2618 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002619 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002620}
2621
Dan Gohman98ca4f22009-08-05 01:29:28 +00002622/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2623/// for tail call optimization. Targets which want to do tail call
2624/// optimization should implement this function.
2625bool
2626X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002627 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002628 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002629 bool isCalleeStructRet,
2630 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002631 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002632 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002633 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002634 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002635 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002636 CalleeCC != CallingConv::C)
2637 return false;
2638
Evan Cheng7096ae42010-01-29 06:45:59 +00002639 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002640 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002641 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002642 CallingConv::ID CallerCC = CallerF->getCallingConv();
2643 bool CCMatch = CallerCC == CalleeCC;
2644
Dan Gohman1797ed52010-02-08 20:27:50 +00002645 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002646 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002647 return true;
2648 return false;
2649 }
2650
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002651 // Look for obvious safe cases to perform tail call optimization that do not
2652 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002653
Evan Cheng2c12cb42010-03-26 16:26:03 +00002654 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2655 // emit a special epilogue.
2656 if (RegInfo->needsStackRealignment(MF))
2657 return false;
2658
Evan Chenga375d472010-03-15 18:54:48 +00002659 // Also avoid sibcall optimization if either caller or callee uses struct
2660 // return semantics.
2661 if (isCalleeStructRet || isCallerStructRet)
2662 return false;
2663
Chad Rosier2416da32011-06-24 21:15:36 +00002664 // An stdcall caller is expected to clean up its arguments; the callee
2665 // isn't going to do that.
2666 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2667 return false;
2668
Chad Rosier871f6642011-05-18 19:59:50 +00002669 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002670 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002671 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002672
2673 // Optimizing for varargs on Win64 is unlikely to be safe without
2674 // additional testing.
2675 if (Subtarget->isTargetWin64())
2676 return false;
2677
Chad Rosier871f6642011-05-18 19:59:50 +00002678 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002679 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2680 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002681
Chad Rosier871f6642011-05-18 19:59:50 +00002682 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2683 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2684 if (!ArgLocs[i].isRegLoc())
2685 return false;
2686 }
2687
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002688 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2689 // Therefore if it's not used by the call it is not safe to optimize this into
2690 // a sibcall.
2691 bool Unused = false;
2692 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2693 if (!Ins[i].Used) {
2694 Unused = true;
2695 break;
2696 }
2697 }
2698 if (Unused) {
2699 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002700 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2701 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002702 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002703 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002704 CCValAssign &VA = RVLocs[i];
2705 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2706 return false;
2707 }
2708 }
2709
Evan Cheng13617962010-04-30 01:12:32 +00002710 // If the calling conventions do not match, then we'd better make sure the
2711 // results are returned in the same way as what the caller expects.
2712 if (!CCMatch) {
2713 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002714 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2715 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002716 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2717
2718 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002719 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2720 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002721 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2722
2723 if (RVLocs1.size() != RVLocs2.size())
2724 return false;
2725 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2726 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2727 return false;
2728 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2729 return false;
2730 if (RVLocs1[i].isRegLoc()) {
2731 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2732 return false;
2733 } else {
2734 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2735 return false;
2736 }
2737 }
2738 }
2739
Evan Chenga6bff982010-01-30 01:22:00 +00002740 // If the callee takes no arguments then go on to check the results of the
2741 // call.
2742 if (!Outs.empty()) {
2743 // Check if stack adjustment is needed. For now, do not do this if any
2744 // argument is passed on the stack.
2745 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002746 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2747 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002748
2749 // Allocate shadow area for Win64
2750 if (Subtarget->isTargetWin64()) {
2751 CCInfo.AllocateStack(32, 8);
2752 }
2753
Duncan Sands45907662010-10-31 13:21:44 +00002754 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002755 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002756 MachineFunction &MF = DAG.getMachineFunction();
2757 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2758 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002759
2760 // Check if the arguments are already laid out in the right way as
2761 // the caller's fixed stack objects.
2762 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002763 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2764 const X86InstrInfo *TII =
2765 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002766 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2767 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002768 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002769 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002770 if (VA.getLocInfo() == CCValAssign::Indirect)
2771 return false;
2772 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002773 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2774 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002775 return false;
2776 }
2777 }
2778 }
Evan Cheng9c044672010-05-29 01:35:22 +00002779
2780 // If the tailcall address may be in a register, then make sure it's
2781 // possible to register allocate for it. In 32-bit, the call address can
2782 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002783 // callee-saved registers are restored. These happen to be the same
2784 // registers used to pass 'inreg' arguments so watch out for those.
2785 if (!Subtarget->is64Bit() &&
2786 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002787 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002788 unsigned NumInRegs = 0;
2789 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2790 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002791 if (!VA.isRegLoc())
2792 continue;
2793 unsigned Reg = VA.getLocReg();
2794 switch (Reg) {
2795 default: break;
2796 case X86::EAX: case X86::EDX: case X86::ECX:
2797 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002798 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002799 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002800 }
2801 }
2802 }
Evan Chenga6bff982010-01-30 01:22:00 +00002803 }
Evan Chengb1712452010-01-27 06:25:16 +00002804
Evan Cheng86809cc2010-02-03 03:28:02 +00002805 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002806}
2807
Dan Gohman3df24e62008-09-03 23:12:08 +00002808FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002809X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2810 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002811}
2812
2813
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002814//===----------------------------------------------------------------------===//
2815// Other Lowering Hooks
2816//===----------------------------------------------------------------------===//
2817
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002818static bool MayFoldLoad(SDValue Op) {
2819 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2820}
2821
2822static bool MayFoldIntoStore(SDValue Op) {
2823 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2824}
2825
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002826static bool isTargetShuffle(unsigned Opcode) {
2827 switch(Opcode) {
2828 default: return false;
2829 case X86ISD::PSHUFD:
2830 case X86ISD::PSHUFHW:
2831 case X86ISD::PSHUFLW:
2832 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002833 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002834 case X86ISD::SHUFPS:
2835 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002836 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002837 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002838 case X86ISD::MOVLPS:
2839 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002840 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002841 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002842 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002843 case X86ISD::MOVSS:
2844 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002845 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002846 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002847 case X86ISD::VUNPCKLPSY:
2848 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002849 case X86ISD::PUNPCKLWD:
2850 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002851 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002852 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002853 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002854 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002855 case X86ISD::VUNPCKHPSY:
2856 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002857 case X86ISD::PUNPCKHWD:
2858 case X86ISD::PUNPCKHBW:
2859 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002860 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002861 case X86ISD::VPERMILPS:
2862 case X86ISD::VPERMILPSY:
2863 case X86ISD::VPERMILPD:
2864 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002865 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002866 return true;
2867 }
2868 return false;
2869}
2870
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002871static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002872 SDValue V1, SelectionDAG &DAG) {
2873 switch(Opc) {
2874 default: llvm_unreachable("Unknown x86 shuffle node");
2875 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002876 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002877 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002878 return DAG.getNode(Opc, dl, VT, V1);
2879 }
2880
2881 return SDValue();
2882}
2883
2884static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002885 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002886 switch(Opc) {
2887 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002888 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002889 case X86ISD::PSHUFHW:
2890 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002891 case X86ISD::VPERMILPS:
2892 case X86ISD::VPERMILPSY:
2893 case X86ISD::VPERMILPD:
2894 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002895 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2896 }
2897
2898 return SDValue();
2899}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002900
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002901static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2902 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2903 switch(Opc) {
2904 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002905 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002906 case X86ISD::SHUFPD:
2907 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002908 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002909 return DAG.getNode(Opc, dl, VT, V1, V2,
2910 DAG.getConstant(TargetMask, MVT::i8));
2911 }
2912 return SDValue();
2913}
2914
2915static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2916 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2917 switch(Opc) {
2918 default: llvm_unreachable("Unknown x86 shuffle node");
2919 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002920 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002921 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002922 case X86ISD::MOVLPS:
2923 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002924 case X86ISD::MOVSS:
2925 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002926 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002927 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002928 case X86ISD::VUNPCKLPSY:
2929 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002930 case X86ISD::PUNPCKLWD:
2931 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002932 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002933 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002934 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002935 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002936 case X86ISD::VUNPCKHPSY:
2937 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002938 case X86ISD::PUNPCKHWD:
2939 case X86ISD::PUNPCKHBW:
2940 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002941 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002942 return DAG.getNode(Opc, dl, VT, V1, V2);
2943 }
2944 return SDValue();
2945}
2946
Dan Gohmand858e902010-04-17 15:26:15 +00002947SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002948 MachineFunction &MF = DAG.getMachineFunction();
2949 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2950 int ReturnAddrIndex = FuncInfo->getRAIndex();
2951
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002952 if (ReturnAddrIndex == 0) {
2953 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002954 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002955 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002956 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002957 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002958 }
2959
Evan Cheng25ab6902006-09-08 06:48:29 +00002960 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002961}
2962
2963
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002964bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2965 bool hasSymbolicDisplacement) {
2966 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002967 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002968 return false;
2969
2970 // If we don't have a symbolic displacement - we don't have any extra
2971 // restrictions.
2972 if (!hasSymbolicDisplacement)
2973 return true;
2974
2975 // FIXME: Some tweaks might be needed for medium code model.
2976 if (M != CodeModel::Small && M != CodeModel::Kernel)
2977 return false;
2978
2979 // For small code model we assume that latest object is 16MB before end of 31
2980 // bits boundary. We may also accept pretty large negative constants knowing
2981 // that all objects are in the positive half of address space.
2982 if (M == CodeModel::Small && Offset < 16*1024*1024)
2983 return true;
2984
2985 // For kernel code model we know that all object resist in the negative half
2986 // of 32bits address space. We may not accept negative offsets, since they may
2987 // be just off and we may accept pretty large positive ones.
2988 if (M == CodeModel::Kernel && Offset > 0)
2989 return true;
2990
2991 return false;
2992}
2993
Evan Chengef41ff62011-06-23 17:54:54 +00002994/// isCalleePop - Determines whether the callee is required to pop its
2995/// own arguments. Callee pop is necessary to support tail calls.
2996bool X86::isCalleePop(CallingConv::ID CallingConv,
2997 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2998 if (IsVarArg)
2999 return false;
3000
3001 switch (CallingConv) {
3002 default:
3003 return false;
3004 case CallingConv::X86_StdCall:
3005 return !is64Bit;
3006 case CallingConv::X86_FastCall:
3007 return !is64Bit;
3008 case CallingConv::X86_ThisCall:
3009 return !is64Bit;
3010 case CallingConv::Fast:
3011 return TailCallOpt;
3012 case CallingConv::GHC:
3013 return TailCallOpt;
3014 }
3015}
3016
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003017/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3018/// specific condition code, returning the condition code and the LHS/RHS of the
3019/// comparison to make.
3020static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3021 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003022 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003023 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3024 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3025 // X > -1 -> X == 0, jump !sign.
3026 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003027 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003028 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
3029 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003030 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00003031 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003032 // X < 1 -> X <= 0
3033 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003034 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003035 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003036 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003037
Evan Chengd9558e02006-01-06 00:43:03 +00003038 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003039 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003040 case ISD::SETEQ: return X86::COND_E;
3041 case ISD::SETGT: return X86::COND_G;
3042 case ISD::SETGE: return X86::COND_GE;
3043 case ISD::SETLT: return X86::COND_L;
3044 case ISD::SETLE: return X86::COND_LE;
3045 case ISD::SETNE: return X86::COND_NE;
3046 case ISD::SETULT: return X86::COND_B;
3047 case ISD::SETUGT: return X86::COND_A;
3048 case ISD::SETULE: return X86::COND_BE;
3049 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003050 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003051 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003052
Chris Lattner4c78e022008-12-23 23:42:27 +00003053 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003054
Chris Lattner4c78e022008-12-23 23:42:27 +00003055 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003056 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3057 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003058 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3059 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003060 }
3061
Chris Lattner4c78e022008-12-23 23:42:27 +00003062 switch (SetCCOpcode) {
3063 default: break;
3064 case ISD::SETOLT:
3065 case ISD::SETOLE:
3066 case ISD::SETUGT:
3067 case ISD::SETUGE:
3068 std::swap(LHS, RHS);
3069 break;
3070 }
3071
3072 // On a floating point condition, the flags are set as follows:
3073 // ZF PF CF op
3074 // 0 | 0 | 0 | X > Y
3075 // 0 | 0 | 1 | X < Y
3076 // 1 | 0 | 0 | X == Y
3077 // 1 | 1 | 1 | unordered
3078 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003079 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003080 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003081 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003082 case ISD::SETOLT: // flipped
3083 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003084 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003085 case ISD::SETOLE: // flipped
3086 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003087 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003088 case ISD::SETUGT: // flipped
3089 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003090 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003091 case ISD::SETUGE: // flipped
3092 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003093 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003094 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003095 case ISD::SETNE: return X86::COND_NE;
3096 case ISD::SETUO: return X86::COND_P;
3097 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003098 case ISD::SETOEQ:
3099 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003100 }
Evan Chengd9558e02006-01-06 00:43:03 +00003101}
3102
Evan Cheng4a460802006-01-11 00:33:36 +00003103/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3104/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003105/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003106static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003107 switch (X86CC) {
3108 default:
3109 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003110 case X86::COND_B:
3111 case X86::COND_BE:
3112 case X86::COND_E:
3113 case X86::COND_P:
3114 case X86::COND_A:
3115 case X86::COND_AE:
3116 case X86::COND_NE:
3117 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003118 return true;
3119 }
3120}
3121
Evan Chengeb2f9692009-10-27 19:56:55 +00003122/// isFPImmLegal - Returns true if the target can instruction select the
3123/// specified FP immediate natively. If false, the legalizer will
3124/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003125bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003126 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3127 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3128 return true;
3129 }
3130 return false;
3131}
3132
Nate Begeman9008ca62009-04-27 18:41:29 +00003133/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3134/// the specified range (L, H].
3135static bool isUndefOrInRange(int Val, int Low, int Hi) {
3136 return (Val < 0) || (Val >= Low && Val < Hi);
3137}
3138
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003139/// isUndefOrInRange - Return true if every element in Mask, begining
3140/// from position Pos and ending in Pos+Size, falls within the specified
3141/// range (L, L+Pos]. or is undef.
3142static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3143 int Pos, int Size, int Low, int Hi) {
3144 for (int i = Pos, e = Pos+Size; i != e; ++i)
3145 if (!isUndefOrInRange(Mask[i], Low, Hi))
3146 return false;
3147 return true;
3148}
3149
Nate Begeman9008ca62009-04-27 18:41:29 +00003150/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3151/// specified value.
3152static bool isUndefOrEqual(int Val, int CmpVal) {
3153 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003154 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003155 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003156}
3157
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003158/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3159/// from position Pos and ending in Pos+Size, falls within the specified
3160/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003161static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3162 int Pos, int Size, int Low) {
3163 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3164 if (!isUndefOrEqual(Mask[i], Low))
3165 return false;
3166 return true;
3167}
3168
Nate Begeman9008ca62009-04-27 18:41:29 +00003169/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3170/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3171/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003172static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003173 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003174 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003175 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003176 return (Mask[0] < 2 && Mask[1] < 2);
3177 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003178}
3179
Nate Begeman9008ca62009-04-27 18:41:29 +00003180bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003181 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003182 N->getMask(M);
3183 return ::isPSHUFDMask(M, N->getValueType(0));
3184}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003185
Nate Begeman9008ca62009-04-27 18:41:29 +00003186/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3187/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003188static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003189 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003190 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003191
Nate Begeman9008ca62009-04-27 18:41:29 +00003192 // Lower quadword copied in order or undef.
3193 for (int i = 0; i != 4; ++i)
3194 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003195 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003196
Evan Cheng506d3df2006-03-29 23:07:14 +00003197 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003198 for (int i = 4; i != 8; ++i)
3199 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003200 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003201
Evan Cheng506d3df2006-03-29 23:07:14 +00003202 return true;
3203}
3204
Nate Begeman9008ca62009-04-27 18:41:29 +00003205bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003206 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003207 N->getMask(M);
3208 return ::isPSHUFHWMask(M, N->getValueType(0));
3209}
Evan Cheng506d3df2006-03-29 23:07:14 +00003210
Nate Begeman9008ca62009-04-27 18:41:29 +00003211/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3212/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003213static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003214 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003215 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003216
Rafael Espindola15684b22009-04-24 12:40:33 +00003217 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003218 for (int i = 4; i != 8; ++i)
3219 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003220 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003221
Rafael Espindola15684b22009-04-24 12:40:33 +00003222 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003223 for (int i = 0; i != 4; ++i)
3224 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003225 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003226
Rafael Espindola15684b22009-04-24 12:40:33 +00003227 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003228}
3229
Nate Begeman9008ca62009-04-27 18:41:29 +00003230bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003231 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003232 N->getMask(M);
3233 return ::isPSHUFLWMask(M, N->getValueType(0));
3234}
3235
Nate Begemana09008b2009-10-19 02:17:23 +00003236/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3237/// is suitable for input to PALIGNR.
3238static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003239 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003240 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003241 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3242 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003243
Nate Begemana09008b2009-10-19 02:17:23 +00003244 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003245 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003246 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003247
Nate Begemana09008b2009-10-19 02:17:23 +00003248 for (i = 0; i != e; ++i)
3249 if (Mask[i] >= 0)
3250 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003251
Nate Begemana09008b2009-10-19 02:17:23 +00003252 // All undef, not a palignr.
3253 if (i == e)
3254 return false;
3255
Eli Friedman63f8dde2011-07-25 21:36:45 +00003256 // Make sure we're shifting in the right direction.
3257 if (Mask[i] <= i)
3258 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003259
3260 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003261
Nate Begemana09008b2009-10-19 02:17:23 +00003262 // Check the rest of the elements to see if they are consecutive.
3263 for (++i; i != e; ++i) {
3264 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003265 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003266 return false;
3267 }
3268 return true;
3269}
3270
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003271/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3272/// specifies a shuffle of elements that is suitable for input to 256-bit
3273/// VSHUFPSY.
3274static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3275 const X86Subtarget *Subtarget) {
3276 int NumElems = VT.getVectorNumElements();
3277
3278 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3279 return false;
3280
3281 if (NumElems != 8)
3282 return false;
3283
3284 // VSHUFPSY 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 => X7 X6 X5 X4 X3 X2 X1 X0
3289 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3290 //
3291 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3292 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3293 //
3294 int QuarterSize = NumElems/4;
3295 int HalfSize = QuarterSize*2;
3296 for (int i = 0; i < QuarterSize; ++i)
3297 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3298 return false;
3299 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3300 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3301 return false;
3302
3303 // The mask of the second half must be the same as the first but with
3304 // the appropriate offsets. This works in the same way as VPERMILPS
3305 // works with masks.
3306 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3307 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3308 return false;
3309 int FstHalfIdx = i-HalfSize;
3310 if (Mask[FstHalfIdx] < 0)
3311 continue;
3312 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3313 return false;
3314 }
3315 for (int i = QuarterSize*3; i < NumElems; ++i) {
3316 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3317 return false;
3318 int FstHalfIdx = i-HalfSize;
3319 if (Mask[FstHalfIdx] < 0)
3320 continue;
3321 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3322 return false;
3323
3324 }
3325
3326 return true;
3327}
3328
3329/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3330/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3331static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3332 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3333 EVT VT = SVOp->getValueType(0);
3334 int NumElems = VT.getVectorNumElements();
3335
3336 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3337 "Only supports v8i32 and v8f32 types");
3338
3339 int HalfSize = NumElems/2;
3340 unsigned Mask = 0;
3341 for (int i = 0; i != NumElems ; ++i) {
3342 if (SVOp->getMaskElt(i) < 0)
3343 continue;
3344 // The mask of the first half must be equal to the second one.
3345 unsigned Shamt = (i%HalfSize)*2;
3346 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3347 Mask |= Elt << Shamt;
3348 }
3349
3350 return Mask;
3351}
3352
3353/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3354/// specifies a shuffle of elements that is suitable for input to 256-bit
3355/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3356/// version and the mask of the second half isn't binded with the first
3357/// one.
3358static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3359 const X86Subtarget *Subtarget) {
3360 int NumElems = VT.getVectorNumElements();
3361
3362 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3363 return false;
3364
3365 if (NumElems != 4)
3366 return false;
3367
3368 // VSHUFPSY divides the resulting vector into 4 chunks.
3369 // The sources are also splitted into 4 chunks, and each destination
3370 // chunk must come from a different source chunk.
3371 //
3372 // SRC1 => X3 X2 X1 X0
3373 // SRC2 => Y3 Y2 Y1 Y0
3374 //
3375 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3376 //
3377 int QuarterSize = NumElems/4;
3378 int HalfSize = QuarterSize*2;
3379 for (int i = 0; i < QuarterSize; ++i)
3380 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3381 return false;
3382 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3383 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3384 return false;
3385 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3386 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3387 return false;
3388 for (int i = QuarterSize*3; i < NumElems; ++i)
3389 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3390 return false;
3391
3392 return true;
3393}
3394
3395/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3396/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3397static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3398 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3399 EVT VT = SVOp->getValueType(0);
3400 int NumElems = VT.getVectorNumElements();
3401
3402 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3403 "Only supports v4i64 and v4f64 types");
3404
3405 int HalfSize = NumElems/2;
3406 unsigned Mask = 0;
3407 for (int i = 0; i != NumElems ; ++i) {
3408 if (SVOp->getMaskElt(i) < 0)
3409 continue;
3410 int Elt = SVOp->getMaskElt(i) % HalfSize;
3411 Mask |= Elt << i;
3412 }
3413
3414 return Mask;
3415}
3416
Evan Cheng14aed5e2006-03-24 01:18:28 +00003417/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003418/// specifies a shuffle of elements that is suitable for input to 128-bit
3419/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003420static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003421 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003422
3423 if (VT.getSizeInBits() != 128)
3424 return false;
3425
Nate Begeman9008ca62009-04-27 18:41:29 +00003426 if (NumElems != 2 && NumElems != 4)
3427 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003428
Nate Begeman9008ca62009-04-27 18:41:29 +00003429 int Half = NumElems / 2;
3430 for (int i = 0; i < Half; ++i)
3431 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003432 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003433 for (int i = Half; i < NumElems; ++i)
3434 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003435 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003436
Evan Cheng14aed5e2006-03-24 01:18:28 +00003437 return true;
3438}
3439
Nate Begeman9008ca62009-04-27 18:41:29 +00003440bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3441 SmallVector<int, 8> M;
3442 N->getMask(M);
3443 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003444}
3445
Evan Cheng213d2cf2007-05-17 18:45:50 +00003446/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003447/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3448/// half elements to come from vector 1 (which would equal the dest.) and
3449/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003450static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003451 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003452
3453 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003454 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003455
Nate Begeman9008ca62009-04-27 18:41:29 +00003456 int Half = NumElems / 2;
3457 for (int i = 0; i < Half; ++i)
3458 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003459 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003460 for (int i = Half; i < NumElems; ++i)
3461 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003462 return false;
3463 return true;
3464}
3465
Nate Begeman9008ca62009-04-27 18:41:29 +00003466static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3467 SmallVector<int, 8> M;
3468 N->getMask(M);
3469 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003470}
3471
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003472/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3473/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003474bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003475 EVT VT = N->getValueType(0);
3476 unsigned NumElems = VT.getVectorNumElements();
3477
3478 if (VT.getSizeInBits() != 128)
3479 return false;
3480
3481 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003482 return false;
3483
Evan Cheng2064a2b2006-03-28 06:50:32 +00003484 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003485 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3486 isUndefOrEqual(N->getMaskElt(1), 7) &&
3487 isUndefOrEqual(N->getMaskElt(2), 2) &&
3488 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003489}
3490
Nate Begeman0b10b912009-11-07 23:17:15 +00003491/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3492/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3493/// <2, 3, 2, 3>
3494bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003495 EVT VT = N->getValueType(0);
3496 unsigned NumElems = VT.getVectorNumElements();
3497
3498 if (VT.getSizeInBits() != 128)
3499 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003500
Nate Begeman0b10b912009-11-07 23:17:15 +00003501 if (NumElems != 4)
3502 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003503
Nate Begeman0b10b912009-11-07 23:17:15 +00003504 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003505 isUndefOrEqual(N->getMaskElt(1), 3) &&
3506 isUndefOrEqual(N->getMaskElt(2), 2) &&
3507 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003508}
3509
Evan Cheng5ced1d82006-04-06 23:23:56 +00003510/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3511/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003512bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3513 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003514
Evan Cheng5ced1d82006-04-06 23:23:56 +00003515 if (NumElems != 2 && NumElems != 4)
3516 return false;
3517
Evan Chengc5cdff22006-04-07 21:53:05 +00003518 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003519 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003520 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003521
Evan Chengc5cdff22006-04-07 21:53:05 +00003522 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003523 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003524 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003525
3526 return true;
3527}
3528
Nate Begeman0b10b912009-11-07 23:17:15 +00003529/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3530/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3531bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003532 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003533
David Greenea20244d2011-03-02 17:23:43 +00003534 if ((NumElems != 2 && NumElems != 4)
3535 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003536 return false;
3537
Evan Chengc5cdff22006-04-07 21:53:05 +00003538 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003539 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003540 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003541
Nate Begeman9008ca62009-04-27 18:41:29 +00003542 for (unsigned i = 0; i < NumElems/2; ++i)
3543 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003544 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003545
3546 return true;
3547}
3548
Evan Cheng0038e592006-03-28 00:39:58 +00003549/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3550/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003551static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003552 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003553 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003554
3555 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3556 "Unsupported vector type for unpckh");
3557
3558 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003559 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003560
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003561 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3562 // independently on 128-bit lanes.
3563 unsigned NumLanes = VT.getSizeInBits()/128;
3564 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003565
3566 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003567 unsigned End = NumLaneElts;
3568 for (unsigned s = 0; s < NumLanes; ++s) {
3569 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003570 i != End;
3571 i += 2, ++j) {
3572 int BitI = Mask[i];
3573 int BitI1 = Mask[i+1];
3574 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003575 return false;
David Greenea20244d2011-03-02 17:23:43 +00003576 if (V2IsSplat) {
3577 if (!isUndefOrEqual(BitI1, NumElts))
3578 return false;
3579 } else {
3580 if (!isUndefOrEqual(BitI1, j + NumElts))
3581 return false;
3582 }
Evan Cheng39623da2006-04-20 08:58:49 +00003583 }
David Greenea20244d2011-03-02 17:23:43 +00003584 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003585 Start += NumLaneElts;
3586 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003587 }
David Greenea20244d2011-03-02 17:23:43 +00003588
Evan Cheng0038e592006-03-28 00:39:58 +00003589 return true;
3590}
3591
Nate Begeman9008ca62009-04-27 18:41:29 +00003592bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3593 SmallVector<int, 8> M;
3594 N->getMask(M);
3595 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003596}
3597
Evan Cheng4fcb9222006-03-28 02:43:26 +00003598/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3599/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003600static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003601 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003602 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003603
3604 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3605 "Unsupported vector type for unpckh");
3606
3607 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003608 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003609
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003610 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3611 // independently on 128-bit lanes.
3612 unsigned NumLanes = VT.getSizeInBits()/128;
3613 unsigned NumLaneElts = NumElts/NumLanes;
3614
3615 unsigned Start = 0;
3616 unsigned End = NumLaneElts;
3617 for (unsigned l = 0; l != NumLanes; ++l) {
3618 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3619 i != End; i += 2, ++j) {
3620 int BitI = Mask[i];
3621 int BitI1 = Mask[i+1];
3622 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003623 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003624 if (V2IsSplat) {
3625 if (isUndefOrEqual(BitI1, NumElts))
3626 return false;
3627 } else {
3628 if (!isUndefOrEqual(BitI1, j+NumElts))
3629 return false;
3630 }
Evan Cheng39623da2006-04-20 08:58:49 +00003631 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003632 // Process the next 128 bits.
3633 Start += NumLaneElts;
3634 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003635 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003636 return true;
3637}
3638
Nate Begeman9008ca62009-04-27 18:41:29 +00003639bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3640 SmallVector<int, 8> M;
3641 N->getMask(M);
3642 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003643}
3644
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003645/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3646/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3647/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003648static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003649 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003650 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003651 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003652
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003653 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3654 // FIXME: Need a better way to get rid of this, there's no latency difference
3655 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3656 // the former later. We should also remove the "_undef" special mask.
3657 if (NumElems == 4 && VT.getSizeInBits() == 256)
3658 return false;
3659
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003660 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3661 // independently on 128-bit lanes.
3662 unsigned NumLanes = VT.getSizeInBits() / 128;
3663 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003664
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003665 for (unsigned s = 0; s < NumLanes; ++s) {
3666 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3667 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003668 i += 2, ++j) {
3669 int BitI = Mask[i];
3670 int BitI1 = Mask[i+1];
3671
3672 if (!isUndefOrEqual(BitI, j))
3673 return false;
3674 if (!isUndefOrEqual(BitI1, j))
3675 return false;
3676 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003677 }
David Greenea20244d2011-03-02 17:23:43 +00003678
Rafael Espindola15684b22009-04-24 12:40:33 +00003679 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003680}
3681
Nate Begeman9008ca62009-04-27 18:41:29 +00003682bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3683 SmallVector<int, 8> M;
3684 N->getMask(M);
3685 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3686}
3687
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003688/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3689/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3690/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003691static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003692 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003693 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3694 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003695
Nate Begeman9008ca62009-04-27 18:41:29 +00003696 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3697 int BitI = Mask[i];
3698 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003699 if (!isUndefOrEqual(BitI, j))
3700 return false;
3701 if (!isUndefOrEqual(BitI1, j))
3702 return false;
3703 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003704 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003705}
3706
Nate Begeman9008ca62009-04-27 18:41:29 +00003707bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3708 SmallVector<int, 8> M;
3709 N->getMask(M);
3710 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3711}
3712
Evan Cheng017dcc62006-04-21 01:05:10 +00003713/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3714/// specifies a shuffle of elements that is suitable for input to MOVSS,
3715/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003716static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003717 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003718 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003719
3720 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003721
Nate Begeman9008ca62009-04-27 18:41:29 +00003722 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003723 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003724
Nate Begeman9008ca62009-04-27 18:41:29 +00003725 for (int i = 1; i < NumElts; ++i)
3726 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003727 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003728
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003729 return true;
3730}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003731
Nate Begeman9008ca62009-04-27 18:41:29 +00003732bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3733 SmallVector<int, 8> M;
3734 N->getMask(M);
3735 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003736}
3737
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003738/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3739/// as permutations between 128-bit chunks or halves. As an example: this
3740/// shuffle bellow:
3741/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3742/// The first half comes from the second half of V1 and the second half from the
3743/// the second half of V2.
3744static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3745 const X86Subtarget *Subtarget) {
3746 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3747 return false;
3748
3749 // The shuffle result is divided into half A and half B. In total the two
3750 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3751 // B must come from C, D, E or F.
3752 int HalfSize = VT.getVectorNumElements()/2;
3753 bool MatchA = false, MatchB = false;
3754
3755 // Check if A comes from one of C, D, E, F.
3756 for (int Half = 0; Half < 4; ++Half) {
3757 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3758 MatchA = true;
3759 break;
3760 }
3761 }
3762
3763 // Check if B comes from one of C, D, E, F.
3764 for (int Half = 0; Half < 4; ++Half) {
3765 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3766 MatchB = true;
3767 break;
3768 }
3769 }
3770
3771 return MatchA && MatchB;
3772}
3773
3774/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3775/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3776static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3777 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3778 EVT VT = SVOp->getValueType(0);
3779
3780 int HalfSize = VT.getVectorNumElements()/2;
3781
3782 int FstHalf = 0, SndHalf = 0;
3783 for (int i = 0; i < HalfSize; ++i) {
3784 if (SVOp->getMaskElt(i) > 0) {
3785 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3786 break;
3787 }
3788 }
3789 for (int i = HalfSize; i < HalfSize*2; ++i) {
3790 if (SVOp->getMaskElt(i) > 0) {
3791 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3792 break;
3793 }
3794 }
3795
3796 return (FstHalf | (SndHalf << 4));
3797}
3798
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003799/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3800/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3801/// Note that VPERMIL mask matching is different depending whether theunderlying
3802/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3803/// to the same elements of the low, but to the higher half of the source.
3804/// In VPERMILPD the two lanes could be shuffled independently of each other
3805/// with the same restriction that lanes can't be crossed.
3806static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3807 const X86Subtarget *Subtarget) {
3808 int NumElts = VT.getVectorNumElements();
3809 int NumLanes = VT.getSizeInBits()/128;
3810
3811 if (!Subtarget->hasAVX())
3812 return false;
3813
Eli Friedmandca62d52011-10-10 22:28:47 +00003814 // Only match 256-bit with 64-bit types
3815 if (VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003816 return false;
3817
3818 // The mask on the high lane is independent of the low. Both can match
3819 // any element in inside its own lane, but can't cross.
3820 int LaneSize = NumElts/NumLanes;
3821 for (int l = 0; l < NumLanes; ++l)
3822 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3823 int LaneStart = l*LaneSize;
3824 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3825 return false;
3826 }
3827
3828 return true;
3829}
3830
3831/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3832/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3833/// Note that VPERMIL mask matching is different depending whether theunderlying
3834/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3835/// to the same elements of the low, but to the higher half of the source.
3836/// In VPERMILPD the two lanes could be shuffled independently of each other
3837/// with the same restriction that lanes can't be crossed.
3838static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3839 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003840 unsigned NumElts = VT.getVectorNumElements();
3841 unsigned NumLanes = VT.getSizeInBits()/128;
3842
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003843 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003844 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003845
Eli Friedmandca62d52011-10-10 22:28:47 +00003846 // Only match 256-bit with 32-bit types
3847 if (VT.getSizeInBits() != 256 || NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003848 return false;
3849
3850 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003851 // they can differ if any of the corresponding index in a lane is undef
3852 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003853 int LaneSize = NumElts/NumLanes;
3854 for (int i = 0; i < LaneSize; ++i) {
3855 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003856 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3857 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3858
3859 if (!HighValid || !LowValid)
3860 return false;
3861 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003862 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003863 if (Mask[HighElt]-Mask[i] != LaneSize)
3864 return false;
3865 }
3866
3867 return true;
3868}
3869
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003870/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3871/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3872static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003873 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3874 EVT VT = SVOp->getValueType(0);
3875
3876 int NumElts = VT.getVectorNumElements();
3877 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003878 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003879
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003880 // Although the mask is equal for both lanes do it twice to get the cases
3881 // where a mask will match because the same mask element is undef on the
3882 // first half but valid on the second. This would get pathological cases
3883 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003884 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003885 for (int l = 0; l < NumLanes; ++l) {
3886 for (int i = 0; i < LaneSize; ++i) {
3887 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3888 if (MaskElt < 0)
3889 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003890 if (MaskElt >= LaneSize)
3891 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003892 Mask |= MaskElt << (i*2);
3893 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003894 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003895
3896 return Mask;
3897}
3898
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003899/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3900/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3901static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3902 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3903 EVT VT = SVOp->getValueType(0);
3904
3905 int NumElts = VT.getVectorNumElements();
3906 int NumLanes = VT.getSizeInBits()/128;
3907
3908 unsigned Mask = 0;
3909 int LaneSize = NumElts/NumLanes;
3910 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003911 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3912 int MaskElt = SVOp->getMaskElt(i);
3913 if (MaskElt < 0)
3914 continue;
3915 Mask |= (MaskElt-l*LaneSize) << i;
3916 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003917
3918 return Mask;
3919}
3920
Evan Cheng017dcc62006-04-21 01:05:10 +00003921/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3922/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003923/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003924static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003925 bool V2IsSplat = false, bool V2IsUndef = false) {
3926 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003927 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003928 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003929
Nate Begeman9008ca62009-04-27 18:41:29 +00003930 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003931 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003932
Nate Begeman9008ca62009-04-27 18:41:29 +00003933 for (int i = 1; i < NumOps; ++i)
3934 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3935 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3936 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003937 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003938
Evan Cheng39623da2006-04-20 08:58:49 +00003939 return true;
3940}
3941
Nate Begeman9008ca62009-04-27 18:41:29 +00003942static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003943 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003944 SmallVector<int, 8> M;
3945 N->getMask(M);
3946 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003947}
3948
Evan Chengd9539472006-04-14 21:59:03 +00003949/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3950/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003951/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3952bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3953 const X86Subtarget *Subtarget) {
3954 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003955 return false;
3956
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003957 // The second vector must be undef
3958 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3959 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003960
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003961 EVT VT = N->getValueType(0);
3962 unsigned NumElems = VT.getVectorNumElements();
3963
3964 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3965 (VT.getSizeInBits() == 256 && NumElems != 8))
3966 return false;
3967
3968 // "i+1" is the value the indexed mask element must have
3969 for (unsigned i = 0; i < NumElems; i += 2)
3970 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3971 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003972 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003973
3974 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003975}
3976
3977/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3978/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003979/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3980bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3981 const X86Subtarget *Subtarget) {
3982 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003983 return false;
3984
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003985 // The second vector must be undef
3986 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3987 return false;
3988
3989 EVT VT = N->getValueType(0);
3990 unsigned NumElems = VT.getVectorNumElements();
3991
3992 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3993 (VT.getSizeInBits() == 256 && NumElems != 8))
3994 return false;
3995
3996 // "i" is the value the indexed mask element must have
3997 for (unsigned i = 0; i < NumElems; i += 2)
3998 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3999 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00004000 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00004001
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004002 return true;
Evan Chengd9539472006-04-14 21:59:03 +00004003}
4004
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004005/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
4006/// specifies a shuffle of elements that is suitable for input to 256-bit
4007/// version of MOVDDUP.
4008static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
4009 const X86Subtarget *Subtarget) {
4010 EVT VT = N->getValueType(0);
4011 int NumElts = VT.getVectorNumElements();
4012 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
4013
4014 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
4015 !V2IsUndef || NumElts != 4)
4016 return false;
4017
4018 for (int i = 0; i != NumElts/2; ++i)
4019 if (!isUndefOrEqual(N->getMaskElt(i), 0))
4020 return false;
4021 for (int i = NumElts/2; i != NumElts; ++i)
4022 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
4023 return false;
4024 return true;
4025}
4026
Evan Cheng0b457f02008-09-25 20:50:48 +00004027/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004028/// specifies a shuffle of elements that is suitable for input to 128-bit
4029/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00004030bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004031 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004032
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004033 if (VT.getSizeInBits() != 128)
4034 return false;
4035
4036 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004037 for (int i = 0; i < e; ++i)
4038 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004039 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004040 for (int i = 0; i < e; ++i)
4041 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004042 return false;
4043 return true;
4044}
4045
David Greenec38a03e2011-02-03 15:50:00 +00004046/// isVEXTRACTF128Index - Return true if the specified
4047/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4048/// suitable for input to VEXTRACTF128.
4049bool X86::isVEXTRACTF128Index(SDNode *N) {
4050 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4051 return false;
4052
4053 // The index should be aligned on a 128-bit boundary.
4054 uint64_t Index =
4055 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4056
4057 unsigned VL = N->getValueType(0).getVectorNumElements();
4058 unsigned VBits = N->getValueType(0).getSizeInBits();
4059 unsigned ElSize = VBits / VL;
4060 bool Result = (Index * ElSize) % 128 == 0;
4061
4062 return Result;
4063}
4064
David Greeneccacdc12011-02-04 16:08:29 +00004065/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4066/// operand specifies a subvector insert that is suitable for input to
4067/// VINSERTF128.
4068bool X86::isVINSERTF128Index(SDNode *N) {
4069 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4070 return false;
4071
4072 // The index should be aligned on a 128-bit boundary.
4073 uint64_t Index =
4074 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4075
4076 unsigned VL = N->getValueType(0).getVectorNumElements();
4077 unsigned VBits = N->getValueType(0).getSizeInBits();
4078 unsigned ElSize = VBits / VL;
4079 bool Result = (Index * ElSize) % 128 == 0;
4080
4081 return Result;
4082}
4083
Evan Cheng63d33002006-03-22 08:01:21 +00004084/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004085/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004086unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004087 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4088 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4089
Evan Chengb9df0ca2006-03-22 02:53:00 +00004090 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4091 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004092 for (int i = 0; i < NumOperands; ++i) {
4093 int Val = SVOp->getMaskElt(NumOperands-i-1);
4094 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004095 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004096 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004097 if (i != NumOperands - 1)
4098 Mask <<= Shift;
4099 }
Evan Cheng63d33002006-03-22 08:01:21 +00004100 return Mask;
4101}
4102
Evan Cheng506d3df2006-03-29 23:07:14 +00004103/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004104/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004105unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004106 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004107 unsigned Mask = 0;
4108 // 8 nodes, but we only care about the last 4.
4109 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004110 int Val = SVOp->getMaskElt(i);
4111 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004112 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004113 if (i != 4)
4114 Mask <<= 2;
4115 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004116 return Mask;
4117}
4118
4119/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004120/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004121unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004122 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004123 unsigned Mask = 0;
4124 // 8 nodes, but we only care about the first 4.
4125 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004126 int Val = SVOp->getMaskElt(i);
4127 if (Val >= 0)
4128 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004129 if (i != 0)
4130 Mask <<= 2;
4131 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004132 return Mask;
4133}
4134
Nate Begemana09008b2009-10-19 02:17:23 +00004135/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4136/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4137unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4138 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4139 EVT VVT = N->getValueType(0);
4140 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4141 int Val = 0;
4142
4143 unsigned i, e;
4144 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4145 Val = SVOp->getMaskElt(i);
4146 if (Val >= 0)
4147 break;
4148 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004149 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004150 return (Val - i) * EltSize;
4151}
4152
David Greenec38a03e2011-02-03 15:50:00 +00004153/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4154/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4155/// instructions.
4156unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4157 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4158 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4159
4160 uint64_t Index =
4161 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4162
4163 EVT VecVT = N->getOperand(0).getValueType();
4164 EVT ElVT = VecVT.getVectorElementType();
4165
4166 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004167 return Index / NumElemsPerChunk;
4168}
4169
David Greeneccacdc12011-02-04 16:08:29 +00004170/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4171/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4172/// instructions.
4173unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4174 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4175 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4176
4177 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004178 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004179
4180 EVT VecVT = N->getValueType(0);
4181 EVT ElVT = VecVT.getVectorElementType();
4182
4183 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004184 return Index / NumElemsPerChunk;
4185}
4186
Evan Cheng37b73872009-07-30 08:33:02 +00004187/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4188/// constant +0.0.
4189bool X86::isZeroNode(SDValue Elt) {
4190 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004191 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004192 (isa<ConstantFPSDNode>(Elt) &&
4193 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4194}
4195
Nate Begeman9008ca62009-04-27 18:41:29 +00004196/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4197/// their permute mask.
4198static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4199 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004200 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004201 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004202 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004203
Nate Begeman5a5ca152009-04-29 05:20:52 +00004204 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004205 int idx = SVOp->getMaskElt(i);
4206 if (idx < 0)
4207 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004208 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004209 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004210 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004211 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004212 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004213 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4214 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004215}
4216
Evan Cheng779ccea2007-12-07 21:30:01 +00004217/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4218/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004219static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004220 unsigned NumElems = VT.getVectorNumElements();
4221 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004222 int idx = Mask[i];
4223 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004224 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004225 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004226 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004227 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004228 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004229 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004230}
4231
Evan Cheng533a0aa2006-04-19 20:35:22 +00004232/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4233/// match movhlps. The lower half elements should come from upper half of
4234/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004235/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004236static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004237 EVT VT = Op->getValueType(0);
4238 if (VT.getSizeInBits() != 128)
4239 return false;
4240 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004241 return false;
4242 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004243 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004244 return false;
4245 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004246 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004247 return false;
4248 return true;
4249}
4250
Evan Cheng5ced1d82006-04-06 23:23:56 +00004251/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004252/// is promoted to a vector. It also returns the LoadSDNode by reference if
4253/// required.
4254static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004255 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4256 return false;
4257 N = N->getOperand(0).getNode();
4258 if (!ISD::isNON_EXTLoad(N))
4259 return false;
4260 if (LD)
4261 *LD = cast<LoadSDNode>(N);
4262 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004263}
4264
Dan Gohman65fd6562011-11-03 21:49:52 +00004265// Test whether the given value is a vector value which will be legalized
4266// into a load.
4267static bool WillBeConstantPoolLoad(SDNode *N) {
4268 if (N->getOpcode() != ISD::BUILD_VECTOR)
4269 return false;
4270
4271 // Check for any non-constant elements.
4272 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4273 switch (N->getOperand(i).getNode()->getOpcode()) {
4274 case ISD::UNDEF:
4275 case ISD::ConstantFP:
4276 case ISD::Constant:
4277 break;
4278 default:
4279 return false;
4280 }
4281
4282 // Vectors of all-zeros and all-ones are materialized with special
4283 // instructions rather than being loaded.
4284 return !ISD::isBuildVectorAllZeros(N) &&
4285 !ISD::isBuildVectorAllOnes(N);
4286}
4287
Evan Cheng533a0aa2006-04-19 20:35:22 +00004288/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4289/// match movlp{s|d}. The lower half elements should come from lower half of
4290/// V1 (and in order), and the upper half elements should come from the upper
4291/// half of V2 (and in order). And since V1 will become the source of the
4292/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004293static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4294 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004295 EVT VT = Op->getValueType(0);
4296 if (VT.getSizeInBits() != 128)
4297 return false;
4298
Evan Cheng466685d2006-10-09 20:57:25 +00004299 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004300 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004301 // Is V2 is a vector load, don't do this transformation. We will try to use
4302 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004303 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004304 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004305
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004306 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004307
Evan Cheng533a0aa2006-04-19 20:35:22 +00004308 if (NumElems != 2 && NumElems != 4)
4309 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004310 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004311 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004312 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004313 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004314 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004315 return false;
4316 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004317}
4318
Evan Cheng39623da2006-04-20 08:58:49 +00004319/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4320/// all the same.
4321static bool isSplatVector(SDNode *N) {
4322 if (N->getOpcode() != ISD::BUILD_VECTOR)
4323 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004324
Dan Gohman475871a2008-07-27 21:46:04 +00004325 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004326 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4327 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004328 return false;
4329 return true;
4330}
4331
Evan Cheng213d2cf2007-05-17 18:45:50 +00004332/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004333/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004334/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004335static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004336 SDValue V1 = N->getOperand(0);
4337 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004338 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4339 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004340 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004341 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004342 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004343 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4344 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004345 if (Opc != ISD::BUILD_VECTOR ||
4346 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004347 return false;
4348 } else if (Idx >= 0) {
4349 unsigned Opc = V1.getOpcode();
4350 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4351 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004352 if (Opc != ISD::BUILD_VECTOR ||
4353 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004354 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004355 }
4356 }
4357 return true;
4358}
4359
4360/// getZeroVector - Returns a vector of specified type with all zero elements.
4361///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004362static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004363 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004364 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004365
Dale Johannesen0488fb62010-09-30 23:57:10 +00004366 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004367 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004368 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004369 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004370 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004371 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4372 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4373 } else { // SSE1
4374 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4375 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4376 }
4377 } else if (VT.getSizeInBits() == 256) { // AVX
4378 // 256-bit logic and arithmetic instructions in AVX are
4379 // all floating-point, no support for integer ops. Default
4380 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004381 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004382 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4383 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004384 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004385 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004386}
4387
Chris Lattner8a594482007-11-25 00:24:49 +00004388/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004389/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4390/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4391/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004392static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004393 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004394 assert((VT.is128BitVector() || VT.is256BitVector())
4395 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004396
Owen Anderson825b72b2009-08-11 20:47:22 +00004397 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004398 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4399 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004400
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004401 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004402 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4403 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4404 Vec = Insert128BitVector(InsV, Vec,
4405 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4406 }
4407
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004408 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004409}
4410
Evan Cheng39623da2006-04-20 08:58:49 +00004411/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4412/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004413static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004414 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004415 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004416
Evan Cheng39623da2006-04-20 08:58:49 +00004417 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004418 SmallVector<int, 8> MaskVec;
4419 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004420
Nate Begeman5a5ca152009-04-29 05:20:52 +00004421 for (unsigned i = 0; i != NumElems; ++i) {
4422 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004423 MaskVec[i] = NumElems;
4424 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004425 }
Evan Cheng39623da2006-04-20 08:58:49 +00004426 }
Evan Cheng39623da2006-04-20 08:58:49 +00004427 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004428 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4429 SVOp->getOperand(1), &MaskVec[0]);
4430 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004431}
4432
Evan Cheng017dcc62006-04-21 01:05:10 +00004433/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4434/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004435static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004436 SDValue V2) {
4437 unsigned NumElems = VT.getVectorNumElements();
4438 SmallVector<int, 8> Mask;
4439 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004440 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004441 Mask.push_back(i);
4442 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004443}
4444
Nate Begeman9008ca62009-04-27 18:41:29 +00004445/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004446static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004447 SDValue V2) {
4448 unsigned NumElems = VT.getVectorNumElements();
4449 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004450 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004451 Mask.push_back(i);
4452 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004453 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004454 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004455}
4456
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004457/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004458static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004459 SDValue V2) {
4460 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004461 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004462 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004463 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004464 Mask.push_back(i + Half);
4465 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004466 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004467 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004468}
4469
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004470// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004471// a generic shuffle instruction because the target has no such instructions.
4472// Generate shuffles which repeat i16 and i8 several times until they can be
4473// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004474static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004475 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004476 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004477 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004478
Nate Begeman9008ca62009-04-27 18:41:29 +00004479 while (NumElems > 4) {
4480 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004481 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004482 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004483 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004484 EltNo -= NumElems/2;
4485 }
4486 NumElems >>= 1;
4487 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004488 return V;
4489}
Eric Christopherfd179292009-08-27 18:07:15 +00004490
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004491/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4492static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4493 EVT VT = V.getValueType();
4494 DebugLoc dl = V.getDebugLoc();
4495 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4496 && "Vector size not supported");
4497
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004498 if (VT.getSizeInBits() == 128) {
4499 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004500 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004501 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4502 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004503 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004504 // To use VPERMILPS to splat scalars, the second half of indicies must
4505 // refer to the higher part, which is a duplication of the lower one,
4506 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004507 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4508 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004509
4510 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4511 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4512 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004513 }
4514
4515 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4516}
4517
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004518/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004519static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4520 EVT SrcVT = SV->getValueType(0);
4521 SDValue V1 = SV->getOperand(0);
4522 DebugLoc dl = SV->getDebugLoc();
4523
4524 int EltNo = SV->getSplatIndex();
4525 int NumElems = SrcVT.getVectorNumElements();
4526 unsigned Size = SrcVT.getSizeInBits();
4527
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004528 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4529 "Unknown how to promote splat for type");
4530
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004531 // Extract the 128-bit part containing the splat element and update
4532 // the splat element index when it refers to the higher register.
4533 if (Size == 256) {
4534 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4535 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4536 if (Idx > 0)
4537 EltNo -= NumElems/2;
4538 }
4539
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004540 // All i16 and i8 vector types can't be used directly by a generic shuffle
4541 // instruction because the target has no such instruction. Generate shuffles
4542 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004543 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004544 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004545 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004546 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004547
4548 // Recreate the 256-bit vector and place the same 128-bit vector
4549 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004550 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004551 if (Size == 256) {
4552 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4553 DAG.getConstant(0, MVT::i32), DAG, dl);
4554 V1 = Insert128BitVector(InsV, V1,
4555 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4556 }
4557
4558 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004559}
4560
Evan Chengba05f722006-04-21 23:03:30 +00004561/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004562/// vector of zero or undef vector. This produces a shuffle where the low
4563/// element of V2 is swizzled into the zero/undef vector, landing at element
4564/// 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 +00004565static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004566 bool isZero, bool HasXMMInt,
4567 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004568 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004569 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004570 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004571 unsigned NumElems = VT.getVectorNumElements();
4572 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004573 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004574 // If this is the insertion idx, put the low elt of V2 here.
4575 MaskVec.push_back(i == Idx ? NumElems : i);
4576 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004577}
4578
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004579/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4580/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004581static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4582 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004583 if (Depth == 6)
4584 return SDValue(); // Limit search depth.
4585
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004586 SDValue V = SDValue(N, 0);
4587 EVT VT = V.getValueType();
4588 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004589
4590 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4591 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4592 Index = SV->getMaskElt(Index);
4593
4594 if (Index < 0)
4595 return DAG.getUNDEF(VT.getVectorElementType());
4596
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004597 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004598 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004599 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004600 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004601
4602 // Recurse into target specific vector shuffles to find scalars.
4603 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004604 int NumElems = VT.getVectorNumElements();
4605 SmallVector<unsigned, 16> ShuffleMask;
4606 SDValue ImmN;
4607
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004608 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004609 case X86ISD::SHUFPS:
4610 case X86ISD::SHUFPD:
4611 ImmN = N->getOperand(N->getNumOperands()-1);
4612 DecodeSHUFPSMask(NumElems,
4613 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4614 ShuffleMask);
4615 break;
4616 case X86ISD::PUNPCKHBW:
4617 case X86ISD::PUNPCKHWD:
4618 case X86ISD::PUNPCKHDQ:
4619 case X86ISD::PUNPCKHQDQ:
4620 DecodePUNPCKHMask(NumElems, ShuffleMask);
4621 break;
4622 case X86ISD::UNPCKHPS:
4623 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004624 case X86ISD::VUNPCKHPSY:
4625 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004626 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4627 break;
4628 case X86ISD::PUNPCKLBW:
4629 case X86ISD::PUNPCKLWD:
4630 case X86ISD::PUNPCKLDQ:
4631 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004632 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004633 break;
4634 case X86ISD::UNPCKLPS:
4635 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004636 case X86ISD::VUNPCKLPSY:
4637 case X86ISD::VUNPCKLPDY:
4638 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004639 break;
4640 case X86ISD::MOVHLPS:
4641 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4642 break;
4643 case X86ISD::MOVLHPS:
4644 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4645 break;
4646 case X86ISD::PSHUFD:
4647 ImmN = N->getOperand(N->getNumOperands()-1);
4648 DecodePSHUFMask(NumElems,
4649 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4650 ShuffleMask);
4651 break;
4652 case X86ISD::PSHUFHW:
4653 ImmN = N->getOperand(N->getNumOperands()-1);
4654 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4655 ShuffleMask);
4656 break;
4657 case X86ISD::PSHUFLW:
4658 ImmN = N->getOperand(N->getNumOperands()-1);
4659 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4660 ShuffleMask);
4661 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004662 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004663 case X86ISD::MOVSD: {
4664 // The index 0 always comes from the first element of the second source,
4665 // this is why MOVSS and MOVSD are used in the first place. The other
4666 // elements come from the other positions of the first source vector.
4667 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004668 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4669 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004670 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004671 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004672 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004673 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004674 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004675 break;
4676 case X86ISD::VPERMILPSY:
4677 ImmN = N->getOperand(N->getNumOperands()-1);
4678 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4679 ShuffleMask);
4680 break;
4681 case X86ISD::VPERMILPD:
4682 ImmN = N->getOperand(N->getNumOperands()-1);
4683 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4684 ShuffleMask);
4685 break;
4686 case X86ISD::VPERMILPDY:
4687 ImmN = N->getOperand(N->getNumOperands()-1);
4688 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4689 ShuffleMask);
4690 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004691 case X86ISD::VPERM2F128:
4692 ImmN = N->getOperand(N->getNumOperands()-1);
4693 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4694 ShuffleMask);
4695 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004696 case X86ISD::MOVDDUP:
4697 case X86ISD::MOVLHPD:
4698 case X86ISD::MOVLPD:
4699 case X86ISD::MOVLPS:
4700 case X86ISD::MOVSHDUP:
4701 case X86ISD::MOVSLDUP:
4702 case X86ISD::PALIGN:
4703 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004704 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004705 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004706 return SDValue();
4707 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004708
4709 Index = ShuffleMask[Index];
4710 if (Index < 0)
4711 return DAG.getUNDEF(VT.getVectorElementType());
4712
4713 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4714 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4715 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004716 }
4717
4718 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004719 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004720 V = V.getOperand(0);
4721 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004722 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004723
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004724 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004725 return SDValue();
4726 }
4727
4728 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4729 return (Index == 0) ? V.getOperand(0)
4730 : DAG.getUNDEF(VT.getVectorElementType());
4731
4732 if (V.getOpcode() == ISD::BUILD_VECTOR)
4733 return V.getOperand(Index);
4734
4735 return SDValue();
4736}
4737
4738/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4739/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004740/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004741static
4742unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4743 bool ZerosFromLeft, SelectionDAG &DAG) {
4744 int i = 0;
4745
4746 while (i < NumElems) {
4747 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004748 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004749 if (!(Elt.getNode() &&
4750 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4751 break;
4752 ++i;
4753 }
4754
4755 return i;
4756}
4757
4758/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4759/// MaskE correspond consecutively to elements from one of the vector operands,
4760/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4761static
4762bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4763 int OpIdx, int NumElems, unsigned &OpNum) {
4764 bool SeenV1 = false;
4765 bool SeenV2 = false;
4766
4767 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4768 int Idx = SVOp->getMaskElt(i);
4769 // Ignore undef indicies
4770 if (Idx < 0)
4771 continue;
4772
4773 if (Idx < NumElems)
4774 SeenV1 = true;
4775 else
4776 SeenV2 = true;
4777
4778 // Only accept consecutive elements from the same vector
4779 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4780 return false;
4781 }
4782
4783 OpNum = SeenV1 ? 0 : 1;
4784 return true;
4785}
4786
4787/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4788/// logical left shift of a vector.
4789static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4790 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4791 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4792 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4793 false /* check zeros from right */, DAG);
4794 unsigned OpSrc;
4795
4796 if (!NumZeros)
4797 return false;
4798
4799 // Considering the elements in the mask that are not consecutive zeros,
4800 // check if they consecutively come from only one of the source vectors.
4801 //
4802 // V1 = {X, A, B, C} 0
4803 // \ \ \ /
4804 // vector_shuffle V1, V2 <1, 2, 3, X>
4805 //
4806 if (!isShuffleMaskConsecutive(SVOp,
4807 0, // Mask Start Index
4808 NumElems-NumZeros-1, // Mask End Index
4809 NumZeros, // Where to start looking in the src vector
4810 NumElems, // Number of elements in vector
4811 OpSrc)) // Which source operand ?
4812 return false;
4813
4814 isLeft = false;
4815 ShAmt = NumZeros;
4816 ShVal = SVOp->getOperand(OpSrc);
4817 return true;
4818}
4819
4820/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4821/// logical left shift of a vector.
4822static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4823 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4824 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4825 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4826 true /* check zeros from left */, DAG);
4827 unsigned OpSrc;
4828
4829 if (!NumZeros)
4830 return false;
4831
4832 // Considering the elements in the mask that are not consecutive zeros,
4833 // check if they consecutively come from only one of the source vectors.
4834 //
4835 // 0 { A, B, X, X } = V2
4836 // / \ / /
4837 // vector_shuffle V1, V2 <X, X, 4, 5>
4838 //
4839 if (!isShuffleMaskConsecutive(SVOp,
4840 NumZeros, // Mask Start Index
4841 NumElems-1, // Mask End Index
4842 0, // Where to start looking in the src vector
4843 NumElems, // Number of elements in vector
4844 OpSrc)) // Which source operand ?
4845 return false;
4846
4847 isLeft = true;
4848 ShAmt = NumZeros;
4849 ShVal = SVOp->getOperand(OpSrc);
4850 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004851}
4852
4853/// isVectorShift - Returns true if the shuffle can be implemented as a
4854/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004855static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004856 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004857 // Although the logic below support any bitwidth size, there are no
4858 // shift instructions which handle more than 128-bit vectors.
4859 if (SVOp->getValueType(0).getSizeInBits() > 128)
4860 return false;
4861
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004862 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4863 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4864 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004865
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004866 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004867}
4868
Evan Chengc78d3b42006-04-24 18:01:45 +00004869/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4870///
Dan Gohman475871a2008-07-27 21:46:04 +00004871static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004872 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004873 SelectionDAG &DAG,
4874 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004875 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004876 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004877
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004878 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004879 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004880 bool First = true;
4881 for (unsigned i = 0; i < 16; ++i) {
4882 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4883 if (ThisIsNonZero && First) {
4884 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004885 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004886 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004887 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004888 First = false;
4889 }
4890
4891 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004892 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004893 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4894 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004895 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004896 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004897 }
4898 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004899 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4900 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4901 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004902 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004903 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004904 } else
4905 ThisElt = LastElt;
4906
Gabor Greifba36cb52008-08-28 21:40:38 +00004907 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004908 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004909 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004910 }
4911 }
4912
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004913 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004914}
4915
Bill Wendlinga348c562007-03-22 18:42:45 +00004916/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004917///
Dan Gohman475871a2008-07-27 21:46:04 +00004918static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004919 unsigned NumNonZero, unsigned NumZero,
4920 SelectionDAG &DAG,
4921 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004922 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004923 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004924
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004925 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004926 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004927 bool First = true;
4928 for (unsigned i = 0; i < 8; ++i) {
4929 bool isNonZero = (NonZeros & (1 << i)) != 0;
4930 if (isNonZero) {
4931 if (First) {
4932 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004933 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004934 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004935 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004936 First = false;
4937 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004938 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004939 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004940 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004941 }
4942 }
4943
4944 return V;
4945}
4946
Evan Chengf26ffe92008-05-29 08:22:04 +00004947/// getVShift - Return a vector logical shift node.
4948///
Owen Andersone50ed302009-08-10 22:56:29 +00004949static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004950 unsigned NumBits, SelectionDAG &DAG,
4951 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004952 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004953 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004954 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004955 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4956 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004957 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004958 DAG.getConstant(NumBits,
4959 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004960}
4961
Dan Gohman475871a2008-07-27 21:46:04 +00004962SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004963X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004964 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004965
Evan Chengc3630942009-12-09 21:00:30 +00004966 // Check if the scalar load can be widened into a vector load. And if
4967 // the address is "base + cst" see if the cst can be "absorbed" into
4968 // the shuffle mask.
4969 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4970 SDValue Ptr = LD->getBasePtr();
4971 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4972 return SDValue();
4973 EVT PVT = LD->getValueType(0);
4974 if (PVT != MVT::i32 && PVT != MVT::f32)
4975 return SDValue();
4976
4977 int FI = -1;
4978 int64_t Offset = 0;
4979 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4980 FI = FINode->getIndex();
4981 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004982 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004983 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4984 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4985 Offset = Ptr.getConstantOperandVal(1);
4986 Ptr = Ptr.getOperand(0);
4987 } else {
4988 return SDValue();
4989 }
4990
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004991 // FIXME: 256-bit vector instructions don't require a strict alignment,
4992 // improve this code to support it better.
4993 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004994 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004995 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004996 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004997 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004998 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004999 // Can't change the alignment. FIXME: It's possible to compute
5000 // the exact stack offset and reference FI + adjust offset instead.
5001 // If someone *really* cares about this. That's the way to implement it.
5002 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005003 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005004 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00005005 }
5006 }
5007
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005008 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00005009 // Ptr + (Offset & ~15).
5010 if (Offset < 0)
5011 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005012 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00005013 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005014 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00005015 if (StartOffset)
5016 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
5017 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
5018
5019 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005020 int NumElems = VT.getVectorNumElements();
5021
5022 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
5023 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5024 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00005025 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005026 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005027
5028 // Canonicalize it to a v4i32 or v8i32 shuffle.
5029 SmallVector<int, 8> Mask;
5030 for (int i = 0; i < NumElems; ++i)
5031 Mask.push_back(EltNo);
5032
5033 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
5034 return DAG.getNode(ISD::BITCAST, dl, NVT,
5035 DAG.getVectorShuffle(CanonVT, dl, V1,
5036 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00005037 }
5038
5039 return SDValue();
5040}
5041
Michael J. Spencerec38de22010-10-10 22:04:20 +00005042/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5043/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005044/// load which has the same value as a build_vector whose operands are 'elts'.
5045///
5046/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005047///
Nate Begeman1449f292010-03-24 22:19:06 +00005048/// FIXME: we'd also like to handle the case where the last elements are zero
5049/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5050/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005051static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005052 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005053 EVT EltVT = VT.getVectorElementType();
5054 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005055
Nate Begemanfdea31a2010-03-24 20:49:50 +00005056 LoadSDNode *LDBase = NULL;
5057 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005058
Nate Begeman1449f292010-03-24 22:19:06 +00005059 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005060 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005061 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005062 for (unsigned i = 0; i < NumElems; ++i) {
5063 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005064
Nate Begemanfdea31a2010-03-24 20:49:50 +00005065 if (!Elt.getNode() ||
5066 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5067 return SDValue();
5068 if (!LDBase) {
5069 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5070 return SDValue();
5071 LDBase = cast<LoadSDNode>(Elt.getNode());
5072 LastLoadedElt = i;
5073 continue;
5074 }
5075 if (Elt.getOpcode() == ISD::UNDEF)
5076 continue;
5077
5078 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5079 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5080 return SDValue();
5081 LastLoadedElt = i;
5082 }
Nate Begeman1449f292010-03-24 22:19:06 +00005083
5084 // If we have found an entire vector of loads and undefs, then return a large
5085 // load of the entire vector width starting at the base pointer. If we found
5086 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005087 if (LastLoadedElt == NumElems - 1) {
5088 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005089 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005090 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005091 LDBase->isVolatile(), LDBase->isNonTemporal(),
5092 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005093 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005094 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005095 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005096 LDBase->isInvariant(), LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005097 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5098 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005099 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5100 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005101 SDValue ResNode =
5102 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5103 LDBase->getPointerInfo(),
5104 LDBase->getAlignment(),
5105 false/*isVolatile*/, true/*ReadMem*/,
5106 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005107 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005108 }
5109 return SDValue();
5110}
5111
Evan Chengc3630942009-12-09 21:00:30 +00005112SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005113X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005114 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005115
David Greenef125a292011-02-08 19:04:41 +00005116 EVT VT = Op.getValueType();
5117 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005118 unsigned NumElems = Op.getNumOperands();
5119
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005120 // Vectors containing all zeros can be matched by pxor and xorps later
5121 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5122 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5123 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005124 if (Op.getValueType() == MVT::v4i32 ||
5125 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005126 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005127
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005128 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005129 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005130
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005131 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
5132 // vectors or broken into v4i32 operations on 256-bit vectors.
5133 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5134 if (Op.getValueType() == MVT::v4i32)
5135 return Op;
5136
5137 return getOnesVector(Op.getValueType(), DAG, dl);
5138 }
5139
Owen Andersone50ed302009-08-10 22:56:29 +00005140 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005141
Evan Cheng0db9fe62006-04-25 20:13:52 +00005142 unsigned NumZero = 0;
5143 unsigned NumNonZero = 0;
5144 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005145 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005146 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005147 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005148 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005149 if (Elt.getOpcode() == ISD::UNDEF)
5150 continue;
5151 Values.insert(Elt);
5152 if (Elt.getOpcode() != ISD::Constant &&
5153 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005154 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005155 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005156 NumZero++;
5157 else {
5158 NonZeros |= (1 << i);
5159 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005160 }
5161 }
5162
Chris Lattner97a2a562010-08-26 05:24:29 +00005163 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5164 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005165 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005166
Chris Lattner67f453a2008-03-09 05:42:06 +00005167 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005168 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005169 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005170 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005171
Chris Lattner62098042008-03-09 01:05:04 +00005172 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5173 // the value are obviously zero, truncate the value to i32 and do the
5174 // insertion that way. Only do this if the value is non-constant or if the
5175 // value is a constant being inserted into element 0. It is cheaper to do
5176 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005177 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005178 (!IsAllConstants || Idx == 0)) {
5179 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005180 // Handle SSE only.
5181 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5182 EVT VecVT = MVT::v4i32;
5183 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005184
Chris Lattner62098042008-03-09 01:05:04 +00005185 // Truncate the value (which may itself be a constant) to i32, and
5186 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005187 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005188 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005189 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005190 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005191
Chris Lattner62098042008-03-09 01:05:04 +00005192 // Now we have our 32-bit value zero extended in the low element of
5193 // a vector. If Idx != 0, swizzle it into place.
5194 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005195 SmallVector<int, 4> Mask;
5196 Mask.push_back(Idx);
5197 for (unsigned i = 1; i != VecElts; ++i)
5198 Mask.push_back(i);
5199 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005200 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005201 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005202 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005203 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005204 }
5205 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005206
Chris Lattner19f79692008-03-08 22:59:52 +00005207 // If we have a constant or non-constant insertion into the low element of
5208 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5209 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005210 // depending on what the source datatype is.
5211 if (Idx == 0) {
5212 if (NumZero == 0) {
5213 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005214 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5215 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005216 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5217 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005218 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005219 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005220 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5221 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005222 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5223 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005224 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5225 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005226 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005227 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005228 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005229 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005230
5231 // Is it a vector logical left shift?
5232 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005233 X86::isZeroNode(Op.getOperand(0)) &&
5234 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005235 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005236 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005237 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005238 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005239 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005240 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005241
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005242 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005243 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005244
Chris Lattner19f79692008-03-08 22:59:52 +00005245 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5246 // is a non-constant being inserted into an element other than the low one,
5247 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5248 // movd/movss) to move this into the low element, then shuffle it into
5249 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005250 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005251 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005252
Evan Cheng0db9fe62006-04-25 20:13:52 +00005253 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005254 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005255 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005256 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005257 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005258 MaskVec.push_back(i == Idx ? 0 : 1);
5259 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005260 }
5261 }
5262
Chris Lattner67f453a2008-03-09 05:42:06 +00005263 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005264 if (Values.size() == 1) {
5265 if (EVTBits == 32) {
5266 // Instead of a shuffle like this:
5267 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5268 // Check if it's possible to issue this instead.
5269 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5270 unsigned Idx = CountTrailingZeros_32(NonZeros);
5271 SDValue Item = Op.getOperand(Idx);
5272 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5273 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5274 }
Dan Gohman475871a2008-07-27 21:46:04 +00005275 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005276 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005277
Dan Gohmana3941172007-07-24 22:55:08 +00005278 // A vector full of immediates; various special cases are already
5279 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005280 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005281 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005282
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005283 // For AVX-length vectors, build the individual 128-bit pieces and use
5284 // shuffles to put them in place.
5285 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5286 SmallVector<SDValue, 32> V;
5287 for (unsigned i = 0; i < NumElems; ++i)
5288 V.push_back(Op.getOperand(i));
5289
5290 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5291
5292 // Build both the lower and upper subvector.
5293 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5294 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5295 NumElems/2);
5296
5297 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005298 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5299 DAG.getConstant(0, MVT::i32), DAG, dl);
5300 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005301 DAG, dl);
5302 }
5303
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005304 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005305 if (EVTBits == 64) {
5306 if (NumNonZero == 1) {
5307 // One half is zero or undef.
5308 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005309 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005310 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005311 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005312 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005313 }
Dan Gohman475871a2008-07-27 21:46:04 +00005314 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005315 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005316
5317 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005318 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005319 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005320 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005321 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005322 }
5323
Bill Wendling826f36f2007-03-28 00:57:11 +00005324 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005325 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005326 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005327 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005328 }
5329
5330 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005331 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005332 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005333 if (NumElems == 4 && NumZero > 0) {
5334 for (unsigned i = 0; i < 4; ++i) {
5335 bool isZero = !(NonZeros & (1 << i));
5336 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005337 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005338 else
Dale Johannesenace16102009-02-03 19:33:06 +00005339 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005340 }
5341
5342 for (unsigned i = 0; i < 2; ++i) {
5343 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5344 default: break;
5345 case 0:
5346 V[i] = V[i*2]; // Must be a zero vector.
5347 break;
5348 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005349 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005350 break;
5351 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005352 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005353 break;
5354 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005355 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005356 break;
5357 }
5358 }
5359
Nate Begeman9008ca62009-04-27 18:41:29 +00005360 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005361 bool Reverse = (NonZeros & 0x3) == 2;
5362 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005363 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005364 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5365 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005366 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5367 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005368 }
5369
Nate Begemanfdea31a2010-03-24 20:49:50 +00005370 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5371 // Check for a build vector of consecutive loads.
5372 for (unsigned i = 0; i < NumElems; ++i)
5373 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005374
Nate Begemanfdea31a2010-03-24 20:49:50 +00005375 // Check for elements which are consecutive loads.
5376 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5377 if (LD.getNode())
5378 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005379
5380 // For SSE 4.1, use insertps to put the high elements into the low element.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005381 if (getSubtarget()->hasSSE41() || getSubtarget()->hasAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005382 SDValue Result;
5383 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5384 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5385 else
5386 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005387
Chris Lattner24faf612010-08-28 17:59:08 +00005388 for (unsigned i = 1; i < NumElems; ++i) {
5389 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5390 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005391 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005392 }
5393 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005394 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005395
Chris Lattner6e80e442010-08-28 17:15:43 +00005396 // Otherwise, expand into a number of unpckl*, start by extending each of
5397 // our (non-undef) elements to the full vector width with the element in the
5398 // bottom slot of the vector (which generates no code for SSE).
5399 for (unsigned i = 0; i < NumElems; ++i) {
5400 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5401 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5402 else
5403 V[i] = DAG.getUNDEF(VT);
5404 }
5405
5406 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005407 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5408 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5409 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005410 unsigned EltStride = NumElems >> 1;
5411 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005412 for (unsigned i = 0; i < EltStride; ++i) {
5413 // If V[i+EltStride] is undef and this is the first round of mixing,
5414 // then it is safe to just drop this shuffle: V[i] is already in the
5415 // right place, the one element (since it's the first round) being
5416 // inserted as undef can be dropped. This isn't safe for successive
5417 // rounds because they will permute elements within both vectors.
5418 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5419 EltStride == NumElems/2)
5420 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005421
Chris Lattner6e80e442010-08-28 17:15:43 +00005422 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005423 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005424 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005425 }
5426 return V[0];
5427 }
Dan Gohman475871a2008-07-27 21:46:04 +00005428 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005429}
5430
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005431// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5432// them in a MMX register. This is better than doing a stack convert.
5433static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005434 DebugLoc dl = Op.getDebugLoc();
5435 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005436
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005437 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5438 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5439 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005440 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005441 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5442 InVec = Op.getOperand(1);
5443 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5444 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005445 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005446 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5447 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5448 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005449 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005450 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5451 Mask[0] = 0; Mask[1] = 2;
5452 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5453 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005454 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005455}
5456
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005457// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5458// to create 256-bit vectors from two other 128-bit ones.
5459static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5460 DebugLoc dl = Op.getDebugLoc();
5461 EVT ResVT = Op.getValueType();
5462
5463 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5464
5465 SDValue V1 = Op.getOperand(0);
5466 SDValue V2 = Op.getOperand(1);
5467 unsigned NumElems = ResVT.getVectorNumElements();
5468
5469 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5470 DAG.getConstant(0, MVT::i32), DAG, dl);
5471 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5472 DAG, dl);
5473}
5474
5475SDValue
5476X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005477 EVT ResVT = Op.getValueType();
5478
5479 assert(Op.getNumOperands() == 2);
5480 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5481 "Unsupported CONCAT_VECTORS for value type");
5482
5483 // We support concatenate two MMX registers and place them in a MMX register.
5484 // This is better than doing a stack convert.
5485 if (ResVT.is128BitVector())
5486 return LowerMMXCONCAT_VECTORS(Op, DAG);
5487
5488 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5489 // from two other 128-bit ones.
5490 return LowerAVXCONCAT_VECTORS(Op, DAG);
5491}
5492
Nate Begemanb9a47b82009-02-23 08:49:38 +00005493// v8i16 shuffles - Prefer shuffles in the following order:
5494// 1. [all] pshuflw, pshufhw, optional move
5495// 2. [ssse3] 1 x pshufb
5496// 3. [ssse3] 2 x pshufb + 1 x por
5497// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005498SDValue
5499X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5500 SelectionDAG &DAG) const {
5501 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005502 SDValue V1 = SVOp->getOperand(0);
5503 SDValue V2 = SVOp->getOperand(1);
5504 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005505 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005506
Nate Begemanb9a47b82009-02-23 08:49:38 +00005507 // Determine if more than 1 of the words in each of the low and high quadwords
5508 // of the result come from the same quadword of one of the two inputs. Undef
5509 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005510 unsigned LoQuad[] = { 0, 0, 0, 0 };
5511 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005512 BitVector InputQuads(4);
5513 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005514 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005515 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005516 MaskVals.push_back(EltIdx);
5517 if (EltIdx < 0) {
5518 ++Quad[0];
5519 ++Quad[1];
5520 ++Quad[2];
5521 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005522 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005523 }
5524 ++Quad[EltIdx / 4];
5525 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005526 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005527
Nate Begemanb9a47b82009-02-23 08:49:38 +00005528 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005529 unsigned MaxQuad = 1;
5530 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005531 if (LoQuad[i] > MaxQuad) {
5532 BestLoQuad = i;
5533 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005534 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005535 }
5536
Nate Begemanb9a47b82009-02-23 08:49:38 +00005537 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005538 MaxQuad = 1;
5539 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005540 if (HiQuad[i] > MaxQuad) {
5541 BestHiQuad = i;
5542 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005543 }
5544 }
5545
Nate Begemanb9a47b82009-02-23 08:49:38 +00005546 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005547 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005548 // single pshufb instruction is necessary. If There are more than 2 input
5549 // quads, disable the next transformation since it does not help SSSE3.
5550 bool V1Used = InputQuads[0] || InputQuads[1];
5551 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005552 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005553 if (InputQuads.count() == 2 && V1Used && V2Used) {
5554 BestLoQuad = InputQuads.find_first();
5555 BestHiQuad = InputQuads.find_next(BestLoQuad);
5556 }
5557 if (InputQuads.count() > 2) {
5558 BestLoQuad = -1;
5559 BestHiQuad = -1;
5560 }
5561 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005562
Nate Begemanb9a47b82009-02-23 08:49:38 +00005563 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5564 // the shuffle mask. If a quad is scored as -1, that means that it contains
5565 // words from all 4 input quadwords.
5566 SDValue NewV;
5567 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005568 SmallVector<int, 8> MaskV;
5569 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5570 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005571 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005572 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5573 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5574 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005575
Nate Begemanb9a47b82009-02-23 08:49:38 +00005576 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5577 // source words for the shuffle, to aid later transformations.
5578 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005579 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005580 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005581 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005582 if (idx != (int)i)
5583 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005584 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005585 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005586 AllWordsInNewV = false;
5587 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005588 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005589
Nate Begemanb9a47b82009-02-23 08:49:38 +00005590 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5591 if (AllWordsInNewV) {
5592 for (int i = 0; i != 8; ++i) {
5593 int idx = MaskVals[i];
5594 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005595 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005596 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005597 if ((idx != i) && idx < 4)
5598 pshufhw = false;
5599 if ((idx != i) && idx > 3)
5600 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005601 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005602 V1 = NewV;
5603 V2Used = false;
5604 BestLoQuad = 0;
5605 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005606 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005607
Nate Begemanb9a47b82009-02-23 08:49:38 +00005608 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5609 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005610 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005611 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5612 unsigned TargetMask = 0;
5613 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005614 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005615 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5616 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5617 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005618 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005619 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005620 }
Eric Christopherfd179292009-08-27 18:07:15 +00005621
Nate Begemanb9a47b82009-02-23 08:49:38 +00005622 // If we have SSSE3, and all words of the result are from 1 input vector,
5623 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5624 // is present, fall back to case 4.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005625 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005626 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005627
Nate Begemanb9a47b82009-02-23 08:49:38 +00005628 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005629 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005630 // mask, and elements that come from V1 in the V2 mask, so that the two
5631 // results can be OR'd together.
5632 bool TwoInputs = V1Used && V2Used;
5633 for (unsigned i = 0; i != 8; ++i) {
5634 int EltIdx = MaskVals[i] * 2;
5635 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005636 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5637 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005638 continue;
5639 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005640 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5641 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005642 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005643 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005644 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005645 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005646 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005647 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005648 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005649
Nate Begemanb9a47b82009-02-23 08:49:38 +00005650 // Calculate the shuffle mask for the second input, shuffle it, and
5651 // OR it with the first shuffled input.
5652 pshufbMask.clear();
5653 for (unsigned i = 0; i != 8; ++i) {
5654 int EltIdx = MaskVals[i] * 2;
5655 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005656 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5657 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005658 continue;
5659 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005660 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5661 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005662 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005663 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005664 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005665 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005666 MVT::v16i8, &pshufbMask[0], 16));
5667 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005668 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005669 }
5670
5671 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5672 // and update MaskVals with new element order.
5673 BitVector InOrder(8);
5674 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005675 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005676 for (int i = 0; i != 4; ++i) {
5677 int idx = MaskVals[i];
5678 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005679 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005680 InOrder.set(i);
5681 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005682 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005683 InOrder.set(i);
5684 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005685 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005686 }
5687 }
5688 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005689 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005690 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005691 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005692
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005693 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5694 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005695 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5696 NewV.getOperand(0),
5697 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5698 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005699 }
Eric Christopherfd179292009-08-27 18:07:15 +00005700
Nate Begemanb9a47b82009-02-23 08:49:38 +00005701 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5702 // and update MaskVals with the new element order.
5703 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005704 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005705 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005706 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005707 for (unsigned i = 4; i != 8; ++i) {
5708 int idx = MaskVals[i];
5709 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005710 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005711 InOrder.set(i);
5712 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005713 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005714 InOrder.set(i);
5715 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005716 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005717 }
5718 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005719 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005720 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005721
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005722 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5723 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005724 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5725 NewV.getOperand(0),
5726 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5727 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005728 }
Eric Christopherfd179292009-08-27 18:07:15 +00005729
Nate Begemanb9a47b82009-02-23 08:49:38 +00005730 // In case BestHi & BestLo were both -1, which means each quadword has a word
5731 // from each of the four input quadwords, calculate the InOrder bitvector now
5732 // before falling through to the insert/extract cleanup.
5733 if (BestLoQuad == -1 && BestHiQuad == -1) {
5734 NewV = V1;
5735 for (int i = 0; i != 8; ++i)
5736 if (MaskVals[i] < 0 || MaskVals[i] == i)
5737 InOrder.set(i);
5738 }
Eric Christopherfd179292009-08-27 18:07:15 +00005739
Nate Begemanb9a47b82009-02-23 08:49:38 +00005740 // The other elements are put in the right place using pextrw and pinsrw.
5741 for (unsigned i = 0; i != 8; ++i) {
5742 if (InOrder[i])
5743 continue;
5744 int EltIdx = MaskVals[i];
5745 if (EltIdx < 0)
5746 continue;
5747 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005748 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005749 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005750 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005751 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005752 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005753 DAG.getIntPtrConstant(i));
5754 }
5755 return NewV;
5756}
5757
5758// v16i8 shuffles - Prefer shuffles in the following order:
5759// 1. [ssse3] 1 x pshufb
5760// 2. [ssse3] 2 x pshufb + 1 x por
5761// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5762static
Nate Begeman9008ca62009-04-27 18:41:29 +00005763SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005764 SelectionDAG &DAG,
5765 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005766 SDValue V1 = SVOp->getOperand(0);
5767 SDValue V2 = SVOp->getOperand(1);
5768 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005769 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005770 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005771
Nate Begemanb9a47b82009-02-23 08:49:38 +00005772 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005773 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005774 // present, fall back to case 3.
5775 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5776 bool V1Only = true;
5777 bool V2Only = true;
5778 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005779 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005780 if (EltIdx < 0)
5781 continue;
5782 if (EltIdx < 16)
5783 V2Only = false;
5784 else
5785 V1Only = false;
5786 }
Eric Christopherfd179292009-08-27 18:07:15 +00005787
Nate Begemanb9a47b82009-02-23 08:49:38 +00005788 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005789 if (TLI.getSubtarget()->hasSSSE3() || TLI.getSubtarget()->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005790 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005791
Nate Begemanb9a47b82009-02-23 08:49:38 +00005792 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005793 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005794 //
5795 // Otherwise, we have elements from both input vectors, and must zero out
5796 // elements that come from V2 in the first mask, and V1 in the second mask
5797 // so that we can OR them together.
5798 bool TwoInputs = !(V1Only || V2Only);
5799 for (unsigned i = 0; i != 16; ++i) {
5800 int EltIdx = MaskVals[i];
5801 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005802 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005803 continue;
5804 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005805 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005806 }
5807 // If all the elements are from V2, assign it to V1 and return after
5808 // building the first pshufb.
5809 if (V2Only)
5810 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005811 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005812 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005813 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005814 if (!TwoInputs)
5815 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005816
Nate Begemanb9a47b82009-02-23 08:49:38 +00005817 // Calculate the shuffle mask for the second input, shuffle it, and
5818 // OR it with the first shuffled input.
5819 pshufbMask.clear();
5820 for (unsigned i = 0; i != 16; ++i) {
5821 int EltIdx = MaskVals[i];
5822 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005823 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005824 continue;
5825 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005826 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005827 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005828 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005829 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005830 MVT::v16i8, &pshufbMask[0], 16));
5831 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005832 }
Eric Christopherfd179292009-08-27 18:07:15 +00005833
Nate Begemanb9a47b82009-02-23 08:49:38 +00005834 // No SSSE3 - Calculate in place words and then fix all out of place words
5835 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5836 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005837 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5838 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005839 SDValue NewV = V2Only ? V2 : V1;
5840 for (int i = 0; i != 8; ++i) {
5841 int Elt0 = MaskVals[i*2];
5842 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005843
Nate Begemanb9a47b82009-02-23 08:49:38 +00005844 // This word of the result is all undef, skip it.
5845 if (Elt0 < 0 && Elt1 < 0)
5846 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005847
Nate Begemanb9a47b82009-02-23 08:49:38 +00005848 // This word of the result is already in the correct place, skip it.
5849 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5850 continue;
5851 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5852 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005853
Nate Begemanb9a47b82009-02-23 08:49:38 +00005854 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5855 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5856 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005857
5858 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5859 // using a single extract together, load it and store it.
5860 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005861 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005862 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005863 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005864 DAG.getIntPtrConstant(i));
5865 continue;
5866 }
5867
Nate Begemanb9a47b82009-02-23 08:49:38 +00005868 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005869 // source byte is not also odd, shift the extracted word left 8 bits
5870 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005871 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005872 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005873 DAG.getIntPtrConstant(Elt1 / 2));
5874 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005875 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005876 DAG.getConstant(8,
5877 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005878 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005879 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5880 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005881 }
5882 // If Elt0 is defined, extract it from the appropriate source. If the
5883 // source byte is not also even, shift the extracted word right 8 bits. If
5884 // Elt1 was also defined, OR the extracted values together before
5885 // inserting them in the result.
5886 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005887 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005888 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5889 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005890 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005891 DAG.getConstant(8,
5892 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005893 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005894 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5895 DAG.getConstant(0x00FF, MVT::i16));
5896 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005897 : InsElt0;
5898 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005899 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005900 DAG.getIntPtrConstant(i));
5901 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005902 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005903}
5904
Evan Cheng7a831ce2007-12-15 03:00:47 +00005905/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005906/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005907/// done when every pair / quad of shuffle mask elements point to elements in
5908/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005909/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005910static
Nate Begeman9008ca62009-04-27 18:41:29 +00005911SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005912 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005913 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005914 SDValue V1 = SVOp->getOperand(0);
5915 SDValue V2 = SVOp->getOperand(1);
5916 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005917 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005918 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005919 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005920 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005921 case MVT::v4f32: NewVT = MVT::v2f64; break;
5922 case MVT::v4i32: NewVT = MVT::v2i64; break;
5923 case MVT::v8i16: NewVT = MVT::v4i32; break;
5924 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005925 }
5926
Nate Begeman9008ca62009-04-27 18:41:29 +00005927 int Scale = NumElems / NewWidth;
5928 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005929 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005930 int StartIdx = -1;
5931 for (int j = 0; j < Scale; ++j) {
5932 int EltIdx = SVOp->getMaskElt(i+j);
5933 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005934 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005935 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005936 StartIdx = EltIdx - (EltIdx % Scale);
5937 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005938 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005939 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005940 if (StartIdx == -1)
5941 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005942 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005943 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005944 }
5945
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005946 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5947 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005948 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005949}
5950
Evan Chengd880b972008-05-09 21:53:03 +00005951/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005952///
Owen Andersone50ed302009-08-10 22:56:29 +00005953static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005954 SDValue SrcOp, SelectionDAG &DAG,
5955 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005956 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005957 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005958 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005959 LD = dyn_cast<LoadSDNode>(SrcOp);
5960 if (!LD) {
5961 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5962 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005963 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005964 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005965 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005966 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005967 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005968 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005969 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005970 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005971 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5972 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5973 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005974 SrcOp.getOperand(0)
5975 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005976 }
5977 }
5978 }
5979
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005980 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005981 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005982 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005983 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005984}
5985
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005986/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5987/// shuffle node referes to only one lane in the sources.
5988static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5989 EVT VT = SVOp->getValueType(0);
5990 int NumElems = VT.getVectorNumElements();
5991 int HalfSize = NumElems/2;
5992 SmallVector<int, 16> M;
5993 SVOp->getMask(M);
5994 bool MatchA = false, MatchB = false;
5995
5996 for (int l = 0; l < NumElems*2; l += HalfSize) {
5997 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5998 MatchA = true;
5999 break;
6000 }
6001 }
6002
6003 for (int l = 0; l < NumElems*2; l += HalfSize) {
6004 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
6005 MatchB = true;
6006 break;
6007 }
6008 }
6009
6010 return MatchA && MatchB;
6011}
6012
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006013/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6014/// which could not be matched by any known target speficic shuffle
6015static SDValue
6016LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006017 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
6018 // If each half of a vector shuffle node referes to only one lane in the
6019 // source vectors, extract each used 128-bit lane and shuffle them using
6020 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
6021 // the work to the legalizer.
6022 DebugLoc dl = SVOp->getDebugLoc();
6023 EVT VT = SVOp->getValueType(0);
6024 int NumElems = VT.getVectorNumElements();
6025 int HalfSize = NumElems/2;
6026
6027 // Extract the reference for each half
6028 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
6029 int FstVecOpNum = 0, SndVecOpNum = 0;
6030 for (int i = 0; i < HalfSize; ++i) {
6031 int Elt = SVOp->getMaskElt(i);
6032 if (SVOp->getMaskElt(i) < 0)
6033 continue;
6034 FstVecOpNum = Elt/NumElems;
6035 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6036 break;
6037 }
6038 for (int i = HalfSize; i < NumElems; ++i) {
6039 int Elt = SVOp->getMaskElt(i);
6040 if (SVOp->getMaskElt(i) < 0)
6041 continue;
6042 SndVecOpNum = Elt/NumElems;
6043 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6044 break;
6045 }
6046
6047 // Extract the subvectors
6048 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
6049 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
6050 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
6051 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
6052
6053 // Generate 128-bit shuffles
6054 SmallVector<int, 16> MaskV1, MaskV2;
6055 for (int i = 0; i < HalfSize; ++i) {
6056 int Elt = SVOp->getMaskElt(i);
6057 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6058 }
6059 for (int i = HalfSize; i < NumElems; ++i) {
6060 int Elt = SVOp->getMaskElt(i);
6061 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6062 }
6063
6064 EVT NVT = V1.getValueType();
6065 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
6066 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
6067
6068 // Concatenate the result back
6069 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
6070 DAG.getConstant(0, MVT::i32), DAG, dl);
6071 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
6072 DAG, dl);
6073 }
6074
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006075 return SDValue();
6076}
6077
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006078/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6079/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006080static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006081LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006082 SDValue V1 = SVOp->getOperand(0);
6083 SDValue V2 = SVOp->getOperand(1);
6084 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006085 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006086
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006087 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6088
Evan Chengace3c172008-07-22 21:13:36 +00006089 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006090 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006091 SmallVector<int, 8> Mask1(4U, -1);
6092 SmallVector<int, 8> PermMask;
6093 SVOp->getMask(PermMask);
6094
Evan Chengace3c172008-07-22 21:13:36 +00006095 unsigned NumHi = 0;
6096 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006097 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006098 int Idx = PermMask[i];
6099 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006100 Locs[i] = std::make_pair(-1, -1);
6101 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006102 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6103 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006104 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006105 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006106 NumLo++;
6107 } else {
6108 Locs[i] = std::make_pair(1, NumHi);
6109 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006110 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006111 NumHi++;
6112 }
6113 }
6114 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006115
Evan Chengace3c172008-07-22 21:13:36 +00006116 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006117 // If no more than two elements come from either vector. This can be
6118 // implemented with two shuffles. First shuffle gather the elements.
6119 // The second shuffle, which takes the first shuffle as both of its
6120 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006121 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006122
Nate Begeman9008ca62009-04-27 18:41:29 +00006123 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006124
Evan Chengace3c172008-07-22 21:13:36 +00006125 for (unsigned i = 0; i != 4; ++i) {
6126 if (Locs[i].first == -1)
6127 continue;
6128 else {
6129 unsigned Idx = (i < 2) ? 0 : 4;
6130 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006131 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006132 }
6133 }
6134
Nate Begeman9008ca62009-04-27 18:41:29 +00006135 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006136 } else if (NumLo == 3 || NumHi == 3) {
6137 // Otherwise, we must have three elements from one vector, call it X, and
6138 // one element from the other, call it Y. First, use a shufps to build an
6139 // intermediate vector with the one element from Y and the element from X
6140 // that will be in the same half in the final destination (the indexes don't
6141 // matter). Then, use a shufps to build the final vector, taking the half
6142 // containing the element from Y from the intermediate, and the other half
6143 // from X.
6144 if (NumHi == 3) {
6145 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006146 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006147 std::swap(V1, V2);
6148 }
6149
6150 // Find the element from V2.
6151 unsigned HiIndex;
6152 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006153 int Val = PermMask[HiIndex];
6154 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006155 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006156 if (Val >= 4)
6157 break;
6158 }
6159
Nate Begeman9008ca62009-04-27 18:41:29 +00006160 Mask1[0] = PermMask[HiIndex];
6161 Mask1[1] = -1;
6162 Mask1[2] = PermMask[HiIndex^1];
6163 Mask1[3] = -1;
6164 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006165
6166 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006167 Mask1[0] = PermMask[0];
6168 Mask1[1] = PermMask[1];
6169 Mask1[2] = HiIndex & 1 ? 6 : 4;
6170 Mask1[3] = HiIndex & 1 ? 4 : 6;
6171 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006172 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006173 Mask1[0] = HiIndex & 1 ? 2 : 0;
6174 Mask1[1] = HiIndex & 1 ? 0 : 2;
6175 Mask1[2] = PermMask[2];
6176 Mask1[3] = PermMask[3];
6177 if (Mask1[2] >= 0)
6178 Mask1[2] += 4;
6179 if (Mask1[3] >= 0)
6180 Mask1[3] += 4;
6181 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006182 }
Evan Chengace3c172008-07-22 21:13:36 +00006183 }
6184
6185 // Break it into (shuffle shuffle_hi, shuffle_lo).
6186 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006187 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006188 SmallVector<int,8> LoMask(4U, -1);
6189 SmallVector<int,8> HiMask(4U, -1);
6190
6191 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006192 unsigned MaskIdx = 0;
6193 unsigned LoIdx = 0;
6194 unsigned HiIdx = 2;
6195 for (unsigned i = 0; i != 4; ++i) {
6196 if (i == 2) {
6197 MaskPtr = &HiMask;
6198 MaskIdx = 1;
6199 LoIdx = 0;
6200 HiIdx = 2;
6201 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006202 int Idx = PermMask[i];
6203 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006204 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006205 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006206 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006207 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006208 LoIdx++;
6209 } else {
6210 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006211 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006212 HiIdx++;
6213 }
6214 }
6215
Nate Begeman9008ca62009-04-27 18:41:29 +00006216 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6217 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6218 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006219 for (unsigned i = 0; i != 4; ++i) {
6220 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006221 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006222 } else {
6223 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006224 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006225 }
6226 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006227 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006228}
6229
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006230static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006231 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006232 V = V.getOperand(0);
6233 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6234 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006235 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6236 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6237 // BUILD_VECTOR (load), undef
6238 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006239 if (MayFoldLoad(V))
6240 return true;
6241 return false;
6242}
6243
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006244// FIXME: the version above should always be used. Since there's
6245// a bug where several vector shuffles can't be folded because the
6246// DAG is not updated during lowering and a node claims to have two
6247// uses while it only has one, use this version, and let isel match
6248// another instruction if the load really happens to have more than
6249// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006250// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006251static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006252 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006253 V = V.getOperand(0);
6254 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6255 V = V.getOperand(0);
6256 if (ISD::isNormalLoad(V.getNode()))
6257 return true;
6258 return false;
6259}
6260
6261/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6262/// a vector extract, and if both can be later optimized into a single load.
6263/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6264/// here because otherwise a target specific shuffle node is going to be
6265/// emitted for this shuffle, and the optimization not done.
6266/// FIXME: This is probably not the best approach, but fix the problem
6267/// until the right path is decided.
6268static
6269bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6270 const TargetLowering &TLI) {
6271 EVT VT = V.getValueType();
6272 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6273
6274 // Be sure that the vector shuffle is present in a pattern like this:
6275 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6276 if (!V.hasOneUse())
6277 return false;
6278
6279 SDNode *N = *V.getNode()->use_begin();
6280 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6281 return false;
6282
6283 SDValue EltNo = N->getOperand(1);
6284 if (!isa<ConstantSDNode>(EltNo))
6285 return false;
6286
6287 // If the bit convert changed the number of elements, it is unsafe
6288 // to examine the mask.
6289 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006290 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006291 EVT SrcVT = V.getOperand(0).getValueType();
6292 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6293 return false;
6294 V = V.getOperand(0);
6295 HasShuffleIntoBitcast = true;
6296 }
6297
6298 // Select the input vector, guarding against out of range extract vector.
6299 unsigned NumElems = VT.getVectorNumElements();
6300 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6301 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6302 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6303
6304 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006305 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006306 V = V.getOperand(0);
6307
6308 if (ISD::isNormalLoad(V.getNode())) {
6309 // Is the original load suitable?
6310 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6311
6312 // FIXME: avoid the multi-use bug that is preventing lots of
6313 // of foldings to be detected, this is still wrong of course, but
6314 // give the temporary desired behavior, and if it happens that
6315 // the load has real more uses, during isel it will not fold, and
6316 // will generate poor code.
6317 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6318 return false;
6319
6320 if (!HasShuffleIntoBitcast)
6321 return true;
6322
6323 // If there's a bitcast before the shuffle, check if the load type and
6324 // alignment is valid.
6325 unsigned Align = LN0->getAlignment();
6326 unsigned NewAlign =
6327 TLI.getTargetData()->getABITypeAlignment(
6328 VT.getTypeForEVT(*DAG.getContext()));
6329
6330 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6331 return false;
6332 }
6333
6334 return true;
6335}
6336
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006337static
Evan Cheng835580f2010-10-07 20:50:20 +00006338SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6339 EVT VT = Op.getValueType();
6340
6341 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006342 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6343 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006344 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6345 V1, DAG));
6346}
6347
6348static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006349SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006350 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006351 SDValue V1 = Op.getOperand(0);
6352 SDValue V2 = Op.getOperand(1);
6353 EVT VT = Op.getValueType();
6354
6355 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6356
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006357 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006358 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6359
Evan Cheng0899f5c2011-08-31 02:05:24 +00006360 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6361 return DAG.getNode(ISD::BITCAST, dl, VT,
6362 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6363 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6364 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006365}
6366
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006367static
6368SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6369 SDValue V1 = Op.getOperand(0);
6370 SDValue V2 = Op.getOperand(1);
6371 EVT VT = Op.getValueType();
6372
6373 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6374 "unsupported shuffle type");
6375
6376 if (V2.getOpcode() == ISD::UNDEF)
6377 V2 = V1;
6378
6379 // v4i32 or v4f32
6380 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6381}
6382
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006383static inline unsigned getSHUFPOpcode(EVT VT) {
6384 switch(VT.getSimpleVT().SimpleTy) {
6385 case MVT::v8i32: // Use fp unit for int unpack.
6386 case MVT::v8f32:
6387 case MVT::v4i32: // Use fp unit for int unpack.
6388 case MVT::v4f32: return X86ISD::SHUFPS;
6389 case MVT::v4i64: // Use fp unit for int unpack.
6390 case MVT::v4f64:
6391 case MVT::v2i64: // Use fp unit for int unpack.
6392 case MVT::v2f64: return X86ISD::SHUFPD;
6393 default:
6394 llvm_unreachable("Unknown type for shufp*");
6395 }
6396 return 0;
6397}
6398
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006399static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006400SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006401 SDValue V1 = Op.getOperand(0);
6402 SDValue V2 = Op.getOperand(1);
6403 EVT VT = Op.getValueType();
6404 unsigned NumElems = VT.getVectorNumElements();
6405
6406 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6407 // operand of these instructions is only memory, so check if there's a
6408 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6409 // same masks.
6410 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006411
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006412 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006413 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006414 CanFoldLoad = true;
6415
6416 // When V1 is a load, it can be folded later into a store in isel, example:
6417 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6418 // turns into:
6419 // (MOVLPSmr addr:$src1, VR128:$src2)
6420 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006421 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006422 CanFoldLoad = true;
6423
Dan Gohman65fd6562011-11-03 21:49:52 +00006424 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006425 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006426 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006427 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6428
6429 if (NumElems == 4)
Dan Gohman65fd6562011-11-03 21:49:52 +00006430 // If we don't care about the second element, procede to use movss.
6431 if (SVOp->getMaskElt(1) != -1)
6432 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006433 }
6434
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006435 // movl and movlp will both match v2i64, but v2i64 is never matched by
6436 // movl earlier because we make it strict to avoid messing with the movlp load
6437 // folding logic (see the code above getMOVLP call). Match it here then,
6438 // this is horrible, but will stay like this until we move all shuffle
6439 // matching to x86 specific nodes. Note that for the 1st condition all
6440 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006441 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006442 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6443 // as to remove this logic from here, as much as possible
6444 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006445 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006446 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006447 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006448
6449 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6450
6451 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006452 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006453 X86::getShuffleSHUFImmediate(SVOp), DAG);
6454}
6455
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006456static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006457 switch(VT.getSimpleVT().SimpleTy) {
6458 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6459 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006460 case MVT::v4f32: return X86ISD::UNPCKLPS;
6461 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006462 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006463 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006464 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006465 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006466 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6467 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6468 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006469 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006470 }
6471 return 0;
6472}
6473
6474static inline unsigned getUNPCKHOpcode(EVT VT) {
6475 switch(VT.getSimpleVT().SimpleTy) {
6476 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6477 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6478 case MVT::v4f32: return X86ISD::UNPCKHPS;
6479 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006480 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006481 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006482 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006483 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006484 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6485 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6486 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006487 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006488 }
6489 return 0;
6490}
6491
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006492static inline unsigned getVPERMILOpcode(EVT VT) {
6493 switch(VT.getSimpleVT().SimpleTy) {
6494 case MVT::v4i32:
6495 case MVT::v4f32: return X86ISD::VPERMILPS;
6496 case MVT::v2i64:
6497 case MVT::v2f64: return X86ISD::VPERMILPD;
6498 case MVT::v8i32:
6499 case MVT::v8f32: return X86ISD::VPERMILPSY;
6500 case MVT::v4i64:
6501 case MVT::v4f64: return X86ISD::VPERMILPDY;
6502 default:
6503 llvm_unreachable("Unknown type for vpermil");
6504 }
6505 return 0;
6506}
6507
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006508/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6509/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6510/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6511static bool isVectorBroadcast(SDValue &Op) {
6512 EVT VT = Op.getValueType();
6513 bool Is256 = VT.getSizeInBits() == 256;
6514
6515 assert((VT.getSizeInBits() == 128 || Is256) &&
6516 "Unsupported type for vbroadcast node");
6517
6518 SDValue V = Op;
6519 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6520 V = V.getOperand(0);
6521
6522 if (Is256 && !(V.hasOneUse() &&
6523 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6524 V.getOperand(0).getOpcode() == ISD::UNDEF))
6525 return false;
6526
6527 if (Is256)
6528 V = V.getOperand(1);
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006529
6530 if (!V.hasOneUse())
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006531 return false;
6532
6533 // Check the source scalar_to_vector type. 256-bit broadcasts are
6534 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6535 // for 32-bit scalars.
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006536 if (V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6537 return false;
6538
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006539 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6540 if (ScalarSize != 32 && ScalarSize != 64)
6541 return false;
6542 if (!Is256 && ScalarSize == 64)
6543 return false;
6544
6545 V = V.getOperand(0);
6546 if (!MayFoldLoad(V))
6547 return false;
6548
6549 // Return the load node
6550 Op = V;
6551 return true;
6552}
6553
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006554static
6555SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006556 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006557 const X86Subtarget *Subtarget) {
6558 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6559 EVT VT = Op.getValueType();
6560 DebugLoc dl = Op.getDebugLoc();
6561 SDValue V1 = Op.getOperand(0);
6562 SDValue V2 = Op.getOperand(1);
6563
6564 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006565 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006566
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006567 // Handle splat operations
6568 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006569 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006570 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006571 // Special case, this is the only place now where it's allowed to return
6572 // a vector_shuffle operation without using a target specific node, because
6573 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6574 // this be moved to DAGCombine instead?
6575 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006576 return Op;
6577
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006578 // Use vbroadcast whenever the splat comes from a foldable load
6579 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6580 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6581
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006582 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006583 if ((Size == 128 && NumElem <= 4) ||
6584 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006585 return SDValue();
6586
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006587 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006588 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006589 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006590
6591 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6592 // do it!
6593 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6594 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6595 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006596 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006597 } else if ((VT == MVT::v4i32 ||
6598 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006599 // FIXME: Figure out a cleaner way to do this.
6600 // Try to make use of movq to zero out the top part.
6601 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6602 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6603 if (NewOp.getNode()) {
6604 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6605 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6606 DAG, Subtarget, dl);
6607 }
6608 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6609 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6610 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6611 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6612 DAG, Subtarget, dl);
6613 }
6614 }
6615 return SDValue();
6616}
6617
Dan Gohman475871a2008-07-27 21:46:04 +00006618SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006619X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006620 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006621 SDValue V1 = Op.getOperand(0);
6622 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006623 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006624 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006625 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006626 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006627 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6628 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006629 bool V1IsSplat = false;
6630 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006631 bool HasXMMInt = Subtarget->hasXMMInt();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006632 MachineFunction &MF = DAG.getMachineFunction();
6633 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006634
Dale Johannesen0488fb62010-09-30 23:57:10 +00006635 // Shuffle operations on MMX not supported.
6636 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006637 return Op;
6638
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006639 // Vector shuffle lowering takes 3 steps:
6640 //
6641 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6642 // narrowing and commutation of operands should be handled.
6643 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6644 // shuffle nodes.
6645 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6646 // so the shuffle can be broken into other shuffles and the legalizer can
6647 // try the lowering again.
6648 //
6649 // The general ideia is that no vector_shuffle operation should be left to
6650 // be matched during isel, all of them must be converted to a target specific
6651 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006652
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006653 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6654 // narrowing and commutation of operands should be handled. The actual code
6655 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006656 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006657 if (NewOp.getNode())
6658 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006659
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006660 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6661 // unpckh_undef). Only use pshufd if speed is more important than size.
6662 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006663 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006664 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006665 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006666
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006667 if (X86::isMOVDDUPMask(SVOp) &&
6668 (Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
6669 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006670 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006671
Dale Johannesen0488fb62010-09-30 23:57:10 +00006672 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006673 return getMOVHighToLow(Op, dl, DAG);
6674
6675 // Use to match splats
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006676 if (HasXMMInt && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006677 (VT == MVT::v2f64 || VT == MVT::v2i64))
6678 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6679
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006680 if (X86::isPSHUFDMask(SVOp)) {
6681 // The actual implementation will match the mask in the if above and then
6682 // during isel it can match several different instructions, not only pshufd
6683 // as its name says, sad but true, emulate the behavior for now...
6684 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6685 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6686
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006687 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6688
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006689 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006690 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6691
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006692 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6693 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006694 }
Eric Christopherfd179292009-08-27 18:07:15 +00006695
Evan Chengf26ffe92008-05-29 08:22:04 +00006696 // Check if this can be converted into a logical shift.
6697 bool isLeft = false;
6698 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006699 SDValue ShVal;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006700 bool isShift = getSubtarget()->hasXMMInt() &&
6701 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006702 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006703 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006704 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006705 EVT EltVT = VT.getVectorElementType();
6706 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006707 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006708 }
Eric Christopherfd179292009-08-27 18:07:15 +00006709
Nate Begeman9008ca62009-04-27 18:41:29 +00006710 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006711 if (V1IsUndef)
6712 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006713 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006714 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006715 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006716 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006717 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6718
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006719 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006720 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6721 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006722 }
Eric Christopherfd179292009-08-27 18:07:15 +00006723
Nate Begeman9008ca62009-04-27 18:41:29 +00006724 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006725 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006726 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006727
Dale Johannesen0488fb62010-09-30 23:57:10 +00006728 if (X86::isMOVHLPSMask(SVOp))
6729 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006730
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006731 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006732 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006733
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006734 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006735 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006736
Dale Johannesen0488fb62010-09-30 23:57:10 +00006737 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006738 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006739
Nate Begeman9008ca62009-04-27 18:41:29 +00006740 if (ShouldXformToMOVHLPS(SVOp) ||
6741 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6742 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006743
Evan Chengf26ffe92008-05-29 08:22:04 +00006744 if (isShift) {
6745 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006746 EVT EltVT = VT.getVectorElementType();
6747 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006748 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006749 }
Eric Christopherfd179292009-08-27 18:07:15 +00006750
Evan Cheng9eca5e82006-10-25 21:49:50 +00006751 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006752 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6753 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006754 V1IsSplat = isSplatVector(V1.getNode());
6755 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006756
Chris Lattner8a594482007-11-25 00:24:49 +00006757 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006758 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006759 Op = CommuteVectorShuffle(SVOp, DAG);
6760 SVOp = cast<ShuffleVectorSDNode>(Op);
6761 V1 = SVOp->getOperand(0);
6762 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006763 std::swap(V1IsSplat, V2IsSplat);
6764 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006765 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006766 }
6767
Nate Begeman9008ca62009-04-27 18:41:29 +00006768 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6769 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006770 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006771 return V1;
6772 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6773 // the instruction selector will not match, so get a canonical MOVL with
6774 // swapped operands to undo the commute.
6775 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006776 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006777
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006778 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006779 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006780
6781 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006782 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006783
Evan Cheng9bbbb982006-10-25 20:48:19 +00006784 if (V2IsSplat) {
6785 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006786 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006787 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006788 SDValue NewMask = NormalizeMask(SVOp, DAG);
6789 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6790 if (NSVOp != SVOp) {
6791 if (X86::isUNPCKLMask(NSVOp, true)) {
6792 return NewMask;
6793 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6794 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006795 }
6796 }
6797 }
6798
Evan Cheng9eca5e82006-10-25 21:49:50 +00006799 if (Commuted) {
6800 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006801 // FIXME: this seems wrong.
6802 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6803 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006804
6805 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006806 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006807
6808 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006809 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006810 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006811
Nate Begeman9008ca62009-04-27 18:41:29 +00006812 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006813 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006814 return CommuteVectorShuffle(SVOp, DAG);
6815
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006816 // The checks below are all present in isShuffleMaskLegal, but they are
6817 // inlined here right now to enable us to directly emit target specific
6818 // nodes, and remove one by one until they don't return Op anymore.
6819 SmallVector<int, 16> M;
6820 SVOp->getMask(M);
6821
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006822 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006823 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6824 X86::getShufflePALIGNRImmediate(SVOp),
6825 DAG);
6826
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006827 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6828 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006829 if (VT == MVT::v2f64)
6830 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006831 if (VT == MVT::v2i64)
6832 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6833 }
6834
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006835 if (isPSHUFHWMask(M, VT))
6836 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6837 X86::getShufflePSHUFHWImmediate(SVOp),
6838 DAG);
6839
6840 if (isPSHUFLWMask(M, VT))
6841 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6842 X86::getShufflePSHUFLWImmediate(SVOp),
6843 DAG);
6844
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006845 if (isSHUFPMask(M, VT))
6846 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6847 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006848
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006849 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006850 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006851 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006852 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006853
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006854 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006855 // Generate target specific nodes for 128 or 256-bit shuffles only
6856 // supported in the AVX instruction set.
6857 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006858
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006859 // Handle VMOVDDUPY permutations
6860 if (isMOVDDUPYMask(SVOp, Subtarget))
6861 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6862
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006863 // Handle VPERMILPS* permutations
6864 if (isVPERMILPSMask(M, VT, Subtarget))
6865 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6866 getShuffleVPERMILPSImmediate(SVOp), DAG);
6867
6868 // Handle VPERMILPD* permutations
6869 if (isVPERMILPDMask(M, VT, Subtarget))
6870 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6871 getShuffleVPERMILPDImmediate(SVOp), DAG);
6872
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006873 // Handle VPERM2F128 permutations
6874 if (isVPERM2F128Mask(M, VT, Subtarget))
6875 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6876 getShuffleVPERM2F128Immediate(SVOp), DAG);
6877
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006878 // Handle VSHUFPSY permutations
6879 if (isVSHUFPSYMask(M, VT, Subtarget))
6880 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6881 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6882
6883 // Handle VSHUFPDY permutations
6884 if (isVSHUFPDYMask(M, VT, Subtarget))
6885 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6886 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6887
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006888 //===--------------------------------------------------------------------===//
6889 // Since no target specific shuffle was selected for this generic one,
6890 // lower it into other known shuffles. FIXME: this isn't true yet, but
6891 // this is the plan.
6892 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006893
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006894 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6895 if (VT == MVT::v8i16) {
6896 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6897 if (NewOp.getNode())
6898 return NewOp;
6899 }
6900
6901 if (VT == MVT::v16i8) {
6902 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6903 if (NewOp.getNode())
6904 return NewOp;
6905 }
6906
6907 // Handle all 128-bit wide vectors with 4 elements, and match them with
6908 // several different shuffle types.
6909 if (NumElems == 4 && VT.getSizeInBits() == 128)
6910 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6911
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006912 // Handle general 256-bit shuffles
6913 if (VT.is256BitVector())
6914 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6915
Dan Gohman475871a2008-07-27 21:46:04 +00006916 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006917}
6918
Dan Gohman475871a2008-07-27 21:46:04 +00006919SDValue
6920X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006921 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006922 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006923 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006924
6925 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6926 return SDValue();
6927
Duncan Sands83ec4b62008-06-06 12:08:01 +00006928 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006929 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006930 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006931 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006932 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006933 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006934 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006935 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6936 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6937 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006938 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6939 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006940 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006941 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006942 Op.getOperand(0)),
6943 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006944 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006945 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006946 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006947 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006948 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006949 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006950 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6951 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006952 // result has a single use which is a store or a bitcast to i32. And in
6953 // the case of a store, it's not worth it if the index is a constant 0,
6954 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006955 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006956 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006957 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006958 if ((User->getOpcode() != ISD::STORE ||
6959 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6960 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006961 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006962 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006963 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006964 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006965 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006966 Op.getOperand(0)),
6967 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006968 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006969 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006970 // ExtractPS works with constant index.
6971 if (isa<ConstantSDNode>(Op.getOperand(1)))
6972 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006973 }
Dan Gohman475871a2008-07-27 21:46:04 +00006974 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006975}
6976
6977
Dan Gohman475871a2008-07-27 21:46:04 +00006978SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006979X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6980 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006981 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006982 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006983
David Greene74a579d2011-02-10 16:57:36 +00006984 SDValue Vec = Op.getOperand(0);
6985 EVT VecVT = Vec.getValueType();
6986
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006987 // If this is a 256-bit vector result, first extract the 128-bit vector and
6988 // then extract the element from the 128-bit vector.
6989 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006990 DebugLoc dl = Op.getNode()->getDebugLoc();
6991 unsigned NumElems = VecVT.getVectorNumElements();
6992 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006993 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6994
6995 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006996 bool Upper = IdxVal >= NumElems/2;
6997 Vec = Extract128BitVector(Vec,
6998 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006999
David Greene74a579d2011-02-10 16:57:36 +00007000 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007001 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00007002 }
7003
7004 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
7005
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007006 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007007 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007008 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007009 return Res;
7010 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007011
Owen Andersone50ed302009-08-10 22:56:29 +00007012 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007013 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007014 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007015 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007016 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007017 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007018 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007019 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7020 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007021 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007022 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007023 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007024 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007025 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007026 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007027 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007028 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007029 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007030 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007031 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007032 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007033 if (Idx == 0)
7034 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007035
Evan Cheng0db9fe62006-04-25 20:13:52 +00007036 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007037 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007038 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007039 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007040 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007041 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007042 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00007043 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007044 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7045 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7046 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007047 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007048 if (Idx == 0)
7049 return Op;
7050
7051 // UNPCKHPD the element to the lowest double word, then movsd.
7052 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7053 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007054 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007055 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007056 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007057 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007058 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007059 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007060 }
7061
Dan Gohman475871a2008-07-27 21:46:04 +00007062 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007063}
7064
Dan Gohman475871a2008-07-27 21:46:04 +00007065SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007066X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7067 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007068 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007069 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007070 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007071
Dan Gohman475871a2008-07-27 21:46:04 +00007072 SDValue N0 = Op.getOperand(0);
7073 SDValue N1 = Op.getOperand(1);
7074 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007075
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007076 if (VT.getSizeInBits() == 256)
7077 return SDValue();
7078
Dan Gohman8a55ce42009-09-23 21:02:20 +00007079 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007080 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007081 unsigned Opc;
7082 if (VT == MVT::v8i16)
7083 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007084 else if (VT == MVT::v16i8)
7085 Opc = X86ISD::PINSRB;
7086 else
7087 Opc = X86ISD::PINSRB;
7088
Nate Begeman14d12ca2008-02-11 04:19:36 +00007089 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7090 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007091 if (N1.getValueType() != MVT::i32)
7092 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7093 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007094 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007095 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007096 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007097 // Bits [7:6] of the constant are the source select. This will always be
7098 // zero here. The DAG Combiner may combine an extract_elt index into these
7099 // bits. For example (insert (extract, 3), 2) could be matched by putting
7100 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007101 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007102 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007103 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007104 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007105 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007106 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007107 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007108 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007109 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007110 // PINSR* works with constant index.
7111 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007112 }
Dan Gohman475871a2008-07-27 21:46:04 +00007113 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007114}
7115
Dan Gohman475871a2008-07-27 21:46:04 +00007116SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007117X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007118 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007119 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007120
David Greene6b381262011-02-09 15:32:06 +00007121 DebugLoc dl = Op.getDebugLoc();
7122 SDValue N0 = Op.getOperand(0);
7123 SDValue N1 = Op.getOperand(1);
7124 SDValue N2 = Op.getOperand(2);
7125
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007126 // If this is a 256-bit vector result, first extract the 128-bit vector,
7127 // insert the element into the extracted half and then place it back.
7128 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007129 if (!isa<ConstantSDNode>(N2))
7130 return SDValue();
7131
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007132 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007133 unsigned NumElems = VT.getVectorNumElements();
7134 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007135 bool Upper = IdxVal >= NumElems/2;
7136 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7137 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007138
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007139 // Insert the element into the desired half.
7140 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7141 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007142
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007143 // Insert the changed part back to the 256-bit vector
7144 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007145 }
7146
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007147 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007148 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7149
Dan Gohman8a55ce42009-09-23 21:02:20 +00007150 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007151 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007152
Dan Gohman8a55ce42009-09-23 21:02:20 +00007153 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007154 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7155 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007156 if (N1.getValueType() != MVT::i32)
7157 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7158 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007159 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007160 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007161 }
Dan Gohman475871a2008-07-27 21:46:04 +00007162 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007163}
7164
Dan Gohman475871a2008-07-27 21:46:04 +00007165SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007166X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007167 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007168 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007169 EVT OpVT = Op.getValueType();
7170
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007171 // If this is a 256-bit vector result, first insert into a 128-bit
7172 // vector and then insert into the 256-bit vector.
7173 if (OpVT.getSizeInBits() > 128) {
7174 // Insert into a 128-bit vector.
7175 EVT VT128 = EVT::getVectorVT(*Context,
7176 OpVT.getVectorElementType(),
7177 OpVT.getVectorNumElements() / 2);
7178
7179 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7180
7181 // Insert the 128-bit vector.
7182 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7183 DAG.getConstant(0, MVT::i32),
7184 DAG, dl);
7185 }
7186
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007187 if (Op.getValueType() == MVT::v1i64 &&
7188 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007189 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007190
Owen Anderson825b72b2009-08-11 20:47:22 +00007191 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007192 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7193 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007194 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007195 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007196}
7197
David Greene91585092011-01-26 15:38:49 +00007198// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7199// a simple subregister reference or explicit instructions to grab
7200// upper bits of a vector.
7201SDValue
7202X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7203 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007204 DebugLoc dl = Op.getNode()->getDebugLoc();
7205 SDValue Vec = Op.getNode()->getOperand(0);
7206 SDValue Idx = Op.getNode()->getOperand(1);
7207
7208 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7209 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7210 return Extract128BitVector(Vec, Idx, DAG, dl);
7211 }
David Greene91585092011-01-26 15:38:49 +00007212 }
7213 return SDValue();
7214}
7215
David Greenecfe33c42011-01-26 19:13:22 +00007216// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7217// simple superregister reference or explicit instructions to insert
7218// the upper bits of a vector.
7219SDValue
7220X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7221 if (Subtarget->hasAVX()) {
7222 DebugLoc dl = Op.getNode()->getDebugLoc();
7223 SDValue Vec = Op.getNode()->getOperand(0);
7224 SDValue SubVec = Op.getNode()->getOperand(1);
7225 SDValue Idx = Op.getNode()->getOperand(2);
7226
7227 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7228 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007229 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007230 }
7231 }
7232 return SDValue();
7233}
7234
Bill Wendling056292f2008-09-16 21:48:12 +00007235// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7236// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7237// one of the above mentioned nodes. It has to be wrapped because otherwise
7238// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7239// be used to form addressing mode. These wrapped nodes will be selected
7240// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007241SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007242X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007243 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007244
Chris Lattner41621a22009-06-26 19:22:52 +00007245 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7246 // global base reg.
7247 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007248 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007249 CodeModel::Model M = getTargetMachine().getCodeModel();
7250
Chris Lattner4f066492009-07-11 20:29:19 +00007251 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007252 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007253 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007254 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007255 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007256 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007257 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007258
Evan Cheng1606e8e2009-03-13 07:51:59 +00007259 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007260 CP->getAlignment(),
7261 CP->getOffset(), OpFlag);
7262 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007263 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007264 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007265 if (OpFlag) {
7266 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007267 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007268 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007269 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007270 }
7271
7272 return Result;
7273}
7274
Dan Gohmand858e902010-04-17 15:26:15 +00007275SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007276 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007277
Chris Lattner18c59872009-06-27 04:16:01 +00007278 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7279 // global base reg.
7280 unsigned char OpFlag = 0;
7281 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007282 CodeModel::Model M = getTargetMachine().getCodeModel();
7283
Chris Lattner4f066492009-07-11 20:29:19 +00007284 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007285 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007286 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007287 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007288 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007289 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007290 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007291
Chris Lattner18c59872009-06-27 04:16:01 +00007292 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7293 OpFlag);
7294 DebugLoc DL = JT->getDebugLoc();
7295 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007296
Chris Lattner18c59872009-06-27 04:16:01 +00007297 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007298 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007299 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7300 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007301 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007302 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007303
Chris Lattner18c59872009-06-27 04:16:01 +00007304 return Result;
7305}
7306
7307SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007308X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007309 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007310
Chris Lattner18c59872009-06-27 04:16:01 +00007311 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7312 // global base reg.
7313 unsigned char OpFlag = 0;
7314 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007315 CodeModel::Model M = getTargetMachine().getCodeModel();
7316
Chris Lattner4f066492009-07-11 20:29:19 +00007317 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007318 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7319 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7320 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007321 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007322 } else if (Subtarget->isPICStyleGOT()) {
7323 OpFlag = X86II::MO_GOT;
7324 } else if (Subtarget->isPICStyleStubPIC()) {
7325 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7326 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7327 OpFlag = X86II::MO_DARWIN_NONLAZY;
7328 }
Eric Christopherfd179292009-08-27 18:07:15 +00007329
Chris Lattner18c59872009-06-27 04:16:01 +00007330 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007331
Chris Lattner18c59872009-06-27 04:16:01 +00007332 DebugLoc DL = Op.getDebugLoc();
7333 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007334
7335
Chris Lattner18c59872009-06-27 04:16:01 +00007336 // With PIC, the address is actually $g + Offset.
7337 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007338 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007339 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7340 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007341 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007342 Result);
7343 }
Eric Christopherfd179292009-08-27 18:07:15 +00007344
Eli Friedman586272d2011-08-11 01:48:05 +00007345 // For symbols that require a load from a stub to get the address, emit the
7346 // load.
7347 if (isGlobalStubReference(OpFlag))
7348 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007349 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007350
Chris Lattner18c59872009-06-27 04:16:01 +00007351 return Result;
7352}
7353
Dan Gohman475871a2008-07-27 21:46:04 +00007354SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007355X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007356 // Create the TargetBlockAddressAddress node.
7357 unsigned char OpFlags =
7358 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007359 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007360 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007361 DebugLoc dl = Op.getDebugLoc();
7362 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7363 /*isTarget=*/true, OpFlags);
7364
Dan Gohmanf705adb2009-10-30 01:28:02 +00007365 if (Subtarget->isPICStyleRIPRel() &&
7366 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007367 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7368 else
7369 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007370
Dan Gohman29cbade2009-11-20 23:18:13 +00007371 // With PIC, the address is actually $g + Offset.
7372 if (isGlobalRelativeToPICBase(OpFlags)) {
7373 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7374 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7375 Result);
7376 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007377
7378 return Result;
7379}
7380
7381SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007382X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007383 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007384 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007385 // Create the TargetGlobalAddress node, folding in the constant
7386 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007387 unsigned char OpFlags =
7388 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007389 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007390 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007391 if (OpFlags == X86II::MO_NO_FLAG &&
7392 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007393 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007394 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007395 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007396 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007397 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007398 }
Eric Christopherfd179292009-08-27 18:07:15 +00007399
Chris Lattner4f066492009-07-11 20:29:19 +00007400 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007401 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007402 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7403 else
7404 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007405
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007406 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007407 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007408 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7409 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007410 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007411 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007412
Chris Lattner36c25012009-07-10 07:34:39 +00007413 // For globals that require a load from a stub to get the address, emit the
7414 // load.
7415 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007416 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007417 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007418
Dan Gohman6520e202008-10-18 02:06:02 +00007419 // If there was a non-zero offset that we didn't fold, create an explicit
7420 // addition for it.
7421 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007422 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007423 DAG.getConstant(Offset, getPointerTy()));
7424
Evan Cheng0db9fe62006-04-25 20:13:52 +00007425 return Result;
7426}
7427
Evan Chengda43bcf2008-09-24 00:05:32 +00007428SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007429X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007430 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007431 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007432 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007433}
7434
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007435static SDValue
7436GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007437 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007438 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007439 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007440 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007441 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007442 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007443 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007444 GA->getOffset(),
7445 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007446 if (InFlag) {
7447 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007448 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007449 } else {
7450 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007451 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007452 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007453
7454 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007455 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007456
Rafael Espindola15f1b662009-04-24 12:59:40 +00007457 SDValue Flag = Chain.getValue(1);
7458 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007459}
7460
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007461// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007462static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007463LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007464 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007465 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007466 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7467 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007468 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007469 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007470 InFlag = Chain.getValue(1);
7471
Chris Lattnerb903bed2009-06-26 21:20:29 +00007472 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007473}
7474
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007475// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007476static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007477LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007478 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007479 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7480 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007481}
7482
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007483// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7484// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007485static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007486 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007487 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007488 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007489
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007490 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7491 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7492 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007493
Michael J. Spencerec38de22010-10-10 22:04:20 +00007494 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007495 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007496 MachinePointerInfo(Ptr),
7497 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007498
Chris Lattnerb903bed2009-06-26 21:20:29 +00007499 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007500 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7501 // initialexec.
7502 unsigned WrapperKind = X86ISD::Wrapper;
7503 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007504 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007505 } else if (is64Bit) {
7506 assert(model == TLSModel::InitialExec);
7507 OperandFlags = X86II::MO_GOTTPOFF;
7508 WrapperKind = X86ISD::WrapperRIP;
7509 } else {
7510 assert(model == TLSModel::InitialExec);
7511 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007512 }
Eric Christopherfd179292009-08-27 18:07:15 +00007513
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007514 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7515 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007516 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007517 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007518 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007519 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007520
Rafael Espindola9a580232009-02-27 13:37:18 +00007521 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007522 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007523 MachinePointerInfo::getGOT(), false, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007524
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007525 // The address of the thread local variable is the add of the thread
7526 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007527 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007528}
7529
Dan Gohman475871a2008-07-27 21:46:04 +00007530SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007531X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007532
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007533 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007534 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007535
Eric Christopher30ef0e52010-06-03 04:07:48 +00007536 if (Subtarget->isTargetELF()) {
7537 // TODO: implement the "local dynamic" model
7538 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007539
Eric Christopher30ef0e52010-06-03 04:07:48 +00007540 // If GV is an alias then use the aliasee for determining
7541 // thread-localness.
7542 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7543 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007544
7545 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007546 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007547
Eric Christopher30ef0e52010-06-03 04:07:48 +00007548 switch (model) {
7549 case TLSModel::GeneralDynamic:
7550 case TLSModel::LocalDynamic: // not implemented
7551 if (Subtarget->is64Bit())
7552 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7553 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007554
Eric Christopher30ef0e52010-06-03 04:07:48 +00007555 case TLSModel::InitialExec:
7556 case TLSModel::LocalExec:
7557 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7558 Subtarget->is64Bit());
7559 }
7560 } else if (Subtarget->isTargetDarwin()) {
7561 // Darwin only has one model of TLS. Lower to that.
7562 unsigned char OpFlag = 0;
7563 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7564 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007565
Eric Christopher30ef0e52010-06-03 04:07:48 +00007566 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7567 // global base reg.
7568 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7569 !Subtarget->is64Bit();
7570 if (PIC32)
7571 OpFlag = X86II::MO_TLVP_PIC_BASE;
7572 else
7573 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007574 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007575 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007576 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007577 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007578 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007579
Eric Christopher30ef0e52010-06-03 04:07:48 +00007580 // With PIC32, the address is actually $g + Offset.
7581 if (PIC32)
7582 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7583 DAG.getNode(X86ISD::GlobalBaseReg,
7584 DebugLoc(), getPointerTy()),
7585 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007586
Eric Christopher30ef0e52010-06-03 04:07:48 +00007587 // Lowering the machine isd will make sure everything is in the right
7588 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007589 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007590 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007591 SDValue Args[] = { Chain, Offset };
7592 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007593
Eric Christopher30ef0e52010-06-03 04:07:48 +00007594 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7595 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7596 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007597
Eric Christopher30ef0e52010-06-03 04:07:48 +00007598 // And our return value (tls address) is in the standard call return value
7599 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007600 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007601 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7602 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007603 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007604
Eric Christopher30ef0e52010-06-03 04:07:48 +00007605 assert(false &&
7606 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007607
Torok Edwinc23197a2009-07-14 16:55:14 +00007608 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007609 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007610}
7611
Evan Cheng0db9fe62006-04-25 20:13:52 +00007612
Nadav Rotem43012222011-05-11 08:12:09 +00007613/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007614/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007615SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007616 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007617 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007618 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007619 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007620 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007621 SDValue ShOpLo = Op.getOperand(0);
7622 SDValue ShOpHi = Op.getOperand(1);
7623 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007624 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007625 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007626 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007627
Dan Gohman475871a2008-07-27 21:46:04 +00007628 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007629 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007630 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7631 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007632 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007633 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7634 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007635 }
Evan Chenge3413162006-01-09 18:33:28 +00007636
Owen Anderson825b72b2009-08-11 20:47:22 +00007637 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7638 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007639 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007640 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007641
Dan Gohman475871a2008-07-27 21:46:04 +00007642 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007643 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007644 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7645 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007646
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007647 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007648 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7649 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007650 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007651 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7652 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007653 }
7654
Dan Gohman475871a2008-07-27 21:46:04 +00007655 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007656 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007657}
Evan Chenga3195e82006-01-12 22:54:21 +00007658
Dan Gohmand858e902010-04-17 15:26:15 +00007659SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7660 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007661 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007662
Dale Johannesen0488fb62010-09-30 23:57:10 +00007663 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007664 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007665
Owen Anderson825b72b2009-08-11 20:47:22 +00007666 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007667 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007668
Eli Friedman36df4992009-05-27 00:47:34 +00007669 // These are really Legal; return the operand so the caller accepts it as
7670 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007671 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007672 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007673 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007674 Subtarget->is64Bit()) {
7675 return Op;
7676 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007677
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007678 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007679 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007680 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007681 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007682 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007683 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007684 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007685 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007686 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007687 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7688}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007689
Owen Andersone50ed302009-08-10 22:56:29 +00007690SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007691 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007692 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007693 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007694 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007695 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007696 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007697 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007698 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007699 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007700 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007701
Chris Lattner492a43e2010-09-22 01:28:21 +00007702 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007703
Stuart Hastings84be9582011-06-02 15:57:11 +00007704 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7705 MachineMemOperand *MMO;
7706 if (FI) {
7707 int SSFI = FI->getIndex();
7708 MMO =
7709 DAG.getMachineFunction()
7710 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7711 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7712 } else {
7713 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7714 StackSlot = StackSlot.getOperand(1);
7715 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007716 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007717 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7718 X86ISD::FILD, DL,
7719 Tys, Ops, array_lengthof(Ops),
7720 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007721
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007722 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007723 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007724 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007725
7726 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7727 // shouldn't be necessary except that RFP cannot be live across
7728 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007729 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007730 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7731 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007732 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007733 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007734 SDValue Ops[] = {
7735 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7736 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007737 MachineMemOperand *MMO =
7738 DAG.getMachineFunction()
7739 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007740 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007741
Chris Lattner492a43e2010-09-22 01:28:21 +00007742 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7743 Ops, array_lengthof(Ops),
7744 Op.getValueType(), MMO);
7745 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007746 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007747 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007748 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007749
Evan Cheng0db9fe62006-04-25 20:13:52 +00007750 return Result;
7751}
7752
Bill Wendling8b8a6362009-01-17 03:56:04 +00007753// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007754SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7755 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007756 // This algorithm is not obvious. Here it is in C code, more or less:
7757 /*
7758 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7759 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7760 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007761
Bill Wendling8b8a6362009-01-17 03:56:04 +00007762 // Copy ints to xmm registers.
7763 __m128i xh = _mm_cvtsi32_si128( hi );
7764 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007765
Bill Wendling8b8a6362009-01-17 03:56:04 +00007766 // Combine into low half of a single xmm register.
7767 __m128i x = _mm_unpacklo_epi32( xh, xl );
7768 __m128d d;
7769 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007770
Bill Wendling8b8a6362009-01-17 03:56:04 +00007771 // Merge in appropriate exponents to give the integer bits the right
7772 // magnitude.
7773 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007774
Bill Wendling8b8a6362009-01-17 03:56:04 +00007775 // Subtract away the biases to deal with the IEEE-754 double precision
7776 // implicit 1.
7777 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007778
Bill Wendling8b8a6362009-01-17 03:56:04 +00007779 // All conversions up to here are exact. The correctly rounded result is
7780 // calculated using the current rounding mode using the following
7781 // horizontal add.
7782 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7783 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7784 // store doesn't really need to be here (except
7785 // maybe to zero the other double)
7786 return sd;
7787 }
7788 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007789
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007790 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007791 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007792
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007793 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007794 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007795 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7796 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7797 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7798 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007799 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007800 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007801
Bill Wendling8b8a6362009-01-17 03:56:04 +00007802 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007803 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007804 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007805 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007806 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007807 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007808 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007809
Owen Anderson825b72b2009-08-11 20:47:22 +00007810 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7811 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007812 Op.getOperand(0),
7813 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007814 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7815 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007816 Op.getOperand(0),
7817 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007818 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7819 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007820 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007821 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007822 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007823 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007824 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007825 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007826 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007827 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007828
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007829 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007830 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007831 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7832 DAG.getUNDEF(MVT::v2f64), ShufMask);
7833 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7834 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007835 DAG.getIntPtrConstant(0));
7836}
7837
Bill Wendling8b8a6362009-01-17 03:56:04 +00007838// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007839SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7840 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007841 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007842 // FP constant to bias correct the final result.
7843 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007844 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007845
7846 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007847 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007848 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007849
Eli Friedmanf3704762011-08-29 21:15:46 +00007850 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007851 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7852 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007853
Owen Anderson825b72b2009-08-11 20:47:22 +00007854 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007855 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007856 DAG.getIntPtrConstant(0));
7857
7858 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007859 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007860 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007861 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007862 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007863 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007864 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007865 MVT::v2f64, Bias)));
7866 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007867 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007868 DAG.getIntPtrConstant(0));
7869
7870 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007871 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007872
7873 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007874 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007875
Owen Anderson825b72b2009-08-11 20:47:22 +00007876 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007877 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007878 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007879 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007880 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007881 }
7882
7883 // Handle final rounding.
7884 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007885}
7886
Dan Gohmand858e902010-04-17 15:26:15 +00007887SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7888 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007889 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007890 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007891
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007892 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007893 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7894 // the optimization here.
7895 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007896 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007897
Owen Andersone50ed302009-08-10 22:56:29 +00007898 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007899 EVT DstVT = Op.getValueType();
7900 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007901 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007902 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007903 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007904
7905 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007906 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007907 if (SrcVT == MVT::i32) {
7908 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7909 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7910 getPointerTy(), StackSlot, WordOff);
7911 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007912 StackSlot, MachinePointerInfo(),
7913 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007914 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007915 OffsetSlot, MachinePointerInfo(),
7916 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007917 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7918 return Fild;
7919 }
7920
7921 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7922 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007923 StackSlot, MachinePointerInfo(),
7924 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007925 // For i64 source, we need to add the appropriate power of 2 if the input
7926 // was negative. This is the same as the optimization in
7927 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7928 // we must be careful to do the computation in x87 extended precision, not
7929 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007930 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7931 MachineMemOperand *MMO =
7932 DAG.getMachineFunction()
7933 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7934 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007935
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007936 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7937 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007938 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7939 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007940
7941 APInt FF(32, 0x5F800000ULL);
7942
7943 // Check whether the sign bit is set.
7944 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7945 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7946 ISD::SETLT);
7947
7948 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7949 SDValue FudgePtr = DAG.getConstantPool(
7950 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7951 getPointerTy());
7952
7953 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7954 SDValue Zero = DAG.getIntPtrConstant(0);
7955 SDValue Four = DAG.getIntPtrConstant(4);
7956 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7957 Zero, Four);
7958 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7959
7960 // Load the value out, extending it from f32 to f80.
7961 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007962 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007963 FudgePtr, MachinePointerInfo::getConstantPool(),
7964 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007965 // Extend everything to 80 bits to force it to be done on x87.
7966 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7967 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007968}
7969
Dan Gohman475871a2008-07-27 21:46:04 +00007970std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007971FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007972 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007973
Owen Andersone50ed302009-08-10 22:56:29 +00007974 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007975
7976 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007977 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7978 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007979 }
7980
Owen Anderson825b72b2009-08-11 20:47:22 +00007981 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7982 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007983 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007984
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007985 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007986 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007987 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007988 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007989 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007990 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007991 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007992 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007993
Evan Cheng87c89352007-10-15 20:11:21 +00007994 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7995 // stack slot.
7996 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007997 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007998 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007999 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008000
Michael J. Spencerec38de22010-10-10 22:04:20 +00008001
8002
Evan Cheng0db9fe62006-04-25 20:13:52 +00008003 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00008004 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008005 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008006 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8007 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8008 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00008009 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008010
Dan Gohman475871a2008-07-27 21:46:04 +00008011 SDValue Chain = DAG.getEntryNode();
8012 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008013 EVT TheVT = Op.getOperand(0).getValueType();
8014 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008015 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008016 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008017 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008018 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008019 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008020 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008021 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008022 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008023
Chris Lattner492a43e2010-09-22 01:28:21 +00008024 MachineMemOperand *MMO =
8025 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8026 MachineMemOperand::MOLoad, MemSize, MemSize);
8027 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8028 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008029 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008030 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008031 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8032 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008033
Chris Lattner07290932010-09-22 01:05:16 +00008034 MachineMemOperand *MMO =
8035 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8036 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008037
Evan Cheng0db9fe62006-04-25 20:13:52 +00008038 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00008039 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00008040 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8041 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00008042
Chris Lattner27a6c732007-11-24 07:07:01 +00008043 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008044}
8045
Dan Gohmand858e902010-04-17 15:26:15 +00008046SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8047 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008048 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008049 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008050
Eli Friedman948e95a2009-05-23 09:59:16 +00008051 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00008052 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008053 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8054 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008055
Chris Lattner27a6c732007-11-24 07:07:01 +00008056 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008057 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008058 FIST, StackSlot, MachinePointerInfo(),
8059 false, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00008060}
8061
Dan Gohmand858e902010-04-17 15:26:15 +00008062SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8063 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00008064 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
8065 SDValue FIST = Vals.first, StackSlot = Vals.second;
8066 assert(FIST.getNode() && "Unexpected failure");
8067
8068 // Load the result.
8069 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008070 FIST, StackSlot, MachinePointerInfo(),
8071 false, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00008072}
8073
Dan Gohmand858e902010-04-17 15:26:15 +00008074SDValue X86TargetLowering::LowerFABS(SDValue Op,
8075 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008076 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008077 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008078 EVT VT = Op.getValueType();
8079 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008080 if (VT.isVector())
8081 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008082 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008083 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008084 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00008085 CV.push_back(C);
8086 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008087 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008088 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00008089 CV.push_back(C);
8090 CV.push_back(C);
8091 CV.push_back(C);
8092 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008093 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008094 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008095 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008096 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008097 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008098 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008099 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008100}
8101
Dan Gohmand858e902010-04-17 15:26:15 +00008102SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008103 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008104 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008105 EVT VT = Op.getValueType();
8106 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008107 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008108 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008109 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008110 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008111 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008112 CV.push_back(C);
8113 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008114 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008115 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008116 CV.push_back(C);
8117 CV.push_back(C);
8118 CV.push_back(C);
8119 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008120 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008121 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008122 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008123 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008124 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008125 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008126 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008127 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008128 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008129 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008130 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008131 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008132 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008133 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008134 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008135}
8136
Dan Gohmand858e902010-04-17 15:26:15 +00008137SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008138 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008139 SDValue Op0 = Op.getOperand(0);
8140 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008141 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008142 EVT VT = Op.getValueType();
8143 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008144
8145 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008146 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008147 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008148 SrcVT = VT;
8149 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008150 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008151 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008152 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008153 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008154 }
8155
8156 // At this point the operands and the result should have the same
8157 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008158
Evan Cheng68c47cb2007-01-05 07:55:56 +00008159 // First get the sign bit of second operand.
8160 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008161 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008162 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8163 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008164 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008165 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8166 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8167 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8168 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008169 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008170 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008171 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008172 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008173 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008174 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008175 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008176
8177 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008178 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008179 // Op0 is MVT::f32, Op1 is MVT::f64.
8180 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8181 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8182 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008183 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008184 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008185 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008186 }
8187
Evan Cheng73d6cf12007-01-05 21:37:56 +00008188 // Clear first operand sign bit.
8189 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008190 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008191 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8192 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008193 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008194 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8195 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8196 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8197 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008198 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008199 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008200 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008201 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008202 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008203 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008204 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008205
8206 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008207 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008208}
8209
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008210SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8211 SDValue N0 = Op.getOperand(0);
8212 DebugLoc dl = Op.getDebugLoc();
8213 EVT VT = Op.getValueType();
8214
8215 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8216 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8217 DAG.getConstant(1, VT));
8218 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8219}
8220
Dan Gohman076aee32009-03-04 19:44:21 +00008221/// Emit nodes that will be selected as "test Op0,Op0", or something
8222/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008223SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008224 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008225 DebugLoc dl = Op.getDebugLoc();
8226
Dan Gohman31125812009-03-07 01:58:32 +00008227 // CF and OF aren't always set the way we want. Determine which
8228 // of these we need.
8229 bool NeedCF = false;
8230 bool NeedOF = false;
8231 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008232 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008233 case X86::COND_A: case X86::COND_AE:
8234 case X86::COND_B: case X86::COND_BE:
8235 NeedCF = true;
8236 break;
8237 case X86::COND_G: case X86::COND_GE:
8238 case X86::COND_L: case X86::COND_LE:
8239 case X86::COND_O: case X86::COND_NO:
8240 NeedOF = true;
8241 break;
Dan Gohman31125812009-03-07 01:58:32 +00008242 }
8243
Dan Gohman076aee32009-03-04 19:44:21 +00008244 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008245 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8246 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008247 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8248 // Emit a CMP with 0, which is the TEST pattern.
8249 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8250 DAG.getConstant(0, Op.getValueType()));
8251
8252 unsigned Opcode = 0;
8253 unsigned NumOperands = 0;
8254 switch (Op.getNode()->getOpcode()) {
8255 case ISD::ADD:
8256 // Due to an isel shortcoming, be conservative if this add is likely to be
8257 // selected as part of a load-modify-store instruction. When the root node
8258 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8259 // uses of other nodes in the match, such as the ADD in this case. This
8260 // leads to the ADD being left around and reselected, with the result being
8261 // two adds in the output. Alas, even if none our users are stores, that
8262 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8263 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8264 // climbing the DAG back to the root, and it doesn't seem to be worth the
8265 // effort.
8266 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00008267 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008268 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8269 goto default_case;
8270
8271 if (ConstantSDNode *C =
8272 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8273 // An add of one will be selected as an INC.
8274 if (C->getAPIntValue() == 1) {
8275 Opcode = X86ISD::INC;
8276 NumOperands = 1;
8277 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008278 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008279
8280 // An add of negative one (subtract of one) will be selected as a DEC.
8281 if (C->getAPIntValue().isAllOnesValue()) {
8282 Opcode = X86ISD::DEC;
8283 NumOperands = 1;
8284 break;
8285 }
Dan Gohman076aee32009-03-04 19:44:21 +00008286 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008287
8288 // Otherwise use a regular EFLAGS-setting add.
8289 Opcode = X86ISD::ADD;
8290 NumOperands = 2;
8291 break;
8292 case ISD::AND: {
8293 // If the primary and result isn't used, don't bother using X86ISD::AND,
8294 // because a TEST instruction will be better.
8295 bool NonFlagUse = false;
8296 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8297 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8298 SDNode *User = *UI;
8299 unsigned UOpNo = UI.getOperandNo();
8300 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8301 // Look pass truncate.
8302 UOpNo = User->use_begin().getOperandNo();
8303 User = *User->use_begin();
8304 }
8305
8306 if (User->getOpcode() != ISD::BRCOND &&
8307 User->getOpcode() != ISD::SETCC &&
8308 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8309 NonFlagUse = true;
8310 break;
8311 }
Dan Gohman076aee32009-03-04 19:44:21 +00008312 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008313
8314 if (!NonFlagUse)
8315 break;
8316 }
8317 // FALL THROUGH
8318 case ISD::SUB:
8319 case ISD::OR:
8320 case ISD::XOR:
8321 // Due to the ISEL shortcoming noted above, be conservative if this op is
8322 // likely to be selected as part of a load-modify-store instruction.
8323 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8324 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8325 if (UI->getOpcode() == ISD::STORE)
8326 goto default_case;
8327
8328 // Otherwise use a regular EFLAGS-setting instruction.
8329 switch (Op.getNode()->getOpcode()) {
8330 default: llvm_unreachable("unexpected operator!");
8331 case ISD::SUB: Opcode = X86ISD::SUB; break;
8332 case ISD::OR: Opcode = X86ISD::OR; break;
8333 case ISD::XOR: Opcode = X86ISD::XOR; break;
8334 case ISD::AND: Opcode = X86ISD::AND; break;
8335 }
8336
8337 NumOperands = 2;
8338 break;
8339 case X86ISD::ADD:
8340 case X86ISD::SUB:
8341 case X86ISD::INC:
8342 case X86ISD::DEC:
8343 case X86ISD::OR:
8344 case X86ISD::XOR:
8345 case X86ISD::AND:
8346 return SDValue(Op.getNode(), 1);
8347 default:
8348 default_case:
8349 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008350 }
8351
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008352 if (Opcode == 0)
8353 // Emit a CMP with 0, which is the TEST pattern.
8354 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8355 DAG.getConstant(0, Op.getValueType()));
8356
8357 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8358 SmallVector<SDValue, 4> Ops;
8359 for (unsigned i = 0; i != NumOperands; ++i)
8360 Ops.push_back(Op.getOperand(i));
8361
8362 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8363 DAG.ReplaceAllUsesWith(Op, New);
8364 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008365}
8366
8367/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8368/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008369SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008370 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008371 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8372 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008373 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008374
8375 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008376 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008377}
8378
Evan Chengd40d03e2010-01-06 19:38:29 +00008379/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8380/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008381SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8382 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008383 SDValue Op0 = And.getOperand(0);
8384 SDValue Op1 = And.getOperand(1);
8385 if (Op0.getOpcode() == ISD::TRUNCATE)
8386 Op0 = Op0.getOperand(0);
8387 if (Op1.getOpcode() == ISD::TRUNCATE)
8388 Op1 = Op1.getOperand(0);
8389
Evan Chengd40d03e2010-01-06 19:38:29 +00008390 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008391 if (Op1.getOpcode() == ISD::SHL)
8392 std::swap(Op0, Op1);
8393 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008394 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8395 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008396 // If we looked past a truncate, check that it's only truncating away
8397 // known zeros.
8398 unsigned BitWidth = Op0.getValueSizeInBits();
8399 unsigned AndBitWidth = And.getValueSizeInBits();
8400 if (BitWidth > AndBitWidth) {
8401 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8402 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8403 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8404 return SDValue();
8405 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008406 LHS = Op1;
8407 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008408 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008409 } else if (Op1.getOpcode() == ISD::Constant) {
8410 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8411 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008412 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8413 LHS = AndLHS.getOperand(0);
8414 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008415 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008416 }
Evan Cheng0488db92007-09-25 01:57:46 +00008417
Evan Chengd40d03e2010-01-06 19:38:29 +00008418 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008419 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008420 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008421 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008422 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008423 // Also promote i16 to i32 for performance / code size reason.
8424 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008425 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008426 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008427
Evan Chengd40d03e2010-01-06 19:38:29 +00008428 // If the operand types disagree, extend the shift amount to match. Since
8429 // BT ignores high bits (like shifts) we can use anyextend.
8430 if (LHS.getValueType() != RHS.getValueType())
8431 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008432
Evan Chengd40d03e2010-01-06 19:38:29 +00008433 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8434 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8435 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8436 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008437 }
8438
Evan Cheng54de3ea2010-01-05 06:52:31 +00008439 return SDValue();
8440}
8441
Dan Gohmand858e902010-04-17 15:26:15 +00008442SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008443
8444 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8445
Evan Cheng54de3ea2010-01-05 06:52:31 +00008446 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8447 SDValue Op0 = Op.getOperand(0);
8448 SDValue Op1 = Op.getOperand(1);
8449 DebugLoc dl = Op.getDebugLoc();
8450 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8451
8452 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008453 // Lower (X & (1 << N)) == 0 to BT(X, N).
8454 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8455 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008456 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008457 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008458 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008459 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8460 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8461 if (NewSetCC.getNode())
8462 return NewSetCC;
8463 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008464
Chris Lattner481eebc2010-12-19 21:23:48 +00008465 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8466 // these.
8467 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008468 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008469 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8470 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008471
Chris Lattner481eebc2010-12-19 21:23:48 +00008472 // If the input is a setcc, then reuse the input setcc or use a new one with
8473 // the inverted condition.
8474 if (Op0.getOpcode() == X86ISD::SETCC) {
8475 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8476 bool Invert = (CC == ISD::SETNE) ^
8477 cast<ConstantSDNode>(Op1)->isNullValue();
8478 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008479
Evan Cheng2c755ba2010-02-27 07:36:59 +00008480 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008481 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8482 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8483 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008484 }
8485
Evan Chenge5b51ac2010-04-17 06:13:15 +00008486 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008487 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008488 if (X86CC == X86::COND_INVALID)
8489 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008490
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008491 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008492 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008493 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008494}
8495
Craig Topper89af15e2011-09-18 08:03:58 +00008496// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008497// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008498static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008499 EVT VT = Op.getValueType();
8500
Duncan Sands28b77e92011-09-06 19:07:46 +00008501 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008502 "Unsupported value type for operation");
8503
8504 int NumElems = VT.getVectorNumElements();
8505 DebugLoc dl = Op.getDebugLoc();
8506 SDValue CC = Op.getOperand(2);
8507 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8508 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8509
8510 // Extract the LHS vectors
8511 SDValue LHS = Op.getOperand(0);
8512 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8513 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8514
8515 // Extract the RHS vectors
8516 SDValue RHS = Op.getOperand(1);
8517 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8518 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8519
8520 // Issue the operation on the smaller types and concatenate the result back
8521 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8522 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8523 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8524 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8525 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8526}
8527
8528
Dan Gohmand858e902010-04-17 15:26:15 +00008529SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008530 SDValue Cond;
8531 SDValue Op0 = Op.getOperand(0);
8532 SDValue Op1 = Op.getOperand(1);
8533 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008534 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008535 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8536 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008537 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008538
8539 if (isFP) {
8540 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008541 EVT EltVT = Op0.getValueType().getVectorElementType();
8542 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8543
8544 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008545 bool Swap = false;
8546
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008547 // SSE Condition code mapping:
8548 // 0 - EQ
8549 // 1 - LT
8550 // 2 - LE
8551 // 3 - UNORD
8552 // 4 - NEQ
8553 // 5 - NLT
8554 // 6 - NLE
8555 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008556 switch (SetCCOpcode) {
8557 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008558 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008559 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008560 case ISD::SETOGT:
8561 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008562 case ISD::SETLT:
8563 case ISD::SETOLT: SSECC = 1; break;
8564 case ISD::SETOGE:
8565 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008566 case ISD::SETLE:
8567 case ISD::SETOLE: SSECC = 2; break;
8568 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008569 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008570 case ISD::SETNE: SSECC = 4; break;
8571 case ISD::SETULE: Swap = true;
8572 case ISD::SETUGE: SSECC = 5; break;
8573 case ISD::SETULT: Swap = true;
8574 case ISD::SETUGT: SSECC = 6; break;
8575 case ISD::SETO: SSECC = 7; break;
8576 }
8577 if (Swap)
8578 std::swap(Op0, Op1);
8579
Nate Begemanfb8ead02008-07-25 19:05:58 +00008580 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008581 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008582 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008583 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008584 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8585 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008586 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Craig Topper0a150352011-11-09 08:06:13 +00008587 } else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008588 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008589 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8590 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008591 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008592 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008593 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008594 }
8595 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008596 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008597 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008598
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008599 // Break 256-bit integer vector compare into smaller ones.
Craig Topper0a150352011-11-09 08:06:13 +00008600 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008601 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008602
Nate Begeman30a0de92008-07-17 16:51:19 +00008603 // We are handling one of the integer comparisons here. Since SSE only has
8604 // GT and EQ comparisons for integer, swapping operands and multiple
8605 // operations may be required for some comparisons.
8606 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8607 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008608
Craig Topper0a150352011-11-09 08:06:13 +00008609 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008610 default: break;
Craig Topper0a150352011-11-09 08:06:13 +00008611 case MVT::i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
8612 case MVT::i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
8613 case MVT::i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8614 case MVT::i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008615 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008616
Nate Begeman30a0de92008-07-17 16:51:19 +00008617 switch (SetCCOpcode) {
8618 default: break;
8619 case ISD::SETNE: Invert = true;
8620 case ISD::SETEQ: Opc = EQOpc; break;
8621 case ISD::SETLT: Swap = true;
8622 case ISD::SETGT: Opc = GTOpc; break;
8623 case ISD::SETGE: Swap = true;
8624 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8625 case ISD::SETULT: Swap = true;
8626 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8627 case ISD::SETUGE: Swap = true;
8628 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8629 }
8630 if (Swap)
8631 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008632
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008633 // Check that the operation in question is available (most are plain SSE2,
8634 // but PCMPGTQ and PCMPEQQ have different requirements).
8635 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42() && !Subtarget->hasAVX())
8636 return SDValue();
8637 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41() && !Subtarget->hasAVX())
8638 return SDValue();
8639
Nate Begeman30a0de92008-07-17 16:51:19 +00008640 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8641 // bits of the inputs before performing those operations.
8642 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008643 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008644 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8645 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008646 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008647 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8648 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008649 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8650 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008651 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008652
Dale Johannesenace16102009-02-03 19:33:06 +00008653 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008654
8655 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008656 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008657 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008658
Nate Begeman30a0de92008-07-17 16:51:19 +00008659 return Result;
8660}
Evan Cheng0488db92007-09-25 01:57:46 +00008661
Evan Cheng370e5342008-12-03 08:38:43 +00008662// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008663static bool isX86LogicalCmp(SDValue Op) {
8664 unsigned Opc = Op.getNode()->getOpcode();
8665 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8666 return true;
8667 if (Op.getResNo() == 1 &&
8668 (Opc == X86ISD::ADD ||
8669 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008670 Opc == X86ISD::ADC ||
8671 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008672 Opc == X86ISD::SMUL ||
8673 Opc == X86ISD::UMUL ||
8674 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008675 Opc == X86ISD::DEC ||
8676 Opc == X86ISD::OR ||
8677 Opc == X86ISD::XOR ||
8678 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008679 return true;
8680
Chris Lattner9637d5b2010-12-05 07:49:54 +00008681 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8682 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008683
Dan Gohman076aee32009-03-04 19:44:21 +00008684 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008685}
8686
Chris Lattnera2b56002010-12-05 01:23:24 +00008687static bool isZero(SDValue V) {
8688 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8689 return C && C->isNullValue();
8690}
8691
Chris Lattner96908b12010-12-05 02:00:51 +00008692static bool isAllOnes(SDValue V) {
8693 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8694 return C && C->isAllOnesValue();
8695}
8696
Dan Gohmand858e902010-04-17 15:26:15 +00008697SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008698 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008699 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008700 SDValue Op1 = Op.getOperand(1);
8701 SDValue Op2 = Op.getOperand(2);
8702 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008703 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008704
Dan Gohman1a492952009-10-20 16:22:37 +00008705 if (Cond.getOpcode() == ISD::SETCC) {
8706 SDValue NewCond = LowerSETCC(Cond, DAG);
8707 if (NewCond.getNode())
8708 Cond = NewCond;
8709 }
Evan Cheng734503b2006-09-11 02:19:56 +00008710
Chris Lattnera2b56002010-12-05 01:23:24 +00008711 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008712 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008713 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008714 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008715 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008716 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8717 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008718 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008719
Chris Lattnera2b56002010-12-05 01:23:24 +00008720 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008721
8722 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008723 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8724 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008725
8726 SDValue CmpOp0 = Cmp.getOperand(0);
8727 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8728 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008729
Chris Lattner96908b12010-12-05 02:00:51 +00008730 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008731 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8732 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008733
Chris Lattner96908b12010-12-05 02:00:51 +00008734 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8735 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008736
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008737 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008738 if (N2C == 0 || !N2C->isNullValue())
8739 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8740 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008741 }
8742 }
8743
Chris Lattnera2b56002010-12-05 01:23:24 +00008744 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008745 if (Cond.getOpcode() == ISD::AND &&
8746 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8747 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008748 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008749 Cond = Cond.getOperand(0);
8750 }
8751
Evan Cheng3f41d662007-10-08 22:16:29 +00008752 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8753 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008754 unsigned CondOpcode = Cond.getOpcode();
8755 if (CondOpcode == X86ISD::SETCC ||
8756 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008757 CC = Cond.getOperand(0);
8758
Dan Gohman475871a2008-07-27 21:46:04 +00008759 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008760 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008761 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008762
Evan Cheng3f41d662007-10-08 22:16:29 +00008763 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008764 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008765 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008766 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008767
Chris Lattnerd1980a52009-03-12 06:52:53 +00008768 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8769 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008770 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008771 addTest = false;
8772 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008773 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8774 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8775 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8776 Cond.getOperand(0).getValueType() != MVT::i8)) {
8777 SDValue LHS = Cond.getOperand(0);
8778 SDValue RHS = Cond.getOperand(1);
8779 unsigned X86Opcode;
8780 unsigned X86Cond;
8781 SDVTList VTs;
8782 switch (CondOpcode) {
8783 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8784 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8785 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8786 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8787 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8788 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8789 default: llvm_unreachable("unexpected overflowing operator");
8790 }
8791 if (CondOpcode == ISD::UMULO)
8792 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8793 MVT::i32);
8794 else
8795 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8796
8797 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8798
8799 if (CondOpcode == ISD::UMULO)
8800 Cond = X86Op.getValue(2);
8801 else
8802 Cond = X86Op.getValue(1);
8803
8804 CC = DAG.getConstant(X86Cond, MVT::i8);
8805 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008806 }
8807
8808 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008809 // Look pass the truncate.
8810 if (Cond.getOpcode() == ISD::TRUNCATE)
8811 Cond = Cond.getOperand(0);
8812
8813 // We know the result of AND is compared against zero. Try to match
8814 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008815 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008816 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008817 if (NewSetCC.getNode()) {
8818 CC = NewSetCC.getOperand(0);
8819 Cond = NewSetCC.getOperand(1);
8820 addTest = false;
8821 }
8822 }
8823 }
8824
8825 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008826 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008827 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008828 }
8829
Benjamin Kramere915ff32010-12-22 23:09:28 +00008830 // a < b ? -1 : 0 -> RES = ~setcc_carry
8831 // a < b ? 0 : -1 -> RES = setcc_carry
8832 // a >= b ? -1 : 0 -> RES = setcc_carry
8833 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8834 if (Cond.getOpcode() == X86ISD::CMP) {
8835 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8836
8837 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8838 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8839 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8840 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8841 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8842 return DAG.getNOT(DL, Res, Res.getValueType());
8843 return Res;
8844 }
8845 }
8846
Evan Cheng0488db92007-09-25 01:57:46 +00008847 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8848 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008849 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008850 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008851 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008852}
8853
Evan Cheng370e5342008-12-03 08:38:43 +00008854// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8855// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8856// from the AND / OR.
8857static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8858 Opc = Op.getOpcode();
8859 if (Opc != ISD::OR && Opc != ISD::AND)
8860 return false;
8861 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8862 Op.getOperand(0).hasOneUse() &&
8863 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8864 Op.getOperand(1).hasOneUse());
8865}
8866
Evan Cheng961d6d42009-02-02 08:19:07 +00008867// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8868// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008869static bool isXor1OfSetCC(SDValue Op) {
8870 if (Op.getOpcode() != ISD::XOR)
8871 return false;
8872 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8873 if (N1C && N1C->getAPIntValue() == 1) {
8874 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8875 Op.getOperand(0).hasOneUse();
8876 }
8877 return false;
8878}
8879
Dan Gohmand858e902010-04-17 15:26:15 +00008880SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008881 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008882 SDValue Chain = Op.getOperand(0);
8883 SDValue Cond = Op.getOperand(1);
8884 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008885 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008886 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008887 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008888
Dan Gohman1a492952009-10-20 16:22:37 +00008889 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008890 // Check for setcc([su]{add,sub,mul}o == 0).
8891 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8892 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8893 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8894 Cond.getOperand(0).getResNo() == 1 &&
8895 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
8896 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
8897 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
8898 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
8899 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
8900 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
8901 Inverted = true;
8902 Cond = Cond.getOperand(0);
8903 } else {
8904 SDValue NewCond = LowerSETCC(Cond, DAG);
8905 if (NewCond.getNode())
8906 Cond = NewCond;
8907 }
Dan Gohman1a492952009-10-20 16:22:37 +00008908 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008909#if 0
8910 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008911 else if (Cond.getOpcode() == X86ISD::ADD ||
8912 Cond.getOpcode() == X86ISD::SUB ||
8913 Cond.getOpcode() == X86ISD::SMUL ||
8914 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008915 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008916#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008917
Evan Chengad9c0a32009-12-15 00:53:42 +00008918 // Look pass (and (setcc_carry (cmp ...)), 1).
8919 if (Cond.getOpcode() == ISD::AND &&
8920 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8921 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008922 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008923 Cond = Cond.getOperand(0);
8924 }
8925
Evan Cheng3f41d662007-10-08 22:16:29 +00008926 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8927 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008928 unsigned CondOpcode = Cond.getOpcode();
8929 if (CondOpcode == X86ISD::SETCC ||
8930 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008931 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008932
Dan Gohman475871a2008-07-27 21:46:04 +00008933 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008934 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008935 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008936 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008937 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008938 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008939 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008940 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008941 default: break;
8942 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008943 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008944 // These can only come from an arithmetic instruction with overflow,
8945 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008946 Cond = Cond.getNode()->getOperand(1);
8947 addTest = false;
8948 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008949 }
Evan Cheng0488db92007-09-25 01:57:46 +00008950 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008951 }
8952 CondOpcode = Cond.getOpcode();
8953 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8954 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8955 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8956 Cond.getOperand(0).getValueType() != MVT::i8)) {
8957 SDValue LHS = Cond.getOperand(0);
8958 SDValue RHS = Cond.getOperand(1);
8959 unsigned X86Opcode;
8960 unsigned X86Cond;
8961 SDVTList VTs;
8962 switch (CondOpcode) {
8963 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8964 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8965 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8966 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8967 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8968 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8969 default: llvm_unreachable("unexpected overflowing operator");
8970 }
8971 if (Inverted)
8972 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
8973 if (CondOpcode == ISD::UMULO)
8974 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8975 MVT::i32);
8976 else
8977 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8978
8979 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
8980
8981 if (CondOpcode == ISD::UMULO)
8982 Cond = X86Op.getValue(2);
8983 else
8984 Cond = X86Op.getValue(1);
8985
8986 CC = DAG.getConstant(X86Cond, MVT::i8);
8987 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00008988 } else {
8989 unsigned CondOpc;
8990 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8991 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008992 if (CondOpc == ISD::OR) {
8993 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8994 // two branches instead of an explicit OR instruction with a
8995 // separate test.
8996 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008997 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008998 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008999 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009000 Chain, Dest, CC, Cmp);
9001 CC = Cond.getOperand(1).getOperand(0);
9002 Cond = Cmp;
9003 addTest = false;
9004 }
9005 } else { // ISD::AND
9006 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9007 // two branches instead of an explicit AND instruction with a
9008 // separate test. However, we only do this if this block doesn't
9009 // have a fall-through edge, because this requires an explicit
9010 // jmp when the condition is false.
9011 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009012 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009013 Op.getNode()->hasOneUse()) {
9014 X86::CondCode CCode =
9015 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9016 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009017 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009018 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009019 // Look for an unconditional branch following this conditional branch.
9020 // We need this because we need to reverse the successors in order
9021 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009022 if (User->getOpcode() == ISD::BR) {
9023 SDValue FalseBB = User->getOperand(1);
9024 SDNode *NewBR =
9025 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009026 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009027 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009028 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009029
Dale Johannesene4d209d2009-02-03 20:21:25 +00009030 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009031 Chain, Dest, CC, Cmp);
9032 X86::CondCode CCode =
9033 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9034 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009035 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009036 Cond = Cmp;
9037 addTest = false;
9038 }
9039 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009040 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009041 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9042 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9043 // It should be transformed during dag combiner except when the condition
9044 // is set by a arithmetics with overflow node.
9045 X86::CondCode CCode =
9046 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9047 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009048 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009049 Cond = Cond.getOperand(0).getOperand(1);
9050 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009051 } else if (Cond.getOpcode() == ISD::SETCC &&
9052 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9053 // For FCMP_OEQ, we can emit
9054 // two branches instead of an explicit AND instruction with a
9055 // separate test. However, we only do this if this block doesn't
9056 // have a fall-through edge, because this requires an explicit
9057 // jmp when the condition is false.
9058 if (Op.getNode()->hasOneUse()) {
9059 SDNode *User = *Op.getNode()->use_begin();
9060 // Look for an unconditional branch following this conditional branch.
9061 // We need this because we need to reverse the successors in order
9062 // to implement FCMP_OEQ.
9063 if (User->getOpcode() == ISD::BR) {
9064 SDValue FalseBB = User->getOperand(1);
9065 SDNode *NewBR =
9066 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9067 assert(NewBR == User);
9068 (void)NewBR;
9069 Dest = FalseBB;
9070
9071 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9072 Cond.getOperand(0), Cond.getOperand(1));
9073 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9074 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9075 Chain, Dest, CC, Cmp);
9076 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9077 Cond = Cmp;
9078 addTest = false;
9079 }
9080 }
9081 } else if (Cond.getOpcode() == ISD::SETCC &&
9082 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9083 // For FCMP_UNE, we can emit
9084 // two branches instead of an explicit AND instruction with a
9085 // separate test. However, we only do this if this block doesn't
9086 // have a fall-through edge, because this requires an explicit
9087 // jmp when the condition is false.
9088 if (Op.getNode()->hasOneUse()) {
9089 SDNode *User = *Op.getNode()->use_begin();
9090 // Look for an unconditional branch following this conditional branch.
9091 // We need this because we need to reverse the successors in order
9092 // to implement FCMP_UNE.
9093 if (User->getOpcode() == ISD::BR) {
9094 SDValue FalseBB = User->getOperand(1);
9095 SDNode *NewBR =
9096 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9097 assert(NewBR == User);
9098 (void)NewBR;
9099
9100 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9101 Cond.getOperand(0), Cond.getOperand(1));
9102 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9103 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9104 Chain, Dest, CC, Cmp);
9105 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9106 Cond = Cmp;
9107 addTest = false;
9108 Dest = FalseBB;
9109 }
9110 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009111 }
Evan Cheng0488db92007-09-25 01:57:46 +00009112 }
9113
9114 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009115 // Look pass the truncate.
9116 if (Cond.getOpcode() == ISD::TRUNCATE)
9117 Cond = Cond.getOperand(0);
9118
9119 // We know the result of AND is compared against zero. Try to match
9120 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009121 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009122 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9123 if (NewSetCC.getNode()) {
9124 CC = NewSetCC.getOperand(0);
9125 Cond = NewSetCC.getOperand(1);
9126 addTest = false;
9127 }
9128 }
9129 }
9130
9131 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009132 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009133 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009134 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00009135 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009136 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009137}
9138
Anton Korobeynikove060b532007-04-17 19:34:00 +00009139
9140// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9141// Calls to _alloca is needed to probe the stack when allocating more than 4k
9142// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9143// that the guard pages used by the OS virtual memory manager are allocated in
9144// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009145SDValue
9146X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009147 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009148 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
9149 EnableSegmentedStacks) &&
9150 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009151 "are being used");
9152 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009153 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009154
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009155 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009156 SDValue Chain = Op.getOperand(0);
9157 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009158 // FIXME: Ensure alignment here
9159
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009160 bool Is64Bit = Subtarget->is64Bit();
9161 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009162
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009163 if (EnableSegmentedStacks) {
9164 MachineFunction &MF = DAG.getMachineFunction();
9165 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009166
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009167 if (Is64Bit) {
9168 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009169 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009170 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009171
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009172 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
9173 I != E; I++)
9174 if (I->hasNestAttr())
9175 report_fatal_error("Cannot use segmented stacks with functions that "
9176 "have nested arguments.");
9177 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009178
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009179 const TargetRegisterClass *AddrRegClass =
9180 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9181 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9182 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9183 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9184 DAG.getRegister(Vreg, SPTy));
9185 SDValue Ops1[2] = { Value, Chain };
9186 return DAG.getMergeValues(Ops1, 2, dl);
9187 } else {
9188 SDValue Flag;
9189 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009190
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009191 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9192 Flag = Chain.getValue(1);
9193 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009194
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009195 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9196 Flag = Chain.getValue(1);
9197
9198 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9199
9200 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9201 return DAG.getMergeValues(Ops1, 2, dl);
9202 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009203}
9204
Dan Gohmand858e902010-04-17 15:26:15 +00009205SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009206 MachineFunction &MF = DAG.getMachineFunction();
9207 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9208
Dan Gohman69de1932008-02-06 22:27:42 +00009209 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009210 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009211
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009212 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009213 // vastart just stores the address of the VarArgsFrameIndex slot into the
9214 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009215 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9216 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009217 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9218 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009219 }
9220
9221 // __va_list_tag:
9222 // gp_offset (0 - 6 * 8)
9223 // fp_offset (48 - 48 + 8 * 16)
9224 // overflow_arg_area (point to parameters coming in memory).
9225 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009226 SmallVector<SDValue, 8> MemOps;
9227 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009228 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009229 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009230 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9231 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009232 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009233 MemOps.push_back(Store);
9234
9235 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009236 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009237 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009238 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009239 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9240 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009241 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009242 MemOps.push_back(Store);
9243
9244 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009245 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009246 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009247 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9248 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009249 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9250 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009251 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009252 MemOps.push_back(Store);
9253
9254 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009255 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009256 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009257 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9258 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009259 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9260 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009261 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009262 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009263 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009264}
9265
Dan Gohmand858e902010-04-17 15:26:15 +00009266SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009267 assert(Subtarget->is64Bit() &&
9268 "LowerVAARG only handles 64-bit va_arg!");
9269 assert((Subtarget->isTargetLinux() ||
9270 Subtarget->isTargetDarwin()) &&
9271 "Unhandled target in LowerVAARG");
9272 assert(Op.getNode()->getNumOperands() == 4);
9273 SDValue Chain = Op.getOperand(0);
9274 SDValue SrcPtr = Op.getOperand(1);
9275 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9276 unsigned Align = Op.getConstantOperandVal(3);
9277 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009278
Dan Gohman320afb82010-10-12 18:00:49 +00009279 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009280 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009281 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9282 uint8_t ArgMode;
9283
9284 // Decide which area this value should be read from.
9285 // TODO: Implement the AMD64 ABI in its entirety. This simple
9286 // selection mechanism works only for the basic types.
9287 if (ArgVT == MVT::f80) {
9288 llvm_unreachable("va_arg for f80 not yet implemented");
9289 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9290 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9291 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9292 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9293 } else {
9294 llvm_unreachable("Unhandled argument type in LowerVAARG");
9295 }
9296
9297 if (ArgMode == 2) {
9298 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009299 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009300 !(DAG.getMachineFunction()
9301 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009302 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009303 }
9304
9305 // Insert VAARG_64 node into the DAG
9306 // VAARG_64 returns two values: Variable Argument Address, Chain
9307 SmallVector<SDValue, 11> InstOps;
9308 InstOps.push_back(Chain);
9309 InstOps.push_back(SrcPtr);
9310 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9311 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9312 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9313 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9314 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9315 VTs, &InstOps[0], InstOps.size(),
9316 MVT::i64,
9317 MachinePointerInfo(SV),
9318 /*Align=*/0,
9319 /*Volatile=*/false,
9320 /*ReadMem=*/true,
9321 /*WriteMem=*/true);
9322 Chain = VAARG.getValue(1);
9323
9324 // Load the next argument and return it
9325 return DAG.getLoad(ArgVT, dl,
9326 Chain,
9327 VAARG,
9328 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009329 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009330}
9331
Dan Gohmand858e902010-04-17 15:26:15 +00009332SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009333 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009334 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009335 SDValue Chain = Op.getOperand(0);
9336 SDValue DstPtr = Op.getOperand(1);
9337 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009338 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9339 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009340 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009341
Chris Lattnere72f2022010-09-21 05:40:29 +00009342 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009343 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009344 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009345 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009346}
9347
Dan Gohman475871a2008-07-27 21:46:04 +00009348SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009349X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009350 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009351 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009352 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009353 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009354 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009355 case Intrinsic::x86_sse_comieq_ss:
9356 case Intrinsic::x86_sse_comilt_ss:
9357 case Intrinsic::x86_sse_comile_ss:
9358 case Intrinsic::x86_sse_comigt_ss:
9359 case Intrinsic::x86_sse_comige_ss:
9360 case Intrinsic::x86_sse_comineq_ss:
9361 case Intrinsic::x86_sse_ucomieq_ss:
9362 case Intrinsic::x86_sse_ucomilt_ss:
9363 case Intrinsic::x86_sse_ucomile_ss:
9364 case Intrinsic::x86_sse_ucomigt_ss:
9365 case Intrinsic::x86_sse_ucomige_ss:
9366 case Intrinsic::x86_sse_ucomineq_ss:
9367 case Intrinsic::x86_sse2_comieq_sd:
9368 case Intrinsic::x86_sse2_comilt_sd:
9369 case Intrinsic::x86_sse2_comile_sd:
9370 case Intrinsic::x86_sse2_comigt_sd:
9371 case Intrinsic::x86_sse2_comige_sd:
9372 case Intrinsic::x86_sse2_comineq_sd:
9373 case Intrinsic::x86_sse2_ucomieq_sd:
9374 case Intrinsic::x86_sse2_ucomilt_sd:
9375 case Intrinsic::x86_sse2_ucomile_sd:
9376 case Intrinsic::x86_sse2_ucomigt_sd:
9377 case Intrinsic::x86_sse2_ucomige_sd:
9378 case Intrinsic::x86_sse2_ucomineq_sd: {
9379 unsigned Opc = 0;
9380 ISD::CondCode CC = ISD::SETCC_INVALID;
9381 switch (IntNo) {
9382 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009383 case Intrinsic::x86_sse_comieq_ss:
9384 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009385 Opc = X86ISD::COMI;
9386 CC = ISD::SETEQ;
9387 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009388 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009389 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009390 Opc = X86ISD::COMI;
9391 CC = ISD::SETLT;
9392 break;
9393 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009394 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009395 Opc = X86ISD::COMI;
9396 CC = ISD::SETLE;
9397 break;
9398 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009399 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009400 Opc = X86ISD::COMI;
9401 CC = ISD::SETGT;
9402 break;
9403 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009404 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009405 Opc = X86ISD::COMI;
9406 CC = ISD::SETGE;
9407 break;
9408 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009409 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009410 Opc = X86ISD::COMI;
9411 CC = ISD::SETNE;
9412 break;
9413 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009414 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009415 Opc = X86ISD::UCOMI;
9416 CC = ISD::SETEQ;
9417 break;
9418 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009419 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009420 Opc = X86ISD::UCOMI;
9421 CC = ISD::SETLT;
9422 break;
9423 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009424 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009425 Opc = X86ISD::UCOMI;
9426 CC = ISD::SETLE;
9427 break;
9428 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009429 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009430 Opc = X86ISD::UCOMI;
9431 CC = ISD::SETGT;
9432 break;
9433 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009434 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009435 Opc = X86ISD::UCOMI;
9436 CC = ISD::SETGE;
9437 break;
9438 case Intrinsic::x86_sse_ucomineq_ss:
9439 case Intrinsic::x86_sse2_ucomineq_sd:
9440 Opc = X86ISD::UCOMI;
9441 CC = ISD::SETNE;
9442 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009443 }
Evan Cheng734503b2006-09-11 02:19:56 +00009444
Dan Gohman475871a2008-07-27 21:46:04 +00009445 SDValue LHS = Op.getOperand(1);
9446 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009447 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009448 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009449 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9450 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9451 DAG.getConstant(X86CC, MVT::i8), Cond);
9452 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009453 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009454 // Arithmetic intrinsics.
9455 case Intrinsic::x86_sse3_hadd_ps:
9456 case Intrinsic::x86_sse3_hadd_pd:
9457 case Intrinsic::x86_avx_hadd_ps_256:
9458 case Intrinsic::x86_avx_hadd_pd_256:
9459 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9460 Op.getOperand(1), Op.getOperand(2));
9461 case Intrinsic::x86_sse3_hsub_ps:
9462 case Intrinsic::x86_sse3_hsub_pd:
9463 case Intrinsic::x86_avx_hsub_ps_256:
9464 case Intrinsic::x86_avx_hsub_pd_256:
9465 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9466 Op.getOperand(1), Op.getOperand(2));
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009467 // ptest and testp intrinsics. The intrinsic these come from are designed to
9468 // return an integer value, not just an instruction so lower it to the ptest
9469 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009470 case Intrinsic::x86_sse41_ptestz:
9471 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009472 case Intrinsic::x86_sse41_ptestnzc:
9473 case Intrinsic::x86_avx_ptestz_256:
9474 case Intrinsic::x86_avx_ptestc_256:
9475 case Intrinsic::x86_avx_ptestnzc_256:
9476 case Intrinsic::x86_avx_vtestz_ps:
9477 case Intrinsic::x86_avx_vtestc_ps:
9478 case Intrinsic::x86_avx_vtestnzc_ps:
9479 case Intrinsic::x86_avx_vtestz_pd:
9480 case Intrinsic::x86_avx_vtestc_pd:
9481 case Intrinsic::x86_avx_vtestnzc_pd:
9482 case Intrinsic::x86_avx_vtestz_ps_256:
9483 case Intrinsic::x86_avx_vtestc_ps_256:
9484 case Intrinsic::x86_avx_vtestnzc_ps_256:
9485 case Intrinsic::x86_avx_vtestz_pd_256:
9486 case Intrinsic::x86_avx_vtestc_pd_256:
9487 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9488 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009489 unsigned X86CC = 0;
9490 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009491 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009492 case Intrinsic::x86_avx_vtestz_ps:
9493 case Intrinsic::x86_avx_vtestz_pd:
9494 case Intrinsic::x86_avx_vtestz_ps_256:
9495 case Intrinsic::x86_avx_vtestz_pd_256:
9496 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009497 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009498 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009499 // ZF = 1
9500 X86CC = X86::COND_E;
9501 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009502 case Intrinsic::x86_avx_vtestc_ps:
9503 case Intrinsic::x86_avx_vtestc_pd:
9504 case Intrinsic::x86_avx_vtestc_ps_256:
9505 case Intrinsic::x86_avx_vtestc_pd_256:
9506 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009507 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009508 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009509 // CF = 1
9510 X86CC = X86::COND_B;
9511 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009512 case Intrinsic::x86_avx_vtestnzc_ps:
9513 case Intrinsic::x86_avx_vtestnzc_pd:
9514 case Intrinsic::x86_avx_vtestnzc_ps_256:
9515 case Intrinsic::x86_avx_vtestnzc_pd_256:
9516 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009517 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009518 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009519 // ZF and CF = 0
9520 X86CC = X86::COND_A;
9521 break;
9522 }
Eric Christopherfd179292009-08-27 18:07:15 +00009523
Eric Christopher71c67532009-07-29 00:28:05 +00009524 SDValue LHS = Op.getOperand(1);
9525 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009526 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9527 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009528 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9529 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9530 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009531 }
Evan Cheng5759f972008-05-04 09:15:50 +00009532
9533 // Fix vector shift instructions where the last operand is a non-immediate
9534 // i32 value.
Craig Topper7be5dfd2011-11-12 09:58:49 +00009535 case Intrinsic::x86_avx2_pslli_w:
9536 case Intrinsic::x86_avx2_pslli_d:
9537 case Intrinsic::x86_avx2_pslli_q:
9538 case Intrinsic::x86_avx2_psrli_w:
9539 case Intrinsic::x86_avx2_psrli_d:
9540 case Intrinsic::x86_avx2_psrli_q:
9541 case Intrinsic::x86_avx2_psrai_w:
9542 case Intrinsic::x86_avx2_psrai_d:
Evan Cheng5759f972008-05-04 09:15:50 +00009543 case Intrinsic::x86_sse2_pslli_w:
9544 case Intrinsic::x86_sse2_pslli_d:
9545 case Intrinsic::x86_sse2_pslli_q:
9546 case Intrinsic::x86_sse2_psrli_w:
9547 case Intrinsic::x86_sse2_psrli_d:
9548 case Intrinsic::x86_sse2_psrli_q:
9549 case Intrinsic::x86_sse2_psrai_w:
9550 case Intrinsic::x86_sse2_psrai_d:
9551 case Intrinsic::x86_mmx_pslli_w:
9552 case Intrinsic::x86_mmx_pslli_d:
9553 case Intrinsic::x86_mmx_pslli_q:
9554 case Intrinsic::x86_mmx_psrli_w:
9555 case Intrinsic::x86_mmx_psrli_d:
9556 case Intrinsic::x86_mmx_psrli_q:
9557 case Intrinsic::x86_mmx_psrai_w:
9558 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009559 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009560 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009561 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009562
9563 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009564 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009565 switch (IntNo) {
9566 case Intrinsic::x86_sse2_pslli_w:
9567 NewIntNo = Intrinsic::x86_sse2_psll_w;
9568 break;
9569 case Intrinsic::x86_sse2_pslli_d:
9570 NewIntNo = Intrinsic::x86_sse2_psll_d;
9571 break;
9572 case Intrinsic::x86_sse2_pslli_q:
9573 NewIntNo = Intrinsic::x86_sse2_psll_q;
9574 break;
9575 case Intrinsic::x86_sse2_psrli_w:
9576 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9577 break;
9578 case Intrinsic::x86_sse2_psrli_d:
9579 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9580 break;
9581 case Intrinsic::x86_sse2_psrli_q:
9582 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9583 break;
9584 case Intrinsic::x86_sse2_psrai_w:
9585 NewIntNo = Intrinsic::x86_sse2_psra_w;
9586 break;
9587 case Intrinsic::x86_sse2_psrai_d:
9588 NewIntNo = Intrinsic::x86_sse2_psra_d;
9589 break;
Craig Topper7be5dfd2011-11-12 09:58:49 +00009590 case Intrinsic::x86_avx2_pslli_w:
9591 NewIntNo = Intrinsic::x86_avx2_psll_w;
9592 break;
9593 case Intrinsic::x86_avx2_pslli_d:
9594 NewIntNo = Intrinsic::x86_avx2_psll_d;
9595 break;
9596 case Intrinsic::x86_avx2_pslli_q:
9597 NewIntNo = Intrinsic::x86_avx2_psll_q;
9598 break;
9599 case Intrinsic::x86_avx2_psrli_w:
9600 NewIntNo = Intrinsic::x86_avx2_psrl_w;
9601 break;
9602 case Intrinsic::x86_avx2_psrli_d:
9603 NewIntNo = Intrinsic::x86_avx2_psrl_d;
9604 break;
9605 case Intrinsic::x86_avx2_psrli_q:
9606 NewIntNo = Intrinsic::x86_avx2_psrl_q;
9607 break;
9608 case Intrinsic::x86_avx2_psrai_w:
9609 NewIntNo = Intrinsic::x86_avx2_psra_w;
9610 break;
9611 case Intrinsic::x86_avx2_psrai_d:
9612 NewIntNo = Intrinsic::x86_avx2_psra_d;
9613 break;
Evan Cheng5759f972008-05-04 09:15:50 +00009614 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009615 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009616 switch (IntNo) {
9617 case Intrinsic::x86_mmx_pslli_w:
9618 NewIntNo = Intrinsic::x86_mmx_psll_w;
9619 break;
9620 case Intrinsic::x86_mmx_pslli_d:
9621 NewIntNo = Intrinsic::x86_mmx_psll_d;
9622 break;
9623 case Intrinsic::x86_mmx_pslli_q:
9624 NewIntNo = Intrinsic::x86_mmx_psll_q;
9625 break;
9626 case Intrinsic::x86_mmx_psrli_w:
9627 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9628 break;
9629 case Intrinsic::x86_mmx_psrli_d:
9630 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9631 break;
9632 case Intrinsic::x86_mmx_psrli_q:
9633 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9634 break;
9635 case Intrinsic::x86_mmx_psrai_w:
9636 NewIntNo = Intrinsic::x86_mmx_psra_w;
9637 break;
9638 case Intrinsic::x86_mmx_psrai_d:
9639 NewIntNo = Intrinsic::x86_mmx_psra_d;
9640 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009641 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009642 }
9643 break;
9644 }
9645 }
Mon P Wangefa42202009-09-03 19:56:25 +00009646
9647 // The vector shift intrinsics with scalars uses 32b shift amounts but
9648 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9649 // to be zero.
9650 SDValue ShOps[4];
9651 ShOps[0] = ShAmt;
9652 ShOps[1] = DAG.getConstant(0, MVT::i32);
9653 if (ShAmtVT == MVT::v4i32) {
9654 ShOps[2] = DAG.getUNDEF(MVT::i32);
9655 ShOps[3] = DAG.getUNDEF(MVT::i32);
9656 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9657 } else {
9658 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009659// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009660 }
9661
Owen Andersone50ed302009-08-10 22:56:29 +00009662 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009663 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009664 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009665 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009666 Op.getOperand(1), ShAmt);
9667 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009668 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009669}
Evan Cheng72261582005-12-20 06:22:03 +00009670
Dan Gohmand858e902010-04-17 15:26:15 +00009671SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9672 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009673 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9674 MFI->setReturnAddressIsTaken(true);
9675
Bill Wendling64e87322009-01-16 19:25:27 +00009676 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009677 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009678
9679 if (Depth > 0) {
9680 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9681 SDValue Offset =
9682 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009683 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009684 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009685 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009686 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009687 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009688 }
9689
9690 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009691 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009692 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009693 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009694}
9695
Dan Gohmand858e902010-04-17 15:26:15 +00009696SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009697 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9698 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009699
Owen Andersone50ed302009-08-10 22:56:29 +00009700 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009701 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009702 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9703 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009704 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009705 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009706 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9707 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009708 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009709 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009710}
9711
Dan Gohman475871a2008-07-27 21:46:04 +00009712SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009713 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009714 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009715}
9716
Dan Gohmand858e902010-04-17 15:26:15 +00009717SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009718 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009719 SDValue Chain = Op.getOperand(0);
9720 SDValue Offset = Op.getOperand(1);
9721 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009722 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009723
Dan Gohmand8816272010-08-11 18:14:00 +00009724 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9725 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9726 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009727 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009728
Dan Gohmand8816272010-08-11 18:14:00 +00009729 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9730 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009731 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009732 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9733 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009734 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009735 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009736
Dale Johannesene4d209d2009-02-03 20:21:25 +00009737 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009738 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009739 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009740}
9741
Duncan Sands4a544a72011-09-06 13:37:06 +00009742SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9743 SelectionDAG &DAG) const {
9744 return Op.getOperand(0);
9745}
9746
9747SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9748 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009749 SDValue Root = Op.getOperand(0);
9750 SDValue Trmp = Op.getOperand(1); // trampoline
9751 SDValue FPtr = Op.getOperand(2); // nested function
9752 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009753 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009754
Dan Gohman69de1932008-02-06 22:27:42 +00009755 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009756
9757 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009758 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009759
9760 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009761 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9762 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009763
Evan Cheng0e6a0522011-07-18 20:57:22 +00009764 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9765 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009766
9767 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9768
9769 // Load the pointer to the nested function into R11.
9770 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009771 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009772 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009773 Addr, MachinePointerInfo(TrmpAddr),
9774 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009775
Owen Anderson825b72b2009-08-11 20:47:22 +00009776 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9777 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009778 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9779 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009780 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009781
9782 // Load the 'nest' parameter value into R10.
9783 // R10 is specified in X86CallingConv.td
9784 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009785 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9786 DAG.getConstant(10, MVT::i64));
9787 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009788 Addr, MachinePointerInfo(TrmpAddr, 10),
9789 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009790
Owen Anderson825b72b2009-08-11 20:47:22 +00009791 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9792 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009793 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9794 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009795 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009796
9797 // Jump to the nested function.
9798 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009799 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9800 DAG.getConstant(20, MVT::i64));
9801 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009802 Addr, MachinePointerInfo(TrmpAddr, 20),
9803 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009804
9805 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009806 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9807 DAG.getConstant(22, MVT::i64));
9808 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009809 MachinePointerInfo(TrmpAddr, 22),
9810 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009811
Duncan Sands4a544a72011-09-06 13:37:06 +00009812 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009813 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009814 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009815 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009816 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009817 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009818
9819 switch (CC) {
9820 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009821 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009822 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009823 case CallingConv::X86_StdCall: {
9824 // Pass 'nest' parameter in ECX.
9825 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009826 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009827
9828 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009829 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009830 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009831
Chris Lattner58d74912008-03-12 17:45:29 +00009832 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009833 unsigned InRegCount = 0;
9834 unsigned Idx = 1;
9835
9836 for (FunctionType::param_iterator I = FTy->param_begin(),
9837 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009838 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009839 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009840 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009841
9842 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009843 report_fatal_error("Nest register in use - reduce number of inreg"
9844 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009845 }
9846 }
9847 break;
9848 }
9849 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009850 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009851 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009852 // Pass 'nest' parameter in EAX.
9853 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009854 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009855 break;
9856 }
9857
Dan Gohman475871a2008-07-27 21:46:04 +00009858 SDValue OutChains[4];
9859 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009860
Owen Anderson825b72b2009-08-11 20:47:22 +00009861 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9862 DAG.getConstant(10, MVT::i32));
9863 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009864
Chris Lattnera62fe662010-02-05 19:20:30 +00009865 // This is storing the opcode for MOV32ri.
9866 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009867 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009868 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009869 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009870 Trmp, MachinePointerInfo(TrmpAddr),
9871 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009872
Owen Anderson825b72b2009-08-11 20:47:22 +00009873 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9874 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009875 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9876 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009877 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009878
Chris Lattnera62fe662010-02-05 19:20:30 +00009879 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009880 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9881 DAG.getConstant(5, MVT::i32));
9882 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009883 MachinePointerInfo(TrmpAddr, 5),
9884 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009885
Owen Anderson825b72b2009-08-11 20:47:22 +00009886 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9887 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009888 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9889 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009890 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009891
Duncan Sands4a544a72011-09-06 13:37:06 +00009892 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009893 }
9894}
9895
Dan Gohmand858e902010-04-17 15:26:15 +00009896SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9897 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009898 /*
9899 The rounding mode is in bits 11:10 of FPSR, and has the following
9900 settings:
9901 00 Round to nearest
9902 01 Round to -inf
9903 10 Round to +inf
9904 11 Round to 0
9905
9906 FLT_ROUNDS, on the other hand, expects the following:
9907 -1 Undefined
9908 0 Round to 0
9909 1 Round to nearest
9910 2 Round to +inf
9911 3 Round to -inf
9912
9913 To perform the conversion, we do:
9914 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9915 */
9916
9917 MachineFunction &MF = DAG.getMachineFunction();
9918 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009919 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009920 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009921 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009922 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009923
9924 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009925 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009926 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009927
Michael J. Spencerec38de22010-10-10 22:04:20 +00009928
Chris Lattner2156b792010-09-22 01:11:26 +00009929 MachineMemOperand *MMO =
9930 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9931 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009932
Chris Lattner2156b792010-09-22 01:11:26 +00009933 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9934 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9935 DAG.getVTList(MVT::Other),
9936 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009937
9938 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009939 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009940 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009941
9942 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009943 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009944 DAG.getNode(ISD::SRL, DL, MVT::i16,
9945 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009946 CWD, DAG.getConstant(0x800, MVT::i16)),
9947 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009948 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009949 DAG.getNode(ISD::SRL, DL, MVT::i16,
9950 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009951 CWD, DAG.getConstant(0x400, MVT::i16)),
9952 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009953
Dan Gohman475871a2008-07-27 21:46:04 +00009954 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009955 DAG.getNode(ISD::AND, DL, MVT::i16,
9956 DAG.getNode(ISD::ADD, DL, MVT::i16,
9957 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009958 DAG.getConstant(1, MVT::i16)),
9959 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009960
9961
Duncan Sands83ec4b62008-06-06 12:08:01 +00009962 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009963 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009964}
9965
Dan Gohmand858e902010-04-17 15:26:15 +00009966SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009967 EVT VT = Op.getValueType();
9968 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009969 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009970 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009971
9972 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009973 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009974 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009975 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009976 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009977 }
Evan Cheng18efe262007-12-14 02:13:44 +00009978
Evan Cheng152804e2007-12-14 08:30:15 +00009979 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009980 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009981 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009982
9983 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009984 SDValue Ops[] = {
9985 Op,
9986 DAG.getConstant(NumBits+NumBits-1, OpVT),
9987 DAG.getConstant(X86::COND_E, MVT::i8),
9988 Op.getValue(1)
9989 };
9990 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009991
9992 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009993 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009994
Owen Anderson825b72b2009-08-11 20:47:22 +00009995 if (VT == MVT::i8)
9996 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009997 return Op;
9998}
9999
Dan Gohmand858e902010-04-17 15:26:15 +000010000SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010001 EVT VT = Op.getValueType();
10002 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010003 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010004 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010005
10006 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010007 if (VT == MVT::i8) {
10008 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010009 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010010 }
Evan Cheng152804e2007-12-14 08:30:15 +000010011
10012 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010013 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010014 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010015
10016 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010017 SDValue Ops[] = {
10018 Op,
10019 DAG.getConstant(NumBits, OpVT),
10020 DAG.getConstant(X86::COND_E, MVT::i8),
10021 Op.getValue(1)
10022 };
10023 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010024
Owen Anderson825b72b2009-08-11 20:47:22 +000010025 if (VT == MVT::i8)
10026 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010027 return Op;
10028}
10029
Craig Topper13894fa2011-08-24 06:14:18 +000010030// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10031// ones, and then concatenate the result back.
10032static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010033 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010034
10035 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
10036 "Unsupported value type for operation");
10037
10038 int NumElems = VT.getVectorNumElements();
10039 DebugLoc dl = Op.getDebugLoc();
10040 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10041 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10042
10043 // Extract the LHS vectors
10044 SDValue LHS = Op.getOperand(0);
10045 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10046 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10047
10048 // Extract the RHS vectors
10049 SDValue RHS = Op.getOperand(1);
10050 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
10051 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
10052
10053 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10054 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10055
10056 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10057 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10058 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10059}
10060
10061SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
10062 assert(Op.getValueType().getSizeInBits() == 256 &&
10063 Op.getValueType().isInteger() &&
10064 "Only handle AVX 256-bit vector integer operation");
10065 return Lower256IntArith(Op, DAG);
10066}
10067
10068SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
10069 assert(Op.getValueType().getSizeInBits() == 256 &&
10070 Op.getValueType().isInteger() &&
10071 "Only handle AVX 256-bit vector integer operation");
10072 return Lower256IntArith(Op, DAG);
10073}
10074
10075SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10076 EVT VT = Op.getValueType();
10077
10078 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +000010079 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010080 return Lower256IntArith(Op, DAG);
10081
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010082 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010083
Craig Topperaaa643c2011-11-09 07:28:55 +000010084 SDValue A = Op.getOperand(0);
10085 SDValue B = Op.getOperand(1);
10086
10087 if (VT == MVT::v4i64) {
10088 assert(Subtarget->hasAVX2() && "Lowering v4i64 multiply requires AVX2");
10089
10090 // ulong2 Ahi = __builtin_ia32_psrlqi256( a, 32);
10091 // ulong2 Bhi = __builtin_ia32_psrlqi256( b, 32);
10092 // ulong2 AloBlo = __builtin_ia32_pmuludq256( a, b );
10093 // ulong2 AloBhi = __builtin_ia32_pmuludq256( a, Bhi );
10094 // ulong2 AhiBlo = __builtin_ia32_pmuludq256( Ahi, b );
10095 //
10096 // AloBhi = __builtin_ia32_psllqi256( AloBhi, 32 );
10097 // AhiBlo = __builtin_ia32_psllqi256( AhiBlo, 32 );
10098 // return AloBlo + AloBhi + AhiBlo;
10099
10100 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10101 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10102 A, DAG.getConstant(32, MVT::i32));
10103 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10104 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10105 B, DAG.getConstant(32, MVT::i32));
10106 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10107 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10108 A, B);
10109 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10110 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10111 A, Bhi);
10112 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10113 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10114 Ahi, B);
10115 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10116 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10117 AloBhi, DAG.getConstant(32, MVT::i32));
10118 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10119 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10120 AhiBlo, DAG.getConstant(32, MVT::i32));
10121 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10122 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
10123 return Res;
10124 }
10125
10126 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
10127
Mon P Wangaf9b9522008-12-18 21:42:19 +000010128 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
10129 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
10130 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
10131 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
10132 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
10133 //
10134 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
10135 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
10136 // return AloBlo + AloBhi + AhiBlo;
10137
Dale Johannesene4d209d2009-02-03 20:21:25 +000010138 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010139 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10140 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010141 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010142 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10143 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010144 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010145 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010146 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010147 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010148 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010149 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010150 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010151 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010152 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010153 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010154 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10155 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010156 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010157 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10158 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010159 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10160 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010161 return Res;
10162}
10163
Nadav Rotem43012222011-05-11 08:12:09 +000010164SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10165
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010166 EVT VT = Op.getValueType();
10167 DebugLoc dl = Op.getDebugLoc();
10168 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010169 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010170 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010171
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010172 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010173 return SDValue();
10174
Nadav Rotem43012222011-05-11 08:12:09 +000010175 // Optimize shl/srl/sra with constant shift amount.
10176 if (isSplatVector(Amt.getNode())) {
10177 SDValue SclrAmt = Amt->getOperand(0);
10178 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10179 uint64_t ShiftAmt = C->getZExtValue();
10180
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010181 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SHL) {
10182 // Make a large shift.
10183 SDValue SHL =
10184 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10185 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10186 R, DAG.getConstant(ShiftAmt, MVT::i32));
10187 // Zero out the rightmost bits.
10188 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10189 MVT::i8));
10190 return DAG.getNode(ISD::AND, dl, VT, SHL,
10191 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10192 }
10193
Nadav Rotem43012222011-05-11 08:12:09 +000010194 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
10195 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10196 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10197 R, DAG.getConstant(ShiftAmt, MVT::i32));
10198
10199 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
10200 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10201 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10202 R, DAG.getConstant(ShiftAmt, MVT::i32));
10203
10204 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
10205 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10206 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10207 R, DAG.getConstant(ShiftAmt, MVT::i32));
10208
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010209 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRL) {
10210 // Make a large shift.
10211 SDValue SRL =
10212 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10213 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10214 R, DAG.getConstant(ShiftAmt, MVT::i32));
10215 // Zero out the leftmost bits.
10216 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10217 MVT::i8));
10218 return DAG.getNode(ISD::AND, dl, VT, SRL,
10219 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10220 }
10221
Nadav Rotem43012222011-05-11 08:12:09 +000010222 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
10223 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10224 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10225 R, DAG.getConstant(ShiftAmt, MVT::i32));
10226
10227 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
10228 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10229 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
10230 R, DAG.getConstant(ShiftAmt, MVT::i32));
10231
10232 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
10233 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10234 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10235 R, DAG.getConstant(ShiftAmt, MVT::i32));
10236
10237 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
10238 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10239 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
10240 R, DAG.getConstant(ShiftAmt, MVT::i32));
10241
10242 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
10243 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10244 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
10245 R, DAG.getConstant(ShiftAmt, MVT::i32));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010246
10247 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRA) {
10248 if (ShiftAmt == 7) {
10249 // R s>> 7 === R s< 0
10250 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10251 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10252 }
10253
10254 // R s>> a === ((R u>> a) ^ m) - m
10255 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10256 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10257 MVT::i8));
10258 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10259 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10260 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10261 return Res;
10262 }
Craig Topper46154eb2011-11-11 07:39:23 +000010263
10264 if (Subtarget->hasAVX2()) {
10265 if (VT == MVT::v4i64 && Op.getOpcode() == ISD::SHL)
10266 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10267 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10268 R, DAG.getConstant(ShiftAmt, MVT::i32));
10269
10270 if (VT == MVT::v8i32 && Op.getOpcode() == ISD::SHL)
10271 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10272 DAG.getConstant(Intrinsic::x86_avx2_pslli_d, MVT::i32),
10273 R, DAG.getConstant(ShiftAmt, MVT::i32));
10274
10275 if (VT == MVT::v16i16 && Op.getOpcode() == ISD::SHL)
10276 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10277 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
10278 R, DAG.getConstant(ShiftAmt, MVT::i32));
10279
10280 if (VT == MVT::v4i64 && Op.getOpcode() == ISD::SRL)
10281 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10282 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10283 R, DAG.getConstant(ShiftAmt, MVT::i32));
10284
10285 if (VT == MVT::v8i32 && Op.getOpcode() == ISD::SRL)
10286 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10287 DAG.getConstant(Intrinsic::x86_avx2_psrli_d, MVT::i32),
10288 R, DAG.getConstant(ShiftAmt, MVT::i32));
10289
10290 if (VT == MVT::v16i16 && Op.getOpcode() == ISD::SRL)
10291 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10292 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
10293 R, DAG.getConstant(ShiftAmt, MVT::i32));
10294
10295 if (VT == MVT::v8i32 && Op.getOpcode() == ISD::SRA)
10296 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10297 DAG.getConstant(Intrinsic::x86_avx2_psrai_d, MVT::i32),
10298 R, DAG.getConstant(ShiftAmt, MVT::i32));
10299
10300 if (VT == MVT::v16i16 && Op.getOpcode() == ISD::SRA)
10301 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10302 DAG.getConstant(Intrinsic::x86_avx2_psrai_w, MVT::i32),
10303 R, DAG.getConstant(ShiftAmt, MVT::i32));
10304 }
Nadav Rotem43012222011-05-11 08:12:09 +000010305 }
10306 }
10307
10308 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010309 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010310 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10311 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10312 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
10313
10314 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010315
Nate Begeman51409212010-07-28 00:21:48 +000010316 std::vector<Constant*> CV(4, CI);
10317 Constant *C = ConstantVector::get(CV);
10318 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10319 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010320 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010321 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010322
10323 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010324 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010325 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10326 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10327 }
Nadav Rotem43012222011-05-11 08:12:09 +000010328 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010329 // a = a << 5;
10330 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10331 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10332 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
10333
10334 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
10335 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
10336
10337 std::vector<Constant*> CVM1(16, CM1);
10338 std::vector<Constant*> CVM2(16, CM2);
10339 Constant *C = ConstantVector::get(CVM1);
10340 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10341 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010342 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010343 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010344
10345 // r = pblendv(r, psllw(r & (char16)15, 4), a);
10346 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10347 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10348 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10349 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010350 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010351 // a += a
10352 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010353
Nate Begeman51409212010-07-28 00:21:48 +000010354 C = ConstantVector::get(CVM2);
10355 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10356 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010357 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010358 false, false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010359
Nate Begeman51409212010-07-28 00:21:48 +000010360 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10361 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10362 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10363 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10364 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010365 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010366 // a += a
10367 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010368
Nate Begeman51409212010-07-28 00:21:48 +000010369 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010370 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10371 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010372 return R;
10373 }
Craig Topper46154eb2011-11-11 07:39:23 +000010374
10375 // Decompose 256-bit shifts into smaller 128-bit shifts.
10376 if (VT.getSizeInBits() == 256) {
10377 int NumElems = VT.getVectorNumElements();
10378 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10379 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10380
10381 // Extract the two vectors
10382 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10383 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10384 DAG, dl);
10385
10386 // Recreate the shift amount vectors
10387 SDValue Amt1, Amt2;
10388 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10389 // Constant shift amount
10390 SmallVector<SDValue, 4> Amt1Csts;
10391 SmallVector<SDValue, 4> Amt2Csts;
10392 for (int i = 0; i < NumElems/2; ++i)
10393 Amt1Csts.push_back(Amt->getOperand(i));
10394 for (int i = NumElems/2; i < NumElems; ++i)
10395 Amt2Csts.push_back(Amt->getOperand(i));
10396
10397 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10398 &Amt1Csts[0], NumElems/2);
10399 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10400 &Amt2Csts[0], NumElems/2);
10401 } else {
10402 // Variable shift amount
10403 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10404 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10405 DAG, dl);
10406 }
10407
10408 // Issue new vector shifts for the smaller types
10409 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10410 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10411
10412 // Concatenate the result back
10413 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10414 }
10415
Nate Begeman51409212010-07-28 00:21:48 +000010416 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010417}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010418
Dan Gohmand858e902010-04-17 15:26:15 +000010419SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010420 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10421 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010422 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10423 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010424 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010425 SDValue LHS = N->getOperand(0);
10426 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010427 unsigned BaseOp = 0;
10428 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010429 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010430 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010431 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010432 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010433 // A subtract of one will be selected as a INC. Note that INC doesn't
10434 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010435 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10436 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010437 BaseOp = X86ISD::INC;
10438 Cond = X86::COND_O;
10439 break;
10440 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010441 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010442 Cond = X86::COND_O;
10443 break;
10444 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010445 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010446 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010447 break;
10448 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010449 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10450 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010451 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10452 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010453 BaseOp = X86ISD::DEC;
10454 Cond = X86::COND_O;
10455 break;
10456 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010457 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010458 Cond = X86::COND_O;
10459 break;
10460 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010461 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010462 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010463 break;
10464 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010465 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010466 Cond = X86::COND_O;
10467 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010468 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10469 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10470 MVT::i32);
10471 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010472
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010473 SDValue SetCC =
10474 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10475 DAG.getConstant(X86::COND_O, MVT::i32),
10476 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010477
Dan Gohman6e5fda22011-07-22 18:45:15 +000010478 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010479 }
Bill Wendling74c37652008-12-09 22:08:41 +000010480 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010481
Bill Wendling61edeb52008-12-02 01:06:39 +000010482 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010483 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010484 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010485
Bill Wendling61edeb52008-12-02 01:06:39 +000010486 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010487 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10488 DAG.getConstant(Cond, MVT::i32),
10489 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010490
Dan Gohman6e5fda22011-07-22 18:45:15 +000010491 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010492}
10493
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010494SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10495 DebugLoc dl = Op.getDebugLoc();
10496 SDNode* Node = Op.getNode();
10497 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
10498 EVT VT = Node->getValueType(0);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010499 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010500 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10501 ExtraVT.getScalarType().getSizeInBits();
10502 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10503
10504 unsigned SHLIntrinsicsID = 0;
10505 unsigned SRAIntrinsicsID = 0;
10506 switch (VT.getSimpleVT().SimpleTy) {
10507 default:
10508 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010509 case MVT::v4i32: {
10510 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10511 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10512 break;
10513 }
10514 case MVT::v8i16: {
10515 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10516 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10517 break;
10518 }
10519 }
10520
10521 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10522 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
10523 Node->getOperand(0), ShAmt);
10524
Nadav Rotema7934dd2011-10-10 19:31:45 +000010525 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10526 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10527 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010528 }
10529
10530 return SDValue();
10531}
10532
10533
Eric Christopher9a9d2752010-07-22 02:48:34 +000010534SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10535 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010536
Eric Christopher77ed1352011-07-08 00:04:56 +000010537 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10538 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010539 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010540 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010541 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010542 SDValue Ops[] = {
10543 DAG.getRegister(X86::ESP, MVT::i32), // Base
10544 DAG.getTargetConstant(1, MVT::i8), // Scale
10545 DAG.getRegister(0, MVT::i32), // Index
10546 DAG.getTargetConstant(0, MVT::i32), // Disp
10547 DAG.getRegister(0, MVT::i32), // Segment.
10548 Zero,
10549 Chain
10550 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010551 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010552 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10553 array_lengthof(Ops));
10554 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010555 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010556
Eric Christopher9a9d2752010-07-22 02:48:34 +000010557 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010558 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010559 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010560
Chris Lattner132929a2010-08-14 17:26:09 +000010561 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10562 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10563 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10564 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010565
Chris Lattner132929a2010-08-14 17:26:09 +000010566 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10567 if (!Op1 && !Op2 && !Op3 && Op4)
10568 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010569
Chris Lattner132929a2010-08-14 17:26:09 +000010570 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10571 if (Op1 && !Op2 && !Op3 && !Op4)
10572 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010573
10574 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010575 // (MFENCE)>;
10576 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010577}
10578
Eli Friedman14648462011-07-27 22:21:52 +000010579SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10580 SelectionDAG &DAG) const {
10581 DebugLoc dl = Op.getDebugLoc();
10582 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10583 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10584 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10585 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10586
10587 // The only fence that needs an instruction is a sequentially-consistent
10588 // cross-thread fence.
10589 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10590 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10591 // no-sse2). There isn't any reason to disable it if the target processor
10592 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010593 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010594 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10595
10596 SDValue Chain = Op.getOperand(0);
10597 SDValue Zero = DAG.getConstant(0, MVT::i32);
10598 SDValue Ops[] = {
10599 DAG.getRegister(X86::ESP, MVT::i32), // Base
10600 DAG.getTargetConstant(1, MVT::i8), // Scale
10601 DAG.getRegister(0, MVT::i32), // Index
10602 DAG.getTargetConstant(0, MVT::i32), // Disp
10603 DAG.getRegister(0, MVT::i32), // Segment.
10604 Zero,
10605 Chain
10606 };
10607 SDNode *Res =
10608 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10609 array_lengthof(Ops));
10610 return SDValue(Res, 0);
10611 }
10612
10613 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10614 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10615}
10616
10617
Dan Gohmand858e902010-04-17 15:26:15 +000010618SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010619 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010620 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010621 unsigned Reg = 0;
10622 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010623 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010624 default:
10625 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010626 case MVT::i8: Reg = X86::AL; size = 1; break;
10627 case MVT::i16: Reg = X86::AX; size = 2; break;
10628 case MVT::i32: Reg = X86::EAX; size = 4; break;
10629 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010630 assert(Subtarget->is64Bit() && "Node not type legal!");
10631 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010632 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010633 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010634 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010635 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010636 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010637 Op.getOperand(1),
10638 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010639 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010640 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010641 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010642 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10643 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10644 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010645 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010646 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010647 return cpOut;
10648}
10649
Duncan Sands1607f052008-12-01 11:39:25 +000010650SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010651 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010652 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010653 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010654 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010655 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010656 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010657 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10658 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010659 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010660 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10661 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010662 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010663 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010664 rdx.getValue(1)
10665 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010666 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010667}
10668
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010669SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010670 SelectionDAG &DAG) const {
10671 EVT SrcVT = Op.getOperand(0).getValueType();
10672 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010673 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010674 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010675 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010676 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010677 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010678 // i64 <=> MMX conversions are Legal.
10679 if (SrcVT==MVT::i64 && DstVT.isVector())
10680 return Op;
10681 if (DstVT==MVT::i64 && SrcVT.isVector())
10682 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010683 // MMX <=> MMX conversions are Legal.
10684 if (SrcVT.isVector() && DstVT.isVector())
10685 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010686 // All other conversions need to be expanded.
10687 return SDValue();
10688}
Chris Lattner5b856542010-12-20 00:59:46 +000010689
Dan Gohmand858e902010-04-17 15:26:15 +000010690SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010691 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010692 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010693 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010694 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010695 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010696 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010697 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010698 Node->getOperand(0),
10699 Node->getOperand(1), negOp,
10700 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010701 cast<AtomicSDNode>(Node)->getAlignment(),
10702 cast<AtomicSDNode>(Node)->getOrdering(),
10703 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010704}
10705
Eli Friedman327236c2011-08-24 20:50:09 +000010706static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10707 SDNode *Node = Op.getNode();
10708 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010709 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010710
10711 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010712 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10713 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10714 // (The only way to get a 16-byte store is cmpxchg16b)
10715 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10716 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10717 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010718 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10719 cast<AtomicSDNode>(Node)->getMemoryVT(),
10720 Node->getOperand(0),
10721 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010722 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010723 cast<AtomicSDNode>(Node)->getOrdering(),
10724 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010725 return Swap.getValue(1);
10726 }
10727 // Other atomic stores have a simple pattern.
10728 return Op;
10729}
10730
Chris Lattner5b856542010-12-20 00:59:46 +000010731static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10732 EVT VT = Op.getNode()->getValueType(0);
10733
10734 // Let legalize expand this if it isn't a legal type yet.
10735 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10736 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010737
Chris Lattner5b856542010-12-20 00:59:46 +000010738 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010739
Chris Lattner5b856542010-12-20 00:59:46 +000010740 unsigned Opc;
10741 bool ExtraOp = false;
10742 switch (Op.getOpcode()) {
10743 default: assert(0 && "Invalid code");
10744 case ISD::ADDC: Opc = X86ISD::ADD; break;
10745 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10746 case ISD::SUBC: Opc = X86ISD::SUB; break;
10747 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10748 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010749
Chris Lattner5b856542010-12-20 00:59:46 +000010750 if (!ExtraOp)
10751 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10752 Op.getOperand(1));
10753 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10754 Op.getOperand(1), Op.getOperand(2));
10755}
10756
Evan Cheng0db9fe62006-04-25 20:13:52 +000010757/// LowerOperation - Provide custom lowering hooks for some operations.
10758///
Dan Gohmand858e902010-04-17 15:26:15 +000010759SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010760 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010761 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010762 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010763 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010764 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010765 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10766 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010767 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010768 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010769 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010770 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10771 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10772 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010773 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010774 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010775 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10776 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10777 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010778 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010779 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010780 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010781 case ISD::SHL_PARTS:
10782 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010783 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010784 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010785 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010786 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010787 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010788 case ISD::FABS: return LowerFABS(Op, DAG);
10789 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010790 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010791 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010792 case ISD::SETCC: return LowerSETCC(Op, DAG);
10793 case ISD::SELECT: return LowerSELECT(Op, DAG);
10794 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010795 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010796 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010797 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010798 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010799 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010800 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10801 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010802 case ISD::FRAME_TO_ARGS_OFFSET:
10803 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010804 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010805 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010806 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10807 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010808 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010809 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10810 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010811 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010812 case ISD::SRA:
10813 case ISD::SRL:
10814 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010815 case ISD::SADDO:
10816 case ISD::UADDO:
10817 case ISD::SSUBO:
10818 case ISD::USUBO:
10819 case ISD::SMULO:
10820 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010821 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010822 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010823 case ISD::ADDC:
10824 case ISD::ADDE:
10825 case ISD::SUBC:
10826 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010827 case ISD::ADD: return LowerADD(Op, DAG);
10828 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010829 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010830}
10831
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010832static void ReplaceATOMIC_LOAD(SDNode *Node,
10833 SmallVectorImpl<SDValue> &Results,
10834 SelectionDAG &DAG) {
10835 DebugLoc dl = Node->getDebugLoc();
10836 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10837
10838 // Convert wide load -> cmpxchg8b/cmpxchg16b
10839 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10840 // (The only way to get a 16-byte load is cmpxchg16b)
10841 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010842 SDValue Zero = DAG.getConstant(0, VT);
10843 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010844 Node->getOperand(0),
10845 Node->getOperand(1), Zero, Zero,
10846 cast<AtomicSDNode>(Node)->getMemOperand(),
10847 cast<AtomicSDNode>(Node)->getOrdering(),
10848 cast<AtomicSDNode>(Node)->getSynchScope());
10849 Results.push_back(Swap.getValue(0));
10850 Results.push_back(Swap.getValue(1));
10851}
10852
Duncan Sands1607f052008-12-01 11:39:25 +000010853void X86TargetLowering::
10854ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010855 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010856 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000010857 assert (Node->getValueType(0) == MVT::i64 &&
10858 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010859
10860 SDValue Chain = Node->getOperand(0);
10861 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010862 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010863 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010864 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010865 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010866 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010867 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010868 SDValue Result =
10869 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10870 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010871 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010872 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010873 Results.push_back(Result.getValue(2));
10874}
10875
Duncan Sands126d9072008-07-04 11:47:58 +000010876/// ReplaceNodeResults - Replace a node with an illegal result type
10877/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010878void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10879 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010880 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010881 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010882 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010883 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010884 assert(false && "Do not know how to custom type legalize this operation!");
10885 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010886 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010887 case ISD::ADDC:
10888 case ISD::ADDE:
10889 case ISD::SUBC:
10890 case ISD::SUBE:
10891 // We don't want to expand or promote these.
10892 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010893 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010894 std::pair<SDValue,SDValue> Vals =
10895 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010896 SDValue FIST = Vals.first, StackSlot = Vals.second;
10897 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010898 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010899 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010900 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010901 MachinePointerInfo(),
10902 false, false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010903 }
10904 return;
10905 }
10906 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010907 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010908 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010909 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010910 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010911 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010912 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010913 eax.getValue(2));
10914 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10915 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010916 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010917 Results.push_back(edx.getValue(1));
10918 return;
10919 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010920 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010921 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010922 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010923 bool Regs64bit = T == MVT::i128;
10924 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010925 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010926 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10927 DAG.getConstant(0, HalfT));
10928 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10929 DAG.getConstant(1, HalfT));
10930 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10931 Regs64bit ? X86::RAX : X86::EAX,
10932 cpInL, SDValue());
10933 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10934 Regs64bit ? X86::RDX : X86::EDX,
10935 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010936 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010937 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10938 DAG.getConstant(0, HalfT));
10939 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10940 DAG.getConstant(1, HalfT));
10941 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10942 Regs64bit ? X86::RBX : X86::EBX,
10943 swapInL, cpInH.getValue(1));
10944 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10945 Regs64bit ? X86::RCX : X86::ECX,
10946 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010947 SDValue Ops[] = { swapInH.getValue(0),
10948 N->getOperand(1),
10949 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010950 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010951 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010952 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10953 X86ISD::LCMPXCHG8_DAG;
10954 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010955 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010956 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10957 Regs64bit ? X86::RAX : X86::EAX,
10958 HalfT, Result.getValue(1));
10959 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10960 Regs64bit ? X86::RDX : X86::EDX,
10961 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010962 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010963 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010964 Results.push_back(cpOutH.getValue(1));
10965 return;
10966 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010967 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010968 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10969 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010970 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010971 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10972 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010973 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010974 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10975 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010976 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010977 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10978 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010979 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010980 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10981 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010982 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010983 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10984 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010985 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010986 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10987 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010988 case ISD::ATOMIC_LOAD:
10989 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010990 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010991}
10992
Evan Cheng72261582005-12-20 06:22:03 +000010993const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10994 switch (Opcode) {
10995 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010996 case X86ISD::BSF: return "X86ISD::BSF";
10997 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010998 case X86ISD::SHLD: return "X86ISD::SHLD";
10999 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011000 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011001 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011002 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011003 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011004 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011005 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011006 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11007 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11008 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011009 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011010 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011011 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011012 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011013 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011014 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011015 case X86ISD::COMI: return "X86ISD::COMI";
11016 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011017 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011018 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011019 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11020 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011021 case X86ISD::CMOV: return "X86ISD::CMOV";
11022 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011023 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011024 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11025 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011026 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011027 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011028 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011029 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011030 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011031 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11032 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011033 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011034 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011035 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000011036 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
11037 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
11038 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Craig Toppere6a62772011-11-13 17:31:07 +000011039 case X86ISD::BLENDV: return "X86ISD::BLENDV";
11040 case X86ISD::FHADD: return "X86ISD::FHADD";
11041 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011042 case X86ISD::FMAX: return "X86ISD::FMAX";
11043 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000011044 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11045 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011046 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011047 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011048 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011049 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011050 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011051 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11052 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011053 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11054 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11055 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11056 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11057 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11058 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011059 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
11060 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000011061 case X86ISD::VSHL: return "X86ISD::VSHL";
11062 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000011063 case X86ISD::CMPPD: return "X86ISD::CMPPD";
11064 case X86ISD::CMPPS: return "X86ISD::CMPPS";
11065 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
11066 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
11067 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
11068 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
11069 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
11070 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
11071 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
11072 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011073 case X86ISD::ADD: return "X86ISD::ADD";
11074 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011075 case X86ISD::ADC: return "X86ISD::ADC";
11076 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011077 case X86ISD::SMUL: return "X86ISD::SMUL";
11078 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011079 case X86ISD::INC: return "X86ISD::INC";
11080 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011081 case X86ISD::OR: return "X86ISD::OR";
11082 case X86ISD::XOR: return "X86ISD::XOR";
11083 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011084 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011085 case X86ISD::BLSI: return "X86ISD::BLSI";
11086 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11087 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011088 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011089 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011090 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011091 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11092 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11093 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
11094 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
11095 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
11096 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
11097 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
11098 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
11099 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011100 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011101 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011102 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011103 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11104 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011105 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11106 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11107 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
11108 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
11109 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
11110 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11111 case X86ISD::MOVSS: return "X86ISD::MOVSS";
11112 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
11113 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000011114 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011115 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
11116 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
11117 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
11118 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
11119 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
11120 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
11121 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
11122 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
11123 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
11124 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011125 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000011126 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
11127 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
11128 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
11129 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000011130 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011131 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011132 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011133 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011134 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011135 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000011136 }
11137}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011138
Chris Lattnerc9addb72007-03-30 23:15:24 +000011139// isLegalAddressingMode - Return true if the addressing mode represented
11140// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011141bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011142 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011143 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011144 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011145 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011146
Chris Lattnerc9addb72007-03-30 23:15:24 +000011147 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011148 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011149 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011150
Chris Lattnerc9addb72007-03-30 23:15:24 +000011151 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011152 unsigned GVFlags =
11153 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011154
Chris Lattnerdfed4132009-07-10 07:38:24 +000011155 // If a reference to this global requires an extra load, we can't fold it.
11156 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011157 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011158
Chris Lattnerdfed4132009-07-10 07:38:24 +000011159 // If BaseGV requires a register for the PIC base, we cannot also have a
11160 // BaseReg specified.
11161 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011162 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011163
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011164 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011165 if ((M != CodeModel::Small || R != Reloc::Static) &&
11166 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011167 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011168 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011169
Chris Lattnerc9addb72007-03-30 23:15:24 +000011170 switch (AM.Scale) {
11171 case 0:
11172 case 1:
11173 case 2:
11174 case 4:
11175 case 8:
11176 // These scales always work.
11177 break;
11178 case 3:
11179 case 5:
11180 case 9:
11181 // These scales are formed with basereg+scalereg. Only accept if there is
11182 // no basereg yet.
11183 if (AM.HasBaseReg)
11184 return false;
11185 break;
11186 default: // Other stuff never works.
11187 return false;
11188 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011189
Chris Lattnerc9addb72007-03-30 23:15:24 +000011190 return true;
11191}
11192
11193
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011194bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011195 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011196 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011197 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11198 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011199 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011200 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011201 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011202}
11203
Owen Andersone50ed302009-08-10 22:56:29 +000011204bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011205 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011206 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011207 unsigned NumBits1 = VT1.getSizeInBits();
11208 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011209 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011210 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011211 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011212}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011213
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011214bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011215 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011216 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011217}
11218
Owen Andersone50ed302009-08-10 22:56:29 +000011219bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011220 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011221 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011222}
11223
Owen Andersone50ed302009-08-10 22:56:29 +000011224bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011225 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011226 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011227}
11228
Evan Cheng60c07e12006-07-05 22:17:51 +000011229/// isShuffleMaskLegal - Targets can use this to indicate that they only
11230/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11231/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11232/// are assumed to be legal.
11233bool
Eric Christopherfd179292009-08-27 18:07:15 +000011234X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011235 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011236 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011237 if (VT.getSizeInBits() == 64)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000011238 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX());
Nate Begeman9008ca62009-04-27 18:41:29 +000011239
Nate Begemana09008b2009-10-19 02:17:23 +000011240 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011241 return (VT.getVectorNumElements() == 2 ||
11242 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11243 isMOVLMask(M, VT) ||
11244 isSHUFPMask(M, VT) ||
11245 isPSHUFDMask(M, VT) ||
11246 isPSHUFHWMask(M, VT) ||
11247 isPSHUFLWMask(M, VT) ||
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000011248 isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011249 isUNPCKLMask(M, VT) ||
11250 isUNPCKHMask(M, VT) ||
11251 isUNPCKL_v_undef_Mask(M, VT) ||
11252 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011253}
11254
Dan Gohman7d8143f2008-04-09 20:09:42 +000011255bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011256X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011257 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011258 unsigned NumElts = VT.getVectorNumElements();
11259 // FIXME: This collection of masks seems suspect.
11260 if (NumElts == 2)
11261 return true;
11262 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11263 return (isMOVLMask(Mask, VT) ||
11264 isCommutedMOVLMask(Mask, VT, true) ||
11265 isSHUFPMask(Mask, VT) ||
11266 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011267 }
11268 return false;
11269}
11270
11271//===----------------------------------------------------------------------===//
11272// X86 Scheduler Hooks
11273//===----------------------------------------------------------------------===//
11274
Mon P Wang63307c32008-05-05 19:05:59 +000011275// private utility function
11276MachineBasicBlock *
11277X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11278 MachineBasicBlock *MBB,
11279 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011280 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011281 unsigned LoadOpc,
11282 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011283 unsigned notOpc,
11284 unsigned EAXreg,
11285 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011286 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011287 // For the atomic bitwise operator, we generate
11288 // thisMBB:
11289 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011290 // ld t1 = [bitinstr.addr]
11291 // op t2 = t1, [bitinstr.val]
11292 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011293 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11294 // bz newMBB
11295 // fallthrough -->nextMBB
11296 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11297 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011298 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011299 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011300
Mon P Wang63307c32008-05-05 19:05:59 +000011301 /// First build the CFG
11302 MachineFunction *F = MBB->getParent();
11303 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011304 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11305 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11306 F->insert(MBBIter, newMBB);
11307 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011308
Dan Gohman14152b42010-07-06 20:24:04 +000011309 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11310 nextMBB->splice(nextMBB->begin(), thisMBB,
11311 llvm::next(MachineBasicBlock::iterator(bInstr)),
11312 thisMBB->end());
11313 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011314
Mon P Wang63307c32008-05-05 19:05:59 +000011315 // Update thisMBB to fall through to newMBB
11316 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011317
Mon P Wang63307c32008-05-05 19:05:59 +000011318 // newMBB jumps to itself and fall through to nextMBB
11319 newMBB->addSuccessor(nextMBB);
11320 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011321
Mon P Wang63307c32008-05-05 19:05:59 +000011322 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011323 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011324 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011325 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011326 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011327 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011328 int numArgs = bInstr->getNumOperands() - 1;
11329 for (int i=0; i < numArgs; ++i)
11330 argOpers[i] = &bInstr->getOperand(i+1);
11331
11332 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011333 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011334 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011335
Dale Johannesen140be2d2008-08-19 18:47:28 +000011336 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011337 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011338 for (int i=0; i <= lastAddrIndx; ++i)
11339 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011340
Dale Johannesen140be2d2008-08-19 18:47:28 +000011341 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011342 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011343 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011344 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011345 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011346 tt = t1;
11347
Dale Johannesen140be2d2008-08-19 18:47:28 +000011348 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011349 assert((argOpers[valArgIndx]->isReg() ||
11350 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011351 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011352 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011353 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011354 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011355 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011356 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011357 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011358
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011359 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011360 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011361
Dale Johannesene4d209d2009-02-03 20:21:25 +000011362 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011363 for (int i=0; i <= lastAddrIndx; ++i)
11364 (*MIB).addOperand(*argOpers[i]);
11365 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011366 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011367 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11368 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011369
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011370 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011371 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011372
Mon P Wang63307c32008-05-05 19:05:59 +000011373 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011374 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011375
Dan Gohman14152b42010-07-06 20:24:04 +000011376 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011377 return nextMBB;
11378}
11379
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011380// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011381MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011382X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11383 MachineBasicBlock *MBB,
11384 unsigned regOpcL,
11385 unsigned regOpcH,
11386 unsigned immOpcL,
11387 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011388 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011389 // For the atomic bitwise operator, we generate
11390 // thisMBB (instructions are in pairs, except cmpxchg8b)
11391 // ld t1,t2 = [bitinstr.addr]
11392 // newMBB:
11393 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11394 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011395 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011396 // mov ECX, EBX <- t5, t6
11397 // mov EAX, EDX <- t1, t2
11398 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11399 // mov t3, t4 <- EAX, EDX
11400 // bz newMBB
11401 // result in out1, out2
11402 // fallthrough -->nextMBB
11403
11404 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11405 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011406 const unsigned NotOpc = X86::NOT32r;
11407 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11408 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11409 MachineFunction::iterator MBBIter = MBB;
11410 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011411
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011412 /// First build the CFG
11413 MachineFunction *F = MBB->getParent();
11414 MachineBasicBlock *thisMBB = MBB;
11415 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11416 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11417 F->insert(MBBIter, newMBB);
11418 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011419
Dan Gohman14152b42010-07-06 20:24:04 +000011420 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11421 nextMBB->splice(nextMBB->begin(), thisMBB,
11422 llvm::next(MachineBasicBlock::iterator(bInstr)),
11423 thisMBB->end());
11424 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011425
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011426 // Update thisMBB to fall through to newMBB
11427 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011428
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011429 // newMBB jumps to itself and fall through to nextMBB
11430 newMBB->addSuccessor(nextMBB);
11431 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011432
Dale Johannesene4d209d2009-02-03 20:21:25 +000011433 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011434 // Insert instructions into newMBB based on incoming instruction
11435 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011436 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011437 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011438 MachineOperand& dest1Oper = bInstr->getOperand(0);
11439 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011440 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11441 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011442 argOpers[i] = &bInstr->getOperand(i+2);
11443
Dan Gohman71ea4e52010-05-14 21:01:44 +000011444 // We use some of the operands multiple times, so conservatively just
11445 // clear any kill flags that might be present.
11446 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11447 argOpers[i]->setIsKill(false);
11448 }
11449
Evan Chengad5b52f2010-01-08 19:14:57 +000011450 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011451 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011452
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011453 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011454 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011455 for (int i=0; i <= lastAddrIndx; ++i)
11456 (*MIB).addOperand(*argOpers[i]);
11457 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011458 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011459 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011460 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011461 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011462 MachineOperand newOp3 = *(argOpers[3]);
11463 if (newOp3.isImm())
11464 newOp3.setImm(newOp3.getImm()+4);
11465 else
11466 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011467 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011468 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011469
11470 // t3/4 are defined later, at the bottom of the loop
11471 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11472 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011473 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011474 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011475 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011476 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11477
Evan Cheng306b4ca2010-01-08 23:41:50 +000011478 // The subsequent operations should be using the destination registers of
11479 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011480 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011481 t1 = F->getRegInfo().createVirtualRegister(RC);
11482 t2 = F->getRegInfo().createVirtualRegister(RC);
11483 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11484 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011485 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011486 t1 = dest1Oper.getReg();
11487 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011488 }
11489
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011490 int valArgIndx = lastAddrIndx + 1;
11491 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011492 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011493 "invalid operand");
11494 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11495 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011496 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011497 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011498 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011499 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011500 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011501 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011502 (*MIB).addOperand(*argOpers[valArgIndx]);
11503 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011504 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011505 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011506 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011507 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011508 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011509 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011510 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011511 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011512 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011513 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011514
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011515 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011516 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011517 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011518 MIB.addReg(t2);
11519
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011520 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011521 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011522 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011523 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011524
Dale Johannesene4d209d2009-02-03 20:21:25 +000011525 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011526 for (int i=0; i <= lastAddrIndx; ++i)
11527 (*MIB).addOperand(*argOpers[i]);
11528
11529 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011530 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11531 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011532
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011533 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011534 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011535 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011536 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011537
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011538 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011539 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011540
Dan Gohman14152b42010-07-06 20:24:04 +000011541 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011542 return nextMBB;
11543}
11544
11545// private utility function
11546MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011547X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11548 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011549 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011550 // For the atomic min/max operator, we generate
11551 // thisMBB:
11552 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011553 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011554 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011555 // cmp t1, t2
11556 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011557 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011558 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11559 // bz newMBB
11560 // fallthrough -->nextMBB
11561 //
11562 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11563 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011564 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011565 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011566
Mon P Wang63307c32008-05-05 19:05:59 +000011567 /// First build the CFG
11568 MachineFunction *F = MBB->getParent();
11569 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011570 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11571 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11572 F->insert(MBBIter, newMBB);
11573 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011574
Dan Gohman14152b42010-07-06 20:24:04 +000011575 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11576 nextMBB->splice(nextMBB->begin(), thisMBB,
11577 llvm::next(MachineBasicBlock::iterator(mInstr)),
11578 thisMBB->end());
11579 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011580
Mon P Wang63307c32008-05-05 19:05:59 +000011581 // Update thisMBB to fall through to newMBB
11582 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011583
Mon P Wang63307c32008-05-05 19:05:59 +000011584 // newMBB jumps to newMBB and fall through to nextMBB
11585 newMBB->addSuccessor(nextMBB);
11586 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011587
Dale Johannesene4d209d2009-02-03 20:21:25 +000011588 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011589 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011590 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011591 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011592 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011593 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011594 int numArgs = mInstr->getNumOperands() - 1;
11595 for (int i=0; i < numArgs; ++i)
11596 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011597
Mon P Wang63307c32008-05-05 19:05:59 +000011598 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011599 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011600 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011601
Mon P Wangab3e7472008-05-05 22:56:23 +000011602 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011603 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011604 for (int i=0; i <= lastAddrIndx; ++i)
11605 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011606
Mon P Wang63307c32008-05-05 19:05:59 +000011607 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011608 assert((argOpers[valArgIndx]->isReg() ||
11609 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011610 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011611
11612 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011613 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011614 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011615 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011616 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011617 (*MIB).addOperand(*argOpers[valArgIndx]);
11618
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011619 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011620 MIB.addReg(t1);
11621
Dale Johannesene4d209d2009-02-03 20:21:25 +000011622 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011623 MIB.addReg(t1);
11624 MIB.addReg(t2);
11625
11626 // Generate movc
11627 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011628 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011629 MIB.addReg(t2);
11630 MIB.addReg(t1);
11631
11632 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011633 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011634 for (int i=0; i <= lastAddrIndx; ++i)
11635 (*MIB).addOperand(*argOpers[i]);
11636 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011637 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011638 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11639 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011640
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011641 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011642 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011643
Mon P Wang63307c32008-05-05 19:05:59 +000011644 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011645 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011646
Dan Gohman14152b42010-07-06 20:24:04 +000011647 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011648 return nextMBB;
11649}
11650
Eric Christopherf83a5de2009-08-27 18:08:16 +000011651// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011652// or XMM0_V32I8 in AVX all of this code can be replaced with that
11653// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011654MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011655X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011656 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011657 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11658 "Target must have SSE4.2 or AVX features enabled");
11659
Eric Christopherb120ab42009-08-18 22:50:32 +000011660 DebugLoc dl = MI->getDebugLoc();
11661 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011662 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011663 if (!Subtarget->hasAVX()) {
11664 if (memArg)
11665 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11666 else
11667 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11668 } else {
11669 if (memArg)
11670 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11671 else
11672 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11673 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011674
Eric Christopher41c902f2010-11-30 08:20:21 +000011675 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011676 for (unsigned i = 0; i < numArgs; ++i) {
11677 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011678 if (!(Op.isReg() && Op.isImplicit()))
11679 MIB.addOperand(Op);
11680 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011681 BuildMI(*BB, MI, dl,
11682 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11683 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011684 .addReg(X86::XMM0);
11685
Dan Gohman14152b42010-07-06 20:24:04 +000011686 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011687 return BB;
11688}
11689
11690MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011691X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011692 DebugLoc dl = MI->getDebugLoc();
11693 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011694
Eric Christopher228232b2010-11-30 07:20:12 +000011695 // Address into RAX/EAX, other two args into ECX, EDX.
11696 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11697 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11698 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11699 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011700 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011701
Eric Christopher228232b2010-11-30 07:20:12 +000011702 unsigned ValOps = X86::AddrNumOperands;
11703 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11704 .addReg(MI->getOperand(ValOps).getReg());
11705 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11706 .addReg(MI->getOperand(ValOps+1).getReg());
11707
11708 // The instruction doesn't actually take any operands though.
11709 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011710
Eric Christopher228232b2010-11-30 07:20:12 +000011711 MI->eraseFromParent(); // The pseudo is gone now.
11712 return BB;
11713}
11714
11715MachineBasicBlock *
11716X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011717 DebugLoc dl = MI->getDebugLoc();
11718 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011719
Eric Christopher228232b2010-11-30 07:20:12 +000011720 // First arg in ECX, the second in EAX.
11721 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11722 .addReg(MI->getOperand(0).getReg());
11723 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11724 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011725
Eric Christopher228232b2010-11-30 07:20:12 +000011726 // The instruction doesn't actually take any operands though.
11727 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011728
Eric Christopher228232b2010-11-30 07:20:12 +000011729 MI->eraseFromParent(); // The pseudo is gone now.
11730 return BB;
11731}
11732
11733MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011734X86TargetLowering::EmitVAARG64WithCustomInserter(
11735 MachineInstr *MI,
11736 MachineBasicBlock *MBB) const {
11737 // Emit va_arg instruction on X86-64.
11738
11739 // Operands to this pseudo-instruction:
11740 // 0 ) Output : destination address (reg)
11741 // 1-5) Input : va_list address (addr, i64mem)
11742 // 6 ) ArgSize : Size (in bytes) of vararg type
11743 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11744 // 8 ) Align : Alignment of type
11745 // 9 ) EFLAGS (implicit-def)
11746
11747 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11748 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11749
11750 unsigned DestReg = MI->getOperand(0).getReg();
11751 MachineOperand &Base = MI->getOperand(1);
11752 MachineOperand &Scale = MI->getOperand(2);
11753 MachineOperand &Index = MI->getOperand(3);
11754 MachineOperand &Disp = MI->getOperand(4);
11755 MachineOperand &Segment = MI->getOperand(5);
11756 unsigned ArgSize = MI->getOperand(6).getImm();
11757 unsigned ArgMode = MI->getOperand(7).getImm();
11758 unsigned Align = MI->getOperand(8).getImm();
11759
11760 // Memory Reference
11761 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11762 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11763 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11764
11765 // Machine Information
11766 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11767 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11768 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11769 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11770 DebugLoc DL = MI->getDebugLoc();
11771
11772 // struct va_list {
11773 // i32 gp_offset
11774 // i32 fp_offset
11775 // i64 overflow_area (address)
11776 // i64 reg_save_area (address)
11777 // }
11778 // sizeof(va_list) = 24
11779 // alignment(va_list) = 8
11780
11781 unsigned TotalNumIntRegs = 6;
11782 unsigned TotalNumXMMRegs = 8;
11783 bool UseGPOffset = (ArgMode == 1);
11784 bool UseFPOffset = (ArgMode == 2);
11785 unsigned MaxOffset = TotalNumIntRegs * 8 +
11786 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11787
11788 /* Align ArgSize to a multiple of 8 */
11789 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11790 bool NeedsAlign = (Align > 8);
11791
11792 MachineBasicBlock *thisMBB = MBB;
11793 MachineBasicBlock *overflowMBB;
11794 MachineBasicBlock *offsetMBB;
11795 MachineBasicBlock *endMBB;
11796
11797 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11798 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11799 unsigned OffsetReg = 0;
11800
11801 if (!UseGPOffset && !UseFPOffset) {
11802 // If we only pull from the overflow region, we don't create a branch.
11803 // We don't need to alter control flow.
11804 OffsetDestReg = 0; // unused
11805 OverflowDestReg = DestReg;
11806
11807 offsetMBB = NULL;
11808 overflowMBB = thisMBB;
11809 endMBB = thisMBB;
11810 } else {
11811 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11812 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11813 // If not, pull from overflow_area. (branch to overflowMBB)
11814 //
11815 // thisMBB
11816 // | .
11817 // | .
11818 // offsetMBB overflowMBB
11819 // | .
11820 // | .
11821 // endMBB
11822
11823 // Registers for the PHI in endMBB
11824 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11825 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11826
11827 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11828 MachineFunction *MF = MBB->getParent();
11829 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11830 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11831 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11832
11833 MachineFunction::iterator MBBIter = MBB;
11834 ++MBBIter;
11835
11836 // Insert the new basic blocks
11837 MF->insert(MBBIter, offsetMBB);
11838 MF->insert(MBBIter, overflowMBB);
11839 MF->insert(MBBIter, endMBB);
11840
11841 // Transfer the remainder of MBB and its successor edges to endMBB.
11842 endMBB->splice(endMBB->begin(), thisMBB,
11843 llvm::next(MachineBasicBlock::iterator(MI)),
11844 thisMBB->end());
11845 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11846
11847 // Make offsetMBB and overflowMBB successors of thisMBB
11848 thisMBB->addSuccessor(offsetMBB);
11849 thisMBB->addSuccessor(overflowMBB);
11850
11851 // endMBB is a successor of both offsetMBB and overflowMBB
11852 offsetMBB->addSuccessor(endMBB);
11853 overflowMBB->addSuccessor(endMBB);
11854
11855 // Load the offset value into a register
11856 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11857 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11858 .addOperand(Base)
11859 .addOperand(Scale)
11860 .addOperand(Index)
11861 .addDisp(Disp, UseFPOffset ? 4 : 0)
11862 .addOperand(Segment)
11863 .setMemRefs(MMOBegin, MMOEnd);
11864
11865 // Check if there is enough room left to pull this argument.
11866 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11867 .addReg(OffsetReg)
11868 .addImm(MaxOffset + 8 - ArgSizeA8);
11869
11870 // Branch to "overflowMBB" if offset >= max
11871 // Fall through to "offsetMBB" otherwise
11872 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11873 .addMBB(overflowMBB);
11874 }
11875
11876 // In offsetMBB, emit code to use the reg_save_area.
11877 if (offsetMBB) {
11878 assert(OffsetReg != 0);
11879
11880 // Read the reg_save_area address.
11881 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11882 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11883 .addOperand(Base)
11884 .addOperand(Scale)
11885 .addOperand(Index)
11886 .addDisp(Disp, 16)
11887 .addOperand(Segment)
11888 .setMemRefs(MMOBegin, MMOEnd);
11889
11890 // Zero-extend the offset
11891 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11892 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11893 .addImm(0)
11894 .addReg(OffsetReg)
11895 .addImm(X86::sub_32bit);
11896
11897 // Add the offset to the reg_save_area to get the final address.
11898 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11899 .addReg(OffsetReg64)
11900 .addReg(RegSaveReg);
11901
11902 // Compute the offset for the next argument
11903 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11904 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11905 .addReg(OffsetReg)
11906 .addImm(UseFPOffset ? 16 : 8);
11907
11908 // Store it back into the va_list.
11909 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11910 .addOperand(Base)
11911 .addOperand(Scale)
11912 .addOperand(Index)
11913 .addDisp(Disp, UseFPOffset ? 4 : 0)
11914 .addOperand(Segment)
11915 .addReg(NextOffsetReg)
11916 .setMemRefs(MMOBegin, MMOEnd);
11917
11918 // Jump to endMBB
11919 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11920 .addMBB(endMBB);
11921 }
11922
11923 //
11924 // Emit code to use overflow area
11925 //
11926
11927 // Load the overflow_area address into a register.
11928 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11929 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11930 .addOperand(Base)
11931 .addOperand(Scale)
11932 .addOperand(Index)
11933 .addDisp(Disp, 8)
11934 .addOperand(Segment)
11935 .setMemRefs(MMOBegin, MMOEnd);
11936
11937 // If we need to align it, do so. Otherwise, just copy the address
11938 // to OverflowDestReg.
11939 if (NeedsAlign) {
11940 // Align the overflow address
11941 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11942 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11943
11944 // aligned_addr = (addr + (align-1)) & ~(align-1)
11945 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11946 .addReg(OverflowAddrReg)
11947 .addImm(Align-1);
11948
11949 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11950 .addReg(TmpReg)
11951 .addImm(~(uint64_t)(Align-1));
11952 } else {
11953 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11954 .addReg(OverflowAddrReg);
11955 }
11956
11957 // Compute the next overflow address after this argument.
11958 // (the overflow address should be kept 8-byte aligned)
11959 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11960 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11961 .addReg(OverflowDestReg)
11962 .addImm(ArgSizeA8);
11963
11964 // Store the new overflow address.
11965 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11966 .addOperand(Base)
11967 .addOperand(Scale)
11968 .addOperand(Index)
11969 .addDisp(Disp, 8)
11970 .addOperand(Segment)
11971 .addReg(NextAddrReg)
11972 .setMemRefs(MMOBegin, MMOEnd);
11973
11974 // If we branched, emit the PHI to the front of endMBB.
11975 if (offsetMBB) {
11976 BuildMI(*endMBB, endMBB->begin(), DL,
11977 TII->get(X86::PHI), DestReg)
11978 .addReg(OffsetDestReg).addMBB(offsetMBB)
11979 .addReg(OverflowDestReg).addMBB(overflowMBB);
11980 }
11981
11982 // Erase the pseudo instruction
11983 MI->eraseFromParent();
11984
11985 return endMBB;
11986}
11987
11988MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011989X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11990 MachineInstr *MI,
11991 MachineBasicBlock *MBB) const {
11992 // Emit code to save XMM registers to the stack. The ABI says that the
11993 // number of registers to save is given in %al, so it's theoretically
11994 // possible to do an indirect jump trick to avoid saving all of them,
11995 // however this code takes a simpler approach and just executes all
11996 // of the stores if %al is non-zero. It's less code, and it's probably
11997 // easier on the hardware branch predictor, and stores aren't all that
11998 // expensive anyway.
11999
12000 // Create the new basic blocks. One block contains all the XMM stores,
12001 // and one block is the final destination regardless of whether any
12002 // stores were performed.
12003 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12004 MachineFunction *F = MBB->getParent();
12005 MachineFunction::iterator MBBIter = MBB;
12006 ++MBBIter;
12007 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12008 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12009 F->insert(MBBIter, XMMSaveMBB);
12010 F->insert(MBBIter, EndMBB);
12011
Dan Gohman14152b42010-07-06 20:24:04 +000012012 // Transfer the remainder of MBB and its successor edges to EndMBB.
12013 EndMBB->splice(EndMBB->begin(), MBB,
12014 llvm::next(MachineBasicBlock::iterator(MI)),
12015 MBB->end());
12016 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12017
Dan Gohmand6708ea2009-08-15 01:38:56 +000012018 // The original block will now fall through to the XMM save block.
12019 MBB->addSuccessor(XMMSaveMBB);
12020 // The XMMSaveMBB will fall through to the end block.
12021 XMMSaveMBB->addSuccessor(EndMBB);
12022
12023 // Now add the instructions.
12024 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12025 DebugLoc DL = MI->getDebugLoc();
12026
12027 unsigned CountReg = MI->getOperand(0).getReg();
12028 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12029 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12030
12031 if (!Subtarget->isTargetWin64()) {
12032 // If %al is 0, branch around the XMM save block.
12033 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012034 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012035 MBB->addSuccessor(EndMBB);
12036 }
12037
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012038 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012039 // In the XMM save block, save all the XMM argument registers.
12040 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12041 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012042 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012043 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012044 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012045 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012046 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012047 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012048 .addFrameIndex(RegSaveFrameIndex)
12049 .addImm(/*Scale=*/1)
12050 .addReg(/*IndexReg=*/0)
12051 .addImm(/*Disp=*/Offset)
12052 .addReg(/*Segment=*/0)
12053 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012054 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012055 }
12056
Dan Gohman14152b42010-07-06 20:24:04 +000012057 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012058
12059 return EndMBB;
12060}
Mon P Wang63307c32008-05-05 19:05:59 +000012061
Evan Cheng60c07e12006-07-05 22:17:51 +000012062MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012063X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012064 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012065 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12066 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012067
Chris Lattner52600972009-09-02 05:57:00 +000012068 // To "insert" a SELECT_CC instruction, we actually have to insert the
12069 // diamond control-flow pattern. The incoming instruction knows the
12070 // destination vreg to set, the condition code register to branch on, the
12071 // true/false values to select between, and a branch opcode to use.
12072 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12073 MachineFunction::iterator It = BB;
12074 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012075
Chris Lattner52600972009-09-02 05:57:00 +000012076 // thisMBB:
12077 // ...
12078 // TrueVal = ...
12079 // cmpTY ccX, r1, r2
12080 // bCC copy1MBB
12081 // fallthrough --> copy0MBB
12082 MachineBasicBlock *thisMBB = BB;
12083 MachineFunction *F = BB->getParent();
12084 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12085 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012086 F->insert(It, copy0MBB);
12087 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012088
Bill Wendling730c07e2010-06-25 20:48:10 +000012089 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12090 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000012091 if (!MI->killsRegister(X86::EFLAGS)) {
12092 copy0MBB->addLiveIn(X86::EFLAGS);
12093 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012094 }
12095
Dan Gohman14152b42010-07-06 20:24:04 +000012096 // Transfer the remainder of BB and its successor edges to sinkMBB.
12097 sinkMBB->splice(sinkMBB->begin(), BB,
12098 llvm::next(MachineBasicBlock::iterator(MI)),
12099 BB->end());
12100 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12101
12102 // Add the true and fallthrough blocks as its successors.
12103 BB->addSuccessor(copy0MBB);
12104 BB->addSuccessor(sinkMBB);
12105
12106 // Create the conditional branch instruction.
12107 unsigned Opc =
12108 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12109 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12110
Chris Lattner52600972009-09-02 05:57:00 +000012111 // copy0MBB:
12112 // %FalseValue = ...
12113 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012114 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012115
Chris Lattner52600972009-09-02 05:57:00 +000012116 // sinkMBB:
12117 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12118 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012119 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12120 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012121 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12122 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12123
Dan Gohman14152b42010-07-06 20:24:04 +000012124 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012125 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012126}
12127
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012128MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012129X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12130 bool Is64Bit) const {
12131 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12132 DebugLoc DL = MI->getDebugLoc();
12133 MachineFunction *MF = BB->getParent();
12134 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12135
12136 assert(EnableSegmentedStacks);
12137
12138 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12139 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12140
12141 // BB:
12142 // ... [Till the alloca]
12143 // If stacklet is not large enough, jump to mallocMBB
12144 //
12145 // bumpMBB:
12146 // Allocate by subtracting from RSP
12147 // Jump to continueMBB
12148 //
12149 // mallocMBB:
12150 // Allocate by call to runtime
12151 //
12152 // continueMBB:
12153 // ...
12154 // [rest of original BB]
12155 //
12156
12157 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12158 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12159 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12160
12161 MachineRegisterInfo &MRI = MF->getRegInfo();
12162 const TargetRegisterClass *AddrRegClass =
12163 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12164
12165 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12166 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12167 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012168 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012169 sizeVReg = MI->getOperand(1).getReg(),
12170 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12171
12172 MachineFunction::iterator MBBIter = BB;
12173 ++MBBIter;
12174
12175 MF->insert(MBBIter, bumpMBB);
12176 MF->insert(MBBIter, mallocMBB);
12177 MF->insert(MBBIter, continueMBB);
12178
12179 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12180 (MachineBasicBlock::iterator(MI)), BB->end());
12181 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12182
12183 // Add code to the main basic block to check if the stack limit has been hit,
12184 // and if so, jump to mallocMBB otherwise to bumpMBB.
12185 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012186 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012187 .addReg(tmpSPVReg).addReg(sizeVReg);
12188 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
12189 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012190 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012191 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12192
12193 // bumpMBB simply decreases the stack pointer, since we know the current
12194 // stacklet has enough space.
12195 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012196 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012197 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012198 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012199 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12200
12201 // Calls into a routine in libgcc to allocate more space from the heap.
12202 if (Is64Bit) {
12203 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12204 .addReg(sizeVReg);
12205 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12206 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12207 } else {
12208 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12209 .addImm(12);
12210 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12211 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12212 .addExternalSymbol("__morestack_allocate_stack_space");
12213 }
12214
12215 if (!Is64Bit)
12216 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12217 .addImm(16);
12218
12219 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12220 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12221 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12222
12223 // Set up the CFG correctly.
12224 BB->addSuccessor(bumpMBB);
12225 BB->addSuccessor(mallocMBB);
12226 mallocMBB->addSuccessor(continueMBB);
12227 bumpMBB->addSuccessor(continueMBB);
12228
12229 // Take care of the PHI nodes.
12230 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12231 MI->getOperand(0).getReg())
12232 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12233 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12234
12235 // Delete the original pseudo instruction.
12236 MI->eraseFromParent();
12237
12238 // And we're done.
12239 return continueMBB;
12240}
12241
12242MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012243X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012244 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012245 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12246 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012247
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012248 assert(!Subtarget->isTargetEnvMacho());
12249
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012250 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12251 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012252
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012253 if (Subtarget->isTargetWin64()) {
12254 if (Subtarget->isTargetCygMing()) {
12255 // ___chkstk(Mingw64):
12256 // Clobbers R10, R11, RAX and EFLAGS.
12257 // Updates RSP.
12258 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12259 .addExternalSymbol("___chkstk")
12260 .addReg(X86::RAX, RegState::Implicit)
12261 .addReg(X86::RSP, RegState::Implicit)
12262 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12263 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12264 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12265 } else {
12266 // __chkstk(MSVCRT): does not update stack pointer.
12267 // Clobbers R10, R11 and EFLAGS.
12268 // FIXME: RAX(allocated size) might be reused and not killed.
12269 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12270 .addExternalSymbol("__chkstk")
12271 .addReg(X86::RAX, RegState::Implicit)
12272 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12273 // RAX has the offset to subtracted from RSP.
12274 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12275 .addReg(X86::RSP)
12276 .addReg(X86::RAX);
12277 }
12278 } else {
12279 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012280 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12281
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012282 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12283 .addExternalSymbol(StackProbeSymbol)
12284 .addReg(X86::EAX, RegState::Implicit)
12285 .addReg(X86::ESP, RegState::Implicit)
12286 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12287 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12288 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12289 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012290
Dan Gohman14152b42010-07-06 20:24:04 +000012291 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012292 return BB;
12293}
Chris Lattner52600972009-09-02 05:57:00 +000012294
12295MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012296X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12297 MachineBasicBlock *BB) const {
12298 // This is pretty easy. We're taking the value that we received from
12299 // our load from the relocation, sticking it in either RDI (x86-64)
12300 // or EAX and doing an indirect call. The return value will then
12301 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012302 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012303 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012304 DebugLoc DL = MI->getDebugLoc();
12305 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012306
12307 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012308 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012309
Eric Christopher30ef0e52010-06-03 04:07:48 +000012310 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012311 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12312 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012313 .addReg(X86::RIP)
12314 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012315 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012316 MI->getOperand(3).getTargetFlags())
12317 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012318 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012319 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012320 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012321 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12322 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012323 .addReg(0)
12324 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012325 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012326 MI->getOperand(3).getTargetFlags())
12327 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012328 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012329 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012330 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012331 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12332 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012333 .addReg(TII->getGlobalBaseReg(F))
12334 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012335 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012336 MI->getOperand(3).getTargetFlags())
12337 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012338 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012339 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012340 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012341
Dan Gohman14152b42010-07-06 20:24:04 +000012342 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012343 return BB;
12344}
12345
12346MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012347X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012348 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012349 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000012350 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012351 case X86::TAILJMPd64:
12352 case X86::TAILJMPr64:
12353 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000012354 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012355 case X86::TCRETURNdi64:
12356 case X86::TCRETURNri64:
12357 case X86::TCRETURNmi64:
12358 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12359 // On AMD64, additional defs should be added before register allocation.
12360 if (!Subtarget->isTargetWin64()) {
12361 MI->addRegisterDefined(X86::RSI);
12362 MI->addRegisterDefined(X86::RDI);
12363 MI->addRegisterDefined(X86::XMM6);
12364 MI->addRegisterDefined(X86::XMM7);
12365 MI->addRegisterDefined(X86::XMM8);
12366 MI->addRegisterDefined(X86::XMM9);
12367 MI->addRegisterDefined(X86::XMM10);
12368 MI->addRegisterDefined(X86::XMM11);
12369 MI->addRegisterDefined(X86::XMM12);
12370 MI->addRegisterDefined(X86::XMM13);
12371 MI->addRegisterDefined(X86::XMM14);
12372 MI->addRegisterDefined(X86::XMM15);
12373 }
12374 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012375 case X86::WIN_ALLOCA:
12376 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012377 case X86::SEG_ALLOCA_32:
12378 return EmitLoweredSegAlloca(MI, BB, false);
12379 case X86::SEG_ALLOCA_64:
12380 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012381 case X86::TLSCall_32:
12382 case X86::TLSCall_64:
12383 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012384 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012385 case X86::CMOV_FR32:
12386 case X86::CMOV_FR64:
12387 case X86::CMOV_V4F32:
12388 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012389 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012390 case X86::CMOV_V8F32:
12391 case X86::CMOV_V4F64:
12392 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012393 case X86::CMOV_GR16:
12394 case X86::CMOV_GR32:
12395 case X86::CMOV_RFP32:
12396 case X86::CMOV_RFP64:
12397 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012398 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012399
Dale Johannesen849f2142007-07-03 00:53:03 +000012400 case X86::FP32_TO_INT16_IN_MEM:
12401 case X86::FP32_TO_INT32_IN_MEM:
12402 case X86::FP32_TO_INT64_IN_MEM:
12403 case X86::FP64_TO_INT16_IN_MEM:
12404 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012405 case X86::FP64_TO_INT64_IN_MEM:
12406 case X86::FP80_TO_INT16_IN_MEM:
12407 case X86::FP80_TO_INT32_IN_MEM:
12408 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012409 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12410 DebugLoc DL = MI->getDebugLoc();
12411
Evan Cheng60c07e12006-07-05 22:17:51 +000012412 // Change the floating point control register to use "round towards zero"
12413 // mode when truncating to an integer value.
12414 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012415 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012416 addFrameReference(BuildMI(*BB, MI, DL,
12417 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012418
12419 // Load the old value of the high byte of the control word...
12420 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012421 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012422 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012423 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012424
12425 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012426 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012427 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012428
12429 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012430 addFrameReference(BuildMI(*BB, MI, DL,
12431 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012432
12433 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012434 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012435 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012436
12437 // Get the X86 opcode to use.
12438 unsigned Opc;
12439 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012440 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012441 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12442 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12443 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12444 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12445 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12446 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012447 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12448 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12449 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012450 }
12451
12452 X86AddressMode AM;
12453 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012454 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012455 AM.BaseType = X86AddressMode::RegBase;
12456 AM.Base.Reg = Op.getReg();
12457 } else {
12458 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012459 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012460 }
12461 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012462 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012463 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012464 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012465 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012466 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012467 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012468 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012469 AM.GV = Op.getGlobal();
12470 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012471 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012472 }
Dan Gohman14152b42010-07-06 20:24:04 +000012473 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012474 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012475
12476 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012477 addFrameReference(BuildMI(*BB, MI, DL,
12478 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012479
Dan Gohman14152b42010-07-06 20:24:04 +000012480 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012481 return BB;
12482 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012483 // String/text processing lowering.
12484 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012485 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012486 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12487 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012488 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012489 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12490 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012491 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012492 return EmitPCMP(MI, BB, 5, false /* in mem */);
12493 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012494 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012495 return EmitPCMP(MI, BB, 5, true /* in mem */);
12496
Eric Christopher228232b2010-11-30 07:20:12 +000012497 // Thread synchronization.
12498 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012499 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012500 case X86::MWAIT:
12501 return EmitMwait(MI, BB);
12502
Eric Christopherb120ab42009-08-18 22:50:32 +000012503 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012504 case X86::ATOMAND32:
12505 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012506 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012507 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012508 X86::NOT32r, X86::EAX,
12509 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012510 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012511 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12512 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012513 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012514 X86::NOT32r, X86::EAX,
12515 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012516 case X86::ATOMXOR32:
12517 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012518 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012519 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012520 X86::NOT32r, X86::EAX,
12521 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012522 case X86::ATOMNAND32:
12523 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012524 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012525 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012526 X86::NOT32r, X86::EAX,
12527 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012528 case X86::ATOMMIN32:
12529 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12530 case X86::ATOMMAX32:
12531 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12532 case X86::ATOMUMIN32:
12533 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12534 case X86::ATOMUMAX32:
12535 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012536
12537 case X86::ATOMAND16:
12538 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12539 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012540 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012541 X86::NOT16r, X86::AX,
12542 X86::GR16RegisterClass);
12543 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012544 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012545 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012546 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012547 X86::NOT16r, X86::AX,
12548 X86::GR16RegisterClass);
12549 case X86::ATOMXOR16:
12550 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12551 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012552 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012553 X86::NOT16r, X86::AX,
12554 X86::GR16RegisterClass);
12555 case X86::ATOMNAND16:
12556 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12557 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012558 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012559 X86::NOT16r, X86::AX,
12560 X86::GR16RegisterClass, true);
12561 case X86::ATOMMIN16:
12562 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12563 case X86::ATOMMAX16:
12564 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12565 case X86::ATOMUMIN16:
12566 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12567 case X86::ATOMUMAX16:
12568 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12569
12570 case X86::ATOMAND8:
12571 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12572 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012573 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012574 X86::NOT8r, X86::AL,
12575 X86::GR8RegisterClass);
12576 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012577 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012578 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012579 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012580 X86::NOT8r, X86::AL,
12581 X86::GR8RegisterClass);
12582 case X86::ATOMXOR8:
12583 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12584 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012585 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012586 X86::NOT8r, X86::AL,
12587 X86::GR8RegisterClass);
12588 case X86::ATOMNAND8:
12589 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12590 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012591 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012592 X86::NOT8r, X86::AL,
12593 X86::GR8RegisterClass, true);
12594 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012595 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012596 case X86::ATOMAND64:
12597 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012598 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012599 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012600 X86::NOT64r, X86::RAX,
12601 X86::GR64RegisterClass);
12602 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012603 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12604 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012605 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012606 X86::NOT64r, X86::RAX,
12607 X86::GR64RegisterClass);
12608 case X86::ATOMXOR64:
12609 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012610 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012611 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012612 X86::NOT64r, X86::RAX,
12613 X86::GR64RegisterClass);
12614 case X86::ATOMNAND64:
12615 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12616 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012617 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012618 X86::NOT64r, X86::RAX,
12619 X86::GR64RegisterClass, true);
12620 case X86::ATOMMIN64:
12621 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12622 case X86::ATOMMAX64:
12623 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12624 case X86::ATOMUMIN64:
12625 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12626 case X86::ATOMUMAX64:
12627 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012628
12629 // This group does 64-bit operations on a 32-bit host.
12630 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012631 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012632 X86::AND32rr, X86::AND32rr,
12633 X86::AND32ri, X86::AND32ri,
12634 false);
12635 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012636 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012637 X86::OR32rr, X86::OR32rr,
12638 X86::OR32ri, X86::OR32ri,
12639 false);
12640 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012641 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012642 X86::XOR32rr, X86::XOR32rr,
12643 X86::XOR32ri, X86::XOR32ri,
12644 false);
12645 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012646 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012647 X86::AND32rr, X86::AND32rr,
12648 X86::AND32ri, X86::AND32ri,
12649 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012650 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012651 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012652 X86::ADD32rr, X86::ADC32rr,
12653 X86::ADD32ri, X86::ADC32ri,
12654 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012655 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012656 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012657 X86::SUB32rr, X86::SBB32rr,
12658 X86::SUB32ri, X86::SBB32ri,
12659 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012660 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012661 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012662 X86::MOV32rr, X86::MOV32rr,
12663 X86::MOV32ri, X86::MOV32ri,
12664 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012665 case X86::VASTART_SAVE_XMM_REGS:
12666 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012667
12668 case X86::VAARG_64:
12669 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012670 }
12671}
12672
12673//===----------------------------------------------------------------------===//
12674// X86 Optimization Hooks
12675//===----------------------------------------------------------------------===//
12676
Dan Gohman475871a2008-07-27 21:46:04 +000012677void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012678 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012679 APInt &KnownZero,
12680 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012681 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012682 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012683 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012684 assert((Opc >= ISD::BUILTIN_OP_END ||
12685 Opc == ISD::INTRINSIC_WO_CHAIN ||
12686 Opc == ISD::INTRINSIC_W_CHAIN ||
12687 Opc == ISD::INTRINSIC_VOID) &&
12688 "Should use MaskedValueIsZero if you don't know whether Op"
12689 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012690
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012691 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012692 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012693 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012694 case X86ISD::ADD:
12695 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012696 case X86ISD::ADC:
12697 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012698 case X86ISD::SMUL:
12699 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012700 case X86ISD::INC:
12701 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012702 case X86ISD::OR:
12703 case X86ISD::XOR:
12704 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012705 // These nodes' second result is a boolean.
12706 if (Op.getResNo() == 0)
12707 break;
12708 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012709 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012710 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12711 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012712 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012713 case ISD::INTRINSIC_WO_CHAIN: {
12714 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12715 unsigned NumLoBits = 0;
12716 switch (IntId) {
12717 default: break;
12718 case Intrinsic::x86_sse_movmsk_ps:
12719 case Intrinsic::x86_avx_movmsk_ps_256:
12720 case Intrinsic::x86_sse2_movmsk_pd:
12721 case Intrinsic::x86_avx_movmsk_pd_256:
12722 case Intrinsic::x86_mmx_pmovmskb:
12723 case Intrinsic::x86_sse2_pmovmskb_128: {
12724 // High bits of movmskp{s|d}, pmovmskb are known zero.
12725 switch (IntId) {
12726 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12727 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12728 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12729 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12730 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12731 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
12732 }
12733 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12734 Mask.getBitWidth() - NumLoBits);
12735 break;
12736 }
12737 }
12738 break;
12739 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012740 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012741}
Chris Lattner259e97c2006-01-31 19:43:35 +000012742
Owen Andersonbc146b02010-09-21 20:42:50 +000012743unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12744 unsigned Depth) const {
12745 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12746 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12747 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012748
Owen Andersonbc146b02010-09-21 20:42:50 +000012749 // Fallback case.
12750 return 1;
12751}
12752
Evan Cheng206ee9d2006-07-07 08:33:52 +000012753/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012754/// node is a GlobalAddress + offset.
12755bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012756 const GlobalValue* &GA,
12757 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012758 if (N->getOpcode() == X86ISD::Wrapper) {
12759 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012760 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012761 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012762 return true;
12763 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012764 }
Evan Chengad4196b2008-05-12 19:56:52 +000012765 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012766}
12767
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012768/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12769/// same as extracting the high 128-bit part of 256-bit vector and then
12770/// inserting the result into the low part of a new 256-bit vector
12771static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12772 EVT VT = SVOp->getValueType(0);
12773 int NumElems = VT.getVectorNumElements();
12774
12775 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12776 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12777 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12778 SVOp->getMaskElt(j) >= 0)
12779 return false;
12780
12781 return true;
12782}
12783
12784/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12785/// same as extracting the low 128-bit part of 256-bit vector and then
12786/// inserting the result into the high part of a new 256-bit vector
12787static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12788 EVT VT = SVOp->getValueType(0);
12789 int NumElems = VT.getVectorNumElements();
12790
12791 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12792 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12793 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12794 SVOp->getMaskElt(j) >= 0)
12795 return false;
12796
12797 return true;
12798}
12799
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012800/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12801static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12802 TargetLowering::DAGCombinerInfo &DCI) {
12803 DebugLoc dl = N->getDebugLoc();
12804 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12805 SDValue V1 = SVOp->getOperand(0);
12806 SDValue V2 = SVOp->getOperand(1);
12807 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012808 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012809
12810 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12811 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12812 //
12813 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012814 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012815 // V UNDEF BUILD_VECTOR UNDEF
12816 // \ / \ /
12817 // CONCAT_VECTOR CONCAT_VECTOR
12818 // \ /
12819 // \ /
12820 // RESULT: V + zero extended
12821 //
12822 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12823 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12824 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12825 return SDValue();
12826
12827 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12828 return SDValue();
12829
12830 // To match the shuffle mask, the first half of the mask should
12831 // be exactly the first vector, and all the rest a splat with the
12832 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012833 for (int i = 0; i < NumElems/2; ++i)
12834 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12835 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12836 return SDValue();
12837
12838 // Emit a zeroed vector and insert the desired subvector on its
12839 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012840 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012841 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12842 DAG.getConstant(0, MVT::i32), DAG, dl);
12843 return DCI.CombineTo(N, InsV);
12844 }
12845
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012846 //===--------------------------------------------------------------------===//
12847 // Combine some shuffles into subvector extracts and inserts:
12848 //
12849
12850 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12851 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12852 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12853 DAG, dl);
12854 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12855 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12856 return DCI.CombineTo(N, InsV);
12857 }
12858
12859 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12860 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12861 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12862 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12863 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12864 return DCI.CombineTo(N, InsV);
12865 }
12866
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012867 return SDValue();
12868}
12869
12870/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012871static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012872 TargetLowering::DAGCombinerInfo &DCI,
12873 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012874 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012875 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012876
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012877 // Don't create instructions with illegal types after legalize types has run.
12878 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12879 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12880 return SDValue();
12881
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012882 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12883 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12884 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012885 return PerformShuffleCombine256(N, DAG, DCI);
12886
12887 // Only handle 128 wide vector from here on.
12888 if (VT.getSizeInBits() != 128)
12889 return SDValue();
12890
12891 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12892 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12893 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012894 SmallVector<SDValue, 16> Elts;
12895 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012896 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012897
Nate Begemanfdea31a2010-03-24 20:49:50 +000012898 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012899}
Evan Chengd880b972008-05-09 21:53:03 +000012900
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012901/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12902/// generation and convert it from being a bunch of shuffles and extracts
12903/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012904static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12905 const TargetLowering &TLI) {
12906 SDValue InputVector = N->getOperand(0);
12907
12908 // Only operate on vectors of 4 elements, where the alternative shuffling
12909 // gets to be more expensive.
12910 if (InputVector.getValueType() != MVT::v4i32)
12911 return SDValue();
12912
12913 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12914 // single use which is a sign-extend or zero-extend, and all elements are
12915 // used.
12916 SmallVector<SDNode *, 4> Uses;
12917 unsigned ExtractedElements = 0;
12918 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12919 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12920 if (UI.getUse().getResNo() != InputVector.getResNo())
12921 return SDValue();
12922
12923 SDNode *Extract = *UI;
12924 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12925 return SDValue();
12926
12927 if (Extract->getValueType(0) != MVT::i32)
12928 return SDValue();
12929 if (!Extract->hasOneUse())
12930 return SDValue();
12931 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12932 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12933 return SDValue();
12934 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12935 return SDValue();
12936
12937 // Record which element was extracted.
12938 ExtractedElements |=
12939 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12940
12941 Uses.push_back(Extract);
12942 }
12943
12944 // If not all the elements were used, this may not be worthwhile.
12945 if (ExtractedElements != 15)
12946 return SDValue();
12947
12948 // Ok, we've now decided to do the transformation.
12949 DebugLoc dl = InputVector.getDebugLoc();
12950
12951 // Store the value to a temporary stack slot.
12952 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012953 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12954 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012955
12956 // Replace each use (extract) with a load of the appropriate element.
12957 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12958 UE = Uses.end(); UI != UE; ++UI) {
12959 SDNode *Extract = *UI;
12960
Nadav Rotem86694292011-05-17 08:31:57 +000012961 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012962 SDValue Idx = Extract->getOperand(1);
12963 unsigned EltSize =
12964 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12965 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12966 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12967
Nadav Rotem86694292011-05-17 08:31:57 +000012968 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012969 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012970
12971 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012972 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012973 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000012974 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012975
12976 // Replace the exact with the load.
12977 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12978 }
12979
12980 // The replacement was made in place; don't return anything.
12981 return SDValue();
12982}
12983
Duncan Sands6bcd2192011-09-17 16:49:39 +000012984/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
12985/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012986static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012987 const X86Subtarget *Subtarget) {
12988 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012989 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012990 // Get the LHS/RHS of the select.
12991 SDValue LHS = N->getOperand(1);
12992 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000012993 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000012994
Dan Gohman670e5392009-09-21 18:03:22 +000012995 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012996 // instructions match the semantics of the common C idiom x<y?x:y but not
12997 // x<=y?x:y, because of how they handle negative zero (which can be
12998 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000012999 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13000 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
13001 (Subtarget->hasXMMInt() ||
13002 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013003 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013004
Chris Lattner47b4ce82009-03-11 05:48:52 +000013005 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013006 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013007 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13008 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013009 switch (CC) {
13010 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013011 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013012 // Converting this to a min would handle NaNs incorrectly, and swapping
13013 // the operands would cause it to handle comparisons between positive
13014 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013015 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000013016 if (!UnsafeFPMath &&
13017 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13018 break;
13019 std::swap(LHS, RHS);
13020 }
Dan Gohman670e5392009-09-21 18:03:22 +000013021 Opcode = X86ISD::FMIN;
13022 break;
13023 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013024 // Converting this to a min would handle comparisons between positive
13025 // and negative zero incorrectly.
13026 if (!UnsafeFPMath &&
13027 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13028 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013029 Opcode = X86ISD::FMIN;
13030 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013031 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013032 // Converting this to a min would handle both negative zeros and NaNs
13033 // incorrectly, but we can swap the operands to fix both.
13034 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013035 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013036 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013037 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013038 Opcode = X86ISD::FMIN;
13039 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013040
Dan Gohman670e5392009-09-21 18:03:22 +000013041 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013042 // Converting this to a max would handle comparisons between positive
13043 // and negative zero incorrectly.
13044 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013045 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013046 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013047 Opcode = X86ISD::FMAX;
13048 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013049 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013050 // Converting this to a max would handle NaNs incorrectly, and swapping
13051 // the operands would cause it to handle comparisons between positive
13052 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013053 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000013054 if (!UnsafeFPMath &&
13055 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13056 break;
13057 std::swap(LHS, RHS);
13058 }
Dan Gohman670e5392009-09-21 18:03:22 +000013059 Opcode = X86ISD::FMAX;
13060 break;
13061 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013062 // Converting this to a max would handle both negative zeros and NaNs
13063 // incorrectly, but we can swap the operands to fix both.
13064 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013065 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013066 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013067 case ISD::SETGE:
13068 Opcode = X86ISD::FMAX;
13069 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013070 }
Dan Gohman670e5392009-09-21 18:03:22 +000013071 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013072 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13073 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013074 switch (CC) {
13075 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013076 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013077 // Converting this to a min would handle comparisons between positive
13078 // and negative zero incorrectly, and swapping the operands would
13079 // cause it to handle NaNs incorrectly.
13080 if (!UnsafeFPMath &&
13081 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013082 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013083 break;
13084 std::swap(LHS, RHS);
13085 }
Dan Gohman670e5392009-09-21 18:03:22 +000013086 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013087 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013088 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013089 // Converting this to a min would handle NaNs incorrectly.
13090 if (!UnsafeFPMath &&
13091 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13092 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013093 Opcode = X86ISD::FMIN;
13094 break;
13095 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013096 // Converting this to a min would handle both negative zeros and NaNs
13097 // incorrectly, but we can swap the operands to fix both.
13098 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013099 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013100 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013101 case ISD::SETGE:
13102 Opcode = X86ISD::FMIN;
13103 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013104
Dan Gohman670e5392009-09-21 18:03:22 +000013105 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013106 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013107 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013108 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013109 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013110 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013111 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013112 // Converting this to a max would handle comparisons between positive
13113 // and negative zero incorrectly, and swapping the operands would
13114 // cause it to handle NaNs incorrectly.
13115 if (!UnsafeFPMath &&
13116 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013117 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013118 break;
13119 std::swap(LHS, RHS);
13120 }
Dan Gohman670e5392009-09-21 18:03:22 +000013121 Opcode = X86ISD::FMAX;
13122 break;
13123 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013124 // Converting this to a max would handle both negative zeros and NaNs
13125 // incorrectly, but we can swap the operands to fix both.
13126 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013127 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013128 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013129 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013130 Opcode = X86ISD::FMAX;
13131 break;
13132 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013133 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013134
Chris Lattner47b4ce82009-03-11 05:48:52 +000013135 if (Opcode)
13136 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013137 }
Eric Christopherfd179292009-08-27 18:07:15 +000013138
Chris Lattnerd1980a52009-03-12 06:52:53 +000013139 // If this is a select between two integer constants, try to do some
13140 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013141 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13142 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013143 // Don't do this for crazy integer types.
13144 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13145 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013146 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013147 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013148
Chris Lattnercee56e72009-03-13 05:53:31 +000013149 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013150 // Efficiently invertible.
13151 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13152 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13153 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13154 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013155 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013156 }
Eric Christopherfd179292009-08-27 18:07:15 +000013157
Chris Lattnerd1980a52009-03-12 06:52:53 +000013158 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013159 if (FalseC->getAPIntValue() == 0 &&
13160 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013161 if (NeedsCondInvert) // Invert the condition if needed.
13162 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13163 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013164
Chris Lattnerd1980a52009-03-12 06:52:53 +000013165 // Zero extend the condition if needed.
13166 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013167
Chris Lattnercee56e72009-03-13 05:53:31 +000013168 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013169 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013170 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013171 }
Eric Christopherfd179292009-08-27 18:07:15 +000013172
Chris Lattner97a29a52009-03-13 05:22:11 +000013173 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013174 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013175 if (NeedsCondInvert) // Invert the condition if needed.
13176 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13177 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013178
Chris Lattner97a29a52009-03-13 05:22:11 +000013179 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013180 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13181 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013182 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013183 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013184 }
Eric Christopherfd179292009-08-27 18:07:15 +000013185
Chris Lattnercee56e72009-03-13 05:53:31 +000013186 // Optimize cases that will turn into an LEA instruction. This requires
13187 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013188 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013189 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013190 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013191
Chris Lattnercee56e72009-03-13 05:53:31 +000013192 bool isFastMultiplier = false;
13193 if (Diff < 10) {
13194 switch ((unsigned char)Diff) {
13195 default: break;
13196 case 1: // result = add base, cond
13197 case 2: // result = lea base( , cond*2)
13198 case 3: // result = lea base(cond, cond*2)
13199 case 4: // result = lea base( , cond*4)
13200 case 5: // result = lea base(cond, cond*4)
13201 case 8: // result = lea base( , cond*8)
13202 case 9: // result = lea base(cond, cond*8)
13203 isFastMultiplier = true;
13204 break;
13205 }
13206 }
Eric Christopherfd179292009-08-27 18:07:15 +000013207
Chris Lattnercee56e72009-03-13 05:53:31 +000013208 if (isFastMultiplier) {
13209 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13210 if (NeedsCondInvert) // Invert the condition if needed.
13211 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13212 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013213
Chris Lattnercee56e72009-03-13 05:53:31 +000013214 // Zero extend the condition if needed.
13215 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13216 Cond);
13217 // Scale the condition by the difference.
13218 if (Diff != 1)
13219 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13220 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013221
Chris Lattnercee56e72009-03-13 05:53:31 +000013222 // Add the base if non-zero.
13223 if (FalseC->getAPIntValue() != 0)
13224 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13225 SDValue(FalseC, 0));
13226 return Cond;
13227 }
Eric Christopherfd179292009-08-27 18:07:15 +000013228 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013229 }
13230 }
Eric Christopherfd179292009-08-27 18:07:15 +000013231
Dan Gohman475871a2008-07-27 21:46:04 +000013232 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013233}
13234
Chris Lattnerd1980a52009-03-12 06:52:53 +000013235/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13236static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13237 TargetLowering::DAGCombinerInfo &DCI) {
13238 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013239
Chris Lattnerd1980a52009-03-12 06:52:53 +000013240 // If the flag operand isn't dead, don't touch this CMOV.
13241 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13242 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013243
Evan Chengb5a55d92011-05-24 01:48:22 +000013244 SDValue FalseOp = N->getOperand(0);
13245 SDValue TrueOp = N->getOperand(1);
13246 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13247 SDValue Cond = N->getOperand(3);
13248 if (CC == X86::COND_E || CC == X86::COND_NE) {
13249 switch (Cond.getOpcode()) {
13250 default: break;
13251 case X86ISD::BSR:
13252 case X86ISD::BSF:
13253 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13254 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13255 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13256 }
13257 }
13258
Chris Lattnerd1980a52009-03-12 06:52:53 +000013259 // If this is a select between two integer constants, try to do some
13260 // optimizations. Note that the operands are ordered the opposite of SELECT
13261 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013262 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13263 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013264 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13265 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013266 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13267 CC = X86::GetOppositeBranchCondition(CC);
13268 std::swap(TrueC, FalseC);
13269 }
Eric Christopherfd179292009-08-27 18:07:15 +000013270
Chris Lattnerd1980a52009-03-12 06:52:53 +000013271 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013272 // This is efficient for any integer data type (including i8/i16) and
13273 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013274 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013275 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13276 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013277
Chris Lattnerd1980a52009-03-12 06:52:53 +000013278 // Zero extend the condition if needed.
13279 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013280
Chris Lattnerd1980a52009-03-12 06:52:53 +000013281 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13282 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013283 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013284 if (N->getNumValues() == 2) // Dead flag value?
13285 return DCI.CombineTo(N, Cond, SDValue());
13286 return Cond;
13287 }
Eric Christopherfd179292009-08-27 18:07:15 +000013288
Chris Lattnercee56e72009-03-13 05:53:31 +000013289 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13290 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013291 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013292 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13293 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013294
Chris Lattner97a29a52009-03-13 05:22:11 +000013295 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013296 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13297 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013298 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13299 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013300
Chris Lattner97a29a52009-03-13 05:22:11 +000013301 if (N->getNumValues() == 2) // Dead flag value?
13302 return DCI.CombineTo(N, Cond, SDValue());
13303 return Cond;
13304 }
Eric Christopherfd179292009-08-27 18:07:15 +000013305
Chris Lattnercee56e72009-03-13 05:53:31 +000013306 // Optimize cases that will turn into an LEA instruction. This requires
13307 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013308 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013309 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013310 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013311
Chris Lattnercee56e72009-03-13 05:53:31 +000013312 bool isFastMultiplier = false;
13313 if (Diff < 10) {
13314 switch ((unsigned char)Diff) {
13315 default: break;
13316 case 1: // result = add base, cond
13317 case 2: // result = lea base( , cond*2)
13318 case 3: // result = lea base(cond, cond*2)
13319 case 4: // result = lea base( , cond*4)
13320 case 5: // result = lea base(cond, cond*4)
13321 case 8: // result = lea base( , cond*8)
13322 case 9: // result = lea base(cond, cond*8)
13323 isFastMultiplier = true;
13324 break;
13325 }
13326 }
Eric Christopherfd179292009-08-27 18:07:15 +000013327
Chris Lattnercee56e72009-03-13 05:53:31 +000013328 if (isFastMultiplier) {
13329 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013330 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13331 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013332 // Zero extend the condition if needed.
13333 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13334 Cond);
13335 // Scale the condition by the difference.
13336 if (Diff != 1)
13337 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13338 DAG.getConstant(Diff, Cond.getValueType()));
13339
13340 // Add the base if non-zero.
13341 if (FalseC->getAPIntValue() != 0)
13342 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13343 SDValue(FalseC, 0));
13344 if (N->getNumValues() == 2) // Dead flag value?
13345 return DCI.CombineTo(N, Cond, SDValue());
13346 return Cond;
13347 }
Eric Christopherfd179292009-08-27 18:07:15 +000013348 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013349 }
13350 }
13351 return SDValue();
13352}
13353
13354
Evan Cheng0b0cd912009-03-28 05:57:29 +000013355/// PerformMulCombine - Optimize a single multiply with constant into two
13356/// in order to implement it with two cheaper instructions, e.g.
13357/// LEA + SHL, LEA + LEA.
13358static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13359 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013360 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13361 return SDValue();
13362
Owen Andersone50ed302009-08-10 22:56:29 +000013363 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013364 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013365 return SDValue();
13366
13367 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13368 if (!C)
13369 return SDValue();
13370 uint64_t MulAmt = C->getZExtValue();
13371 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13372 return SDValue();
13373
13374 uint64_t MulAmt1 = 0;
13375 uint64_t MulAmt2 = 0;
13376 if ((MulAmt % 9) == 0) {
13377 MulAmt1 = 9;
13378 MulAmt2 = MulAmt / 9;
13379 } else if ((MulAmt % 5) == 0) {
13380 MulAmt1 = 5;
13381 MulAmt2 = MulAmt / 5;
13382 } else if ((MulAmt % 3) == 0) {
13383 MulAmt1 = 3;
13384 MulAmt2 = MulAmt / 3;
13385 }
13386 if (MulAmt2 &&
13387 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13388 DebugLoc DL = N->getDebugLoc();
13389
13390 if (isPowerOf2_64(MulAmt2) &&
13391 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13392 // If second multiplifer is pow2, issue it first. We want the multiply by
13393 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13394 // is an add.
13395 std::swap(MulAmt1, MulAmt2);
13396
13397 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013398 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013399 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013400 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013401 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013402 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013403 DAG.getConstant(MulAmt1, VT));
13404
Eric Christopherfd179292009-08-27 18:07:15 +000013405 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013406 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013407 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013408 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013409 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013410 DAG.getConstant(MulAmt2, VT));
13411
13412 // Do not add new nodes to DAG combiner worklist.
13413 DCI.CombineTo(N, NewMul, false);
13414 }
13415 return SDValue();
13416}
13417
Evan Chengad9c0a32009-12-15 00:53:42 +000013418static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13419 SDValue N0 = N->getOperand(0);
13420 SDValue N1 = N->getOperand(1);
13421 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13422 EVT VT = N0.getValueType();
13423
13424 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13425 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013426 if (VT.isInteger() && !VT.isVector() &&
13427 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013428 N0.getOperand(1).getOpcode() == ISD::Constant) {
13429 SDValue N00 = N0.getOperand(0);
13430 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13431 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13432 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13433 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13434 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13435 APInt ShAmt = N1C->getAPIntValue();
13436 Mask = Mask.shl(ShAmt);
13437 if (Mask != 0)
13438 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13439 N00, DAG.getConstant(Mask, VT));
13440 }
13441 }
13442
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013443
13444 // Hardware support for vector shifts is sparse which makes us scalarize the
13445 // vector operations in many cases. Also, on sandybridge ADD is faster than
13446 // shl.
13447 // (shl V, 1) -> add V,V
13448 if (isSplatVector(N1.getNode())) {
13449 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13450 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13451 // We shift all of the values by one. In many cases we do not have
13452 // hardware support for this operation. This is better expressed as an ADD
13453 // of two values.
13454 if (N1C && (1 == N1C->getZExtValue())) {
13455 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13456 }
13457 }
13458
Evan Chengad9c0a32009-12-15 00:53:42 +000013459 return SDValue();
13460}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013461
Nate Begeman740ab032009-01-26 00:52:55 +000013462/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13463/// when possible.
13464static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13465 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013466 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013467 if (N->getOpcode() == ISD::SHL) {
13468 SDValue V = PerformSHLCombine(N, DAG);
13469 if (V.getNode()) return V;
13470 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013471
Nate Begeman740ab032009-01-26 00:52:55 +000013472 // On X86 with SSE2 support, we can transform this to a vector shift if
13473 // all elements are shifted by the same amount. We can't do this in legalize
13474 // because the a constant vector is typically transformed to a constant pool
13475 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013476 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013477 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013478
Craig Topper7be5dfd2011-11-12 09:58:49 +000013479 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
13480 (!Subtarget->hasAVX2() ||
13481 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013482 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013483
Mon P Wang3becd092009-01-28 08:12:05 +000013484 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013485 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013486 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013487 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013488 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13489 unsigned NumElts = VT.getVectorNumElements();
13490 unsigned i = 0;
13491 for (; i != NumElts; ++i) {
13492 SDValue Arg = ShAmtOp.getOperand(i);
13493 if (Arg.getOpcode() == ISD::UNDEF) continue;
13494 BaseShAmt = Arg;
13495 break;
13496 }
13497 for (; i != NumElts; ++i) {
13498 SDValue Arg = ShAmtOp.getOperand(i);
13499 if (Arg.getOpcode() == ISD::UNDEF) continue;
13500 if (Arg != BaseShAmt) {
13501 return SDValue();
13502 }
13503 }
13504 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013505 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013506 SDValue InVec = ShAmtOp.getOperand(0);
13507 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13508 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13509 unsigned i = 0;
13510 for (; i != NumElts; ++i) {
13511 SDValue Arg = InVec.getOperand(i);
13512 if (Arg.getOpcode() == ISD::UNDEF) continue;
13513 BaseShAmt = Arg;
13514 break;
13515 }
13516 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13517 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013518 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013519 if (C->getZExtValue() == SplatIdx)
13520 BaseShAmt = InVec.getOperand(1);
13521 }
13522 }
13523 if (BaseShAmt.getNode() == 0)
13524 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13525 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013526 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013527 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013528
Mon P Wangefa42202009-09-03 19:56:25 +000013529 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013530 if (EltVT.bitsGT(MVT::i32))
13531 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13532 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013533 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013534
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013535 // The shift amount is identical so we can do a vector shift.
13536 SDValue ValOp = N->getOperand(0);
13537 switch (N->getOpcode()) {
13538 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013539 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013540 break;
13541 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013542 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013543 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013544 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013545 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013546 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013547 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013548 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013549 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013550 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013551 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013552 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013553 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013554 if (VT == MVT::v4i64)
13555 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13556 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
13557 ValOp, BaseShAmt);
13558 if (VT == MVT::v8i32)
13559 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13560 DAG.getConstant(Intrinsic::x86_avx2_pslli_d, MVT::i32),
13561 ValOp, BaseShAmt);
13562 if (VT == MVT::v16i16)
13563 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13564 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
13565 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013566 break;
13567 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013568 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013569 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013570 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013571 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013572 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013573 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013574 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013575 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013576 if (VT == MVT::v8i32)
13577 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13578 DAG.getConstant(Intrinsic::x86_avx2_psrai_d, MVT::i32),
13579 ValOp, BaseShAmt);
13580 if (VT == MVT::v16i16)
13581 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13582 DAG.getConstant(Intrinsic::x86_avx2_psrai_w, MVT::i32),
13583 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013584 break;
13585 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013586 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013587 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013588 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013589 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013590 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013591 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013592 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013593 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013594 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013595 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013596 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013597 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013598 if (VT == MVT::v4i64)
13599 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13600 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
13601 ValOp, BaseShAmt);
13602 if (VT == MVT::v8i32)
13603 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13604 DAG.getConstant(Intrinsic::x86_avx2_psrli_d, MVT::i32),
13605 ValOp, BaseShAmt);
13606 if (VT == MVT::v16i16)
13607 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13608 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
13609 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013610 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013611 }
13612 return SDValue();
13613}
13614
Nate Begemanb65c1752010-12-17 22:55:37 +000013615
Stuart Hastings865f0932011-06-03 23:53:54 +000013616// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13617// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13618// and friends. Likewise for OR -> CMPNEQSS.
13619static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13620 TargetLowering::DAGCombinerInfo &DCI,
13621 const X86Subtarget *Subtarget) {
13622 unsigned opcode;
13623
13624 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13625 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013626 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013627 SDValue N0 = N->getOperand(0);
13628 SDValue N1 = N->getOperand(1);
13629 SDValue CMP0 = N0->getOperand(1);
13630 SDValue CMP1 = N1->getOperand(1);
13631 DebugLoc DL = N->getDebugLoc();
13632
13633 // The SETCCs should both refer to the same CMP.
13634 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13635 return SDValue();
13636
13637 SDValue CMP00 = CMP0->getOperand(0);
13638 SDValue CMP01 = CMP0->getOperand(1);
13639 EVT VT = CMP00.getValueType();
13640
13641 if (VT == MVT::f32 || VT == MVT::f64) {
13642 bool ExpectingFlags = false;
13643 // Check for any users that want flags:
13644 for (SDNode::use_iterator UI = N->use_begin(),
13645 UE = N->use_end();
13646 !ExpectingFlags && UI != UE; ++UI)
13647 switch (UI->getOpcode()) {
13648 default:
13649 case ISD::BR_CC:
13650 case ISD::BRCOND:
13651 case ISD::SELECT:
13652 ExpectingFlags = true;
13653 break;
13654 case ISD::CopyToReg:
13655 case ISD::SIGN_EXTEND:
13656 case ISD::ZERO_EXTEND:
13657 case ISD::ANY_EXTEND:
13658 break;
13659 }
13660
13661 if (!ExpectingFlags) {
13662 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13663 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13664
13665 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13666 X86::CondCode tmp = cc0;
13667 cc0 = cc1;
13668 cc1 = tmp;
13669 }
13670
13671 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13672 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13673 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13674 X86ISD::NodeType NTOperator = is64BitFP ?
13675 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13676 // FIXME: need symbolic constants for these magic numbers.
13677 // See X86ATTInstPrinter.cpp:printSSECC().
13678 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13679 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13680 DAG.getConstant(x86cc, MVT::i8));
13681 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13682 OnesOrZeroesF);
13683 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13684 DAG.getConstant(1, MVT::i32));
13685 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13686 return OneBitOfTruth;
13687 }
13688 }
13689 }
13690 }
13691 return SDValue();
13692}
13693
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013694/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13695/// so it can be folded inside ANDNP.
13696static bool CanFoldXORWithAllOnes(const SDNode *N) {
13697 EVT VT = N->getValueType(0);
13698
13699 // Match direct AllOnes for 128 and 256-bit vectors
13700 if (ISD::isBuildVectorAllOnes(N))
13701 return true;
13702
13703 // Look through a bit convert.
13704 if (N->getOpcode() == ISD::BITCAST)
13705 N = N->getOperand(0).getNode();
13706
13707 // Sometimes the operand may come from a insert_subvector building a 256-bit
13708 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013709 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013710 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13711 SDValue V1 = N->getOperand(0);
13712 SDValue V2 = N->getOperand(1);
13713
13714 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13715 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13716 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13717 ISD::isBuildVectorAllOnes(V2.getNode()))
13718 return true;
13719 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013720
13721 return false;
13722}
13723
Nate Begemanb65c1752010-12-17 22:55:37 +000013724static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13725 TargetLowering::DAGCombinerInfo &DCI,
13726 const X86Subtarget *Subtarget) {
13727 if (DCI.isBeforeLegalizeOps())
13728 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013729
Stuart Hastings865f0932011-06-03 23:53:54 +000013730 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13731 if (R.getNode())
13732 return R;
13733
Craig Topper54a11172011-10-14 07:06:56 +000013734 EVT VT = N->getValueType(0);
13735
Craig Topperb4c94572011-10-21 06:55:01 +000013736 // Create ANDN, BLSI, and BLSR instructions
13737 // BLSI is X & (-X)
13738 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013739 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13740 SDValue N0 = N->getOperand(0);
13741 SDValue N1 = N->getOperand(1);
13742 DebugLoc DL = N->getDebugLoc();
13743
13744 // Check LHS for not
13745 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13746 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13747 // Check RHS for not
13748 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13749 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13750
Craig Topperb4c94572011-10-21 06:55:01 +000013751 // Check LHS for neg
13752 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13753 isZero(N0.getOperand(0)))
13754 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13755
13756 // Check RHS for neg
13757 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13758 isZero(N1.getOperand(0)))
13759 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13760
13761 // Check LHS for X-1
13762 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13763 isAllOnes(N0.getOperand(1)))
13764 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13765
13766 // Check RHS for X-1
13767 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13768 isAllOnes(N1.getOperand(1)))
13769 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13770
Craig Topper54a11172011-10-14 07:06:56 +000013771 return SDValue();
13772 }
13773
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013774 // Want to form ANDNP nodes:
13775 // 1) In the hopes of then easily combining them with OR and AND nodes
13776 // to form PBLEND/PSIGN.
13777 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013778 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013779 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013780
Nate Begemanb65c1752010-12-17 22:55:37 +000013781 SDValue N0 = N->getOperand(0);
13782 SDValue N1 = N->getOperand(1);
13783 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013784
Nate Begemanb65c1752010-12-17 22:55:37 +000013785 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013786 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013787 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13788 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013789 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013790
13791 // Check RHS for vnot
13792 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013793 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13794 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013795 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013796
Nate Begemanb65c1752010-12-17 22:55:37 +000013797 return SDValue();
13798}
13799
Evan Cheng760d1942010-01-04 21:22:48 +000013800static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013801 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013802 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013803 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013804 return SDValue();
13805
Stuart Hastings865f0932011-06-03 23:53:54 +000013806 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13807 if (R.getNode())
13808 return R;
13809
Evan Cheng760d1942010-01-04 21:22:48 +000013810 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000013811 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000013812 return SDValue();
13813
Evan Cheng760d1942010-01-04 21:22:48 +000013814 SDValue N0 = N->getOperand(0);
13815 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013816
Nate Begemanb65c1752010-12-17 22:55:37 +000013817 // look for psign/blend
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013818 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013819 if (VT == MVT::v2i64) {
13820 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013821 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000013822 std::swap(N0, N1);
13823 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013824 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013825 SDValue Mask = N1.getOperand(0);
13826 SDValue X = N1.getOperand(1);
13827 SDValue Y;
13828 if (N0.getOperand(0) == Mask)
13829 Y = N0.getOperand(1);
13830 if (N0.getOperand(1) == Mask)
13831 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013832
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013833 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000013834 if (!Y.getNode())
13835 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013836
Nate Begemanb65c1752010-12-17 22:55:37 +000013837 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13838 if (Mask.getOpcode() != ISD::BITCAST ||
13839 X.getOpcode() != ISD::BITCAST ||
13840 Y.getOpcode() != ISD::BITCAST)
13841 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013842
Nate Begemanb65c1752010-12-17 22:55:37 +000013843 // Look through mask bitcast.
13844 Mask = Mask.getOperand(0);
13845 EVT MaskVT = Mask.getValueType();
13846
13847 // Validate that the Mask operand is a vector sra node. The sra node
13848 // will be an intrinsic.
13849 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13850 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013851
Nate Begemanb65c1752010-12-17 22:55:37 +000013852 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13853 // there is no psrai.b
13854 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13855 case Intrinsic::x86_sse2_psrai_w:
13856 case Intrinsic::x86_sse2_psrai_d:
13857 break;
13858 default: return SDValue();
13859 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013860
Nate Begemanb65c1752010-12-17 22:55:37 +000013861 // Check that the SRA is all signbits.
13862 SDValue SraC = Mask.getOperand(2);
13863 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13864 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13865 if ((SraAmt + 1) != EltBits)
13866 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013867
Nate Begemanb65c1752010-12-17 22:55:37 +000013868 DebugLoc DL = N->getDebugLoc();
13869
13870 // Now we know we at least have a plendvb with the mask val. See if
13871 // we can form a psignb/w/d.
13872 // psign = x.type == y.type == mask.type && y = sub(0, x);
13873 X = X.getOperand(0);
13874 Y = Y.getOperand(0);
13875 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13876 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
13877 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
13878 unsigned Opc = 0;
13879 switch (EltBits) {
13880 case 8: Opc = X86ISD::PSIGNB; break;
13881 case 16: Opc = X86ISD::PSIGNW; break;
13882 case 32: Opc = X86ISD::PSIGND; break;
13883 default: break;
13884 }
13885 if (Opc) {
13886 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
13887 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
13888 }
13889 }
13890 // PBLENDVB only available on SSE 4.1
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013891 if (!(Subtarget->hasSSE41() || Subtarget->hasAVX()))
Nate Begemanb65c1752010-12-17 22:55:37 +000013892 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013893
Nate Begemanb65c1752010-12-17 22:55:37 +000013894 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
13895 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
13896 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000013897 Mask = DAG.getNode(ISD::VSELECT, DL, MVT::v16i8, Mask, X, Y);
Nate Begemanb65c1752010-12-17 22:55:37 +000013898 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
13899 }
13900 }
13901 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013902
Nate Begemanb65c1752010-12-17 22:55:37 +000013903 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013904 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13905 std::swap(N0, N1);
13906 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13907 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013908 if (!N0.hasOneUse() || !N1.hasOneUse())
13909 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013910
13911 SDValue ShAmt0 = N0.getOperand(1);
13912 if (ShAmt0.getValueType() != MVT::i8)
13913 return SDValue();
13914 SDValue ShAmt1 = N1.getOperand(1);
13915 if (ShAmt1.getValueType() != MVT::i8)
13916 return SDValue();
13917 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13918 ShAmt0 = ShAmt0.getOperand(0);
13919 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13920 ShAmt1 = ShAmt1.getOperand(0);
13921
13922 DebugLoc DL = N->getDebugLoc();
13923 unsigned Opc = X86ISD::SHLD;
13924 SDValue Op0 = N0.getOperand(0);
13925 SDValue Op1 = N1.getOperand(0);
13926 if (ShAmt0.getOpcode() == ISD::SUB) {
13927 Opc = X86ISD::SHRD;
13928 std::swap(Op0, Op1);
13929 std::swap(ShAmt0, ShAmt1);
13930 }
13931
Evan Cheng8b1190a2010-04-28 01:18:01 +000013932 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013933 if (ShAmt1.getOpcode() == ISD::SUB) {
13934 SDValue Sum = ShAmt1.getOperand(0);
13935 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013936 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13937 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13938 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13939 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013940 return DAG.getNode(Opc, DL, VT,
13941 Op0, Op1,
13942 DAG.getNode(ISD::TRUNCATE, DL,
13943 MVT::i8, ShAmt0));
13944 }
13945 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13946 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13947 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013948 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013949 return DAG.getNode(Opc, DL, VT,
13950 N0.getOperand(0), N1.getOperand(0),
13951 DAG.getNode(ISD::TRUNCATE, DL,
13952 MVT::i8, ShAmt0));
13953 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013954
Evan Cheng760d1942010-01-04 21:22:48 +000013955 return SDValue();
13956}
13957
Craig Topperb4c94572011-10-21 06:55:01 +000013958static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
13959 TargetLowering::DAGCombinerInfo &DCI,
13960 const X86Subtarget *Subtarget) {
13961 if (DCI.isBeforeLegalizeOps())
13962 return SDValue();
13963
13964 EVT VT = N->getValueType(0);
13965
13966 if (VT != MVT::i32 && VT != MVT::i64)
13967 return SDValue();
13968
13969 // Create BLSMSK instructions by finding X ^ (X-1)
13970 SDValue N0 = N->getOperand(0);
13971 SDValue N1 = N->getOperand(1);
13972 DebugLoc DL = N->getDebugLoc();
13973
13974 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13975 isAllOnes(N0.getOperand(1)))
13976 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
13977
13978 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13979 isAllOnes(N1.getOperand(1)))
13980 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
13981
13982 return SDValue();
13983}
13984
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013985/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
13986static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
13987 const X86Subtarget *Subtarget) {
13988 LoadSDNode *Ld = cast<LoadSDNode>(N);
13989 EVT RegVT = Ld->getValueType(0);
13990 EVT MemVT = Ld->getMemoryVT();
13991 DebugLoc dl = Ld->getDebugLoc();
13992 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13993
13994 ISD::LoadExtType Ext = Ld->getExtensionType();
13995
Nadav Rotemca6f2962011-09-18 19:00:23 +000013996 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013997 // shuffle. We need SSE4 for the shuffles.
13998 // TODO: It is possible to support ZExt by zeroing the undef values
13999 // during the shuffle phase or after the shuffle.
14000 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
14001 assert(MemVT != RegVT && "Cannot extend to the same type");
14002 assert(MemVT.isVector() && "Must load a vector from memory");
14003
14004 unsigned NumElems = RegVT.getVectorNumElements();
14005 unsigned RegSz = RegVT.getSizeInBits();
14006 unsigned MemSz = MemVT.getSizeInBits();
14007 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000014008 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014009 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
14010
14011 // Attempt to load the original value using a single load op.
14012 // Find a scalar type which is equal to the loaded word size.
14013 MVT SclrLoadTy = MVT::i8;
14014 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14015 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14016 MVT Tp = (MVT::SimpleValueType)tp;
14017 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
14018 SclrLoadTy = Tp;
14019 break;
14020 }
14021 }
14022
14023 // Proceed if a load word is found.
14024 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
14025
14026 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
14027 RegSz/SclrLoadTy.getSizeInBits());
14028
14029 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
14030 RegSz/MemVT.getScalarType().getSizeInBits());
14031 // Can't shuffle using an illegal type.
14032 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14033
14034 // Perform a single load.
14035 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
14036 Ld->getBasePtr(),
14037 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014038 Ld->isNonTemporal(), Ld->isInvariant(),
14039 Ld->getAlignment());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014040
14041 // Insert the word loaded into a vector.
14042 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
14043 LoadUnitVecVT, ScalarLoad);
14044
14045 // Bitcast the loaded value to a vector of the original element type, in
14046 // the size of the target vector type.
14047 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
14048 unsigned SizeRatio = RegSz/MemSz;
14049
14050 // Redistribute the loaded elements into the different locations.
14051 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14052 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
14053
14054 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
14055 DAG.getUNDEF(SlicedVec.getValueType()),
14056 ShuffleVec.data());
14057
14058 // Bitcast to the requested type.
14059 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
14060 // Replace the original load with the new sequence
14061 // and return the new chain.
14062 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
14063 return SDValue(ScalarLoad.getNode(), 1);
14064 }
14065
14066 return SDValue();
14067}
14068
Chris Lattner149a4e52008-02-22 02:09:43 +000014069/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014070static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000014071 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000014072 StoreSDNode *St = cast<StoreSDNode>(N);
14073 EVT VT = St->getValue().getValueType();
14074 EVT StVT = St->getMemoryVT();
14075 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000014076 SDValue StoredVal = St->getOperand(1);
14077 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14078
14079 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000014080 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
14081 // 128-bit ones. If in the future the cost becomes only one memory access the
14082 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000014083 if (VT.getSizeInBits() == 256 &&
14084 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
14085 StoredVal.getNumOperands() == 2) {
14086
14087 SDValue Value0 = StoredVal.getOperand(0);
14088 SDValue Value1 = StoredVal.getOperand(1);
14089
14090 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
14091 SDValue Ptr0 = St->getBasePtr();
14092 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
14093
14094 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
14095 St->getPointerInfo(), St->isVolatile(),
14096 St->isNonTemporal(), St->getAlignment());
14097 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
14098 St->getPointerInfo(), St->isVolatile(),
14099 St->isNonTemporal(), St->getAlignment());
14100 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
14101 }
Nadav Rotem614061b2011-08-10 19:30:14 +000014102
14103 // Optimize trunc store (of multiple scalars) to shuffle and store.
14104 // First, pack all of the elements in one place. Next, store to memory
14105 // in fewer chunks.
14106 if (St->isTruncatingStore() && VT.isVector()) {
14107 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14108 unsigned NumElems = VT.getVectorNumElements();
14109 assert(StVT != VT && "Cannot truncate to the same type");
14110 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
14111 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
14112
14113 // From, To sizes and ElemCount must be pow of two
14114 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014115 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000014116 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014117 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014118
Nadav Rotem614061b2011-08-10 19:30:14 +000014119 unsigned SizeRatio = FromSz / ToSz;
14120
14121 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
14122
14123 // Create a type on which we perform the shuffle
14124 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
14125 StVT.getScalarType(), NumElems*SizeRatio);
14126
14127 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
14128
14129 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
14130 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14131 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
14132
14133 // Can't shuffle using an illegal type
14134 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14135
14136 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
14137 DAG.getUNDEF(WideVec.getValueType()),
14138 ShuffleVec.data());
14139 // At this point all of the data is stored at the bottom of the
14140 // register. We now need to save it to mem.
14141
14142 // Find the largest store unit
14143 MVT StoreType = MVT::i8;
14144 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14145 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14146 MVT Tp = (MVT::SimpleValueType)tp;
14147 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
14148 StoreType = Tp;
14149 }
14150
14151 // Bitcast the original vector into a vector of store-size units
14152 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
14153 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
14154 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
14155 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
14156 SmallVector<SDValue, 8> Chains;
14157 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
14158 TLI.getPointerTy());
14159 SDValue Ptr = St->getBasePtr();
14160
14161 // Perform one or more big stores into memory.
14162 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
14163 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14164 StoreType, ShuffWide,
14165 DAG.getIntPtrConstant(i));
14166 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14167 St->getPointerInfo(), St->isVolatile(),
14168 St->isNonTemporal(), St->getAlignment());
14169 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14170 Chains.push_back(Ch);
14171 }
14172
14173 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14174 Chains.size());
14175 }
14176
14177
Chris Lattner149a4e52008-02-22 02:09:43 +000014178 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14179 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014180 // A preferable solution to the general problem is to figure out the right
14181 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014182
14183 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014184 if (VT.getSizeInBits() != 64)
14185 return SDValue();
14186
Devang Patel578efa92009-06-05 21:57:13 +000014187 const Function *F = DAG.getMachineFunction().getFunction();
14188 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000014189 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000014190 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000014191 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014192 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014193 isa<LoadSDNode>(St->getValue()) &&
14194 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14195 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014196 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014197 LoadSDNode *Ld = 0;
14198 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014199 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014200 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014201 // Must be a store of a load. We currently handle two cases: the load
14202 // is a direct child, and it's under an intervening TokenFactor. It is
14203 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014204 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014205 Ld = cast<LoadSDNode>(St->getChain());
14206 else if (St->getValue().hasOneUse() &&
14207 ChainVal->getOpcode() == ISD::TokenFactor) {
14208 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014209 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014210 TokenFactorIndex = i;
14211 Ld = cast<LoadSDNode>(St->getValue());
14212 } else
14213 Ops.push_back(ChainVal->getOperand(i));
14214 }
14215 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014216
Evan Cheng536e6672009-03-12 05:59:15 +000014217 if (!Ld || !ISD::isNormalLoad(Ld))
14218 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014219
Evan Cheng536e6672009-03-12 05:59:15 +000014220 // If this is not the MMX case, i.e. we are just turning i64 load/store
14221 // into f64 load/store, avoid the transformation if there are multiple
14222 // uses of the loaded value.
14223 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14224 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014225
Evan Cheng536e6672009-03-12 05:59:15 +000014226 DebugLoc LdDL = Ld->getDebugLoc();
14227 DebugLoc StDL = N->getDebugLoc();
14228 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14229 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14230 // pair instead.
14231 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014232 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014233 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14234 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014235 Ld->isNonTemporal(), Ld->isInvariant(),
14236 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014237 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014238 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014239 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014240 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014241 Ops.size());
14242 }
Evan Cheng536e6672009-03-12 05:59:15 +000014243 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014244 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014245 St->isVolatile(), St->isNonTemporal(),
14246 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014247 }
Evan Cheng536e6672009-03-12 05:59:15 +000014248
14249 // Otherwise, lower to two pairs of 32-bit loads / stores.
14250 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014251 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14252 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014253
Owen Anderson825b72b2009-08-11 20:47:22 +000014254 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014255 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014256 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014257 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014258 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014259 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014260 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014261 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014262 MinAlign(Ld->getAlignment(), 4));
14263
14264 SDValue NewChain = LoLd.getValue(1);
14265 if (TokenFactorIndex != -1) {
14266 Ops.push_back(LoLd);
14267 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014268 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014269 Ops.size());
14270 }
14271
14272 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014273 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14274 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014275
14276 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014277 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014278 St->isVolatile(), St->isNonTemporal(),
14279 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014280 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014281 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014282 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014283 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014284 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014285 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014286 }
Dan Gohman475871a2008-07-27 21:46:04 +000014287 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014288}
14289
Duncan Sands17470be2011-09-22 20:15:48 +000014290/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14291/// and return the operands for the horizontal operation in LHS and RHS. A
14292/// horizontal operation performs the binary operation on successive elements
14293/// of its first operand, then on successive elements of its second operand,
14294/// returning the resulting values in a vector. For example, if
14295/// A = < float a0, float a1, float a2, float a3 >
14296/// and
14297/// B = < float b0, float b1, float b2, float b3 >
14298/// then the result of doing a horizontal operation on A and B is
14299/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14300/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14301/// A horizontal-op B, for some already available A and B, and if so then LHS is
14302/// set to A, RHS to B, and the routine returns 'true'.
14303/// Note that the binary operation should have the property that if one of the
14304/// operands is UNDEF then the result is UNDEF.
14305static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool isCommutative) {
14306 // Look for the following pattern: if
14307 // A = < float a0, float a1, float a2, float a3 >
14308 // B = < float b0, float b1, float b2, float b3 >
14309 // and
14310 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14311 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14312 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14313 // which is A horizontal-op B.
14314
14315 // At least one of the operands should be a vector shuffle.
14316 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14317 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14318 return false;
14319
14320 EVT VT = LHS.getValueType();
14321 unsigned N = VT.getVectorNumElements();
14322
14323 // View LHS in the form
14324 // LHS = VECTOR_SHUFFLE A, B, LMask
14325 // If LHS is not a shuffle then pretend it is the shuffle
14326 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14327 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14328 // type VT.
14329 SDValue A, B;
14330 SmallVector<int, 8> LMask(N);
14331 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14332 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14333 A = LHS.getOperand(0);
14334 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14335 B = LHS.getOperand(1);
14336 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
14337 } else {
14338 if (LHS.getOpcode() != ISD::UNDEF)
14339 A = LHS;
14340 for (unsigned i = 0; i != N; ++i)
14341 LMask[i] = i;
14342 }
14343
14344 // Likewise, view RHS in the form
14345 // RHS = VECTOR_SHUFFLE C, D, RMask
14346 SDValue C, D;
14347 SmallVector<int, 8> RMask(N);
14348 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14349 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14350 C = RHS.getOperand(0);
14351 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14352 D = RHS.getOperand(1);
14353 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
14354 } else {
14355 if (RHS.getOpcode() != ISD::UNDEF)
14356 C = RHS;
14357 for (unsigned i = 0; i != N; ++i)
14358 RMask[i] = i;
14359 }
14360
14361 // Check that the shuffles are both shuffling the same vectors.
14362 if (!(A == C && B == D) && !(A == D && B == C))
14363 return false;
14364
14365 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14366 if (!A.getNode() && !B.getNode())
14367 return false;
14368
14369 // If A and B occur in reverse order in RHS, then "swap" them (which means
14370 // rewriting the mask).
14371 if (A != C)
14372 for (unsigned i = 0; i != N; ++i) {
14373 unsigned Idx = RMask[i];
14374 if (Idx < N)
14375 RMask[i] += N;
14376 else if (Idx < 2*N)
14377 RMask[i] -= N;
14378 }
14379
14380 // At this point LHS and RHS are equivalent to
14381 // LHS = VECTOR_SHUFFLE A, B, LMask
14382 // RHS = VECTOR_SHUFFLE A, B, RMask
14383 // Check that the masks correspond to performing a horizontal operation.
14384 for (unsigned i = 0; i != N; ++i) {
14385 unsigned LIdx = LMask[i], RIdx = RMask[i];
14386
14387 // Ignore any UNDEF components.
14388 if (LIdx >= 2*N || RIdx >= 2*N || (!A.getNode() && (LIdx < N || RIdx < N))
14389 || (!B.getNode() && (LIdx >= N || RIdx >= N)))
14390 continue;
14391
14392 // Check that successive elements are being operated on. If not, this is
14393 // not a horizontal operation.
14394 if (!(LIdx == 2*i && RIdx == 2*i + 1) &&
14395 !(isCommutative && LIdx == 2*i + 1 && RIdx == 2*i))
14396 return false;
14397 }
14398
14399 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14400 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14401 return true;
14402}
14403
14404/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14405static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14406 const X86Subtarget *Subtarget) {
14407 EVT VT = N->getValueType(0);
14408 SDValue LHS = N->getOperand(0);
14409 SDValue RHS = N->getOperand(1);
14410
14411 // Try to synthesize horizontal adds from adds of shuffles.
14412 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
14413 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
14414 isHorizontalBinOp(LHS, RHS, true))
14415 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14416 return SDValue();
14417}
14418
14419/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14420static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14421 const X86Subtarget *Subtarget) {
14422 EVT VT = N->getValueType(0);
14423 SDValue LHS = N->getOperand(0);
14424 SDValue RHS = N->getOperand(1);
14425
14426 // Try to synthesize horizontal subs from subs of shuffles.
14427 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
14428 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
14429 isHorizontalBinOp(LHS, RHS, false))
14430 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14431 return SDValue();
14432}
14433
Chris Lattner6cf73262008-01-25 06:14:17 +000014434/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14435/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014436static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014437 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14438 // F[X]OR(0.0, x) -> x
14439 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014440 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14441 if (C->getValueAPF().isPosZero())
14442 return N->getOperand(1);
14443 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14444 if (C->getValueAPF().isPosZero())
14445 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014446 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014447}
14448
14449/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014450static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014451 // FAND(0.0, x) -> 0.0
14452 // FAND(x, 0.0) -> 0.0
14453 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14454 if (C->getValueAPF().isPosZero())
14455 return N->getOperand(0);
14456 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14457 if (C->getValueAPF().isPosZero())
14458 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014459 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014460}
14461
Dan Gohmane5af2d32009-01-29 01:59:02 +000014462static SDValue PerformBTCombine(SDNode *N,
14463 SelectionDAG &DAG,
14464 TargetLowering::DAGCombinerInfo &DCI) {
14465 // BT ignores high bits in the bit index operand.
14466 SDValue Op1 = N->getOperand(1);
14467 if (Op1.hasOneUse()) {
14468 unsigned BitWidth = Op1.getValueSizeInBits();
14469 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14470 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014471 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14472 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014473 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014474 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14475 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14476 DCI.CommitTargetLoweringOpt(TLO);
14477 }
14478 return SDValue();
14479}
Chris Lattner83e6c992006-10-04 06:57:07 +000014480
Eli Friedman7a5e5552009-06-07 06:52:44 +000014481static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14482 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014483 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014484 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014485 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014486 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014487 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014488 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014489 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014490 }
14491 return SDValue();
14492}
14493
Evan Cheng2e489c42009-12-16 00:53:11 +000014494static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14495 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14496 // (and (i32 x86isd::setcc_carry), 1)
14497 // This eliminates the zext. This transformation is necessary because
14498 // ISD::SETCC is always legalized to i8.
14499 DebugLoc dl = N->getDebugLoc();
14500 SDValue N0 = N->getOperand(0);
14501 EVT VT = N->getValueType(0);
14502 if (N0.getOpcode() == ISD::AND &&
14503 N0.hasOneUse() &&
14504 N0.getOperand(0).hasOneUse()) {
14505 SDValue N00 = N0.getOperand(0);
14506 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14507 return SDValue();
14508 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14509 if (!C || C->getZExtValue() != 1)
14510 return SDValue();
14511 return DAG.getNode(ISD::AND, dl, VT,
14512 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14513 N00.getOperand(0), N00.getOperand(1)),
14514 DAG.getConstant(1, VT));
14515 }
14516
14517 return SDValue();
14518}
14519
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014520// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14521static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14522 unsigned X86CC = N->getConstantOperandVal(0);
14523 SDValue EFLAG = N->getOperand(1);
14524 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014525
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014526 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14527 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14528 // cases.
14529 if (X86CC == X86::COND_B)
14530 return DAG.getNode(ISD::AND, DL, MVT::i8,
14531 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14532 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14533 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014534
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014535 return SDValue();
14536}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014537
Benjamin Kramer1396c402011-06-18 11:09:41 +000014538static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14539 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014540 SDValue Op0 = N->getOperand(0);
14541 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14542 // a 32-bit target where SSE doesn't support i64->FP operations.
14543 if (Op0.getOpcode() == ISD::LOAD) {
14544 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14545 EVT VT = Ld->getValueType(0);
14546 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14547 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14548 !XTLI->getSubtarget()->is64Bit() &&
14549 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014550 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14551 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014552 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14553 return FILDChain;
14554 }
14555 }
14556 return SDValue();
14557}
14558
Chris Lattner23a01992010-12-20 01:37:09 +000014559// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14560static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14561 X86TargetLowering::DAGCombinerInfo &DCI) {
14562 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14563 // the result is either zero or one (depending on the input carry bit).
14564 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14565 if (X86::isZeroNode(N->getOperand(0)) &&
14566 X86::isZeroNode(N->getOperand(1)) &&
14567 // We don't have a good way to replace an EFLAGS use, so only do this when
14568 // dead right now.
14569 SDValue(N, 1).use_empty()) {
14570 DebugLoc DL = N->getDebugLoc();
14571 EVT VT = N->getValueType(0);
14572 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14573 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14574 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14575 DAG.getConstant(X86::COND_B,MVT::i8),
14576 N->getOperand(2)),
14577 DAG.getConstant(1, VT));
14578 return DCI.CombineTo(N, Res1, CarryOut);
14579 }
14580
14581 return SDValue();
14582}
14583
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014584// fold (add Y, (sete X, 0)) -> adc 0, Y
14585// (add Y, (setne X, 0)) -> sbb -1, Y
14586// (sub (sete X, 0), Y) -> sbb 0, Y
14587// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014588static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014589 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014590
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014591 // Look through ZExts.
14592 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14593 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14594 return SDValue();
14595
14596 SDValue SetCC = Ext.getOperand(0);
14597 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14598 return SDValue();
14599
14600 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14601 if (CC != X86::COND_E && CC != X86::COND_NE)
14602 return SDValue();
14603
14604 SDValue Cmp = SetCC.getOperand(1);
14605 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014606 !X86::isZeroNode(Cmp.getOperand(1)) ||
14607 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014608 return SDValue();
14609
14610 SDValue CmpOp0 = Cmp.getOperand(0);
14611 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14612 DAG.getConstant(1, CmpOp0.getValueType()));
14613
14614 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14615 if (CC == X86::COND_NE)
14616 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14617 DL, OtherVal.getValueType(), OtherVal,
14618 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14619 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14620 DL, OtherVal.getValueType(), OtherVal,
14621 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14622}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014623
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014624static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
14625 SDValue Op0 = N->getOperand(0);
14626 SDValue Op1 = N->getOperand(1);
14627
14628 // X86 can't encode an immediate LHS of a sub. See if we can push the
14629 // negation into a preceding instruction.
14630 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014631 // If the RHS of the sub is a XOR with one use and a constant, invert the
14632 // immediate. Then add one to the LHS of the sub so we can turn
14633 // X-Y -> X+~Y+1, saving one register.
14634 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14635 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014636 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014637 EVT VT = Op0.getValueType();
14638 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14639 Op1.getOperand(0),
14640 DAG.getConstant(~XorC, VT));
14641 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014642 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014643 }
14644 }
14645
14646 return OptimizeConditionalInDecrement(N, DAG);
14647}
14648
Dan Gohman475871a2008-07-27 21:46:04 +000014649SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014650 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014651 SelectionDAG &DAG = DCI.DAG;
14652 switch (N->getOpcode()) {
14653 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014654 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014655 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014656 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014657 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014658 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014659 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
14660 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000014661 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014662 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014663 case ISD::SHL:
14664 case ISD::SRA:
14665 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014666 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014667 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014668 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014669 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014670 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014671 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014672 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14673 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014674 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014675 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14676 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014677 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014678 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014679 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014680 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014681 case X86ISD::SHUFPS: // Handle all target specific shuffles
14682 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014683 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014684 case X86ISD::PUNPCKHBW:
14685 case X86ISD::PUNPCKHWD:
14686 case X86ISD::PUNPCKHDQ:
14687 case X86ISD::PUNPCKHQDQ:
14688 case X86ISD::UNPCKHPS:
14689 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000014690 case X86ISD::VUNPCKHPSY:
14691 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014692 case X86ISD::PUNPCKLBW:
14693 case X86ISD::PUNPCKLWD:
14694 case X86ISD::PUNPCKLDQ:
14695 case X86ISD::PUNPCKLQDQ:
14696 case X86ISD::UNPCKLPS:
14697 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000014698 case X86ISD::VUNPCKLPSY:
14699 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014700 case X86ISD::MOVHLPS:
14701 case X86ISD::MOVLHPS:
14702 case X86ISD::PSHUFD:
14703 case X86ISD::PSHUFHW:
14704 case X86ISD::PSHUFLW:
14705 case X86ISD::MOVSS:
14706 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000014707 case X86ISD::VPERMILPS:
14708 case X86ISD::VPERMILPSY:
14709 case X86ISD::VPERMILPD:
14710 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000014711 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014712 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014713 }
14714
Dan Gohman475871a2008-07-27 21:46:04 +000014715 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014716}
14717
Evan Chenge5b51ac2010-04-17 06:13:15 +000014718/// isTypeDesirableForOp - Return true if the target has native support for
14719/// the specified value type and it is 'desirable' to use the type for the
14720/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14721/// instruction encodings are longer and some i16 instructions are slow.
14722bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14723 if (!isTypeLegal(VT))
14724 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014725 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014726 return true;
14727
14728 switch (Opc) {
14729 default:
14730 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014731 case ISD::LOAD:
14732 case ISD::SIGN_EXTEND:
14733 case ISD::ZERO_EXTEND:
14734 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014735 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014736 case ISD::SRL:
14737 case ISD::SUB:
14738 case ISD::ADD:
14739 case ISD::MUL:
14740 case ISD::AND:
14741 case ISD::OR:
14742 case ISD::XOR:
14743 return false;
14744 }
14745}
14746
14747/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014748/// beneficial for dag combiner to promote the specified node. If true, it
14749/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014750bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014751 EVT VT = Op.getValueType();
14752 if (VT != MVT::i16)
14753 return false;
14754
Evan Cheng4c26e932010-04-19 19:29:22 +000014755 bool Promote = false;
14756 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014757 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014758 default: break;
14759 case ISD::LOAD: {
14760 LoadSDNode *LD = cast<LoadSDNode>(Op);
14761 // If the non-extending load has a single use and it's not live out, then it
14762 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014763 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14764 Op.hasOneUse()*/) {
14765 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14766 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14767 // The only case where we'd want to promote LOAD (rather then it being
14768 // promoted as an operand is when it's only use is liveout.
14769 if (UI->getOpcode() != ISD::CopyToReg)
14770 return false;
14771 }
14772 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014773 Promote = true;
14774 break;
14775 }
14776 case ISD::SIGN_EXTEND:
14777 case ISD::ZERO_EXTEND:
14778 case ISD::ANY_EXTEND:
14779 Promote = true;
14780 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014781 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014782 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014783 SDValue N0 = Op.getOperand(0);
14784 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014785 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014786 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014787 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014788 break;
14789 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014790 case ISD::ADD:
14791 case ISD::MUL:
14792 case ISD::AND:
14793 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014794 case ISD::XOR:
14795 Commute = true;
14796 // fallthrough
14797 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014798 SDValue N0 = Op.getOperand(0);
14799 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014800 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014801 return false;
14802 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014803 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014804 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014805 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014806 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014807 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014808 }
14809 }
14810
14811 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014812 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014813}
14814
Evan Cheng60c07e12006-07-05 22:17:51 +000014815//===----------------------------------------------------------------------===//
14816// X86 Inline Assembly Support
14817//===----------------------------------------------------------------------===//
14818
Chris Lattnerb8105652009-07-20 17:51:36 +000014819bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14820 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014821
14822 std::string AsmStr = IA->getAsmString();
14823
14824 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014825 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014826 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014827
14828 switch (AsmPieces.size()) {
14829 default: return false;
14830 case 1:
14831 AsmStr = AsmPieces[0];
14832 AsmPieces.clear();
14833 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14834
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014835 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014836 // we will turn this bswap into something that will be lowered to logical ops
14837 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14838 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014839 // bswap $0
14840 if (AsmPieces.size() == 2 &&
14841 (AsmPieces[0] == "bswap" ||
14842 AsmPieces[0] == "bswapq" ||
14843 AsmPieces[0] == "bswapl") &&
14844 (AsmPieces[1] == "$0" ||
14845 AsmPieces[1] == "${0:q}")) {
14846 // No need to check constraints, nothing other than the equivalent of
14847 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014848 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014849 if (!Ty || Ty->getBitWidth() % 16 != 0)
14850 return false;
14851 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014852 }
14853 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014854 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014855 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014856 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014857 AsmPieces[1] == "$$8," &&
14858 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014859 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14860 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014861 const std::string &ConstraintsStr = IA->getConstraintString();
14862 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014863 std::sort(AsmPieces.begin(), AsmPieces.end());
14864 if (AsmPieces.size() == 4 &&
14865 AsmPieces[0] == "~{cc}" &&
14866 AsmPieces[1] == "~{dirflag}" &&
14867 AsmPieces[2] == "~{flags}" &&
14868 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014869 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014870 if (!Ty || Ty->getBitWidth() % 16 != 0)
14871 return false;
14872 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014873 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014874 }
14875 break;
14876 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014877 if (CI->getType()->isIntegerTy(32) &&
14878 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14879 SmallVector<StringRef, 4> Words;
14880 SplitString(AsmPieces[0], Words, " \t,");
14881 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14882 Words[2] == "${0:w}") {
14883 Words.clear();
14884 SplitString(AsmPieces[1], Words, " \t,");
14885 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14886 Words[2] == "$0") {
14887 Words.clear();
14888 SplitString(AsmPieces[2], Words, " \t,");
14889 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14890 Words[2] == "${0:w}") {
14891 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014892 const std::string &ConstraintsStr = IA->getConstraintString();
14893 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014894 std::sort(AsmPieces.begin(), AsmPieces.end());
14895 if (AsmPieces.size() == 4 &&
14896 AsmPieces[0] == "~{cc}" &&
14897 AsmPieces[1] == "~{dirflag}" &&
14898 AsmPieces[2] == "~{flags}" &&
14899 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014900 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014901 if (!Ty || Ty->getBitWidth() % 16 != 0)
14902 return false;
14903 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014904 }
14905 }
14906 }
14907 }
14908 }
Evan Cheng55d42002011-01-08 01:24:27 +000014909
14910 if (CI->getType()->isIntegerTy(64)) {
14911 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14912 if (Constraints.size() >= 2 &&
14913 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14914 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14915 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14916 SmallVector<StringRef, 4> Words;
14917 SplitString(AsmPieces[0], Words, " \t");
14918 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014919 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014920 SplitString(AsmPieces[1], Words, " \t");
14921 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14922 Words.clear();
14923 SplitString(AsmPieces[2], Words, " \t,");
14924 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14925 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014926 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014927 if (!Ty || Ty->getBitWidth() % 16 != 0)
14928 return false;
14929 return IntrinsicLowering::LowerToByteSwap(CI);
14930 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014931 }
14932 }
14933 }
14934 }
14935 break;
14936 }
14937 return false;
14938}
14939
14940
14941
Chris Lattnerf4dff842006-07-11 02:54:03 +000014942/// getConstraintType - Given a constraint letter, return the type of
14943/// constraint it is for this target.
14944X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014945X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14946 if (Constraint.size() == 1) {
14947 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014948 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014949 case 'q':
14950 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014951 case 'f':
14952 case 't':
14953 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014954 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014955 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014956 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014957 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014958 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014959 case 'a':
14960 case 'b':
14961 case 'c':
14962 case 'd':
14963 case 'S':
14964 case 'D':
14965 case 'A':
14966 return C_Register;
14967 case 'I':
14968 case 'J':
14969 case 'K':
14970 case 'L':
14971 case 'M':
14972 case 'N':
14973 case 'G':
14974 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014975 case 'e':
14976 case 'Z':
14977 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014978 default:
14979 break;
14980 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014981 }
Chris Lattner4234f572007-03-25 02:14:49 +000014982 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014983}
14984
John Thompson44ab89e2010-10-29 17:29:13 +000014985/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014986/// This object must already have been set up with the operand type
14987/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014988TargetLowering::ConstraintWeight
14989 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014990 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014991 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014992 Value *CallOperandVal = info.CallOperandVal;
14993 // If we don't have a value, we can't do a match,
14994 // but allow it at the lowest weight.
14995 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014996 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014997 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014998 // Look at the constraint type.
14999 switch (*constraint) {
15000 default:
John Thompson44ab89e2010-10-29 17:29:13 +000015001 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
15002 case 'R':
15003 case 'q':
15004 case 'Q':
15005 case 'a':
15006 case 'b':
15007 case 'c':
15008 case 'd':
15009 case 'S':
15010 case 'D':
15011 case 'A':
15012 if (CallOperandVal->getType()->isIntegerTy())
15013 weight = CW_SpecificReg;
15014 break;
15015 case 'f':
15016 case 't':
15017 case 'u':
15018 if (type->isFloatingPointTy())
15019 weight = CW_SpecificReg;
15020 break;
15021 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000015022 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000015023 weight = CW_SpecificReg;
15024 break;
15025 case 'x':
15026 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015027 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000015028 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015029 break;
15030 case 'I':
15031 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
15032 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000015033 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015034 }
15035 break;
John Thompson44ab89e2010-10-29 17:29:13 +000015036 case 'J':
15037 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15038 if (C->getZExtValue() <= 63)
15039 weight = CW_Constant;
15040 }
15041 break;
15042 case 'K':
15043 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15044 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
15045 weight = CW_Constant;
15046 }
15047 break;
15048 case 'L':
15049 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15050 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
15051 weight = CW_Constant;
15052 }
15053 break;
15054 case 'M':
15055 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15056 if (C->getZExtValue() <= 3)
15057 weight = CW_Constant;
15058 }
15059 break;
15060 case 'N':
15061 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15062 if (C->getZExtValue() <= 0xff)
15063 weight = CW_Constant;
15064 }
15065 break;
15066 case 'G':
15067 case 'C':
15068 if (dyn_cast<ConstantFP>(CallOperandVal)) {
15069 weight = CW_Constant;
15070 }
15071 break;
15072 case 'e':
15073 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15074 if ((C->getSExtValue() >= -0x80000000LL) &&
15075 (C->getSExtValue() <= 0x7fffffffLL))
15076 weight = CW_Constant;
15077 }
15078 break;
15079 case 'Z':
15080 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15081 if (C->getZExtValue() <= 0xffffffff)
15082 weight = CW_Constant;
15083 }
15084 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015085 }
15086 return weight;
15087}
15088
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015089/// LowerXConstraint - try to replace an X constraint, which matches anything,
15090/// with another that has more specific requirements based on the type of the
15091/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000015092const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000015093LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000015094 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
15095 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000015096 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015097 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000015098 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015099 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000015100 return "x";
15101 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015102
Chris Lattner5e764232008-04-26 23:02:14 +000015103 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015104}
15105
Chris Lattner48884cd2007-08-25 00:47:38 +000015106/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
15107/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000015108void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000015109 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000015110 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000015111 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000015112 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000015113
Eric Christopher100c8332011-06-02 23:16:42 +000015114 // Only support length 1 constraints for now.
15115 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000015116
Eric Christopher100c8332011-06-02 23:16:42 +000015117 char ConstraintLetter = Constraint[0];
15118 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015119 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000015120 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000015121 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015122 if (C->getZExtValue() <= 31) {
15123 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015124 break;
15125 }
Devang Patel84f7fd22007-03-17 00:13:28 +000015126 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015127 return;
Evan Cheng364091e2008-09-22 23:57:37 +000015128 case 'J':
15129 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015130 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000015131 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15132 break;
15133 }
15134 }
15135 return;
15136 case 'K':
15137 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015138 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000015139 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15140 break;
15141 }
15142 }
15143 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000015144 case 'N':
15145 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015146 if (C->getZExtValue() <= 255) {
15147 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015148 break;
15149 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000015150 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015151 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000015152 case 'e': {
15153 // 32-bit signed value
15154 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015155 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15156 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015157 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015158 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000015159 break;
15160 }
15161 // FIXME gcc accepts some relocatable values here too, but only in certain
15162 // memory models; it's complicated.
15163 }
15164 return;
15165 }
15166 case 'Z': {
15167 // 32-bit unsigned value
15168 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015169 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15170 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015171 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15172 break;
15173 }
15174 }
15175 // FIXME gcc accepts some relocatable values here too, but only in certain
15176 // memory models; it's complicated.
15177 return;
15178 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015179 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015180 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000015181 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015182 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015183 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000015184 break;
15185 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015186
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015187 // In any sort of PIC mode addresses need to be computed at runtime by
15188 // adding in a register or some sort of table lookup. These can't
15189 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000015190 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015191 return;
15192
Chris Lattnerdc43a882007-05-03 16:52:29 +000015193 // If we are in non-pic codegen mode, we allow the address of a global (with
15194 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000015195 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015196 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000015197
Chris Lattner49921962009-05-08 18:23:14 +000015198 // Match either (GA), (GA+C), (GA+C1+C2), etc.
15199 while (1) {
15200 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15201 Offset += GA->getOffset();
15202 break;
15203 } else if (Op.getOpcode() == ISD::ADD) {
15204 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15205 Offset += C->getZExtValue();
15206 Op = Op.getOperand(0);
15207 continue;
15208 }
15209 } else if (Op.getOpcode() == ISD::SUB) {
15210 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15211 Offset += -C->getZExtValue();
15212 Op = Op.getOperand(0);
15213 continue;
15214 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015215 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015216
Chris Lattner49921962009-05-08 18:23:14 +000015217 // Otherwise, this isn't something we can handle, reject it.
15218 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015219 }
Eric Christopherfd179292009-08-27 18:07:15 +000015220
Dan Gohman46510a72010-04-15 01:51:59 +000015221 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015222 // If we require an extra load to get this address, as in PIC mode, we
15223 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015224 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15225 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015226 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015227
Devang Patel0d881da2010-07-06 22:08:15 +000015228 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15229 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015230 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015231 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015232 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015233
Gabor Greifba36cb52008-08-28 21:40:38 +000015234 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015235 Ops.push_back(Result);
15236 return;
15237 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015238 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015239}
15240
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015241std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015242X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015243 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015244 // First, see if this is a constraint that directly corresponds to an LLVM
15245 // register class.
15246 if (Constraint.size() == 1) {
15247 // GCC Constraint Letters
15248 switch (Constraint[0]) {
15249 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015250 // TODO: Slight differences here in allocation order and leaving
15251 // RIP in the class. Do they matter any more here than they do
15252 // in the normal allocation?
15253 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15254 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015255 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015256 return std::make_pair(0U, X86::GR32RegisterClass);
15257 else if (VT == MVT::i16)
15258 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015259 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015260 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015261 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015262 return std::make_pair(0U, X86::GR64RegisterClass);
15263 break;
15264 }
15265 // 32-bit fallthrough
15266 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015267 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015268 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
15269 else if (VT == MVT::i16)
15270 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015271 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015272 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
15273 else if (VT == MVT::i64)
15274 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15275 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015276 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015277 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015278 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015279 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015280 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015281 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015282 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015283 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015284 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015285 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015286 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015287 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15288 if (VT == MVT::i16)
15289 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15290 if (VT == MVT::i32 || !Subtarget->is64Bit())
15291 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15292 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015293 case 'f': // FP Stack registers.
15294 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15295 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015296 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015297 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015298 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015299 return std::make_pair(0U, X86::RFP64RegisterClass);
15300 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015301 case 'y': // MMX_REGS if MMX allowed.
15302 if (!Subtarget->hasMMX()) break;
15303 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015304 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015305 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015306 // FALL THROUGH.
15307 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015308 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015309
Owen Anderson825b72b2009-08-11 20:47:22 +000015310 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015311 default: break;
15312 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015313 case MVT::f32:
15314 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015315 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015316 case MVT::f64:
15317 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015318 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015319 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015320 case MVT::v16i8:
15321 case MVT::v8i16:
15322 case MVT::v4i32:
15323 case MVT::v2i64:
15324 case MVT::v4f32:
15325 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015326 return std::make_pair(0U, X86::VR128RegisterClass);
15327 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015328 break;
15329 }
15330 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015331
Chris Lattnerf76d1802006-07-31 23:26:50 +000015332 // Use the default implementation in TargetLowering to convert the register
15333 // constraint into a member of a register class.
15334 std::pair<unsigned, const TargetRegisterClass*> Res;
15335 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015336
15337 // Not found as a standard register?
15338 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015339 // Map st(0) -> st(7) -> ST0
15340 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15341 tolower(Constraint[1]) == 's' &&
15342 tolower(Constraint[2]) == 't' &&
15343 Constraint[3] == '(' &&
15344 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15345 Constraint[5] == ')' &&
15346 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015347
Chris Lattner56d77c72009-09-13 22:41:48 +000015348 Res.first = X86::ST0+Constraint[4]-'0';
15349 Res.second = X86::RFP80RegisterClass;
15350 return Res;
15351 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015352
Chris Lattner56d77c72009-09-13 22:41:48 +000015353 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015354 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015355 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015356 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015357 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015358 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015359
15360 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015361 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015362 Res.first = X86::EFLAGS;
15363 Res.second = X86::CCRRegisterClass;
15364 return Res;
15365 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015366
Dale Johannesen330169f2008-11-13 21:52:36 +000015367 // 'A' means EAX + EDX.
15368 if (Constraint == "A") {
15369 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015370 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015371 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015372 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015373 return Res;
15374 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015375
Chris Lattnerf76d1802006-07-31 23:26:50 +000015376 // Otherwise, check to see if this is a register class of the wrong value
15377 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15378 // turn into {ax},{dx}.
15379 if (Res.second->hasType(VT))
15380 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015381
Chris Lattnerf76d1802006-07-31 23:26:50 +000015382 // All of the single-register GCC register classes map their values onto
15383 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15384 // really want an 8-bit or 32-bit register, map to the appropriate register
15385 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015386 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015387 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015388 unsigned DestReg = 0;
15389 switch (Res.first) {
15390 default: break;
15391 case X86::AX: DestReg = X86::AL; break;
15392 case X86::DX: DestReg = X86::DL; break;
15393 case X86::CX: DestReg = X86::CL; break;
15394 case X86::BX: DestReg = X86::BL; break;
15395 }
15396 if (DestReg) {
15397 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015398 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015399 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015400 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015401 unsigned DestReg = 0;
15402 switch (Res.first) {
15403 default: break;
15404 case X86::AX: DestReg = X86::EAX; break;
15405 case X86::DX: DestReg = X86::EDX; break;
15406 case X86::CX: DestReg = X86::ECX; break;
15407 case X86::BX: DestReg = X86::EBX; break;
15408 case X86::SI: DestReg = X86::ESI; break;
15409 case X86::DI: DestReg = X86::EDI; break;
15410 case X86::BP: DestReg = X86::EBP; break;
15411 case X86::SP: DestReg = X86::ESP; break;
15412 }
15413 if (DestReg) {
15414 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015415 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015416 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015417 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015418 unsigned DestReg = 0;
15419 switch (Res.first) {
15420 default: break;
15421 case X86::AX: DestReg = X86::RAX; break;
15422 case X86::DX: DestReg = X86::RDX; break;
15423 case X86::CX: DestReg = X86::RCX; break;
15424 case X86::BX: DestReg = X86::RBX; break;
15425 case X86::SI: DestReg = X86::RSI; break;
15426 case X86::DI: DestReg = X86::RDI; break;
15427 case X86::BP: DestReg = X86::RBP; break;
15428 case X86::SP: DestReg = X86::RSP; break;
15429 }
15430 if (DestReg) {
15431 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015432 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015433 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015434 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015435 } else if (Res.second == X86::FR32RegisterClass ||
15436 Res.second == X86::FR64RegisterClass ||
15437 Res.second == X86::VR128RegisterClass) {
15438 // Handle references to XMM physical registers that got mapped into the
15439 // wrong class. This can happen with constraints like {xmm0} where the
15440 // target independent register mapper will just pick the first match it can
15441 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015442 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015443 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015444 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015445 Res.second = X86::FR64RegisterClass;
15446 else if (X86::VR128RegisterClass->hasType(VT))
15447 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015448 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015449
Chris Lattnerf76d1802006-07-31 23:26:50 +000015450 return Res;
15451}