blob: dbf9ef0193298e3bbeaa66e6a235e34a1c07027f [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 Lopesaed890b2011-08-01 21:54:05 +0000172 X86ScalarSSEf64 = Subtarget->hasXMMInt() || Subtarget->hasAVX();
173 X86ScalarSSEf32 = Subtarget->hasXMM() || Subtarget->hasAVX();
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) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000318 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000319 // Expand FP_TO_UINT into a select.
320 // FIXME: We would like to use a Custom expander here eventually to do
321 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000323 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000324 // With SSE3 we can use fisttpll to convert to a signed i64; without
325 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000327 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000328
Chris Lattner399610a2006-12-05 18:22:22 +0000329 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000330 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000331 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
332 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000333 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000335 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000336 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000337 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000338 }
Chris Lattner21f66852005-12-23 05:15:23 +0000339
Dan Gohmanb00ee212008-02-18 19:34:53 +0000340 // Scalar integer divide and remainder are lowered to use operations that
341 // produce two results, to match the available instructions. This exposes
342 // the two-result form to trivial CSE, which is able to combine x/y and x%y
343 // into a single instruction.
344 //
345 // Scalar integer multiply-high is also lowered to use two-result
346 // operations, to match the available instructions. However, plain multiply
347 // (low) operations are left as Legal, as there are single-result
348 // instructions for this in x86. Using the two-result multiply instructions
349 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000350 for (unsigned i = 0, e = 4; i != e; ++i) {
351 MVT VT = IntVTs[i];
352 setOperationAction(ISD::MULHS, VT, Expand);
353 setOperationAction(ISD::MULHU, VT, Expand);
354 setOperationAction(ISD::SDIV, VT, Expand);
355 setOperationAction(ISD::UDIV, VT, Expand);
356 setOperationAction(ISD::SREM, VT, Expand);
357 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000358
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000359 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000360 setOperationAction(ISD::ADDC, VT, Custom);
361 setOperationAction(ISD::ADDE, VT, Custom);
362 setOperationAction(ISD::SUBC, VT, Custom);
363 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000364 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000365
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
367 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
368 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
369 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000370 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
375 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
376 setOperationAction(ISD::FREM , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f64 , Expand);
378 setOperationAction(ISD::FREM , MVT::f80 , Expand);
379 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000380
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
382 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000383 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
384 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
386 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000387 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
389 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000390 }
391
Benjamin Kramer1292c222010-12-04 20:32:23 +0000392 if (Subtarget->hasPOPCNT()) {
393 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
394 } else {
395 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
396 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
397 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
398 if (Subtarget->is64Bit())
399 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
400 }
401
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
403 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000404
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000405 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000406 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000407 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000408 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000409 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
411 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
412 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
413 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
414 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000415 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
417 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
418 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
419 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000420 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000422 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000423 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000425
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000426 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
428 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
429 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
430 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000431 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000432 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
433 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000434 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000435 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
437 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
438 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
439 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000440 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000441 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000442 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
444 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
445 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000446 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
448 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
449 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000450 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000451
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000452 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000454
Eric Christopher9a9d2752010-07-22 02:48:34 +0000455 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000456 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000457
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000458 // On X86 and X86-64, atomic operations are lowered to locked instructions.
459 // Locked instructions, in turn, have implicit fence semantics (all memory
460 // operations are flushed before issuing the locked instruction, and they
461 // are not buffered), so we can fold away the common pattern of
462 // fence-atomic-fence.
463 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000464
Mon P Wang63307c32008-05-05 19:05:59 +0000465 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000466 for (unsigned i = 0, e = 4; i != e; ++i) {
467 MVT VT = IntVTs[i];
468 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
469 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000470 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000471 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000472
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000473 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000474 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
476 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
477 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
478 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
479 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
480 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
481 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000482 }
483
Eli Friedman43f51ae2011-08-26 21:21:21 +0000484 if (Subtarget->hasCmpxchg16b()) {
485 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
486 }
487
Evan Cheng3c992d22006-03-07 02:02:57 +0000488 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000489 if (!Subtarget->isTargetDarwin() &&
490 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000491 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000493 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000494
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
496 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
497 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
498 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000499 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000500 setExceptionPointerRegister(X86::RAX);
501 setExceptionSelectorRegister(X86::RDX);
502 } else {
503 setExceptionPointerRegister(X86::EAX);
504 setExceptionSelectorRegister(X86::EDX);
505 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
507 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000508
Duncan Sands4a544a72011-09-06 13:37:06 +0000509 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
510 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000511
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000513
Nate Begemanacc398c2006-01-25 18:21:52 +0000514 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::VASTART , MVT::Other, Custom);
516 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000517 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 setOperationAction(ISD::VAARG , MVT::Other, Custom);
519 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000520 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::VAARG , MVT::Other, Expand);
522 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000523 }
Evan Chengae642192007-03-02 23:16:35 +0000524
Owen Anderson825b72b2009-08-11 20:47:22 +0000525 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
526 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000527
528 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
529 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
530 MVT::i64 : MVT::i32, Custom);
531 else if (EnableSegmentedStacks)
532 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
533 MVT::i64 : MVT::i32, Custom);
534 else
535 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
536 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000537
Evan Chengc7ce29b2009-02-13 22:36:38 +0000538 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000539 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000540 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
542 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000543
Evan Cheng223547a2006-01-31 22:28:30 +0000544 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 setOperationAction(ISD::FABS , MVT::f64, Custom);
546 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000547
548 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 setOperationAction(ISD::FNEG , MVT::f64, Custom);
550 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000551
Evan Cheng68c47cb2007-01-05 07:55:56 +0000552 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
554 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000555
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000556 // Lower this to FGETSIGNx86 plus an AND.
557 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
558 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
559
Evan Chengd25e9e82006-02-02 00:28:23 +0000560 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000561 setOperationAction(ISD::FSIN , MVT::f64, Expand);
562 setOperationAction(ISD::FCOS , MVT::f64, Expand);
563 setOperationAction(ISD::FSIN , MVT::f32, Expand);
564 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000565
Chris Lattnera54aa942006-01-29 06:26:08 +0000566 // Expand FP immediates into loads from the stack, except for the special
567 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000568 addLegalFPImmediate(APFloat(+0.0)); // xorpd
569 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000570 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000571 // Use SSE for f32, x87 for f64.
572 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
574 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000575
576 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000578
579 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000581
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000583
584 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
586 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000587
588 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 setOperationAction(ISD::FSIN , MVT::f32, Expand);
590 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000591
Nate Begemane1795842008-02-14 08:57:00 +0000592 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000593 addLegalFPImmediate(APFloat(+0.0f)); // xorps
594 addLegalFPImmediate(APFloat(+0.0)); // FLD0
595 addLegalFPImmediate(APFloat(+1.0)); // FLD1
596 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
597 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
598
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
601 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000603 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000604 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000605 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
607 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000608
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
610 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
611 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
612 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000613
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
616 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000617 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000618 addLegalFPImmediate(APFloat(+0.0)); // FLD0
619 addLegalFPImmediate(APFloat(+1.0)); // FLD1
620 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
621 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
623 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
624 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
625 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000627
Cameron Zwarich33390842011-07-08 21:39:21 +0000628 // We don't support FMA.
629 setOperationAction(ISD::FMA, MVT::f64, Expand);
630 setOperationAction(ISD::FMA, MVT::f32, Expand);
631
Dale Johannesen59a58732007-08-05 18:49:15 +0000632 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000633 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
635 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
636 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000637 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000638 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000639 addLegalFPImmediate(TmpFlt); // FLD0
640 TmpFlt.changeSign();
641 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000642
643 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000644 APFloat TmpFlt2(+1.0);
645 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
646 &ignored);
647 addLegalFPImmediate(TmpFlt2); // FLD1
648 TmpFlt2.changeSign();
649 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
650 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000651
Evan Chengc7ce29b2009-02-13 22:36:38 +0000652 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
654 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000655 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000656
657 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000658 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000659
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000660 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
662 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
663 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000664
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::FLOG, MVT::f80, Expand);
666 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
667 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
668 setOperationAction(ISD::FEXP, MVT::f80, Expand);
669 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000670
Mon P Wangf007a8b2008-11-06 05:31:54 +0000671 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000672 // (for widening) or expand (for scalarization). Then we will selectively
673 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
675 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
676 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
677 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
678 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
679 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
680 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
681 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
682 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
683 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
684 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
685 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000691 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000692 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
693 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000715 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000725 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000726 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
730 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
731 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
732 setTruncStoreAction((MVT::SimpleValueType)VT,
733 (MVT::SimpleValueType)InnerVT, Expand);
734 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
735 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
736 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000737 }
738
Evan Chengc7ce29b2009-02-13 22:36:38 +0000739 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
740 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000741 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000742 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000743 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000744 }
745
Dale Johannesen0488fb62010-09-30 23:57:10 +0000746 // MMX-sized vectors (other than x86mmx) are expected to be expanded
747 // into smaller operations.
748 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
749 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
750 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
751 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
752 setOperationAction(ISD::AND, MVT::v8i8, Expand);
753 setOperationAction(ISD::AND, MVT::v4i16, Expand);
754 setOperationAction(ISD::AND, MVT::v2i32, Expand);
755 setOperationAction(ISD::AND, MVT::v1i64, Expand);
756 setOperationAction(ISD::OR, MVT::v8i8, Expand);
757 setOperationAction(ISD::OR, MVT::v4i16, Expand);
758 setOperationAction(ISD::OR, MVT::v2i32, Expand);
759 setOperationAction(ISD::OR, MVT::v1i64, Expand);
760 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
761 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
762 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
763 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
764 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
765 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
766 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
767 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
768 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
769 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
770 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
771 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
772 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000773 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
774 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
775 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
776 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000777
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000778 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000779 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000780
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
782 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
783 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
784 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
785 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
786 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
787 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
788 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
789 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
790 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
791 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000792 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000793 }
794
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000795 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000797
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000798 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
799 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000800 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
801 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
802 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
803 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000804
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
806 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
807 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
808 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
809 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
810 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
811 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
812 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
813 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
814 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
815 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
816 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
817 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
818 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
819 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
820 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000821
Duncan Sands28b77e92011-09-06 19:07:46 +0000822 setOperationAction(ISD::SETCC, MVT::v2f64, Custom);
823 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
824 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
825 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000826
Owen Anderson825b72b2009-08-11 20:47:22 +0000827 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
828 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
829 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
830 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
831 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000832
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000833 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
834 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
835 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
836 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
837 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
838
Evan Cheng2c3ae372006-04-12 21:21:57 +0000839 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
841 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000842 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000843 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000844 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000845 // Do not attempt to custom lower non-128-bit vectors
846 if (!VT.is128BitVector())
847 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000848 setOperationAction(ISD::BUILD_VECTOR,
849 VT.getSimpleVT().SimpleTy, Custom);
850 setOperationAction(ISD::VECTOR_SHUFFLE,
851 VT.getSimpleVT().SimpleTy, Custom);
852 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
853 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000854 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000855
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
857 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
858 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
859 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
860 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
861 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000862
Nate Begemancdd1eec2008-02-12 22:51:28 +0000863 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000864 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
865 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000866 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000867
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000868 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000869 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
870 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000871 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000872
873 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000874 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000875 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000876
Owen Andersond6662ad2009-08-10 20:46:15 +0000877 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000878 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000879 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000881 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000882 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000883 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000885 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000887 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000888
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000890
Evan Cheng2c3ae372006-04-12 21:21:57 +0000891 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
893 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
894 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
895 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000896
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
898 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000899 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000900
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000901 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000902 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
903 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
904 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
905 setOperationAction(ISD::FRINT, MVT::f32, Legal);
906 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
907 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
908 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
909 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
910 setOperationAction(ISD::FRINT, MVT::f64, Legal);
911 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
912
Nate Begeman14d12ca2008-02-11 04:19:36 +0000913 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000915
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000916 // Can turn SHL into an integer multiply.
917 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000918 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000919
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000920 setOperationAction(ISD::VSELECT, MVT::v2f64, Custom);
921 setOperationAction(ISD::VSELECT, MVT::v2i64, Custom);
922 setOperationAction(ISD::VSELECT, MVT::v16i8, Custom);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000923 setOperationAction(ISD::VSELECT, MVT::v4i32, Custom);
924 setOperationAction(ISD::VSELECT, MVT::v4f32, Custom);
925
Nate Begeman14d12ca2008-02-11 04:19:36 +0000926 // i8 and i16 vectors are custom , because the source register and source
927 // source memory operand types are not the same width. f32 vectors are
928 // custom since the immediate controlling the insert encodes additional
929 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
931 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
932 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
933 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000934
Owen Anderson825b72b2009-08-11 20:47:22 +0000935 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
936 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
937 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
938 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000939
940 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
942 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000943 }
944 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000945
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000946 if (Subtarget->hasSSE2() || Subtarget->hasAVX()) {
Nadav Rotem43012222011-05-11 08:12:09 +0000947 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
948 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
949 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000950 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000951
952 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
953 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
954 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
955
956 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
957 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
958 }
959
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000960 if (Subtarget->hasSSE42() || Subtarget->hasAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000961 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000962
David Greene9b9838d2009-06-29 16:47:10 +0000963 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000964 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
965 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
966 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
967 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
968 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
969 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000970
Owen Anderson825b72b2009-08-11 20:47:22 +0000971 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000972 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
973 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000974
Owen Anderson825b72b2009-08-11 20:47:22 +0000975 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
976 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
977 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
978 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
979 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
980 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000981
Owen Anderson825b72b2009-08-11 20:47:22 +0000982 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
983 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
984 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
985 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
986 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
987 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000988
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000989 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
990 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +0000991 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000992
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +0000993 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
994 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
995 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
996 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
997 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
998 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
999
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001000 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1001 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1002 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1003 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1004
1005 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1006 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1007 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1008 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1009
1010 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1011 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
1012
Duncan Sands28b77e92011-09-06 19:07:46 +00001013 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1014 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1015 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1016 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001017
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001018 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1019 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1020 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1021
Nadav Rotem8ffad562011-09-09 20:29:17 +00001022 setOperationAction(ISD::VSELECT, MVT::v4f64, Custom);
1023 setOperationAction(ISD::VSELECT, MVT::v4i64, Custom);
1024 setOperationAction(ISD::VSELECT, MVT::v8i32, Custom);
1025 setOperationAction(ISD::VSELECT, MVT::v8f32, Custom);
1026
Craig Topper13894fa2011-08-24 06:14:18 +00001027 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1028 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1029 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1030 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1031
1032 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1033 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1034 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1035 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1036
1037 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1038 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1039 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1040 // Don't lower v32i8 because there is no 128-bit byte mul
1041
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001042 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001043 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001044 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1045 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1046 EVT VT = SVT;
1047
1048 // Extract subvector is special because the value type
1049 // (result) is 128-bit but the source is 256-bit wide.
1050 if (VT.is128BitVector())
1051 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1052
1053 // Do not attempt to custom lower other non-256-bit vectors
1054 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001055 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001056
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001057 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1058 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1059 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1060 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001061 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001062 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001063 }
1064
David Greene54d8eba2011-01-27 22:38:56 +00001065 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001066 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1067 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1068 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001069
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001070 // Do not attempt to promote non-256-bit vectors
1071 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001072 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001073
1074 setOperationAction(ISD::AND, SVT, Promote);
1075 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1076 setOperationAction(ISD::OR, SVT, Promote);
1077 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1078 setOperationAction(ISD::XOR, SVT, Promote);
1079 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1080 setOperationAction(ISD::LOAD, SVT, Promote);
1081 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1082 setOperationAction(ISD::SELECT, SVT, Promote);
1083 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001084 }
David Greene9b9838d2009-06-29 16:47:10 +00001085 }
1086
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001087 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1088 // of this type with custom code.
1089 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1090 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1091 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1092 }
1093
Evan Cheng6be2c582006-04-05 23:38:46 +00001094 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001095 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001096
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001097
Eli Friedman962f5492010-06-02 19:35:46 +00001098 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1099 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001100 //
Eli Friedman962f5492010-06-02 19:35:46 +00001101 // FIXME: We really should do custom legalization for addition and
1102 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1103 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001104 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1105 // Add/Sub/Mul with overflow operations are custom lowered.
1106 MVT VT = IntVTs[i];
1107 setOperationAction(ISD::SADDO, VT, Custom);
1108 setOperationAction(ISD::UADDO, VT, Custom);
1109 setOperationAction(ISD::SSUBO, VT, Custom);
1110 setOperationAction(ISD::USUBO, VT, Custom);
1111 setOperationAction(ISD::SMULO, VT, Custom);
1112 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001113 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001114
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001115 // There are no 8-bit 3-address imul/mul instructions
1116 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1117 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001118
Evan Chengd54f2d52009-03-31 19:38:51 +00001119 if (!Subtarget->is64Bit()) {
1120 // These libcalls are not available in 32-bit.
1121 setLibcallName(RTLIB::SHL_I128, 0);
1122 setLibcallName(RTLIB::SRL_I128, 0);
1123 setLibcallName(RTLIB::SRA_I128, 0);
1124 }
1125
Evan Cheng206ee9d2006-07-07 08:33:52 +00001126 // We have target-specific dag combine patterns for the following nodes:
1127 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001128 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001129 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001130 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001131 setTargetDAGCombine(ISD::SHL);
1132 setTargetDAGCombine(ISD::SRA);
1133 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001134 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001135 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001136 setTargetDAGCombine(ISD::ADD);
1137 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001138 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001139 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001140 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001141 if (Subtarget->is64Bit())
1142 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001143
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001144 computeRegisterProperties();
1145
Evan Cheng05219282011-01-06 06:52:41 +00001146 // On Darwin, -Os means optimize for size without hurting performance,
1147 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001148 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001149 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001150 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001151 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1152 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1153 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001154 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001155 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001156
1157 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001158}
1159
Scott Michel5b8f82e2008-03-10 15:42:14 +00001160
Duncan Sands28b77e92011-09-06 19:07:46 +00001161EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1162 if (!VT.isVector()) return MVT::i8;
1163 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001164}
1165
1166
Evan Cheng29286502008-01-23 23:17:41 +00001167/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1168/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001169static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001170 if (MaxAlign == 16)
1171 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001172 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001173 if (VTy->getBitWidth() == 128)
1174 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001175 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001176 unsigned EltAlign = 0;
1177 getMaxByValAlign(ATy->getElementType(), EltAlign);
1178 if (EltAlign > MaxAlign)
1179 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001180 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001181 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1182 unsigned EltAlign = 0;
1183 getMaxByValAlign(STy->getElementType(i), EltAlign);
1184 if (EltAlign > MaxAlign)
1185 MaxAlign = EltAlign;
1186 if (MaxAlign == 16)
1187 break;
1188 }
1189 }
1190 return;
1191}
1192
1193/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1194/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001195/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1196/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001197unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001198 if (Subtarget->is64Bit()) {
1199 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001200 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001201 if (TyAlign > 8)
1202 return TyAlign;
1203 return 8;
1204 }
1205
Evan Cheng29286502008-01-23 23:17:41 +00001206 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001207 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001208 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001209 return Align;
1210}
Chris Lattner2b02a442007-02-25 08:29:00 +00001211
Evan Chengf0df0312008-05-15 08:39:06 +00001212/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001213/// and store operations as a result of memset, memcpy, and memmove
1214/// lowering. If DstAlign is zero that means it's safe to destination
1215/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1216/// means there isn't a need to check it against alignment requirement,
1217/// probably because the source does not need to be loaded. If
1218/// 'NonScalarIntSafe' is true, that means it's safe to return a
1219/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1220/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1221/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001222/// It returns EVT::Other if the type should be determined using generic
1223/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001224EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001225X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1226 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001227 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001228 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001229 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001230 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1231 // linux. This is because the stack realignment code can't handle certain
1232 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001233 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001234 if (NonScalarIntSafe &&
1235 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001236 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001237 (Subtarget->isUnalignedMemAccessFast() ||
1238 ((DstAlign == 0 || DstAlign >= 16) &&
1239 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001240 Subtarget->getStackAlignment() >= 16) {
1241 if (Subtarget->hasSSE2())
1242 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001243 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001244 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001245 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001246 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001247 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001248 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001249 // Do not use f64 to lower memcpy if source is string constant. It's
1250 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001251 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001252 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001253 }
Evan Chengf0df0312008-05-15 08:39:06 +00001254 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001255 return MVT::i64;
1256 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001257}
1258
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001259/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1260/// current function. The returned value is a member of the
1261/// MachineJumpTableInfo::JTEntryKind enum.
1262unsigned X86TargetLowering::getJumpTableEncoding() const {
1263 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1264 // symbol.
1265 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1266 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001267 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001268
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001269 // Otherwise, use the normal jump table encoding heuristics.
1270 return TargetLowering::getJumpTableEncoding();
1271}
1272
Chris Lattnerc64daab2010-01-26 05:02:42 +00001273const MCExpr *
1274X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1275 const MachineBasicBlock *MBB,
1276 unsigned uid,MCContext &Ctx) const{
1277 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1278 Subtarget->isPICStyleGOT());
1279 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1280 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001281 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1282 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001283}
1284
Evan Chengcc415862007-11-09 01:32:10 +00001285/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1286/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001287SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001288 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001289 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001290 // This doesn't have DebugLoc associated with it, but is not really the
1291 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001292 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001293 return Table;
1294}
1295
Chris Lattner589c6f62010-01-26 06:28:43 +00001296/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1297/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1298/// MCExpr.
1299const MCExpr *X86TargetLowering::
1300getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1301 MCContext &Ctx) const {
1302 // X86-64 uses RIP relative addressing based on the jump table label.
1303 if (Subtarget->isPICStyleRIPRel())
1304 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1305
1306 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001307 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001308}
1309
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001310// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001311std::pair<const TargetRegisterClass*, uint8_t>
1312X86TargetLowering::findRepresentativeClass(EVT VT) const{
1313 const TargetRegisterClass *RRC = 0;
1314 uint8_t Cost = 1;
1315 switch (VT.getSimpleVT().SimpleTy) {
1316 default:
1317 return TargetLowering::findRepresentativeClass(VT);
1318 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1319 RRC = (Subtarget->is64Bit()
1320 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1321 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001322 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001323 RRC = X86::VR64RegisterClass;
1324 break;
1325 case MVT::f32: case MVT::f64:
1326 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1327 case MVT::v4f32: case MVT::v2f64:
1328 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1329 case MVT::v4f64:
1330 RRC = X86::VR128RegisterClass;
1331 break;
1332 }
1333 return std::make_pair(RRC, Cost);
1334}
1335
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001336bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1337 unsigned &Offset) const {
1338 if (!Subtarget->isTargetLinux())
1339 return false;
1340
1341 if (Subtarget->is64Bit()) {
1342 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1343 Offset = 0x28;
1344 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1345 AddressSpace = 256;
1346 else
1347 AddressSpace = 257;
1348 } else {
1349 // %gs:0x14 on i386
1350 Offset = 0x14;
1351 AddressSpace = 256;
1352 }
1353 return true;
1354}
1355
1356
Chris Lattner2b02a442007-02-25 08:29:00 +00001357//===----------------------------------------------------------------------===//
1358// Return Value Calling Convention Implementation
1359//===----------------------------------------------------------------------===//
1360
Chris Lattner59ed56b2007-02-28 04:55:35 +00001361#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001362
Michael J. Spencerec38de22010-10-10 22:04:20 +00001363bool
Eric Christopher471e4222011-06-08 23:55:35 +00001364X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1365 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001366 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001367 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001368 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001369 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001370 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001371 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001372}
1373
Dan Gohman98ca4f22009-08-05 01:29:28 +00001374SDValue
1375X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001376 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001377 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001378 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001379 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001380 MachineFunction &MF = DAG.getMachineFunction();
1381 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001382
Chris Lattner9774c912007-02-27 05:28:59 +00001383 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001384 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001385 RVLocs, *DAG.getContext());
1386 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001387
Evan Chengdcea1632010-02-04 02:40:39 +00001388 // Add the regs to the liveout set for the function.
1389 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1390 for (unsigned i = 0; i != RVLocs.size(); ++i)
1391 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1392 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001393
Dan Gohman475871a2008-07-27 21:46:04 +00001394 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001395
Dan Gohman475871a2008-07-27 21:46:04 +00001396 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001397 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1398 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001399 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1400 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001401
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001402 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001403 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1404 CCValAssign &VA = RVLocs[i];
1405 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001406 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001407 EVT ValVT = ValToCopy.getValueType();
1408
Dale Johannesenc4510512010-09-24 19:05:48 +00001409 // If this is x86-64, and we disabled SSE, we can't return FP values,
1410 // or SSE or MMX vectors.
1411 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1412 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001413 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001414 report_fatal_error("SSE register return with SSE disabled");
1415 }
1416 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1417 // llvm-gcc has never done it right and no one has noticed, so this
1418 // should be OK for now.
1419 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001420 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001421 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001422
Chris Lattner447ff682008-03-11 03:23:40 +00001423 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1424 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001425 if (VA.getLocReg() == X86::ST0 ||
1426 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001427 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1428 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001429 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001430 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001431 RetOps.push_back(ValToCopy);
1432 // Don't emit a copytoreg.
1433 continue;
1434 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001435
Evan Cheng242b38b2009-02-23 09:03:22 +00001436 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1437 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001438 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001439 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001440 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001441 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001442 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1443 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001444 // If we don't have SSE2 available, convert to v4f32 so the generated
1445 // register is legal.
1446 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001447 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001448 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001449 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001450 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001451
Dale Johannesendd64c412009-02-04 00:33:20 +00001452 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001453 Flag = Chain.getValue(1);
1454 }
Dan Gohman61a92132008-04-21 23:59:07 +00001455
1456 // The x86-64 ABI for returning structs by value requires that we copy
1457 // the sret argument into %rax for the return. We saved the argument into
1458 // a virtual register in the entry block, so now we copy the value out
1459 // and into %rax.
1460 if (Subtarget->is64Bit() &&
1461 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1462 MachineFunction &MF = DAG.getMachineFunction();
1463 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1464 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001465 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001466 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001467 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001468
Dale Johannesendd64c412009-02-04 00:33:20 +00001469 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001470 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001471
1472 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001473 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001474 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001475
Chris Lattner447ff682008-03-11 03:23:40 +00001476 RetOps[0] = Chain; // Update chain.
1477
1478 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001479 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001480 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001481
1482 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001483 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001484}
1485
Evan Cheng3d2125c2010-11-30 23:55:39 +00001486bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1487 if (N->getNumValues() != 1)
1488 return false;
1489 if (!N->hasNUsesOfValue(1, 0))
1490 return false;
1491
1492 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001493 if (Copy->getOpcode() != ISD::CopyToReg &&
1494 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001495 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001496
1497 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001498 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001499 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001500 if (UI->getOpcode() != X86ISD::RET_FLAG)
1501 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001502 HasRet = true;
1503 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001504
Evan Cheng1bf891a2010-12-01 22:59:46 +00001505 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001506}
1507
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001508EVT
1509X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001510 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001511 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001512 // TODO: Is this also valid on 32-bit?
1513 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001514 ReturnMVT = MVT::i8;
1515 else
1516 ReturnMVT = MVT::i32;
1517
1518 EVT MinVT = getRegisterType(Context, ReturnMVT);
1519 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001520}
1521
Dan Gohman98ca4f22009-08-05 01:29:28 +00001522/// LowerCallResult - Lower the result values of a call into the
1523/// appropriate copies out of appropriate physical registers.
1524///
1525SDValue
1526X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001527 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001528 const SmallVectorImpl<ISD::InputArg> &Ins,
1529 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001530 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001531
Chris Lattnere32bbf62007-02-28 07:09:55 +00001532 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001533 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001534 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001535 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1536 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001537 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001538
Chris Lattner3085e152007-02-25 08:59:22 +00001539 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001540 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001541 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001542 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001543
Torok Edwin3f142c32009-02-01 18:15:56 +00001544 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001545 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001546 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001547 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001548 }
1549
Evan Cheng79fb3b42009-02-20 20:43:02 +00001550 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001551
1552 // If this is a call to a function that returns an fp value on the floating
1553 // point stack, we must guarantee the the value is popped from the stack, so
1554 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001555 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001556 // instead.
1557 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1558 // If we prefer to use the value in xmm registers, copy it out as f80 and
1559 // use a truncate to move it from fp stack reg to xmm reg.
1560 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001561 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001562 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1563 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001564 Val = Chain.getValue(0);
1565
1566 // Round the f80 to the right size, which also moves it to the appropriate
1567 // xmm register.
1568 if (CopyVT != VA.getValVT())
1569 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1570 // This truncation won't change the value.
1571 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001572 } else {
1573 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1574 CopyVT, InFlag).getValue(1);
1575 Val = Chain.getValue(0);
1576 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001577 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001578 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001579 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001580
Dan Gohman98ca4f22009-08-05 01:29:28 +00001581 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001582}
1583
1584
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001585//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001586// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001587//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001588// StdCall calling convention seems to be standard for many Windows' API
1589// routines and around. It differs from C calling convention just a little:
1590// callee should clean up the stack, not caller. Symbols should be also
1591// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001592// For info on fast calling convention see Fast Calling Convention (tail call)
1593// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001594
Dan Gohman98ca4f22009-08-05 01:29:28 +00001595/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001596/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001597static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1598 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001599 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001600
Dan Gohman98ca4f22009-08-05 01:29:28 +00001601 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001602}
1603
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001604/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001605/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001606static bool
1607ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1608 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001609 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001610
Dan Gohman98ca4f22009-08-05 01:29:28 +00001611 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001612}
1613
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001614/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1615/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001616/// the specific parameter attribute. The copy will be passed as a byval
1617/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001618static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001619CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001620 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1621 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001622 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001623
Dale Johannesendd64c412009-02-04 00:33:20 +00001624 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001625 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001626 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001627}
1628
Chris Lattner29689432010-03-11 00:22:57 +00001629/// IsTailCallConvention - Return true if the calling convention is one that
1630/// supports tail call optimization.
1631static bool IsTailCallConvention(CallingConv::ID CC) {
1632 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1633}
1634
Evan Cheng485fafc2011-03-21 01:19:09 +00001635bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1636 if (!CI->isTailCall())
1637 return false;
1638
1639 CallSite CS(CI);
1640 CallingConv::ID CalleeCC = CS.getCallingConv();
1641 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1642 return false;
1643
1644 return true;
1645}
1646
Evan Cheng0c439eb2010-01-27 00:07:07 +00001647/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1648/// a tailcall target by changing its ABI.
1649static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001650 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001651}
1652
Dan Gohman98ca4f22009-08-05 01:29:28 +00001653SDValue
1654X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001655 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001656 const SmallVectorImpl<ISD::InputArg> &Ins,
1657 DebugLoc dl, SelectionDAG &DAG,
1658 const CCValAssign &VA,
1659 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001660 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001661 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001662 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001663 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001664 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001665 EVT ValVT;
1666
1667 // If value is passed by pointer we have address passed instead of the value
1668 // itself.
1669 if (VA.getLocInfo() == CCValAssign::Indirect)
1670 ValVT = VA.getLocVT();
1671 else
1672 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001673
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001674 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001675 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001676 // In case of tail call optimization mark all arguments mutable. Since they
1677 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001678 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001679 unsigned Bytes = Flags.getByValSize();
1680 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1681 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001682 return DAG.getFrameIndex(FI, getPointerTy());
1683 } else {
1684 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001685 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001686 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1687 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001688 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001689 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001690 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001691}
1692
Dan Gohman475871a2008-07-27 21:46:04 +00001693SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001694X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001695 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001696 bool isVarArg,
1697 const SmallVectorImpl<ISD::InputArg> &Ins,
1698 DebugLoc dl,
1699 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001700 SmallVectorImpl<SDValue> &InVals)
1701 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001702 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001703 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001704
Gordon Henriksen86737662008-01-05 16:56:59 +00001705 const Function* Fn = MF.getFunction();
1706 if (Fn->hasExternalLinkage() &&
1707 Subtarget->isTargetCygMing() &&
1708 Fn->getName() == "main")
1709 FuncInfo->setForceFramePointer(true);
1710
Evan Cheng1bc78042006-04-26 01:20:17 +00001711 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001712 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001713 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001714
Chris Lattner29689432010-03-11 00:22:57 +00001715 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1716 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001717
Chris Lattner638402b2007-02-28 07:00:42 +00001718 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001719 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001720 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001721 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001722
1723 // Allocate shadow area for Win64
1724 if (IsWin64) {
1725 CCInfo.AllocateStack(32, 8);
1726 }
1727
Duncan Sands45907662010-10-31 13:21:44 +00001728 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001729
Chris Lattnerf39f7712007-02-28 05:46:49 +00001730 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001731 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001732 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1733 CCValAssign &VA = ArgLocs[i];
1734 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1735 // places.
1736 assert(VA.getValNo() != LastVal &&
1737 "Don't support value assigned to multiple locs yet");
1738 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001739
Chris Lattnerf39f7712007-02-28 05:46:49 +00001740 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001741 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001742 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001743 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001744 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001745 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001746 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001747 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001748 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001749 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001750 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001751 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1752 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001753 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001754 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001755 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001756 RC = X86::VR64RegisterClass;
1757 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001758 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001759
Devang Patel68e6bee2011-02-21 23:21:26 +00001760 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001761 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001762
Chris Lattnerf39f7712007-02-28 05:46:49 +00001763 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1764 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1765 // right size.
1766 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001767 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001768 DAG.getValueType(VA.getValVT()));
1769 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001770 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001771 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001772 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001773 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001774
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001775 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001776 // Handle MMX values passed in XMM regs.
1777 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001778 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1779 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001780 } else
1781 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001782 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001783 } else {
1784 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001785 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001786 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001787
1788 // If value is passed via pointer - do a load.
1789 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001790 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1791 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001792
Dan Gohman98ca4f22009-08-05 01:29:28 +00001793 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001794 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001795
Dan Gohman61a92132008-04-21 23:59:07 +00001796 // The x86-64 ABI for returning structs by value requires that we copy
1797 // the sret argument into %rax for the return. Save the argument into
1798 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001799 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001800 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1801 unsigned Reg = FuncInfo->getSRetReturnReg();
1802 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001803 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001804 FuncInfo->setSRetReturnReg(Reg);
1805 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001806 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001807 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001808 }
1809
Chris Lattnerf39f7712007-02-28 05:46:49 +00001810 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001811 // Align stack specially for tail calls.
1812 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001813 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001814
Evan Cheng1bc78042006-04-26 01:20:17 +00001815 // If the function takes variable number of arguments, make a frame index for
1816 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001817 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001818 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1819 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001820 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001821 }
1822 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001823 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1824
1825 // FIXME: We should really autogenerate these arrays
1826 static const unsigned GPR64ArgRegsWin64[] = {
1827 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001828 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001829 static const unsigned GPR64ArgRegs64Bit[] = {
1830 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1831 };
1832 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001833 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1834 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1835 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001836 const unsigned *GPR64ArgRegs;
1837 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001838
1839 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001840 // The XMM registers which might contain var arg parameters are shadowed
1841 // in their paired GPR. So we only need to save the GPR to their home
1842 // slots.
1843 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001844 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001845 } else {
1846 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1847 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001848
1849 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001850 }
1851 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1852 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001853
Devang Patel578efa92009-06-05 21:57:13 +00001854 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001855 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001856 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001857 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001858 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001859 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001860 // Kernel mode asks for SSE to be disabled, so don't push them
1861 // on the stack.
1862 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001863
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001864 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001865 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001866 // Get to the caller-allocated home save location. Add 8 to account
1867 // for the return address.
1868 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001869 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001870 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001871 // Fixup to set vararg frame on shadow area (4 x i64).
1872 if (NumIntRegs < 4)
1873 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001874 } else {
1875 // For X86-64, if there are vararg parameters that are passed via
1876 // registers, then we must store them to their spots on the stack so they
1877 // may be loaded by deferencing the result of va_next.
1878 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1879 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1880 FuncInfo->setRegSaveFrameIndex(
1881 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001882 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001883 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001884
Gordon Henriksen86737662008-01-05 16:56:59 +00001885 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001886 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001887 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1888 getPointerTy());
1889 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001890 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001891 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1892 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001893 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001894 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001895 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001896 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001897 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001898 MachinePointerInfo::getFixedStack(
1899 FuncInfo->getRegSaveFrameIndex(), Offset),
1900 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001901 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001902 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001903 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001904
Dan Gohmanface41a2009-08-16 21:24:25 +00001905 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1906 // Now store the XMM (fp + vector) parameter registers.
1907 SmallVector<SDValue, 11> SaveXMMOps;
1908 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001909
Devang Patel68e6bee2011-02-21 23:21:26 +00001910 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001911 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1912 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001913
Dan Gohman1e93df62010-04-17 14:41:14 +00001914 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1915 FuncInfo->getRegSaveFrameIndex()));
1916 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1917 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001918
Dan Gohmanface41a2009-08-16 21:24:25 +00001919 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001920 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001921 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001922 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1923 SaveXMMOps.push_back(Val);
1924 }
1925 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1926 MVT::Other,
1927 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001928 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001929
1930 if (!MemOps.empty())
1931 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1932 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001933 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001934 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001935
Gordon Henriksen86737662008-01-05 16:56:59 +00001936 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001937 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001938 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001939 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001940 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001941 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001942 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001943 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001944 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001945
Gordon Henriksen86737662008-01-05 16:56:59 +00001946 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001947 // RegSaveFrameIndex is X86-64 only.
1948 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001949 if (CallConv == CallingConv::X86_FastCall ||
1950 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001951 // fastcc functions can't have varargs.
1952 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001953 }
Evan Cheng25caf632006-05-23 21:06:34 +00001954
Rafael Espindola76927d752011-08-30 19:39:58 +00001955 FuncInfo->setArgumentStackSize(StackSize);
1956
Dan Gohman98ca4f22009-08-05 01:29:28 +00001957 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001958}
1959
Dan Gohman475871a2008-07-27 21:46:04 +00001960SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001961X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1962 SDValue StackPtr, SDValue Arg,
1963 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001964 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001965 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001966 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001967 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001968 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001969 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001970 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001971
1972 return DAG.getStore(Chain, dl, Arg, PtrOff,
1973 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001974 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001975}
1976
Bill Wendling64e87322009-01-16 19:25:27 +00001977/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001978/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001979SDValue
1980X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001981 SDValue &OutRetAddr, SDValue Chain,
1982 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001983 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001984 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001985 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001986 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001987
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001988 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001989 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1990 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001991 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001992}
1993
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001994/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001995/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001996static SDValue
1997EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001998 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001999 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002000 // Store the return address to the appropriate stack slot.
2001 if (!FPDiff) return Chain;
2002 // Calculate the new stack slot for the return address.
2003 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002004 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002005 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002006 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002007 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002008 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002009 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002010 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002011 return Chain;
2012}
2013
Dan Gohman98ca4f22009-08-05 01:29:28 +00002014SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002015X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002016 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002017 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002018 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002019 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002020 const SmallVectorImpl<ISD::InputArg> &Ins,
2021 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002022 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002023 MachineFunction &MF = DAG.getMachineFunction();
2024 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002025 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002026 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002027 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002028
Evan Cheng5f941932010-02-05 02:21:12 +00002029 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002030 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002031 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2032 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002033 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002034
2035 // Sibcalls are automatically detected tailcalls which do not require
2036 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002037 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002038 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002039
2040 if (isTailCall)
2041 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002042 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002043
Chris Lattner29689432010-03-11 00:22:57 +00002044 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2045 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002046
Chris Lattner638402b2007-02-28 07:00:42 +00002047 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002048 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002049 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002050 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002051
2052 // Allocate shadow area for Win64
2053 if (IsWin64) {
2054 CCInfo.AllocateStack(32, 8);
2055 }
2056
Duncan Sands45907662010-10-31 13:21:44 +00002057 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002058
Chris Lattner423c5f42007-02-28 05:31:48 +00002059 // Get a count of how many bytes are to be pushed on the stack.
2060 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002061 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002062 // This is a sibcall. The memory operands are available in caller's
2063 // own caller's stack.
2064 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002065 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002066 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002067
Gordon Henriksen86737662008-01-05 16:56:59 +00002068 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002069 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002070 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002071 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002072 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2073 FPDiff = NumBytesCallerPushed - NumBytes;
2074
2075 // Set the delta of movement of the returnaddr stackslot.
2076 // But only set if delta is greater than previous delta.
2077 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2078 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2079 }
2080
Evan Chengf22f9b32010-02-06 03:28:46 +00002081 if (!IsSibcall)
2082 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002083
Dan Gohman475871a2008-07-27 21:46:04 +00002084 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002085 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002086 if (isTailCall && FPDiff)
2087 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2088 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002089
Dan Gohman475871a2008-07-27 21:46:04 +00002090 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2091 SmallVector<SDValue, 8> MemOpChains;
2092 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002093
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002094 // Walk the register/memloc assignments, inserting copies/loads. In the case
2095 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002096 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2097 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002098 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002099 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002100 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002101 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002102
Chris Lattner423c5f42007-02-28 05:31:48 +00002103 // Promote the value if needed.
2104 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002105 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002106 case CCValAssign::Full: break;
2107 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002108 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002109 break;
2110 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002111 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002112 break;
2113 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002114 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2115 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002116 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002117 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2118 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002119 } else
2120 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2121 break;
2122 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002123 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002124 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002125 case CCValAssign::Indirect: {
2126 // Store the argument.
2127 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002128 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002129 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002130 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002131 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002132 Arg = SpillSlot;
2133 break;
2134 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002135 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002136
Chris Lattner423c5f42007-02-28 05:31:48 +00002137 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002138 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2139 if (isVarArg && IsWin64) {
2140 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2141 // shadow reg if callee is a varargs function.
2142 unsigned ShadowReg = 0;
2143 switch (VA.getLocReg()) {
2144 case X86::XMM0: ShadowReg = X86::RCX; break;
2145 case X86::XMM1: ShadowReg = X86::RDX; break;
2146 case X86::XMM2: ShadowReg = X86::R8; break;
2147 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002148 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002149 if (ShadowReg)
2150 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002151 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002152 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002153 assert(VA.isMemLoc());
2154 if (StackPtr.getNode() == 0)
2155 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2156 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2157 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002158 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002159 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002160
Evan Cheng32fe1032006-05-25 00:59:30 +00002161 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002162 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002163 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002164
Evan Cheng347d5f72006-04-28 21:29:37 +00002165 // Build a sequence of copy-to-reg nodes chained together with token chain
2166 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002167 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002168 // Tail call byval lowering might overwrite argument registers so in case of
2169 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002170 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002171 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002172 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002173 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002174 InFlag = Chain.getValue(1);
2175 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002176
Chris Lattner88e1fd52009-07-09 04:24:46 +00002177 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002178 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2179 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002180 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002181 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2182 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002183 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002184 InFlag);
2185 InFlag = Chain.getValue(1);
2186 } else {
2187 // If we are tail calling and generating PIC/GOT style code load the
2188 // address of the callee into ECX. The value in ecx is used as target of
2189 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2190 // for tail calls on PIC/GOT architectures. Normally we would just put the
2191 // address of GOT into ebx and then call target@PLT. But for tail calls
2192 // ebx would be restored (since ebx is callee saved) before jumping to the
2193 // target@PLT.
2194
2195 // Note: The actual moving to ECX is done further down.
2196 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2197 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2198 !G->getGlobal()->hasProtectedVisibility())
2199 Callee = LowerGlobalAddress(Callee, DAG);
2200 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002201 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002202 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002203 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002204
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002205 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002206 // From AMD64 ABI document:
2207 // For calls that may call functions that use varargs or stdargs
2208 // (prototype-less calls or calls to functions containing ellipsis (...) in
2209 // the declaration) %al is used as hidden argument to specify the number
2210 // of SSE registers used. The contents of %al do not need to match exactly
2211 // the number of registers, but must be an ubound on the number of SSE
2212 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002213
Gordon Henriksen86737662008-01-05 16:56:59 +00002214 // Count the number of XMM registers allocated.
2215 static const unsigned XMMArgRegs[] = {
2216 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2217 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2218 };
2219 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002220 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002221 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002222
Dale Johannesendd64c412009-02-04 00:33:20 +00002223 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002224 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002225 InFlag = Chain.getValue(1);
2226 }
2227
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002228
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002229 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002230 if (isTailCall) {
2231 // Force all the incoming stack arguments to be loaded from the stack
2232 // before any new outgoing arguments are stored to the stack, because the
2233 // outgoing stack slots may alias the incoming argument stack slots, and
2234 // the alias isn't otherwise explicit. This is slightly more conservative
2235 // than necessary, because it means that each store effectively depends
2236 // on every argument instead of just those arguments it would clobber.
2237 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2238
Dan Gohman475871a2008-07-27 21:46:04 +00002239 SmallVector<SDValue, 8> MemOpChains2;
2240 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002241 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002242 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002243 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002244 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002245 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2246 CCValAssign &VA = ArgLocs[i];
2247 if (VA.isRegLoc())
2248 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002249 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002250 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002251 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002252 // Create frame index.
2253 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002254 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002255 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002256 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002257
Duncan Sands276dcbd2008-03-21 09:14:45 +00002258 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002259 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002260 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002261 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002262 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002263 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002264 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002265
Dan Gohman98ca4f22009-08-05 01:29:28 +00002266 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2267 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002268 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002269 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002270 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002271 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002272 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002273 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002274 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002275 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002276 }
2277 }
2278
2279 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002280 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002281 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002282
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002283 // Copy arguments to their registers.
2284 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002285 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002286 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002287 InFlag = Chain.getValue(1);
2288 }
Dan Gohman475871a2008-07-27 21:46:04 +00002289 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002290
Gordon Henriksen86737662008-01-05 16:56:59 +00002291 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002292 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002293 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002294 }
2295
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002296 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2297 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2298 // In the 64-bit large code model, we have to make all calls
2299 // through a register, since the call instruction's 32-bit
2300 // pc-relative offset may not be large enough to hold the whole
2301 // address.
2302 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002303 // If the callee is a GlobalAddress node (quite common, every direct call
2304 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2305 // it.
2306
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002307 // We should use extra load for direct calls to dllimported functions in
2308 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002309 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002310 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002311 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002312 bool ExtraLoad = false;
2313 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002314
Chris Lattner48a7d022009-07-09 05:02:21 +00002315 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2316 // external symbols most go through the PLT in PIC mode. If the symbol
2317 // has hidden or protected visibility, or if it is static or local, then
2318 // we don't need to use the PLT - we can directly call it.
2319 if (Subtarget->isTargetELF() &&
2320 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002321 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002322 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002323 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002324 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002325 (!Subtarget->getTargetTriple().isMacOSX() ||
2326 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002327 // PC-relative references to external symbols should go through $stub,
2328 // unless we're building with the leopard linker or later, which
2329 // automatically synthesizes these stubs.
2330 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002331 } else if (Subtarget->isPICStyleRIPRel() &&
2332 isa<Function>(GV) &&
2333 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2334 // If the function is marked as non-lazy, generate an indirect call
2335 // which loads from the GOT directly. This avoids runtime overhead
2336 // at the cost of eager binding (and one extra byte of encoding).
2337 OpFlags = X86II::MO_GOTPCREL;
2338 WrapperKind = X86ISD::WrapperRIP;
2339 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002340 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002341
Devang Patel0d881da2010-07-06 22:08:15 +00002342 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002343 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002344
2345 // Add a wrapper if needed.
2346 if (WrapperKind != ISD::DELETED_NODE)
2347 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2348 // Add extra indirection if needed.
2349 if (ExtraLoad)
2350 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2351 MachinePointerInfo::getGOT(),
2352 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002353 }
Bill Wendling056292f2008-09-16 21:48:12 +00002354 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002355 unsigned char OpFlags = 0;
2356
Evan Cheng1bf891a2010-12-01 22:59:46 +00002357 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2358 // external symbols should go through the PLT.
2359 if (Subtarget->isTargetELF() &&
2360 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2361 OpFlags = X86II::MO_PLT;
2362 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002363 (!Subtarget->getTargetTriple().isMacOSX() ||
2364 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002365 // PC-relative references to external symbols should go through $stub,
2366 // unless we're building with the leopard linker or later, which
2367 // automatically synthesizes these stubs.
2368 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002369 }
Eric Christopherfd179292009-08-27 18:07:15 +00002370
Chris Lattner48a7d022009-07-09 05:02:21 +00002371 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2372 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002373 }
2374
Chris Lattnerd96d0722007-02-25 06:40:16 +00002375 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002376 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002377 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002378
Evan Chengf22f9b32010-02-06 03:28:46 +00002379 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002380 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2381 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002382 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002383 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002384
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002385 Ops.push_back(Chain);
2386 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002387
Dan Gohman98ca4f22009-08-05 01:29:28 +00002388 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002389 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002390
Gordon Henriksen86737662008-01-05 16:56:59 +00002391 // Add argument registers to the end of the list so that they are known live
2392 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002393 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2394 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2395 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002396
Evan Cheng586ccac2008-03-18 23:36:35 +00002397 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002398 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002399 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2400
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002401 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002402 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002403 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002404
Gabor Greifba36cb52008-08-28 21:40:38 +00002405 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002406 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002407
Dan Gohman98ca4f22009-08-05 01:29:28 +00002408 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002409 // We used to do:
2410 //// If this is the first return lowered for this function, add the regs
2411 //// to the liveout set for the function.
2412 // This isn't right, although it's probably harmless on x86; liveouts
2413 // should be computed from returns not tail calls. Consider a void
2414 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002415 return DAG.getNode(X86ISD::TC_RETURN, dl,
2416 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002417 }
2418
Dale Johannesenace16102009-02-03 19:33:06 +00002419 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002420 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002421
Chris Lattner2d297092006-05-23 18:50:38 +00002422 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002423 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002424 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002425 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002426 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002427 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002428 // pops the hidden struct pointer, so we have to push it back.
2429 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002430 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002431 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002432 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002433
Gordon Henriksenae636f82008-01-03 16:47:34 +00002434 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002435 if (!IsSibcall) {
2436 Chain = DAG.getCALLSEQ_END(Chain,
2437 DAG.getIntPtrConstant(NumBytes, true),
2438 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2439 true),
2440 InFlag);
2441 InFlag = Chain.getValue(1);
2442 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002443
Chris Lattner3085e152007-02-25 08:59:22 +00002444 // Handle result values, copying them out of physregs into vregs that we
2445 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002446 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2447 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002448}
2449
Evan Cheng25ab6902006-09-08 06:48:29 +00002450
2451//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002452// Fast Calling Convention (tail call) implementation
2453//===----------------------------------------------------------------------===//
2454
2455// Like std call, callee cleans arguments, convention except that ECX is
2456// reserved for storing the tail called function address. Only 2 registers are
2457// free for argument passing (inreg). Tail call optimization is performed
2458// provided:
2459// * tailcallopt is enabled
2460// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002461// On X86_64 architecture with GOT-style position independent code only local
2462// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002463// To keep the stack aligned according to platform abi the function
2464// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2465// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002466// If a tail called function callee has more arguments than the caller the
2467// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002468// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002469// original REtADDR, but before the saved framepointer or the spilled registers
2470// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2471// stack layout:
2472// arg1
2473// arg2
2474// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002475// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002476// move area ]
2477// (possible EBP)
2478// ESI
2479// EDI
2480// local1 ..
2481
2482/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2483/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002484unsigned
2485X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2486 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002487 MachineFunction &MF = DAG.getMachineFunction();
2488 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002489 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002490 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002491 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002492 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002493 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002494 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2495 // Number smaller than 12 so just add the difference.
2496 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2497 } else {
2498 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002499 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002500 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002501 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002502 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002503}
2504
Evan Cheng5f941932010-02-05 02:21:12 +00002505/// MatchingStackOffset - Return true if the given stack call argument is
2506/// already available in the same position (relatively) of the caller's
2507/// incoming argument stack.
2508static
2509bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2510 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2511 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002512 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2513 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002514 if (Arg.getOpcode() == ISD::CopyFromReg) {
2515 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002516 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002517 return false;
2518 MachineInstr *Def = MRI->getVRegDef(VR);
2519 if (!Def)
2520 return false;
2521 if (!Flags.isByVal()) {
2522 if (!TII->isLoadFromStackSlot(Def, FI))
2523 return false;
2524 } else {
2525 unsigned Opcode = Def->getOpcode();
2526 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2527 Def->getOperand(1).isFI()) {
2528 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002529 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002530 } else
2531 return false;
2532 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002533 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2534 if (Flags.isByVal())
2535 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002536 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002537 // define @foo(%struct.X* %A) {
2538 // tail call @bar(%struct.X* byval %A)
2539 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002540 return false;
2541 SDValue Ptr = Ld->getBasePtr();
2542 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2543 if (!FINode)
2544 return false;
2545 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002546 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002547 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002548 FI = FINode->getIndex();
2549 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002550 } else
2551 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002552
Evan Cheng4cae1332010-03-05 08:38:04 +00002553 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002554 if (!MFI->isFixedObjectIndex(FI))
2555 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002556 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002557}
2558
Dan Gohman98ca4f22009-08-05 01:29:28 +00002559/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2560/// for tail call optimization. Targets which want to do tail call
2561/// optimization should implement this function.
2562bool
2563X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002564 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002565 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002566 bool isCalleeStructRet,
2567 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002568 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002569 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002570 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002571 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002572 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002573 CalleeCC != CallingConv::C)
2574 return false;
2575
Evan Cheng7096ae42010-01-29 06:45:59 +00002576 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002577 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002578 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002579 CallingConv::ID CallerCC = CallerF->getCallingConv();
2580 bool CCMatch = CallerCC == CalleeCC;
2581
Dan Gohman1797ed52010-02-08 20:27:50 +00002582 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002583 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002584 return true;
2585 return false;
2586 }
2587
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002588 // Look for obvious safe cases to perform tail call optimization that do not
2589 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002590
Evan Cheng2c12cb42010-03-26 16:26:03 +00002591 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2592 // emit a special epilogue.
2593 if (RegInfo->needsStackRealignment(MF))
2594 return false;
2595
Evan Chenga375d472010-03-15 18:54:48 +00002596 // Also avoid sibcall optimization if either caller or callee uses struct
2597 // return semantics.
2598 if (isCalleeStructRet || isCallerStructRet)
2599 return false;
2600
Chad Rosier2416da32011-06-24 21:15:36 +00002601 // An stdcall caller is expected to clean up its arguments; the callee
2602 // isn't going to do that.
2603 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2604 return false;
2605
Chad Rosier871f6642011-05-18 19:59:50 +00002606 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002607 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002608 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002609
2610 // Optimizing for varargs on Win64 is unlikely to be safe without
2611 // additional testing.
2612 if (Subtarget->isTargetWin64())
2613 return false;
2614
Chad Rosier871f6642011-05-18 19:59:50 +00002615 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002616 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2617 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002618
Chad Rosier871f6642011-05-18 19:59:50 +00002619 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2620 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2621 if (!ArgLocs[i].isRegLoc())
2622 return false;
2623 }
2624
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002625 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2626 // Therefore if it's not used by the call it is not safe to optimize this into
2627 // a sibcall.
2628 bool Unused = false;
2629 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2630 if (!Ins[i].Used) {
2631 Unused = true;
2632 break;
2633 }
2634 }
2635 if (Unused) {
2636 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002637 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2638 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002639 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002640 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002641 CCValAssign &VA = RVLocs[i];
2642 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2643 return false;
2644 }
2645 }
2646
Evan Cheng13617962010-04-30 01:12:32 +00002647 // If the calling conventions do not match, then we'd better make sure the
2648 // results are returned in the same way as what the caller expects.
2649 if (!CCMatch) {
2650 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002651 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2652 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002653 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2654
2655 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002656 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2657 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002658 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2659
2660 if (RVLocs1.size() != RVLocs2.size())
2661 return false;
2662 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2663 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2664 return false;
2665 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2666 return false;
2667 if (RVLocs1[i].isRegLoc()) {
2668 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2669 return false;
2670 } else {
2671 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2672 return false;
2673 }
2674 }
2675 }
2676
Evan Chenga6bff982010-01-30 01:22:00 +00002677 // If the callee takes no arguments then go on to check the results of the
2678 // call.
2679 if (!Outs.empty()) {
2680 // Check if stack adjustment is needed. For now, do not do this if any
2681 // argument is passed on the stack.
2682 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002683 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2684 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002685
2686 // Allocate shadow area for Win64
2687 if (Subtarget->isTargetWin64()) {
2688 CCInfo.AllocateStack(32, 8);
2689 }
2690
Duncan Sands45907662010-10-31 13:21:44 +00002691 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002692 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002693 MachineFunction &MF = DAG.getMachineFunction();
2694 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2695 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002696
2697 // Check if the arguments are already laid out in the right way as
2698 // the caller's fixed stack objects.
2699 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002700 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2701 const X86InstrInfo *TII =
2702 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002703 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2704 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002705 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002706 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002707 if (VA.getLocInfo() == CCValAssign::Indirect)
2708 return false;
2709 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002710 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2711 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002712 return false;
2713 }
2714 }
2715 }
Evan Cheng9c044672010-05-29 01:35:22 +00002716
2717 // If the tailcall address may be in a register, then make sure it's
2718 // possible to register allocate for it. In 32-bit, the call address can
2719 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002720 // callee-saved registers are restored. These happen to be the same
2721 // registers used to pass 'inreg' arguments so watch out for those.
2722 if (!Subtarget->is64Bit() &&
2723 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002724 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002725 unsigned NumInRegs = 0;
2726 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2727 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002728 if (!VA.isRegLoc())
2729 continue;
2730 unsigned Reg = VA.getLocReg();
2731 switch (Reg) {
2732 default: break;
2733 case X86::EAX: case X86::EDX: case X86::ECX:
2734 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002735 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002736 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002737 }
2738 }
2739 }
Evan Chenga6bff982010-01-30 01:22:00 +00002740 }
Evan Chengb1712452010-01-27 06:25:16 +00002741
Evan Cheng86809cc2010-02-03 03:28:02 +00002742 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002743}
2744
Dan Gohman3df24e62008-09-03 23:12:08 +00002745FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002746X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2747 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002748}
2749
2750
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002751//===----------------------------------------------------------------------===//
2752// Other Lowering Hooks
2753//===----------------------------------------------------------------------===//
2754
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002755static bool MayFoldLoad(SDValue Op) {
2756 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2757}
2758
2759static bool MayFoldIntoStore(SDValue Op) {
2760 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2761}
2762
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002763static bool isTargetShuffle(unsigned Opcode) {
2764 switch(Opcode) {
2765 default: return false;
2766 case X86ISD::PSHUFD:
2767 case X86ISD::PSHUFHW:
2768 case X86ISD::PSHUFLW:
2769 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002770 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002771 case X86ISD::SHUFPS:
2772 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002773 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002774 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002775 case X86ISD::MOVLPS:
2776 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002777 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002778 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002779 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002780 case X86ISD::MOVSS:
2781 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002782 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002783 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002784 case X86ISD::VUNPCKLPSY:
2785 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002786 case X86ISD::PUNPCKLWD:
2787 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002788 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002789 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002790 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002791 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002792 case X86ISD::VUNPCKHPSY:
2793 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002794 case X86ISD::PUNPCKHWD:
2795 case X86ISD::PUNPCKHBW:
2796 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002797 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002798 case X86ISD::VPERMILPS:
2799 case X86ISD::VPERMILPSY:
2800 case X86ISD::VPERMILPD:
2801 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002802 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002803 return true;
2804 }
2805 return false;
2806}
2807
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002808static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002809 SDValue V1, SelectionDAG &DAG) {
2810 switch(Opc) {
2811 default: llvm_unreachable("Unknown x86 shuffle node");
2812 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002813 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002814 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002815 return DAG.getNode(Opc, dl, VT, V1);
2816 }
2817
2818 return SDValue();
2819}
2820
2821static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002822 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002823 switch(Opc) {
2824 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002825 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002826 case X86ISD::PSHUFHW:
2827 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002828 case X86ISD::VPERMILPS:
2829 case X86ISD::VPERMILPSY:
2830 case X86ISD::VPERMILPD:
2831 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002832 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2833 }
2834
2835 return SDValue();
2836}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002837
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002838static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2839 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2840 switch(Opc) {
2841 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002842 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002843 case X86ISD::SHUFPD:
2844 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002845 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002846 return DAG.getNode(Opc, dl, VT, V1, V2,
2847 DAG.getConstant(TargetMask, MVT::i8));
2848 }
2849 return SDValue();
2850}
2851
2852static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2853 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2854 switch(Opc) {
2855 default: llvm_unreachable("Unknown x86 shuffle node");
2856 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002857 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002858 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002859 case X86ISD::MOVLPS:
2860 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002861 case X86ISD::MOVSS:
2862 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002863 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002864 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002865 case X86ISD::VUNPCKLPSY:
2866 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002867 case X86ISD::PUNPCKLWD:
2868 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002869 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002870 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002871 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002872 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002873 case X86ISD::VUNPCKHPSY:
2874 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002875 case X86ISD::PUNPCKHWD:
2876 case X86ISD::PUNPCKHBW:
2877 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002878 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002879 return DAG.getNode(Opc, dl, VT, V1, V2);
2880 }
2881 return SDValue();
2882}
2883
Dan Gohmand858e902010-04-17 15:26:15 +00002884SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002885 MachineFunction &MF = DAG.getMachineFunction();
2886 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2887 int ReturnAddrIndex = FuncInfo->getRAIndex();
2888
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002889 if (ReturnAddrIndex == 0) {
2890 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002891 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002892 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002893 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002894 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002895 }
2896
Evan Cheng25ab6902006-09-08 06:48:29 +00002897 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002898}
2899
2900
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002901bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2902 bool hasSymbolicDisplacement) {
2903 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002904 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002905 return false;
2906
2907 // If we don't have a symbolic displacement - we don't have any extra
2908 // restrictions.
2909 if (!hasSymbolicDisplacement)
2910 return true;
2911
2912 // FIXME: Some tweaks might be needed for medium code model.
2913 if (M != CodeModel::Small && M != CodeModel::Kernel)
2914 return false;
2915
2916 // For small code model we assume that latest object is 16MB before end of 31
2917 // bits boundary. We may also accept pretty large negative constants knowing
2918 // that all objects are in the positive half of address space.
2919 if (M == CodeModel::Small && Offset < 16*1024*1024)
2920 return true;
2921
2922 // For kernel code model we know that all object resist in the negative half
2923 // of 32bits address space. We may not accept negative offsets, since they may
2924 // be just off and we may accept pretty large positive ones.
2925 if (M == CodeModel::Kernel && Offset > 0)
2926 return true;
2927
2928 return false;
2929}
2930
Evan Chengef41ff62011-06-23 17:54:54 +00002931/// isCalleePop - Determines whether the callee is required to pop its
2932/// own arguments. Callee pop is necessary to support tail calls.
2933bool X86::isCalleePop(CallingConv::ID CallingConv,
2934 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2935 if (IsVarArg)
2936 return false;
2937
2938 switch (CallingConv) {
2939 default:
2940 return false;
2941 case CallingConv::X86_StdCall:
2942 return !is64Bit;
2943 case CallingConv::X86_FastCall:
2944 return !is64Bit;
2945 case CallingConv::X86_ThisCall:
2946 return !is64Bit;
2947 case CallingConv::Fast:
2948 return TailCallOpt;
2949 case CallingConv::GHC:
2950 return TailCallOpt;
2951 }
2952}
2953
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002954/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2955/// specific condition code, returning the condition code and the LHS/RHS of the
2956/// comparison to make.
2957static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2958 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002959 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002960 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2961 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2962 // X > -1 -> X == 0, jump !sign.
2963 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002964 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002965 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2966 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002967 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002968 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002969 // X < 1 -> X <= 0
2970 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002971 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002972 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002973 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002974
Evan Chengd9558e02006-01-06 00:43:03 +00002975 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002976 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002977 case ISD::SETEQ: return X86::COND_E;
2978 case ISD::SETGT: return X86::COND_G;
2979 case ISD::SETGE: return X86::COND_GE;
2980 case ISD::SETLT: return X86::COND_L;
2981 case ISD::SETLE: return X86::COND_LE;
2982 case ISD::SETNE: return X86::COND_NE;
2983 case ISD::SETULT: return X86::COND_B;
2984 case ISD::SETUGT: return X86::COND_A;
2985 case ISD::SETULE: return X86::COND_BE;
2986 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002987 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002988 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002989
Chris Lattner4c78e022008-12-23 23:42:27 +00002990 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002991
Chris Lattner4c78e022008-12-23 23:42:27 +00002992 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002993 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2994 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002995 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2996 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002997 }
2998
Chris Lattner4c78e022008-12-23 23:42:27 +00002999 switch (SetCCOpcode) {
3000 default: break;
3001 case ISD::SETOLT:
3002 case ISD::SETOLE:
3003 case ISD::SETUGT:
3004 case ISD::SETUGE:
3005 std::swap(LHS, RHS);
3006 break;
3007 }
3008
3009 // On a floating point condition, the flags are set as follows:
3010 // ZF PF CF op
3011 // 0 | 0 | 0 | X > Y
3012 // 0 | 0 | 1 | X < Y
3013 // 1 | 0 | 0 | X == Y
3014 // 1 | 1 | 1 | unordered
3015 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003016 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003017 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003018 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003019 case ISD::SETOLT: // flipped
3020 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003021 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003022 case ISD::SETOLE: // flipped
3023 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003024 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003025 case ISD::SETUGT: // flipped
3026 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003027 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003028 case ISD::SETUGE: // flipped
3029 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003030 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003031 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003032 case ISD::SETNE: return X86::COND_NE;
3033 case ISD::SETUO: return X86::COND_P;
3034 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003035 case ISD::SETOEQ:
3036 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003037 }
Evan Chengd9558e02006-01-06 00:43:03 +00003038}
3039
Evan Cheng4a460802006-01-11 00:33:36 +00003040/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3041/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003042/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003043static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003044 switch (X86CC) {
3045 default:
3046 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003047 case X86::COND_B:
3048 case X86::COND_BE:
3049 case X86::COND_E:
3050 case X86::COND_P:
3051 case X86::COND_A:
3052 case X86::COND_AE:
3053 case X86::COND_NE:
3054 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003055 return true;
3056 }
3057}
3058
Evan Chengeb2f9692009-10-27 19:56:55 +00003059/// isFPImmLegal - Returns true if the target can instruction select the
3060/// specified FP immediate natively. If false, the legalizer will
3061/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003062bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003063 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3064 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3065 return true;
3066 }
3067 return false;
3068}
3069
Nate Begeman9008ca62009-04-27 18:41:29 +00003070/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3071/// the specified range (L, H].
3072static bool isUndefOrInRange(int Val, int Low, int Hi) {
3073 return (Val < 0) || (Val >= Low && Val < Hi);
3074}
3075
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003076/// isUndefOrInRange - Return true if every element in Mask, begining
3077/// from position Pos and ending in Pos+Size, falls within the specified
3078/// range (L, L+Pos]. or is undef.
3079static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3080 int Pos, int Size, int Low, int Hi) {
3081 for (int i = Pos, e = Pos+Size; i != e; ++i)
3082 if (!isUndefOrInRange(Mask[i], Low, Hi))
3083 return false;
3084 return true;
3085}
3086
Nate Begeman9008ca62009-04-27 18:41:29 +00003087/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3088/// specified value.
3089static bool isUndefOrEqual(int Val, int CmpVal) {
3090 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003091 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003092 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003093}
3094
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003095/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3096/// from position Pos and ending in Pos+Size, falls within the specified
3097/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003098static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3099 int Pos, int Size, int Low) {
3100 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3101 if (!isUndefOrEqual(Mask[i], Low))
3102 return false;
3103 return true;
3104}
3105
Nate Begeman9008ca62009-04-27 18:41:29 +00003106/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3107/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3108/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003109static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003110 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003111 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003112 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003113 return (Mask[0] < 2 && Mask[1] < 2);
3114 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003115}
3116
Nate Begeman9008ca62009-04-27 18:41:29 +00003117bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003118 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003119 N->getMask(M);
3120 return ::isPSHUFDMask(M, N->getValueType(0));
3121}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003122
Nate Begeman9008ca62009-04-27 18:41:29 +00003123/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3124/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003125static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003126 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003127 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003128
Nate Begeman9008ca62009-04-27 18:41:29 +00003129 // Lower quadword copied in order or undef.
3130 for (int i = 0; i != 4; ++i)
3131 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003132 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003133
Evan Cheng506d3df2006-03-29 23:07:14 +00003134 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003135 for (int i = 4; i != 8; ++i)
3136 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003137 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003138
Evan Cheng506d3df2006-03-29 23:07:14 +00003139 return true;
3140}
3141
Nate Begeman9008ca62009-04-27 18:41:29 +00003142bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003143 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003144 N->getMask(M);
3145 return ::isPSHUFHWMask(M, N->getValueType(0));
3146}
Evan Cheng506d3df2006-03-29 23:07:14 +00003147
Nate Begeman9008ca62009-04-27 18:41:29 +00003148/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3149/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003150static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003151 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003152 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003153
Rafael Espindola15684b22009-04-24 12:40:33 +00003154 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003155 for (int i = 4; i != 8; ++i)
3156 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003157 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003158
Rafael Espindola15684b22009-04-24 12:40:33 +00003159 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003160 for (int i = 0; i != 4; ++i)
3161 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003162 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003163
Rafael Espindola15684b22009-04-24 12:40:33 +00003164 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003165}
3166
Nate Begeman9008ca62009-04-27 18:41:29 +00003167bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003168 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003169 N->getMask(M);
3170 return ::isPSHUFLWMask(M, N->getValueType(0));
3171}
3172
Nate Begemana09008b2009-10-19 02:17:23 +00003173/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3174/// is suitable for input to PALIGNR.
3175static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3176 bool hasSSSE3) {
3177 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003178 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3179 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003180
Nate Begemana09008b2009-10-19 02:17:23 +00003181 // Do not handle v2i64 / v2f64 shuffles with palignr.
3182 if (e < 4 || !hasSSSE3)
3183 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003184
Nate Begemana09008b2009-10-19 02:17:23 +00003185 for (i = 0; i != e; ++i)
3186 if (Mask[i] >= 0)
3187 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003188
Nate Begemana09008b2009-10-19 02:17:23 +00003189 // All undef, not a palignr.
3190 if (i == e)
3191 return false;
3192
Eli Friedman63f8dde2011-07-25 21:36:45 +00003193 // Make sure we're shifting in the right direction.
3194 if (Mask[i] <= i)
3195 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003196
3197 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003198
Nate Begemana09008b2009-10-19 02:17:23 +00003199 // Check the rest of the elements to see if they are consecutive.
3200 for (++i; i != e; ++i) {
3201 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003202 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003203 return false;
3204 }
3205 return true;
3206}
3207
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003208/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3209/// specifies a shuffle of elements that is suitable for input to 256-bit
3210/// VSHUFPSY.
3211static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3212 const X86Subtarget *Subtarget) {
3213 int NumElems = VT.getVectorNumElements();
3214
3215 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3216 return false;
3217
3218 if (NumElems != 8)
3219 return false;
3220
3221 // VSHUFPSY divides the resulting vector into 4 chunks.
3222 // The sources are also splitted into 4 chunks, and each destination
3223 // chunk must come from a different source chunk.
3224 //
3225 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3226 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3227 //
3228 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3229 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3230 //
3231 int QuarterSize = NumElems/4;
3232 int HalfSize = QuarterSize*2;
3233 for (int i = 0; i < QuarterSize; ++i)
3234 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3235 return false;
3236 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3237 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3238 return false;
3239
3240 // The mask of the second half must be the same as the first but with
3241 // the appropriate offsets. This works in the same way as VPERMILPS
3242 // works with masks.
3243 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3244 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3245 return false;
3246 int FstHalfIdx = i-HalfSize;
3247 if (Mask[FstHalfIdx] < 0)
3248 continue;
3249 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3250 return false;
3251 }
3252 for (int i = QuarterSize*3; i < NumElems; ++i) {
3253 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3254 return false;
3255 int FstHalfIdx = i-HalfSize;
3256 if (Mask[FstHalfIdx] < 0)
3257 continue;
3258 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3259 return false;
3260
3261 }
3262
3263 return true;
3264}
3265
3266/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3267/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3268static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3269 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3270 EVT VT = SVOp->getValueType(0);
3271 int NumElems = VT.getVectorNumElements();
3272
3273 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3274 "Only supports v8i32 and v8f32 types");
3275
3276 int HalfSize = NumElems/2;
3277 unsigned Mask = 0;
3278 for (int i = 0; i != NumElems ; ++i) {
3279 if (SVOp->getMaskElt(i) < 0)
3280 continue;
3281 // The mask of the first half must be equal to the second one.
3282 unsigned Shamt = (i%HalfSize)*2;
3283 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3284 Mask |= Elt << Shamt;
3285 }
3286
3287 return Mask;
3288}
3289
3290/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3291/// specifies a shuffle of elements that is suitable for input to 256-bit
3292/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3293/// version and the mask of the second half isn't binded with the first
3294/// one.
3295static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3296 const X86Subtarget *Subtarget) {
3297 int NumElems = VT.getVectorNumElements();
3298
3299 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3300 return false;
3301
3302 if (NumElems != 4)
3303 return false;
3304
3305 // VSHUFPSY divides the resulting vector into 4 chunks.
3306 // The sources are also splitted into 4 chunks, and each destination
3307 // chunk must come from a different source chunk.
3308 //
3309 // SRC1 => X3 X2 X1 X0
3310 // SRC2 => Y3 Y2 Y1 Y0
3311 //
3312 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3313 //
3314 int QuarterSize = NumElems/4;
3315 int HalfSize = QuarterSize*2;
3316 for (int i = 0; i < QuarterSize; ++i)
3317 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3318 return false;
3319 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3320 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3321 return false;
3322 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3323 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3324 return false;
3325 for (int i = QuarterSize*3; i < NumElems; ++i)
3326 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3327 return false;
3328
3329 return true;
3330}
3331
3332/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3333/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3334static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3335 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3336 EVT VT = SVOp->getValueType(0);
3337 int NumElems = VT.getVectorNumElements();
3338
3339 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3340 "Only supports v4i64 and v4f64 types");
3341
3342 int HalfSize = NumElems/2;
3343 unsigned Mask = 0;
3344 for (int i = 0; i != NumElems ; ++i) {
3345 if (SVOp->getMaskElt(i) < 0)
3346 continue;
3347 int Elt = SVOp->getMaskElt(i) % HalfSize;
3348 Mask |= Elt << i;
3349 }
3350
3351 return Mask;
3352}
3353
Evan Cheng14aed5e2006-03-24 01:18:28 +00003354/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003355/// specifies a shuffle of elements that is suitable for input to 128-bit
3356/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003357static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003358 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003359
3360 if (VT.getSizeInBits() != 128)
3361 return false;
3362
Nate Begeman9008ca62009-04-27 18:41:29 +00003363 if (NumElems != 2 && NumElems != 4)
3364 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003365
Nate Begeman9008ca62009-04-27 18:41:29 +00003366 int Half = NumElems / 2;
3367 for (int i = 0; i < Half; ++i)
3368 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003369 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003370 for (int i = Half; i < NumElems; ++i)
3371 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003372 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003373
Evan Cheng14aed5e2006-03-24 01:18:28 +00003374 return true;
3375}
3376
Nate Begeman9008ca62009-04-27 18:41:29 +00003377bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3378 SmallVector<int, 8> M;
3379 N->getMask(M);
3380 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003381}
3382
Evan Cheng213d2cf2007-05-17 18:45:50 +00003383/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003384/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3385/// half elements to come from vector 1 (which would equal the dest.) and
3386/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003387static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003388 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003389
3390 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003391 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003392
Nate Begeman9008ca62009-04-27 18:41:29 +00003393 int Half = NumElems / 2;
3394 for (int i = 0; i < Half; ++i)
3395 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003396 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003397 for (int i = Half; i < NumElems; ++i)
3398 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003399 return false;
3400 return true;
3401}
3402
Nate Begeman9008ca62009-04-27 18:41:29 +00003403static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3404 SmallVector<int, 8> M;
3405 N->getMask(M);
3406 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003407}
3408
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003409/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3410/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003411bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003412 EVT VT = N->getValueType(0);
3413 unsigned NumElems = VT.getVectorNumElements();
3414
3415 if (VT.getSizeInBits() != 128)
3416 return false;
3417
3418 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003419 return false;
3420
Evan Cheng2064a2b2006-03-28 06:50:32 +00003421 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003422 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3423 isUndefOrEqual(N->getMaskElt(1), 7) &&
3424 isUndefOrEqual(N->getMaskElt(2), 2) &&
3425 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003426}
3427
Nate Begeman0b10b912009-11-07 23:17:15 +00003428/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3429/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3430/// <2, 3, 2, 3>
3431bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003432 EVT VT = N->getValueType(0);
3433 unsigned NumElems = VT.getVectorNumElements();
3434
3435 if (VT.getSizeInBits() != 128)
3436 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003437
Nate Begeman0b10b912009-11-07 23:17:15 +00003438 if (NumElems != 4)
3439 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003440
Nate Begeman0b10b912009-11-07 23:17:15 +00003441 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003442 isUndefOrEqual(N->getMaskElt(1), 3) &&
3443 isUndefOrEqual(N->getMaskElt(2), 2) &&
3444 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003445}
3446
Evan Cheng5ced1d82006-04-06 23:23:56 +00003447/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3448/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003449bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3450 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003451
Evan Cheng5ced1d82006-04-06 23:23:56 +00003452 if (NumElems != 2 && NumElems != 4)
3453 return false;
3454
Evan Chengc5cdff22006-04-07 21:53:05 +00003455 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003456 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003457 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003458
Evan Chengc5cdff22006-04-07 21:53:05 +00003459 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003460 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003461 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003462
3463 return true;
3464}
3465
Nate Begeman0b10b912009-11-07 23:17:15 +00003466/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3467/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3468bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003469 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003470
David Greenea20244d2011-03-02 17:23:43 +00003471 if ((NumElems != 2 && NumElems != 4)
3472 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003473 return false;
3474
Evan Chengc5cdff22006-04-07 21:53:05 +00003475 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003476 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003477 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003478
Nate Begeman9008ca62009-04-27 18:41:29 +00003479 for (unsigned i = 0; i < NumElems/2; ++i)
3480 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003481 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003482
3483 return true;
3484}
3485
Evan Cheng0038e592006-03-28 00:39:58 +00003486/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3487/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003488static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003489 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003490 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003491
3492 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3493 "Unsupported vector type for unpckh");
3494
3495 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003496 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003497
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003498 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3499 // independently on 128-bit lanes.
3500 unsigned NumLanes = VT.getSizeInBits()/128;
3501 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003502
3503 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003504 unsigned End = NumLaneElts;
3505 for (unsigned s = 0; s < NumLanes; ++s) {
3506 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003507 i != End;
3508 i += 2, ++j) {
3509 int BitI = Mask[i];
3510 int BitI1 = Mask[i+1];
3511 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003512 return false;
David Greenea20244d2011-03-02 17:23:43 +00003513 if (V2IsSplat) {
3514 if (!isUndefOrEqual(BitI1, NumElts))
3515 return false;
3516 } else {
3517 if (!isUndefOrEqual(BitI1, j + NumElts))
3518 return false;
3519 }
Evan Cheng39623da2006-04-20 08:58:49 +00003520 }
David Greenea20244d2011-03-02 17:23:43 +00003521 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003522 Start += NumLaneElts;
3523 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003524 }
David Greenea20244d2011-03-02 17:23:43 +00003525
Evan Cheng0038e592006-03-28 00:39:58 +00003526 return true;
3527}
3528
Nate Begeman9008ca62009-04-27 18:41:29 +00003529bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3530 SmallVector<int, 8> M;
3531 N->getMask(M);
3532 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003533}
3534
Evan Cheng4fcb9222006-03-28 02:43:26 +00003535/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3536/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003537static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003538 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003539 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003540
3541 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3542 "Unsupported vector type for unpckh");
3543
3544 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003545 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003546
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003547 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3548 // independently on 128-bit lanes.
3549 unsigned NumLanes = VT.getSizeInBits()/128;
3550 unsigned NumLaneElts = NumElts/NumLanes;
3551
3552 unsigned Start = 0;
3553 unsigned End = NumLaneElts;
3554 for (unsigned l = 0; l != NumLanes; ++l) {
3555 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3556 i != End; i += 2, ++j) {
3557 int BitI = Mask[i];
3558 int BitI1 = Mask[i+1];
3559 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003560 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003561 if (V2IsSplat) {
3562 if (isUndefOrEqual(BitI1, NumElts))
3563 return false;
3564 } else {
3565 if (!isUndefOrEqual(BitI1, j+NumElts))
3566 return false;
3567 }
Evan Cheng39623da2006-04-20 08:58:49 +00003568 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003569 // Process the next 128 bits.
3570 Start += NumLaneElts;
3571 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003572 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003573 return true;
3574}
3575
Nate Begeman9008ca62009-04-27 18:41:29 +00003576bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3577 SmallVector<int, 8> M;
3578 N->getMask(M);
3579 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003580}
3581
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003582/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3583/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3584/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003585static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003586 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003587 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003588 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003589
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003590 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3591 // FIXME: Need a better way to get rid of this, there's no latency difference
3592 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3593 // the former later. We should also remove the "_undef" special mask.
3594 if (NumElems == 4 && VT.getSizeInBits() == 256)
3595 return false;
3596
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003597 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3598 // independently on 128-bit lanes.
3599 unsigned NumLanes = VT.getSizeInBits() / 128;
3600 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003601
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003602 for (unsigned s = 0; s < NumLanes; ++s) {
3603 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3604 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003605 i += 2, ++j) {
3606 int BitI = Mask[i];
3607 int BitI1 = Mask[i+1];
3608
3609 if (!isUndefOrEqual(BitI, j))
3610 return false;
3611 if (!isUndefOrEqual(BitI1, j))
3612 return false;
3613 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003614 }
David Greenea20244d2011-03-02 17:23:43 +00003615
Rafael Espindola15684b22009-04-24 12:40:33 +00003616 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003617}
3618
Nate Begeman9008ca62009-04-27 18:41:29 +00003619bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3620 SmallVector<int, 8> M;
3621 N->getMask(M);
3622 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3623}
3624
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003625/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3626/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3627/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003628static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003629 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003630 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3631 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003632
Nate Begeman9008ca62009-04-27 18:41:29 +00003633 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3634 int BitI = Mask[i];
3635 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003636 if (!isUndefOrEqual(BitI, j))
3637 return false;
3638 if (!isUndefOrEqual(BitI1, j))
3639 return false;
3640 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003641 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003642}
3643
Nate Begeman9008ca62009-04-27 18:41:29 +00003644bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3645 SmallVector<int, 8> M;
3646 N->getMask(M);
3647 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3648}
3649
Evan Cheng017dcc62006-04-21 01:05:10 +00003650/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3651/// specifies a shuffle of elements that is suitable for input to MOVSS,
3652/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003653static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003654 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003655 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003656
3657 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003658
Nate Begeman9008ca62009-04-27 18:41:29 +00003659 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003660 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003661
Nate Begeman9008ca62009-04-27 18:41:29 +00003662 for (int i = 1; i < NumElts; ++i)
3663 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003664 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003665
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003666 return true;
3667}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003668
Nate Begeman9008ca62009-04-27 18:41:29 +00003669bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3670 SmallVector<int, 8> M;
3671 N->getMask(M);
3672 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003673}
3674
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003675/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3676/// as permutations between 128-bit chunks or halves. As an example: this
3677/// shuffle bellow:
3678/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3679/// The first half comes from the second half of V1 and the second half from the
3680/// the second half of V2.
3681static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3682 const X86Subtarget *Subtarget) {
3683 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3684 return false;
3685
3686 // The shuffle result is divided into half A and half B. In total the two
3687 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3688 // B must come from C, D, E or F.
3689 int HalfSize = VT.getVectorNumElements()/2;
3690 bool MatchA = false, MatchB = false;
3691
3692 // Check if A comes from one of C, D, E, F.
3693 for (int Half = 0; Half < 4; ++Half) {
3694 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3695 MatchA = true;
3696 break;
3697 }
3698 }
3699
3700 // Check if B comes from one of C, D, E, F.
3701 for (int Half = 0; Half < 4; ++Half) {
3702 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3703 MatchB = true;
3704 break;
3705 }
3706 }
3707
3708 return MatchA && MatchB;
3709}
3710
3711/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3712/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3713static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3714 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3715 EVT VT = SVOp->getValueType(0);
3716
3717 int HalfSize = VT.getVectorNumElements()/2;
3718
3719 int FstHalf = 0, SndHalf = 0;
3720 for (int i = 0; i < HalfSize; ++i) {
3721 if (SVOp->getMaskElt(i) > 0) {
3722 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3723 break;
3724 }
3725 }
3726 for (int i = HalfSize; i < HalfSize*2; ++i) {
3727 if (SVOp->getMaskElt(i) > 0) {
3728 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3729 break;
3730 }
3731 }
3732
3733 return (FstHalf | (SndHalf << 4));
3734}
3735
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003736/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3737/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3738/// Note that VPERMIL mask matching is different depending whether theunderlying
3739/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3740/// to the same elements of the low, but to the higher half of the source.
3741/// In VPERMILPD the two lanes could be shuffled independently of each other
3742/// with the same restriction that lanes can't be crossed.
3743static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3744 const X86Subtarget *Subtarget) {
3745 int NumElts = VT.getVectorNumElements();
3746 int NumLanes = VT.getSizeInBits()/128;
3747
3748 if (!Subtarget->hasAVX())
3749 return false;
3750
3751 // Match any permutation of 128-bit vector with 64-bit types
3752 if (NumLanes == 1 && NumElts != 2)
3753 return false;
3754
3755 // Only match 256-bit with 32 types
3756 if (VT.getSizeInBits() == 256 && NumElts != 4)
3757 return false;
3758
3759 // The mask on the high lane is independent of the low. Both can match
3760 // any element in inside its own lane, but can't cross.
3761 int LaneSize = NumElts/NumLanes;
3762 for (int l = 0; l < NumLanes; ++l)
3763 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3764 int LaneStart = l*LaneSize;
3765 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3766 return false;
3767 }
3768
3769 return true;
3770}
3771
3772/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3773/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3774/// Note that VPERMIL mask matching is different depending whether theunderlying
3775/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3776/// to the same elements of the low, but to the higher half of the source.
3777/// In VPERMILPD the two lanes could be shuffled independently of each other
3778/// with the same restriction that lanes can't be crossed.
3779static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3780 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003781 unsigned NumElts = VT.getVectorNumElements();
3782 unsigned NumLanes = VT.getSizeInBits()/128;
3783
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003784 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003785 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003786
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003787 // Match any permutation of 128-bit vector with 32-bit types
3788 if (NumLanes == 1 && NumElts != 4)
3789 return false;
3790
3791 // Only match 256-bit with 32 types
3792 if (VT.getSizeInBits() == 256 && NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003793 return false;
3794
3795 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003796 // they can differ if any of the corresponding index in a lane is undef
3797 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003798 int LaneSize = NumElts/NumLanes;
3799 for (int i = 0; i < LaneSize; ++i) {
3800 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003801 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3802 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3803
3804 if (!HighValid || !LowValid)
3805 return false;
3806 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003807 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003808 if (Mask[HighElt]-Mask[i] != LaneSize)
3809 return false;
3810 }
3811
3812 return true;
3813}
3814
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003815/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3816/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3817static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003818 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3819 EVT VT = SVOp->getValueType(0);
3820
3821 int NumElts = VT.getVectorNumElements();
3822 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003823 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003824
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003825 // Although the mask is equal for both lanes do it twice to get the cases
3826 // where a mask will match because the same mask element is undef on the
3827 // first half but valid on the second. This would get pathological cases
3828 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003829 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003830 for (int l = 0; l < NumLanes; ++l) {
3831 for (int i = 0; i < LaneSize; ++i) {
3832 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3833 if (MaskElt < 0)
3834 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003835 if (MaskElt >= LaneSize)
3836 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003837 Mask |= MaskElt << (i*2);
3838 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003839 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003840
3841 return Mask;
3842}
3843
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003844/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3845/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3846static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3847 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3848 EVT VT = SVOp->getValueType(0);
3849
3850 int NumElts = VT.getVectorNumElements();
3851 int NumLanes = VT.getSizeInBits()/128;
3852
3853 unsigned Mask = 0;
3854 int LaneSize = NumElts/NumLanes;
3855 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003856 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3857 int MaskElt = SVOp->getMaskElt(i);
3858 if (MaskElt < 0)
3859 continue;
3860 Mask |= (MaskElt-l*LaneSize) << i;
3861 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003862
3863 return Mask;
3864}
3865
Evan Cheng017dcc62006-04-21 01:05:10 +00003866/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3867/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003868/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003869static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003870 bool V2IsSplat = false, bool V2IsUndef = false) {
3871 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003872 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003873 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003874
Nate Begeman9008ca62009-04-27 18:41:29 +00003875 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003876 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003877
Nate Begeman9008ca62009-04-27 18:41:29 +00003878 for (int i = 1; i < NumOps; ++i)
3879 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3880 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3881 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003882 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003883
Evan Cheng39623da2006-04-20 08:58:49 +00003884 return true;
3885}
3886
Nate Begeman9008ca62009-04-27 18:41:29 +00003887static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003888 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003889 SmallVector<int, 8> M;
3890 N->getMask(M);
3891 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003892}
3893
Evan Chengd9539472006-04-14 21:59:03 +00003894/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3895/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003896/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3897bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3898 const X86Subtarget *Subtarget) {
3899 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003900 return false;
3901
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003902 // The second vector must be undef
3903 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3904 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003905
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003906 EVT VT = N->getValueType(0);
3907 unsigned NumElems = VT.getVectorNumElements();
3908
3909 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3910 (VT.getSizeInBits() == 256 && NumElems != 8))
3911 return false;
3912
3913 // "i+1" is the value the indexed mask element must have
3914 for (unsigned i = 0; i < NumElems; i += 2)
3915 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3916 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003917 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003918
3919 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003920}
3921
3922/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3923/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003924/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3925bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3926 const X86Subtarget *Subtarget) {
3927 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003928 return false;
3929
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003930 // The second vector must be undef
3931 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3932 return false;
3933
3934 EVT VT = N->getValueType(0);
3935 unsigned NumElems = VT.getVectorNumElements();
3936
3937 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3938 (VT.getSizeInBits() == 256 && NumElems != 8))
3939 return false;
3940
3941 // "i" is the value the indexed mask element must have
3942 for (unsigned i = 0; i < NumElems; i += 2)
3943 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3944 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003945 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003946
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003947 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003948}
3949
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003950/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3951/// specifies a shuffle of elements that is suitable for input to 256-bit
3952/// version of MOVDDUP.
3953static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
3954 const X86Subtarget *Subtarget) {
3955 EVT VT = N->getValueType(0);
3956 int NumElts = VT.getVectorNumElements();
3957 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
3958
3959 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
3960 !V2IsUndef || NumElts != 4)
3961 return false;
3962
3963 for (int i = 0; i != NumElts/2; ++i)
3964 if (!isUndefOrEqual(N->getMaskElt(i), 0))
3965 return false;
3966 for (int i = NumElts/2; i != NumElts; ++i)
3967 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
3968 return false;
3969 return true;
3970}
3971
Evan Cheng0b457f02008-09-25 20:50:48 +00003972/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003973/// specifies a shuffle of elements that is suitable for input to 128-bit
3974/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003975bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003976 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003977
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003978 if (VT.getSizeInBits() != 128)
3979 return false;
3980
3981 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003982 for (int i = 0; i < e; ++i)
3983 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003984 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003985 for (int i = 0; i < e; ++i)
3986 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003987 return false;
3988 return true;
3989}
3990
David Greenec38a03e2011-02-03 15:50:00 +00003991/// isVEXTRACTF128Index - Return true if the specified
3992/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3993/// suitable for input to VEXTRACTF128.
3994bool X86::isVEXTRACTF128Index(SDNode *N) {
3995 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3996 return false;
3997
3998 // The index should be aligned on a 128-bit boundary.
3999 uint64_t Index =
4000 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4001
4002 unsigned VL = N->getValueType(0).getVectorNumElements();
4003 unsigned VBits = N->getValueType(0).getSizeInBits();
4004 unsigned ElSize = VBits / VL;
4005 bool Result = (Index * ElSize) % 128 == 0;
4006
4007 return Result;
4008}
4009
David Greeneccacdc12011-02-04 16:08:29 +00004010/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4011/// operand specifies a subvector insert that is suitable for input to
4012/// VINSERTF128.
4013bool X86::isVINSERTF128Index(SDNode *N) {
4014 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4015 return false;
4016
4017 // The index should be aligned on a 128-bit boundary.
4018 uint64_t Index =
4019 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4020
4021 unsigned VL = N->getValueType(0).getVectorNumElements();
4022 unsigned VBits = N->getValueType(0).getSizeInBits();
4023 unsigned ElSize = VBits / VL;
4024 bool Result = (Index * ElSize) % 128 == 0;
4025
4026 return Result;
4027}
4028
Evan Cheng63d33002006-03-22 08:01:21 +00004029/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004030/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004031unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004032 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4033 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4034
Evan Chengb9df0ca2006-03-22 02:53:00 +00004035 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4036 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004037 for (int i = 0; i < NumOperands; ++i) {
4038 int Val = SVOp->getMaskElt(NumOperands-i-1);
4039 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004040 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004041 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004042 if (i != NumOperands - 1)
4043 Mask <<= Shift;
4044 }
Evan Cheng63d33002006-03-22 08:01:21 +00004045 return Mask;
4046}
4047
Evan Cheng506d3df2006-03-29 23:07:14 +00004048/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004049/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004050unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004051 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004052 unsigned Mask = 0;
4053 // 8 nodes, but we only care about the last 4.
4054 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004055 int Val = SVOp->getMaskElt(i);
4056 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004057 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004058 if (i != 4)
4059 Mask <<= 2;
4060 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004061 return Mask;
4062}
4063
4064/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004065/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004066unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004067 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004068 unsigned Mask = 0;
4069 // 8 nodes, but we only care about the first 4.
4070 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004071 int Val = SVOp->getMaskElt(i);
4072 if (Val >= 0)
4073 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004074 if (i != 0)
4075 Mask <<= 2;
4076 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004077 return Mask;
4078}
4079
Nate Begemana09008b2009-10-19 02:17:23 +00004080/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4081/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4082unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4083 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4084 EVT VVT = N->getValueType(0);
4085 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4086 int Val = 0;
4087
4088 unsigned i, e;
4089 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4090 Val = SVOp->getMaskElt(i);
4091 if (Val >= 0)
4092 break;
4093 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004094 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004095 return (Val - i) * EltSize;
4096}
4097
David Greenec38a03e2011-02-03 15:50:00 +00004098/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4099/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4100/// instructions.
4101unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4102 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4103 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4104
4105 uint64_t Index =
4106 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4107
4108 EVT VecVT = N->getOperand(0).getValueType();
4109 EVT ElVT = VecVT.getVectorElementType();
4110
4111 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004112 return Index / NumElemsPerChunk;
4113}
4114
David Greeneccacdc12011-02-04 16:08:29 +00004115/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4116/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4117/// instructions.
4118unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4119 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4120 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4121
4122 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004123 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004124
4125 EVT VecVT = N->getValueType(0);
4126 EVT ElVT = VecVT.getVectorElementType();
4127
4128 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004129 return Index / NumElemsPerChunk;
4130}
4131
Evan Cheng37b73872009-07-30 08:33:02 +00004132/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4133/// constant +0.0.
4134bool X86::isZeroNode(SDValue Elt) {
4135 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004136 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004137 (isa<ConstantFPSDNode>(Elt) &&
4138 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4139}
4140
Nate Begeman9008ca62009-04-27 18:41:29 +00004141/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4142/// their permute mask.
4143static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4144 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004145 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004146 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004147 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004148
Nate Begeman5a5ca152009-04-29 05:20:52 +00004149 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004150 int idx = SVOp->getMaskElt(i);
4151 if (idx < 0)
4152 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004153 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004154 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004155 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004156 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004157 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004158 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4159 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004160}
4161
Evan Cheng779ccea2007-12-07 21:30:01 +00004162/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4163/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004164static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004165 unsigned NumElems = VT.getVectorNumElements();
4166 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004167 int idx = Mask[i];
4168 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004169 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004170 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004171 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004172 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004173 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004174 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004175}
4176
Evan Cheng533a0aa2006-04-19 20:35:22 +00004177/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4178/// match movhlps. The lower half elements should come from upper half of
4179/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004180/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004181static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004182 EVT VT = Op->getValueType(0);
4183 if (VT.getSizeInBits() != 128)
4184 return false;
4185 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004186 return false;
4187 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004188 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004189 return false;
4190 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004191 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004192 return false;
4193 return true;
4194}
4195
Evan Cheng5ced1d82006-04-06 23:23:56 +00004196/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004197/// is promoted to a vector. It also returns the LoadSDNode by reference if
4198/// required.
4199static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004200 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4201 return false;
4202 N = N->getOperand(0).getNode();
4203 if (!ISD::isNON_EXTLoad(N))
4204 return false;
4205 if (LD)
4206 *LD = cast<LoadSDNode>(N);
4207 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004208}
4209
Evan Cheng533a0aa2006-04-19 20:35:22 +00004210/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4211/// match movlp{s|d}. The lower half elements should come from lower half of
4212/// V1 (and in order), and the upper half elements should come from the upper
4213/// half of V2 (and in order). And since V1 will become the source of the
4214/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004215static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4216 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004217 EVT VT = Op->getValueType(0);
4218 if (VT.getSizeInBits() != 128)
4219 return false;
4220
Evan Cheng466685d2006-10-09 20:57:25 +00004221 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004222 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004223 // Is V2 is a vector load, don't do this transformation. We will try to use
4224 // load folding shufps op.
4225 if (ISD::isNON_EXTLoad(V2))
4226 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004227
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004228 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004229
Evan Cheng533a0aa2006-04-19 20:35:22 +00004230 if (NumElems != 2 && NumElems != 4)
4231 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004232 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004233 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004234 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004235 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004236 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004237 return false;
4238 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004239}
4240
Evan Cheng39623da2006-04-20 08:58:49 +00004241/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4242/// all the same.
4243static bool isSplatVector(SDNode *N) {
4244 if (N->getOpcode() != ISD::BUILD_VECTOR)
4245 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004246
Dan Gohman475871a2008-07-27 21:46:04 +00004247 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004248 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4249 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004250 return false;
4251 return true;
4252}
4253
Evan Cheng213d2cf2007-05-17 18:45:50 +00004254/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004255/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004256/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004257static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004258 SDValue V1 = N->getOperand(0);
4259 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004260 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4261 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004262 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004263 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004264 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004265 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4266 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004267 if (Opc != ISD::BUILD_VECTOR ||
4268 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004269 return false;
4270 } else if (Idx >= 0) {
4271 unsigned Opc = V1.getOpcode();
4272 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4273 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004274 if (Opc != ISD::BUILD_VECTOR ||
4275 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004276 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004277 }
4278 }
4279 return true;
4280}
4281
4282/// getZeroVector - Returns a vector of specified type with all zero elements.
4283///
Owen Andersone50ed302009-08-10 22:56:29 +00004284static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004285 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004286 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004287
Dale Johannesen0488fb62010-09-30 23:57:10 +00004288 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004289 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004290 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004291 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004292 if (HasSSE2) { // SSE2
4293 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4294 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4295 } else { // SSE1
4296 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4297 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4298 }
4299 } else if (VT.getSizeInBits() == 256) { // AVX
4300 // 256-bit logic and arithmetic instructions in AVX are
4301 // all floating-point, no support for integer ops. Default
4302 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004303 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004304 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4305 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004306 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004307 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004308}
4309
Chris Lattner8a594482007-11-25 00:24:49 +00004310/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004311/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4312/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4313/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004314static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004315 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004316 assert((VT.is128BitVector() || VT.is256BitVector())
4317 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004318
Owen Anderson825b72b2009-08-11 20:47:22 +00004319 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004320 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4321 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004322
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004323 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004324 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4325 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4326 Vec = Insert128BitVector(InsV, Vec,
4327 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4328 }
4329
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004330 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004331}
4332
Evan Cheng39623da2006-04-20 08:58:49 +00004333/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4334/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004335static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004336 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004337 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004338
Evan Cheng39623da2006-04-20 08:58:49 +00004339 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004340 SmallVector<int, 8> MaskVec;
4341 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004342
Nate Begeman5a5ca152009-04-29 05:20:52 +00004343 for (unsigned i = 0; i != NumElems; ++i) {
4344 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004345 MaskVec[i] = NumElems;
4346 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004347 }
Evan Cheng39623da2006-04-20 08:58:49 +00004348 }
Evan Cheng39623da2006-04-20 08:58:49 +00004349 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004350 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4351 SVOp->getOperand(1), &MaskVec[0]);
4352 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004353}
4354
Evan Cheng017dcc62006-04-21 01:05:10 +00004355/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4356/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004357static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004358 SDValue V2) {
4359 unsigned NumElems = VT.getVectorNumElements();
4360 SmallVector<int, 8> Mask;
4361 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004362 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004363 Mask.push_back(i);
4364 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004365}
4366
Nate Begeman9008ca62009-04-27 18:41:29 +00004367/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004368static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004369 SDValue V2) {
4370 unsigned NumElems = VT.getVectorNumElements();
4371 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004372 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004373 Mask.push_back(i);
4374 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004375 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004377}
4378
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004379/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004380static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004381 SDValue V2) {
4382 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004383 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004384 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004385 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004386 Mask.push_back(i + Half);
4387 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004388 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004389 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004390}
4391
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004392// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004393// a generic shuffle instruction because the target has no such instructions.
4394// Generate shuffles which repeat i16 and i8 several times until they can be
4395// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004396static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004397 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004398 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004399 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004400
Nate Begeman9008ca62009-04-27 18:41:29 +00004401 while (NumElems > 4) {
4402 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004403 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004404 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004405 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004406 EltNo -= NumElems/2;
4407 }
4408 NumElems >>= 1;
4409 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004410 return V;
4411}
Eric Christopherfd179292009-08-27 18:07:15 +00004412
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004413/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4414static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4415 EVT VT = V.getValueType();
4416 DebugLoc dl = V.getDebugLoc();
4417 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4418 && "Vector size not supported");
4419
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004420 if (VT.getSizeInBits() == 128) {
4421 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004422 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004423 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4424 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004425 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004426 // To use VPERMILPS to splat scalars, the second half of indicies must
4427 // refer to the higher part, which is a duplication of the lower one,
4428 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004429 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4430 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004431
4432 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4433 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4434 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004435 }
4436
4437 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4438}
4439
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004440/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004441static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4442 EVT SrcVT = SV->getValueType(0);
4443 SDValue V1 = SV->getOperand(0);
4444 DebugLoc dl = SV->getDebugLoc();
4445
4446 int EltNo = SV->getSplatIndex();
4447 int NumElems = SrcVT.getVectorNumElements();
4448 unsigned Size = SrcVT.getSizeInBits();
4449
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004450 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4451 "Unknown how to promote splat for type");
4452
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004453 // Extract the 128-bit part containing the splat element and update
4454 // the splat element index when it refers to the higher register.
4455 if (Size == 256) {
4456 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4457 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4458 if (Idx > 0)
4459 EltNo -= NumElems/2;
4460 }
4461
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004462 // All i16 and i8 vector types can't be used directly by a generic shuffle
4463 // instruction because the target has no such instruction. Generate shuffles
4464 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004465 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004466 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004467 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004468 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004469
4470 // Recreate the 256-bit vector and place the same 128-bit vector
4471 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004472 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004473 if (Size == 256) {
4474 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4475 DAG.getConstant(0, MVT::i32), DAG, dl);
4476 V1 = Insert128BitVector(InsV, V1,
4477 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4478 }
4479
4480 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004481}
4482
Evan Chengba05f722006-04-21 23:03:30 +00004483/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004484/// vector of zero or undef vector. This produces a shuffle where the low
4485/// element of V2 is swizzled into the zero/undef vector, landing at element
4486/// 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 +00004487static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00004488 bool isZero, bool HasSSE2,
4489 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004490 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004491 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00004492 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
4493 unsigned NumElems = VT.getVectorNumElements();
4494 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004495 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004496 // If this is the insertion idx, put the low elt of V2 here.
4497 MaskVec.push_back(i == Idx ? NumElems : i);
4498 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004499}
4500
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004501/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4502/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004503static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4504 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004505 if (Depth == 6)
4506 return SDValue(); // Limit search depth.
4507
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004508 SDValue V = SDValue(N, 0);
4509 EVT VT = V.getValueType();
4510 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004511
4512 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4513 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4514 Index = SV->getMaskElt(Index);
4515
4516 if (Index < 0)
4517 return DAG.getUNDEF(VT.getVectorElementType());
4518
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004519 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004520 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004521 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004522 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004523
4524 // Recurse into target specific vector shuffles to find scalars.
4525 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004526 int NumElems = VT.getVectorNumElements();
4527 SmallVector<unsigned, 16> ShuffleMask;
4528 SDValue ImmN;
4529
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004530 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004531 case X86ISD::SHUFPS:
4532 case X86ISD::SHUFPD:
4533 ImmN = N->getOperand(N->getNumOperands()-1);
4534 DecodeSHUFPSMask(NumElems,
4535 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4536 ShuffleMask);
4537 break;
4538 case X86ISD::PUNPCKHBW:
4539 case X86ISD::PUNPCKHWD:
4540 case X86ISD::PUNPCKHDQ:
4541 case X86ISD::PUNPCKHQDQ:
4542 DecodePUNPCKHMask(NumElems, ShuffleMask);
4543 break;
4544 case X86ISD::UNPCKHPS:
4545 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004546 case X86ISD::VUNPCKHPSY:
4547 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004548 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4549 break;
4550 case X86ISD::PUNPCKLBW:
4551 case X86ISD::PUNPCKLWD:
4552 case X86ISD::PUNPCKLDQ:
4553 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004554 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004555 break;
4556 case X86ISD::UNPCKLPS:
4557 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004558 case X86ISD::VUNPCKLPSY:
4559 case X86ISD::VUNPCKLPDY:
4560 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004561 break;
4562 case X86ISD::MOVHLPS:
4563 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4564 break;
4565 case X86ISD::MOVLHPS:
4566 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4567 break;
4568 case X86ISD::PSHUFD:
4569 ImmN = N->getOperand(N->getNumOperands()-1);
4570 DecodePSHUFMask(NumElems,
4571 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4572 ShuffleMask);
4573 break;
4574 case X86ISD::PSHUFHW:
4575 ImmN = N->getOperand(N->getNumOperands()-1);
4576 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4577 ShuffleMask);
4578 break;
4579 case X86ISD::PSHUFLW:
4580 ImmN = N->getOperand(N->getNumOperands()-1);
4581 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4582 ShuffleMask);
4583 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004584 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004585 case X86ISD::MOVSD: {
4586 // The index 0 always comes from the first element of the second source,
4587 // this is why MOVSS and MOVSD are used in the first place. The other
4588 // elements come from the other positions of the first source vector.
4589 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004590 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4591 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004592 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004593 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004594 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004595 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004596 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004597 break;
4598 case X86ISD::VPERMILPSY:
4599 ImmN = N->getOperand(N->getNumOperands()-1);
4600 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4601 ShuffleMask);
4602 break;
4603 case X86ISD::VPERMILPD:
4604 ImmN = N->getOperand(N->getNumOperands()-1);
4605 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4606 ShuffleMask);
4607 break;
4608 case X86ISD::VPERMILPDY:
4609 ImmN = N->getOperand(N->getNumOperands()-1);
4610 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4611 ShuffleMask);
4612 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004613 case X86ISD::VPERM2F128:
4614 ImmN = N->getOperand(N->getNumOperands()-1);
4615 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4616 ShuffleMask);
4617 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004618 case X86ISD::MOVDDUP:
4619 case X86ISD::MOVLHPD:
4620 case X86ISD::MOVLPD:
4621 case X86ISD::MOVLPS:
4622 case X86ISD::MOVSHDUP:
4623 case X86ISD::MOVSLDUP:
4624 case X86ISD::PALIGN:
4625 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004626 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004627 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004628 return SDValue();
4629 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004630
4631 Index = ShuffleMask[Index];
4632 if (Index < 0)
4633 return DAG.getUNDEF(VT.getVectorElementType());
4634
4635 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4636 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4637 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004638 }
4639
4640 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004641 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004642 V = V.getOperand(0);
4643 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004644 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004645
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004646 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004647 return SDValue();
4648 }
4649
4650 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4651 return (Index == 0) ? V.getOperand(0)
4652 : DAG.getUNDEF(VT.getVectorElementType());
4653
4654 if (V.getOpcode() == ISD::BUILD_VECTOR)
4655 return V.getOperand(Index);
4656
4657 return SDValue();
4658}
4659
4660/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4661/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004662/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004663static
4664unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4665 bool ZerosFromLeft, SelectionDAG &DAG) {
4666 int i = 0;
4667
4668 while (i < NumElems) {
4669 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004670 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004671 if (!(Elt.getNode() &&
4672 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4673 break;
4674 ++i;
4675 }
4676
4677 return i;
4678}
4679
4680/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4681/// MaskE correspond consecutively to elements from one of the vector operands,
4682/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4683static
4684bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4685 int OpIdx, int NumElems, unsigned &OpNum) {
4686 bool SeenV1 = false;
4687 bool SeenV2 = false;
4688
4689 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4690 int Idx = SVOp->getMaskElt(i);
4691 // Ignore undef indicies
4692 if (Idx < 0)
4693 continue;
4694
4695 if (Idx < NumElems)
4696 SeenV1 = true;
4697 else
4698 SeenV2 = true;
4699
4700 // Only accept consecutive elements from the same vector
4701 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4702 return false;
4703 }
4704
4705 OpNum = SeenV1 ? 0 : 1;
4706 return true;
4707}
4708
4709/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4710/// logical left shift of a vector.
4711static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4712 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4713 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4714 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4715 false /* check zeros from right */, DAG);
4716 unsigned OpSrc;
4717
4718 if (!NumZeros)
4719 return false;
4720
4721 // Considering the elements in the mask that are not consecutive zeros,
4722 // check if they consecutively come from only one of the source vectors.
4723 //
4724 // V1 = {X, A, B, C} 0
4725 // \ \ \ /
4726 // vector_shuffle V1, V2 <1, 2, 3, X>
4727 //
4728 if (!isShuffleMaskConsecutive(SVOp,
4729 0, // Mask Start Index
4730 NumElems-NumZeros-1, // Mask End Index
4731 NumZeros, // Where to start looking in the src vector
4732 NumElems, // Number of elements in vector
4733 OpSrc)) // Which source operand ?
4734 return false;
4735
4736 isLeft = false;
4737 ShAmt = NumZeros;
4738 ShVal = SVOp->getOperand(OpSrc);
4739 return true;
4740}
4741
4742/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4743/// logical left shift of a vector.
4744static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4745 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4746 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4747 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4748 true /* check zeros from left */, DAG);
4749 unsigned OpSrc;
4750
4751 if (!NumZeros)
4752 return false;
4753
4754 // Considering the elements in the mask that are not consecutive zeros,
4755 // check if they consecutively come from only one of the source vectors.
4756 //
4757 // 0 { A, B, X, X } = V2
4758 // / \ / /
4759 // vector_shuffle V1, V2 <X, X, 4, 5>
4760 //
4761 if (!isShuffleMaskConsecutive(SVOp,
4762 NumZeros, // Mask Start Index
4763 NumElems-1, // Mask End Index
4764 0, // Where to start looking in the src vector
4765 NumElems, // Number of elements in vector
4766 OpSrc)) // Which source operand ?
4767 return false;
4768
4769 isLeft = true;
4770 ShAmt = NumZeros;
4771 ShVal = SVOp->getOperand(OpSrc);
4772 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004773}
4774
4775/// isVectorShift - Returns true if the shuffle can be implemented as a
4776/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004777static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004778 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004779 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4780 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4781 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004782
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004783 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004784}
4785
Evan Chengc78d3b42006-04-24 18:01:45 +00004786/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4787///
Dan Gohman475871a2008-07-27 21:46:04 +00004788static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004789 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004790 SelectionDAG &DAG,
4791 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004792 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004793 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004794
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004795 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004796 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004797 bool First = true;
4798 for (unsigned i = 0; i < 16; ++i) {
4799 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4800 if (ThisIsNonZero && First) {
4801 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004802 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004803 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004804 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004805 First = false;
4806 }
4807
4808 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004809 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004810 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4811 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004812 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004813 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004814 }
4815 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004816 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4817 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4818 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004819 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004820 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004821 } else
4822 ThisElt = LastElt;
4823
Gabor Greifba36cb52008-08-28 21:40:38 +00004824 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004825 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004826 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004827 }
4828 }
4829
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004830 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004831}
4832
Bill Wendlinga348c562007-03-22 18:42:45 +00004833/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004834///
Dan Gohman475871a2008-07-27 21:46:04 +00004835static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004836 unsigned NumNonZero, unsigned NumZero,
4837 SelectionDAG &DAG,
4838 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004839 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004840 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004841
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004842 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004843 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004844 bool First = true;
4845 for (unsigned i = 0; i < 8; ++i) {
4846 bool isNonZero = (NonZeros & (1 << i)) != 0;
4847 if (isNonZero) {
4848 if (First) {
4849 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004850 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004851 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004852 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004853 First = false;
4854 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004855 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004856 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004857 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004858 }
4859 }
4860
4861 return V;
4862}
4863
Evan Chengf26ffe92008-05-29 08:22:04 +00004864/// getVShift - Return a vector logical shift node.
4865///
Owen Andersone50ed302009-08-10 22:56:29 +00004866static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004867 unsigned NumBits, SelectionDAG &DAG,
4868 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004869 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004870 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004871 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4872 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004873 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004874 DAG.getConstant(NumBits,
4875 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004876}
4877
Dan Gohman475871a2008-07-27 21:46:04 +00004878SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004879X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004880 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004881
Evan Chengc3630942009-12-09 21:00:30 +00004882 // Check if the scalar load can be widened into a vector load. And if
4883 // the address is "base + cst" see if the cst can be "absorbed" into
4884 // the shuffle mask.
4885 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4886 SDValue Ptr = LD->getBasePtr();
4887 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4888 return SDValue();
4889 EVT PVT = LD->getValueType(0);
4890 if (PVT != MVT::i32 && PVT != MVT::f32)
4891 return SDValue();
4892
4893 int FI = -1;
4894 int64_t Offset = 0;
4895 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4896 FI = FINode->getIndex();
4897 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004898 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004899 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4900 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4901 Offset = Ptr.getConstantOperandVal(1);
4902 Ptr = Ptr.getOperand(0);
4903 } else {
4904 return SDValue();
4905 }
4906
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004907 // FIXME: 256-bit vector instructions don't require a strict alignment,
4908 // improve this code to support it better.
4909 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004910 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004911 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004912 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004913 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004914 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004915 // Can't change the alignment. FIXME: It's possible to compute
4916 // the exact stack offset and reference FI + adjust offset instead.
4917 // If someone *really* cares about this. That's the way to implement it.
4918 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004919 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004920 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004921 }
4922 }
4923
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004924 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004925 // Ptr + (Offset & ~15).
4926 if (Offset < 0)
4927 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004928 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004929 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004930 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004931 if (StartOffset)
4932 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4933 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4934
4935 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004936 int NumElems = VT.getVectorNumElements();
4937
4938 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4939 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4940 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004941 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004942 false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004943
4944 // Canonicalize it to a v4i32 or v8i32 shuffle.
4945 SmallVector<int, 8> Mask;
4946 for (int i = 0; i < NumElems; ++i)
4947 Mask.push_back(EltNo);
4948
4949 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4950 return DAG.getNode(ISD::BITCAST, dl, NVT,
4951 DAG.getVectorShuffle(CanonVT, dl, V1,
4952 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004953 }
4954
4955 return SDValue();
4956}
4957
Michael J. Spencerec38de22010-10-10 22:04:20 +00004958/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4959/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004960/// load which has the same value as a build_vector whose operands are 'elts'.
4961///
4962/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004963///
Nate Begeman1449f292010-03-24 22:19:06 +00004964/// FIXME: we'd also like to handle the case where the last elements are zero
4965/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4966/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004967static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004968 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004969 EVT EltVT = VT.getVectorElementType();
4970 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004971
Nate Begemanfdea31a2010-03-24 20:49:50 +00004972 LoadSDNode *LDBase = NULL;
4973 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004974
Nate Begeman1449f292010-03-24 22:19:06 +00004975 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004976 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004977 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004978 for (unsigned i = 0; i < NumElems; ++i) {
4979 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004980
Nate Begemanfdea31a2010-03-24 20:49:50 +00004981 if (!Elt.getNode() ||
4982 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4983 return SDValue();
4984 if (!LDBase) {
4985 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4986 return SDValue();
4987 LDBase = cast<LoadSDNode>(Elt.getNode());
4988 LastLoadedElt = i;
4989 continue;
4990 }
4991 if (Elt.getOpcode() == ISD::UNDEF)
4992 continue;
4993
4994 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4995 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4996 return SDValue();
4997 LastLoadedElt = i;
4998 }
Nate Begeman1449f292010-03-24 22:19:06 +00004999
5000 // If we have found an entire vector of loads and undefs, then return a large
5001 // load of the entire vector width starting at the base pointer. If we found
5002 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005003 if (LastLoadedElt == NumElems - 1) {
5004 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005005 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005006 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005007 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005008 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005009 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005010 LDBase->isVolatile(), LDBase->isNonTemporal(),
5011 LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005012 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5013 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005014 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5015 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00005016 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
5017 Ops, 2, MVT::i32,
5018 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005019 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005020 }
5021 return SDValue();
5022}
5023
Evan Chengc3630942009-12-09 21:00:30 +00005024SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005025X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005026 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005027
David Greenef125a292011-02-08 19:04:41 +00005028 EVT VT = Op.getValueType();
5029 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005030 unsigned NumElems = Op.getNumOperands();
5031
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005032 // Vectors containing all zeros can be matched by pxor and xorps later
5033 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5034 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5035 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005036 if (Op.getValueType() == MVT::v4i32 ||
5037 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005038 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005039
Dale Johannesenace16102009-02-03 19:33:06 +00005040 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005041 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005042
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005043 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
5044 // vectors or broken into v4i32 operations on 256-bit vectors.
5045 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5046 if (Op.getValueType() == MVT::v4i32)
5047 return Op;
5048
5049 return getOnesVector(Op.getValueType(), DAG, dl);
5050 }
5051
Owen Andersone50ed302009-08-10 22:56:29 +00005052 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005053
Evan Cheng0db9fe62006-04-25 20:13:52 +00005054 unsigned NumZero = 0;
5055 unsigned NumNonZero = 0;
5056 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005057 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005058 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005059 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005060 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005061 if (Elt.getOpcode() == ISD::UNDEF)
5062 continue;
5063 Values.insert(Elt);
5064 if (Elt.getOpcode() != ISD::Constant &&
5065 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005066 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005067 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005068 NumZero++;
5069 else {
5070 NonZeros |= (1 << i);
5071 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005072 }
5073 }
5074
Chris Lattner97a2a562010-08-26 05:24:29 +00005075 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5076 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005077 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005078
Chris Lattner67f453a2008-03-09 05:42:06 +00005079 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005080 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005081 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005082 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005083
Chris Lattner62098042008-03-09 01:05:04 +00005084 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5085 // the value are obviously zero, truncate the value to i32 and do the
5086 // insertion that way. Only do this if the value is non-constant or if the
5087 // value is a constant being inserted into element 0. It is cheaper to do
5088 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005089 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005090 (!IsAllConstants || Idx == 0)) {
5091 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005092 // Handle SSE only.
5093 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5094 EVT VecVT = MVT::v4i32;
5095 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005096
Chris Lattner62098042008-03-09 01:05:04 +00005097 // Truncate the value (which may itself be a constant) to i32, and
5098 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005099 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005100 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005101 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
5102 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005103
Chris Lattner62098042008-03-09 01:05:04 +00005104 // Now we have our 32-bit value zero extended in the low element of
5105 // a vector. If Idx != 0, swizzle it into place.
5106 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005107 SmallVector<int, 4> Mask;
5108 Mask.push_back(Idx);
5109 for (unsigned i = 1; i != VecElts; ++i)
5110 Mask.push_back(i);
5111 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005112 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005113 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005114 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005115 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005116 }
5117 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005118
Chris Lattner19f79692008-03-08 22:59:52 +00005119 // If we have a constant or non-constant insertion into the low element of
5120 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5121 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005122 // depending on what the source datatype is.
5123 if (Idx == 0) {
5124 if (NumZero == 0) {
5125 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005126 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5127 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005128 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5129 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
5130 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
5131 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005132 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5133 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005134 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5135 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005136 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5137 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
5138 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005139 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005140 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005141 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005142
5143 // Is it a vector logical left shift?
5144 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005145 X86::isZeroNode(Op.getOperand(0)) &&
5146 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005147 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005148 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005149 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005150 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005151 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005152 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005153
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005154 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005155 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005156
Chris Lattner19f79692008-03-08 22:59:52 +00005157 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5158 // is a non-constant being inserted into an element other than the low one,
5159 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5160 // movd/movss) to move this into the low element, then shuffle it into
5161 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005162 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005163 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005164
Evan Cheng0db9fe62006-04-25 20:13:52 +00005165 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005166 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
5167 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005168 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005169 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005170 MaskVec.push_back(i == Idx ? 0 : 1);
5171 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005172 }
5173 }
5174
Chris Lattner67f453a2008-03-09 05:42:06 +00005175 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005176 if (Values.size() == 1) {
5177 if (EVTBits == 32) {
5178 // Instead of a shuffle like this:
5179 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5180 // Check if it's possible to issue this instead.
5181 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5182 unsigned Idx = CountTrailingZeros_32(NonZeros);
5183 SDValue Item = Op.getOperand(Idx);
5184 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5185 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5186 }
Dan Gohman475871a2008-07-27 21:46:04 +00005187 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005188 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005189
Dan Gohmana3941172007-07-24 22:55:08 +00005190 // A vector full of immediates; various special cases are already
5191 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005192 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005193 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005194
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005195 // For AVX-length vectors, build the individual 128-bit pieces and use
5196 // shuffles to put them in place.
5197 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5198 SmallVector<SDValue, 32> V;
5199 for (unsigned i = 0; i < NumElems; ++i)
5200 V.push_back(Op.getOperand(i));
5201
5202 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5203
5204 // Build both the lower and upper subvector.
5205 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5206 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5207 NumElems/2);
5208
5209 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005210 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5211 DAG.getConstant(0, MVT::i32), DAG, dl);
5212 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005213 DAG, dl);
5214 }
5215
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005216 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005217 if (EVTBits == 64) {
5218 if (NumNonZero == 1) {
5219 // One half is zero or undef.
5220 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005221 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005222 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005223 return getShuffleVectorZeroOrUndef(V2, Idx, true,
5224 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005225 }
Dan Gohman475871a2008-07-27 21:46:04 +00005226 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005227 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005228
5229 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005230 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005231 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005232 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005233 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005234 }
5235
Bill Wendling826f36f2007-03-28 00:57:11 +00005236 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005237 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005238 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005239 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005240 }
5241
5242 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005243 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005244 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005245 if (NumElems == 4 && NumZero > 0) {
5246 for (unsigned i = 0; i < 4; ++i) {
5247 bool isZero = !(NonZeros & (1 << i));
5248 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00005249 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005250 else
Dale Johannesenace16102009-02-03 19:33:06 +00005251 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005252 }
5253
5254 for (unsigned i = 0; i < 2; ++i) {
5255 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5256 default: break;
5257 case 0:
5258 V[i] = V[i*2]; // Must be a zero vector.
5259 break;
5260 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005261 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005262 break;
5263 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005264 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005265 break;
5266 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005267 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005268 break;
5269 }
5270 }
5271
Nate Begeman9008ca62009-04-27 18:41:29 +00005272 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005273 bool Reverse = (NonZeros & 0x3) == 2;
5274 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005275 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005276 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5277 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005278 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5279 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005280 }
5281
Nate Begemanfdea31a2010-03-24 20:49:50 +00005282 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5283 // Check for a build vector of consecutive loads.
5284 for (unsigned i = 0; i < NumElems; ++i)
5285 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005286
Nate Begemanfdea31a2010-03-24 20:49:50 +00005287 // Check for elements which are consecutive loads.
5288 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5289 if (LD.getNode())
5290 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005291
5292 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005293 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005294 SDValue Result;
5295 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5296 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5297 else
5298 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005299
Chris Lattner24faf612010-08-28 17:59:08 +00005300 for (unsigned i = 1; i < NumElems; ++i) {
5301 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5302 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005303 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005304 }
5305 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005306 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005307
Chris Lattner6e80e442010-08-28 17:15:43 +00005308 // Otherwise, expand into a number of unpckl*, start by extending each of
5309 // our (non-undef) elements to the full vector width with the element in the
5310 // bottom slot of the vector (which generates no code for SSE).
5311 for (unsigned i = 0; i < NumElems; ++i) {
5312 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5313 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5314 else
5315 V[i] = DAG.getUNDEF(VT);
5316 }
5317
5318 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005319 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5320 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5321 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005322 unsigned EltStride = NumElems >> 1;
5323 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005324 for (unsigned i = 0; i < EltStride; ++i) {
5325 // If V[i+EltStride] is undef and this is the first round of mixing,
5326 // then it is safe to just drop this shuffle: V[i] is already in the
5327 // right place, the one element (since it's the first round) being
5328 // inserted as undef can be dropped. This isn't safe for successive
5329 // rounds because they will permute elements within both vectors.
5330 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5331 EltStride == NumElems/2)
5332 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005333
Chris Lattner6e80e442010-08-28 17:15:43 +00005334 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005335 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005336 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005337 }
5338 return V[0];
5339 }
Dan Gohman475871a2008-07-27 21:46:04 +00005340 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005341}
5342
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005343// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5344// them in a MMX register. This is better than doing a stack convert.
5345static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005346 DebugLoc dl = Op.getDebugLoc();
5347 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005348
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005349 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5350 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5351 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005352 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005353 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5354 InVec = Op.getOperand(1);
5355 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5356 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005357 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005358 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5359 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5360 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005361 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005362 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5363 Mask[0] = 0; Mask[1] = 2;
5364 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5365 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005366 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005367}
5368
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005369// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5370// to create 256-bit vectors from two other 128-bit ones.
5371static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5372 DebugLoc dl = Op.getDebugLoc();
5373 EVT ResVT = Op.getValueType();
5374
5375 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5376
5377 SDValue V1 = Op.getOperand(0);
5378 SDValue V2 = Op.getOperand(1);
5379 unsigned NumElems = ResVT.getVectorNumElements();
5380
5381 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5382 DAG.getConstant(0, MVT::i32), DAG, dl);
5383 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5384 DAG, dl);
5385}
5386
5387SDValue
5388X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005389 EVT ResVT = Op.getValueType();
5390
5391 assert(Op.getNumOperands() == 2);
5392 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5393 "Unsupported CONCAT_VECTORS for value type");
5394
5395 // We support concatenate two MMX registers and place them in a MMX register.
5396 // This is better than doing a stack convert.
5397 if (ResVT.is128BitVector())
5398 return LowerMMXCONCAT_VECTORS(Op, DAG);
5399
5400 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5401 // from two other 128-bit ones.
5402 return LowerAVXCONCAT_VECTORS(Op, DAG);
5403}
5404
Nate Begemanb9a47b82009-02-23 08:49:38 +00005405// v8i16 shuffles - Prefer shuffles in the following order:
5406// 1. [all] pshuflw, pshufhw, optional move
5407// 2. [ssse3] 1 x pshufb
5408// 3. [ssse3] 2 x pshufb + 1 x por
5409// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005410SDValue
5411X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5412 SelectionDAG &DAG) const {
5413 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005414 SDValue V1 = SVOp->getOperand(0);
5415 SDValue V2 = SVOp->getOperand(1);
5416 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005417 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005418
Nate Begemanb9a47b82009-02-23 08:49:38 +00005419 // Determine if more than 1 of the words in each of the low and high quadwords
5420 // of the result come from the same quadword of one of the two inputs. Undef
5421 // mask values count as coming from any quadword, for better codegen.
5422 SmallVector<unsigned, 4> LoQuad(4);
5423 SmallVector<unsigned, 4> HiQuad(4);
5424 BitVector InputQuads(4);
5425 for (unsigned i = 0; i < 8; ++i) {
5426 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005427 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005428 MaskVals.push_back(EltIdx);
5429 if (EltIdx < 0) {
5430 ++Quad[0];
5431 ++Quad[1];
5432 ++Quad[2];
5433 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005434 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005435 }
5436 ++Quad[EltIdx / 4];
5437 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005438 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005439
Nate Begemanb9a47b82009-02-23 08:49:38 +00005440 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005441 unsigned MaxQuad = 1;
5442 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005443 if (LoQuad[i] > MaxQuad) {
5444 BestLoQuad = i;
5445 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005446 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005447 }
5448
Nate Begemanb9a47b82009-02-23 08:49:38 +00005449 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005450 MaxQuad = 1;
5451 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005452 if (HiQuad[i] > MaxQuad) {
5453 BestHiQuad = i;
5454 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005455 }
5456 }
5457
Nate Begemanb9a47b82009-02-23 08:49:38 +00005458 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005459 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005460 // single pshufb instruction is necessary. If There are more than 2 input
5461 // quads, disable the next transformation since it does not help SSSE3.
5462 bool V1Used = InputQuads[0] || InputQuads[1];
5463 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005464 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005465 if (InputQuads.count() == 2 && V1Used && V2Used) {
5466 BestLoQuad = InputQuads.find_first();
5467 BestHiQuad = InputQuads.find_next(BestLoQuad);
5468 }
5469 if (InputQuads.count() > 2) {
5470 BestLoQuad = -1;
5471 BestHiQuad = -1;
5472 }
5473 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005474
Nate Begemanb9a47b82009-02-23 08:49:38 +00005475 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5476 // the shuffle mask. If a quad is scored as -1, that means that it contains
5477 // words from all 4 input quadwords.
5478 SDValue NewV;
5479 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005480 SmallVector<int, 8> MaskV;
5481 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5482 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005483 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005484 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5485 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5486 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005487
Nate Begemanb9a47b82009-02-23 08:49:38 +00005488 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5489 // source words for the shuffle, to aid later transformations.
5490 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005491 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005492 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005493 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005494 if (idx != (int)i)
5495 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005496 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005497 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005498 AllWordsInNewV = false;
5499 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005500 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005501
Nate Begemanb9a47b82009-02-23 08:49:38 +00005502 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5503 if (AllWordsInNewV) {
5504 for (int i = 0; i != 8; ++i) {
5505 int idx = MaskVals[i];
5506 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005507 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005508 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005509 if ((idx != i) && idx < 4)
5510 pshufhw = false;
5511 if ((idx != i) && idx > 3)
5512 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005513 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005514 V1 = NewV;
5515 V2Used = false;
5516 BestLoQuad = 0;
5517 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005518 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005519
Nate Begemanb9a47b82009-02-23 08:49:38 +00005520 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5521 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005522 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005523 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5524 unsigned TargetMask = 0;
5525 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005526 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005527 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5528 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5529 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005530 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005531 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005532 }
Eric Christopherfd179292009-08-27 18:07:15 +00005533
Nate Begemanb9a47b82009-02-23 08:49:38 +00005534 // If we have SSSE3, and all words of the result are from 1 input vector,
5535 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5536 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005537 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005538 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005539
Nate Begemanb9a47b82009-02-23 08:49:38 +00005540 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005541 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005542 // mask, and elements that come from V1 in the V2 mask, so that the two
5543 // results can be OR'd together.
5544 bool TwoInputs = V1Used && V2Used;
5545 for (unsigned i = 0; i != 8; ++i) {
5546 int EltIdx = MaskVals[i] * 2;
5547 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005548 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5549 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005550 continue;
5551 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005552 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5553 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005554 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005555 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005556 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005557 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005558 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005559 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005560 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005561
Nate Begemanb9a47b82009-02-23 08:49:38 +00005562 // Calculate the shuffle mask for the second input, shuffle it, and
5563 // OR it with the first shuffled input.
5564 pshufbMask.clear();
5565 for (unsigned i = 0; i != 8; ++i) {
5566 int EltIdx = MaskVals[i] * 2;
5567 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005568 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5569 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005570 continue;
5571 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005572 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5573 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005574 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005575 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005576 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005577 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005578 MVT::v16i8, &pshufbMask[0], 16));
5579 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005580 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005581 }
5582
5583 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5584 // and update MaskVals with new element order.
5585 BitVector InOrder(8);
5586 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005587 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005588 for (int i = 0; i != 4; ++i) {
5589 int idx = MaskVals[i];
5590 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005591 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005592 InOrder.set(i);
5593 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005594 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005595 InOrder.set(i);
5596 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005597 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005598 }
5599 }
5600 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005601 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005602 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005603 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005604
5605 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5606 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5607 NewV.getOperand(0),
5608 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5609 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005610 }
Eric Christopherfd179292009-08-27 18:07:15 +00005611
Nate Begemanb9a47b82009-02-23 08:49:38 +00005612 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5613 // and update MaskVals with the new element order.
5614 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005615 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005616 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005617 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005618 for (unsigned i = 4; i != 8; ++i) {
5619 int idx = MaskVals[i];
5620 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005621 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005622 InOrder.set(i);
5623 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005624 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005625 InOrder.set(i);
5626 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005627 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005628 }
5629 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005630 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005631 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005632
5633 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5634 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5635 NewV.getOperand(0),
5636 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5637 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005638 }
Eric Christopherfd179292009-08-27 18:07:15 +00005639
Nate Begemanb9a47b82009-02-23 08:49:38 +00005640 // In case BestHi & BestLo were both -1, which means each quadword has a word
5641 // from each of the four input quadwords, calculate the InOrder bitvector now
5642 // before falling through to the insert/extract cleanup.
5643 if (BestLoQuad == -1 && BestHiQuad == -1) {
5644 NewV = V1;
5645 for (int i = 0; i != 8; ++i)
5646 if (MaskVals[i] < 0 || MaskVals[i] == i)
5647 InOrder.set(i);
5648 }
Eric Christopherfd179292009-08-27 18:07:15 +00005649
Nate Begemanb9a47b82009-02-23 08:49:38 +00005650 // The other elements are put in the right place using pextrw and pinsrw.
5651 for (unsigned i = 0; i != 8; ++i) {
5652 if (InOrder[i])
5653 continue;
5654 int EltIdx = MaskVals[i];
5655 if (EltIdx < 0)
5656 continue;
5657 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005658 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005659 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005660 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005661 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005662 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005663 DAG.getIntPtrConstant(i));
5664 }
5665 return NewV;
5666}
5667
5668// v16i8 shuffles - Prefer shuffles in the following order:
5669// 1. [ssse3] 1 x pshufb
5670// 2. [ssse3] 2 x pshufb + 1 x por
5671// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5672static
Nate Begeman9008ca62009-04-27 18:41:29 +00005673SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005674 SelectionDAG &DAG,
5675 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005676 SDValue V1 = SVOp->getOperand(0);
5677 SDValue V2 = SVOp->getOperand(1);
5678 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005679 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005680 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005681
Nate Begemanb9a47b82009-02-23 08:49:38 +00005682 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005683 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005684 // present, fall back to case 3.
5685 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5686 bool V1Only = true;
5687 bool V2Only = true;
5688 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005689 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005690 if (EltIdx < 0)
5691 continue;
5692 if (EltIdx < 16)
5693 V2Only = false;
5694 else
5695 V1Only = false;
5696 }
Eric Christopherfd179292009-08-27 18:07:15 +00005697
Nate Begemanb9a47b82009-02-23 08:49:38 +00005698 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5699 if (TLI.getSubtarget()->hasSSSE3()) {
5700 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005701
Nate Begemanb9a47b82009-02-23 08:49:38 +00005702 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005703 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005704 //
5705 // Otherwise, we have elements from both input vectors, and must zero out
5706 // elements that come from V2 in the first mask, and V1 in the second mask
5707 // so that we can OR them together.
5708 bool TwoInputs = !(V1Only || V2Only);
5709 for (unsigned i = 0; i != 16; ++i) {
5710 int EltIdx = MaskVals[i];
5711 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005712 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005713 continue;
5714 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005715 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005716 }
5717 // If all the elements are from V2, assign it to V1 and return after
5718 // building the first pshufb.
5719 if (V2Only)
5720 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005721 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005722 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005723 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005724 if (!TwoInputs)
5725 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005726
Nate Begemanb9a47b82009-02-23 08:49:38 +00005727 // Calculate the shuffle mask for the second input, shuffle it, and
5728 // OR it with the first shuffled input.
5729 pshufbMask.clear();
5730 for (unsigned i = 0; i != 16; ++i) {
5731 int EltIdx = MaskVals[i];
5732 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005733 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005734 continue;
5735 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005736 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005737 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005738 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005739 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005740 MVT::v16i8, &pshufbMask[0], 16));
5741 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005742 }
Eric Christopherfd179292009-08-27 18:07:15 +00005743
Nate Begemanb9a47b82009-02-23 08:49:38 +00005744 // No SSSE3 - Calculate in place words and then fix all out of place words
5745 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5746 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005747 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5748 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005749 SDValue NewV = V2Only ? V2 : V1;
5750 for (int i = 0; i != 8; ++i) {
5751 int Elt0 = MaskVals[i*2];
5752 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005753
Nate Begemanb9a47b82009-02-23 08:49:38 +00005754 // This word of the result is all undef, skip it.
5755 if (Elt0 < 0 && Elt1 < 0)
5756 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005757
Nate Begemanb9a47b82009-02-23 08:49:38 +00005758 // This word of the result is already in the correct place, skip it.
5759 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5760 continue;
5761 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5762 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005763
Nate Begemanb9a47b82009-02-23 08:49:38 +00005764 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5765 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5766 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005767
5768 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5769 // using a single extract together, load it and store it.
5770 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005771 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005772 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005773 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005774 DAG.getIntPtrConstant(i));
5775 continue;
5776 }
5777
Nate Begemanb9a47b82009-02-23 08:49:38 +00005778 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005779 // source byte is not also odd, shift the extracted word left 8 bits
5780 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005781 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005782 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005783 DAG.getIntPtrConstant(Elt1 / 2));
5784 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005785 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005786 DAG.getConstant(8,
5787 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005788 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005789 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5790 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005791 }
5792 // If Elt0 is defined, extract it from the appropriate source. If the
5793 // source byte is not also even, shift the extracted word right 8 bits. If
5794 // Elt1 was also defined, OR the extracted values together before
5795 // inserting them in the result.
5796 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005797 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005798 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5799 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005800 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005801 DAG.getConstant(8,
5802 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005803 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005804 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5805 DAG.getConstant(0x00FF, MVT::i16));
5806 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005807 : InsElt0;
5808 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005809 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005810 DAG.getIntPtrConstant(i));
5811 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005812 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005813}
5814
Evan Cheng7a831ce2007-12-15 03:00:47 +00005815/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005816/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005817/// done when every pair / quad of shuffle mask elements point to elements in
5818/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005819/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005820static
Nate Begeman9008ca62009-04-27 18:41:29 +00005821SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005822 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005823 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005824 SDValue V1 = SVOp->getOperand(0);
5825 SDValue V2 = SVOp->getOperand(1);
5826 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005827 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005828 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005829 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005830 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005831 case MVT::v4f32: NewVT = MVT::v2f64; break;
5832 case MVT::v4i32: NewVT = MVT::v2i64; break;
5833 case MVT::v8i16: NewVT = MVT::v4i32; break;
5834 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005835 }
5836
Nate Begeman9008ca62009-04-27 18:41:29 +00005837 int Scale = NumElems / NewWidth;
5838 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005839 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005840 int StartIdx = -1;
5841 for (int j = 0; j < Scale; ++j) {
5842 int EltIdx = SVOp->getMaskElt(i+j);
5843 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005844 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005845 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005846 StartIdx = EltIdx - (EltIdx % Scale);
5847 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005848 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005849 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005850 if (StartIdx == -1)
5851 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005852 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005853 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005854 }
5855
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005856 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5857 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005858 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005859}
5860
Evan Chengd880b972008-05-09 21:53:03 +00005861/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005862///
Owen Andersone50ed302009-08-10 22:56:29 +00005863static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005864 SDValue SrcOp, SelectionDAG &DAG,
5865 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005866 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005867 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005868 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005869 LD = dyn_cast<LoadSDNode>(SrcOp);
5870 if (!LD) {
5871 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5872 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005873 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005874 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005875 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005876 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005877 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005878 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005879 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005880 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005881 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5882 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5883 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005884 SrcOp.getOperand(0)
5885 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005886 }
5887 }
5888 }
5889
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005890 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005891 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005892 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005893 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005894}
5895
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005896/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5897/// shuffle node referes to only one lane in the sources.
5898static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5899 EVT VT = SVOp->getValueType(0);
5900 int NumElems = VT.getVectorNumElements();
5901 int HalfSize = NumElems/2;
5902 SmallVector<int, 16> M;
5903 SVOp->getMask(M);
5904 bool MatchA = false, MatchB = false;
5905
5906 for (int l = 0; l < NumElems*2; l += HalfSize) {
5907 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5908 MatchA = true;
5909 break;
5910 }
5911 }
5912
5913 for (int l = 0; l < NumElems*2; l += HalfSize) {
5914 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5915 MatchB = true;
5916 break;
5917 }
5918 }
5919
5920 return MatchA && MatchB;
5921}
5922
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005923/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5924/// which could not be matched by any known target speficic shuffle
5925static SDValue
5926LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005927 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5928 // If each half of a vector shuffle node referes to only one lane in the
5929 // source vectors, extract each used 128-bit lane and shuffle them using
5930 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5931 // the work to the legalizer.
5932 DebugLoc dl = SVOp->getDebugLoc();
5933 EVT VT = SVOp->getValueType(0);
5934 int NumElems = VT.getVectorNumElements();
5935 int HalfSize = NumElems/2;
5936
5937 // Extract the reference for each half
5938 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5939 int FstVecOpNum = 0, SndVecOpNum = 0;
5940 for (int i = 0; i < HalfSize; ++i) {
5941 int Elt = SVOp->getMaskElt(i);
5942 if (SVOp->getMaskElt(i) < 0)
5943 continue;
5944 FstVecOpNum = Elt/NumElems;
5945 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5946 break;
5947 }
5948 for (int i = HalfSize; i < NumElems; ++i) {
5949 int Elt = SVOp->getMaskElt(i);
5950 if (SVOp->getMaskElt(i) < 0)
5951 continue;
5952 SndVecOpNum = Elt/NumElems;
5953 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5954 break;
5955 }
5956
5957 // Extract the subvectors
5958 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5959 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5960 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5961 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5962
5963 // Generate 128-bit shuffles
5964 SmallVector<int, 16> MaskV1, MaskV2;
5965 for (int i = 0; i < HalfSize; ++i) {
5966 int Elt = SVOp->getMaskElt(i);
5967 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5968 }
5969 for (int i = HalfSize; i < NumElems; ++i) {
5970 int Elt = SVOp->getMaskElt(i);
5971 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5972 }
5973
5974 EVT NVT = V1.getValueType();
5975 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5976 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5977
5978 // Concatenate the result back
5979 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
5980 DAG.getConstant(0, MVT::i32), DAG, dl);
5981 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5982 DAG, dl);
5983 }
5984
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005985 return SDValue();
5986}
5987
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005988/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
5989/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00005990static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005991LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005992 SDValue V1 = SVOp->getOperand(0);
5993 SDValue V2 = SVOp->getOperand(1);
5994 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005995 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005996
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005997 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
5998
Evan Chengace3c172008-07-22 21:13:36 +00005999 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006000 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006001 SmallVector<int, 8> Mask1(4U, -1);
6002 SmallVector<int, 8> PermMask;
6003 SVOp->getMask(PermMask);
6004
Evan Chengace3c172008-07-22 21:13:36 +00006005 unsigned NumHi = 0;
6006 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006007 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006008 int Idx = PermMask[i];
6009 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006010 Locs[i] = std::make_pair(-1, -1);
6011 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006012 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6013 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006014 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006015 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006016 NumLo++;
6017 } else {
6018 Locs[i] = std::make_pair(1, NumHi);
6019 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006020 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006021 NumHi++;
6022 }
6023 }
6024 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006025
Evan Chengace3c172008-07-22 21:13:36 +00006026 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006027 // If no more than two elements come from either vector. This can be
6028 // implemented with two shuffles. First shuffle gather the elements.
6029 // The second shuffle, which takes the first shuffle as both of its
6030 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006031 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006032
Nate Begeman9008ca62009-04-27 18:41:29 +00006033 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006034
Evan Chengace3c172008-07-22 21:13:36 +00006035 for (unsigned i = 0; i != 4; ++i) {
6036 if (Locs[i].first == -1)
6037 continue;
6038 else {
6039 unsigned Idx = (i < 2) ? 0 : 4;
6040 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006041 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006042 }
6043 }
6044
Nate Begeman9008ca62009-04-27 18:41:29 +00006045 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006046 } else if (NumLo == 3 || NumHi == 3) {
6047 // Otherwise, we must have three elements from one vector, call it X, and
6048 // one element from the other, call it Y. First, use a shufps to build an
6049 // intermediate vector with the one element from Y and the element from X
6050 // that will be in the same half in the final destination (the indexes don't
6051 // matter). Then, use a shufps to build the final vector, taking the half
6052 // containing the element from Y from the intermediate, and the other half
6053 // from X.
6054 if (NumHi == 3) {
6055 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006056 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006057 std::swap(V1, V2);
6058 }
6059
6060 // Find the element from V2.
6061 unsigned HiIndex;
6062 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006063 int Val = PermMask[HiIndex];
6064 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006065 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006066 if (Val >= 4)
6067 break;
6068 }
6069
Nate Begeman9008ca62009-04-27 18:41:29 +00006070 Mask1[0] = PermMask[HiIndex];
6071 Mask1[1] = -1;
6072 Mask1[2] = PermMask[HiIndex^1];
6073 Mask1[3] = -1;
6074 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006075
6076 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006077 Mask1[0] = PermMask[0];
6078 Mask1[1] = PermMask[1];
6079 Mask1[2] = HiIndex & 1 ? 6 : 4;
6080 Mask1[3] = HiIndex & 1 ? 4 : 6;
6081 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006082 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006083 Mask1[0] = HiIndex & 1 ? 2 : 0;
6084 Mask1[1] = HiIndex & 1 ? 0 : 2;
6085 Mask1[2] = PermMask[2];
6086 Mask1[3] = PermMask[3];
6087 if (Mask1[2] >= 0)
6088 Mask1[2] += 4;
6089 if (Mask1[3] >= 0)
6090 Mask1[3] += 4;
6091 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006092 }
Evan Chengace3c172008-07-22 21:13:36 +00006093 }
6094
6095 // Break it into (shuffle shuffle_hi, shuffle_lo).
6096 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006097 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006098 SmallVector<int,8> LoMask(4U, -1);
6099 SmallVector<int,8> HiMask(4U, -1);
6100
6101 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006102 unsigned MaskIdx = 0;
6103 unsigned LoIdx = 0;
6104 unsigned HiIdx = 2;
6105 for (unsigned i = 0; i != 4; ++i) {
6106 if (i == 2) {
6107 MaskPtr = &HiMask;
6108 MaskIdx = 1;
6109 LoIdx = 0;
6110 HiIdx = 2;
6111 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006112 int Idx = PermMask[i];
6113 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006114 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006115 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006116 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006117 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006118 LoIdx++;
6119 } else {
6120 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006121 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006122 HiIdx++;
6123 }
6124 }
6125
Nate Begeman9008ca62009-04-27 18:41:29 +00006126 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6127 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6128 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006129 for (unsigned i = 0; i != 4; ++i) {
6130 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006131 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006132 } else {
6133 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006134 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006135 }
6136 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006137 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006138}
6139
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006140static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006141 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006142 V = V.getOperand(0);
6143 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6144 V = V.getOperand(0);
6145 if (MayFoldLoad(V))
6146 return true;
6147 return false;
6148}
6149
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006150// FIXME: the version above should always be used. Since there's
6151// a bug where several vector shuffles can't be folded because the
6152// DAG is not updated during lowering and a node claims to have two
6153// uses while it only has one, use this version, and let isel match
6154// another instruction if the load really happens to have more than
6155// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006156// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006157static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006158 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006159 V = V.getOperand(0);
6160 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6161 V = V.getOperand(0);
6162 if (ISD::isNormalLoad(V.getNode()))
6163 return true;
6164 return false;
6165}
6166
6167/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6168/// a vector extract, and if both can be later optimized into a single load.
6169/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6170/// here because otherwise a target specific shuffle node is going to be
6171/// emitted for this shuffle, and the optimization not done.
6172/// FIXME: This is probably not the best approach, but fix the problem
6173/// until the right path is decided.
6174static
6175bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6176 const TargetLowering &TLI) {
6177 EVT VT = V.getValueType();
6178 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6179
6180 // Be sure that the vector shuffle is present in a pattern like this:
6181 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6182 if (!V.hasOneUse())
6183 return false;
6184
6185 SDNode *N = *V.getNode()->use_begin();
6186 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6187 return false;
6188
6189 SDValue EltNo = N->getOperand(1);
6190 if (!isa<ConstantSDNode>(EltNo))
6191 return false;
6192
6193 // If the bit convert changed the number of elements, it is unsafe
6194 // to examine the mask.
6195 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006196 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006197 EVT SrcVT = V.getOperand(0).getValueType();
6198 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6199 return false;
6200 V = V.getOperand(0);
6201 HasShuffleIntoBitcast = true;
6202 }
6203
6204 // Select the input vector, guarding against out of range extract vector.
6205 unsigned NumElems = VT.getVectorNumElements();
6206 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6207 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6208 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6209
6210 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006211 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006212 V = V.getOperand(0);
6213
6214 if (ISD::isNormalLoad(V.getNode())) {
6215 // Is the original load suitable?
6216 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6217
6218 // FIXME: avoid the multi-use bug that is preventing lots of
6219 // of foldings to be detected, this is still wrong of course, but
6220 // give the temporary desired behavior, and if it happens that
6221 // the load has real more uses, during isel it will not fold, and
6222 // will generate poor code.
6223 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6224 return false;
6225
6226 if (!HasShuffleIntoBitcast)
6227 return true;
6228
6229 // If there's a bitcast before the shuffle, check if the load type and
6230 // alignment is valid.
6231 unsigned Align = LN0->getAlignment();
6232 unsigned NewAlign =
6233 TLI.getTargetData()->getABITypeAlignment(
6234 VT.getTypeForEVT(*DAG.getContext()));
6235
6236 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6237 return false;
6238 }
6239
6240 return true;
6241}
6242
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006243static
Evan Cheng835580f2010-10-07 20:50:20 +00006244SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6245 EVT VT = Op.getValueType();
6246
6247 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006248 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6249 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006250 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6251 V1, DAG));
6252}
6253
6254static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006255SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
6256 bool HasSSE2) {
6257 SDValue V1 = Op.getOperand(0);
6258 SDValue V2 = Op.getOperand(1);
6259 EVT VT = Op.getValueType();
6260
6261 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6262
6263 if (HasSSE2 && VT == MVT::v2f64)
6264 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6265
Evan Cheng0899f5c2011-08-31 02:05:24 +00006266 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6267 return DAG.getNode(ISD::BITCAST, dl, VT,
6268 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6269 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6270 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006271}
6272
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006273static
6274SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6275 SDValue V1 = Op.getOperand(0);
6276 SDValue V2 = Op.getOperand(1);
6277 EVT VT = Op.getValueType();
6278
6279 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6280 "unsupported shuffle type");
6281
6282 if (V2.getOpcode() == ISD::UNDEF)
6283 V2 = V1;
6284
6285 // v4i32 or v4f32
6286 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6287}
6288
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006289static inline unsigned getSHUFPOpcode(EVT VT) {
6290 switch(VT.getSimpleVT().SimpleTy) {
6291 case MVT::v8i32: // Use fp unit for int unpack.
6292 case MVT::v8f32:
6293 case MVT::v4i32: // Use fp unit for int unpack.
6294 case MVT::v4f32: return X86ISD::SHUFPS;
6295 case MVT::v4i64: // Use fp unit for int unpack.
6296 case MVT::v4f64:
6297 case MVT::v2i64: // Use fp unit for int unpack.
6298 case MVT::v2f64: return X86ISD::SHUFPD;
6299 default:
6300 llvm_unreachable("Unknown type for shufp*");
6301 }
6302 return 0;
6303}
6304
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006305static
6306SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
6307 SDValue V1 = Op.getOperand(0);
6308 SDValue V2 = Op.getOperand(1);
6309 EVT VT = Op.getValueType();
6310 unsigned NumElems = VT.getVectorNumElements();
6311
6312 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6313 // operand of these instructions is only memory, so check if there's a
6314 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6315 // same masks.
6316 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006317
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006318 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006319 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006320 CanFoldLoad = true;
6321
6322 // When V1 is a load, it can be folded later into a store in isel, example:
6323 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6324 // turns into:
6325 // (MOVLPSmr addr:$src1, VR128:$src2)
6326 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006327 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006328 CanFoldLoad = true;
6329
Eric Christopher893a8822011-02-20 05:04:42 +00006330 // Both of them can't be memory operations though.
6331 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
6332 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00006333
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006334 if (CanFoldLoad) {
6335 if (HasSSE2 && NumElems == 2)
6336 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6337
6338 if (NumElems == 4)
6339 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
6340 }
6341
6342 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6343 // movl and movlp will both match v2i64, but v2i64 is never matched by
6344 // movl earlier because we make it strict to avoid messing with the movlp load
6345 // folding logic (see the code above getMOVLP call). Match it here then,
6346 // this is horrible, but will stay like this until we move all shuffle
6347 // matching to x86 specific nodes. Note that for the 1st condition all
6348 // types are matched with movsd.
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006349 if (HasSSE2) {
6350 if (NumElems == 2)
6351 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006352 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006353 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006354
6355 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6356
6357 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006358 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006359 X86::getShuffleSHUFImmediate(SVOp), DAG);
6360}
6361
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006362static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006363 switch(VT.getSimpleVT().SimpleTy) {
6364 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6365 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006366 case MVT::v4f32: return X86ISD::UNPCKLPS;
6367 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006368 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006369 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006370 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006371 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006372 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6373 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6374 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006375 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006376 }
6377 return 0;
6378}
6379
6380static inline unsigned getUNPCKHOpcode(EVT VT) {
6381 switch(VT.getSimpleVT().SimpleTy) {
6382 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6383 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6384 case MVT::v4f32: return X86ISD::UNPCKHPS;
6385 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006386 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006387 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006388 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006389 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006390 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6391 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6392 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006393 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006394 }
6395 return 0;
6396}
6397
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006398static inline unsigned getVPERMILOpcode(EVT VT) {
6399 switch(VT.getSimpleVT().SimpleTy) {
6400 case MVT::v4i32:
6401 case MVT::v4f32: return X86ISD::VPERMILPS;
6402 case MVT::v2i64:
6403 case MVT::v2f64: return X86ISD::VPERMILPD;
6404 case MVT::v8i32:
6405 case MVT::v8f32: return X86ISD::VPERMILPSY;
6406 case MVT::v4i64:
6407 case MVT::v4f64: return X86ISD::VPERMILPDY;
6408 default:
6409 llvm_unreachable("Unknown type for vpermil");
6410 }
6411 return 0;
6412}
6413
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006414/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6415/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6416/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6417static bool isVectorBroadcast(SDValue &Op) {
6418 EVT VT = Op.getValueType();
6419 bool Is256 = VT.getSizeInBits() == 256;
6420
6421 assert((VT.getSizeInBits() == 128 || Is256) &&
6422 "Unsupported type for vbroadcast node");
6423
6424 SDValue V = Op;
6425 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6426 V = V.getOperand(0);
6427
6428 if (Is256 && !(V.hasOneUse() &&
6429 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6430 V.getOperand(0).getOpcode() == ISD::UNDEF))
6431 return false;
6432
6433 if (Is256)
6434 V = V.getOperand(1);
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006435
6436 if (!V.hasOneUse())
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006437 return false;
6438
6439 // Check the source scalar_to_vector type. 256-bit broadcasts are
6440 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6441 // for 32-bit scalars.
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006442 if (V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6443 return false;
6444
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006445 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6446 if (ScalarSize != 32 && ScalarSize != 64)
6447 return false;
6448 if (!Is256 && ScalarSize == 64)
6449 return false;
6450
6451 V = V.getOperand(0);
6452 if (!MayFoldLoad(V))
6453 return false;
6454
6455 // Return the load node
6456 Op = V;
6457 return true;
6458}
6459
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006460static
6461SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006462 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006463 const X86Subtarget *Subtarget) {
6464 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6465 EVT VT = Op.getValueType();
6466 DebugLoc dl = Op.getDebugLoc();
6467 SDValue V1 = Op.getOperand(0);
6468 SDValue V2 = Op.getOperand(1);
6469
6470 if (isZeroShuffle(SVOp))
6471 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
6472
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006473 // Handle splat operations
6474 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006475 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006476 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006477 // Special case, this is the only place now where it's allowed to return
6478 // a vector_shuffle operation without using a target specific node, because
6479 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6480 // this be moved to DAGCombine instead?
6481 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006482 return Op;
6483
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006484 // Use vbroadcast whenever the splat comes from a foldable load
6485 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6486 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6487
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006488 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006489 if ((Size == 128 && NumElem <= 4) ||
6490 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006491 return SDValue();
6492
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006493 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006494 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006495 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006496
6497 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6498 // do it!
6499 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6500 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6501 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006502 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006503 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
6504 // FIXME: Figure out a cleaner way to do this.
6505 // Try to make use of movq to zero out the top part.
6506 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6507 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6508 if (NewOp.getNode()) {
6509 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6510 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6511 DAG, Subtarget, dl);
6512 }
6513 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6514 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6515 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6516 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6517 DAG, Subtarget, dl);
6518 }
6519 }
6520 return SDValue();
6521}
6522
Dan Gohman475871a2008-07-27 21:46:04 +00006523SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006524X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006525 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006526 SDValue V1 = Op.getOperand(0);
6527 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006528 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006529 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006530 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006531 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006532 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6533 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006534 bool V1IsSplat = false;
6535 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006536 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006537 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006538 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006539 MachineFunction &MF = DAG.getMachineFunction();
6540 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006541
Dale Johannesen0488fb62010-09-30 23:57:10 +00006542 // Shuffle operations on MMX not supported.
6543 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006544 return Op;
6545
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006546 // Vector shuffle lowering takes 3 steps:
6547 //
6548 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6549 // narrowing and commutation of operands should be handled.
6550 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6551 // shuffle nodes.
6552 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6553 // so the shuffle can be broken into other shuffles and the legalizer can
6554 // try the lowering again.
6555 //
6556 // The general ideia is that no vector_shuffle operation should be left to
6557 // be matched during isel, all of them must be converted to a target specific
6558 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006559
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006560 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6561 // narrowing and commutation of operands should be handled. The actual code
6562 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006563 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006564 if (NewOp.getNode())
6565 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006566
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006567 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6568 // unpckh_undef). Only use pshufd if speed is more important than size.
6569 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006570 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006571 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006572 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006573
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006574 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00006575 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006576 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006577
Dale Johannesen0488fb62010-09-30 23:57:10 +00006578 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006579 return getMOVHighToLow(Op, dl, DAG);
6580
6581 // Use to match splats
6582 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
6583 (VT == MVT::v2f64 || VT == MVT::v2i64))
6584 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6585
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006586 if (X86::isPSHUFDMask(SVOp)) {
6587 // The actual implementation will match the mask in the if above and then
6588 // during isel it can match several different instructions, not only pshufd
6589 // as its name says, sad but true, emulate the behavior for now...
6590 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6591 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6592
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006593 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6594
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006595 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006596 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6597
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006598 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6599 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006600 }
Eric Christopherfd179292009-08-27 18:07:15 +00006601
Evan Chengf26ffe92008-05-29 08:22:04 +00006602 // Check if this can be converted into a logical shift.
6603 bool isLeft = false;
6604 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006605 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00006606 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00006607 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006608 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006609 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006610 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006611 EVT EltVT = VT.getVectorElementType();
6612 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006613 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006614 }
Eric Christopherfd179292009-08-27 18:07:15 +00006615
Nate Begeman9008ca62009-04-27 18:41:29 +00006616 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006617 if (V1IsUndef)
6618 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006619 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006620 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006621 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006622 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006623 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6624
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006625 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006626 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6627 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006628 }
Eric Christopherfd179292009-08-27 18:07:15 +00006629
Nate Begeman9008ca62009-04-27 18:41:29 +00006630 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006631 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
6632 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006633
Dale Johannesen0488fb62010-09-30 23:57:10 +00006634 if (X86::isMOVHLPSMask(SVOp))
6635 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006636
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006637 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006638 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006639
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006640 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006641 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006642
Dale Johannesen0488fb62010-09-30 23:57:10 +00006643 if (X86::isMOVLPMask(SVOp))
6644 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006645
Nate Begeman9008ca62009-04-27 18:41:29 +00006646 if (ShouldXformToMOVHLPS(SVOp) ||
6647 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6648 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006649
Evan Chengf26ffe92008-05-29 08:22:04 +00006650 if (isShift) {
6651 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006652 EVT EltVT = VT.getVectorElementType();
6653 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006654 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006655 }
Eric Christopherfd179292009-08-27 18:07:15 +00006656
Evan Cheng9eca5e82006-10-25 21:49:50 +00006657 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006658 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6659 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006660 V1IsSplat = isSplatVector(V1.getNode());
6661 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006662
Chris Lattner8a594482007-11-25 00:24:49 +00006663 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006664 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006665 Op = CommuteVectorShuffle(SVOp, DAG);
6666 SVOp = cast<ShuffleVectorSDNode>(Op);
6667 V1 = SVOp->getOperand(0);
6668 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006669 std::swap(V1IsSplat, V2IsSplat);
6670 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006671 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006672 }
6673
Nate Begeman9008ca62009-04-27 18:41:29 +00006674 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6675 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006676 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006677 return V1;
6678 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6679 // the instruction selector will not match, so get a canonical MOVL with
6680 // swapped operands to undo the commute.
6681 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006682 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006683
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006684 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006685 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006686
6687 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006688 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006689
Evan Cheng9bbbb982006-10-25 20:48:19 +00006690 if (V2IsSplat) {
6691 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006692 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006693 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006694 SDValue NewMask = NormalizeMask(SVOp, DAG);
6695 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6696 if (NSVOp != SVOp) {
6697 if (X86::isUNPCKLMask(NSVOp, true)) {
6698 return NewMask;
6699 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6700 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006701 }
6702 }
6703 }
6704
Evan Cheng9eca5e82006-10-25 21:49:50 +00006705 if (Commuted) {
6706 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006707 // FIXME: this seems wrong.
6708 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6709 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006710
6711 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006712 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006713
6714 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006715 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006716 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006717
Nate Begeman9008ca62009-04-27 18:41:29 +00006718 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006719 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006720 return CommuteVectorShuffle(SVOp, DAG);
6721
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006722 // The checks below are all present in isShuffleMaskLegal, but they are
6723 // inlined here right now to enable us to directly emit target specific
6724 // nodes, and remove one by one until they don't return Op anymore.
6725 SmallVector<int, 16> M;
6726 SVOp->getMask(M);
6727
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006728 if (isPALIGNRMask(M, VT, HasSSSE3))
6729 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6730 X86::getShufflePALIGNRImmediate(SVOp),
6731 DAG);
6732
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006733 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6734 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006735 if (VT == MVT::v2f64)
6736 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006737 if (VT == MVT::v2i64)
6738 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6739 }
6740
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006741 if (isPSHUFHWMask(M, VT))
6742 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6743 X86::getShufflePSHUFHWImmediate(SVOp),
6744 DAG);
6745
6746 if (isPSHUFLWMask(M, VT))
6747 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6748 X86::getShufflePSHUFLWImmediate(SVOp),
6749 DAG);
6750
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006751 if (isSHUFPMask(M, VT))
6752 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6753 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006754
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006755 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006756 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006757 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006758 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006759
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006760 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006761 // Generate target specific nodes for 128 or 256-bit shuffles only
6762 // supported in the AVX instruction set.
6763 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006764
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006765 // Handle VMOVDDUPY permutations
6766 if (isMOVDDUPYMask(SVOp, Subtarget))
6767 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6768
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006769 // Handle VPERMILPS* permutations
6770 if (isVPERMILPSMask(M, VT, Subtarget))
6771 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6772 getShuffleVPERMILPSImmediate(SVOp), DAG);
6773
6774 // Handle VPERMILPD* permutations
6775 if (isVPERMILPDMask(M, VT, Subtarget))
6776 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6777 getShuffleVPERMILPDImmediate(SVOp), DAG);
6778
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006779 // Handle VPERM2F128 permutations
6780 if (isVPERM2F128Mask(M, VT, Subtarget))
6781 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6782 getShuffleVPERM2F128Immediate(SVOp), DAG);
6783
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006784 // Handle VSHUFPSY permutations
6785 if (isVSHUFPSYMask(M, VT, Subtarget))
6786 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6787 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6788
6789 // Handle VSHUFPDY permutations
6790 if (isVSHUFPDYMask(M, VT, Subtarget))
6791 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6792 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6793
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006794 //===--------------------------------------------------------------------===//
6795 // Since no target specific shuffle was selected for this generic one,
6796 // lower it into other known shuffles. FIXME: this isn't true yet, but
6797 // this is the plan.
6798 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006799
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006800 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6801 if (VT == MVT::v8i16) {
6802 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6803 if (NewOp.getNode())
6804 return NewOp;
6805 }
6806
6807 if (VT == MVT::v16i8) {
6808 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6809 if (NewOp.getNode())
6810 return NewOp;
6811 }
6812
6813 // Handle all 128-bit wide vectors with 4 elements, and match them with
6814 // several different shuffle types.
6815 if (NumElems == 4 && VT.getSizeInBits() == 128)
6816 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6817
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006818 // Handle general 256-bit shuffles
6819 if (VT.is256BitVector())
6820 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6821
Dan Gohman475871a2008-07-27 21:46:04 +00006822 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006823}
6824
Dan Gohman475871a2008-07-27 21:46:04 +00006825SDValue
6826X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006827 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006828 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006829 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006830
6831 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6832 return SDValue();
6833
Duncan Sands83ec4b62008-06-06 12:08:01 +00006834 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006835 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006836 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006837 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006838 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006839 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006840 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006841 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6842 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6843 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006844 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6845 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006846 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006847 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006848 Op.getOperand(0)),
6849 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006850 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006851 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006852 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006853 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006854 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006855 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006856 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6857 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006858 // result has a single use which is a store or a bitcast to i32. And in
6859 // the case of a store, it's not worth it if the index is a constant 0,
6860 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006861 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006862 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006863 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006864 if ((User->getOpcode() != ISD::STORE ||
6865 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6866 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006867 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006868 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006869 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006870 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006871 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006872 Op.getOperand(0)),
6873 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006874 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006875 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006876 // ExtractPS works with constant index.
6877 if (isa<ConstantSDNode>(Op.getOperand(1)))
6878 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006879 }
Dan Gohman475871a2008-07-27 21:46:04 +00006880 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006881}
6882
6883
Dan Gohman475871a2008-07-27 21:46:04 +00006884SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006885X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6886 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006887 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006888 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006889
David Greene74a579d2011-02-10 16:57:36 +00006890 SDValue Vec = Op.getOperand(0);
6891 EVT VecVT = Vec.getValueType();
6892
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006893 // If this is a 256-bit vector result, first extract the 128-bit vector and
6894 // then extract the element from the 128-bit vector.
6895 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006896 DebugLoc dl = Op.getNode()->getDebugLoc();
6897 unsigned NumElems = VecVT.getVectorNumElements();
6898 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006899 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6900
6901 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006902 bool Upper = IdxVal >= NumElems/2;
6903 Vec = Extract128BitVector(Vec,
6904 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006905
David Greene74a579d2011-02-10 16:57:36 +00006906 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006907 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006908 }
6909
6910 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6911
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006912 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006913 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006914 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006915 return Res;
6916 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006917
Owen Andersone50ed302009-08-10 22:56:29 +00006918 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006919 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006920 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006921 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006922 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006923 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006924 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006925 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6926 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006927 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006928 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006929 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006930 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006931 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006932 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006933 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006934 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006935 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006936 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006937 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006938 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006939 if (Idx == 0)
6940 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006941
Evan Cheng0db9fe62006-04-25 20:13:52 +00006942 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006943 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006944 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006945 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006946 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006947 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006948 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006949 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006950 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6951 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6952 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006953 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006954 if (Idx == 0)
6955 return Op;
6956
6957 // UNPCKHPD the element to the lowest double word, then movsd.
6958 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6959 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006960 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006961 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006962 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006963 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006964 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006965 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006966 }
6967
Dan Gohman475871a2008-07-27 21:46:04 +00006968 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006969}
6970
Dan Gohman475871a2008-07-27 21:46:04 +00006971SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006972X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6973 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006974 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006975 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006976 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006977
Dan Gohman475871a2008-07-27 21:46:04 +00006978 SDValue N0 = Op.getOperand(0);
6979 SDValue N1 = Op.getOperand(1);
6980 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006981
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006982 if (VT.getSizeInBits() == 256)
6983 return SDValue();
6984
Dan Gohman8a55ce42009-09-23 21:02:20 +00006985 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006986 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006987 unsigned Opc;
6988 if (VT == MVT::v8i16)
6989 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006990 else if (VT == MVT::v16i8)
6991 Opc = X86ISD::PINSRB;
6992 else
6993 Opc = X86ISD::PINSRB;
6994
Nate Begeman14d12ca2008-02-11 04:19:36 +00006995 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6996 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006997 if (N1.getValueType() != MVT::i32)
6998 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6999 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007000 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007001 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007002 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007003 // Bits [7:6] of the constant are the source select. This will always be
7004 // zero here. The DAG Combiner may combine an extract_elt index into these
7005 // bits. For example (insert (extract, 3), 2) could be matched by putting
7006 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007007 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007008 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007009 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007010 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007011 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007012 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007013 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007014 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007015 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007016 // PINSR* works with constant index.
7017 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007018 }
Dan Gohman475871a2008-07-27 21:46:04 +00007019 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007020}
7021
Dan Gohman475871a2008-07-27 21:46:04 +00007022SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007023X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007024 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007025 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007026
David Greene6b381262011-02-09 15:32:06 +00007027 DebugLoc dl = Op.getDebugLoc();
7028 SDValue N0 = Op.getOperand(0);
7029 SDValue N1 = Op.getOperand(1);
7030 SDValue N2 = Op.getOperand(2);
7031
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007032 // If this is a 256-bit vector result, first extract the 128-bit vector,
7033 // insert the element into the extracted half and then place it back.
7034 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007035 if (!isa<ConstantSDNode>(N2))
7036 return SDValue();
7037
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007038 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007039 unsigned NumElems = VT.getVectorNumElements();
7040 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007041 bool Upper = IdxVal >= NumElems/2;
7042 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7043 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007044
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007045 // Insert the element into the desired half.
7046 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7047 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007048
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007049 // Insert the changed part back to the 256-bit vector
7050 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007051 }
7052
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007053 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007054 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7055
Dan Gohman8a55ce42009-09-23 21:02:20 +00007056 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007057 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007058
Dan Gohman8a55ce42009-09-23 21:02:20 +00007059 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007060 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7061 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007062 if (N1.getValueType() != MVT::i32)
7063 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7064 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007065 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007066 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007067 }
Dan Gohman475871a2008-07-27 21:46:04 +00007068 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007069}
7070
Dan Gohman475871a2008-07-27 21:46:04 +00007071SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007072X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007073 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007074 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007075 EVT OpVT = Op.getValueType();
7076
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007077 // If this is a 256-bit vector result, first insert into a 128-bit
7078 // vector and then insert into the 256-bit vector.
7079 if (OpVT.getSizeInBits() > 128) {
7080 // Insert into a 128-bit vector.
7081 EVT VT128 = EVT::getVectorVT(*Context,
7082 OpVT.getVectorElementType(),
7083 OpVT.getVectorNumElements() / 2);
7084
7085 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7086
7087 // Insert the 128-bit vector.
7088 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7089 DAG.getConstant(0, MVT::i32),
7090 DAG, dl);
7091 }
7092
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007093 if (Op.getValueType() == MVT::v1i64 &&
7094 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007095 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007096
Owen Anderson825b72b2009-08-11 20:47:22 +00007097 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007098 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7099 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007100 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007101 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007102}
7103
David Greene91585092011-01-26 15:38:49 +00007104// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7105// a simple subregister reference or explicit instructions to grab
7106// upper bits of a vector.
7107SDValue
7108X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7109 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007110 DebugLoc dl = Op.getNode()->getDebugLoc();
7111 SDValue Vec = Op.getNode()->getOperand(0);
7112 SDValue Idx = Op.getNode()->getOperand(1);
7113
7114 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7115 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7116 return Extract128BitVector(Vec, Idx, DAG, dl);
7117 }
David Greene91585092011-01-26 15:38:49 +00007118 }
7119 return SDValue();
7120}
7121
David Greenecfe33c42011-01-26 19:13:22 +00007122// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7123// simple superregister reference or explicit instructions to insert
7124// the upper bits of a vector.
7125SDValue
7126X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7127 if (Subtarget->hasAVX()) {
7128 DebugLoc dl = Op.getNode()->getDebugLoc();
7129 SDValue Vec = Op.getNode()->getOperand(0);
7130 SDValue SubVec = Op.getNode()->getOperand(1);
7131 SDValue Idx = Op.getNode()->getOperand(2);
7132
7133 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7134 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007135 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007136 }
7137 }
7138 return SDValue();
7139}
7140
Bill Wendling056292f2008-09-16 21:48:12 +00007141// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7142// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7143// one of the above mentioned nodes. It has to be wrapped because otherwise
7144// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7145// be used to form addressing mode. These wrapped nodes will be selected
7146// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007147SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007148X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007149 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007150
Chris Lattner41621a22009-06-26 19:22:52 +00007151 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7152 // global base reg.
7153 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007154 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007155 CodeModel::Model M = getTargetMachine().getCodeModel();
7156
Chris Lattner4f066492009-07-11 20:29:19 +00007157 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007158 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007159 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007160 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007161 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007162 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007163 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007164
Evan Cheng1606e8e2009-03-13 07:51:59 +00007165 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007166 CP->getAlignment(),
7167 CP->getOffset(), OpFlag);
7168 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007169 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007170 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007171 if (OpFlag) {
7172 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007173 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007174 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007175 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007176 }
7177
7178 return Result;
7179}
7180
Dan Gohmand858e902010-04-17 15:26:15 +00007181SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007182 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007183
Chris Lattner18c59872009-06-27 04:16:01 +00007184 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7185 // global base reg.
7186 unsigned char OpFlag = 0;
7187 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007188 CodeModel::Model M = getTargetMachine().getCodeModel();
7189
Chris Lattner4f066492009-07-11 20:29:19 +00007190 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007191 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007192 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007193 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007194 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007195 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007196 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007197
Chris Lattner18c59872009-06-27 04:16:01 +00007198 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7199 OpFlag);
7200 DebugLoc DL = JT->getDebugLoc();
7201 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007202
Chris Lattner18c59872009-06-27 04:16:01 +00007203 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007204 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007205 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7206 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007207 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007208 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007209
Chris Lattner18c59872009-06-27 04:16:01 +00007210 return Result;
7211}
7212
7213SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007214X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007215 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007216
Chris Lattner18c59872009-06-27 04:16:01 +00007217 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7218 // global base reg.
7219 unsigned char OpFlag = 0;
7220 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007221 CodeModel::Model M = getTargetMachine().getCodeModel();
7222
Chris Lattner4f066492009-07-11 20:29:19 +00007223 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007224 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7225 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7226 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007227 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007228 } else if (Subtarget->isPICStyleGOT()) {
7229 OpFlag = X86II::MO_GOT;
7230 } else if (Subtarget->isPICStyleStubPIC()) {
7231 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7232 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7233 OpFlag = X86II::MO_DARWIN_NONLAZY;
7234 }
Eric Christopherfd179292009-08-27 18:07:15 +00007235
Chris Lattner18c59872009-06-27 04:16:01 +00007236 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007237
Chris Lattner18c59872009-06-27 04:16:01 +00007238 DebugLoc DL = Op.getDebugLoc();
7239 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007240
7241
Chris Lattner18c59872009-06-27 04:16:01 +00007242 // With PIC, the address is actually $g + Offset.
7243 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007244 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007245 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7246 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007247 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007248 Result);
7249 }
Eric Christopherfd179292009-08-27 18:07:15 +00007250
Eli Friedman586272d2011-08-11 01:48:05 +00007251 // For symbols that require a load from a stub to get the address, emit the
7252 // load.
7253 if (isGlobalStubReference(OpFlag))
7254 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
7255 MachinePointerInfo::getGOT(), false, false, 0);
7256
Chris Lattner18c59872009-06-27 04:16:01 +00007257 return Result;
7258}
7259
Dan Gohman475871a2008-07-27 21:46:04 +00007260SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007261X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007262 // Create the TargetBlockAddressAddress node.
7263 unsigned char OpFlags =
7264 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007265 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007266 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007267 DebugLoc dl = Op.getDebugLoc();
7268 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7269 /*isTarget=*/true, OpFlags);
7270
Dan Gohmanf705adb2009-10-30 01:28:02 +00007271 if (Subtarget->isPICStyleRIPRel() &&
7272 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007273 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7274 else
7275 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007276
Dan Gohman29cbade2009-11-20 23:18:13 +00007277 // With PIC, the address is actually $g + Offset.
7278 if (isGlobalRelativeToPICBase(OpFlags)) {
7279 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7280 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7281 Result);
7282 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007283
7284 return Result;
7285}
7286
7287SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007288X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007289 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007290 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007291 // Create the TargetGlobalAddress node, folding in the constant
7292 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007293 unsigned char OpFlags =
7294 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007295 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007296 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007297 if (OpFlags == X86II::MO_NO_FLAG &&
7298 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007299 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007300 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007301 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007302 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007303 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007304 }
Eric Christopherfd179292009-08-27 18:07:15 +00007305
Chris Lattner4f066492009-07-11 20:29:19 +00007306 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007307 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007308 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7309 else
7310 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007311
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007312 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007313 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007314 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7315 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007316 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007317 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007318
Chris Lattner36c25012009-07-10 07:34:39 +00007319 // For globals that require a load from a stub to get the address, emit the
7320 // load.
7321 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007322 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007323 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007324
Dan Gohman6520e202008-10-18 02:06:02 +00007325 // If there was a non-zero offset that we didn't fold, create an explicit
7326 // addition for it.
7327 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007328 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007329 DAG.getConstant(Offset, getPointerTy()));
7330
Evan Cheng0db9fe62006-04-25 20:13:52 +00007331 return Result;
7332}
7333
Evan Chengda43bcf2008-09-24 00:05:32 +00007334SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007335X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007336 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007337 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007338 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007339}
7340
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007341static SDValue
7342GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007343 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007344 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007345 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007346 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007347 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007348 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007349 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007350 GA->getOffset(),
7351 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007352 if (InFlag) {
7353 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007354 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007355 } else {
7356 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007357 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007358 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007359
7360 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007361 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007362
Rafael Espindola15f1b662009-04-24 12:59:40 +00007363 SDValue Flag = Chain.getValue(1);
7364 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007365}
7366
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007367// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007368static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007369LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007370 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007371 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007372 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7373 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007374 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007375 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007376 InFlag = Chain.getValue(1);
7377
Chris Lattnerb903bed2009-06-26 21:20:29 +00007378 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007379}
7380
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007381// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007382static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007383LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007384 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007385 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7386 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007387}
7388
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007389// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7390// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007391static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007392 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007393 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007394 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007395
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007396 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7397 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7398 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007399
Michael J. Spencerec38de22010-10-10 22:04:20 +00007400 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007401 DAG.getIntPtrConstant(0),
7402 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007403
Chris Lattnerb903bed2009-06-26 21:20:29 +00007404 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007405 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7406 // initialexec.
7407 unsigned WrapperKind = X86ISD::Wrapper;
7408 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007409 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007410 } else if (is64Bit) {
7411 assert(model == TLSModel::InitialExec);
7412 OperandFlags = X86II::MO_GOTTPOFF;
7413 WrapperKind = X86ISD::WrapperRIP;
7414 } else {
7415 assert(model == TLSModel::InitialExec);
7416 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007417 }
Eric Christopherfd179292009-08-27 18:07:15 +00007418
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007419 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7420 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007421 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007422 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007423 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007424 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007425
Rafael Espindola9a580232009-02-27 13:37:18 +00007426 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007427 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007428 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007429
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007430 // The address of the thread local variable is the add of the thread
7431 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007432 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007433}
7434
Dan Gohman475871a2008-07-27 21:46:04 +00007435SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007436X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007437
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007438 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007439 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007440
Eric Christopher30ef0e52010-06-03 04:07:48 +00007441 if (Subtarget->isTargetELF()) {
7442 // TODO: implement the "local dynamic" model
7443 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007444
Eric Christopher30ef0e52010-06-03 04:07:48 +00007445 // If GV is an alias then use the aliasee for determining
7446 // thread-localness.
7447 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7448 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007449
7450 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007451 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007452
Eric Christopher30ef0e52010-06-03 04:07:48 +00007453 switch (model) {
7454 case TLSModel::GeneralDynamic:
7455 case TLSModel::LocalDynamic: // not implemented
7456 if (Subtarget->is64Bit())
7457 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7458 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007459
Eric Christopher30ef0e52010-06-03 04:07:48 +00007460 case TLSModel::InitialExec:
7461 case TLSModel::LocalExec:
7462 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7463 Subtarget->is64Bit());
7464 }
7465 } else if (Subtarget->isTargetDarwin()) {
7466 // Darwin only has one model of TLS. Lower to that.
7467 unsigned char OpFlag = 0;
7468 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7469 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007470
Eric Christopher30ef0e52010-06-03 04:07:48 +00007471 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7472 // global base reg.
7473 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7474 !Subtarget->is64Bit();
7475 if (PIC32)
7476 OpFlag = X86II::MO_TLVP_PIC_BASE;
7477 else
7478 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007479 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007480 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007481 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007482 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007483 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007484
Eric Christopher30ef0e52010-06-03 04:07:48 +00007485 // With PIC32, the address is actually $g + Offset.
7486 if (PIC32)
7487 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7488 DAG.getNode(X86ISD::GlobalBaseReg,
7489 DebugLoc(), getPointerTy()),
7490 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007491
Eric Christopher30ef0e52010-06-03 04:07:48 +00007492 // Lowering the machine isd will make sure everything is in the right
7493 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007494 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007495 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007496 SDValue Args[] = { Chain, Offset };
7497 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007498
Eric Christopher30ef0e52010-06-03 04:07:48 +00007499 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7500 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7501 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007502
Eric Christopher30ef0e52010-06-03 04:07:48 +00007503 // And our return value (tls address) is in the standard call return value
7504 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007505 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
7506 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007507 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007508
Eric Christopher30ef0e52010-06-03 04:07:48 +00007509 assert(false &&
7510 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007511
Torok Edwinc23197a2009-07-14 16:55:14 +00007512 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007513 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007514}
7515
Evan Cheng0db9fe62006-04-25 20:13:52 +00007516
Nadav Rotem43012222011-05-11 08:12:09 +00007517/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007518/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007519SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007520 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007521 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007522 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007523 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007524 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007525 SDValue ShOpLo = Op.getOperand(0);
7526 SDValue ShOpHi = Op.getOperand(1);
7527 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007528 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007529 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007530 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007531
Dan Gohman475871a2008-07-27 21:46:04 +00007532 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007533 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007534 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7535 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007536 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007537 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7538 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007539 }
Evan Chenge3413162006-01-09 18:33:28 +00007540
Owen Anderson825b72b2009-08-11 20:47:22 +00007541 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7542 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007543 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007544 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007545
Dan Gohman475871a2008-07-27 21:46:04 +00007546 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007547 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007548 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7549 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007550
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007551 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007552 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7553 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007554 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007555 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7556 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007557 }
7558
Dan Gohman475871a2008-07-27 21:46:04 +00007559 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007560 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007561}
Evan Chenga3195e82006-01-12 22:54:21 +00007562
Dan Gohmand858e902010-04-17 15:26:15 +00007563SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7564 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007565 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007566
Dale Johannesen0488fb62010-09-30 23:57:10 +00007567 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007568 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007569
Owen Anderson825b72b2009-08-11 20:47:22 +00007570 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007571 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007572
Eli Friedman36df4992009-05-27 00:47:34 +00007573 // These are really Legal; return the operand so the caller accepts it as
7574 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007575 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007576 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007577 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007578 Subtarget->is64Bit()) {
7579 return Op;
7580 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007581
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007582 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007583 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007584 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007585 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007586 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007587 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007588 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007589 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007590 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007591 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7592}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007593
Owen Andersone50ed302009-08-10 22:56:29 +00007594SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007595 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007596 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007597 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007598 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007599 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007600 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007601 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007602 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007603 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007604 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007605
Chris Lattner492a43e2010-09-22 01:28:21 +00007606 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007607
Stuart Hastings84be9582011-06-02 15:57:11 +00007608 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7609 MachineMemOperand *MMO;
7610 if (FI) {
7611 int SSFI = FI->getIndex();
7612 MMO =
7613 DAG.getMachineFunction()
7614 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7615 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7616 } else {
7617 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7618 StackSlot = StackSlot.getOperand(1);
7619 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007620 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007621 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7622 X86ISD::FILD, DL,
7623 Tys, Ops, array_lengthof(Ops),
7624 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007625
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007626 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007627 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007628 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007629
7630 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7631 // shouldn't be necessary except that RFP cannot be live across
7632 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007633 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007634 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7635 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007636 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007637 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007638 SDValue Ops[] = {
7639 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7640 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007641 MachineMemOperand *MMO =
7642 DAG.getMachineFunction()
7643 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007644 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007645
Chris Lattner492a43e2010-09-22 01:28:21 +00007646 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7647 Ops, array_lengthof(Ops),
7648 Op.getValueType(), MMO);
7649 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007650 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007651 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007652 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007653
Evan Cheng0db9fe62006-04-25 20:13:52 +00007654 return Result;
7655}
7656
Bill Wendling8b8a6362009-01-17 03:56:04 +00007657// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007658SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7659 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007660 // This algorithm is not obvious. Here it is in C code, more or less:
7661 /*
7662 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7663 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7664 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007665
Bill Wendling8b8a6362009-01-17 03:56:04 +00007666 // Copy ints to xmm registers.
7667 __m128i xh = _mm_cvtsi32_si128( hi );
7668 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007669
Bill Wendling8b8a6362009-01-17 03:56:04 +00007670 // Combine into low half of a single xmm register.
7671 __m128i x = _mm_unpacklo_epi32( xh, xl );
7672 __m128d d;
7673 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007674
Bill Wendling8b8a6362009-01-17 03:56:04 +00007675 // Merge in appropriate exponents to give the integer bits the right
7676 // magnitude.
7677 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007678
Bill Wendling8b8a6362009-01-17 03:56:04 +00007679 // Subtract away the biases to deal with the IEEE-754 double precision
7680 // implicit 1.
7681 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007682
Bill Wendling8b8a6362009-01-17 03:56:04 +00007683 // All conversions up to here are exact. The correctly rounded result is
7684 // calculated using the current rounding mode using the following
7685 // horizontal add.
7686 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7687 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7688 // store doesn't really need to be here (except
7689 // maybe to zero the other double)
7690 return sd;
7691 }
7692 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007693
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007694 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007695 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007696
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007697 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007698 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007699 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7700 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7701 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7702 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007703 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007704 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007705
Bill Wendling8b8a6362009-01-17 03:56:04 +00007706 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007707 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007708 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007709 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007710 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007711 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007712 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007713
Owen Anderson825b72b2009-08-11 20:47:22 +00007714 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7715 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007716 Op.getOperand(0),
7717 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007718 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7719 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007720 Op.getOperand(0),
7721 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007722 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7723 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007724 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007725 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007726 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007727 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007728 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007729 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007730 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007731 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007732
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007733 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007734 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007735 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7736 DAG.getUNDEF(MVT::v2f64), ShufMask);
7737 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7738 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007739 DAG.getIntPtrConstant(0));
7740}
7741
Bill Wendling8b8a6362009-01-17 03:56:04 +00007742// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007743SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7744 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007745 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007746 // FP constant to bias correct the final result.
7747 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007748 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007749
7750 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007751 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007752 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007753
Eli Friedmanf3704762011-08-29 21:15:46 +00007754 // Zero out the upper parts of the register.
7755 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasSSE2(), DAG);
7756
Owen Anderson825b72b2009-08-11 20:47:22 +00007757 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007758 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007759 DAG.getIntPtrConstant(0));
7760
7761 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007762 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007763 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007764 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007765 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007766 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007767 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007768 MVT::v2f64, Bias)));
7769 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007770 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007771 DAG.getIntPtrConstant(0));
7772
7773 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007774 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007775
7776 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007777 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007778
Owen Anderson825b72b2009-08-11 20:47:22 +00007779 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007780 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007781 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007782 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007783 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007784 }
7785
7786 // Handle final rounding.
7787 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007788}
7789
Dan Gohmand858e902010-04-17 15:26:15 +00007790SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7791 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007792 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007793 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007794
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007795 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007796 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7797 // the optimization here.
7798 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007799 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007800
Owen Andersone50ed302009-08-10 22:56:29 +00007801 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007802 EVT DstVT = Op.getValueType();
7803 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007804 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007805 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007806 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007807
7808 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007809 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007810 if (SrcVT == MVT::i32) {
7811 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7812 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7813 getPointerTy(), StackSlot, WordOff);
7814 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007815 StackSlot, MachinePointerInfo(),
7816 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007817 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007818 OffsetSlot, MachinePointerInfo(),
7819 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007820 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7821 return Fild;
7822 }
7823
7824 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7825 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007826 StackSlot, MachinePointerInfo(),
7827 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007828 // For i64 source, we need to add the appropriate power of 2 if the input
7829 // was negative. This is the same as the optimization in
7830 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7831 // we must be careful to do the computation in x87 extended precision, not
7832 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007833 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7834 MachineMemOperand *MMO =
7835 DAG.getMachineFunction()
7836 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7837 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007838
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007839 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7840 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007841 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7842 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007843
7844 APInt FF(32, 0x5F800000ULL);
7845
7846 // Check whether the sign bit is set.
7847 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7848 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7849 ISD::SETLT);
7850
7851 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7852 SDValue FudgePtr = DAG.getConstantPool(
7853 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7854 getPointerTy());
7855
7856 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7857 SDValue Zero = DAG.getIntPtrConstant(0);
7858 SDValue Four = DAG.getIntPtrConstant(4);
7859 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7860 Zero, Four);
7861 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7862
7863 // Load the value out, extending it from f32 to f80.
7864 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007865 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007866 FudgePtr, MachinePointerInfo::getConstantPool(),
7867 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007868 // Extend everything to 80 bits to force it to be done on x87.
7869 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7870 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007871}
7872
Dan Gohman475871a2008-07-27 21:46:04 +00007873std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007874FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007875 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007876
Owen Andersone50ed302009-08-10 22:56:29 +00007877 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007878
7879 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007880 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7881 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007882 }
7883
Owen Anderson825b72b2009-08-11 20:47:22 +00007884 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7885 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007886 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007887
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007888 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007889 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007890 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007891 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007892 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007893 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007894 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007895 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007896
Evan Cheng87c89352007-10-15 20:11:21 +00007897 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7898 // stack slot.
7899 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007900 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007901 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007902 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007903
Michael J. Spencerec38de22010-10-10 22:04:20 +00007904
7905
Evan Cheng0db9fe62006-04-25 20:13:52 +00007906 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007907 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007908 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007909 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7910 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7911 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007912 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007913
Dan Gohman475871a2008-07-27 21:46:04 +00007914 SDValue Chain = DAG.getEntryNode();
7915 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007916 EVT TheVT = Op.getOperand(0).getValueType();
7917 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007918 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007919 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007920 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007921 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007922 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007923 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007924 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007925 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007926
Chris Lattner492a43e2010-09-22 01:28:21 +00007927 MachineMemOperand *MMO =
7928 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7929 MachineMemOperand::MOLoad, MemSize, MemSize);
7930 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7931 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007932 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007933 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007934 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7935 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007936
Chris Lattner07290932010-09-22 01:05:16 +00007937 MachineMemOperand *MMO =
7938 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7939 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007940
Evan Cheng0db9fe62006-04-25 20:13:52 +00007941 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007942 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007943 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7944 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007945
Chris Lattner27a6c732007-11-24 07:07:01 +00007946 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007947}
7948
Dan Gohmand858e902010-04-17 15:26:15 +00007949SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7950 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007951 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007952 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007953
Eli Friedman948e95a2009-05-23 09:59:16 +00007954 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007955 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007956 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7957 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007958
Chris Lattner27a6c732007-11-24 07:07:01 +00007959 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007960 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007961 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007962}
7963
Dan Gohmand858e902010-04-17 15:26:15 +00007964SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7965 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007966 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7967 SDValue FIST = Vals.first, StackSlot = Vals.second;
7968 assert(FIST.getNode() && "Unexpected failure");
7969
7970 // Load the result.
7971 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007972 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007973}
7974
Dan Gohmand858e902010-04-17 15:26:15 +00007975SDValue X86TargetLowering::LowerFABS(SDValue Op,
7976 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007977 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007978 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007979 EVT VT = Op.getValueType();
7980 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007981 if (VT.isVector())
7982 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007983 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007984 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007985 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007986 CV.push_back(C);
7987 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007988 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007989 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007990 CV.push_back(C);
7991 CV.push_back(C);
7992 CV.push_back(C);
7993 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007994 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007995 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007996 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007997 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007998 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007999 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008000 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008001}
8002
Dan Gohmand858e902010-04-17 15:26:15 +00008003SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008004 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008005 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008006 EVT VT = Op.getValueType();
8007 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008008 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008009 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008010 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008011 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008012 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008013 CV.push_back(C);
8014 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008015 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008016 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008017 CV.push_back(C);
8018 CV.push_back(C);
8019 CV.push_back(C);
8020 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008021 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008022 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008023 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008024 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008025 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008026 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008027 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008028 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008029 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008030 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008031 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008032 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008033 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008034 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008035 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008036}
8037
Dan Gohmand858e902010-04-17 15:26:15 +00008038SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008039 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008040 SDValue Op0 = Op.getOperand(0);
8041 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008042 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008043 EVT VT = Op.getValueType();
8044 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008045
8046 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008047 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008048 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008049 SrcVT = VT;
8050 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008051 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008052 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008053 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008054 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008055 }
8056
8057 // At this point the operands and the result should have the same
8058 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008059
Evan Cheng68c47cb2007-01-05 07:55:56 +00008060 // First get the sign bit of second operand.
8061 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008062 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008063 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8064 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008065 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008066 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8067 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8068 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8069 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008070 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008071 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008072 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008073 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008074 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008075 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008076 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008077
8078 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008079 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008080 // Op0 is MVT::f32, Op1 is MVT::f64.
8081 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8082 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8083 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008084 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008085 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008086 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008087 }
8088
Evan Cheng73d6cf12007-01-05 21:37:56 +00008089 // Clear first operand sign bit.
8090 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008091 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008092 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8093 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008094 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008095 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8096 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8097 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8098 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008099 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008100 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008101 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008102 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008103 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008104 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008105 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008106
8107 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008108 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008109}
8110
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008111SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8112 SDValue N0 = Op.getOperand(0);
8113 DebugLoc dl = Op.getDebugLoc();
8114 EVT VT = Op.getValueType();
8115
8116 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8117 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8118 DAG.getConstant(1, VT));
8119 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8120}
8121
Dan Gohman076aee32009-03-04 19:44:21 +00008122/// Emit nodes that will be selected as "test Op0,Op0", or something
8123/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008124SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008125 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008126 DebugLoc dl = Op.getDebugLoc();
8127
Dan Gohman31125812009-03-07 01:58:32 +00008128 // CF and OF aren't always set the way we want. Determine which
8129 // of these we need.
8130 bool NeedCF = false;
8131 bool NeedOF = false;
8132 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008133 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008134 case X86::COND_A: case X86::COND_AE:
8135 case X86::COND_B: case X86::COND_BE:
8136 NeedCF = true;
8137 break;
8138 case X86::COND_G: case X86::COND_GE:
8139 case X86::COND_L: case X86::COND_LE:
8140 case X86::COND_O: case X86::COND_NO:
8141 NeedOF = true;
8142 break;
Dan Gohman31125812009-03-07 01:58:32 +00008143 }
8144
Dan Gohman076aee32009-03-04 19:44:21 +00008145 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008146 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8147 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008148 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8149 // Emit a CMP with 0, which is the TEST pattern.
8150 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8151 DAG.getConstant(0, Op.getValueType()));
8152
8153 unsigned Opcode = 0;
8154 unsigned NumOperands = 0;
8155 switch (Op.getNode()->getOpcode()) {
8156 case ISD::ADD:
8157 // Due to an isel shortcoming, be conservative if this add is likely to be
8158 // selected as part of a load-modify-store instruction. When the root node
8159 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8160 // uses of other nodes in the match, such as the ADD in this case. This
8161 // leads to the ADD being left around and reselected, with the result being
8162 // two adds in the output. Alas, even if none our users are stores, that
8163 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8164 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8165 // climbing the DAG back to the root, and it doesn't seem to be worth the
8166 // effort.
8167 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00008168 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008169 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8170 goto default_case;
8171
8172 if (ConstantSDNode *C =
8173 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8174 // An add of one will be selected as an INC.
8175 if (C->getAPIntValue() == 1) {
8176 Opcode = X86ISD::INC;
8177 NumOperands = 1;
8178 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008179 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008180
8181 // An add of negative one (subtract of one) will be selected as a DEC.
8182 if (C->getAPIntValue().isAllOnesValue()) {
8183 Opcode = X86ISD::DEC;
8184 NumOperands = 1;
8185 break;
8186 }
Dan Gohman076aee32009-03-04 19:44:21 +00008187 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008188
8189 // Otherwise use a regular EFLAGS-setting add.
8190 Opcode = X86ISD::ADD;
8191 NumOperands = 2;
8192 break;
8193 case ISD::AND: {
8194 // If the primary and result isn't used, don't bother using X86ISD::AND,
8195 // because a TEST instruction will be better.
8196 bool NonFlagUse = false;
8197 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8198 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8199 SDNode *User = *UI;
8200 unsigned UOpNo = UI.getOperandNo();
8201 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8202 // Look pass truncate.
8203 UOpNo = User->use_begin().getOperandNo();
8204 User = *User->use_begin();
8205 }
8206
8207 if (User->getOpcode() != ISD::BRCOND &&
8208 User->getOpcode() != ISD::SETCC &&
8209 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8210 NonFlagUse = true;
8211 break;
8212 }
Dan Gohman076aee32009-03-04 19:44:21 +00008213 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008214
8215 if (!NonFlagUse)
8216 break;
8217 }
8218 // FALL THROUGH
8219 case ISD::SUB:
8220 case ISD::OR:
8221 case ISD::XOR:
8222 // Due to the ISEL shortcoming noted above, be conservative if this op is
8223 // likely to be selected as part of a load-modify-store instruction.
8224 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8225 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8226 if (UI->getOpcode() == ISD::STORE)
8227 goto default_case;
8228
8229 // Otherwise use a regular EFLAGS-setting instruction.
8230 switch (Op.getNode()->getOpcode()) {
8231 default: llvm_unreachable("unexpected operator!");
8232 case ISD::SUB: Opcode = X86ISD::SUB; break;
8233 case ISD::OR: Opcode = X86ISD::OR; break;
8234 case ISD::XOR: Opcode = X86ISD::XOR; break;
8235 case ISD::AND: Opcode = X86ISD::AND; break;
8236 }
8237
8238 NumOperands = 2;
8239 break;
8240 case X86ISD::ADD:
8241 case X86ISD::SUB:
8242 case X86ISD::INC:
8243 case X86ISD::DEC:
8244 case X86ISD::OR:
8245 case X86ISD::XOR:
8246 case X86ISD::AND:
8247 return SDValue(Op.getNode(), 1);
8248 default:
8249 default_case:
8250 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008251 }
8252
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008253 if (Opcode == 0)
8254 // Emit a CMP with 0, which is the TEST pattern.
8255 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8256 DAG.getConstant(0, Op.getValueType()));
8257
8258 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8259 SmallVector<SDValue, 4> Ops;
8260 for (unsigned i = 0; i != NumOperands; ++i)
8261 Ops.push_back(Op.getOperand(i));
8262
8263 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8264 DAG.ReplaceAllUsesWith(Op, New);
8265 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008266}
8267
8268/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8269/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008270SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008271 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008272 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8273 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008274 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008275
8276 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008277 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008278}
8279
Evan Chengd40d03e2010-01-06 19:38:29 +00008280/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8281/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008282SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8283 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008284 SDValue Op0 = And.getOperand(0);
8285 SDValue Op1 = And.getOperand(1);
8286 if (Op0.getOpcode() == ISD::TRUNCATE)
8287 Op0 = Op0.getOperand(0);
8288 if (Op1.getOpcode() == ISD::TRUNCATE)
8289 Op1 = Op1.getOperand(0);
8290
Evan Chengd40d03e2010-01-06 19:38:29 +00008291 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008292 if (Op1.getOpcode() == ISD::SHL)
8293 std::swap(Op0, Op1);
8294 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008295 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8296 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008297 // If we looked past a truncate, check that it's only truncating away
8298 // known zeros.
8299 unsigned BitWidth = Op0.getValueSizeInBits();
8300 unsigned AndBitWidth = And.getValueSizeInBits();
8301 if (BitWidth > AndBitWidth) {
8302 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8303 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8304 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8305 return SDValue();
8306 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008307 LHS = Op1;
8308 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008309 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008310 } else if (Op1.getOpcode() == ISD::Constant) {
8311 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8312 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008313 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8314 LHS = AndLHS.getOperand(0);
8315 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008316 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008317 }
Evan Cheng0488db92007-09-25 01:57:46 +00008318
Evan Chengd40d03e2010-01-06 19:38:29 +00008319 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008320 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008321 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008322 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008323 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008324 // Also promote i16 to i32 for performance / code size reason.
8325 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008326 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008327 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008328
Evan Chengd40d03e2010-01-06 19:38:29 +00008329 // If the operand types disagree, extend the shift amount to match. Since
8330 // BT ignores high bits (like shifts) we can use anyextend.
8331 if (LHS.getValueType() != RHS.getValueType())
8332 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008333
Evan Chengd40d03e2010-01-06 19:38:29 +00008334 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8335 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8336 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8337 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008338 }
8339
Evan Cheng54de3ea2010-01-05 06:52:31 +00008340 return SDValue();
8341}
8342
Dan Gohmand858e902010-04-17 15:26:15 +00008343SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008344
8345 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8346
Evan Cheng54de3ea2010-01-05 06:52:31 +00008347 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8348 SDValue Op0 = Op.getOperand(0);
8349 SDValue Op1 = Op.getOperand(1);
8350 DebugLoc dl = Op.getDebugLoc();
8351 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8352
8353 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008354 // Lower (X & (1 << N)) == 0 to BT(X, N).
8355 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8356 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008357 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008358 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008359 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008360 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8361 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8362 if (NewSetCC.getNode())
8363 return NewSetCC;
8364 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008365
Chris Lattner481eebc2010-12-19 21:23:48 +00008366 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8367 // these.
8368 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008369 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008370 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8371 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008372
Chris Lattner481eebc2010-12-19 21:23:48 +00008373 // If the input is a setcc, then reuse the input setcc or use a new one with
8374 // the inverted condition.
8375 if (Op0.getOpcode() == X86ISD::SETCC) {
8376 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8377 bool Invert = (CC == ISD::SETNE) ^
8378 cast<ConstantSDNode>(Op1)->isNullValue();
8379 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008380
Evan Cheng2c755ba2010-02-27 07:36:59 +00008381 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008382 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8383 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8384 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008385 }
8386
Evan Chenge5b51ac2010-04-17 06:13:15 +00008387 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008388 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008389 if (X86CC == X86::COND_INVALID)
8390 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008391
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008392 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008393 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008394 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008395}
8396
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008397// Lower256IntVETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
8398// ones, and then concatenate the result back.
8399static SDValue Lower256IntVETCC(SDValue Op, SelectionDAG &DAG) {
8400 EVT VT = Op.getValueType();
8401
Duncan Sands28b77e92011-09-06 19:07:46 +00008402 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008403 "Unsupported value type for operation");
8404
8405 int NumElems = VT.getVectorNumElements();
8406 DebugLoc dl = Op.getDebugLoc();
8407 SDValue CC = Op.getOperand(2);
8408 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8409 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8410
8411 // Extract the LHS vectors
8412 SDValue LHS = Op.getOperand(0);
8413 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8414 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8415
8416 // Extract the RHS vectors
8417 SDValue RHS = Op.getOperand(1);
8418 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8419 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8420
8421 // Issue the operation on the smaller types and concatenate the result back
8422 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8423 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8424 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8425 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8426 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8427}
8428
8429
Dan Gohmand858e902010-04-17 15:26:15 +00008430SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008431 SDValue Cond;
8432 SDValue Op0 = Op.getOperand(0);
8433 SDValue Op1 = Op.getOperand(1);
8434 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008435 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008436 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8437 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008438 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008439
8440 if (isFP) {
8441 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008442 EVT EltVT = Op0.getValueType().getVectorElementType();
8443 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8444
8445 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008446 bool Swap = false;
8447
8448 switch (SetCCOpcode) {
8449 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008450 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008451 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00008452 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00008453 case ISD::SETGT: Swap = true; // Fallthrough
8454 case ISD::SETLT:
8455 case ISD::SETOLT: SSECC = 1; break;
8456 case ISD::SETOGE:
8457 case ISD::SETGE: Swap = true; // Fallthrough
8458 case ISD::SETLE:
8459 case ISD::SETOLE: SSECC = 2; break;
8460 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008461 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008462 case ISD::SETNE: SSECC = 4; break;
8463 case ISD::SETULE: Swap = true;
8464 case ISD::SETUGE: SSECC = 5; break;
8465 case ISD::SETULT: Swap = true;
8466 case ISD::SETUGT: SSECC = 6; break;
8467 case ISD::SETO: SSECC = 7; break;
8468 }
8469 if (Swap)
8470 std::swap(Op0, Op1);
8471
Nate Begemanfb8ead02008-07-25 19:05:58 +00008472 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008473 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008474 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008475 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00008476 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8477 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008478 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008479 }
8480 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008481 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00008482 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8483 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008484 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008485 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008486 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008487 }
8488 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00008489 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008490 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008491
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008492 // Break 256-bit integer vector compare into smaller ones.
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008493 if (!isFP && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008494 return Lower256IntVETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008495
Nate Begeman30a0de92008-07-17 16:51:19 +00008496 // We are handling one of the integer comparisons here. Since SSE only has
8497 // GT and EQ comparisons for integer, swapping operands and multiple
8498 // operations may be required for some comparisons.
8499 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8500 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008501
Owen Anderson825b72b2009-08-11 20:47:22 +00008502 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008503 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008504 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008505 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008506 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8507 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008508 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008509
Nate Begeman30a0de92008-07-17 16:51:19 +00008510 switch (SetCCOpcode) {
8511 default: break;
8512 case ISD::SETNE: Invert = true;
8513 case ISD::SETEQ: Opc = EQOpc; break;
8514 case ISD::SETLT: Swap = true;
8515 case ISD::SETGT: Opc = GTOpc; break;
8516 case ISD::SETGE: Swap = true;
8517 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8518 case ISD::SETULT: Swap = true;
8519 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8520 case ISD::SETUGE: Swap = true;
8521 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8522 }
8523 if (Swap)
8524 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008525
Nate Begeman30a0de92008-07-17 16:51:19 +00008526 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8527 // bits of the inputs before performing those operations.
8528 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008529 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008530 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8531 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008532 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008533 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8534 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008535 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8536 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008537 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008538
Dale Johannesenace16102009-02-03 19:33:06 +00008539 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008540
8541 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008542 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008543 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008544
Nate Begeman30a0de92008-07-17 16:51:19 +00008545 return Result;
8546}
Evan Cheng0488db92007-09-25 01:57:46 +00008547
Evan Cheng370e5342008-12-03 08:38:43 +00008548// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008549static bool isX86LogicalCmp(SDValue Op) {
8550 unsigned Opc = Op.getNode()->getOpcode();
8551 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8552 return true;
8553 if (Op.getResNo() == 1 &&
8554 (Opc == X86ISD::ADD ||
8555 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008556 Opc == X86ISD::ADC ||
8557 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008558 Opc == X86ISD::SMUL ||
8559 Opc == X86ISD::UMUL ||
8560 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008561 Opc == X86ISD::DEC ||
8562 Opc == X86ISD::OR ||
8563 Opc == X86ISD::XOR ||
8564 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008565 return true;
8566
Chris Lattner9637d5b2010-12-05 07:49:54 +00008567 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8568 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008569
Dan Gohman076aee32009-03-04 19:44:21 +00008570 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008571}
8572
Chris Lattnera2b56002010-12-05 01:23:24 +00008573static bool isZero(SDValue V) {
8574 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8575 return C && C->isNullValue();
8576}
8577
Chris Lattner96908b12010-12-05 02:00:51 +00008578static bool isAllOnes(SDValue V) {
8579 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8580 return C && C->isAllOnesValue();
8581}
8582
Dan Gohmand858e902010-04-17 15:26:15 +00008583SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008584 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008585 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008586 SDValue Op1 = Op.getOperand(1);
8587 SDValue Op2 = Op.getOperand(2);
8588 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008589 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008590
Dan Gohman1a492952009-10-20 16:22:37 +00008591 if (Cond.getOpcode() == ISD::SETCC) {
8592 SDValue NewCond = LowerSETCC(Cond, DAG);
8593 if (NewCond.getNode())
8594 Cond = NewCond;
8595 }
Evan Cheng734503b2006-09-11 02:19:56 +00008596
Chris Lattnera2b56002010-12-05 01:23:24 +00008597 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008598 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008599 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008600 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008601 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008602 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8603 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008604 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008605
Chris Lattnera2b56002010-12-05 01:23:24 +00008606 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008607
8608 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008609 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8610 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008611
8612 SDValue CmpOp0 = Cmp.getOperand(0);
8613 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8614 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008615
Chris Lattner96908b12010-12-05 02:00:51 +00008616 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008617 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8618 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008619
Chris Lattner96908b12010-12-05 02:00:51 +00008620 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8621 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008622
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008623 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008624 if (N2C == 0 || !N2C->isNullValue())
8625 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8626 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008627 }
8628 }
8629
Chris Lattnera2b56002010-12-05 01:23:24 +00008630 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008631 if (Cond.getOpcode() == ISD::AND &&
8632 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8633 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008634 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008635 Cond = Cond.getOperand(0);
8636 }
8637
Evan Cheng3f41d662007-10-08 22:16:29 +00008638 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8639 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008640 if (Cond.getOpcode() == X86ISD::SETCC ||
8641 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008642 CC = Cond.getOperand(0);
8643
Dan Gohman475871a2008-07-27 21:46:04 +00008644 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008645 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008646 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008647
Evan Cheng3f41d662007-10-08 22:16:29 +00008648 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008649 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008650 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008651 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008652
Chris Lattnerd1980a52009-03-12 06:52:53 +00008653 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8654 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008655 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008656 addTest = false;
8657 }
8658 }
8659
8660 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008661 // Look pass the truncate.
8662 if (Cond.getOpcode() == ISD::TRUNCATE)
8663 Cond = Cond.getOperand(0);
8664
8665 // We know the result of AND is compared against zero. Try to match
8666 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008667 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008668 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008669 if (NewSetCC.getNode()) {
8670 CC = NewSetCC.getOperand(0);
8671 Cond = NewSetCC.getOperand(1);
8672 addTest = false;
8673 }
8674 }
8675 }
8676
8677 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008678 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008679 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008680 }
8681
Benjamin Kramere915ff32010-12-22 23:09:28 +00008682 // a < b ? -1 : 0 -> RES = ~setcc_carry
8683 // a < b ? 0 : -1 -> RES = setcc_carry
8684 // a >= b ? -1 : 0 -> RES = setcc_carry
8685 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8686 if (Cond.getOpcode() == X86ISD::CMP) {
8687 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8688
8689 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8690 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8691 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8692 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8693 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8694 return DAG.getNOT(DL, Res, Res.getValueType());
8695 return Res;
8696 }
8697 }
8698
Evan Cheng0488db92007-09-25 01:57:46 +00008699 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8700 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008701 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008702 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008703 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008704}
8705
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00008706SDValue X86TargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
8707 SDValue Cond = Op.getOperand(0);
8708 SDValue Op1 = Op.getOperand(1);
8709 SDValue Op2 = Op.getOperand(2);
8710 DebugLoc DL = Op.getDebugLoc();
8711
Bruno Cardoso Lopes814c6ce2011-09-08 18:05:08 +00008712 SDValue Ops[] = {Op1, Op2, Cond};
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00008713
8714 assert(Op1.getValueType().isVector() && "Op1 must be a vector");
8715 assert(Op2.getValueType().isVector() && "Op2 must be a vector");
8716 assert(Cond.getValueType().isVector() && "Cond must be a vector");
8717 assert(Op1.getValueType() == Op2.getValueType() && "Type mismatch");
Nadav Rotemee64be92011-09-08 22:17:35 +00008718
8719 EVT VT = Op1.getValueType();
8720 switch (VT.getSimpleVT().SimpleTy) {
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00008721 default: break;
Nadav Rotem8ffad562011-09-09 20:29:17 +00008722 // SSE4:
Nadav Rotemee64be92011-09-08 22:17:35 +00008723 case MVT::v2i64:
8724 case MVT::v2f64:
Nadav Rotemee64be92011-09-08 22:17:35 +00008725 case MVT::v4i32:
8726 case MVT::v4f32:
Nadav Rotemee64be92011-09-08 22:17:35 +00008727 case MVT::v16i8:
Nadav Rotem8ffad562011-09-09 20:29:17 +00008728 case MVT::v8i16:
8729 // AVX:
8730 case MVT::v4i64:
8731 case MVT::v4f64:
8732 case MVT::v8i32:
8733 case MVT::v8f32:
8734 case MVT::v32i8:
8735 case MVT::v16i16:
8736 return DAG.getNode(X86ISD::BLENDV, DL, VT, Ops, array_lengthof(Ops));
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00008737 }
Nadav Rotemee64be92011-09-08 22:17:35 +00008738
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00008739 return SDValue();
8740}
8741
8742
Evan Cheng370e5342008-12-03 08:38:43 +00008743// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8744// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8745// from the AND / OR.
8746static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8747 Opc = Op.getOpcode();
8748 if (Opc != ISD::OR && Opc != ISD::AND)
8749 return false;
8750 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8751 Op.getOperand(0).hasOneUse() &&
8752 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8753 Op.getOperand(1).hasOneUse());
8754}
8755
Evan Cheng961d6d42009-02-02 08:19:07 +00008756// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8757// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008758static bool isXor1OfSetCC(SDValue Op) {
8759 if (Op.getOpcode() != ISD::XOR)
8760 return false;
8761 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8762 if (N1C && N1C->getAPIntValue() == 1) {
8763 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8764 Op.getOperand(0).hasOneUse();
8765 }
8766 return false;
8767}
8768
Dan Gohmand858e902010-04-17 15:26:15 +00008769SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008770 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008771 SDValue Chain = Op.getOperand(0);
8772 SDValue Cond = Op.getOperand(1);
8773 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008774 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008775 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00008776
Dan Gohman1a492952009-10-20 16:22:37 +00008777 if (Cond.getOpcode() == ISD::SETCC) {
8778 SDValue NewCond = LowerSETCC(Cond, DAG);
8779 if (NewCond.getNode())
8780 Cond = NewCond;
8781 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008782#if 0
8783 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008784 else if (Cond.getOpcode() == X86ISD::ADD ||
8785 Cond.getOpcode() == X86ISD::SUB ||
8786 Cond.getOpcode() == X86ISD::SMUL ||
8787 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008788 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008789#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008790
Evan Chengad9c0a32009-12-15 00:53:42 +00008791 // Look pass (and (setcc_carry (cmp ...)), 1).
8792 if (Cond.getOpcode() == ISD::AND &&
8793 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8794 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008795 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008796 Cond = Cond.getOperand(0);
8797 }
8798
Evan Cheng3f41d662007-10-08 22:16:29 +00008799 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8800 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008801 if (Cond.getOpcode() == X86ISD::SETCC ||
8802 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008803 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008804
Dan Gohman475871a2008-07-27 21:46:04 +00008805 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008806 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008807 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008808 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008809 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008810 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008811 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008812 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008813 default: break;
8814 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008815 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008816 // These can only come from an arithmetic instruction with overflow,
8817 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008818 Cond = Cond.getNode()->getOperand(1);
8819 addTest = false;
8820 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008821 }
Evan Cheng0488db92007-09-25 01:57:46 +00008822 }
Evan Cheng370e5342008-12-03 08:38:43 +00008823 } else {
8824 unsigned CondOpc;
8825 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8826 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008827 if (CondOpc == ISD::OR) {
8828 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8829 // two branches instead of an explicit OR instruction with a
8830 // separate test.
8831 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008832 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008833 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008834 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008835 Chain, Dest, CC, Cmp);
8836 CC = Cond.getOperand(1).getOperand(0);
8837 Cond = Cmp;
8838 addTest = false;
8839 }
8840 } else { // ISD::AND
8841 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8842 // two branches instead of an explicit AND instruction with a
8843 // separate test. However, we only do this if this block doesn't
8844 // have a fall-through edge, because this requires an explicit
8845 // jmp when the condition is false.
8846 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008847 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008848 Op.getNode()->hasOneUse()) {
8849 X86::CondCode CCode =
8850 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8851 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008852 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008853 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008854 // Look for an unconditional branch following this conditional branch.
8855 // We need this because we need to reverse the successors in order
8856 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008857 if (User->getOpcode() == ISD::BR) {
8858 SDValue FalseBB = User->getOperand(1);
8859 SDNode *NewBR =
8860 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008861 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008862 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008863 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008864
Dale Johannesene4d209d2009-02-03 20:21:25 +00008865 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008866 Chain, Dest, CC, Cmp);
8867 X86::CondCode CCode =
8868 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8869 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008870 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008871 Cond = Cmp;
8872 addTest = false;
8873 }
8874 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008875 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008876 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8877 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8878 // It should be transformed during dag combiner except when the condition
8879 // is set by a arithmetics with overflow node.
8880 X86::CondCode CCode =
8881 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8882 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008883 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008884 Cond = Cond.getOperand(0).getOperand(1);
8885 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00008886 }
Evan Cheng0488db92007-09-25 01:57:46 +00008887 }
8888
8889 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008890 // Look pass the truncate.
8891 if (Cond.getOpcode() == ISD::TRUNCATE)
8892 Cond = Cond.getOperand(0);
8893
8894 // We know the result of AND is compared against zero. Try to match
8895 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008896 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008897 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8898 if (NewSetCC.getNode()) {
8899 CC = NewSetCC.getOperand(0);
8900 Cond = NewSetCC.getOperand(1);
8901 addTest = false;
8902 }
8903 }
8904 }
8905
8906 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008907 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008908 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008909 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008910 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008911 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008912}
8913
Anton Korobeynikove060b532007-04-17 19:34:00 +00008914
8915// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8916// Calls to _alloca is needed to probe the stack when allocating more than 4k
8917// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8918// that the guard pages used by the OS virtual memory manager are allocated in
8919// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008920SDValue
8921X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008922 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008923 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
8924 EnableSegmentedStacks) &&
8925 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008926 "are being used");
8927 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008928 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008929
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008930 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008931 SDValue Chain = Op.getOperand(0);
8932 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008933 // FIXME: Ensure alignment here
8934
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008935 bool Is64Bit = Subtarget->is64Bit();
8936 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008937
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008938 if (EnableSegmentedStacks) {
8939 MachineFunction &MF = DAG.getMachineFunction();
8940 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008941
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008942 if (Is64Bit) {
8943 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008944 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008945 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008946
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008947 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8948 I != E; I++)
8949 if (I->hasNestAttr())
8950 report_fatal_error("Cannot use segmented stacks with functions that "
8951 "have nested arguments.");
8952 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008953
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008954 const TargetRegisterClass *AddrRegClass =
8955 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8956 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8957 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8958 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8959 DAG.getRegister(Vreg, SPTy));
8960 SDValue Ops1[2] = { Value, Chain };
8961 return DAG.getMergeValues(Ops1, 2, dl);
8962 } else {
8963 SDValue Flag;
8964 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008965
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008966 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8967 Flag = Chain.getValue(1);
8968 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008969
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008970 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
8971 Flag = Chain.getValue(1);
8972
8973 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
8974
8975 SDValue Ops1[2] = { Chain.getValue(0), Chain };
8976 return DAG.getMergeValues(Ops1, 2, dl);
8977 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008978}
8979
Dan Gohmand858e902010-04-17 15:26:15 +00008980SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008981 MachineFunction &MF = DAG.getMachineFunction();
8982 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8983
Dan Gohman69de1932008-02-06 22:27:42 +00008984 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008985 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008986
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008987 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008988 // vastart just stores the address of the VarArgsFrameIndex slot into the
8989 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008990 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8991 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008992 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8993 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008994 }
8995
8996 // __va_list_tag:
8997 // gp_offset (0 - 6 * 8)
8998 // fp_offset (48 - 48 + 8 * 16)
8999 // overflow_arg_area (point to parameters coming in memory).
9000 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009001 SmallVector<SDValue, 8> MemOps;
9002 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009003 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009004 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009005 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9006 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009007 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009008 MemOps.push_back(Store);
9009
9010 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009011 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009012 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009013 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009014 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9015 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009016 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009017 MemOps.push_back(Store);
9018
9019 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009020 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009021 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009022 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9023 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009024 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9025 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009026 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009027 MemOps.push_back(Store);
9028
9029 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009030 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009031 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009032 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9033 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009034 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9035 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009036 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009037 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009038 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009039}
9040
Dan Gohmand858e902010-04-17 15:26:15 +00009041SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009042 assert(Subtarget->is64Bit() &&
9043 "LowerVAARG only handles 64-bit va_arg!");
9044 assert((Subtarget->isTargetLinux() ||
9045 Subtarget->isTargetDarwin()) &&
9046 "Unhandled target in LowerVAARG");
9047 assert(Op.getNode()->getNumOperands() == 4);
9048 SDValue Chain = Op.getOperand(0);
9049 SDValue SrcPtr = Op.getOperand(1);
9050 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9051 unsigned Align = Op.getConstantOperandVal(3);
9052 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009053
Dan Gohman320afb82010-10-12 18:00:49 +00009054 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009055 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009056 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9057 uint8_t ArgMode;
9058
9059 // Decide which area this value should be read from.
9060 // TODO: Implement the AMD64 ABI in its entirety. This simple
9061 // selection mechanism works only for the basic types.
9062 if (ArgVT == MVT::f80) {
9063 llvm_unreachable("va_arg for f80 not yet implemented");
9064 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9065 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9066 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9067 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9068 } else {
9069 llvm_unreachable("Unhandled argument type in LowerVAARG");
9070 }
9071
9072 if (ArgMode == 2) {
9073 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009074 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009075 !(DAG.getMachineFunction()
9076 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009077 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009078 }
9079
9080 // Insert VAARG_64 node into the DAG
9081 // VAARG_64 returns two values: Variable Argument Address, Chain
9082 SmallVector<SDValue, 11> InstOps;
9083 InstOps.push_back(Chain);
9084 InstOps.push_back(SrcPtr);
9085 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9086 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9087 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9088 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9089 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9090 VTs, &InstOps[0], InstOps.size(),
9091 MVT::i64,
9092 MachinePointerInfo(SV),
9093 /*Align=*/0,
9094 /*Volatile=*/false,
9095 /*ReadMem=*/true,
9096 /*WriteMem=*/true);
9097 Chain = VAARG.getValue(1);
9098
9099 // Load the next argument and return it
9100 return DAG.getLoad(ArgVT, dl,
9101 Chain,
9102 VAARG,
9103 MachinePointerInfo(),
9104 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009105}
9106
Dan Gohmand858e902010-04-17 15:26:15 +00009107SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009108 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009109 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009110 SDValue Chain = Op.getOperand(0);
9111 SDValue DstPtr = Op.getOperand(1);
9112 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009113 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9114 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009115 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009116
Chris Lattnere72f2022010-09-21 05:40:29 +00009117 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009118 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009119 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009120 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009121}
9122
Dan Gohman475871a2008-07-27 21:46:04 +00009123SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009124X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009125 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009126 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009127 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009128 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009129 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009130 case Intrinsic::x86_sse_comieq_ss:
9131 case Intrinsic::x86_sse_comilt_ss:
9132 case Intrinsic::x86_sse_comile_ss:
9133 case Intrinsic::x86_sse_comigt_ss:
9134 case Intrinsic::x86_sse_comige_ss:
9135 case Intrinsic::x86_sse_comineq_ss:
9136 case Intrinsic::x86_sse_ucomieq_ss:
9137 case Intrinsic::x86_sse_ucomilt_ss:
9138 case Intrinsic::x86_sse_ucomile_ss:
9139 case Intrinsic::x86_sse_ucomigt_ss:
9140 case Intrinsic::x86_sse_ucomige_ss:
9141 case Intrinsic::x86_sse_ucomineq_ss:
9142 case Intrinsic::x86_sse2_comieq_sd:
9143 case Intrinsic::x86_sse2_comilt_sd:
9144 case Intrinsic::x86_sse2_comile_sd:
9145 case Intrinsic::x86_sse2_comigt_sd:
9146 case Intrinsic::x86_sse2_comige_sd:
9147 case Intrinsic::x86_sse2_comineq_sd:
9148 case Intrinsic::x86_sse2_ucomieq_sd:
9149 case Intrinsic::x86_sse2_ucomilt_sd:
9150 case Intrinsic::x86_sse2_ucomile_sd:
9151 case Intrinsic::x86_sse2_ucomigt_sd:
9152 case Intrinsic::x86_sse2_ucomige_sd:
9153 case Intrinsic::x86_sse2_ucomineq_sd: {
9154 unsigned Opc = 0;
9155 ISD::CondCode CC = ISD::SETCC_INVALID;
9156 switch (IntNo) {
9157 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009158 case Intrinsic::x86_sse_comieq_ss:
9159 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009160 Opc = X86ISD::COMI;
9161 CC = ISD::SETEQ;
9162 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009163 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009164 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009165 Opc = X86ISD::COMI;
9166 CC = ISD::SETLT;
9167 break;
9168 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009169 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009170 Opc = X86ISD::COMI;
9171 CC = ISD::SETLE;
9172 break;
9173 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009174 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009175 Opc = X86ISD::COMI;
9176 CC = ISD::SETGT;
9177 break;
9178 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009179 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009180 Opc = X86ISD::COMI;
9181 CC = ISD::SETGE;
9182 break;
9183 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009184 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009185 Opc = X86ISD::COMI;
9186 CC = ISD::SETNE;
9187 break;
9188 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009189 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009190 Opc = X86ISD::UCOMI;
9191 CC = ISD::SETEQ;
9192 break;
9193 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009194 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009195 Opc = X86ISD::UCOMI;
9196 CC = ISD::SETLT;
9197 break;
9198 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009199 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009200 Opc = X86ISD::UCOMI;
9201 CC = ISD::SETLE;
9202 break;
9203 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009204 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009205 Opc = X86ISD::UCOMI;
9206 CC = ISD::SETGT;
9207 break;
9208 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009209 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009210 Opc = X86ISD::UCOMI;
9211 CC = ISD::SETGE;
9212 break;
9213 case Intrinsic::x86_sse_ucomineq_ss:
9214 case Intrinsic::x86_sse2_ucomineq_sd:
9215 Opc = X86ISD::UCOMI;
9216 CC = ISD::SETNE;
9217 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009218 }
Evan Cheng734503b2006-09-11 02:19:56 +00009219
Dan Gohman475871a2008-07-27 21:46:04 +00009220 SDValue LHS = Op.getOperand(1);
9221 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009222 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009223 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009224 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9225 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9226 DAG.getConstant(X86CC, MVT::i8), Cond);
9227 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009228 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009229 // ptest and testp intrinsics. The intrinsic these come from are designed to
9230 // return an integer value, not just an instruction so lower it to the ptest
9231 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009232 case Intrinsic::x86_sse41_ptestz:
9233 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009234 case Intrinsic::x86_sse41_ptestnzc:
9235 case Intrinsic::x86_avx_ptestz_256:
9236 case Intrinsic::x86_avx_ptestc_256:
9237 case Intrinsic::x86_avx_ptestnzc_256:
9238 case Intrinsic::x86_avx_vtestz_ps:
9239 case Intrinsic::x86_avx_vtestc_ps:
9240 case Intrinsic::x86_avx_vtestnzc_ps:
9241 case Intrinsic::x86_avx_vtestz_pd:
9242 case Intrinsic::x86_avx_vtestc_pd:
9243 case Intrinsic::x86_avx_vtestnzc_pd:
9244 case Intrinsic::x86_avx_vtestz_ps_256:
9245 case Intrinsic::x86_avx_vtestc_ps_256:
9246 case Intrinsic::x86_avx_vtestnzc_ps_256:
9247 case Intrinsic::x86_avx_vtestz_pd_256:
9248 case Intrinsic::x86_avx_vtestc_pd_256:
9249 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9250 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009251 unsigned X86CC = 0;
9252 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009253 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009254 case Intrinsic::x86_avx_vtestz_ps:
9255 case Intrinsic::x86_avx_vtestz_pd:
9256 case Intrinsic::x86_avx_vtestz_ps_256:
9257 case Intrinsic::x86_avx_vtestz_pd_256:
9258 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009259 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009260 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009261 // ZF = 1
9262 X86CC = X86::COND_E;
9263 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009264 case Intrinsic::x86_avx_vtestc_ps:
9265 case Intrinsic::x86_avx_vtestc_pd:
9266 case Intrinsic::x86_avx_vtestc_ps_256:
9267 case Intrinsic::x86_avx_vtestc_pd_256:
9268 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009269 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009270 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009271 // CF = 1
9272 X86CC = X86::COND_B;
9273 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009274 case Intrinsic::x86_avx_vtestnzc_ps:
9275 case Intrinsic::x86_avx_vtestnzc_pd:
9276 case Intrinsic::x86_avx_vtestnzc_ps_256:
9277 case Intrinsic::x86_avx_vtestnzc_pd_256:
9278 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009279 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009280 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009281 // ZF and CF = 0
9282 X86CC = X86::COND_A;
9283 break;
9284 }
Eric Christopherfd179292009-08-27 18:07:15 +00009285
Eric Christopher71c67532009-07-29 00:28:05 +00009286 SDValue LHS = Op.getOperand(1);
9287 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009288 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9289 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009290 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9291 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9292 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009293 }
Evan Cheng5759f972008-05-04 09:15:50 +00009294
9295 // Fix vector shift instructions where the last operand is a non-immediate
9296 // i32 value.
9297 case Intrinsic::x86_sse2_pslli_w:
9298 case Intrinsic::x86_sse2_pslli_d:
9299 case Intrinsic::x86_sse2_pslli_q:
9300 case Intrinsic::x86_sse2_psrli_w:
9301 case Intrinsic::x86_sse2_psrli_d:
9302 case Intrinsic::x86_sse2_psrli_q:
9303 case Intrinsic::x86_sse2_psrai_w:
9304 case Intrinsic::x86_sse2_psrai_d:
9305 case Intrinsic::x86_mmx_pslli_w:
9306 case Intrinsic::x86_mmx_pslli_d:
9307 case Intrinsic::x86_mmx_pslli_q:
9308 case Intrinsic::x86_mmx_psrli_w:
9309 case Intrinsic::x86_mmx_psrli_d:
9310 case Intrinsic::x86_mmx_psrli_q:
9311 case Intrinsic::x86_mmx_psrai_w:
9312 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009313 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009314 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009315 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009316
9317 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009318 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009319 switch (IntNo) {
9320 case Intrinsic::x86_sse2_pslli_w:
9321 NewIntNo = Intrinsic::x86_sse2_psll_w;
9322 break;
9323 case Intrinsic::x86_sse2_pslli_d:
9324 NewIntNo = Intrinsic::x86_sse2_psll_d;
9325 break;
9326 case Intrinsic::x86_sse2_pslli_q:
9327 NewIntNo = Intrinsic::x86_sse2_psll_q;
9328 break;
9329 case Intrinsic::x86_sse2_psrli_w:
9330 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9331 break;
9332 case Intrinsic::x86_sse2_psrli_d:
9333 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9334 break;
9335 case Intrinsic::x86_sse2_psrli_q:
9336 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9337 break;
9338 case Intrinsic::x86_sse2_psrai_w:
9339 NewIntNo = Intrinsic::x86_sse2_psra_w;
9340 break;
9341 case Intrinsic::x86_sse2_psrai_d:
9342 NewIntNo = Intrinsic::x86_sse2_psra_d;
9343 break;
9344 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009345 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009346 switch (IntNo) {
9347 case Intrinsic::x86_mmx_pslli_w:
9348 NewIntNo = Intrinsic::x86_mmx_psll_w;
9349 break;
9350 case Intrinsic::x86_mmx_pslli_d:
9351 NewIntNo = Intrinsic::x86_mmx_psll_d;
9352 break;
9353 case Intrinsic::x86_mmx_pslli_q:
9354 NewIntNo = Intrinsic::x86_mmx_psll_q;
9355 break;
9356 case Intrinsic::x86_mmx_psrli_w:
9357 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9358 break;
9359 case Intrinsic::x86_mmx_psrli_d:
9360 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9361 break;
9362 case Intrinsic::x86_mmx_psrli_q:
9363 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9364 break;
9365 case Intrinsic::x86_mmx_psrai_w:
9366 NewIntNo = Intrinsic::x86_mmx_psra_w;
9367 break;
9368 case Intrinsic::x86_mmx_psrai_d:
9369 NewIntNo = Intrinsic::x86_mmx_psra_d;
9370 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009371 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009372 }
9373 break;
9374 }
9375 }
Mon P Wangefa42202009-09-03 19:56:25 +00009376
9377 // The vector shift intrinsics with scalars uses 32b shift amounts but
9378 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9379 // to be zero.
9380 SDValue ShOps[4];
9381 ShOps[0] = ShAmt;
9382 ShOps[1] = DAG.getConstant(0, MVT::i32);
9383 if (ShAmtVT == MVT::v4i32) {
9384 ShOps[2] = DAG.getUNDEF(MVT::i32);
9385 ShOps[3] = DAG.getUNDEF(MVT::i32);
9386 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9387 } else {
9388 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009389// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009390 }
9391
Owen Andersone50ed302009-08-10 22:56:29 +00009392 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009393 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009394 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009395 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009396 Op.getOperand(1), ShAmt);
9397 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009398 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009399}
Evan Cheng72261582005-12-20 06:22:03 +00009400
Dan Gohmand858e902010-04-17 15:26:15 +00009401SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9402 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009403 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9404 MFI->setReturnAddressIsTaken(true);
9405
Bill Wendling64e87322009-01-16 19:25:27 +00009406 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009407 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009408
9409 if (Depth > 0) {
9410 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9411 SDValue Offset =
9412 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009413 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009414 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009415 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009416 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00009417 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009418 }
9419
9420 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009421 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009422 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009423 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009424}
9425
Dan Gohmand858e902010-04-17 15:26:15 +00009426SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009427 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9428 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009429
Owen Andersone50ed302009-08-10 22:56:29 +00009430 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009431 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009432 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9433 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009434 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009435 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009436 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9437 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00009438 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009439 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009440}
9441
Dan Gohman475871a2008-07-27 21:46:04 +00009442SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009443 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009444 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009445}
9446
Dan Gohmand858e902010-04-17 15:26:15 +00009447SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009448 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009449 SDValue Chain = Op.getOperand(0);
9450 SDValue Offset = Op.getOperand(1);
9451 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009452 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009453
Dan Gohmand8816272010-08-11 18:14:00 +00009454 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9455 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9456 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009457 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009458
Dan Gohmand8816272010-08-11 18:14:00 +00009459 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9460 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009461 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009462 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9463 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009464 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009465 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009466
Dale Johannesene4d209d2009-02-03 20:21:25 +00009467 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009468 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009469 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009470}
9471
Duncan Sands4a544a72011-09-06 13:37:06 +00009472SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9473 SelectionDAG &DAG) const {
9474 return Op.getOperand(0);
9475}
9476
9477SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9478 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009479 SDValue Root = Op.getOperand(0);
9480 SDValue Trmp = Op.getOperand(1); // trampoline
9481 SDValue FPtr = Op.getOperand(2); // nested function
9482 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009483 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009484
Dan Gohman69de1932008-02-06 22:27:42 +00009485 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009486
9487 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009488 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009489
9490 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009491 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9492 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009493
Evan Cheng0e6a0522011-07-18 20:57:22 +00009494 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9495 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009496
9497 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9498
9499 // Load the pointer to the nested function into R11.
9500 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009501 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009502 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009503 Addr, MachinePointerInfo(TrmpAddr),
9504 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009505
Owen Anderson825b72b2009-08-11 20:47:22 +00009506 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9507 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009508 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9509 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009510 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009511
9512 // Load the 'nest' parameter value into R10.
9513 // R10 is specified in X86CallingConv.td
9514 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009515 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9516 DAG.getConstant(10, MVT::i64));
9517 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009518 Addr, MachinePointerInfo(TrmpAddr, 10),
9519 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009520
Owen Anderson825b72b2009-08-11 20:47:22 +00009521 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9522 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009523 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9524 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009525 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009526
9527 // Jump to the nested function.
9528 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009529 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9530 DAG.getConstant(20, MVT::i64));
9531 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009532 Addr, MachinePointerInfo(TrmpAddr, 20),
9533 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009534
9535 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009536 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9537 DAG.getConstant(22, MVT::i64));
9538 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009539 MachinePointerInfo(TrmpAddr, 22),
9540 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009541
Duncan Sands4a544a72011-09-06 13:37:06 +00009542 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009543 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009544 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009545 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009546 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009547 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009548
9549 switch (CC) {
9550 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009551 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009552 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009553 case CallingConv::X86_StdCall: {
9554 // Pass 'nest' parameter in ECX.
9555 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009556 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009557
9558 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009559 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009560 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009561
Chris Lattner58d74912008-03-12 17:45:29 +00009562 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009563 unsigned InRegCount = 0;
9564 unsigned Idx = 1;
9565
9566 for (FunctionType::param_iterator I = FTy->param_begin(),
9567 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009568 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009569 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009570 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009571
9572 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009573 report_fatal_error("Nest register in use - reduce number of inreg"
9574 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009575 }
9576 }
9577 break;
9578 }
9579 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009580 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009581 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009582 // Pass 'nest' parameter in EAX.
9583 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009584 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009585 break;
9586 }
9587
Dan Gohman475871a2008-07-27 21:46:04 +00009588 SDValue OutChains[4];
9589 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009590
Owen Anderson825b72b2009-08-11 20:47:22 +00009591 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9592 DAG.getConstant(10, MVT::i32));
9593 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009594
Chris Lattnera62fe662010-02-05 19:20:30 +00009595 // This is storing the opcode for MOV32ri.
9596 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009597 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009598 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009599 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009600 Trmp, MachinePointerInfo(TrmpAddr),
9601 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009602
Owen Anderson825b72b2009-08-11 20:47:22 +00009603 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9604 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009605 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9606 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009607 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009608
Chris Lattnera62fe662010-02-05 19:20:30 +00009609 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009610 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9611 DAG.getConstant(5, MVT::i32));
9612 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009613 MachinePointerInfo(TrmpAddr, 5),
9614 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009615
Owen Anderson825b72b2009-08-11 20:47:22 +00009616 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9617 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009618 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9619 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009620 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009621
Duncan Sands4a544a72011-09-06 13:37:06 +00009622 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009623 }
9624}
9625
Dan Gohmand858e902010-04-17 15:26:15 +00009626SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9627 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009628 /*
9629 The rounding mode is in bits 11:10 of FPSR, and has the following
9630 settings:
9631 00 Round to nearest
9632 01 Round to -inf
9633 10 Round to +inf
9634 11 Round to 0
9635
9636 FLT_ROUNDS, on the other hand, expects the following:
9637 -1 Undefined
9638 0 Round to 0
9639 1 Round to nearest
9640 2 Round to +inf
9641 3 Round to -inf
9642
9643 To perform the conversion, we do:
9644 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9645 */
9646
9647 MachineFunction &MF = DAG.getMachineFunction();
9648 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009649 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009650 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009651 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009652 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009653
9654 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009655 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009656 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009657
Michael J. Spencerec38de22010-10-10 22:04:20 +00009658
Chris Lattner2156b792010-09-22 01:11:26 +00009659 MachineMemOperand *MMO =
9660 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9661 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009662
Chris Lattner2156b792010-09-22 01:11:26 +00009663 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9664 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9665 DAG.getVTList(MVT::Other),
9666 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009667
9668 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009669 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00009670 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009671
9672 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009673 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009674 DAG.getNode(ISD::SRL, DL, MVT::i16,
9675 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009676 CWD, DAG.getConstant(0x800, MVT::i16)),
9677 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009678 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009679 DAG.getNode(ISD::SRL, DL, MVT::i16,
9680 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009681 CWD, DAG.getConstant(0x400, MVT::i16)),
9682 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009683
Dan Gohman475871a2008-07-27 21:46:04 +00009684 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009685 DAG.getNode(ISD::AND, DL, MVT::i16,
9686 DAG.getNode(ISD::ADD, DL, MVT::i16,
9687 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009688 DAG.getConstant(1, MVT::i16)),
9689 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009690
9691
Duncan Sands83ec4b62008-06-06 12:08:01 +00009692 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009693 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009694}
9695
Dan Gohmand858e902010-04-17 15:26:15 +00009696SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009697 EVT VT = Op.getValueType();
9698 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009699 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009700 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009701
9702 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009703 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009704 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009705 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009706 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009707 }
Evan Cheng18efe262007-12-14 02:13:44 +00009708
Evan Cheng152804e2007-12-14 08:30:15 +00009709 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009710 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009711 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009712
9713 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009714 SDValue Ops[] = {
9715 Op,
9716 DAG.getConstant(NumBits+NumBits-1, OpVT),
9717 DAG.getConstant(X86::COND_E, MVT::i8),
9718 Op.getValue(1)
9719 };
9720 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009721
9722 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009723 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009724
Owen Anderson825b72b2009-08-11 20:47:22 +00009725 if (VT == MVT::i8)
9726 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009727 return Op;
9728}
9729
Dan Gohmand858e902010-04-17 15:26:15 +00009730SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009731 EVT VT = Op.getValueType();
9732 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009733 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009734 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009735
9736 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009737 if (VT == MVT::i8) {
9738 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009739 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009740 }
Evan Cheng152804e2007-12-14 08:30:15 +00009741
9742 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009743 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009744 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009745
9746 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009747 SDValue Ops[] = {
9748 Op,
9749 DAG.getConstant(NumBits, OpVT),
9750 DAG.getConstant(X86::COND_E, MVT::i8),
9751 Op.getValue(1)
9752 };
9753 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009754
Owen Anderson825b72b2009-08-11 20:47:22 +00009755 if (VT == MVT::i8)
9756 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009757 return Op;
9758}
9759
Craig Topper13894fa2011-08-24 06:14:18 +00009760// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9761// ones, and then concatenate the result back.
9762static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009763 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009764
9765 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9766 "Unsupported value type for operation");
9767
9768 int NumElems = VT.getVectorNumElements();
9769 DebugLoc dl = Op.getDebugLoc();
9770 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9771 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9772
9773 // Extract the LHS vectors
9774 SDValue LHS = Op.getOperand(0);
9775 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9776 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9777
9778 // Extract the RHS vectors
9779 SDValue RHS = Op.getOperand(1);
9780 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9781 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9782
9783 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9784 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9785
9786 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9787 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9788 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9789}
9790
9791SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9792 assert(Op.getValueType().getSizeInBits() == 256 &&
9793 Op.getValueType().isInteger() &&
9794 "Only handle AVX 256-bit vector integer operation");
9795 return Lower256IntArith(Op, DAG);
9796}
9797
9798SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9799 assert(Op.getValueType().getSizeInBits() == 256 &&
9800 Op.getValueType().isInteger() &&
9801 "Only handle AVX 256-bit vector integer operation");
9802 return Lower256IntArith(Op, DAG);
9803}
9804
9805SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9806 EVT VT = Op.getValueType();
9807
9808 // Decompose 256-bit ops into smaller 128-bit ops.
9809 if (VT.getSizeInBits() == 256)
9810 return Lower256IntArith(Op, DAG);
9811
Owen Anderson825b72b2009-08-11 20:47:22 +00009812 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009813 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009814
Mon P Wangaf9b9522008-12-18 21:42:19 +00009815 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9816 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9817 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9818 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9819 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9820 //
9821 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9822 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9823 // return AloBlo + AloBhi + AhiBlo;
9824
9825 SDValue A = Op.getOperand(0);
9826 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009827
Dale Johannesene4d209d2009-02-03 20:21:25 +00009828 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009829 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9830 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009831 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009832 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9833 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009834 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009835 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009836 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009837 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009838 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009839 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009840 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009841 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009842 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009843 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009844 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9845 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009846 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009847 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9848 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009849 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9850 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009851 return Res;
9852}
9853
Nadav Rotem43012222011-05-11 08:12:09 +00009854SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
9855
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009856 EVT VT = Op.getValueType();
9857 DebugLoc dl = Op.getDebugLoc();
9858 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009859 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009860 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009861
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009862 if (!(Subtarget->hasSSE2() || Subtarget->hasAVX()))
9863 return SDValue();
9864
9865 // Decompose 256-bit shifts into smaller 128-bit shifts.
9866 if (VT.getSizeInBits() == 256) {
9867 int NumElems = VT.getVectorNumElements();
9868 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9869 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9870
9871 // Extract the two vectors
9872 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
9873 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
9874 DAG, dl);
9875
9876 // Recreate the shift amount vectors
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009877 SDValue Amt1, Amt2;
9878 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
9879 // Constant shift amount
9880 SmallVector<SDValue, 4> Amt1Csts;
9881 SmallVector<SDValue, 4> Amt2Csts;
9882 for (int i = 0; i < NumElems/2; ++i)
9883 Amt1Csts.push_back(Amt->getOperand(i));
9884 for (int i = NumElems/2; i < NumElems; ++i)
9885 Amt2Csts.push_back(Amt->getOperand(i));
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009886
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009887 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9888 &Amt1Csts[0], NumElems/2);
9889 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9890 &Amt2Csts[0], NumElems/2);
9891 } else {
9892 // Variable shift amount
9893 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
9894 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
9895 DAG, dl);
9896 }
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009897
9898 // Issue new vector shifts for the smaller types
9899 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
9900 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
9901
9902 // Concatenate the result back
9903 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
9904 }
Nate Begeman51409212010-07-28 00:21:48 +00009905
Nadav Rotem43012222011-05-11 08:12:09 +00009906 // Optimize shl/srl/sra with constant shift amount.
9907 if (isSplatVector(Amt.getNode())) {
9908 SDValue SclrAmt = Amt->getOperand(0);
9909 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
9910 uint64_t ShiftAmt = C->getZExtValue();
9911
9912 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
9913 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9914 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9915 R, DAG.getConstant(ShiftAmt, MVT::i32));
9916
9917 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
9918 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9919 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9920 R, DAG.getConstant(ShiftAmt, MVT::i32));
9921
9922 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
9923 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9924 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9925 R, DAG.getConstant(ShiftAmt, MVT::i32));
9926
9927 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
9928 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9929 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9930 R, DAG.getConstant(ShiftAmt, MVT::i32));
9931
9932 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
9933 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9934 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
9935 R, DAG.getConstant(ShiftAmt, MVT::i32));
9936
9937 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
9938 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9939 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
9940 R, DAG.getConstant(ShiftAmt, MVT::i32));
9941
9942 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
9943 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9944 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
9945 R, DAG.getConstant(ShiftAmt, MVT::i32));
9946
9947 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
9948 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9949 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
9950 R, DAG.getConstant(ShiftAmt, MVT::i32));
9951 }
9952 }
9953
9954 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00009955 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009956 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9957 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9958 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9959
9960 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009961
Nate Begeman51409212010-07-28 00:21:48 +00009962 std::vector<Constant*> CV(4, CI);
9963 Constant *C = ConstantVector::get(CV);
9964 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9965 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009966 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009967 false, false, 16);
9968
9969 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009970 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009971 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9972 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9973 }
Nadav Rotem43012222011-05-11 08:12:09 +00009974 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009975 // a = a << 5;
9976 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9977 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9978 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9979
9980 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
9981 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
9982
9983 std::vector<Constant*> CVM1(16, CM1);
9984 std::vector<Constant*> CVM2(16, CM2);
9985 Constant *C = ConstantVector::get(CVM1);
9986 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9987 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009988 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009989 false, false, 16);
9990
9991 // r = pblendv(r, psllw(r & (char16)15, 4), a);
9992 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9993 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9994 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9995 DAG.getConstant(4, MVT::i32));
Nadav Rotem8ffad562011-09-09 20:29:17 +00009996 R = DAG.getNode(X86ISD::BLENDV, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009997 // a += a
9998 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009999
Nate Begeman51409212010-07-28 00:21:48 +000010000 C = ConstantVector::get(CVM2);
10001 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10002 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010003 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010004 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010005
Nate Begeman51409212010-07-28 00:21:48 +000010006 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10007 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10008 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10009 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10010 DAG.getConstant(2, MVT::i32));
Nadav Rotem8ffad562011-09-09 20:29:17 +000010011 R = DAG.getNode(X86ISD::BLENDV, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010012 // a += a
10013 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010014
Nate Begeman51409212010-07-28 00:21:48 +000010015 // return pblendv(r, r+r, a);
Nadav Rotem8ffad562011-09-09 20:29:17 +000010016 R = DAG.getNode(X86ISD::BLENDV, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +000010017 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
10018 return R;
10019 }
10020 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010021}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010022
Dan Gohmand858e902010-04-17 15:26:15 +000010023SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010024 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10025 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010026 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10027 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010028 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010029 SDValue LHS = N->getOperand(0);
10030 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010031 unsigned BaseOp = 0;
10032 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010033 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010034 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010035 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010036 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010037 // A subtract of one will be selected as a INC. Note that INC doesn't
10038 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010039 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10040 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010041 BaseOp = X86ISD::INC;
10042 Cond = X86::COND_O;
10043 break;
10044 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010045 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010046 Cond = X86::COND_O;
10047 break;
10048 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010049 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010050 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010051 break;
10052 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010053 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10054 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010055 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10056 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010057 BaseOp = X86ISD::DEC;
10058 Cond = X86::COND_O;
10059 break;
10060 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010061 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010062 Cond = X86::COND_O;
10063 break;
10064 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010065 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010066 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010067 break;
10068 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010069 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010070 Cond = X86::COND_O;
10071 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010072 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10073 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10074 MVT::i32);
10075 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010076
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010077 SDValue SetCC =
10078 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10079 DAG.getConstant(X86::COND_O, MVT::i32),
10080 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010081
Dan Gohman6e5fda22011-07-22 18:45:15 +000010082 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010083 }
Bill Wendling74c37652008-12-09 22:08:41 +000010084 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010085
Bill Wendling61edeb52008-12-02 01:06:39 +000010086 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010087 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010088 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010089
Bill Wendling61edeb52008-12-02 01:06:39 +000010090 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010091 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10092 DAG.getConstant(Cond, MVT::i32),
10093 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010094
Dan Gohman6e5fda22011-07-22 18:45:15 +000010095 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010096}
10097
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010098SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10099 DebugLoc dl = Op.getDebugLoc();
10100 SDNode* Node = Op.getNode();
10101 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
10102 EVT VT = Node->getValueType(0);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010103 if (Subtarget->hasSSE2() && VT.isVector()) {
10104 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10105 ExtraVT.getScalarType().getSizeInBits();
10106 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10107
10108 unsigned SHLIntrinsicsID = 0;
10109 unsigned SRAIntrinsicsID = 0;
10110 switch (VT.getSimpleVT().SimpleTy) {
10111 default:
10112 return SDValue();
10113 case MVT::v2i64: {
10114 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
10115 SRAIntrinsicsID = 0;
10116 break;
10117 }
10118 case MVT::v4i32: {
10119 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10120 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10121 break;
10122 }
10123 case MVT::v8i16: {
10124 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10125 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10126 break;
10127 }
10128 }
10129
10130 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10131 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
10132 Node->getOperand(0), ShAmt);
10133
10134 // In case of 1 bit sext, no need to shr
10135 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
10136
10137 if (SRAIntrinsicsID) {
10138 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10139 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10140 Tmp1, ShAmt);
10141 }
10142 return Tmp1;
10143 }
10144
10145 return SDValue();
10146}
10147
10148
Eric Christopher9a9d2752010-07-22 02:48:34 +000010149SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10150 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010151
Eric Christopher77ed1352011-07-08 00:04:56 +000010152 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10153 // There isn't any reason to disable it if the target processor supports it.
10154 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010155 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010156 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010157 SDValue Ops[] = {
10158 DAG.getRegister(X86::ESP, MVT::i32), // Base
10159 DAG.getTargetConstant(1, MVT::i8), // Scale
10160 DAG.getRegister(0, MVT::i32), // Index
10161 DAG.getTargetConstant(0, MVT::i32), // Disp
10162 DAG.getRegister(0, MVT::i32), // Segment.
10163 Zero,
10164 Chain
10165 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010166 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010167 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10168 array_lengthof(Ops));
10169 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010170 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010171
Eric Christopher9a9d2752010-07-22 02:48:34 +000010172 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010173 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010174 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010175
Chris Lattner132929a2010-08-14 17:26:09 +000010176 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10177 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10178 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10179 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010180
Chris Lattner132929a2010-08-14 17:26:09 +000010181 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10182 if (!Op1 && !Op2 && !Op3 && Op4)
10183 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010184
Chris Lattner132929a2010-08-14 17:26:09 +000010185 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10186 if (Op1 && !Op2 && !Op3 && !Op4)
10187 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010188
10189 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010190 // (MFENCE)>;
10191 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010192}
10193
Eli Friedman14648462011-07-27 22:21:52 +000010194SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10195 SelectionDAG &DAG) const {
10196 DebugLoc dl = Op.getDebugLoc();
10197 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10198 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10199 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10200 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10201
10202 // The only fence that needs an instruction is a sequentially-consistent
10203 // cross-thread fence.
10204 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10205 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10206 // no-sse2). There isn't any reason to disable it if the target processor
10207 // supports it.
10208 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
10209 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10210
10211 SDValue Chain = Op.getOperand(0);
10212 SDValue Zero = DAG.getConstant(0, MVT::i32);
10213 SDValue Ops[] = {
10214 DAG.getRegister(X86::ESP, MVT::i32), // Base
10215 DAG.getTargetConstant(1, MVT::i8), // Scale
10216 DAG.getRegister(0, MVT::i32), // Index
10217 DAG.getTargetConstant(0, MVT::i32), // Disp
10218 DAG.getRegister(0, MVT::i32), // Segment.
10219 Zero,
10220 Chain
10221 };
10222 SDNode *Res =
10223 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10224 array_lengthof(Ops));
10225 return SDValue(Res, 0);
10226 }
10227
10228 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10229 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10230}
10231
10232
Dan Gohmand858e902010-04-17 15:26:15 +000010233SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010234 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010235 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010236 unsigned Reg = 0;
10237 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010238 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010239 default:
10240 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010241 case MVT::i8: Reg = X86::AL; size = 1; break;
10242 case MVT::i16: Reg = X86::AX; size = 2; break;
10243 case MVT::i32: Reg = X86::EAX; size = 4; break;
10244 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010245 assert(Subtarget->is64Bit() && "Node not type legal!");
10246 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010247 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010248 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010249 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010250 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010251 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010252 Op.getOperand(1),
10253 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010254 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010255 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010256 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010257 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10258 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10259 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010260 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010261 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010262 return cpOut;
10263}
10264
Duncan Sands1607f052008-12-01 11:39:25 +000010265SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010266 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010267 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010268 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010269 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010270 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010271 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010272 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10273 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010274 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010275 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10276 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010277 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010278 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010279 rdx.getValue(1)
10280 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010281 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010282}
10283
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010284SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010285 SelectionDAG &DAG) const {
10286 EVT SrcVT = Op.getOperand(0).getValueType();
10287 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +000010288 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
10289 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010290 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010291 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010292 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010293 // i64 <=> MMX conversions are Legal.
10294 if (SrcVT==MVT::i64 && DstVT.isVector())
10295 return Op;
10296 if (DstVT==MVT::i64 && SrcVT.isVector())
10297 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010298 // MMX <=> MMX conversions are Legal.
10299 if (SrcVT.isVector() && DstVT.isVector())
10300 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010301 // All other conversions need to be expanded.
10302 return SDValue();
10303}
Chris Lattner5b856542010-12-20 00:59:46 +000010304
Dan Gohmand858e902010-04-17 15:26:15 +000010305SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010306 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010307 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010308 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010309 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010310 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010311 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010312 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010313 Node->getOperand(0),
10314 Node->getOperand(1), negOp,
10315 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010316 cast<AtomicSDNode>(Node)->getAlignment(),
10317 cast<AtomicSDNode>(Node)->getOrdering(),
10318 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010319}
10320
Eli Friedman327236c2011-08-24 20:50:09 +000010321static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10322 SDNode *Node = Op.getNode();
10323 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010324 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010325
10326 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010327 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10328 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10329 // (The only way to get a 16-byte store is cmpxchg16b)
10330 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10331 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10332 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010333 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10334 cast<AtomicSDNode>(Node)->getMemoryVT(),
10335 Node->getOperand(0),
10336 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010337 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010338 cast<AtomicSDNode>(Node)->getOrdering(),
10339 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010340 return Swap.getValue(1);
10341 }
10342 // Other atomic stores have a simple pattern.
10343 return Op;
10344}
10345
Chris Lattner5b856542010-12-20 00:59:46 +000010346static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10347 EVT VT = Op.getNode()->getValueType(0);
10348
10349 // Let legalize expand this if it isn't a legal type yet.
10350 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10351 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010352
Chris Lattner5b856542010-12-20 00:59:46 +000010353 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010354
Chris Lattner5b856542010-12-20 00:59:46 +000010355 unsigned Opc;
10356 bool ExtraOp = false;
10357 switch (Op.getOpcode()) {
10358 default: assert(0 && "Invalid code");
10359 case ISD::ADDC: Opc = X86ISD::ADD; break;
10360 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10361 case ISD::SUBC: Opc = X86ISD::SUB; break;
10362 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10363 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010364
Chris Lattner5b856542010-12-20 00:59:46 +000010365 if (!ExtraOp)
10366 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10367 Op.getOperand(1));
10368 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10369 Op.getOperand(1), Op.getOperand(2));
10370}
10371
Evan Cheng0db9fe62006-04-25 20:13:52 +000010372/// LowerOperation - Provide custom lowering hooks for some operations.
10373///
Dan Gohmand858e902010-04-17 15:26:15 +000010374SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010375 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010376 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010377 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010378 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010379 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010380 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10381 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010382 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010383 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010384 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010385 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10386 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10387 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010388 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010389 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010390 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10391 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10392 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010393 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010394 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010395 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010396 case ISD::SHL_PARTS:
10397 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010398 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010399 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010400 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010401 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010402 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010403 case ISD::FABS: return LowerFABS(Op, DAG);
10404 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010405 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010406 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010407 case ISD::SETCC: return LowerSETCC(Op, DAG);
10408 case ISD::SELECT: return LowerSELECT(Op, DAG);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +000010409 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010410 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010411 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010412 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010413 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010414 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010415 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010416 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10417 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010418 case ISD::FRAME_TO_ARGS_OFFSET:
10419 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010420 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010421 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010422 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10423 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010424 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010425 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10426 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010427 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010428 case ISD::SRA:
10429 case ISD::SRL:
10430 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010431 case ISD::SADDO:
10432 case ISD::UADDO:
10433 case ISD::SSUBO:
10434 case ISD::USUBO:
10435 case ISD::SMULO:
10436 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010437 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010438 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010439 case ISD::ADDC:
10440 case ISD::ADDE:
10441 case ISD::SUBC:
10442 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010443 case ISD::ADD: return LowerADD(Op, DAG);
10444 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010445 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010446}
10447
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010448static void ReplaceATOMIC_LOAD(SDNode *Node,
10449 SmallVectorImpl<SDValue> &Results,
10450 SelectionDAG &DAG) {
10451 DebugLoc dl = Node->getDebugLoc();
10452 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10453
10454 // Convert wide load -> cmpxchg8b/cmpxchg16b
10455 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10456 // (The only way to get a 16-byte load is cmpxchg16b)
10457 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010458 SDValue Zero = DAG.getConstant(0, VT);
10459 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010460 Node->getOperand(0),
10461 Node->getOperand(1), Zero, Zero,
10462 cast<AtomicSDNode>(Node)->getMemOperand(),
10463 cast<AtomicSDNode>(Node)->getOrdering(),
10464 cast<AtomicSDNode>(Node)->getSynchScope());
10465 Results.push_back(Swap.getValue(0));
10466 Results.push_back(Swap.getValue(1));
10467}
10468
Duncan Sands1607f052008-12-01 11:39:25 +000010469void X86TargetLowering::
10470ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010471 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010472 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010473 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +000010474 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010475
10476 SDValue Chain = Node->getOperand(0);
10477 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010478 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010479 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010480 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010481 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010482 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010483 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010484 SDValue Result =
10485 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10486 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010487 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010488 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010489 Results.push_back(Result.getValue(2));
10490}
10491
Duncan Sands126d9072008-07-04 11:47:58 +000010492/// ReplaceNodeResults - Replace a node with an illegal result type
10493/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010494void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10495 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010496 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010497 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010498 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010499 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010500 assert(false && "Do not know how to custom type legalize this operation!");
10501 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010502 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010503 case ISD::ADDC:
10504 case ISD::ADDE:
10505 case ISD::SUBC:
10506 case ISD::SUBE:
10507 // We don't want to expand or promote these.
10508 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010509 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010510 std::pair<SDValue,SDValue> Vals =
10511 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010512 SDValue FIST = Vals.first, StackSlot = Vals.second;
10513 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010514 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010515 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010516 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
10517 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010518 }
10519 return;
10520 }
10521 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010522 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010523 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010524 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010525 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010526 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010527 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010528 eax.getValue(2));
10529 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10530 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010531 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010532 Results.push_back(edx.getValue(1));
10533 return;
10534 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010535 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010536 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010537 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010538 bool Regs64bit = T == MVT::i128;
10539 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010540 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010541 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10542 DAG.getConstant(0, HalfT));
10543 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10544 DAG.getConstant(1, HalfT));
10545 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10546 Regs64bit ? X86::RAX : X86::EAX,
10547 cpInL, SDValue());
10548 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10549 Regs64bit ? X86::RDX : X86::EDX,
10550 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010551 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010552 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10553 DAG.getConstant(0, HalfT));
10554 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10555 DAG.getConstant(1, HalfT));
10556 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10557 Regs64bit ? X86::RBX : X86::EBX,
10558 swapInL, cpInH.getValue(1));
10559 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10560 Regs64bit ? X86::RCX : X86::ECX,
10561 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010562 SDValue Ops[] = { swapInH.getValue(0),
10563 N->getOperand(1),
10564 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010565 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010566 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010567 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10568 X86ISD::LCMPXCHG8_DAG;
10569 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010570 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010571 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10572 Regs64bit ? X86::RAX : X86::EAX,
10573 HalfT, Result.getValue(1));
10574 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10575 Regs64bit ? X86::RDX : X86::EDX,
10576 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010577 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010578 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010579 Results.push_back(cpOutH.getValue(1));
10580 return;
10581 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010582 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010583 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10584 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010585 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010586 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10587 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010588 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010589 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10590 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010591 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010592 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10593 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010594 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010595 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10596 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010597 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010598 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10599 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010600 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010601 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10602 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010603 case ISD::ATOMIC_LOAD:
10604 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010605 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010606}
10607
Evan Cheng72261582005-12-20 06:22:03 +000010608const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10609 switch (Opcode) {
10610 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010611 case X86ISD::BSF: return "X86ISD::BSF";
10612 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010613 case X86ISD::SHLD: return "X86ISD::SHLD";
10614 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010615 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010616 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010617 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010618 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010619 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010620 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010621 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10622 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10623 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010624 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010625 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010626 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010627 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010628 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010629 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010630 case X86ISD::COMI: return "X86ISD::COMI";
10631 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010632 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010633 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010634 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10635 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010636 case X86ISD::CMOV: return "X86ISD::CMOV";
10637 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010638 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010639 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10640 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010641 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010642 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010643 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010644 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010645 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010646 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10647 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010648 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010649 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010650 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000010651 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
10652 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
10653 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nadav Rotem8ffad562011-09-09 20:29:17 +000010654 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Evan Cheng8ca29322006-11-10 21:43:37 +000010655 case X86ISD::FMAX: return "X86ISD::FMAX";
10656 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010657 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10658 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010659 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010660 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010661 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010662 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010663 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010664 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10665 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010666 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10667 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10668 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10669 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10670 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10671 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010672 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10673 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010674 case X86ISD::VSHL: return "X86ISD::VSHL";
10675 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010676 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10677 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10678 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10679 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10680 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10681 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10682 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10683 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10684 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10685 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010686 case X86ISD::ADD: return "X86ISD::ADD";
10687 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010688 case X86ISD::ADC: return "X86ISD::ADC";
10689 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010690 case X86ISD::SMUL: return "X86ISD::SMUL";
10691 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010692 case X86ISD::INC: return "X86ISD::INC";
10693 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010694 case X86ISD::OR: return "X86ISD::OR";
10695 case X86ISD::XOR: return "X86ISD::XOR";
10696 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +000010697 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010698 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010699 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010700 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10701 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10702 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10703 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10704 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10705 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10706 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10707 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10708 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010709 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010710 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010711 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010712 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10713 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010714 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10715 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10716 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10717 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10718 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10719 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10720 case X86ISD::MOVSS: return "X86ISD::MOVSS";
10721 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
10722 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000010723 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010724 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
10725 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
10726 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
10727 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
10728 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
10729 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
10730 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
10731 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
10732 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
10733 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010734 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000010735 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
10736 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
10737 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
10738 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000010739 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010740 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010741 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010742 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010743 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010744 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010745 }
10746}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010747
Chris Lattnerc9addb72007-03-30 23:15:24 +000010748// isLegalAddressingMode - Return true if the addressing mode represented
10749// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010750bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010751 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010752 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010753 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010754 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010755
Chris Lattnerc9addb72007-03-30 23:15:24 +000010756 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010757 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010758 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010759
Chris Lattnerc9addb72007-03-30 23:15:24 +000010760 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010761 unsigned GVFlags =
10762 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010763
Chris Lattnerdfed4132009-07-10 07:38:24 +000010764 // If a reference to this global requires an extra load, we can't fold it.
10765 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010766 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010767
Chris Lattnerdfed4132009-07-10 07:38:24 +000010768 // If BaseGV requires a register for the PIC base, we cannot also have a
10769 // BaseReg specified.
10770 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000010771 return false;
Evan Cheng52787842007-08-01 23:46:47 +000010772
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010773 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000010774 if ((M != CodeModel::Small || R != Reloc::Static) &&
10775 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010776 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000010777 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010778
Chris Lattnerc9addb72007-03-30 23:15:24 +000010779 switch (AM.Scale) {
10780 case 0:
10781 case 1:
10782 case 2:
10783 case 4:
10784 case 8:
10785 // These scales always work.
10786 break;
10787 case 3:
10788 case 5:
10789 case 9:
10790 // These scales are formed with basereg+scalereg. Only accept if there is
10791 // no basereg yet.
10792 if (AM.HasBaseReg)
10793 return false;
10794 break;
10795 default: // Other stuff never works.
10796 return false;
10797 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010798
Chris Lattnerc9addb72007-03-30 23:15:24 +000010799 return true;
10800}
10801
10802
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010803bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010804 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000010805 return false;
Evan Chenge127a732007-10-29 07:57:50 +000010806 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
10807 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010808 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000010809 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010810 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000010811}
10812
Owen Andersone50ed302009-08-10 22:56:29 +000010813bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010814 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010815 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010816 unsigned NumBits1 = VT1.getSizeInBits();
10817 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010818 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010819 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010820 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010821}
Evan Cheng2bd122c2007-10-26 01:56:11 +000010822
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010823bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010824 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010825 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010826}
10827
Owen Andersone50ed302009-08-10 22:56:29 +000010828bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010829 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000010830 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010831}
10832
Owen Andersone50ed302009-08-10 22:56:29 +000010833bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000010834 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000010835 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000010836}
10837
Evan Cheng60c07e12006-07-05 22:17:51 +000010838/// isShuffleMaskLegal - Targets can use this to indicate that they only
10839/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
10840/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
10841/// are assumed to be legal.
10842bool
Eric Christopherfd179292009-08-27 18:07:15 +000010843X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000010844 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000010845 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000010846 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +000010847 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +000010848
Nate Begemana09008b2009-10-19 02:17:23 +000010849 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000010850 return (VT.getVectorNumElements() == 2 ||
10851 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
10852 isMOVLMask(M, VT) ||
10853 isSHUFPMask(M, VT) ||
10854 isPSHUFDMask(M, VT) ||
10855 isPSHUFHWMask(M, VT) ||
10856 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +000010857 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000010858 isUNPCKLMask(M, VT) ||
10859 isUNPCKHMask(M, VT) ||
10860 isUNPCKL_v_undef_Mask(M, VT) ||
10861 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010862}
10863
Dan Gohman7d8143f2008-04-09 20:09:42 +000010864bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000010865X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000010866 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000010867 unsigned NumElts = VT.getVectorNumElements();
10868 // FIXME: This collection of masks seems suspect.
10869 if (NumElts == 2)
10870 return true;
10871 if (NumElts == 4 && VT.getSizeInBits() == 128) {
10872 return (isMOVLMask(Mask, VT) ||
10873 isCommutedMOVLMask(Mask, VT, true) ||
10874 isSHUFPMask(Mask, VT) ||
10875 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010876 }
10877 return false;
10878}
10879
10880//===----------------------------------------------------------------------===//
10881// X86 Scheduler Hooks
10882//===----------------------------------------------------------------------===//
10883
Mon P Wang63307c32008-05-05 19:05:59 +000010884// private utility function
10885MachineBasicBlock *
10886X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
10887 MachineBasicBlock *MBB,
10888 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010889 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010890 unsigned LoadOpc,
10891 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010892 unsigned notOpc,
10893 unsigned EAXreg,
10894 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010895 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010896 // For the atomic bitwise operator, we generate
10897 // thisMBB:
10898 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010899 // ld t1 = [bitinstr.addr]
10900 // op t2 = t1, [bitinstr.val]
10901 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010902 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10903 // bz newMBB
10904 // fallthrough -->nextMBB
10905 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10906 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010907 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010908 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010909
Mon P Wang63307c32008-05-05 19:05:59 +000010910 /// First build the CFG
10911 MachineFunction *F = MBB->getParent();
10912 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010913 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10914 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10915 F->insert(MBBIter, newMBB);
10916 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010917
Dan Gohman14152b42010-07-06 20:24:04 +000010918 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10919 nextMBB->splice(nextMBB->begin(), thisMBB,
10920 llvm::next(MachineBasicBlock::iterator(bInstr)),
10921 thisMBB->end());
10922 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010923
Mon P Wang63307c32008-05-05 19:05:59 +000010924 // Update thisMBB to fall through to newMBB
10925 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010926
Mon P Wang63307c32008-05-05 19:05:59 +000010927 // newMBB jumps to itself and fall through to nextMBB
10928 newMBB->addSuccessor(nextMBB);
10929 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010930
Mon P Wang63307c32008-05-05 19:05:59 +000010931 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010932 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010933 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000010934 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010935 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010936 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010937 int numArgs = bInstr->getNumOperands() - 1;
10938 for (int i=0; i < numArgs; ++i)
10939 argOpers[i] = &bInstr->getOperand(i+1);
10940
10941 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010942 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010943 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010944
Dale Johannesen140be2d2008-08-19 18:47:28 +000010945 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010946 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010947 for (int i=0; i <= lastAddrIndx; ++i)
10948 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010949
Dale Johannesen140be2d2008-08-19 18:47:28 +000010950 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010951 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010952 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010953 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010954 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010955 tt = t1;
10956
Dale Johannesen140be2d2008-08-19 18:47:28 +000010957 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000010958 assert((argOpers[valArgIndx]->isReg() ||
10959 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010960 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000010961 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010962 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010963 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010964 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010965 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000010966 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010967
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010968 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000010969 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010970
Dale Johannesene4d209d2009-02-03 20:21:25 +000010971 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000010972 for (int i=0; i <= lastAddrIndx; ++i)
10973 (*MIB).addOperand(*argOpers[i]);
10974 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000010975 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010976 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10977 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000010978
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010979 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000010980 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010981
Mon P Wang63307c32008-05-05 19:05:59 +000010982 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010983 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010984
Dan Gohman14152b42010-07-06 20:24:04 +000010985 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010986 return nextMBB;
10987}
10988
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000010989// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000010990MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010991X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
10992 MachineBasicBlock *MBB,
10993 unsigned regOpcL,
10994 unsigned regOpcH,
10995 unsigned immOpcL,
10996 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010997 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010998 // For the atomic bitwise operator, we generate
10999 // thisMBB (instructions are in pairs, except cmpxchg8b)
11000 // ld t1,t2 = [bitinstr.addr]
11001 // newMBB:
11002 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11003 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011004 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011005 // mov ECX, EBX <- t5, t6
11006 // mov EAX, EDX <- t1, t2
11007 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11008 // mov t3, t4 <- EAX, EDX
11009 // bz newMBB
11010 // result in out1, out2
11011 // fallthrough -->nextMBB
11012
11013 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11014 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011015 const unsigned NotOpc = X86::NOT32r;
11016 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11017 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11018 MachineFunction::iterator MBBIter = MBB;
11019 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011020
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011021 /// First build the CFG
11022 MachineFunction *F = MBB->getParent();
11023 MachineBasicBlock *thisMBB = MBB;
11024 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11025 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11026 F->insert(MBBIter, newMBB);
11027 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011028
Dan Gohman14152b42010-07-06 20:24:04 +000011029 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11030 nextMBB->splice(nextMBB->begin(), thisMBB,
11031 llvm::next(MachineBasicBlock::iterator(bInstr)),
11032 thisMBB->end());
11033 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011034
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011035 // Update thisMBB to fall through to newMBB
11036 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011037
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011038 // newMBB jumps to itself and fall through to nextMBB
11039 newMBB->addSuccessor(nextMBB);
11040 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011041
Dale Johannesene4d209d2009-02-03 20:21:25 +000011042 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011043 // Insert instructions into newMBB based on incoming instruction
11044 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011045 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011046 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011047 MachineOperand& dest1Oper = bInstr->getOperand(0);
11048 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011049 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11050 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011051 argOpers[i] = &bInstr->getOperand(i+2);
11052
Dan Gohman71ea4e52010-05-14 21:01:44 +000011053 // We use some of the operands multiple times, so conservatively just
11054 // clear any kill flags that might be present.
11055 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11056 argOpers[i]->setIsKill(false);
11057 }
11058
Evan Chengad5b52f2010-01-08 19:14:57 +000011059 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011060 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011061
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011062 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011063 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011064 for (int i=0; i <= lastAddrIndx; ++i)
11065 (*MIB).addOperand(*argOpers[i]);
11066 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011067 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011068 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011069 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011070 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011071 MachineOperand newOp3 = *(argOpers[3]);
11072 if (newOp3.isImm())
11073 newOp3.setImm(newOp3.getImm()+4);
11074 else
11075 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011076 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011077 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011078
11079 // t3/4 are defined later, at the bottom of the loop
11080 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11081 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011082 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011083 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011084 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011085 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11086
Evan Cheng306b4ca2010-01-08 23:41:50 +000011087 // The subsequent operations should be using the destination registers of
11088 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011089 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011090 t1 = F->getRegInfo().createVirtualRegister(RC);
11091 t2 = F->getRegInfo().createVirtualRegister(RC);
11092 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11093 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011094 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011095 t1 = dest1Oper.getReg();
11096 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011097 }
11098
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011099 int valArgIndx = lastAddrIndx + 1;
11100 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011101 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011102 "invalid operand");
11103 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11104 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011105 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011106 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011107 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011108 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011109 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011110 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011111 (*MIB).addOperand(*argOpers[valArgIndx]);
11112 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011113 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011114 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011115 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011116 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011117 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011118 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011119 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011120 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011121 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011122 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011123
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011124 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011125 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011126 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011127 MIB.addReg(t2);
11128
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011129 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011130 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011131 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011132 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011133
Dale Johannesene4d209d2009-02-03 20:21:25 +000011134 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011135 for (int i=0; i <= lastAddrIndx; ++i)
11136 (*MIB).addOperand(*argOpers[i]);
11137
11138 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011139 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11140 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011141
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011142 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011143 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011144 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011145 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011146
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011147 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011148 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011149
Dan Gohman14152b42010-07-06 20:24:04 +000011150 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011151 return nextMBB;
11152}
11153
11154// private utility function
11155MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011156X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11157 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011158 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011159 // For the atomic min/max operator, we generate
11160 // thisMBB:
11161 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011162 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011163 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011164 // cmp t1, t2
11165 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011166 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011167 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11168 // bz newMBB
11169 // fallthrough -->nextMBB
11170 //
11171 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11172 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011173 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011174 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011175
Mon P Wang63307c32008-05-05 19:05:59 +000011176 /// First build the CFG
11177 MachineFunction *F = MBB->getParent();
11178 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011179 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11180 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11181 F->insert(MBBIter, newMBB);
11182 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011183
Dan Gohman14152b42010-07-06 20:24:04 +000011184 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11185 nextMBB->splice(nextMBB->begin(), thisMBB,
11186 llvm::next(MachineBasicBlock::iterator(mInstr)),
11187 thisMBB->end());
11188 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011189
Mon P Wang63307c32008-05-05 19:05:59 +000011190 // Update thisMBB to fall through to newMBB
11191 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011192
Mon P Wang63307c32008-05-05 19:05:59 +000011193 // newMBB jumps to newMBB and fall through to nextMBB
11194 newMBB->addSuccessor(nextMBB);
11195 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011196
Dale Johannesene4d209d2009-02-03 20:21:25 +000011197 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011198 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011199 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011200 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011201 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011202 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011203 int numArgs = mInstr->getNumOperands() - 1;
11204 for (int i=0; i < numArgs; ++i)
11205 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011206
Mon P Wang63307c32008-05-05 19:05:59 +000011207 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011208 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011209 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011210
Mon P Wangab3e7472008-05-05 22:56:23 +000011211 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011212 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011213 for (int i=0; i <= lastAddrIndx; ++i)
11214 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011215
Mon P Wang63307c32008-05-05 19:05:59 +000011216 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011217 assert((argOpers[valArgIndx]->isReg() ||
11218 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011219 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011220
11221 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011222 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011223 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011224 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011225 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011226 (*MIB).addOperand(*argOpers[valArgIndx]);
11227
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011228 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011229 MIB.addReg(t1);
11230
Dale Johannesene4d209d2009-02-03 20:21:25 +000011231 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011232 MIB.addReg(t1);
11233 MIB.addReg(t2);
11234
11235 // Generate movc
11236 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011237 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011238 MIB.addReg(t2);
11239 MIB.addReg(t1);
11240
11241 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011242 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011243 for (int i=0; i <= lastAddrIndx; ++i)
11244 (*MIB).addOperand(*argOpers[i]);
11245 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011246 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011247 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11248 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011249
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011250 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011251 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011252
Mon P Wang63307c32008-05-05 19:05:59 +000011253 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011254 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011255
Dan Gohman14152b42010-07-06 20:24:04 +000011256 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011257 return nextMBB;
11258}
11259
Eric Christopherf83a5de2009-08-27 18:08:16 +000011260// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011261// or XMM0_V32I8 in AVX all of this code can be replaced with that
11262// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011263MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011264X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011265 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011266 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11267 "Target must have SSE4.2 or AVX features enabled");
11268
Eric Christopherb120ab42009-08-18 22:50:32 +000011269 DebugLoc dl = MI->getDebugLoc();
11270 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011271 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011272 if (!Subtarget->hasAVX()) {
11273 if (memArg)
11274 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11275 else
11276 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11277 } else {
11278 if (memArg)
11279 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11280 else
11281 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11282 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011283
Eric Christopher41c902f2010-11-30 08:20:21 +000011284 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011285 for (unsigned i = 0; i < numArgs; ++i) {
11286 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011287 if (!(Op.isReg() && Op.isImplicit()))
11288 MIB.addOperand(Op);
11289 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011290 BuildMI(*BB, MI, dl,
11291 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11292 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011293 .addReg(X86::XMM0);
11294
Dan Gohman14152b42010-07-06 20:24:04 +000011295 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011296 return BB;
11297}
11298
11299MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011300X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011301 DebugLoc dl = MI->getDebugLoc();
11302 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011303
Eric Christopher228232b2010-11-30 07:20:12 +000011304 // Address into RAX/EAX, other two args into ECX, EDX.
11305 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11306 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11307 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11308 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011309 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011310
Eric Christopher228232b2010-11-30 07:20:12 +000011311 unsigned ValOps = X86::AddrNumOperands;
11312 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11313 .addReg(MI->getOperand(ValOps).getReg());
11314 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11315 .addReg(MI->getOperand(ValOps+1).getReg());
11316
11317 // The instruction doesn't actually take any operands though.
11318 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011319
Eric Christopher228232b2010-11-30 07:20:12 +000011320 MI->eraseFromParent(); // The pseudo is gone now.
11321 return BB;
11322}
11323
11324MachineBasicBlock *
11325X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011326 DebugLoc dl = MI->getDebugLoc();
11327 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011328
Eric Christopher228232b2010-11-30 07:20:12 +000011329 // First arg in ECX, the second in EAX.
11330 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11331 .addReg(MI->getOperand(0).getReg());
11332 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11333 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011334
Eric Christopher228232b2010-11-30 07:20:12 +000011335 // The instruction doesn't actually take any operands though.
11336 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011337
Eric Christopher228232b2010-11-30 07:20:12 +000011338 MI->eraseFromParent(); // The pseudo is gone now.
11339 return BB;
11340}
11341
11342MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011343X86TargetLowering::EmitVAARG64WithCustomInserter(
11344 MachineInstr *MI,
11345 MachineBasicBlock *MBB) const {
11346 // Emit va_arg instruction on X86-64.
11347
11348 // Operands to this pseudo-instruction:
11349 // 0 ) Output : destination address (reg)
11350 // 1-5) Input : va_list address (addr, i64mem)
11351 // 6 ) ArgSize : Size (in bytes) of vararg type
11352 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11353 // 8 ) Align : Alignment of type
11354 // 9 ) EFLAGS (implicit-def)
11355
11356 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11357 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11358
11359 unsigned DestReg = MI->getOperand(0).getReg();
11360 MachineOperand &Base = MI->getOperand(1);
11361 MachineOperand &Scale = MI->getOperand(2);
11362 MachineOperand &Index = MI->getOperand(3);
11363 MachineOperand &Disp = MI->getOperand(4);
11364 MachineOperand &Segment = MI->getOperand(5);
11365 unsigned ArgSize = MI->getOperand(6).getImm();
11366 unsigned ArgMode = MI->getOperand(7).getImm();
11367 unsigned Align = MI->getOperand(8).getImm();
11368
11369 // Memory Reference
11370 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11371 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11372 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11373
11374 // Machine Information
11375 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11376 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11377 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11378 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11379 DebugLoc DL = MI->getDebugLoc();
11380
11381 // struct va_list {
11382 // i32 gp_offset
11383 // i32 fp_offset
11384 // i64 overflow_area (address)
11385 // i64 reg_save_area (address)
11386 // }
11387 // sizeof(va_list) = 24
11388 // alignment(va_list) = 8
11389
11390 unsigned TotalNumIntRegs = 6;
11391 unsigned TotalNumXMMRegs = 8;
11392 bool UseGPOffset = (ArgMode == 1);
11393 bool UseFPOffset = (ArgMode == 2);
11394 unsigned MaxOffset = TotalNumIntRegs * 8 +
11395 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11396
11397 /* Align ArgSize to a multiple of 8 */
11398 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11399 bool NeedsAlign = (Align > 8);
11400
11401 MachineBasicBlock *thisMBB = MBB;
11402 MachineBasicBlock *overflowMBB;
11403 MachineBasicBlock *offsetMBB;
11404 MachineBasicBlock *endMBB;
11405
11406 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11407 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11408 unsigned OffsetReg = 0;
11409
11410 if (!UseGPOffset && !UseFPOffset) {
11411 // If we only pull from the overflow region, we don't create a branch.
11412 // We don't need to alter control flow.
11413 OffsetDestReg = 0; // unused
11414 OverflowDestReg = DestReg;
11415
11416 offsetMBB = NULL;
11417 overflowMBB = thisMBB;
11418 endMBB = thisMBB;
11419 } else {
11420 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11421 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11422 // If not, pull from overflow_area. (branch to overflowMBB)
11423 //
11424 // thisMBB
11425 // | .
11426 // | .
11427 // offsetMBB overflowMBB
11428 // | .
11429 // | .
11430 // endMBB
11431
11432 // Registers for the PHI in endMBB
11433 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11434 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11435
11436 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11437 MachineFunction *MF = MBB->getParent();
11438 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11439 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11440 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11441
11442 MachineFunction::iterator MBBIter = MBB;
11443 ++MBBIter;
11444
11445 // Insert the new basic blocks
11446 MF->insert(MBBIter, offsetMBB);
11447 MF->insert(MBBIter, overflowMBB);
11448 MF->insert(MBBIter, endMBB);
11449
11450 // Transfer the remainder of MBB and its successor edges to endMBB.
11451 endMBB->splice(endMBB->begin(), thisMBB,
11452 llvm::next(MachineBasicBlock::iterator(MI)),
11453 thisMBB->end());
11454 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11455
11456 // Make offsetMBB and overflowMBB successors of thisMBB
11457 thisMBB->addSuccessor(offsetMBB);
11458 thisMBB->addSuccessor(overflowMBB);
11459
11460 // endMBB is a successor of both offsetMBB and overflowMBB
11461 offsetMBB->addSuccessor(endMBB);
11462 overflowMBB->addSuccessor(endMBB);
11463
11464 // Load the offset value into a register
11465 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11466 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11467 .addOperand(Base)
11468 .addOperand(Scale)
11469 .addOperand(Index)
11470 .addDisp(Disp, UseFPOffset ? 4 : 0)
11471 .addOperand(Segment)
11472 .setMemRefs(MMOBegin, MMOEnd);
11473
11474 // Check if there is enough room left to pull this argument.
11475 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11476 .addReg(OffsetReg)
11477 .addImm(MaxOffset + 8 - ArgSizeA8);
11478
11479 // Branch to "overflowMBB" if offset >= max
11480 // Fall through to "offsetMBB" otherwise
11481 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11482 .addMBB(overflowMBB);
11483 }
11484
11485 // In offsetMBB, emit code to use the reg_save_area.
11486 if (offsetMBB) {
11487 assert(OffsetReg != 0);
11488
11489 // Read the reg_save_area address.
11490 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11491 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11492 .addOperand(Base)
11493 .addOperand(Scale)
11494 .addOperand(Index)
11495 .addDisp(Disp, 16)
11496 .addOperand(Segment)
11497 .setMemRefs(MMOBegin, MMOEnd);
11498
11499 // Zero-extend the offset
11500 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11501 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11502 .addImm(0)
11503 .addReg(OffsetReg)
11504 .addImm(X86::sub_32bit);
11505
11506 // Add the offset to the reg_save_area to get the final address.
11507 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11508 .addReg(OffsetReg64)
11509 .addReg(RegSaveReg);
11510
11511 // Compute the offset for the next argument
11512 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11513 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11514 .addReg(OffsetReg)
11515 .addImm(UseFPOffset ? 16 : 8);
11516
11517 // Store it back into the va_list.
11518 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11519 .addOperand(Base)
11520 .addOperand(Scale)
11521 .addOperand(Index)
11522 .addDisp(Disp, UseFPOffset ? 4 : 0)
11523 .addOperand(Segment)
11524 .addReg(NextOffsetReg)
11525 .setMemRefs(MMOBegin, MMOEnd);
11526
11527 // Jump to endMBB
11528 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11529 .addMBB(endMBB);
11530 }
11531
11532 //
11533 // Emit code to use overflow area
11534 //
11535
11536 // Load the overflow_area address into a register.
11537 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11538 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11539 .addOperand(Base)
11540 .addOperand(Scale)
11541 .addOperand(Index)
11542 .addDisp(Disp, 8)
11543 .addOperand(Segment)
11544 .setMemRefs(MMOBegin, MMOEnd);
11545
11546 // If we need to align it, do so. Otherwise, just copy the address
11547 // to OverflowDestReg.
11548 if (NeedsAlign) {
11549 // Align the overflow address
11550 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11551 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11552
11553 // aligned_addr = (addr + (align-1)) & ~(align-1)
11554 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11555 .addReg(OverflowAddrReg)
11556 .addImm(Align-1);
11557
11558 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11559 .addReg(TmpReg)
11560 .addImm(~(uint64_t)(Align-1));
11561 } else {
11562 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11563 .addReg(OverflowAddrReg);
11564 }
11565
11566 // Compute the next overflow address after this argument.
11567 // (the overflow address should be kept 8-byte aligned)
11568 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11569 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11570 .addReg(OverflowDestReg)
11571 .addImm(ArgSizeA8);
11572
11573 // Store the new overflow address.
11574 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11575 .addOperand(Base)
11576 .addOperand(Scale)
11577 .addOperand(Index)
11578 .addDisp(Disp, 8)
11579 .addOperand(Segment)
11580 .addReg(NextAddrReg)
11581 .setMemRefs(MMOBegin, MMOEnd);
11582
11583 // If we branched, emit the PHI to the front of endMBB.
11584 if (offsetMBB) {
11585 BuildMI(*endMBB, endMBB->begin(), DL,
11586 TII->get(X86::PHI), DestReg)
11587 .addReg(OffsetDestReg).addMBB(offsetMBB)
11588 .addReg(OverflowDestReg).addMBB(overflowMBB);
11589 }
11590
11591 // Erase the pseudo instruction
11592 MI->eraseFromParent();
11593
11594 return endMBB;
11595}
11596
11597MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011598X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11599 MachineInstr *MI,
11600 MachineBasicBlock *MBB) const {
11601 // Emit code to save XMM registers to the stack. The ABI says that the
11602 // number of registers to save is given in %al, so it's theoretically
11603 // possible to do an indirect jump trick to avoid saving all of them,
11604 // however this code takes a simpler approach and just executes all
11605 // of the stores if %al is non-zero. It's less code, and it's probably
11606 // easier on the hardware branch predictor, and stores aren't all that
11607 // expensive anyway.
11608
11609 // Create the new basic blocks. One block contains all the XMM stores,
11610 // and one block is the final destination regardless of whether any
11611 // stores were performed.
11612 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11613 MachineFunction *F = MBB->getParent();
11614 MachineFunction::iterator MBBIter = MBB;
11615 ++MBBIter;
11616 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11617 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11618 F->insert(MBBIter, XMMSaveMBB);
11619 F->insert(MBBIter, EndMBB);
11620
Dan Gohman14152b42010-07-06 20:24:04 +000011621 // Transfer the remainder of MBB and its successor edges to EndMBB.
11622 EndMBB->splice(EndMBB->begin(), MBB,
11623 llvm::next(MachineBasicBlock::iterator(MI)),
11624 MBB->end());
11625 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11626
Dan Gohmand6708ea2009-08-15 01:38:56 +000011627 // The original block will now fall through to the XMM save block.
11628 MBB->addSuccessor(XMMSaveMBB);
11629 // The XMMSaveMBB will fall through to the end block.
11630 XMMSaveMBB->addSuccessor(EndMBB);
11631
11632 // Now add the instructions.
11633 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11634 DebugLoc DL = MI->getDebugLoc();
11635
11636 unsigned CountReg = MI->getOperand(0).getReg();
11637 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11638 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11639
11640 if (!Subtarget->isTargetWin64()) {
11641 // If %al is 0, branch around the XMM save block.
11642 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011643 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011644 MBB->addSuccessor(EndMBB);
11645 }
11646
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011647 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011648 // In the XMM save block, save all the XMM argument registers.
11649 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11650 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011651 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011652 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011653 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011654 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011655 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011656 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011657 .addFrameIndex(RegSaveFrameIndex)
11658 .addImm(/*Scale=*/1)
11659 .addReg(/*IndexReg=*/0)
11660 .addImm(/*Disp=*/Offset)
11661 .addReg(/*Segment=*/0)
11662 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011663 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011664 }
11665
Dan Gohman14152b42010-07-06 20:24:04 +000011666 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011667
11668 return EndMBB;
11669}
Mon P Wang63307c32008-05-05 19:05:59 +000011670
Evan Cheng60c07e12006-07-05 22:17:51 +000011671MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011672X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011673 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011674 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11675 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011676
Chris Lattner52600972009-09-02 05:57:00 +000011677 // To "insert" a SELECT_CC instruction, we actually have to insert the
11678 // diamond control-flow pattern. The incoming instruction knows the
11679 // destination vreg to set, the condition code register to branch on, the
11680 // true/false values to select between, and a branch opcode to use.
11681 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11682 MachineFunction::iterator It = BB;
11683 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011684
Chris Lattner52600972009-09-02 05:57:00 +000011685 // thisMBB:
11686 // ...
11687 // TrueVal = ...
11688 // cmpTY ccX, r1, r2
11689 // bCC copy1MBB
11690 // fallthrough --> copy0MBB
11691 MachineBasicBlock *thisMBB = BB;
11692 MachineFunction *F = BB->getParent();
11693 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11694 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011695 F->insert(It, copy0MBB);
11696 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011697
Bill Wendling730c07e2010-06-25 20:48:10 +000011698 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11699 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011700 if (!MI->killsRegister(X86::EFLAGS)) {
11701 copy0MBB->addLiveIn(X86::EFLAGS);
11702 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011703 }
11704
Dan Gohman14152b42010-07-06 20:24:04 +000011705 // Transfer the remainder of BB and its successor edges to sinkMBB.
11706 sinkMBB->splice(sinkMBB->begin(), BB,
11707 llvm::next(MachineBasicBlock::iterator(MI)),
11708 BB->end());
11709 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11710
11711 // Add the true and fallthrough blocks as its successors.
11712 BB->addSuccessor(copy0MBB);
11713 BB->addSuccessor(sinkMBB);
11714
11715 // Create the conditional branch instruction.
11716 unsigned Opc =
11717 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11718 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11719
Chris Lattner52600972009-09-02 05:57:00 +000011720 // copy0MBB:
11721 // %FalseValue = ...
11722 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011723 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011724
Chris Lattner52600972009-09-02 05:57:00 +000011725 // sinkMBB:
11726 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11727 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011728 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11729 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011730 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11731 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11732
Dan Gohman14152b42010-07-06 20:24:04 +000011733 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011734 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011735}
11736
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011737MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011738X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11739 bool Is64Bit) const {
11740 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11741 DebugLoc DL = MI->getDebugLoc();
11742 MachineFunction *MF = BB->getParent();
11743 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11744
11745 assert(EnableSegmentedStacks);
11746
11747 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11748 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11749
11750 // BB:
11751 // ... [Till the alloca]
11752 // If stacklet is not large enough, jump to mallocMBB
11753 //
11754 // bumpMBB:
11755 // Allocate by subtracting from RSP
11756 // Jump to continueMBB
11757 //
11758 // mallocMBB:
11759 // Allocate by call to runtime
11760 //
11761 // continueMBB:
11762 // ...
11763 // [rest of original BB]
11764 //
11765
11766 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11767 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11768 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11769
11770 MachineRegisterInfo &MRI = MF->getRegInfo();
11771 const TargetRegisterClass *AddrRegClass =
11772 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
11773
11774 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11775 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11776 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
11777 sizeVReg = MI->getOperand(1).getReg(),
11778 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
11779
11780 MachineFunction::iterator MBBIter = BB;
11781 ++MBBIter;
11782
11783 MF->insert(MBBIter, bumpMBB);
11784 MF->insert(MBBIter, mallocMBB);
11785 MF->insert(MBBIter, continueMBB);
11786
11787 continueMBB->splice(continueMBB->begin(), BB, llvm::next
11788 (MachineBasicBlock::iterator(MI)), BB->end());
11789 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
11790
11791 // Add code to the main basic block to check if the stack limit has been hit,
11792 // and if so, jump to mallocMBB otherwise to bumpMBB.
11793 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
11794 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), tmpSPVReg)
11795 .addReg(tmpSPVReg).addReg(sizeVReg);
11796 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
11797 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
11798 .addReg(tmpSPVReg);
11799 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
11800
11801 // bumpMBB simply decreases the stack pointer, since we know the current
11802 // stacklet has enough space.
11803 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
11804 .addReg(tmpSPVReg);
11805 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
11806 .addReg(tmpSPVReg);
11807 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11808
11809 // Calls into a routine in libgcc to allocate more space from the heap.
11810 if (Is64Bit) {
11811 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
11812 .addReg(sizeVReg);
11813 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
11814 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
11815 } else {
11816 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
11817 .addImm(12);
11818 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
11819 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
11820 .addExternalSymbol("__morestack_allocate_stack_space");
11821 }
11822
11823 if (!Is64Bit)
11824 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
11825 .addImm(16);
11826
11827 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
11828 .addReg(Is64Bit ? X86::RAX : X86::EAX);
11829 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11830
11831 // Set up the CFG correctly.
11832 BB->addSuccessor(bumpMBB);
11833 BB->addSuccessor(mallocMBB);
11834 mallocMBB->addSuccessor(continueMBB);
11835 bumpMBB->addSuccessor(continueMBB);
11836
11837 // Take care of the PHI nodes.
11838 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
11839 MI->getOperand(0).getReg())
11840 .addReg(mallocPtrVReg).addMBB(mallocMBB)
11841 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
11842
11843 // Delete the original pseudo instruction.
11844 MI->eraseFromParent();
11845
11846 // And we're done.
11847 return continueMBB;
11848}
11849
11850MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011851X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011852 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011853 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11854 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011855
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011856 assert(!Subtarget->isTargetEnvMacho());
11857
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011858 // The lowering is pretty easy: we're just emitting the call to _alloca. The
11859 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011860
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011861 if (Subtarget->isTargetWin64()) {
11862 if (Subtarget->isTargetCygMing()) {
11863 // ___chkstk(Mingw64):
11864 // Clobbers R10, R11, RAX and EFLAGS.
11865 // Updates RSP.
11866 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11867 .addExternalSymbol("___chkstk")
11868 .addReg(X86::RAX, RegState::Implicit)
11869 .addReg(X86::RSP, RegState::Implicit)
11870 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
11871 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
11872 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11873 } else {
11874 // __chkstk(MSVCRT): does not update stack pointer.
11875 // Clobbers R10, R11 and EFLAGS.
11876 // FIXME: RAX(allocated size) might be reused and not killed.
11877 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11878 .addExternalSymbol("__chkstk")
11879 .addReg(X86::RAX, RegState::Implicit)
11880 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11881 // RAX has the offset to subtracted from RSP.
11882 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
11883 .addReg(X86::RSP)
11884 .addReg(X86::RAX);
11885 }
11886 } else {
11887 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011888 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
11889
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011890 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
11891 .addExternalSymbol(StackProbeSymbol)
11892 .addReg(X86::EAX, RegState::Implicit)
11893 .addReg(X86::ESP, RegState::Implicit)
11894 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
11895 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
11896 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11897 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011898
Dan Gohman14152b42010-07-06 20:24:04 +000011899 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011900 return BB;
11901}
Chris Lattner52600972009-09-02 05:57:00 +000011902
11903MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000011904X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
11905 MachineBasicBlock *BB) const {
11906 // This is pretty easy. We're taking the value that we received from
11907 // our load from the relocation, sticking it in either RDI (x86-64)
11908 // or EAX and doing an indirect call. The return value will then
11909 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000011910 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000011911 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000011912 DebugLoc DL = MI->getDebugLoc();
11913 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000011914
11915 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000011916 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011917
Eric Christopher30ef0e52010-06-03 04:07:48 +000011918 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000011919 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11920 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000011921 .addReg(X86::RIP)
11922 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011923 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011924 MI->getOperand(3).getTargetFlags())
11925 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000011926 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000011927 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000011928 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000011929 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11930 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000011931 .addReg(0)
11932 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011933 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000011934 MI->getOperand(3).getTargetFlags())
11935 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011936 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011937 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011938 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000011939 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11940 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000011941 .addReg(TII->getGlobalBaseReg(F))
11942 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011943 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011944 MI->getOperand(3).getTargetFlags())
11945 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011946 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011947 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011948 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011949
Dan Gohman14152b42010-07-06 20:24:04 +000011950 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000011951 return BB;
11952}
11953
11954MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000011955X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011956 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000011957 switch (MI->getOpcode()) {
11958 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000011959 case X86::TAILJMPd64:
11960 case X86::TAILJMPr64:
11961 case X86::TAILJMPm64:
11962 assert(!"TAILJMP64 would not be touched here.");
11963 case X86::TCRETURNdi64:
11964 case X86::TCRETURNri64:
11965 case X86::TCRETURNmi64:
11966 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
11967 // On AMD64, additional defs should be added before register allocation.
11968 if (!Subtarget->isTargetWin64()) {
11969 MI->addRegisterDefined(X86::RSI);
11970 MI->addRegisterDefined(X86::RDI);
11971 MI->addRegisterDefined(X86::XMM6);
11972 MI->addRegisterDefined(X86::XMM7);
11973 MI->addRegisterDefined(X86::XMM8);
11974 MI->addRegisterDefined(X86::XMM9);
11975 MI->addRegisterDefined(X86::XMM10);
11976 MI->addRegisterDefined(X86::XMM11);
11977 MI->addRegisterDefined(X86::XMM12);
11978 MI->addRegisterDefined(X86::XMM13);
11979 MI->addRegisterDefined(X86::XMM14);
11980 MI->addRegisterDefined(X86::XMM15);
11981 }
11982 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011983 case X86::WIN_ALLOCA:
11984 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011985 case X86::SEG_ALLOCA_32:
11986 return EmitLoweredSegAlloca(MI, BB, false);
11987 case X86::SEG_ALLOCA_64:
11988 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011989 case X86::TLSCall_32:
11990 case X86::TLSCall_64:
11991 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000011992 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000011993 case X86::CMOV_FR32:
11994 case X86::CMOV_FR64:
11995 case X86::CMOV_V4F32:
11996 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000011997 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000011998 case X86::CMOV_V8F32:
11999 case X86::CMOV_V4F64:
12000 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012001 case X86::CMOV_GR16:
12002 case X86::CMOV_GR32:
12003 case X86::CMOV_RFP32:
12004 case X86::CMOV_RFP64:
12005 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012006 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012007
Dale Johannesen849f2142007-07-03 00:53:03 +000012008 case X86::FP32_TO_INT16_IN_MEM:
12009 case X86::FP32_TO_INT32_IN_MEM:
12010 case X86::FP32_TO_INT64_IN_MEM:
12011 case X86::FP64_TO_INT16_IN_MEM:
12012 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012013 case X86::FP64_TO_INT64_IN_MEM:
12014 case X86::FP80_TO_INT16_IN_MEM:
12015 case X86::FP80_TO_INT32_IN_MEM:
12016 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012017 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12018 DebugLoc DL = MI->getDebugLoc();
12019
Evan Cheng60c07e12006-07-05 22:17:51 +000012020 // Change the floating point control register to use "round towards zero"
12021 // mode when truncating to an integer value.
12022 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012023 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012024 addFrameReference(BuildMI(*BB, MI, DL,
12025 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012026
12027 // Load the old value of the high byte of the control word...
12028 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012029 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012030 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012031 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012032
12033 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012034 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012035 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012036
12037 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012038 addFrameReference(BuildMI(*BB, MI, DL,
12039 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012040
12041 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012042 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012043 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012044
12045 // Get the X86 opcode to use.
12046 unsigned Opc;
12047 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012048 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012049 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12050 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12051 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12052 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12053 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12054 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012055 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12056 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12057 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012058 }
12059
12060 X86AddressMode AM;
12061 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012062 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012063 AM.BaseType = X86AddressMode::RegBase;
12064 AM.Base.Reg = Op.getReg();
12065 } else {
12066 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012067 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012068 }
12069 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012070 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012071 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012072 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012073 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012074 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012075 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012076 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012077 AM.GV = Op.getGlobal();
12078 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012079 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012080 }
Dan Gohman14152b42010-07-06 20:24:04 +000012081 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012082 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012083
12084 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012085 addFrameReference(BuildMI(*BB, MI, DL,
12086 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012087
Dan Gohman14152b42010-07-06 20:24:04 +000012088 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012089 return BB;
12090 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012091 // String/text processing lowering.
12092 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012093 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012094 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12095 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012096 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012097 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12098 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012099 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012100 return EmitPCMP(MI, BB, 5, false /* in mem */);
12101 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012102 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012103 return EmitPCMP(MI, BB, 5, true /* in mem */);
12104
Eric Christopher228232b2010-11-30 07:20:12 +000012105 // Thread synchronization.
12106 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012107 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012108 case X86::MWAIT:
12109 return EmitMwait(MI, BB);
12110
Eric Christopherb120ab42009-08-18 22:50:32 +000012111 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012112 case X86::ATOMAND32:
12113 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012114 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012115 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012116 X86::NOT32r, X86::EAX,
12117 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012118 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012119 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12120 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012121 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012122 X86::NOT32r, X86::EAX,
12123 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012124 case X86::ATOMXOR32:
12125 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012126 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012127 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012128 X86::NOT32r, X86::EAX,
12129 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012130 case X86::ATOMNAND32:
12131 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012132 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012133 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012134 X86::NOT32r, X86::EAX,
12135 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012136 case X86::ATOMMIN32:
12137 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12138 case X86::ATOMMAX32:
12139 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12140 case X86::ATOMUMIN32:
12141 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12142 case X86::ATOMUMAX32:
12143 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012144
12145 case X86::ATOMAND16:
12146 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12147 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012148 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012149 X86::NOT16r, X86::AX,
12150 X86::GR16RegisterClass);
12151 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012152 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012153 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012154 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012155 X86::NOT16r, X86::AX,
12156 X86::GR16RegisterClass);
12157 case X86::ATOMXOR16:
12158 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12159 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012160 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012161 X86::NOT16r, X86::AX,
12162 X86::GR16RegisterClass);
12163 case X86::ATOMNAND16:
12164 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12165 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012166 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012167 X86::NOT16r, X86::AX,
12168 X86::GR16RegisterClass, true);
12169 case X86::ATOMMIN16:
12170 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12171 case X86::ATOMMAX16:
12172 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12173 case X86::ATOMUMIN16:
12174 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12175 case X86::ATOMUMAX16:
12176 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12177
12178 case X86::ATOMAND8:
12179 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12180 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012181 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012182 X86::NOT8r, X86::AL,
12183 X86::GR8RegisterClass);
12184 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012185 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012186 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012187 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012188 X86::NOT8r, X86::AL,
12189 X86::GR8RegisterClass);
12190 case X86::ATOMXOR8:
12191 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12192 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012193 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012194 X86::NOT8r, X86::AL,
12195 X86::GR8RegisterClass);
12196 case X86::ATOMNAND8:
12197 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12198 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012199 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012200 X86::NOT8r, X86::AL,
12201 X86::GR8RegisterClass, true);
12202 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012203 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012204 case X86::ATOMAND64:
12205 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012206 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012207 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012208 X86::NOT64r, X86::RAX,
12209 X86::GR64RegisterClass);
12210 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012211 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12212 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012213 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012214 X86::NOT64r, X86::RAX,
12215 X86::GR64RegisterClass);
12216 case X86::ATOMXOR64:
12217 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012218 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012219 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012220 X86::NOT64r, X86::RAX,
12221 X86::GR64RegisterClass);
12222 case X86::ATOMNAND64:
12223 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12224 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012225 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012226 X86::NOT64r, X86::RAX,
12227 X86::GR64RegisterClass, true);
12228 case X86::ATOMMIN64:
12229 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12230 case X86::ATOMMAX64:
12231 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12232 case X86::ATOMUMIN64:
12233 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12234 case X86::ATOMUMAX64:
12235 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012236
12237 // This group does 64-bit operations on a 32-bit host.
12238 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012239 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012240 X86::AND32rr, X86::AND32rr,
12241 X86::AND32ri, X86::AND32ri,
12242 false);
12243 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012244 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012245 X86::OR32rr, X86::OR32rr,
12246 X86::OR32ri, X86::OR32ri,
12247 false);
12248 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012249 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012250 X86::XOR32rr, X86::XOR32rr,
12251 X86::XOR32ri, X86::XOR32ri,
12252 false);
12253 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012254 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012255 X86::AND32rr, X86::AND32rr,
12256 X86::AND32ri, X86::AND32ri,
12257 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012258 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012259 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012260 X86::ADD32rr, X86::ADC32rr,
12261 X86::ADD32ri, X86::ADC32ri,
12262 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012263 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012264 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012265 X86::SUB32rr, X86::SBB32rr,
12266 X86::SUB32ri, X86::SBB32ri,
12267 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012268 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012269 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012270 X86::MOV32rr, X86::MOV32rr,
12271 X86::MOV32ri, X86::MOV32ri,
12272 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012273 case X86::VASTART_SAVE_XMM_REGS:
12274 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012275
12276 case X86::VAARG_64:
12277 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012278 }
12279}
12280
12281//===----------------------------------------------------------------------===//
12282// X86 Optimization Hooks
12283//===----------------------------------------------------------------------===//
12284
Dan Gohman475871a2008-07-27 21:46:04 +000012285void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012286 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012287 APInt &KnownZero,
12288 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012289 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012290 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012291 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012292 assert((Opc >= ISD::BUILTIN_OP_END ||
12293 Opc == ISD::INTRINSIC_WO_CHAIN ||
12294 Opc == ISD::INTRINSIC_W_CHAIN ||
12295 Opc == ISD::INTRINSIC_VOID) &&
12296 "Should use MaskedValueIsZero if you don't know whether Op"
12297 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012298
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012299 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012300 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012301 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012302 case X86ISD::ADD:
12303 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012304 case X86ISD::ADC:
12305 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012306 case X86ISD::SMUL:
12307 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012308 case X86ISD::INC:
12309 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012310 case X86ISD::OR:
12311 case X86ISD::XOR:
12312 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012313 // These nodes' second result is a boolean.
12314 if (Op.getResNo() == 0)
12315 break;
12316 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012317 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012318 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12319 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012320 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012321 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012322}
Chris Lattner259e97c2006-01-31 19:43:35 +000012323
Owen Andersonbc146b02010-09-21 20:42:50 +000012324unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12325 unsigned Depth) const {
12326 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12327 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12328 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012329
Owen Andersonbc146b02010-09-21 20:42:50 +000012330 // Fallback case.
12331 return 1;
12332}
12333
Evan Cheng206ee9d2006-07-07 08:33:52 +000012334/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012335/// node is a GlobalAddress + offset.
12336bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012337 const GlobalValue* &GA,
12338 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012339 if (N->getOpcode() == X86ISD::Wrapper) {
12340 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012341 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012342 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012343 return true;
12344 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012345 }
Evan Chengad4196b2008-05-12 19:56:52 +000012346 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012347}
12348
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012349/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12350/// same as extracting the high 128-bit part of 256-bit vector and then
12351/// inserting the result into the low part of a new 256-bit vector
12352static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12353 EVT VT = SVOp->getValueType(0);
12354 int NumElems = VT.getVectorNumElements();
12355
12356 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12357 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12358 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12359 SVOp->getMaskElt(j) >= 0)
12360 return false;
12361
12362 return true;
12363}
12364
12365/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12366/// same as extracting the low 128-bit part of 256-bit vector and then
12367/// inserting the result into the high part of a new 256-bit vector
12368static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12369 EVT VT = SVOp->getValueType(0);
12370 int NumElems = VT.getVectorNumElements();
12371
12372 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12373 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12374 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12375 SVOp->getMaskElt(j) >= 0)
12376 return false;
12377
12378 return true;
12379}
12380
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012381/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12382static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12383 TargetLowering::DAGCombinerInfo &DCI) {
12384 DebugLoc dl = N->getDebugLoc();
12385 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12386 SDValue V1 = SVOp->getOperand(0);
12387 SDValue V2 = SVOp->getOperand(1);
12388 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012389 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012390
12391 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12392 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12393 //
12394 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012395 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012396 // V UNDEF BUILD_VECTOR UNDEF
12397 // \ / \ /
12398 // CONCAT_VECTOR CONCAT_VECTOR
12399 // \ /
12400 // \ /
12401 // RESULT: V + zero extended
12402 //
12403 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12404 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12405 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12406 return SDValue();
12407
12408 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12409 return SDValue();
12410
12411 // To match the shuffle mask, the first half of the mask should
12412 // be exactly the first vector, and all the rest a splat with the
12413 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012414 for (int i = 0; i < NumElems/2; ++i)
12415 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12416 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12417 return SDValue();
12418
12419 // Emit a zeroed vector and insert the desired subvector on its
12420 // first half.
12421 SDValue Zeros = getZeroVector(VT, true /* HasSSE2 */, DAG, dl);
12422 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12423 DAG.getConstant(0, MVT::i32), DAG, dl);
12424 return DCI.CombineTo(N, InsV);
12425 }
12426
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012427 //===--------------------------------------------------------------------===//
12428 // Combine some shuffles into subvector extracts and inserts:
12429 //
12430
12431 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12432 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12433 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12434 DAG, dl);
12435 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12436 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12437 return DCI.CombineTo(N, InsV);
12438 }
12439
12440 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12441 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12442 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12443 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12444 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12445 return DCI.CombineTo(N, InsV);
12446 }
12447
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012448 return SDValue();
12449}
12450
12451/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012452static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012453 TargetLowering::DAGCombinerInfo &DCI,
12454 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012455 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012456 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012457
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012458 // Don't create instructions with illegal types after legalize types has run.
12459 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12460 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12461 return SDValue();
12462
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012463 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12464 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12465 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012466 return PerformShuffleCombine256(N, DAG, DCI);
12467
12468 // Only handle 128 wide vector from here on.
12469 if (VT.getSizeInBits() != 128)
12470 return SDValue();
12471
12472 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12473 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12474 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012475 SmallVector<SDValue, 16> Elts;
12476 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012477 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012478
Nate Begemanfdea31a2010-03-24 20:49:50 +000012479 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012480}
Evan Chengd880b972008-05-09 21:53:03 +000012481
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012482/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12483/// generation and convert it from being a bunch of shuffles and extracts
12484/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012485static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12486 const TargetLowering &TLI) {
12487 SDValue InputVector = N->getOperand(0);
12488
12489 // Only operate on vectors of 4 elements, where the alternative shuffling
12490 // gets to be more expensive.
12491 if (InputVector.getValueType() != MVT::v4i32)
12492 return SDValue();
12493
12494 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12495 // single use which is a sign-extend or zero-extend, and all elements are
12496 // used.
12497 SmallVector<SDNode *, 4> Uses;
12498 unsigned ExtractedElements = 0;
12499 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12500 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12501 if (UI.getUse().getResNo() != InputVector.getResNo())
12502 return SDValue();
12503
12504 SDNode *Extract = *UI;
12505 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12506 return SDValue();
12507
12508 if (Extract->getValueType(0) != MVT::i32)
12509 return SDValue();
12510 if (!Extract->hasOneUse())
12511 return SDValue();
12512 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12513 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12514 return SDValue();
12515 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12516 return SDValue();
12517
12518 // Record which element was extracted.
12519 ExtractedElements |=
12520 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12521
12522 Uses.push_back(Extract);
12523 }
12524
12525 // If not all the elements were used, this may not be worthwhile.
12526 if (ExtractedElements != 15)
12527 return SDValue();
12528
12529 // Ok, we've now decided to do the transformation.
12530 DebugLoc dl = InputVector.getDebugLoc();
12531
12532 // Store the value to a temporary stack slot.
12533 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012534 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12535 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012536
12537 // Replace each use (extract) with a load of the appropriate element.
12538 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12539 UE = Uses.end(); UI != UE; ++UI) {
12540 SDNode *Extract = *UI;
12541
Nadav Rotem86694292011-05-17 08:31:57 +000012542 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012543 SDValue Idx = Extract->getOperand(1);
12544 unsigned EltSize =
12545 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12546 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12547 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12548
Nadav Rotem86694292011-05-17 08:31:57 +000012549 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012550 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012551
12552 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012553 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012554 ScalarAddr, MachinePointerInfo(),
12555 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012556
12557 // Replace the exact with the load.
12558 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12559 }
12560
12561 // The replacement was made in place; don't return anything.
12562 return SDValue();
12563}
12564
Chris Lattner83e6c992006-10-04 06:57:07 +000012565/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012566static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012567 const X86Subtarget *Subtarget) {
12568 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012569 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012570 // Get the LHS/RHS of the select.
12571 SDValue LHS = N->getOperand(1);
12572 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000012573
Dan Gohman670e5392009-09-21 18:03:22 +000012574 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012575 // instructions match the semantics of the common C idiom x<y?x:y but not
12576 // x<=y?x:y, because of how they handle negative zero (which can be
12577 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000012578 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000012579 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000012580 Cond.getOpcode() == ISD::SETCC) {
12581 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012582
Chris Lattner47b4ce82009-03-11 05:48:52 +000012583 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012584 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012585 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12586 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012587 switch (CC) {
12588 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012589 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012590 // Converting this to a min would handle NaNs incorrectly, and swapping
12591 // the operands would cause it to handle comparisons between positive
12592 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012593 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012594 if (!UnsafeFPMath &&
12595 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12596 break;
12597 std::swap(LHS, RHS);
12598 }
Dan Gohman670e5392009-09-21 18:03:22 +000012599 Opcode = X86ISD::FMIN;
12600 break;
12601 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012602 // Converting this to a min would handle comparisons between positive
12603 // and negative zero incorrectly.
12604 if (!UnsafeFPMath &&
12605 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12606 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012607 Opcode = X86ISD::FMIN;
12608 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012609 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012610 // Converting this to a min would handle both negative zeros and NaNs
12611 // incorrectly, but we can swap the operands to fix both.
12612 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012613 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012614 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012615 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012616 Opcode = X86ISD::FMIN;
12617 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012618
Dan Gohman670e5392009-09-21 18:03:22 +000012619 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012620 // Converting this to a max would handle comparisons between positive
12621 // and negative zero incorrectly.
12622 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012623 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012624 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012625 Opcode = X86ISD::FMAX;
12626 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012627 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012628 // Converting this to a max would handle NaNs incorrectly, and swapping
12629 // the operands would cause it to handle comparisons between positive
12630 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012631 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012632 if (!UnsafeFPMath &&
12633 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12634 break;
12635 std::swap(LHS, RHS);
12636 }
Dan Gohman670e5392009-09-21 18:03:22 +000012637 Opcode = X86ISD::FMAX;
12638 break;
12639 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012640 // Converting this to a max would handle both negative zeros and NaNs
12641 // incorrectly, but we can swap the operands to fix both.
12642 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012643 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012644 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012645 case ISD::SETGE:
12646 Opcode = X86ISD::FMAX;
12647 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012648 }
Dan Gohman670e5392009-09-21 18:03:22 +000012649 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012650 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12651 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012652 switch (CC) {
12653 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012654 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012655 // Converting this to a min would handle comparisons between positive
12656 // and negative zero incorrectly, and swapping the operands would
12657 // cause it to handle NaNs incorrectly.
12658 if (!UnsafeFPMath &&
12659 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012660 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012661 break;
12662 std::swap(LHS, RHS);
12663 }
Dan Gohman670e5392009-09-21 18:03:22 +000012664 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012665 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012666 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012667 // Converting this to a min would handle NaNs incorrectly.
12668 if (!UnsafeFPMath &&
12669 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12670 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012671 Opcode = X86ISD::FMIN;
12672 break;
12673 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012674 // Converting this to a min would handle both negative zeros and NaNs
12675 // incorrectly, but we can swap the operands to fix both.
12676 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012677 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012678 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012679 case ISD::SETGE:
12680 Opcode = X86ISD::FMIN;
12681 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012682
Dan Gohman670e5392009-09-21 18:03:22 +000012683 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012684 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012685 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012686 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012687 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012688 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012689 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012690 // Converting this to a max would handle comparisons between positive
12691 // and negative zero incorrectly, and swapping the operands would
12692 // cause it to handle NaNs incorrectly.
12693 if (!UnsafeFPMath &&
12694 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012695 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012696 break;
12697 std::swap(LHS, RHS);
12698 }
Dan Gohman670e5392009-09-21 18:03:22 +000012699 Opcode = X86ISD::FMAX;
12700 break;
12701 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012702 // Converting this to a max would handle both negative zeros and NaNs
12703 // incorrectly, but we can swap the operands to fix both.
12704 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012705 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012706 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012707 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012708 Opcode = X86ISD::FMAX;
12709 break;
12710 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012711 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012712
Chris Lattner47b4ce82009-03-11 05:48:52 +000012713 if (Opcode)
12714 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012715 }
Eric Christopherfd179292009-08-27 18:07:15 +000012716
Chris Lattnerd1980a52009-03-12 06:52:53 +000012717 // If this is a select between two integer constants, try to do some
12718 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012719 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12720 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012721 // Don't do this for crazy integer types.
12722 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12723 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012724 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012725 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012726
Chris Lattnercee56e72009-03-13 05:53:31 +000012727 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012728 // Efficiently invertible.
12729 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12730 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12731 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12732 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012733 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012734 }
Eric Christopherfd179292009-08-27 18:07:15 +000012735
Chris Lattnerd1980a52009-03-12 06:52:53 +000012736 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012737 if (FalseC->getAPIntValue() == 0 &&
12738 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012739 if (NeedsCondInvert) // Invert the condition if needed.
12740 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12741 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012742
Chris Lattnerd1980a52009-03-12 06:52:53 +000012743 // Zero extend the condition if needed.
12744 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012745
Chris Lattnercee56e72009-03-13 05:53:31 +000012746 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000012747 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012748 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012749 }
Eric Christopherfd179292009-08-27 18:07:15 +000012750
Chris Lattner97a29a52009-03-13 05:22:11 +000012751 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000012752 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000012753 if (NeedsCondInvert) // Invert the condition if needed.
12754 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12755 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012756
Chris Lattner97a29a52009-03-13 05:22:11 +000012757 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012758 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12759 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012760 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000012761 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000012762 }
Eric Christopherfd179292009-08-27 18:07:15 +000012763
Chris Lattnercee56e72009-03-13 05:53:31 +000012764 // Optimize cases that will turn into an LEA instruction. This requires
12765 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012766 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012767 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012768 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012769
Chris Lattnercee56e72009-03-13 05:53:31 +000012770 bool isFastMultiplier = false;
12771 if (Diff < 10) {
12772 switch ((unsigned char)Diff) {
12773 default: break;
12774 case 1: // result = add base, cond
12775 case 2: // result = lea base( , cond*2)
12776 case 3: // result = lea base(cond, cond*2)
12777 case 4: // result = lea base( , cond*4)
12778 case 5: // result = lea base(cond, cond*4)
12779 case 8: // result = lea base( , cond*8)
12780 case 9: // result = lea base(cond, cond*8)
12781 isFastMultiplier = true;
12782 break;
12783 }
12784 }
Eric Christopherfd179292009-08-27 18:07:15 +000012785
Chris Lattnercee56e72009-03-13 05:53:31 +000012786 if (isFastMultiplier) {
12787 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
12788 if (NeedsCondInvert) // Invert the condition if needed.
12789 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12790 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012791
Chris Lattnercee56e72009-03-13 05:53:31 +000012792 // Zero extend the condition if needed.
12793 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12794 Cond);
12795 // Scale the condition by the difference.
12796 if (Diff != 1)
12797 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12798 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012799
Chris Lattnercee56e72009-03-13 05:53:31 +000012800 // Add the base if non-zero.
12801 if (FalseC->getAPIntValue() != 0)
12802 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12803 SDValue(FalseC, 0));
12804 return Cond;
12805 }
Eric Christopherfd179292009-08-27 18:07:15 +000012806 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012807 }
12808 }
Eric Christopherfd179292009-08-27 18:07:15 +000012809
Dan Gohman475871a2008-07-27 21:46:04 +000012810 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000012811}
12812
Chris Lattnerd1980a52009-03-12 06:52:53 +000012813/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
12814static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
12815 TargetLowering::DAGCombinerInfo &DCI) {
12816 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000012817
Chris Lattnerd1980a52009-03-12 06:52:53 +000012818 // If the flag operand isn't dead, don't touch this CMOV.
12819 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
12820 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000012821
Evan Chengb5a55d92011-05-24 01:48:22 +000012822 SDValue FalseOp = N->getOperand(0);
12823 SDValue TrueOp = N->getOperand(1);
12824 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
12825 SDValue Cond = N->getOperand(3);
12826 if (CC == X86::COND_E || CC == X86::COND_NE) {
12827 switch (Cond.getOpcode()) {
12828 default: break;
12829 case X86ISD::BSR:
12830 case X86ISD::BSF:
12831 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
12832 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
12833 return (CC == X86::COND_E) ? FalseOp : TrueOp;
12834 }
12835 }
12836
Chris Lattnerd1980a52009-03-12 06:52:53 +000012837 // If this is a select between two integer constants, try to do some
12838 // optimizations. Note that the operands are ordered the opposite of SELECT
12839 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000012840 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
12841 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012842 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
12843 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000012844 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
12845 CC = X86::GetOppositeBranchCondition(CC);
12846 std::swap(TrueC, FalseC);
12847 }
Eric Christopherfd179292009-08-27 18:07:15 +000012848
Chris Lattnerd1980a52009-03-12 06:52:53 +000012849 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012850 // This is efficient for any integer data type (including i8/i16) and
12851 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012852 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012853 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12854 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012855
Chris Lattnerd1980a52009-03-12 06:52:53 +000012856 // Zero extend the condition if needed.
12857 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012858
Chris Lattnerd1980a52009-03-12 06:52:53 +000012859 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
12860 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012861 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012862 if (N->getNumValues() == 2) // Dead flag value?
12863 return DCI.CombineTo(N, Cond, SDValue());
12864 return Cond;
12865 }
Eric Christopherfd179292009-08-27 18:07:15 +000012866
Chris Lattnercee56e72009-03-13 05:53:31 +000012867 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
12868 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000012869 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012870 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12871 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012872
Chris Lattner97a29a52009-03-13 05:22:11 +000012873 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012874 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12875 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012876 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12877 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000012878
Chris Lattner97a29a52009-03-13 05:22:11 +000012879 if (N->getNumValues() == 2) // Dead flag value?
12880 return DCI.CombineTo(N, Cond, SDValue());
12881 return Cond;
12882 }
Eric Christopherfd179292009-08-27 18:07:15 +000012883
Chris Lattnercee56e72009-03-13 05:53:31 +000012884 // Optimize cases that will turn into an LEA instruction. This requires
12885 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012886 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012887 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012888 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012889
Chris Lattnercee56e72009-03-13 05:53:31 +000012890 bool isFastMultiplier = false;
12891 if (Diff < 10) {
12892 switch ((unsigned char)Diff) {
12893 default: break;
12894 case 1: // result = add base, cond
12895 case 2: // result = lea base( , cond*2)
12896 case 3: // result = lea base(cond, cond*2)
12897 case 4: // result = lea base( , cond*4)
12898 case 5: // result = lea base(cond, cond*4)
12899 case 8: // result = lea base( , cond*8)
12900 case 9: // result = lea base(cond, cond*8)
12901 isFastMultiplier = true;
12902 break;
12903 }
12904 }
Eric Christopherfd179292009-08-27 18:07:15 +000012905
Chris Lattnercee56e72009-03-13 05:53:31 +000012906 if (isFastMultiplier) {
12907 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012908 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12909 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000012910 // Zero extend the condition if needed.
12911 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12912 Cond);
12913 // Scale the condition by the difference.
12914 if (Diff != 1)
12915 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12916 DAG.getConstant(Diff, Cond.getValueType()));
12917
12918 // Add the base if non-zero.
12919 if (FalseC->getAPIntValue() != 0)
12920 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12921 SDValue(FalseC, 0));
12922 if (N->getNumValues() == 2) // Dead flag value?
12923 return DCI.CombineTo(N, Cond, SDValue());
12924 return Cond;
12925 }
Eric Christopherfd179292009-08-27 18:07:15 +000012926 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012927 }
12928 }
12929 return SDValue();
12930}
12931
12932
Evan Cheng0b0cd912009-03-28 05:57:29 +000012933/// PerformMulCombine - Optimize a single multiply with constant into two
12934/// in order to implement it with two cheaper instructions, e.g.
12935/// LEA + SHL, LEA + LEA.
12936static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
12937 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000012938 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
12939 return SDValue();
12940
Owen Andersone50ed302009-08-10 22:56:29 +000012941 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012942 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000012943 return SDValue();
12944
12945 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
12946 if (!C)
12947 return SDValue();
12948 uint64_t MulAmt = C->getZExtValue();
12949 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
12950 return SDValue();
12951
12952 uint64_t MulAmt1 = 0;
12953 uint64_t MulAmt2 = 0;
12954 if ((MulAmt % 9) == 0) {
12955 MulAmt1 = 9;
12956 MulAmt2 = MulAmt / 9;
12957 } else if ((MulAmt % 5) == 0) {
12958 MulAmt1 = 5;
12959 MulAmt2 = MulAmt / 5;
12960 } else if ((MulAmt % 3) == 0) {
12961 MulAmt1 = 3;
12962 MulAmt2 = MulAmt / 3;
12963 }
12964 if (MulAmt2 &&
12965 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
12966 DebugLoc DL = N->getDebugLoc();
12967
12968 if (isPowerOf2_64(MulAmt2) &&
12969 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
12970 // If second multiplifer is pow2, issue it first. We want the multiply by
12971 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
12972 // is an add.
12973 std::swap(MulAmt1, MulAmt2);
12974
12975 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000012976 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012977 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000012978 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000012979 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012980 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000012981 DAG.getConstant(MulAmt1, VT));
12982
Eric Christopherfd179292009-08-27 18:07:15 +000012983 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012984 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000012985 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000012986 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012987 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000012988 DAG.getConstant(MulAmt2, VT));
12989
12990 // Do not add new nodes to DAG combiner worklist.
12991 DCI.CombineTo(N, NewMul, false);
12992 }
12993 return SDValue();
12994}
12995
Evan Chengad9c0a32009-12-15 00:53:42 +000012996static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
12997 SDValue N0 = N->getOperand(0);
12998 SDValue N1 = N->getOperand(1);
12999 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13000 EVT VT = N0.getValueType();
13001
13002 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13003 // since the result of setcc_c is all zero's or all ones.
13004 if (N1C && N0.getOpcode() == ISD::AND &&
13005 N0.getOperand(1).getOpcode() == ISD::Constant) {
13006 SDValue N00 = N0.getOperand(0);
13007 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13008 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13009 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13010 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13011 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13012 APInt ShAmt = N1C->getAPIntValue();
13013 Mask = Mask.shl(ShAmt);
13014 if (Mask != 0)
13015 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13016 N00, DAG.getConstant(Mask, VT));
13017 }
13018 }
13019
13020 return SDValue();
13021}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013022
Nate Begeman740ab032009-01-26 00:52:55 +000013023/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13024/// when possible.
13025static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13026 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013027 EVT VT = N->getValueType(0);
13028 if (!VT.isVector() && VT.isInteger() &&
13029 N->getOpcode() == ISD::SHL)
13030 return PerformSHLCombine(N, DAG);
13031
Nate Begeman740ab032009-01-26 00:52:55 +000013032 // On X86 with SSE2 support, we can transform this to a vector shift if
13033 // all elements are shifted by the same amount. We can't do this in legalize
13034 // because the a constant vector is typically transformed to a constant pool
13035 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000013036 if (!(Subtarget->hasSSE2() || Subtarget->hasAVX()))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013037 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013038
Owen Anderson825b72b2009-08-11 20:47:22 +000013039 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013040 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013041
Mon P Wang3becd092009-01-28 08:12:05 +000013042 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013043 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013044 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013045 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013046 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13047 unsigned NumElts = VT.getVectorNumElements();
13048 unsigned i = 0;
13049 for (; i != NumElts; ++i) {
13050 SDValue Arg = ShAmtOp.getOperand(i);
13051 if (Arg.getOpcode() == ISD::UNDEF) continue;
13052 BaseShAmt = Arg;
13053 break;
13054 }
13055 for (; i != NumElts; ++i) {
13056 SDValue Arg = ShAmtOp.getOperand(i);
13057 if (Arg.getOpcode() == ISD::UNDEF) continue;
13058 if (Arg != BaseShAmt) {
13059 return SDValue();
13060 }
13061 }
13062 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013063 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013064 SDValue InVec = ShAmtOp.getOperand(0);
13065 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13066 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13067 unsigned i = 0;
13068 for (; i != NumElts; ++i) {
13069 SDValue Arg = InVec.getOperand(i);
13070 if (Arg.getOpcode() == ISD::UNDEF) continue;
13071 BaseShAmt = Arg;
13072 break;
13073 }
13074 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13075 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013076 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013077 if (C->getZExtValue() == SplatIdx)
13078 BaseShAmt = InVec.getOperand(1);
13079 }
13080 }
13081 if (BaseShAmt.getNode() == 0)
13082 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13083 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013084 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013085 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013086
Mon P Wangefa42202009-09-03 19:56:25 +000013087 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013088 if (EltVT.bitsGT(MVT::i32))
13089 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13090 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013091 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013092
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013093 // The shift amount is identical so we can do a vector shift.
13094 SDValue ValOp = N->getOperand(0);
13095 switch (N->getOpcode()) {
13096 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013097 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013098 break;
13099 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013100 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013101 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013102 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013103 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013104 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013105 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013106 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013107 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013108 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013109 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013110 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013111 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013112 break;
13113 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013114 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013115 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013116 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013117 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013118 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013119 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013120 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013121 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013122 break;
13123 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013124 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013125 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013126 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013127 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013128 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013129 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013130 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013131 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013132 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013133 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013134 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013135 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013136 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013137 }
13138 return SDValue();
13139}
13140
Nate Begemanb65c1752010-12-17 22:55:37 +000013141
Stuart Hastings865f0932011-06-03 23:53:54 +000013142// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13143// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13144// and friends. Likewise for OR -> CMPNEQSS.
13145static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13146 TargetLowering::DAGCombinerInfo &DCI,
13147 const X86Subtarget *Subtarget) {
13148 unsigned opcode;
13149
13150 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13151 // we're requiring SSE2 for both.
13152 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
13153 SDValue N0 = N->getOperand(0);
13154 SDValue N1 = N->getOperand(1);
13155 SDValue CMP0 = N0->getOperand(1);
13156 SDValue CMP1 = N1->getOperand(1);
13157 DebugLoc DL = N->getDebugLoc();
13158
13159 // The SETCCs should both refer to the same CMP.
13160 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13161 return SDValue();
13162
13163 SDValue CMP00 = CMP0->getOperand(0);
13164 SDValue CMP01 = CMP0->getOperand(1);
13165 EVT VT = CMP00.getValueType();
13166
13167 if (VT == MVT::f32 || VT == MVT::f64) {
13168 bool ExpectingFlags = false;
13169 // Check for any users that want flags:
13170 for (SDNode::use_iterator UI = N->use_begin(),
13171 UE = N->use_end();
13172 !ExpectingFlags && UI != UE; ++UI)
13173 switch (UI->getOpcode()) {
13174 default:
13175 case ISD::BR_CC:
13176 case ISD::BRCOND:
13177 case ISD::SELECT:
13178 ExpectingFlags = true;
13179 break;
13180 case ISD::CopyToReg:
13181 case ISD::SIGN_EXTEND:
13182 case ISD::ZERO_EXTEND:
13183 case ISD::ANY_EXTEND:
13184 break;
13185 }
13186
13187 if (!ExpectingFlags) {
13188 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13189 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13190
13191 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13192 X86::CondCode tmp = cc0;
13193 cc0 = cc1;
13194 cc1 = tmp;
13195 }
13196
13197 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13198 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13199 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13200 X86ISD::NodeType NTOperator = is64BitFP ?
13201 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13202 // FIXME: need symbolic constants for these magic numbers.
13203 // See X86ATTInstPrinter.cpp:printSSECC().
13204 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13205 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13206 DAG.getConstant(x86cc, MVT::i8));
13207 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13208 OnesOrZeroesF);
13209 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13210 DAG.getConstant(1, MVT::i32));
13211 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13212 return OneBitOfTruth;
13213 }
13214 }
13215 }
13216 }
13217 return SDValue();
13218}
13219
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013220/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13221/// so it can be folded inside ANDNP.
13222static bool CanFoldXORWithAllOnes(const SDNode *N) {
13223 EVT VT = N->getValueType(0);
13224
13225 // Match direct AllOnes for 128 and 256-bit vectors
13226 if (ISD::isBuildVectorAllOnes(N))
13227 return true;
13228
13229 // Look through a bit convert.
13230 if (N->getOpcode() == ISD::BITCAST)
13231 N = N->getOperand(0).getNode();
13232
13233 // Sometimes the operand may come from a insert_subvector building a 256-bit
13234 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013235 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013236 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13237 SDValue V1 = N->getOperand(0);
13238 SDValue V2 = N->getOperand(1);
13239
13240 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13241 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13242 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13243 ISD::isBuildVectorAllOnes(V2.getNode()))
13244 return true;
13245 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013246
13247 return false;
13248}
13249
Nate Begemanb65c1752010-12-17 22:55:37 +000013250static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13251 TargetLowering::DAGCombinerInfo &DCI,
13252 const X86Subtarget *Subtarget) {
13253 if (DCI.isBeforeLegalizeOps())
13254 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013255
Stuart Hastings865f0932011-06-03 23:53:54 +000013256 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13257 if (R.getNode())
13258 return R;
13259
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013260 // Want to form ANDNP nodes:
13261 // 1) In the hopes of then easily combining them with OR and AND nodes
13262 // to form PBLEND/PSIGN.
13263 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000013264 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013265 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013266 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013267
Nate Begemanb65c1752010-12-17 22:55:37 +000013268 SDValue N0 = N->getOperand(0);
13269 SDValue N1 = N->getOperand(1);
13270 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013271
Nate Begemanb65c1752010-12-17 22:55:37 +000013272 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013273 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013274 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13275 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013276 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013277
13278 // Check RHS for vnot
13279 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013280 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13281 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013282 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013283
Nate Begemanb65c1752010-12-17 22:55:37 +000013284 return SDValue();
13285}
13286
Evan Cheng760d1942010-01-04 21:22:48 +000013287static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013288 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013289 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013290 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013291 return SDValue();
13292
Stuart Hastings865f0932011-06-03 23:53:54 +000013293 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13294 if (R.getNode())
13295 return R;
13296
Evan Cheng760d1942010-01-04 21:22:48 +000013297 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000013298 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000013299 return SDValue();
13300
Evan Cheng760d1942010-01-04 21:22:48 +000013301 SDValue N0 = N->getOperand(0);
13302 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013303
Nate Begemanb65c1752010-12-17 22:55:37 +000013304 // look for psign/blend
13305 if (Subtarget->hasSSSE3()) {
13306 if (VT == MVT::v2i64) {
13307 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013308 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000013309 std::swap(N0, N1);
13310 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013311 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013312 SDValue Mask = N1.getOperand(0);
13313 SDValue X = N1.getOperand(1);
13314 SDValue Y;
13315 if (N0.getOperand(0) == Mask)
13316 Y = N0.getOperand(1);
13317 if (N0.getOperand(1) == Mask)
13318 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013319
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013320 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000013321 if (!Y.getNode())
13322 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013323
Nate Begemanb65c1752010-12-17 22:55:37 +000013324 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13325 if (Mask.getOpcode() != ISD::BITCAST ||
13326 X.getOpcode() != ISD::BITCAST ||
13327 Y.getOpcode() != ISD::BITCAST)
13328 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013329
Nate Begemanb65c1752010-12-17 22:55:37 +000013330 // Look through mask bitcast.
13331 Mask = Mask.getOperand(0);
13332 EVT MaskVT = Mask.getValueType();
13333
13334 // Validate that the Mask operand is a vector sra node. The sra node
13335 // will be an intrinsic.
13336 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13337 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013338
Nate Begemanb65c1752010-12-17 22:55:37 +000013339 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13340 // there is no psrai.b
13341 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13342 case Intrinsic::x86_sse2_psrai_w:
13343 case Intrinsic::x86_sse2_psrai_d:
13344 break;
13345 default: return SDValue();
13346 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013347
Nate Begemanb65c1752010-12-17 22:55:37 +000013348 // Check that the SRA is all signbits.
13349 SDValue SraC = Mask.getOperand(2);
13350 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13351 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13352 if ((SraAmt + 1) != EltBits)
13353 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013354
Nate Begemanb65c1752010-12-17 22:55:37 +000013355 DebugLoc DL = N->getDebugLoc();
13356
13357 // Now we know we at least have a plendvb with the mask val. See if
13358 // we can form a psignb/w/d.
13359 // psign = x.type == y.type == mask.type && y = sub(0, x);
13360 X = X.getOperand(0);
13361 Y = Y.getOperand(0);
13362 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13363 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
13364 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
13365 unsigned Opc = 0;
13366 switch (EltBits) {
13367 case 8: Opc = X86ISD::PSIGNB; break;
13368 case 16: Opc = X86ISD::PSIGNW; break;
13369 case 32: Opc = X86ISD::PSIGND; break;
13370 default: break;
13371 }
13372 if (Opc) {
13373 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
13374 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
13375 }
13376 }
13377 // PBLENDVB only available on SSE 4.1
13378 if (!Subtarget->hasSSE41())
13379 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013380
Nate Begemanb65c1752010-12-17 22:55:37 +000013381 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
13382 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
13383 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nadav Rotem8ffad562011-09-09 20:29:17 +000013384 Mask = DAG.getNode(X86ISD::BLENDV, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000013385 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
13386 }
13387 }
13388 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013389
Nate Begemanb65c1752010-12-17 22:55:37 +000013390 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013391 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13392 std::swap(N0, N1);
13393 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13394 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013395 if (!N0.hasOneUse() || !N1.hasOneUse())
13396 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013397
13398 SDValue ShAmt0 = N0.getOperand(1);
13399 if (ShAmt0.getValueType() != MVT::i8)
13400 return SDValue();
13401 SDValue ShAmt1 = N1.getOperand(1);
13402 if (ShAmt1.getValueType() != MVT::i8)
13403 return SDValue();
13404 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13405 ShAmt0 = ShAmt0.getOperand(0);
13406 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13407 ShAmt1 = ShAmt1.getOperand(0);
13408
13409 DebugLoc DL = N->getDebugLoc();
13410 unsigned Opc = X86ISD::SHLD;
13411 SDValue Op0 = N0.getOperand(0);
13412 SDValue Op1 = N1.getOperand(0);
13413 if (ShAmt0.getOpcode() == ISD::SUB) {
13414 Opc = X86ISD::SHRD;
13415 std::swap(Op0, Op1);
13416 std::swap(ShAmt0, ShAmt1);
13417 }
13418
Evan Cheng8b1190a2010-04-28 01:18:01 +000013419 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013420 if (ShAmt1.getOpcode() == ISD::SUB) {
13421 SDValue Sum = ShAmt1.getOperand(0);
13422 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013423 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13424 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13425 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13426 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013427 return DAG.getNode(Opc, DL, VT,
13428 Op0, Op1,
13429 DAG.getNode(ISD::TRUNCATE, DL,
13430 MVT::i8, ShAmt0));
13431 }
13432 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13433 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13434 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013435 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013436 return DAG.getNode(Opc, DL, VT,
13437 N0.getOperand(0), N1.getOperand(0),
13438 DAG.getNode(ISD::TRUNCATE, DL,
13439 MVT::i8, ShAmt0));
13440 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013441
Evan Cheng760d1942010-01-04 21:22:48 +000013442 return SDValue();
13443}
13444
Chris Lattner149a4e52008-02-22 02:09:43 +000013445/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013446static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013447 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013448 StoreSDNode *St = cast<StoreSDNode>(N);
13449 EVT VT = St->getValue().getValueType();
13450 EVT StVT = St->getMemoryVT();
13451 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013452 SDValue StoredVal = St->getOperand(1);
13453 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13454
13455 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013456 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13457 // 128-bit ones. If in the future the cost becomes only one memory access the
13458 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013459 if (VT.getSizeInBits() == 256 &&
13460 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13461 StoredVal.getNumOperands() == 2) {
13462
13463 SDValue Value0 = StoredVal.getOperand(0);
13464 SDValue Value1 = StoredVal.getOperand(1);
13465
13466 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13467 SDValue Ptr0 = St->getBasePtr();
13468 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13469
13470 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13471 St->getPointerInfo(), St->isVolatile(),
13472 St->isNonTemporal(), St->getAlignment());
13473 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13474 St->getPointerInfo(), St->isVolatile(),
13475 St->isNonTemporal(), St->getAlignment());
13476 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13477 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013478
13479 // Optimize trunc store (of multiple scalars) to shuffle and store.
13480 // First, pack all of the elements in one place. Next, store to memory
13481 // in fewer chunks.
13482 if (St->isTruncatingStore() && VT.isVector()) {
13483 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13484 unsigned NumElems = VT.getVectorNumElements();
13485 assert(StVT != VT && "Cannot truncate to the same type");
13486 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13487 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13488
13489 // From, To sizes and ElemCount must be pow of two
13490 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
13491 // We are going to use the original vector elt for storing.
13492 // accumulated smaller vector elements must be a multiple of bigger size.
13493 if (0 != (NumElems * ToSz) % FromSz) return SDValue();
13494 unsigned SizeRatio = FromSz / ToSz;
13495
13496 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13497
13498 // Create a type on which we perform the shuffle
13499 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13500 StVT.getScalarType(), NumElems*SizeRatio);
13501
13502 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13503
13504 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13505 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13506 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13507
13508 // Can't shuffle using an illegal type
13509 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13510
13511 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13512 DAG.getUNDEF(WideVec.getValueType()),
13513 ShuffleVec.data());
13514 // At this point all of the data is stored at the bottom of the
13515 // register. We now need to save it to mem.
13516
13517 // Find the largest store unit
13518 MVT StoreType = MVT::i8;
13519 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13520 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13521 MVT Tp = (MVT::SimpleValueType)tp;
13522 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13523 StoreType = Tp;
13524 }
13525
13526 // Bitcast the original vector into a vector of store-size units
13527 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13528 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13529 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13530 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
13531 SmallVector<SDValue, 8> Chains;
13532 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
13533 TLI.getPointerTy());
13534 SDValue Ptr = St->getBasePtr();
13535
13536 // Perform one or more big stores into memory.
13537 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
13538 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
13539 StoreType, ShuffWide,
13540 DAG.getIntPtrConstant(i));
13541 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
13542 St->getPointerInfo(), St->isVolatile(),
13543 St->isNonTemporal(), St->getAlignment());
13544 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
13545 Chains.push_back(Ch);
13546 }
13547
13548 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
13549 Chains.size());
13550 }
13551
13552
Chris Lattner149a4e52008-02-22 02:09:43 +000013553 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
13554 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000013555 // A preferable solution to the general problem is to figure out the right
13556 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000013557
13558 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000013559 if (VT.getSizeInBits() != 64)
13560 return SDValue();
13561
Devang Patel578efa92009-06-05 21:57:13 +000013562 const Function *F = DAG.getMachineFunction().getFunction();
13563 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000013564 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000013565 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000013566 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000013567 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000013568 isa<LoadSDNode>(St->getValue()) &&
13569 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
13570 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013571 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013572 LoadSDNode *Ld = 0;
13573 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000013574 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000013575 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013576 // Must be a store of a load. We currently handle two cases: the load
13577 // is a direct child, and it's under an intervening TokenFactor. It is
13578 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000013579 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000013580 Ld = cast<LoadSDNode>(St->getChain());
13581 else if (St->getValue().hasOneUse() &&
13582 ChainVal->getOpcode() == ISD::TokenFactor) {
13583 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013584 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000013585 TokenFactorIndex = i;
13586 Ld = cast<LoadSDNode>(St->getValue());
13587 } else
13588 Ops.push_back(ChainVal->getOperand(i));
13589 }
13590 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000013591
Evan Cheng536e6672009-03-12 05:59:15 +000013592 if (!Ld || !ISD::isNormalLoad(Ld))
13593 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013594
Evan Cheng536e6672009-03-12 05:59:15 +000013595 // If this is not the MMX case, i.e. we are just turning i64 load/store
13596 // into f64 load/store, avoid the transformation if there are multiple
13597 // uses of the loaded value.
13598 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
13599 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013600
Evan Cheng536e6672009-03-12 05:59:15 +000013601 DebugLoc LdDL = Ld->getDebugLoc();
13602 DebugLoc StDL = N->getDebugLoc();
13603 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
13604 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
13605 // pair instead.
13606 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013607 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000013608 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
13609 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013610 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013611 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000013612 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000013613 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000013614 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000013615 Ops.size());
13616 }
Evan Cheng536e6672009-03-12 05:59:15 +000013617 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013618 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013619 St->isVolatile(), St->isNonTemporal(),
13620 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000013621 }
Evan Cheng536e6672009-03-12 05:59:15 +000013622
13623 // Otherwise, lower to two pairs of 32-bit loads / stores.
13624 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013625 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
13626 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013627
Owen Anderson825b72b2009-08-11 20:47:22 +000013628 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013629 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013630 Ld->isVolatile(), Ld->isNonTemporal(),
13631 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000013632 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013633 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000013634 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013635 MinAlign(Ld->getAlignment(), 4));
13636
13637 SDValue NewChain = LoLd.getValue(1);
13638 if (TokenFactorIndex != -1) {
13639 Ops.push_back(LoLd);
13640 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000013641 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000013642 Ops.size());
13643 }
13644
13645 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013646 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
13647 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013648
13649 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013650 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013651 St->isVolatile(), St->isNonTemporal(),
13652 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013653 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013654 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000013655 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013656 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013657 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000013658 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000013659 }
Dan Gohman475871a2008-07-27 21:46:04 +000013660 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000013661}
13662
Chris Lattner6cf73262008-01-25 06:14:17 +000013663/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
13664/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013665static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000013666 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
13667 // F[X]OR(0.0, x) -> x
13668 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000013669 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13670 if (C->getValueAPF().isPosZero())
13671 return N->getOperand(1);
13672 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13673 if (C->getValueAPF().isPosZero())
13674 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000013675 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013676}
13677
13678/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013679static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000013680 // FAND(0.0, x) -> 0.0
13681 // FAND(x, 0.0) -> 0.0
13682 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13683 if (C->getValueAPF().isPosZero())
13684 return N->getOperand(0);
13685 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13686 if (C->getValueAPF().isPosZero())
13687 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000013688 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013689}
13690
Dan Gohmane5af2d32009-01-29 01:59:02 +000013691static SDValue PerformBTCombine(SDNode *N,
13692 SelectionDAG &DAG,
13693 TargetLowering::DAGCombinerInfo &DCI) {
13694 // BT ignores high bits in the bit index operand.
13695 SDValue Op1 = N->getOperand(1);
13696 if (Op1.hasOneUse()) {
13697 unsigned BitWidth = Op1.getValueSizeInBits();
13698 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
13699 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013700 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
13701 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000013702 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000013703 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
13704 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
13705 DCI.CommitTargetLoweringOpt(TLO);
13706 }
13707 return SDValue();
13708}
Chris Lattner83e6c992006-10-04 06:57:07 +000013709
Eli Friedman7a5e5552009-06-07 06:52:44 +000013710static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
13711 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013712 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000013713 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000013714 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000013715 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000013716 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000013717 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013718 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013719 }
13720 return SDValue();
13721}
13722
Evan Cheng2e489c42009-12-16 00:53:11 +000013723static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
13724 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
13725 // (and (i32 x86isd::setcc_carry), 1)
13726 // This eliminates the zext. This transformation is necessary because
13727 // ISD::SETCC is always legalized to i8.
13728 DebugLoc dl = N->getDebugLoc();
13729 SDValue N0 = N->getOperand(0);
13730 EVT VT = N->getValueType(0);
13731 if (N0.getOpcode() == ISD::AND &&
13732 N0.hasOneUse() &&
13733 N0.getOperand(0).hasOneUse()) {
13734 SDValue N00 = N0.getOperand(0);
13735 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
13736 return SDValue();
13737 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
13738 if (!C || C->getZExtValue() != 1)
13739 return SDValue();
13740 return DAG.getNode(ISD::AND, dl, VT,
13741 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
13742 N00.getOperand(0), N00.getOperand(1)),
13743 DAG.getConstant(1, VT));
13744 }
13745
13746 return SDValue();
13747}
13748
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013749// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
13750static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
13751 unsigned X86CC = N->getConstantOperandVal(0);
13752 SDValue EFLAG = N->getOperand(1);
13753 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013754
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013755 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
13756 // a zext and produces an all-ones bit which is more useful than 0/1 in some
13757 // cases.
13758 if (X86CC == X86::COND_B)
13759 return DAG.getNode(ISD::AND, DL, MVT::i8,
13760 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
13761 DAG.getConstant(X86CC, MVT::i8), EFLAG),
13762 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013763
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013764 return SDValue();
13765}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013766
Benjamin Kramer1396c402011-06-18 11:09:41 +000013767static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
13768 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013769 SDValue Op0 = N->getOperand(0);
13770 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
13771 // a 32-bit target where SSE doesn't support i64->FP operations.
13772 if (Op0.getOpcode() == ISD::LOAD) {
13773 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
13774 EVT VT = Ld->getValueType(0);
13775 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
13776 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
13777 !XTLI->getSubtarget()->is64Bit() &&
13778 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000013779 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
13780 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013781 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
13782 return FILDChain;
13783 }
13784 }
13785 return SDValue();
13786}
13787
Chris Lattner23a01992010-12-20 01:37:09 +000013788// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
13789static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
13790 X86TargetLowering::DAGCombinerInfo &DCI) {
13791 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
13792 // the result is either zero or one (depending on the input carry bit).
13793 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
13794 if (X86::isZeroNode(N->getOperand(0)) &&
13795 X86::isZeroNode(N->getOperand(1)) &&
13796 // We don't have a good way to replace an EFLAGS use, so only do this when
13797 // dead right now.
13798 SDValue(N, 1).use_empty()) {
13799 DebugLoc DL = N->getDebugLoc();
13800 EVT VT = N->getValueType(0);
13801 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
13802 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
13803 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
13804 DAG.getConstant(X86::COND_B,MVT::i8),
13805 N->getOperand(2)),
13806 DAG.getConstant(1, VT));
13807 return DCI.CombineTo(N, Res1, CarryOut);
13808 }
13809
13810 return SDValue();
13811}
13812
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013813// fold (add Y, (sete X, 0)) -> adc 0, Y
13814// (add Y, (setne X, 0)) -> sbb -1, Y
13815// (sub (sete X, 0), Y) -> sbb 0, Y
13816// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013817static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013818 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013819
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013820 // Look through ZExts.
13821 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
13822 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
13823 return SDValue();
13824
13825 SDValue SetCC = Ext.getOperand(0);
13826 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
13827 return SDValue();
13828
13829 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
13830 if (CC != X86::COND_E && CC != X86::COND_NE)
13831 return SDValue();
13832
13833 SDValue Cmp = SetCC.getOperand(1);
13834 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000013835 !X86::isZeroNode(Cmp.getOperand(1)) ||
13836 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013837 return SDValue();
13838
13839 SDValue CmpOp0 = Cmp.getOperand(0);
13840 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
13841 DAG.getConstant(1, CmpOp0.getValueType()));
13842
13843 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
13844 if (CC == X86::COND_NE)
13845 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
13846 DL, OtherVal.getValueType(), OtherVal,
13847 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
13848 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
13849 DL, OtherVal.getValueType(), OtherVal,
13850 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
13851}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013852
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013853static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
13854 SDValue Op0 = N->getOperand(0);
13855 SDValue Op1 = N->getOperand(1);
13856
13857 // X86 can't encode an immediate LHS of a sub. See if we can push the
13858 // negation into a preceding instruction.
13859 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013860 // If the RHS of the sub is a XOR with one use and a constant, invert the
13861 // immediate. Then add one to the LHS of the sub so we can turn
13862 // X-Y -> X+~Y+1, saving one register.
13863 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
13864 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000013865 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013866 EVT VT = Op0.getValueType();
13867 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
13868 Op1.getOperand(0),
13869 DAG.getConstant(~XorC, VT));
13870 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000013871 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013872 }
13873 }
13874
13875 return OptimizeConditionalInDecrement(N, DAG);
13876}
13877
Dan Gohman475871a2008-07-27 21:46:04 +000013878SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000013879 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013880 SelectionDAG &DAG = DCI.DAG;
13881 switch (N->getOpcode()) {
13882 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013883 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013884 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000013885 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013886 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013887 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
13888 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000013889 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000013890 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000013891 case ISD::SHL:
13892 case ISD::SRA:
13893 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000013894 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000013895 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000013896 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013897 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000013898 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000013899 case X86ISD::FOR: return PerformFORCombine(N, DAG);
13900 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000013901 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013902 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000013903 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013904 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013905 case X86ISD::SHUFPS: // Handle all target specific shuffles
13906 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000013907 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013908 case X86ISD::PUNPCKHBW:
13909 case X86ISD::PUNPCKHWD:
13910 case X86ISD::PUNPCKHDQ:
13911 case X86ISD::PUNPCKHQDQ:
13912 case X86ISD::UNPCKHPS:
13913 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000013914 case X86ISD::VUNPCKHPSY:
13915 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013916 case X86ISD::PUNPCKLBW:
13917 case X86ISD::PUNPCKLWD:
13918 case X86ISD::PUNPCKLDQ:
13919 case X86ISD::PUNPCKLQDQ:
13920 case X86ISD::UNPCKLPS:
13921 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000013922 case X86ISD::VUNPCKLPSY:
13923 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013924 case X86ISD::MOVHLPS:
13925 case X86ISD::MOVLHPS:
13926 case X86ISD::PSHUFD:
13927 case X86ISD::PSHUFHW:
13928 case X86ISD::PSHUFLW:
13929 case X86ISD::MOVSS:
13930 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000013931 case X86ISD::VPERMILPS:
13932 case X86ISD::VPERMILPSY:
13933 case X86ISD::VPERMILPD:
13934 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000013935 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013936 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013937 }
13938
Dan Gohman475871a2008-07-27 21:46:04 +000013939 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013940}
13941
Evan Chenge5b51ac2010-04-17 06:13:15 +000013942/// isTypeDesirableForOp - Return true if the target has native support for
13943/// the specified value type and it is 'desirable' to use the type for the
13944/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
13945/// instruction encodings are longer and some i16 instructions are slow.
13946bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
13947 if (!isTypeLegal(VT))
13948 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013949 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000013950 return true;
13951
13952 switch (Opc) {
13953 default:
13954 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000013955 case ISD::LOAD:
13956 case ISD::SIGN_EXTEND:
13957 case ISD::ZERO_EXTEND:
13958 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000013959 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000013960 case ISD::SRL:
13961 case ISD::SUB:
13962 case ISD::ADD:
13963 case ISD::MUL:
13964 case ISD::AND:
13965 case ISD::OR:
13966 case ISD::XOR:
13967 return false;
13968 }
13969}
13970
13971/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000013972/// beneficial for dag combiner to promote the specified node. If true, it
13973/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000013974bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000013975 EVT VT = Op.getValueType();
13976 if (VT != MVT::i16)
13977 return false;
13978
Evan Cheng4c26e932010-04-19 19:29:22 +000013979 bool Promote = false;
13980 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000013981 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000013982 default: break;
13983 case ISD::LOAD: {
13984 LoadSDNode *LD = cast<LoadSDNode>(Op);
13985 // If the non-extending load has a single use and it's not live out, then it
13986 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013987 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
13988 Op.hasOneUse()*/) {
13989 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
13990 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
13991 // The only case where we'd want to promote LOAD (rather then it being
13992 // promoted as an operand is when it's only use is liveout.
13993 if (UI->getOpcode() != ISD::CopyToReg)
13994 return false;
13995 }
13996 }
Evan Cheng4c26e932010-04-19 19:29:22 +000013997 Promote = true;
13998 break;
13999 }
14000 case ISD::SIGN_EXTEND:
14001 case ISD::ZERO_EXTEND:
14002 case ISD::ANY_EXTEND:
14003 Promote = true;
14004 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014005 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014006 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014007 SDValue N0 = Op.getOperand(0);
14008 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014009 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014010 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014011 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014012 break;
14013 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014014 case ISD::ADD:
14015 case ISD::MUL:
14016 case ISD::AND:
14017 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014018 case ISD::XOR:
14019 Commute = true;
14020 // fallthrough
14021 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014022 SDValue N0 = Op.getOperand(0);
14023 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014024 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014025 return false;
14026 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014027 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014028 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014029 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014030 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014031 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014032 }
14033 }
14034
14035 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014036 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014037}
14038
Evan Cheng60c07e12006-07-05 22:17:51 +000014039//===----------------------------------------------------------------------===//
14040// X86 Inline Assembly Support
14041//===----------------------------------------------------------------------===//
14042
Chris Lattnerb8105652009-07-20 17:51:36 +000014043bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14044 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014045
14046 std::string AsmStr = IA->getAsmString();
14047
14048 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014049 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014050 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014051
14052 switch (AsmPieces.size()) {
14053 default: return false;
14054 case 1:
14055 AsmStr = AsmPieces[0];
14056 AsmPieces.clear();
14057 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14058
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014059 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014060 // we will turn this bswap into something that will be lowered to logical ops
14061 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14062 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014063 // bswap $0
14064 if (AsmPieces.size() == 2 &&
14065 (AsmPieces[0] == "bswap" ||
14066 AsmPieces[0] == "bswapq" ||
14067 AsmPieces[0] == "bswapl") &&
14068 (AsmPieces[1] == "$0" ||
14069 AsmPieces[1] == "${0:q}")) {
14070 // No need to check constraints, nothing other than the equivalent of
14071 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014072 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014073 if (!Ty || Ty->getBitWidth() % 16 != 0)
14074 return false;
14075 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014076 }
14077 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014078 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014079 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014080 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014081 AsmPieces[1] == "$$8," &&
14082 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014083 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14084 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014085 const std::string &ConstraintsStr = IA->getConstraintString();
14086 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014087 std::sort(AsmPieces.begin(), AsmPieces.end());
14088 if (AsmPieces.size() == 4 &&
14089 AsmPieces[0] == "~{cc}" &&
14090 AsmPieces[1] == "~{dirflag}" &&
14091 AsmPieces[2] == "~{flags}" &&
14092 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014093 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014094 if (!Ty || Ty->getBitWidth() % 16 != 0)
14095 return false;
14096 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014097 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014098 }
14099 break;
14100 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014101 if (CI->getType()->isIntegerTy(32) &&
14102 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14103 SmallVector<StringRef, 4> Words;
14104 SplitString(AsmPieces[0], Words, " \t,");
14105 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14106 Words[2] == "${0:w}") {
14107 Words.clear();
14108 SplitString(AsmPieces[1], Words, " \t,");
14109 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14110 Words[2] == "$0") {
14111 Words.clear();
14112 SplitString(AsmPieces[2], Words, " \t,");
14113 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14114 Words[2] == "${0:w}") {
14115 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014116 const std::string &ConstraintsStr = IA->getConstraintString();
14117 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014118 std::sort(AsmPieces.begin(), AsmPieces.end());
14119 if (AsmPieces.size() == 4 &&
14120 AsmPieces[0] == "~{cc}" &&
14121 AsmPieces[1] == "~{dirflag}" &&
14122 AsmPieces[2] == "~{flags}" &&
14123 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014124 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014125 if (!Ty || Ty->getBitWidth() % 16 != 0)
14126 return false;
14127 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014128 }
14129 }
14130 }
14131 }
14132 }
Evan Cheng55d42002011-01-08 01:24:27 +000014133
14134 if (CI->getType()->isIntegerTy(64)) {
14135 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14136 if (Constraints.size() >= 2 &&
14137 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14138 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14139 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14140 SmallVector<StringRef, 4> Words;
14141 SplitString(AsmPieces[0], Words, " \t");
14142 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014143 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014144 SplitString(AsmPieces[1], Words, " \t");
14145 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14146 Words.clear();
14147 SplitString(AsmPieces[2], Words, " \t,");
14148 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14149 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014150 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014151 if (!Ty || Ty->getBitWidth() % 16 != 0)
14152 return false;
14153 return IntrinsicLowering::LowerToByteSwap(CI);
14154 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014155 }
14156 }
14157 }
14158 }
14159 break;
14160 }
14161 return false;
14162}
14163
14164
14165
Chris Lattnerf4dff842006-07-11 02:54:03 +000014166/// getConstraintType - Given a constraint letter, return the type of
14167/// constraint it is for this target.
14168X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014169X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14170 if (Constraint.size() == 1) {
14171 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014172 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014173 case 'q':
14174 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014175 case 'f':
14176 case 't':
14177 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014178 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014179 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014180 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014181 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014182 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014183 case 'a':
14184 case 'b':
14185 case 'c':
14186 case 'd':
14187 case 'S':
14188 case 'D':
14189 case 'A':
14190 return C_Register;
14191 case 'I':
14192 case 'J':
14193 case 'K':
14194 case 'L':
14195 case 'M':
14196 case 'N':
14197 case 'G':
14198 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014199 case 'e':
14200 case 'Z':
14201 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014202 default:
14203 break;
14204 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014205 }
Chris Lattner4234f572007-03-25 02:14:49 +000014206 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014207}
14208
John Thompson44ab89e2010-10-29 17:29:13 +000014209/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014210/// This object must already have been set up with the operand type
14211/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014212TargetLowering::ConstraintWeight
14213 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014214 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014215 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014216 Value *CallOperandVal = info.CallOperandVal;
14217 // If we don't have a value, we can't do a match,
14218 // but allow it at the lowest weight.
14219 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014220 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014221 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014222 // Look at the constraint type.
14223 switch (*constraint) {
14224 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014225 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14226 case 'R':
14227 case 'q':
14228 case 'Q':
14229 case 'a':
14230 case 'b':
14231 case 'c':
14232 case 'd':
14233 case 'S':
14234 case 'D':
14235 case 'A':
14236 if (CallOperandVal->getType()->isIntegerTy())
14237 weight = CW_SpecificReg;
14238 break;
14239 case 'f':
14240 case 't':
14241 case 'u':
14242 if (type->isFloatingPointTy())
14243 weight = CW_SpecificReg;
14244 break;
14245 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014246 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014247 weight = CW_SpecificReg;
14248 break;
14249 case 'x':
14250 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014251 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014252 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014253 break;
14254 case 'I':
14255 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14256 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014257 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014258 }
14259 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014260 case 'J':
14261 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14262 if (C->getZExtValue() <= 63)
14263 weight = CW_Constant;
14264 }
14265 break;
14266 case 'K':
14267 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14268 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14269 weight = CW_Constant;
14270 }
14271 break;
14272 case 'L':
14273 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14274 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14275 weight = CW_Constant;
14276 }
14277 break;
14278 case 'M':
14279 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14280 if (C->getZExtValue() <= 3)
14281 weight = CW_Constant;
14282 }
14283 break;
14284 case 'N':
14285 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14286 if (C->getZExtValue() <= 0xff)
14287 weight = CW_Constant;
14288 }
14289 break;
14290 case 'G':
14291 case 'C':
14292 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14293 weight = CW_Constant;
14294 }
14295 break;
14296 case 'e':
14297 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14298 if ((C->getSExtValue() >= -0x80000000LL) &&
14299 (C->getSExtValue() <= 0x7fffffffLL))
14300 weight = CW_Constant;
14301 }
14302 break;
14303 case 'Z':
14304 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14305 if (C->getZExtValue() <= 0xffffffff)
14306 weight = CW_Constant;
14307 }
14308 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014309 }
14310 return weight;
14311}
14312
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014313/// LowerXConstraint - try to replace an X constraint, which matches anything,
14314/// with another that has more specific requirements based on the type of the
14315/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014316const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014317LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014318 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14319 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014320 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014321 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014322 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014323 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014324 return "x";
14325 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014326
Chris Lattner5e764232008-04-26 23:02:14 +000014327 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014328}
14329
Chris Lattner48884cd2007-08-25 00:47:38 +000014330/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14331/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014332void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014333 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014334 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014335 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014336 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014337
Eric Christopher100c8332011-06-02 23:16:42 +000014338 // Only support length 1 constraints for now.
14339 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014340
Eric Christopher100c8332011-06-02 23:16:42 +000014341 char ConstraintLetter = Constraint[0];
14342 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014343 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014344 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014345 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014346 if (C->getZExtValue() <= 31) {
14347 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014348 break;
14349 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014350 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014351 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014352 case 'J':
14353 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014354 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014355 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14356 break;
14357 }
14358 }
14359 return;
14360 case 'K':
14361 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014362 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000014363 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14364 break;
14365 }
14366 }
14367 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000014368 case 'N':
14369 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014370 if (C->getZExtValue() <= 255) {
14371 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014372 break;
14373 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000014374 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014375 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000014376 case 'e': {
14377 // 32-bit signed value
14378 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014379 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14380 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014381 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014382 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000014383 break;
14384 }
14385 // FIXME gcc accepts some relocatable values here too, but only in certain
14386 // memory models; it's complicated.
14387 }
14388 return;
14389 }
14390 case 'Z': {
14391 // 32-bit unsigned value
14392 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014393 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14394 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014395 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14396 break;
14397 }
14398 }
14399 // FIXME gcc accepts some relocatable values here too, but only in certain
14400 // memory models; it's complicated.
14401 return;
14402 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014403 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014404 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000014405 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014406 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014407 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000014408 break;
14409 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014410
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014411 // In any sort of PIC mode addresses need to be computed at runtime by
14412 // adding in a register or some sort of table lookup. These can't
14413 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000014414 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014415 return;
14416
Chris Lattnerdc43a882007-05-03 16:52:29 +000014417 // If we are in non-pic codegen mode, we allow the address of a global (with
14418 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000014419 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014420 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000014421
Chris Lattner49921962009-05-08 18:23:14 +000014422 // Match either (GA), (GA+C), (GA+C1+C2), etc.
14423 while (1) {
14424 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
14425 Offset += GA->getOffset();
14426 break;
14427 } else if (Op.getOpcode() == ISD::ADD) {
14428 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14429 Offset += C->getZExtValue();
14430 Op = Op.getOperand(0);
14431 continue;
14432 }
14433 } else if (Op.getOpcode() == ISD::SUB) {
14434 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14435 Offset += -C->getZExtValue();
14436 Op = Op.getOperand(0);
14437 continue;
14438 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014439 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014440
Chris Lattner49921962009-05-08 18:23:14 +000014441 // Otherwise, this isn't something we can handle, reject it.
14442 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014443 }
Eric Christopherfd179292009-08-27 18:07:15 +000014444
Dan Gohman46510a72010-04-15 01:51:59 +000014445 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014446 // If we require an extra load to get this address, as in PIC mode, we
14447 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000014448 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
14449 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014450 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000014451
Devang Patel0d881da2010-07-06 22:08:15 +000014452 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
14453 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000014454 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014455 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014456 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014457
Gabor Greifba36cb52008-08-28 21:40:38 +000014458 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000014459 Ops.push_back(Result);
14460 return;
14461 }
Dale Johannesen1784d162010-06-25 21:55:36 +000014462 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014463}
14464
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014465std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000014466X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000014467 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000014468 // First, see if this is a constraint that directly corresponds to an LLVM
14469 // register class.
14470 if (Constraint.size() == 1) {
14471 // GCC Constraint Letters
14472 switch (Constraint[0]) {
14473 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000014474 // TODO: Slight differences here in allocation order and leaving
14475 // RIP in the class. Do they matter any more here than they do
14476 // in the normal allocation?
14477 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
14478 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014479 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014480 return std::make_pair(0U, X86::GR32RegisterClass);
14481 else if (VT == MVT::i16)
14482 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014483 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014484 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014485 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000014486 return std::make_pair(0U, X86::GR64RegisterClass);
14487 break;
14488 }
14489 // 32-bit fallthrough
14490 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014491 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014492 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
14493 else if (VT == MVT::i16)
14494 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014495 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014496 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
14497 else if (VT == MVT::i64)
14498 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
14499 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014500 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000014501 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014502 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000014503 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014504 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000014505 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000014506 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000014507 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000014508 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014509 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014510 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014511 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
14512 if (VT == MVT::i16)
14513 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
14514 if (VT == MVT::i32 || !Subtarget->is64Bit())
14515 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
14516 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014517 case 'f': // FP Stack registers.
14518 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
14519 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000014520 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014521 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014522 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014523 return std::make_pair(0U, X86::RFP64RegisterClass);
14524 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000014525 case 'y': // MMX_REGS if MMX allowed.
14526 if (!Subtarget->hasMMX()) break;
14527 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014528 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014529 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014530 // FALL THROUGH.
14531 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014532 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000014533
Owen Anderson825b72b2009-08-11 20:47:22 +000014534 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000014535 default: break;
14536 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014537 case MVT::f32:
14538 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000014539 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014540 case MVT::f64:
14541 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000014542 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014543 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014544 case MVT::v16i8:
14545 case MVT::v8i16:
14546 case MVT::v4i32:
14547 case MVT::v2i64:
14548 case MVT::v4f32:
14549 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000014550 return std::make_pair(0U, X86::VR128RegisterClass);
14551 }
Chris Lattnerad043e82007-04-09 05:11:28 +000014552 break;
14553 }
14554 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014555
Chris Lattnerf76d1802006-07-31 23:26:50 +000014556 // Use the default implementation in TargetLowering to convert the register
14557 // constraint into a member of a register class.
14558 std::pair<unsigned, const TargetRegisterClass*> Res;
14559 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000014560
14561 // Not found as a standard register?
14562 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014563 // Map st(0) -> st(7) -> ST0
14564 if (Constraint.size() == 7 && Constraint[0] == '{' &&
14565 tolower(Constraint[1]) == 's' &&
14566 tolower(Constraint[2]) == 't' &&
14567 Constraint[3] == '(' &&
14568 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
14569 Constraint[5] == ')' &&
14570 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000014571
Chris Lattner56d77c72009-09-13 22:41:48 +000014572 Res.first = X86::ST0+Constraint[4]-'0';
14573 Res.second = X86::RFP80RegisterClass;
14574 return Res;
14575 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014576
Chris Lattner56d77c72009-09-13 22:41:48 +000014577 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014578 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000014579 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000014580 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014581 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000014582 }
Chris Lattner56d77c72009-09-13 22:41:48 +000014583
14584 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014585 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014586 Res.first = X86::EFLAGS;
14587 Res.second = X86::CCRRegisterClass;
14588 return Res;
14589 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014590
Dale Johannesen330169f2008-11-13 21:52:36 +000014591 // 'A' means EAX + EDX.
14592 if (Constraint == "A") {
14593 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000014594 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014595 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000014596 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000014597 return Res;
14598 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014599
Chris Lattnerf76d1802006-07-31 23:26:50 +000014600 // Otherwise, check to see if this is a register class of the wrong value
14601 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
14602 // turn into {ax},{dx}.
14603 if (Res.second->hasType(VT))
14604 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014605
Chris Lattnerf76d1802006-07-31 23:26:50 +000014606 // All of the single-register GCC register classes map their values onto
14607 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
14608 // really want an 8-bit or 32-bit register, map to the appropriate register
14609 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000014610 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014611 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014612 unsigned DestReg = 0;
14613 switch (Res.first) {
14614 default: break;
14615 case X86::AX: DestReg = X86::AL; break;
14616 case X86::DX: DestReg = X86::DL; break;
14617 case X86::CX: DestReg = X86::CL; break;
14618 case X86::BX: DestReg = X86::BL; break;
14619 }
14620 if (DestReg) {
14621 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014622 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014623 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014624 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014625 unsigned DestReg = 0;
14626 switch (Res.first) {
14627 default: break;
14628 case X86::AX: DestReg = X86::EAX; break;
14629 case X86::DX: DestReg = X86::EDX; break;
14630 case X86::CX: DestReg = X86::ECX; break;
14631 case X86::BX: DestReg = X86::EBX; break;
14632 case X86::SI: DestReg = X86::ESI; break;
14633 case X86::DI: DestReg = X86::EDI; break;
14634 case X86::BP: DestReg = X86::EBP; break;
14635 case X86::SP: DestReg = X86::ESP; break;
14636 }
14637 if (DestReg) {
14638 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014639 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014640 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014641 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014642 unsigned DestReg = 0;
14643 switch (Res.first) {
14644 default: break;
14645 case X86::AX: DestReg = X86::RAX; break;
14646 case X86::DX: DestReg = X86::RDX; break;
14647 case X86::CX: DestReg = X86::RCX; break;
14648 case X86::BX: DestReg = X86::RBX; break;
14649 case X86::SI: DestReg = X86::RSI; break;
14650 case X86::DI: DestReg = X86::RDI; break;
14651 case X86::BP: DestReg = X86::RBP; break;
14652 case X86::SP: DestReg = X86::RSP; break;
14653 }
14654 if (DestReg) {
14655 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014656 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014657 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000014658 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000014659 } else if (Res.second == X86::FR32RegisterClass ||
14660 Res.second == X86::FR64RegisterClass ||
14661 Res.second == X86::VR128RegisterClass) {
14662 // Handle references to XMM physical registers that got mapped into the
14663 // wrong class. This can happen with constraints like {xmm0} where the
14664 // target independent register mapper will just pick the first match it can
14665 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000014666 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014667 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000014668 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014669 Res.second = X86::FR64RegisterClass;
14670 else if (X86::VR128RegisterClass->hasType(VT))
14671 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000014672 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014673
Chris Lattnerf76d1802006-07-31 23:26:50 +000014674 return Res;
14675}